Formula.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Session;
  5. use think\Db;
  6. use think\Log;
  7. use pinyin\Pinyin;
  8. /**
  9. * 配方管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Formula extends Backend
  14. {
  15. /**
  16. * Formula模型对象
  17. * @var \app\admin\model\Formula
  18. */
  19. protected $model = null;
  20. protected $searchFields = "id,name";//搜索查询字段
  21. protected $noNeedLogin = ['gyName',"addFormulaChinese"];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Formula;
  26. $this->view->assign("examineStatusList", $this->model->getExamineStatusList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. // $this->view->assign("gyNameList", \app\admin\model\GyName::select());
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. *
  38. * @return string|Json
  39. * @throws \think\Exception
  40. * @throws DbException
  41. */
  42. public function index()
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags', 'trim']);
  46. if (false === $this->request->isAjax()) {
  47. return $this->view->fetch();
  48. }
  49. // 获取搜索框的值
  50. $map = [];
  51. $filter=input('filter');
  52. if($filter){
  53. $filter=urldecode($filter);
  54. $filter=json_decode($filter,TRUE);
  55. foreach($filter as $k=>$v){
  56. $map[$k]=['like',"%{$v}%"];
  57. }
  58. }
  59. //如果发送的来源是 Selectpage,则转发到 Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  64. $user_info = Session::get('admin');
  65. if ($user_info['id'] !== 1){//管理员
  66. $map['company_id'] = $user_info['company_id'];
  67. $map['examine_status'] = 2;
  68. }
  69. if(!empty($map['name'])||!empty($map['material'])){
  70. $sort = 'f.id';
  71. $list = DB::name('formula')
  72. ->where($where)
  73. ->where($map)
  74. ->alias('f')
  75. ->group('f.id')
  76. ->field('f.id,f.name,f.version,f.examine_status,f.create')
  77. ->join('formula_detail fd','fd.pid=f.id','left')
  78. ->order($sort, $order)
  79. ->paginate($limit);
  80. }else{
  81. $list = $this->model
  82. ->where($where)
  83. ->where($map)
  84. ->order($sort, $order)
  85. ->paginate($limit);
  86. }
  87. $result = ['total' => $list->total(), 'rows' => $list->items()];
  88. return json($result);
  89. }
  90. /**
  91. * 添加
  92. *
  93. * @return string
  94. * @throws \think\Exception
  95. */
  96. public function add()
  97. {
  98. if (false === $this->request->isPost()) {
  99. //技术部=>担当人
  100. $jsdd = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jsdd")->order('name desc')->select();
  101. //技术部=>审核人
  102. $jssh = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jssh")->order('name desc')->select();
  103. $this->assign('jsdd',$jsdd);
  104. $this->assign('jssh',$jssh);
  105. return $this->view->fetch();
  106. }
  107. $base = $this->request->post('baseData/a');
  108. $formula = $this->request->post('formulaData/a');
  109. if (empty($base) || empty($formula)){
  110. $this->error('数据不能为空');
  111. }
  112. $params = [];
  113. $params['name'] = $base[0];
  114. $params['no'] = $base[1];
  115. $params['charge_name'] = $base[2];
  116. $params['examine_name'] = $base[3];
  117. $params['remark'] = $base[4];
  118. $params['version'] = $base[5];
  119. $params['date'] = $base[6];
  120. $params['model'] = $base[8];
  121. $params['controlled_one'] = $base[9];
  122. $params['controlled_two'] = $base[10];
  123. if ($base[7] !== 99){
  124. $customer= explode(',',$base[7]);
  125. // print_r($base[7]);die;
  126. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  127. $usability = implode(',',$customer_id);
  128. }
  129. // $order = Db::name('order')->order('desc id')->find();
  130. $params['usability'] = $usability;
  131. $params['create'] = date('Y-m-d H:i:s');
  132. $user_info = Session::get('admin');
  133. $params['user_id'] = $user_info['id'];
  134. $params['company_id'] = $user_info['company_id'];
  135. $result = false;
  136. Db::startTrans();
  137. try {
  138. $pid = Db::name('formula')->insertGetId($params);
  139. if (!$pid){
  140. Db::rollback();
  141. $this->error('数据未插入,请重新操作');
  142. }
  143. $data = [];
  144. $is_replace = 0;
  145. // $gy_num = 1;
  146. foreach($formula as $key=>$value){
  147. if (strpos($value[0],'/') === false){
  148. $data[$key]['is_replace'] = 0;
  149. }else{
  150. $data[$key]['is_replace'] = 1;
  151. $is_replace = 1;
  152. }
  153. $data[$key]['material'] = $value[0];
  154. $data[$key]['percentage'] = encrypt($value[1]);
  155. $data[$key]['gy_name'] = $value[2];
  156. // $data[$key]['gy_num'] = $gy_num;
  157. $data[$key]['gy_num'] = $value[3];
  158. $data[$key]['pid'] = $pid;
  159. $data[$key]['version'] = $params['version'];
  160. $data[$key]['create'] = $params['create'];
  161. // if(!$value[0]&&!$value[1]&&$value[2]){
  162. // $gy_num++;
  163. // }
  164. }
  165. $result = Db::name('formula_detail')->insertAll($data);
  166. //有替代料的话 查出所有替代料 然后分解 插入到数据库 2022年9月19日14:20:37 替代料功能改变
  167. // if ($is_replace == 1){
  168. // $replace = Db::name('formula_detail')->where('pid',$pid)->where('is_replace',1)->field('id,material')->select();
  169. // $replaceData = [];
  170. // $j = 0;
  171. // foreach ($replace as $k=>$v){
  172. // $material = explode('/',$v['material']);
  173. // for ($i=0;$i<count($material);$i++){
  174. // $replaceData[$j]['fid'] = $v['id'];
  175. // $replaceData[$j]['material'] = $material[$i];
  176. // $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  177. // $j++;
  178. // }
  179. // }
  180. // $result = Db::name('formula_replace')->insertAll($replaceData);
  181. // }
  182. Db::commit();
  183. } catch (Exception $e) {
  184. Db::rollback();
  185. $this->error($e->getMessage());
  186. }
  187. if ($result === false) {
  188. $this->error(__('No rows were inserted'));
  189. }
  190. $this->success();
  191. }
  192. public function kuodan_data(){
  193. if( $this->request->isPost()){
  194. $ids = $this->request->post('ids/a');
  195. print_r($ids);
  196. exit;
  197. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num,kuodan')->select();
  198. }
  199. }
  200. /**
  201. * 编辑
  202. *
  203. * @param $ids
  204. * @return string
  205. * @throws DbException
  206. * @throws \think\Exception
  207. */
  208. public function edit($ids = null)
  209. {
  210. if (!$ids) {
  211. $this->error(__('No Results were found'));
  212. }
  213. if (false === $this->request->isPost()) {
  214. $ids = input('ids');
  215. $formula = Db::name('formula')->field('charge_name,examine_name')->where('id',"=",$ids)->find();
  216. //查询公司
  217. $user_info = Session::get('admin');
  218. $map = [];
  219. if ($user_info['id'] !== 1){
  220. $map['company_id'] = $user_info['company_id'];
  221. }
  222. $customer_name = Db::name('customer')->field('id,customer_name')->where($map)->find();
  223. $this->assign('customer_name',$customer_name);
  224. $customer = Db::name('customer')->field('customer_name')->select();
  225. $this->assign('customer',$customer);
  226. //查询 经办 审核
  227. $jsdd = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jsdd")->order('name desc')->select();
  228. $jssh = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jssh")->order('name desc')->select();
  229. $personnel_jsdd = Db::name('personnel')->where('name','=',$formula['charge_name'])->find();
  230. $personnel_jssh = Db::name('personnel')->where('name','=',$formula['examine_name'])->find();
  231. $this->assign('jsdd',$jsdd);
  232. $this->assign('jssh',$jssh);
  233. $this->assign('personnel_jsdd',$personnel_jsdd);
  234. $this->assign('personnel_jssh',$personnel_jssh);
  235. $list = Db::name('formula')->where('id',$ids)->find();
  236. //可用性,对应客户名称
  237. if ($list['usability'] != 99){
  238. $customer = explode(',',$list['usability']);
  239. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  240. }
  241. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  242. foreach ($list['gyinfo'] as $key=>$value){
  243. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  244. }
  245. // dump($list);die;
  246. $this->view->assign('ids',$ids);
  247. $this->view->assign('name',$name);
  248. $this->view->assign('row', $list);
  249. return $this->view->fetch();
  250. }
  251. /**
  252. * 厂家变更?
  253. * 原材料变更?
  254. * 百分比变更? ->工艺信息变更生成新配方
  255. * 工艺变更?
  256. * $change 0:未更改工艺信息 1:原材料更改 2:百分比更改 3:操作工艺更改 4:增加或减少工艺信息
  257. */
  258. $base = $this->request->post('baseData/a');
  259. $formula = $this->request->post('formulaData/a');
  260. if (empty($base) || empty($formula)){
  261. $this->error('数据不能为空');
  262. }
  263. $version = Db::name('formula')->where('id',$ids)->order('id desc')->value('version');
  264. $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改
  265. //基础数据
  266. $params = [];
  267. $params['name'] = $base[0];
  268. $params['no'] = $base[1];
  269. $params['charge_name'] = $base[2];
  270. $params['examine_name'] = $base[3];
  271. $params['remark'] = $base[4];
  272. $params['date'] = $base[6];
  273. $params['model'] = $base[8];
  274. $params['controlled_one'] = $base[9];
  275. $params['controlled_two'] = $base[10];
  276. //配方对应客户id(可用性)
  277. if ($base[7] !== 99){
  278. $customer= explode(',',$base[7]);
  279. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  280. $usability = implode(',',$customer_id);
  281. }
  282. $params['usability'] = $usability;
  283. $params['create'] = date('Y-m-d H:i:s');
  284. $user_info = Session::get('admin');
  285. $params['user_id'] = $user_info['id'];
  286. $params['company_id'] = $user_info['company_id'];
  287. $params['update'] = date('Y-m-d H:i:s');
  288. //数据库的工艺数据
  289. $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select();
  290. //提交的工艺数据
  291. $data = [];
  292. $change = 0;
  293. $materialChange = 0; //原材料变更
  294. $percentageChange = 0; //百分比变更
  295. $gy_nameChange = 0; //工艺信息变更
  296. //总工艺已更改,版本号X位直接增加1
  297. if (count($gy_data) != count($formula)){
  298. $change = 1;
  299. }
  300. foreach($formula as $key=>$value){
  301. foreach ($gy_data as $k=>$v){
  302. //比对同一行原材料 百分比 工艺信息
  303. if ($key == $k){
  304. if ($value[0] != $gy_data[$k]['material']){
  305. $materialChange = 1;
  306. }
  307. if ($value[1] != $gy_data[$k]['percentage']){
  308. $percentageChange = 1;
  309. }
  310. if($value[2] != $gy_data[$k]['gy_name']){
  311. $gy_nameChange = 1;
  312. }
  313. }
  314. }
  315. }
  316. //更改版本号
  317. if ($change === 1){
  318. $version = intval($version) + 1;
  319. }else{
  320. if ($materialChange === 1){
  321. $version = intval($version) + 1;
  322. }else{
  323. if ($percentageChange === 1 || $gy_nameChange === 1){
  324. $version = $version + 0.1;
  325. }
  326. }
  327. }
  328. if (is_int($version)){
  329. $version = $version.'.0';
  330. }
  331. //根据版本号的变化来判断工艺有没有变化
  332. if ($version == substr($base[5],1)){
  333. $isChange = false;
  334. }else{
  335. $isChange = true;
  336. }
  337. $params['version'] = 'v'.$version;
  338. $result = true;
  339. Db::startTrans();
  340. try {
  341. if ($isChange === true){
  342. $ids = Db::name('formula')->insertGetId($params);
  343. }else{
  344. Db::name('formula')->where('id',$ids)->update($params);
  345. }
  346. //新工艺信息
  347. $is_replace = 0;
  348. for($i=0;$i<count($formula);$i++){
  349. if (strpos($formula[$i][0],'/') === false){
  350. $data[$i]['is_replace'] = 0;
  351. }else{
  352. $data[$i]['is_replace'] = 1;
  353. $is_replace = 1;
  354. }
  355. $data[$i]['material'] = $formula[$i][0];
  356. $data[$i]['percentage'] = encrypt($formula[$i][1]);
  357. $data[$i]['gy_name'] = $formula[$i][2];
  358. $data[$i]['gy_num'] = $formula[$i][3];
  359. // $data[$i]['gy_num'] = $gy_num;
  360. $data[$i]['pid'] = $ids;
  361. $data[$i]['version'] = 'v'.$version;
  362. $data[$i]['create'] = date('Y-m-d H:i:s');
  363. }
  364. if ($isChange === true){
  365. Db::name('formula_detail')->insertAll($data);
  366. }
  367. //有替代料的话 查出所有替代料 然后分解 插入到数据库 //2022年9月19日14:19:09 替代料更改
  368. // if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){
  369. // $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select();
  370. // $replaceData = [];
  371. // $j = 0;
  372. // foreach ($replace as $k=>$v){
  373. // $material = explode('/',$v['material']);
  374. // for ($i=0;$i<count($material);$i++){
  375. // $replaceData[$j]['fid'] = $v['id'];
  376. // $replaceData[$j]['material'] = $material[$i];
  377. // $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  378. // $j++;
  379. // }
  380. // }
  381. // $result = Db::name('formula_replace')->insertAll($replaceData);
  382. // }
  383. Db::commit();
  384. } catch (Exception $e) {
  385. Db::rollback();
  386. $this->error($e->getMessage());
  387. }
  388. if (false === $result) {
  389. $this->error(__('No rows were updated'));
  390. }
  391. $this->success();
  392. }
  393. //生成作业单
  394. public function task(){
  395. $ids = input('ids');
  396. if (!$ids) {
  397. $this->error(__('No Results were found'));
  398. }
  399. if (false === $this->request->isPost()) {
  400. //生产部=>开票人
  401. $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select();
  402. //生产部=>审核人
  403. $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select();
  404. $this->assign('sckp',$sckp);
  405. $this->assign('scsh',$scsh);
  406. //批次号顺序新增
  407. $tastbach = Db::name('task')->order('bach desc')->find();
  408. $bach = $tastbach['bach'] +1;
  409. $this->view->assign('bach',$bach);
  410. $list = Db::name('formula')->where('id',$ids)->find();
  411. //查询该配方是否生产过
  412. $taskbach = Db::name('task')->where('fid',$list['id'])->order('create desc')->find();
  413. $count = Db::name('task')->where('fid',$list['id'])->count();
  414. if($taskbach){
  415. $erro = '该配方在于最近 '.$taskbach['create'].' 已生产过'.$count.'次';
  416. }else{
  417. $erro = '该配方未生产过';
  418. }
  419. $this->view->assign('erro',$erro);
  420. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  421. foreach ($list['gyinfo'] as $key=>$value){
  422. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  423. }
  424. //新增关联订单
  425. $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select();
  426. $uncompleted_order = array();
  427. if (!empty($order)){
  428. foreach($order as $k=>$v){
  429. $uncompleted_order[$k]['id'] = $v['id'];
  430. if (empty($v['completed'])){
  431. $completed = 0;
  432. }else{
  433. $completed = $v['completed'];
  434. }
  435. $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)';
  436. }
  437. }
  438. $this->view->assign('ids',$ids);
  439. $this->view->assign('order',$uncompleted_order);
  440. $this->view->assign('machineList',\app\admin\model\Machine::select());
  441. $this->view->assign('row', $list);
  442. return $this->view->fetch();
  443. }
  444. $base = $this->request->post('baseData/a');
  445. // print_r($base);die;
  446. if (empty($base)){
  447. $this->error('数据不能为空');
  448. }
  449. //基础数据
  450. $params = [];
  451. $params['fid'] = $base[0];
  452. $params['name'] = $base[1];
  453. $params['bach'] = $base[2];
  454. $params['drawer_name'] = $base[3];
  455. $params['examine_name'] = $base[4];
  456. $params['number'] = $base[5];
  457. $params['remark'] = $base[6];
  458. $params['machine'] = $base[7];
  459. $params['oid'] = $base[8];
  460. $params['kuodan']=$base[9];
  461. $params['create'] = date('Y-m-d H:i:s');
  462. $result = false;
  463. Db::startTrans();
  464. try {
  465. $task_kd = Db::name('task')->where('bach',$base[2])->find();
  466. if (empty($task_kd)) {
  467. // 没有找到匹配的数据,执行插入操作
  468. $result = Db::name('task')->insert($params);
  469. }
  470. $result = true;
  471. // Db::name('order')->where('id',$params['oid'])->setField('status',2);
  472. // $result = Db::name('task')->insert($params);
  473. //更改订单已完成数量,修改订单状态
  474. $order_info = Db::name('order')->where('id',$params['oid'])->find();
  475. if (empty($order_info['completed'])){
  476. $new_completed = $params['number'];
  477. $order_status = 2;//生产中
  478. Db::name('order')->where('id',$params['oid'])->setField('status',$order_status);
  479. }else{
  480. $new_completed = $params['number'] + $order_info['completed'];
  481. }
  482. Db::name('order')->where('id',$params['oid'])->setField('completed',$new_completed);
  483. Db::commit();
  484. } catch (Exception $e) {
  485. Db::rollback();
  486. $this->error($e->getMessage());
  487. }
  488. if (false === $result) {
  489. $this->error(__('No rows were updated'));
  490. }
  491. $this->success();
  492. }
  493. //配方审核列表
  494. public function examine(){
  495. //设置过滤方法
  496. $this->request->filter(['strip_tags', 'trim']);
  497. if (false === $this->request->isAjax()) {
  498. return $this->view->fetch();
  499. }
  500. //如果发送的来源是 Selectpage,则转发到 Selectpage
  501. if ($this->request->request('keyField')) {
  502. return $this->selectpage();
  503. }
  504. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  505. $user_info = Session::get('admin');
  506. $map = [];
  507. $map['examine_status'] = 1;
  508. if ($user_info['id'] !== 1){
  509. $map['company_id'] = $user_info['company_id'];
  510. }
  511. $list = $this->model
  512. ->where($where)
  513. ->where($map)
  514. ->order($sort, $order)
  515. ->paginate($limit);
  516. $result = ['total' => $list->total(), 'rows' => $list->items()];
  517. return json($result);
  518. }
  519. //审核操作
  520. public function status($ids=null){
  521. if (!$ids) {
  522. $this->error(__('No Results were found'));
  523. }
  524. if (false === $this->request->isPost()) {
  525. $list = Db::name('formula')->where('id',$ids)->find();
  526. //可用性,对应客户名称
  527. if ($list['usability'] != 99){
  528. $customer = explode(',',$list['usability']);
  529. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  530. }
  531. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  532. foreach ($list['gyinfo'] as $key=>$value){
  533. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  534. }
  535. $this->view->assign('ids',$ids);
  536. $this->view->assign('name',$name);
  537. $this->view->assign('row', $list);
  538. return $this->view->fetch();
  539. }
  540. $status = $this->request->post('status');
  541. if (!isset($status) || !isset($ids)){
  542. $this->error('审核失败');
  543. }
  544. $params = [];
  545. $user_info = Session::get('admin');
  546. $params['examine_userid'] = $user_info['id'];//审核用户id
  547. $params['examine_status'] = $status;//审核状态
  548. $params['update'] = date('Y-m-d H:i:s');
  549. $result = false;
  550. Db::startTrans();
  551. try {
  552. $list = Db::name('formula')->where('id',$ids)->find();
  553. if ($list['version'] == 'v0.1'){
  554. Db::name('formula')->where('id',$ids)->setField('version','v1.0');
  555. Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0');
  556. }
  557. $result = Db::name('formula')->where('id',$ids)->update($params);
  558. Db::commit();
  559. } catch (Exception $e) {
  560. Db::rollback();
  561. $this->error($e->getMessage());
  562. }
  563. if ($result){
  564. $this->addFormulaChinese($ids);
  565. $this->success('更新成功');
  566. }else{
  567. $this->error('审核失败');
  568. }
  569. }
  570. //审核记录列表
  571. public function examine_status(){
  572. $user_info = Session::get('admin');
  573. $formula = Db::name('formula')->where('examine_userid',$user_info['id'])->order('update desc')
  574. ->paginate(10,false);
  575. $page = $formula->render();
  576. $this->view->assign('page',$page);
  577. $this->view->assign('formula',$formula);
  578. return $this->view->fetch();
  579. }
  580. //审核记录详情列表
  581. public function examine_list(){
  582. $get = $this->request->get();
  583. $formula = Db::name('formula')->where('id',$get['id'])->find();
  584. $formula_detail = Db::name('formula_detail')->where('pid',$get['id'])->select();
  585. foreach ($formula_detail as $key=>$value){
  586. $formula_detail[$key]['percentage'] = decode($value['percentage']);
  587. }
  588. $this->view->assign('formula',$formula);
  589. $this->view->assign('formula_detail',$formula_detail);
  590. return $this->view->fetch();
  591. }
  592. //下拉选择获取客户列表
  593. public function getCustomer(){
  594. $user_info = Session::get('admin');
  595. $where = [];
  596. if ($user_info['id'] !== 1){
  597. $where['company_id'] =$user_info['company_id'];
  598. }
  599. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  600. $result = ['total' => count($row), 'rows' => $row];
  601. return json($result);
  602. }
  603. //获取生产单应加量
  604. public function getNumber(){
  605. $params = input('');
  606. if ($params['kuodan']) {
  607. $task_kd = Db::name('task')->where('bach',$params['bach'])->find();
  608. if ($task_kd) {
  609. $kuodan['kuodan'] = $params['kuodan'];
  610. Db::name('task')->where('bach', $params['bach'])->update($kuodan);
  611. }else{
  612. $this->error('该配方为生成新批次号');
  613. }
  614. }
  615. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  616. return array('status'=>0,'msg'=>'请求参数错误');
  617. }
  618. $list = Db::name('formula')->where('id',$params['ids'])->find();
  619. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  620. if (empty($gyinfo)){
  621. return array('status'=>0,'msg'=>'数据错误');
  622. }
  623. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
  624. if (empty($total)){
  625. return array('status'=>0,'msg'=>'数据错误');
  626. }
  627. foreach ($total as $k=>$v){
  628. $total[$k] = decode($v);
  629. }
  630. $num = array_sum($total);
  631. foreach ($gyinfo as $key=>$value){
  632. if($gyinfo[$key]['gy_name'] == null){
  633. $gyinfo[$key]['gy_name'] = '';
  634. }
  635. $gyinfo[$key]['num'] = '';
  636. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  637. if (!empty($value['percentage'])){
  638. // $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
  639. $number = ceil($gyinfo[$key]['percentage'] / $num * $params['number'] *1000);
  640. $gyinfo[$key]['num'] = number_format($number/1000,3);
  641. $gyinfo[$key]['numm'] = $num;
  642. }
  643. }
  644. $date = date('Y/m/d');
  645. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  646. }
  647. //获取工艺说明
  648. public function gyName(){
  649. $params = input('gy_name');
  650. // print_r($params);die;
  651. if ($params){
  652. $list = Db::name('formula_detail')->where('gy_name','like','%'.$params.'%')->field('id,gy_name')->limit(20)->select();
  653. }else{
  654. $list = Db::name('formula_detail')->where('gy_name','neq','')->field('id,gy_name')->limit(20)->select();
  655. }
  656. $total = count($list);
  657. $result = ['total'=>$total,'rows'=>$list];
  658. // return array('status'=>1,'rows'=>$list);
  659. return json($result);
  660. }
  661. public function addFormulaChinese($ids = null){
  662. $date = date('Ymd',time());
  663. if (!$ids){
  664. Log::mylog('formula_log','id='.$ids.'未获取到',$date.'_01');
  665. }
  666. $formula = Db::name('formula')->where('id',$ids)->field('name')->find();
  667. if (!$formula){
  668. Log::mylog('formula_log','id='.$ids.'未获取到配方数据',$date.'_01');
  669. }
  670. $material = Db::name('formula_detail')->where('pid',$ids)->field('id,material')->select();
  671. if (!$material){
  672. Log::mylog('formula_log','pid='.$ids.'未获取到配方详情数据',$date.'_01');
  673. }
  674. $list = array();
  675. $pinyin = new pinyin();
  676. foreach ($material as $key=>$value){
  677. if (preg_match("/[\x7f-\xff]/", $value['material'])){
  678. $str = $value['material'];
  679. $newStr = $pinyin->getpy($str,true,true);
  680. $chinese = array();
  681. for ($i=0;$i<strlen($newStr);$i++){
  682. $string = substr($newStr,$i,1);
  683. if (!preg_match_all('/\b([a-z]+)\b/', $string)){
  684. $chinese[$i] = $string;
  685. }
  686. }
  687. $list[$key]['f_id'] = $ids;
  688. $list[$key]['f_name'] = $formula['name'];
  689. $list[$key]['m_id'] = $value['id'];
  690. $list[$key]['m_name'] = $value['material'];
  691. $list[$key]['name'] = implode($chinese);
  692. }
  693. }
  694. // dump($list);die;
  695. if (!empty($list)){
  696. $result = true;
  697. $is_has = Db::name('formula_material')->where('f_id',$ids)->select();
  698. if ($is_has){
  699. Db::startTrans();
  700. try {
  701. Db::name('formula_material')->where('f_id',$ids)->delete();
  702. Db::name('formula_material')->insertAll($list);
  703. Db::commit();
  704. }catch (Exception $e){
  705. $result = false;
  706. Db::rollback();
  707. }
  708. }else{
  709. $res = Db::name('formula_material')->insertAll($list);
  710. if (!$res){
  711. $result = false;
  712. }
  713. }
  714. if (!$result){
  715. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入失败',$date.'_01');
  716. }else{
  717. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入成功',$date.'_01');
  718. }
  719. }
  720. }
  721. }