Formula.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. $year = date('Y');
  408. // 查询数据库中 'task' 表里所有 'create' 字段以当前年份开头的数据,并按照 'bach' 字段降序排列,取出第一条数据
  409. $tastbach = Db::name('task')
  410. ->where('create', 'like', $year. '%')
  411. ->order('bach desc')
  412. ->find();
  413. // 判断查询结果是否为空(即查不到数据)
  414. if (empty($tastbach)) {
  415. // 如果没有查到数据,则新增一条数据到 'task' 表中
  416. Db::name('task')->insert([
  417. 'name' => '测试',
  418. 'bach' => '00000',
  419. 'drawer_name' => '测试',
  420. 'examine_name' => '测试',
  421. 'number' => '1',
  422. 'remark' => '',
  423. 'fid' => '1',
  424. 'create' => date('Y-m-d H:i:s'),
  425. ]);
  426. //代表每年都从00001开始
  427. $bach = '00001';
  428. } else {
  429. // 如果查询到记录,则取出 'bach' 字段的值并加 1,然后格式化为 5 位长度,左侧补 0
  430. $bach = str_pad($tastbach['bach'] + 1, 5, "0", STR_PAD_LEFT);
  431. }
  432. $this->view->assign('bach',$bach);
  433. $list = Db::name('formula')->where('id',$ids)->find();
  434. //查询该配方是否生产过
  435. $taskbach = Db::name('task')->where('fid',$list['id'])->order('create desc')->find();
  436. $count = Db::name('task')->where('fid',$list['id'])->count();
  437. if($taskbach){
  438. $erro = '该配方在于最近 '.$taskbach['create'].' 已生产过'.$count.'次';
  439. }else{
  440. $erro = '该配方未生产过';
  441. }
  442. $this->view->assign('erro',$erro);
  443. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num,version')->select();
  444. foreach ($list['gyinfo'] as $key=>$value){
  445. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  446. }
  447. //新增关联订单
  448. $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select();
  449. $uncompleted_order = array();
  450. if (!empty($order)){
  451. foreach($order as $k=>$v){
  452. $uncompleted_order[$k]['id'] = $v['id'];
  453. if (empty($v['completed'])){
  454. $completed = 0;
  455. }else{
  456. $completed = $v['completed'];
  457. }
  458. $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)';
  459. }
  460. }
  461. $this->view->assign('ids',$ids);
  462. $this->view->assign('order',$uncompleted_order);
  463. $this->view->assign('machineList',\app\admin\model\Machine::select());
  464. $this->view->assign('row', $list);
  465. return $this->view->fetch();
  466. }
  467. $base = $this->request->post('baseData/a');
  468. // print_r($base);die;
  469. if (empty($base)){
  470. $this->error('数据不能为空');
  471. }
  472. //基础数据
  473. $params = [];
  474. $params['fid'] = $base[0];
  475. $params['name'] = $base[1];
  476. $params['bach'] = $base[2];
  477. $params['drawer_name'] = $base[3];
  478. $params['examine_name'] = $base[4];
  479. $params['number'] = $base[5];
  480. $params['remark'] = $base[6];
  481. $params['machine'] = $base[7];
  482. $params['oid'] = $base[8];
  483. $params['kuodan']=$base[9];
  484. $params['create'] = date('Y-m-d H:i:s');
  485. $result = false;
  486. Db::startTrans();
  487. try {
  488. $task_kd = Db::name('task')->where('bach',$base[2])->find();
  489. // if (empty($task_kd)) {
  490. // 没有找到匹配的数据,执行插入操作
  491. $result = Db::name('task')->insert($params);
  492. // }
  493. $result = true;
  494. // Db::name('order')->where('id',$params['oid'])->setField('status',2);
  495. // $result = Db::name('task')->insert($params);
  496. //更改订单已完成数量,修改订单状态
  497. $order_info = Db::name('order')->where('id',$params['oid'])->find();
  498. if (empty($order_info['completed'])){
  499. $new_completed = $params['number'];
  500. $order_status = 2;//生产中
  501. Db::name('order')->where('id',$params['oid'])->setField('status',$order_status);
  502. }else{
  503. $new_completed = $params['number'] + $order_info['completed'];
  504. }
  505. Db::name('order')->where('id',$params['oid'])->setField('completed',$new_completed);
  506. Db::commit();
  507. } catch (Exception $e) {
  508. Db::rollback();
  509. $this->error($e->getMessage());
  510. }
  511. if (false === $result) {
  512. $this->error(__('No rows were updated'));
  513. }
  514. $this->success();
  515. }
  516. //配方审核列表
  517. public function examine(){
  518. //设置过滤方法
  519. $this->request->filter(['strip_tags', 'trim']);
  520. if (false === $this->request->isAjax()) {
  521. return $this->view->fetch();
  522. }
  523. //如果发送的来源是 Selectpage,则转发到 Selectpage
  524. if ($this->request->request('keyField')) {
  525. return $this->selectpage();
  526. }
  527. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  528. $user_info = Session::get('admin');
  529. $map = [];
  530. $map['examine_status'] = 1;
  531. if ($user_info['id'] !== 1){
  532. $map['company_id'] = $user_info['company_id'];
  533. }
  534. $list = $this->model
  535. ->where($where)
  536. ->where($map)
  537. ->order($sort, $order)
  538. ->paginate($limit);
  539. $result = ['total' => $list->total(), 'rows' => $list->items()];
  540. return json($result);
  541. }
  542. //审核操作
  543. public function status($ids=null){
  544. if (!$ids) {
  545. $this->error(__('No Results were found'));
  546. }
  547. if (false === $this->request->isPost()) {
  548. $list = Db::name('formula')->where('id',$ids)->find();
  549. //可用性,对应客户名称
  550. if ($list['usability'] != 99){
  551. $customer = explode(',',$list['usability']);
  552. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  553. }
  554. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  555. foreach ($list['gyinfo'] as $key=>$value){
  556. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  557. }
  558. $this->view->assign('ids',$ids);
  559. $this->view->assign('name',$name);
  560. $this->view->assign('row', $list);
  561. return $this->view->fetch();
  562. }
  563. $status = $this->request->post('status');
  564. if (!isset($status) || !isset($ids)){
  565. $this->error('审核失败');
  566. }
  567. $params = [];
  568. $user_info = Session::get('admin');
  569. $params['examine_userid'] = $user_info['id'];//审核用户id
  570. $params['examine_status'] = $status;//审核状态
  571. $params['update'] = date('Y-m-d H:i:s');
  572. $result = false;
  573. Db::startTrans();
  574. try {
  575. $list = Db::name('formula')->where('id',$ids)->find();
  576. if ($list['version'] == 'v0.1'){
  577. Db::name('formula')->where('id',$ids)->setField('version','v1.0');
  578. Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0');
  579. }
  580. $result = Db::name('formula')->where('id',$ids)->update($params);
  581. Db::commit();
  582. } catch (Exception $e) {
  583. Db::rollback();
  584. $this->error($e->getMessage());
  585. }
  586. if ($result){
  587. $this->addFormulaChinese($ids);
  588. $this->success('更新成功');
  589. }else{
  590. $this->error('审核失败');
  591. }
  592. }
  593. //审核记录列表
  594. public function examine_status(){
  595. $user_info = Session::get('admin');
  596. $formula = Db::name('formula')->where('examine_userid',$user_info['id'])->order('update desc')
  597. ->paginate(10,false);
  598. $page = $formula->render();
  599. $this->view->assign('page',$page);
  600. $this->view->assign('formula',$formula);
  601. return $this->view->fetch();
  602. }
  603. //审核记录详情列表
  604. public function examine_list(){
  605. $get = $this->request->get();
  606. $formula = Db::name('formula')->where('id',$get['id'])->find();
  607. $formula_detail = Db::name('formula_detail')->where('pid',$get['id'])->select();
  608. foreach ($formula_detail as $key=>$value){
  609. $formula_detail[$key]['percentage'] = decode($value['percentage']);
  610. }
  611. $this->view->assign('formula',$formula);
  612. $this->view->assign('formula_detail',$formula_detail);
  613. return $this->view->fetch();
  614. }
  615. //下拉选择获取客户列表
  616. public function getCustomer(){
  617. $user_info = Session::get('admin');
  618. $where = [];
  619. if ($user_info['id'] !== 1){
  620. $where['company_id'] =$user_info['company_id'];
  621. }
  622. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  623. $result = ['total' => count($row), 'rows' => $row];
  624. return json($result);
  625. }
  626. //获取生产单应加量
  627. public function getNumber(){
  628. $params = input('');
  629. if ($params['kuodan']) {
  630. $task_kd = Db::name('task')->where('bach',$params['bach'])->find();
  631. if ($task_kd) {
  632. $kuodan['kuodan'] = $params['kuodan'];
  633. Db::name('task')->where('bach', $params['bach'])->update($kuodan);
  634. }else{
  635. $this->error('该配方为生成新批次号');
  636. }
  637. }
  638. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  639. return array('status'=>0,'msg'=>'请求参数错误');
  640. }
  641. $list = Db::name('formula')->where('id',$params['ids'])->find();
  642. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  643. if (empty($gyinfo)){
  644. return array('status'=>0,'msg'=>'数据错误');
  645. }
  646. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
  647. if (empty($total)){
  648. return array('status'=>0,'msg'=>'数据错误');
  649. }
  650. foreach ($total as $k=>$v){
  651. $total[$k] = decode($v);
  652. }
  653. $num = array_sum($total);
  654. foreach ($gyinfo as $key=>$value){
  655. if($gyinfo[$key]['gy_name'] == null){
  656. $gyinfo[$key]['gy_name'] = '';
  657. }
  658. $gyinfo[$key]['num'] = '';
  659. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  660. if (!empty($value['percentage'])){
  661. // $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
  662. $number = ceil($gyinfo[$key]['percentage'] / $num * $params['number'] *1000);
  663. $gyinfo[$key]['num'] = number_format($number/1000,3);
  664. $gyinfo[$key]['numm'] = $num;
  665. }
  666. }
  667. $date = date('Y/m/d');
  668. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  669. }
  670. //获取工艺说明
  671. public function gyName(){
  672. $params = input('gy_name');
  673. // print_r($params);die;
  674. if ($params){
  675. $list = Db::name('formula_detail')->where('gy_name','like','%'.$params.'%')->field('id,gy_name')->limit(20)->select();
  676. }else{
  677. $list = Db::name('formula_detail')->where('gy_name','neq','')->field('id,gy_name')->limit(20)->select();
  678. }
  679. $total = count($list);
  680. $result = ['total'=>$total,'rows'=>$list];
  681. // return array('status'=>1,'rows'=>$list);
  682. return json($result);
  683. }
  684. public function addFormulaChinese($ids = null){
  685. $date = date('Ymd',time());
  686. if (!$ids){
  687. Log::mylog('formula_log','id='.$ids.'未获取到',$date.'_01');
  688. }
  689. $formula = Db::name('formula')->where('id',$ids)->field('name')->find();
  690. if (!$formula){
  691. Log::mylog('formula_log','id='.$ids.'未获取到配方数据',$date.'_01');
  692. }
  693. $material = Db::name('formula_detail')->where('pid',$ids)->field('id,material')->select();
  694. if (!$material){
  695. Log::mylog('formula_log','pid='.$ids.'未获取到配方详情数据',$date.'_01');
  696. }
  697. $list = array();
  698. $pinyin = new pinyin();
  699. foreach ($material as $key=>$value){
  700. if (preg_match("/[\x7f-\xff]/", $value['material'])){
  701. $str = $value['material'];
  702. $newStr = $pinyin->getpy($str,true,true);
  703. $chinese = array();
  704. for ($i=0;$i<strlen($newStr);$i++){
  705. $string = substr($newStr,$i,1);
  706. if (!preg_match_all('/\b([a-z]+)\b/', $string)){
  707. $chinese[$i] = $string;
  708. }
  709. }
  710. $list[$key]['f_id'] = $ids;
  711. $list[$key]['f_name'] = $formula['name'];
  712. $list[$key]['m_id'] = $value['id'];
  713. $list[$key]['m_name'] = $value['material'];
  714. $list[$key]['name'] = implode($chinese);
  715. }
  716. }
  717. // dump($list);die;
  718. if (!empty($list)){
  719. $result = true;
  720. $is_has = Db::name('formula_material')->where('f_id',$ids)->select();
  721. if ($is_has){
  722. Db::startTrans();
  723. try {
  724. Db::name('formula_material')->where('f_id',$ids)->delete();
  725. Db::name('formula_material')->insertAll($list);
  726. Db::commit();
  727. }catch (Exception $e){
  728. $result = false;
  729. Db::rollback();
  730. }
  731. }else{
  732. $res = Db::name('formula_material')->insertAll($list);
  733. if (!$res){
  734. $result = false;
  735. }
  736. }
  737. if (!$result){
  738. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入失败',$date.'_01');
  739. }else{
  740. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入成功',$date.'_01');
  741. }
  742. }
  743. }
  744. }