Formula.php 20 KB

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