Formula.php 23 KB

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