Formula.php 22 KB

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