Feeding.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Session;
  9. /**
  10. * 生产投料
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Feeding extends Backend
  15. {
  16. protected $searchFields = 'material,bach';
  17. /**
  18. * Feeding模型对象
  19. * @var \app\admin\model\Feeding
  20. */
  21. protected $model = null;
  22. protected $noNeedRight = ['get_formula','get_task','get_material'];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\Feeding;
  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
  37. * @throws \think\Exception
  38. */
  39. public function add()
  40. {
  41. if (false === $this->request->isPost()) {
  42. //车间=>操作人员
  43. $cjcz = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjcz")->order('name desc')->select();
  44. //车间=>检验人员
  45. $cjjy = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjjy")->order('name desc')->select();
  46. $this->assign('cjcz',$cjcz);
  47. $this->assign('cjjy',$cjjy);
  48. return $this->view->fetch();
  49. }
  50. $tid = Db::name('task')->where('bach',$_POST['bach'])->order('id desc')->find();
  51. if(empty($_POST['weight'])){
  52. $this->error('投料数据不能为空');
  53. }
  54. $feeding_no = Db::name('feeding')->where('bach',$_POST['bach'])->order('id desc')->find();
  55. if (isset($feeding_no['no'])) {
  56. // 确保 $feeding_no 是一个数组且 $feeding_no['no'] 已设置
  57. $param['no'] = $feeding_no['no'] + 1;
  58. } else {
  59. // $feeding_no 不存在,或者 $feeding_no['no'] 未设置
  60. $param['no'] = 1;
  61. }
  62. // $param['no'] = $fno['no'] +1;//操作顺序
  63. $param['tid'] = $tid['id'];//关联批次idx
  64. $param['bach'] = $_POST['bach'];//批次号
  65. $param['date'] = $_POST['date'];//日期
  66. $param['operator'] = $_POST['operator'];//操作人员
  67. $param['inspector'] = $_POST['inspector'];//检验人员
  68. $param['material'] = $_POST['material'];//原材料
  69. $param['nweight'] = $_POST['nweight'];//应投重量
  70. $param['gy_num'] = $_POST['gy_num'];//工艺序号
  71. $param['weight'] = $_POST['weight'];//投料重量
  72. $param['material_bach'] = $_POST['material_bach'];//原材料批次号
  73. $param['remark'] = $_POST['gy_name'];//操作记录
  74. $param['create'] = date('Y-m-d H:i:s');//创建时间
  75. $feeding = Db::name('feeding')->insert($param);
  76. if($feeding){return "添加成功";}else{return "添加失败";}
  77. // $params = $this->request->post('row/a');
  78. // $params = $this->preExcludeFields($params);
  79. // $tid = Db::name('task')->where('bach',$params['bach'])->order('id desc')->find();
  80. // $arr = [];
  81. // if(empty($params['weight'])){
  82. // $this->error('投料数据不能为空');
  83. // }
  84. // foreach($params['weight'] as $k=>$v){
  85. // if($v){
  86. // $param['bach'] = $params['bach'];
  87. // $param['date'] = $params['date'];
  88. // $param['operator'] = $params['operator'];
  89. // $param['inspector'] = $params['inspector'];
  90. // $param['weight'] = $params['weight'][$k];
  91. // $param['nweight'] = $params['nweight'][$k];
  92. // $param['material'] = $params['material'][$k];
  93. // $param['gy_num'] = $params['gy_num'][$k];
  94. // $param['tid'] = $tid['id'];
  95. // $param['create'] = date('Y-m-d H:i:s');
  96. // $arr[] = $param;
  97. // }
  98. // }
  99. // //去掉已经存入数据库的工艺数据
  100. // $list = array();
  101. // foreach ($arr as $key=>$value){
  102. // $map=[];
  103. // $map['bach'] = $value['bach'];
  104. // $map['material'] = $value['material'];
  105. // $map['gy_num'] = $value['gy_num'];
  106. // $is_feeding_gy = Db::name('feeding')->where($map)->order('id desc')->find();
  107. // if (!$is_feeding_gy){
  108. // $list[$key] = $value;
  109. // }
  110. // }
  111. // $result = false;
  112. // Db::startTrans();
  113. // try {
  114. // //是否采用模型验证
  115. // if ($this->modelValidate) {
  116. // $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  117. // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  118. // $this->model->validateFailException()->validate($validate);
  119. // }
  120. //
  121. // $result = $this->model->allowField(true)->saveAll($list);
  122. // Db::commit();
  123. // } catch (ValidateException|PDOException|Exception $e) {
  124. // Db::rollback();
  125. // $this->error($e->getMessage());
  126. // }
  127. // if ($result === false) {
  128. // $this->error(__('No rows were inserted'));
  129. // }
  130. // $this->success();
  131. }
  132. //投料添加页面=>修改应投重量
  133. // public function submits(){
  134. // $param = $this->request->post();
  135. // $where = [];
  136. // $where['bach'] = $param['bach'];
  137. // $id = Db::name('feeding')->where($where)->select();
  138. // echo "<pre>";
  139. // print_r($id);
  140. // echo "</pre>";die;
  141. // $arr['weight'] = $param['weight'];
  142. // $feeding = Db::name('feeding')->where('id',$id['id'])->select();
  143. // echo "<pre>";
  144. // print_r($feeding);
  145. // echo "</pre>";die;
  146. // if(!$feeding){
  147. // return '修改失败';
  148. // }else{
  149. // return '修改成功';
  150. // }
  151. // }
  152. //获取作业票信息
  153. public function get_task(){
  154. $bach = $this->request->post('bach');
  155. $year = date('Y'); // 获取当前年份
  156. $row = Db::name('feeding')->where('bach',$bach)->select();
  157. $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
  158. //已有过工序
  159. if($row){
  160. $result['inspector'] = $row[0]['inspector'];
  161. $result['operator'] = $row[0]['operator'];
  162. }else{
  163. $result['inspector'] = '';
  164. $result['operator'] = '';
  165. }
  166. $result['data'] = $res;
  167. return json($result);
  168. }
  169. //获取配方信息 原始代码
  170. public function get_formulas(){
  171. $bach = $this->request->post('bach');//批次号
  172. $num = $this->request->post('num');//生产量
  173. $process = Db::name('feeding')->where('bach',$bach)->select();
  174. if($num){//如果有,批次号重复,需精确查找
  175. $res = Db::name('task')->alias('t')
  176. ->join('formula_detail f','f.pid=t.fid','left')
  177. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  178. ->where('t.bach',$bach)->where('t.number',$num)->select();
  179. }else{//如果没有,,批次号未重复,直接差出数据
  180. $res = Db::name('task')->alias('t')
  181. ->join('formula_detail f','t.fid = f.pid','left')
  182. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  183. ->where('t.bach',$bach)->select();
  184. }
  185. $pro = 0;
  186. //按照百分比计算出应投重量
  187. foreach($res as &$v){
  188. $v['weight']=0;
  189. $v['material_bach'] = '';
  190. if($process){ //已有工序,接上一次工序
  191. foreach ($process as $val){
  192. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  193. $v['weight']=$val['weight'];
  194. $v['material_bach']=$val['material_bach'];
  195. $pro = $val['gy_num'];
  196. }
  197. }
  198. }
  199. if($v['gy_name'] == null){
  200. $v['gy_name'] = '';
  201. }
  202. if($v['gy_num'] == null){
  203. $v['gy_num'] = '';
  204. }
  205. if($v['material'] == null){
  206. $v['material'] = '';
  207. }
  208. if($v['material_bach'] == null){
  209. $v['material_bach'] = '';
  210. }
  211. $total = array_column($res,'percentage');
  212. foreach ($total as $key=>$value){
  213. $total[$key] = decode($value);
  214. }
  215. $num = array_sum($total);
  216. if($v['kuodan']){
  217. $kd_sum = $v['number']+$v['kuodan'];
  218. if($v['percentage']){
  219. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  220. $v['nweight'] = number_format($number/1000,3);
  221. }else{
  222. $v['nweight']='';
  223. }
  224. }else{
  225. if($v['percentage']){
  226. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  227. $v['nweight'] = number_format($number/1000,3);
  228. }else{
  229. $v['nweight']='';
  230. }
  231. }
  232. }
  233. $row['total']=$num;
  234. $row['data'] = $res;
  235. $row['process'] = $pro+1;
  236. return json($row);
  237. }
  238. //获取配方信息
  239. public function get_formula(){
  240. // $bach = $this->request->post('bach');//批次号
  241. // $num = $this->request->post('num');//生产量
  242. // $process = Db::name('feeding')->where('bach',$bach)->select();
  243. // if($num){//如果有,批次号重复,需精确查找
  244. // $res = Db::name('task')->alias('t')
  245. // ->join('formula_detail f','f.pid=t.fid','left')
  246. // ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  247. // ->where('t.bach',$bach)->where('t.number',$num)->select();
  248. // }else{//如果没有,,批次号未重复,直接差出数据
  249. // $res = Db::name('task')->alias('t')
  250. // ->join('formula_detail f','t.fid = f.pid','left')
  251. // ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  252. // ->where('t.bach',$bach)->select();
  253. // }
  254. // $pro = 0;
  255. // //按照百分比计算出应投重量
  256. // foreach($res as &$v){
  257. // $v['weight']=0;
  258. // $v['material_bach'] = '';
  259. // if($process){ //已有工序,接上一次工序
  260. // foreach ($process as $val){
  261. // if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  262. // $v['weight']=$val['weight'];
  263. // $v['material_bach']=$val['material_bach'];
  264. // $pro = $val['gy_num'];
  265. // }
  266. // }
  267. // }
  268. // if($v['gy_name'] == null){
  269. // $v['gy_name'] = '';
  270. // }
  271. // if($v['gy_num'] == null){
  272. // $v['gy_num'] = '';
  273. // }
  274. // if($v['material'] == null){
  275. // $v['material'] = '';
  276. // }
  277. // if($v['material_bach'] == null){
  278. // $v['material_bach'] = '';
  279. // }
  280. // $total = array_column($res,'percentage');
  281. // foreach ($total as $key=>$value){
  282. // $total[$key] = decode($value);
  283. // }
  284. // $num = array_sum($total);
  285. // if($v['kuodan']){
  286. // $kd_sum = $v['number']+$v['kuodan'];
  287. // if($v['percentage']){
  288. // $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  289. // $v['nweight'] = number_format($number/1000,3);
  290. // }else{
  291. // $v['nweight']='';
  292. // }
  293. // }else{
  294. // if($v['percentage']){
  295. // $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  296. // $v['nweight'] = number_format($number/1000,3);
  297. // }else{
  298. // $v['nweight']='';
  299. // }
  300. // }
  301. // }
  302. // $row['total']=$num;
  303. // $row['data'] = $res;
  304. // $row['process'] = $pro+1;
  305. // return json($row);
  306. $bach = $this->request->post('bach');//批次号
  307. $num = $this->request->post('num');//生产量
  308. $process = Db::name('feeding')->where('bach',$bach)->select();
  309. if($num){//如果有,批次号重复,需精确查找
  310. $res = Db::name('task')->alias('t')
  311. ->join('formula_detail f','f.pid=t.fid','left')
  312. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  313. ->where('t.bach',$bach)->where('t.number',$num)->select();
  314. }else{//如果没有,,批次号未重复,直接差出数据
  315. $res = Db::name('task')->alias('t')
  316. ->join('formula_detail f','t.fid = f.pid','left')
  317. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  318. ->where('t.bach',$bach)->select();
  319. }
  320. $pro = 0;
  321. //按照百分比计算出应投重量
  322. foreach($res as &$v){
  323. $v['weight']=0;
  324. $v['material_bach'] = '';
  325. if($process){ //已有工序,接上一次工序
  326. foreach ($process as $val){
  327. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  328. $v['weight']=$val['weight'];
  329. $v['material_bach']=$val['material_bach'];
  330. $pro = $val['gy_num'];
  331. }
  332. }
  333. }
  334. if($v['gy_name'] == null){
  335. $v['gy_name'] = '';
  336. }
  337. if($v['gy_num'] == null){
  338. $v['gy_num'] = '';
  339. }
  340. if($v['material'] == null){
  341. $v['material'] = '';
  342. }
  343. if($v['material_bach'] == null){
  344. $v['material_bach'] = '';
  345. }
  346. $total = array_column($res,'percentage');
  347. foreach ($total as $key=>$value){
  348. $total[$key] = decode($value);
  349. }
  350. $num = array_sum($total);
  351. if($v['kuodan']){
  352. $kd_sum = $v['number']+$v['kuodan'];
  353. if($v['percentage']){
  354. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  355. $v['nweight'] = number_format($number/1000,3);
  356. }else{
  357. $v['nweight']='';
  358. }
  359. }else{
  360. if($v['percentage']){
  361. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  362. $v['nweight'] = number_format($number/1000,3);
  363. }else{
  364. $v['nweight']='';
  365. }
  366. }
  367. }
  368. $originalRes = $res;
  369. if (!$process) {
  370. // 如果 $process 为空,只保留 gy_num 为 1 的数据
  371. $filtered = array_filter($res, function($item) {
  372. return $item['gy_num'] == 1;
  373. });
  374. } else {
  375. // 确定 $process 中最高的 gy_num
  376. $maxGyNumInProcess = max(array_column($process, 'gy_num'));
  377. $nextGyNum = $maxGyNumInProcess + 1;
  378. // 检查并跳过带有特定标记(如 '*****')的工序
  379. do {
  380. $nextGyNumItems = array_filter($res, function($item) use ($nextGyNum) {
  381. return $item['gy_num'] == $nextGyNum;
  382. });
  383. // 如果找到的工序是默认完成的(即 material 包含 '*****'),则跳到下一个工序
  384. if (!empty($nextGyNumItems) && array_values($nextGyNumItems)[0]['material'] === '*****') {
  385. $nextGyNum++;
  386. } else {
  387. break; // 找到了需要处理的下一个工序或者没有更多工序可以处理
  388. }
  389. } while (true);
  390. // 重新过滤,确保处理正确的工序(这一步可能是多余的,根据您的逻辑调整)
  391. $filtered = array_filter($res, function($item) use ($nextGyNum) {
  392. return $item['gy_num'] == $nextGyNum;
  393. });
  394. }
  395. $res = array_values($filtered); // 重置数组索引
  396. if (empty($res)) {
  397. // 这里假设 $originalRes 保存了所有工序的原始数据
  398. // 如果您没有这样一个变量,您需要确保有方法可以获取到所有工序的数据
  399. $res = $originalRes; // 将所有工序的数据赋值给 $res,进行后续处理
  400. }
  401. $row['total']=$num;
  402. $row['data'] = $res;
  403. $row['process'] = $pro+1;
  404. return json($row);
  405. }
  406. public function get_material(){
  407. $bach = $this->request->post('bach');//批次号
  408. $material = $this->request->post('material');//原材料
  409. if (!$bach || !$material){
  410. $this->error('扫码数据不能为空');
  411. }
  412. $task = Db::name('task')->where('bach',$bach)->find();
  413. $map = [];
  414. $map['f_name'] = $task['name'];
  415. $map['name'] = $material;
  416. $data = Db::name('formula_material')->where($map)->select();
  417. $result['data'] = '';
  418. if ($data){
  419. $result['data'] = $data;
  420. if (count($data)>1){
  421. $str = [];
  422. foreach ($data as $key=>$value){
  423. $str[$key] = $value['m_name'];
  424. }
  425. $result['string'] = implode('或 ',$str);
  426. }else{
  427. $result['string'] = $data[0]['m_name'];
  428. }
  429. $result['total'] = count($data);
  430. }else{
  431. $result['total'] = 0;
  432. }
  433. return json($result);
  434. }
  435. //查找替代料
  436. /*public function replace(){
  437. $bach = $this->request->post('bach');
  438. $wuliao = $this->request->post('wuliao');
  439. $res = Db::name('task')->alias('t')
  440. ->join('formula f','f.id = t.fid','left')
  441. ->join('formula_detail fd','fd.pid = f.id','left')
  442. ->join('formula_replace fr','fr.fid = fd.id','left')
  443. ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
  444. ->field('fd.material,fd.id')->find();
  445. $res['yuan'] = explode('/',$res['material']);
  446. $key = array_search($wuliao, $res['yuan']);
  447. if ($key !== false) array_splice($res['yuan'], $key, 1);
  448. return json($res);
  449. }*/
  450. public function cheng(){
  451. $user_info = Session::get('');
  452. return json($user_info);
  453. }
  454. }