Formula.php 19 KB

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