Formula.php 29 KB

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