Formula.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. //配方对应客户id(可用性)
  275. if ($base[7] !== 99){
  276. $customer= explode(',',$base[7]);
  277. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  278. $usability = implode(',',$customer_id);
  279. }
  280. $params['usability'] = $usability;
  281. $params['create'] = date('Y-m-d H:i:s');
  282. $user_info = Session::get('admin');
  283. $params['user_id'] = $user_info['id'];
  284. $params['company_id'] = $user_info['company_id'];
  285. $params['update'] = date('Y-m-d H:i:s');
  286. //数据库的工艺数据
  287. $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select();
  288. //提交的工艺数据
  289. $data = [];
  290. $change = 0;
  291. $materialChange = 0; //原材料变更
  292. $percentageChange = 0; //百分比变更
  293. $gy_nameChange = 0; //工艺信息变更
  294. //总工艺已更改,版本号X位直接增加1
  295. if (count($gy_data) != count($formula)){
  296. $change = 1;
  297. }
  298. foreach($formula as $key=>$value){
  299. foreach ($gy_data as $k=>$v){
  300. //比对同一行原材料 百分比 工艺信息
  301. if ($key == $k){
  302. if ($value[0] != $gy_data[$k]['material']){
  303. $materialChange = 1;
  304. }
  305. if ($value[1] != $gy_data[$k]['percentage']){
  306. $percentageChange = 1;
  307. }
  308. if($value[2] != $gy_data[$k]['gy_name']){
  309. $gy_nameChange = 1;
  310. }
  311. }
  312. }
  313. }
  314. //更改版本号
  315. if ($change === 1){
  316. $version = intval($version) + 1;
  317. }else{
  318. if ($materialChange === 1){
  319. $version = intval($version) + 1;
  320. }else{
  321. if ($percentageChange === 1 || $gy_nameChange === 1){
  322. $version = $version + 0.1;
  323. }
  324. }
  325. }
  326. if (is_int($version)){
  327. $version = $version.'.0';
  328. }
  329. //根据版本号的变化来判断工艺有没有变化
  330. if ($version == substr($base[5],1)){
  331. $isChange = false;
  332. }else{
  333. $isChange = true;
  334. }
  335. $params['version'] = 'v'.$version;
  336. $result = true;
  337. Db::startTrans();
  338. try {
  339. if ($isChange === true){
  340. $ids = Db::name('formula')->insertGetId($params);
  341. }else{
  342. Db::name('formula')->where('id',$ids)->update($params);
  343. }
  344. //新工艺信息
  345. $is_replace = 0;
  346. for($i=0;$i<count($formula);$i++){
  347. if (strpos($formula[$i][0],'/') === false){
  348. $data[$i]['is_replace'] = 0;
  349. }else{
  350. $data[$i]['is_replace'] = 1;
  351. $is_replace = 1;
  352. }
  353. $data[$i]['material'] = $formula[$i][0];
  354. $data[$i]['percentage'] = encrypt($formula[$i][1]);
  355. $data[$i]['gy_name'] = $formula[$i][2];
  356. $data[$i]['gy_num'] = $formula[$i][3];
  357. // $data[$i]['gy_num'] = $gy_num;
  358. $data[$i]['pid'] = $ids;
  359. $data[$i]['version'] = 'v'.$version;
  360. $data[$i]['create'] = date('Y-m-d H:i:s');
  361. }
  362. if ($isChange === true){
  363. Db::name('formula_detail')->insertAll($data);
  364. }
  365. //有替代料的话 查出所有替代料 然后分解 插入到数据库 //2022年9月19日14:19:09 替代料更改
  366. // if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){
  367. // $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select();
  368. // $replaceData = [];
  369. // $j = 0;
  370. // foreach ($replace as $k=>$v){
  371. // $material = explode('/',$v['material']);
  372. // for ($i=0;$i<count($material);$i++){
  373. // $replaceData[$j]['fid'] = $v['id'];
  374. // $replaceData[$j]['material'] = $material[$i];
  375. // $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  376. // $j++;
  377. // }
  378. // }
  379. // $result = Db::name('formula_replace')->insertAll($replaceData);
  380. // }
  381. Db::commit();
  382. } catch (Exception $e) {
  383. Db::rollback();
  384. $this->error($e->getMessage());
  385. }
  386. if (false === $result) {
  387. $this->error(__('No rows were updated'));
  388. }
  389. $this->success();
  390. }
  391. //生成作业单
  392. public function task(){
  393. // $ceshi = Db::name('formula_detail')->where('id','>',52459)->limit(10000)->select();
  394. // foreach ($ceshi as $key=>$value){
  395. // $ceshi[$key]['percentage'] = encrypt($value['percentage']);
  396. // }
  397. // Db::name('formula_detail_c')->insertAll($ceshi);
  398. // print_r('-------------------------------');
  399. // die;
  400. //gyinfo
  401. $ids = input('ids');
  402. if (!$ids) {
  403. $this->error(__('No Results were found'));
  404. }
  405. if (false === $this->request->isPost()) {
  406. //生产部=>开票人
  407. $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select();
  408. //生产部=>审核人
  409. $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select();
  410. $this->assign('sckp',$sckp);
  411. $this->assign('scsh',$scsh);
  412. //批次号顺序新增
  413. $tastbach = Db::name('task')->order('bach desc')->find();
  414. $bach = $tastbach['bach'] +1;
  415. $this->view->assign('bach',$bach);
  416. $list = Db::name('formula')->where('id',$ids)->find();
  417. //查询该配方是否生产过
  418. $taskbach = Db::name('task')->where('fid',$list['id'])->order('create desc')->find();
  419. $count = Db::name('task')->where('fid',$list['id'])->count();
  420. if($taskbach){
  421. $erro = '该配方在于最近 '.$taskbach['create'].' 已生产过'.$count.'次';
  422. }else{
  423. $erro = '该配方未生产过';
  424. }
  425. $this->view->assign('erro',$erro);
  426. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num,kuodan')->select();
  427. foreach ($list['gyinfo'] as $key=>$value){
  428. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  429. }
  430. //新增关联订单
  431. $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select();
  432. $uncompleted_order = array();
  433. if (!empty($order)){
  434. foreach($order as $k=>$v){
  435. $uncompleted_order[$k]['id'] = $v['id'];
  436. if (empty($v['completed'])){
  437. $completed = 0;
  438. }else{
  439. $completed = $v['completed'];
  440. }
  441. $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)';
  442. }
  443. }
  444. $this->view->assign('ids',$ids);
  445. $this->view->assign('order',$uncompleted_order);
  446. $this->view->assign('machineList',\app\admin\model\Machine::select());
  447. $this->view->assign('row', $list);
  448. return $this->view->fetch();
  449. }
  450. $base = $this->request->post('baseData/a');
  451. // print_r($base);die;
  452. if (empty($base)){
  453. $this->error('数据不能为空');
  454. }
  455. //基础数据
  456. $params = [];
  457. $params['fid'] = $base[0];
  458. $params['name'] = $base[1];
  459. $params['bach'] = $base[2];
  460. $params['drawer_name'] = $base[3];
  461. $params['examine_name'] = $base[4];
  462. $params['number'] = $base[5];
  463. $params['remark'] = $base[6];
  464. $params['machine'] = $base[7];
  465. $params['oid'] = $base[8];
  466. if(!empty($base[9]))
  467. if($base[9]){
  468. $params['kuodan']=$base[9];
  469. }
  470. // print_r($list2['gyinfo']);
  471. // exit;
  472. //percentage
  473. //$params['kuodan'] = ($base[9]+$params['number'])*;
  474. $params['create'] = date('Y-m-d H:i:s');
  475. $result = false;
  476. Db::startTrans();
  477. try {
  478. // Db::name('order')->where('id',$params['oid'])->setField('status',2);
  479. $result = Db::name('task')->insert($params);
  480. //更改订单已完成数量,修改订单状态
  481. $order_info = Db::name('order')->where('id',$params['oid'])->find();
  482. if (empty($order_info['completed'])){
  483. $new_completed = $params['number'];
  484. $order_status = 2;//生产中
  485. Db::name('order')->where('id',$params['oid'])->setField('status',$order_status);
  486. }else{
  487. $new_completed = $params['number'] + $order_info['completed'];
  488. }
  489. Db::name('order')->where('id',$params['oid'])->setField('completed',$new_completed);
  490. Db::commit();
  491. } catch (Exception $e) {
  492. Db::rollback();
  493. $this->error($e->getMessage());
  494. }
  495. if (false === $result) {
  496. $this->error(__('No rows were updated'));
  497. }
  498. $this->success();
  499. }
  500. //配方审核列表
  501. public function examine(){
  502. //设置过滤方法
  503. $this->request->filter(['strip_tags', 'trim']);
  504. if (false === $this->request->isAjax()) {
  505. return $this->view->fetch();
  506. }
  507. //如果发送的来源是 Selectpage,则转发到 Selectpage
  508. if ($this->request->request('keyField')) {
  509. return $this->selectpage();
  510. }
  511. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  512. $user_info = Session::get('admin');
  513. $map = [];
  514. $map['examine_status'] = 1;
  515. if ($user_info['id'] !== 1){
  516. $map['company_id'] = $user_info['company_id'];
  517. }
  518. $list = $this->model
  519. ->where($where)
  520. ->where($map)
  521. ->order($sort, $order)
  522. ->paginate($limit);
  523. $result = ['total' => $list->total(), 'rows' => $list->items()];
  524. return json($result);
  525. }
  526. //审核操作
  527. public function status($ids=null){
  528. if (!$ids) {
  529. $this->error(__('No Results were found'));
  530. }
  531. if (false === $this->request->isPost()) {
  532. $list = Db::name('formula')->where('id',$ids)->find();
  533. //可用性,对应客户名称
  534. if ($list['usability'] != 99){
  535. $customer = explode(',',$list['usability']);
  536. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  537. }
  538. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  539. foreach ($list['gyinfo'] as $key=>$value){
  540. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  541. }
  542. $this->view->assign('ids',$ids);
  543. $this->view->assign('name',$name);
  544. $this->view->assign('row', $list);
  545. return $this->view->fetch();
  546. }
  547. $status = $this->request->post('status');
  548. if (!isset($status) || !isset($ids)){
  549. $this->error('审核失败');
  550. }
  551. $params = [];
  552. $user_info = Session::get('admin');
  553. $params['examine_userid'] = $user_info['id'];//审核用户id
  554. $params['examine_status'] = $status;//审核状态
  555. $params['update'] = date('Y-m-d H:i:s');
  556. $result = false;
  557. Db::startTrans();
  558. try {
  559. $list = Db::name('formula')->where('id',$ids)->find();
  560. if ($list['version'] == 'v0.1'){
  561. Db::name('formula')->where('id',$ids)->setField('version','v1.0');
  562. Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0');
  563. }
  564. $result = Db::name('formula')->where('id',$ids)->update($params);
  565. Db::commit();
  566. } catch (Exception $e) {
  567. Db::rollback();
  568. $this->error($e->getMessage());
  569. }
  570. if ($result){
  571. $this->addFormulaChinese($ids);
  572. $this->success('更新成功');
  573. }else{
  574. $this->error('审核失败');
  575. }
  576. }
  577. //审核记录列表
  578. public function examine_status(){
  579. $user_info = Session::get('admin');
  580. $formula = Db::name('formula')->where('examine_userid',$user_info['id'])->order('update desc')
  581. ->paginate(10,false);
  582. $page = $formula->render();
  583. $this->view->assign('page',$page);
  584. $this->view->assign('formula',$formula);
  585. return $this->view->fetch();
  586. }
  587. //审核记录详情列表
  588. public function examine_list(){
  589. $get = $this->request->get();
  590. $formula = Db::name('formula')->where('id',$get['id'])->find();
  591. $formula_detail = Db::name('formula_detail')->where('pid',$get['id'])->select();
  592. foreach ($formula_detail as $key=>$value){
  593. $formula_detail[$key]['percentage'] = decode($value['percentage']);
  594. }
  595. $this->view->assign('formula',$formula);
  596. $this->view->assign('formula_detail',$formula_detail);
  597. return $this->view->fetch();
  598. }
  599. //下拉选择获取客户列表
  600. public function getCustomer(){
  601. $user_info = Session::get('admin');
  602. $where = [];
  603. if ($user_info['id'] !== 1){
  604. $where['company_id'] =$user_info['company_id'];
  605. }
  606. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  607. $result = ['total' => count($row), 'rows' => $row];
  608. return json($result);
  609. }
  610. //获取生产单应加量
  611. public function getNumber(){
  612. $params = input('');
  613. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  614. return array('status'=>0,'msg'=>'请求参数错误');
  615. }
  616. $list = Db::name('formula')->where('id',$params['ids'])->find();
  617. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  618. if (empty($gyinfo)){
  619. return array('status'=>0,'msg'=>'数据错误');
  620. }
  621. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
  622. if (empty($total)){
  623. return array('status'=>0,'msg'=>'数据错误');
  624. }
  625. foreach ($total as $k=>$v){
  626. $total[$k] = decode($v);
  627. }
  628. $num = array_sum($total);
  629. foreach ($gyinfo as $key=>$value){
  630. if($gyinfo[$key]['gy_name'] == null){//
  631. $gyinfo[$key]['gy_name'] = '';//
  632. }//
  633. $gyinfo[$key]['num'] = '';
  634. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  635. if (!empty($value['percentage'])){
  636. // $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
  637. $number = ceil($gyinfo[$key]['percentage'] / $num * $params['number'] *1000);
  638. $gyinfo[$key]['num'] = number_format($number/1000,3);
  639. }
  640. }
  641. $date = date('Y/m/d');
  642. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  643. }
  644. //获取工艺说明
  645. public function gyName(){
  646. $params = input('gy_name');
  647. // print_r($params);die;
  648. if ($params){
  649. $list = Db::name('formula_detail')->where('gy_name','like','%'.$params.'%')->field('id,gy_name')->limit(20)->select();
  650. }else{
  651. $list = Db::name('formula_detail')->where('gy_name','neq','')->field('id,gy_name')->limit(20)->select();
  652. }
  653. $total = count($list);
  654. $result = ['total'=>$total,'rows'=>$list];
  655. // return array('status'=>1,'rows'=>$list);
  656. return json($result);
  657. }
  658. public function addFormulaChinese($ids = null){
  659. $date = date('Ymd',time());
  660. if (!$ids){
  661. Log::mylog('formula_log','id='.$ids.'未获取到',$date.'_01');
  662. }
  663. $formula = Db::name('formula')->where('id',$ids)->field('name')->find();
  664. if (!$formula){
  665. Log::mylog('formula_log','id='.$ids.'未获取到配方数据',$date.'_01');
  666. }
  667. $material = Db::name('formula_detail')->where('pid',$ids)->field('id,material')->select();
  668. if (!$material){
  669. Log::mylog('formula_log','pid='.$ids.'未获取到配方详情数据',$date.'_01');
  670. }
  671. $list = array();
  672. $pinyin = new pinyin();
  673. foreach ($material as $key=>$value){
  674. if (preg_match("/[\x7f-\xff]/", $value['material'])){
  675. $str = $value['material'];
  676. $newStr = $pinyin->getpy($str,true,true);
  677. $chinese = array();
  678. for ($i=0;$i<strlen($newStr);$i++){
  679. $string = substr($newStr,$i,1);
  680. if (!preg_match_all('/\b([a-z]+)\b/', $string)){
  681. $chinese[$i] = $string;
  682. }
  683. }
  684. $list[$key]['f_id'] = $ids;
  685. $list[$key]['f_name'] = $formula['name'];
  686. $list[$key]['m_id'] = $value['id'];
  687. $list[$key]['m_name'] = $value['material'];
  688. $list[$key]['name'] = implode($chinese);
  689. }
  690. }
  691. // dump($list);die;
  692. if (!empty($list)){
  693. $result = true;
  694. $is_has = Db::name('formula_material')->where('f_id',$ids)->select();
  695. if ($is_has){
  696. Db::startTrans();
  697. try {
  698. Db::name('formula_material')->where('f_id',$ids)->delete();
  699. Db::name('formula_material')->insertAll($list);
  700. Db::commit();
  701. }catch (Exception $e){
  702. $result = false;
  703. Db::rollback();
  704. }
  705. }else{
  706. $res = Db::name('formula_material')->insertAll($list);
  707. if (!$res){
  708. $result = false;
  709. }
  710. }
  711. if (!$result){
  712. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入失败',$date.'_01');
  713. }else{
  714. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入成功',$date.'_01');
  715. }
  716. }
  717. }
  718. }