Formula.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. //2024年12月31日 18点00后 使用下面代码
  410. // $year = date('Y'); // 获取当前年份
  411. // $tastbach = Db::name('task')->where('create', 'like', $year.'%')->order('bach desc')->find();
  412. $year = date('Y'); // 获取当前年份
  413. $startDate = $year.'-02-01'; // 设置开始日期为2月1日
  414. $tastbach = Db::name('task')->where('create', '>=', $startDate)->order('bach desc')->find();
  415. $bach = str_pad($tastbach['bach'] + 1, 5, "0", STR_PAD_LEFT);
  416. $this->view->assign('bach',$bach);
  417. $list = Db::name('formula')->where('id',$ids)->find();
  418. //查询该配方是否生产过
  419. $taskbach = Db::name('task')->where('fid',$list['id'])->order('create desc')->find();
  420. $count = Db::name('task')->where('fid',$list['id'])->count();
  421. if($taskbach){
  422. $erro = '该配方在于最近 '.$taskbach['create'].' 已生产过'.$count.'次';
  423. }else{
  424. $erro = '该配方未生产过';
  425. }
  426. $this->view->assign('erro',$erro);
  427. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  428. foreach ($list['gyinfo'] as $key=>$value){
  429. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  430. }
  431. //新增关联订单
  432. $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select();
  433. $uncompleted_order = array();
  434. if (!empty($order)){
  435. foreach($order as $k=>$v){
  436. $uncompleted_order[$k]['id'] = $v['id'];
  437. if (empty($v['completed'])){
  438. $completed = 0;
  439. }else{
  440. $completed = $v['completed'];
  441. }
  442. $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)';
  443. }
  444. }
  445. $this->view->assign('ids',$ids);
  446. $this->view->assign('order',$uncompleted_order);
  447. $this->view->assign('machineList',\app\admin\model\Machine::select());
  448. $this->view->assign('row', $list);
  449. return $this->view->fetch();
  450. }
  451. $base = $this->request->post('baseData/a');
  452. // print_r($base);die;
  453. if (empty($base)){
  454. $this->error('数据不能为空');
  455. }
  456. //基础数据
  457. $params = [];
  458. $params['fid'] = $base[0];
  459. $params['name'] = $base[1];
  460. $params['bach'] = $base[2];
  461. $params['drawer_name'] = $base[3];
  462. $params['examine_name'] = $base[4];
  463. $params['number'] = $base[5];
  464. $params['remark'] = $base[6];
  465. $params['machine'] = $base[7];
  466. $params['oid'] = $base[8];
  467. $params['kuodan']=$base[9];
  468. $params['create'] = date('Y-m-d H:i:s');
  469. $result = false;
  470. Db::startTrans();
  471. try {
  472. $task_kd = Db::name('task')->where('bach',$base[2])->find();
  473. if (empty($task_kd)) {
  474. // 没有找到匹配的数据,执行插入操作
  475. $result = Db::name('task')->insert($params);
  476. }
  477. $result = true;
  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['kuodan']) {
  614. $task_kd = Db::name('task')->where('bach',$params['bach'])->find();
  615. if ($task_kd) {
  616. $kuodan['kuodan'] = $params['kuodan'];
  617. Db::name('task')->where('bach', $params['bach'])->update($kuodan);
  618. }else{
  619. $this->error('该配方为生成新批次号');
  620. }
  621. }
  622. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  623. return array('status'=>0,'msg'=>'请求参数错误');
  624. }
  625. $list = Db::name('formula')->where('id',$params['ids'])->find();
  626. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  627. if (empty($gyinfo)){
  628. return array('status'=>0,'msg'=>'数据错误');
  629. }
  630. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
  631. if (empty($total)){
  632. return array('status'=>0,'msg'=>'数据错误');
  633. }
  634. foreach ($total as $k=>$v){
  635. $total[$k] = decode($v);
  636. }
  637. $num = array_sum($total);
  638. foreach ($gyinfo as $key=>$value){
  639. if($gyinfo[$key]['gy_name'] == null){
  640. $gyinfo[$key]['gy_name'] = '';
  641. }
  642. $gyinfo[$key]['num'] = '';
  643. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  644. if (!empty($value['percentage'])){
  645. // $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
  646. $number = ceil($gyinfo[$key]['percentage'] / $num * $params['number'] *1000);
  647. $gyinfo[$key]['num'] = number_format($number/1000,3);
  648. $gyinfo[$key]['numm'] = $num;
  649. }
  650. }
  651. $date = date('Y/m/d');
  652. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  653. }
  654. //获取工艺说明
  655. public function gyName(){
  656. $params = input('gy_name');
  657. // print_r($params);die;
  658. if ($params){
  659. $list = Db::name('formula_detail')->where('gy_name','like','%'.$params.'%')->field('id,gy_name')->limit(20)->select();
  660. }else{
  661. $list = Db::name('formula_detail')->where('gy_name','neq','')->field('id,gy_name')->limit(20)->select();
  662. }
  663. $total = count($list);
  664. $result = ['total'=>$total,'rows'=>$list];
  665. // return array('status'=>1,'rows'=>$list);
  666. return json($result);
  667. }
  668. public function addFormulaChinese($ids = null){
  669. $date = date('Ymd',time());
  670. if (!$ids){
  671. Log::mylog('formula_log','id='.$ids.'未获取到',$date.'_01');
  672. }
  673. $formula = Db::name('formula')->where('id',$ids)->field('name')->find();
  674. if (!$formula){
  675. Log::mylog('formula_log','id='.$ids.'未获取到配方数据',$date.'_01');
  676. }
  677. $material = Db::name('formula_detail')->where('pid',$ids)->field('id,material')->select();
  678. if (!$material){
  679. Log::mylog('formula_log','pid='.$ids.'未获取到配方详情数据',$date.'_01');
  680. }
  681. $list = array();
  682. $pinyin = new pinyin();
  683. foreach ($material as $key=>$value){
  684. if (preg_match("/[\x7f-\xff]/", $value['material'])){
  685. $str = $value['material'];
  686. $newStr = $pinyin->getpy($str,true,true);
  687. $chinese = array();
  688. for ($i=0;$i<strlen($newStr);$i++){
  689. $string = substr($newStr,$i,1);
  690. if (!preg_match_all('/\b([a-z]+)\b/', $string)){
  691. $chinese[$i] = $string;
  692. }
  693. }
  694. $list[$key]['f_id'] = $ids;
  695. $list[$key]['f_name'] = $formula['name'];
  696. $list[$key]['m_id'] = $value['id'];
  697. $list[$key]['m_name'] = $value['material'];
  698. $list[$key]['name'] = implode($chinese);
  699. }
  700. }
  701. // dump($list);die;
  702. if (!empty($list)){
  703. $result = true;
  704. $is_has = Db::name('formula_material')->where('f_id',$ids)->select();
  705. if ($is_has){
  706. Db::startTrans();
  707. try {
  708. Db::name('formula_material')->where('f_id',$ids)->delete();
  709. Db::name('formula_material')->insertAll($list);
  710. Db::commit();
  711. }catch (Exception $e){
  712. $result = false;
  713. Db::rollback();
  714. }
  715. }else{
  716. $res = Db::name('formula_material')->insertAll($list);
  717. if (!$res){
  718. $result = false;
  719. }
  720. }
  721. if (!$result){
  722. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入失败',$date.'_01');
  723. }else{
  724. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入成功',$date.'_01');
  725. }
  726. }
  727. }
  728. }