Formula.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. $this->view->assign('ids',$ids);
  370. $this->view->assign('machineList',\app\admin\model\Machine::select());
  371. $this->view->assign('row', $list);
  372. return $this->view->fetch();
  373. }
  374. $base = $this->request->post('baseData/a');
  375. if (empty($base)){
  376. $this->error('数据不能为空');
  377. }
  378. //基础数据
  379. $params = [];
  380. $params['fid'] = $base[0];
  381. $params['name'] = $base[1];
  382. $params['bach'] = $base[2];
  383. $params['drawer_name'] = $base[3];
  384. $params['examine_name'] = $base[4];
  385. $params['number'] = $base[5];
  386. $params['remark'] = $base[6];
  387. $params['machine'] = $base[7];
  388. $params['create'] = date('Y-m-d H:i:s');
  389. $result = false;
  390. Db::startTrans();
  391. try {
  392. $result = Db::name('task')->insert($params);
  393. Db::commit();
  394. } catch (Exception $e) {
  395. Db::rollback();
  396. $this->error($e->getMessage());
  397. }
  398. if (false === $result) {
  399. $this->error(__('No rows were updated'));
  400. }
  401. $this->success();
  402. }
  403. //配方审核列表
  404. public function examine(){
  405. //设置过滤方法
  406. $this->request->filter(['strip_tags', 'trim']);
  407. if (false === $this->request->isAjax()) {
  408. return $this->view->fetch();
  409. }
  410. //如果发送的来源是 Selectpage,则转发到 Selectpage
  411. if ($this->request->request('keyField')) {
  412. return $this->selectpage();
  413. }
  414. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  415. $user_info = Session::get('admin');
  416. $map = [];
  417. $map['examine_status'] = 1;
  418. if ($user_info['id'] !== 1){
  419. $map['company_id'] = $user_info['company_id'];
  420. }
  421. $list = $this->model
  422. ->where($where)
  423. ->where($map)
  424. ->order($sort, $order)
  425. ->paginate($limit);
  426. $result = ['total' => $list->total(), 'rows' => $list->items()];
  427. return json($result);
  428. }
  429. //审核操作
  430. public function status($ids=null){
  431. if (!$ids) {
  432. $this->error(__('No Results were found'));
  433. }
  434. if (false === $this->request->isPost()) {
  435. $list = Db::name('formula')->where('id',$ids)->find();
  436. //可用性,对应客户名称
  437. if ($list['usability'] != 99){
  438. $customer = explode(',',$list['usability']);
  439. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  440. }
  441. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  442. foreach ($list['gyinfo'] as $key=>$value){
  443. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  444. }
  445. $this->view->assign('ids',$ids);
  446. $this->view->assign('name',$name);
  447. $this->view->assign('row', $list);
  448. return $this->view->fetch();
  449. }
  450. $status = $this->request->post('status');
  451. if (!isset($status) || !isset($ids)){
  452. $this->error('审核失败');
  453. }
  454. $params = [];
  455. $params['examine_status'] = $status;
  456. $params['update'] = date('Y-m-d H:i:s');
  457. $result = false;
  458. Db::startTrans();
  459. try {
  460. $list = Db::name('formula')->where('id',$ids)->find();
  461. if ($list['version'] == 'v0.1'){
  462. Db::name('formula')->where('id',$ids)->setField('version','v1.0');
  463. Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0');
  464. }
  465. $result = Db::name('formula')->where('id',$ids)->update($params);
  466. Db::commit();
  467. } catch (Exception $e) {
  468. Db::rollback();
  469. $this->error($e->getMessage());
  470. }
  471. if ($result){
  472. $this->success('更新成功');
  473. }else{
  474. $this->error('审核失败');
  475. }
  476. }
  477. //下拉选择获取客户列表
  478. public function getCustomer(){
  479. $user_info = Session::get('admin');
  480. $where = [];
  481. if ($user_info['id'] !== 1){
  482. $where['company_id'] =$user_info['company_id'];
  483. }
  484. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  485. $result = ['total' => count($row), 'rows' => $row];
  486. return json($result);
  487. }
  488. //获取生产单应加量
  489. public function getNumber(){
  490. $params = input('');
  491. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  492. return array('status'=>0,'msg'=>'请求参数错误');
  493. }
  494. $list = Db::name('formula')->where('id',$params['ids'])->find();
  495. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  496. if (empty($gyinfo)){
  497. return array('status'=>0,'msg'=>'数据错误');
  498. }
  499. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
  500. if (empty($total)){
  501. return array('status'=>0,'msg'=>'数据错误');
  502. }
  503. foreach ($total as $k=>$v){
  504. $total[$k] = decode($v);
  505. }
  506. $num = array_sum($total);
  507. foreach ($gyinfo as $key=>$value){
  508. if($gyinfo[$key]['gy_name'] == null){//
  509. $gyinfo[$key]['gy_name'] = '';//
  510. }//
  511. $gyinfo[$key]['num'] = '';
  512. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  513. if (!empty($value['percentage'])){
  514. $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
  515. }
  516. }
  517. $date = date('Y/m/d');
  518. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  519. }
  520. //获取工艺说明
  521. public function gyName(){
  522. $data = \app\admin\model\GyName::select();
  523. return json($data);
  524. }
  525. //根据mn_formula_detail表pid进行gy_num工序号处理
  526. public function demo(){
  527. $info = Db::table ('mn_formula_detail')
  528. ->field('pid')->group('pid')->select();
  529. foreach ($info as $k=>$v){
  530. $res = Db::table ('mn_formula_detail')->field('id,gy_name,gy_num')
  531. ->where('pid','=',$v['pid'])->order('id asc')->select();
  532. $a = 1;
  533. foreach ($res as $k=>$v){
  534. $v['gy_num'] = $a;
  535. Db::table ('mn_formula_detail')->where('id',$v['id'])->update(['gy_num'=>$v['gy_num']]);
  536. if($v['gy_name'] <> null){
  537. $a++;
  538. }
  539. }
  540. print('成功执行'.'|'.date('Y-m-d H:i:s'));
  541. }
  542. }
  543. //根据 某个 表修改 某个字段 值为 多少
  544. public function company(){
  545. $info = Db::table('mn_formula_detail')->limit(0,10)->select();
  546. foreach ($info as $k=>$v){
  547. //根据id进行修改
  548. Db::table ('mn_formula_detail')->where('id',$v['id'])->update(['version'=>'v1.0']);
  549. }
  550. //查看要修改的数据有多少
  551. $zong= Db::table('mn_formula_detail')->count();
  552. //查看修改了多少数据
  553. $count= Db::table('mn_formula_detail')->where('version','=','v1.0')->count();
  554. print('共'.$zong.'数据'.'|'.'成功执行'.$count.'数据'.'|'.date('Y-m-d H:i:s'));
  555. }
  556. //获取原材料中可相互代替的原材料
  557. public function dat(){
  558. $zong= Db::table('mn_formula_detail')->where('material','like','%/%')->count();
  559. $count= Db::table('mn_formula_replace')->count();
  560. print('共'.$zong.'数据'.'&nbsp,&nbsp'.'成功执行'.$count.'数据'.'&nbsp,&nbsp'.date('Y-m-d H:i:s'));
  561. $info = Db::table("mn_formula_detail")
  562. ->field('id,material')
  563. ->where('material','like','%/%')->limit(0,1000)->select();
  564. $arr = [];
  565. $a = 0;
  566. foreach ($info as $k=>$v){
  567. $material = explode('/',$v['material']);
  568. for ($i=0;$i<count($material);$i++){
  569. $arr[$a]['fid'] = $v['id'];//fid
  570. $arr[$a]['material'] = $material[$i];//原材料
  571. // $arr[$a]['remark']= '可完全相互代替';//备注
  572. }
  573. }
  574. // Db::table('mn_formula_replace')->insert($arr);
  575. }
  576. }