Picking.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Think\Exception;
  6. /**
  7. * 领料单
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Picking extends Backend
  12. {
  13. /**
  14. * Picking模型对象
  15. * @var \app\admin\model\Picking
  16. */
  17. protected $model = null;
  18. protected $searchFields = 'order,picking,matter';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Picking;
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. public function order(){
  30. $data = $this->request->request();
  31. if ($this->request->isAjax()) {
  32. $params['pagesize'] = is_null($data['limit']) ? 10 : $data['limit'];//初始查询位置
  33. if ($data['limit'] == 0){
  34. $params['page'] = 1;
  35. }else{
  36. $params['page'] = $data['offset'] / 10 + 1;
  37. }
  38. $search = array();
  39. if (!empty($data['filter'])){
  40. $search = json_decode($data['filter'],true);
  41. }
  42. // print_r($search);die;
  43. /**
  44. * year(Gy0_sj1)=1900 表明尚未排单
  45. * year(Gy0_sj1)>1900 表明在排单中,
  46. * Pd_wg为真,表明工序结束
  47. * */
  48. if (!empty($search)){
  49. $gdbh = isset($search['Gd_gdbh'])?$search['Gd_gdbh']:'';
  50. $jtbh = isset($search['Gy0_sbbh'])?'%'.$search['Gy0_sbbh'].'%':'%JY%';
  51. if ($gdbh){
  52. $sql = "SELECT DISTINCT(a.Gd_gdbh),a.Gd_cpmc,a.gd_statu,b.Gy0_sbbh,b.Gy0_sj1,b.Gy0_sj2,b.PD_WG FROM `工单_基本资料` a
  53. LEFT JOIN `工单_工艺资料` b ON a.Gd_gdbh = b.Gy0_gdbh
  54. WHERE (a.gd_statu = '2-生产中' OR a.gd_statu='3-计划中') AND a.Gd_cpmc !='' AND a.行号 = 1
  55. AND b.Gy0_sbbh LIKE '{$jtbh}' AND YEAR(b.Gy0_sj1) > 1900 AND YEAR(b.Gy0_sj1)<2099 AND YEAR(b.PD_WG) = 1900
  56. AND a.Gd_gdbh = {$gdbh}
  57. ORDER BY b.Gy0_sbbh ASC ,b.Gy0_sj1 ASC ";
  58. }else{
  59. $sql = "SELECT DISTINCT(a.Gd_gdbh),a.Gd_cpmc,a.gd_statu,b.Gy0_sbbh,b.Gy0_sj1,b.Gy0_sj2,b.PD_WG FROM `工单_基本资料` a
  60. LEFT JOIN `工单_工艺资料` b ON a.Gd_gdbh = b.Gy0_gdbh
  61. WHERE (a.gd_statu = '2-生产中' OR a.gd_statu='3-计划中') AND a.Gd_cpmc !='' AND a.行号 = 1
  62. AND b.Gy0_sbbh LIKE '%{$jtbh}%' AND YEAR(b.Gy0_sj1) > 1900 AND YEAR(b.Gy0_sj1)<2099 AND YEAR(b.PD_WG) = 1900
  63. ORDER BY b.Gy0_sbbh ASC ,b.Gy0_sj1 ASC ";
  64. }
  65. $res = Db::connect('db2')
  66. ->query($sql);
  67. }else{
  68. $res = Db::connect('db2')
  69. ->query("SELECT DISTINCT(a.Gd_gdbh),a.Gd_cpmc,a.gd_statu,b.Gy0_sbbh,b.Gy0_sj1,b.Gy0_sj2,b.PD_WG FROM `工单_基本资料` a
  70. LEFT JOIN `工单_工艺资料` b ON a.Gd_gdbh = b.Gy0_gdbh
  71. WHERE (a.gd_statu = '2-生产中' OR a.gd_statu='3-计划中') AND a.Gd_cpmc !='' AND a.行号 = 1
  72. AND b.Gy0_sbbh LIKE '%JY%' AND YEAR(b.Gy0_sj1) > 1900 AND YEAR(b.Gy0_sj1)<2099 AND YEAR(b.PD_WG) = 1900
  73. ORDER BY b.Gy0_sbbh ASC ,b.Gy0_sj1 ASC ");
  74. }
  75. foreach ($res as $key=>$val){
  76. $id = $val['Gd_gdbh'];
  77. $res[$key]['id'] = $id;
  78. }
  79. $total = count($res);
  80. // $res = array_slice($res,$data['offset'],$data['limit']);//分页
  81. $result = array("total" => $total, "rows" => $res);
  82. return json($result);
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 查看
  88. */
  89. public function index()
  90. {
  91. //设置过滤方法
  92. $this->request->filter(['strip_tags']);
  93. if ($this->request->isAjax()) {
  94. //如果发送的来源是Selectpage,则转发到Selectpage
  95. if ($this->request->request('keyField')) {
  96. return $this->selectpage();
  97. }
  98. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  99. $total = $this->model
  100. ->where($where)
  101. ->order($sort, $order)
  102. ->count();
  103. $list = $this->model
  104. ->where($where)
  105. ->order($sort, $order)
  106. ->limit($offset, $limit)
  107. ->select();
  108. // dump($list);die;
  109. $list = collection($list)->toArray();
  110. $result = array("total" => $total, "rows" => $list);
  111. return json($result);
  112. }
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 领料单界面数据
  117. */
  118. public function picking(){
  119. $result = $this->validatePicking(input('temp'));
  120. if (!$result['valid']) {
  121. $this->dialogError($result['msg']);
  122. }
  123. return $this->view->fetch('add', $result['data']);
  124. }
  125. /**
  126. * 生成领料单前校验(Ajax)
  127. */
  128. public function checkPicking()
  129. {
  130. $result = $this->validatePicking(input('temp'));
  131. if ($result['valid']) {
  132. $this->success('可以生成领料单');
  133. }
  134. $this->error($result['msg']);
  135. }
  136. /**
  137. * 校验选中工单是否具备领料配方数据
  138. */
  139. protected function validatePicking($data)
  140. {
  141. $gdbh = array_filter(explode(',', $data));
  142. if (empty($gdbh)) {
  143. return ['valid' => false, 'msg' => '请先选择要生成领料单的工单'];
  144. }
  145. $list = [];
  146. foreach ($gdbh as $key => $value) {
  147. $sql = "SELECT GD_gdbh AS gdbh,成品名称 as name,Gd_cpdh AS cpdh,计划投料 AS num FROM `工单_基本资料` WHERE Gd_gdbh = ? AND `行号`= 1";
  148. $res = Db::connect('db2')->query($sql, [$value]);
  149. if (empty($res[0])) {
  150. return ['valid' => false, 'msg' => '查不到工单【' . $value . '】,请确认工单是否存在'];
  151. }
  152. $list[$key] = $res[0];
  153. }
  154. $consumeList = [];
  155. $formulaList = [];
  156. $total = [];
  157. foreach ($list as $k => $val) {
  158. $formulaList = [];
  159. $box = $this->isBigOrSmall(trim($val['name']));
  160. if ($box == -1) {
  161. return ['valid' => false, 'msg' => '工单【' . $val['gdbh'] . '】产品【' . trim($val['name']) . '】无法识别大小盒类型,请联系开发人员'];
  162. }
  163. $productNumber = trim($val['cpdh']);
  164. if ($productNumber === '') {
  165. return ['valid' => false, 'msg' => '工单【' . $val['gdbh'] . '】产品编码为空,无法生成领料单'];
  166. }
  167. $where = [];
  168. $where[] = ['exp', Db::raw("FIND_IN_SET(:pn, product_number)")];
  169. $bind = ['pn' => $productNumber];
  170. $consume = Db::name('formula_consume')
  171. ->bind($bind)
  172. ->where($where)
  173. ->where('number', 'neq', '')
  174. ->where('box', $box)
  175. ->field('ink,number,code')
  176. ->select();
  177. $formmula = Db::name('formula_detail')->bind($bind)->where($where)->field('color,ink,ink_number,number,total,code')->select();
  178. if (empty($consume)) {
  179. return ['valid' => false, 'msg' => '工单【' . $val['gdbh'] . '】产品编码【' . $productNumber . '】未配置原墨消耗配方,请联系开发人员维护后再生成领料单'];
  180. }
  181. $num = count($consume);
  182. for ($i = 0; $i < $num; $i++) {
  183. $weight = round(trim($consume[$i]['number']) * $val['num'] / 10000, 3);
  184. $consume[$i]['number'] = $weight;
  185. if (empty($consume[$i]['code'])) {
  186. foreach ($formmula as $j => $v) {
  187. if ($v['color'] == $consume[$i]['ink']) {
  188. $formulaList['ink'] = $v['ink'];
  189. $formulaList['number'] = round($weight / $v['number'] * $v['total'] / 1000, 3);
  190. $formulaList['code'] = $v['code'];
  191. array_push($consume, $formulaList);
  192. }
  193. }
  194. unset($consume[$i]);
  195. }
  196. }
  197. $consume = array_values($consume);
  198. foreach ($consume as &$item) {
  199. $total[] = $item;
  200. }
  201. $consumeList[$k]['gdbh'] = $val['gdbh'];
  202. $consumeList[$k]['cpdh'] = $val['cpdh'];
  203. $consumeList[$k]['name'] = trim($val['name']);
  204. $consumeList[$k]['num'] = $val['num'];
  205. $consumeList[$k]['inkData'] = $this->sumWeight($consume, 'code', 'number');
  206. $consumeList[$k]['total'] = count($consumeList[$k]['inkData']);
  207. }
  208. $bach = date('Ymd') . rand(100, 1000);
  209. $totalList = $this->sumWeight($total, 'code', 'number');
  210. return [
  211. 'valid' => true,
  212. 'msg' => 'ok',
  213. 'data' => compact('consumeList', 'totalList', 'bach'),
  214. ];
  215. }
  216. /**
  217. * 新增领料单数据保存
  218. */
  219. public function doAdd()
  220. {
  221. if ($this->request->isPost()) {
  222. $headData = $this->request->post("headData/a");
  223. $footData = $this->request->post("footData/a");
  224. if ($headData && $footData) {
  225. $headData = $this->preExcludeFields($headData);
  226. //处理每个工单领料的数据
  227. $headList = [];
  228. foreach ($headData as $key =>$value){
  229. if (count($value) == 5){
  230. $headList[$key] = $value;
  231. unset($headData[$key]);
  232. }
  233. }
  234. $headRes = [];//定义插入数据库的数组
  235. $bach = $footData[0][0];//获取单据编号用于详细数据
  236. $order = [];
  237. foreach ($headList as $k=>$v){
  238. $headData = array_values($headData);
  239. $number = $v[4];
  240. $order[$k] = $v[0];
  241. foreach ($headData as $i=>$j){
  242. if ($i < $number){
  243. $res = [];
  244. $res['picking'] = $bach;
  245. $res['order'] = $v[0];
  246. $res['product_name'] = $v[1];
  247. $res['number'] = $v[2];
  248. $res['matter'] = $j[1];
  249. $res['matter_code'] = $j[0];
  250. $res['weight'] = $j[2];
  251. $res['create'] = date('Y-m-d H:i:s');
  252. array_push($headRes,$res);
  253. unset($headData[$i]);
  254. }
  255. }
  256. }
  257. //处理领料单统计数据
  258. $orderNumber = implode(',',$order);
  259. $footRes = [];//定义插入数据库的数组
  260. foreach ($footData as $key1=>$value1){
  261. if ($key1 > 0){
  262. $result = [];
  263. $result['picking'] = $bach;
  264. $result['order'] = $orderNumber;
  265. $result['matter'] = $value1[1];
  266. $result['code'] = $value1[0];
  267. $result['weight'] = $value1[2];
  268. $result['create'] = date('Y-m-d H:i:s');
  269. array_push($footRes,$result);
  270. }
  271. }
  272. $result = false;
  273. Db::startTrans();
  274. try {
  275. $resOne = Db::name('picking')->insertAll($footRes);
  276. $resTwo = Db::name('picking_detail')->insertAll($headRes);
  277. Db::commit();
  278. if ($resOne && $resTwo){
  279. $result = true;
  280. }
  281. } catch (ValidateException $e) {
  282. Db::rollback();
  283. $this->error($e->getMessage());
  284. } catch (PDOException $e) {
  285. Db::rollback();
  286. $this->error($e->getMessage());
  287. } catch (Exception $e) {
  288. Db::rollback();
  289. $this->error($e->getMessage());
  290. }
  291. if ($result !== false) {
  292. // $this->success('保存成功','picking/index');
  293. return array('code'=>1,'msg'=>'保存成功');
  294. } else {
  295. $this->error(__('No rows were inserted'));
  296. }
  297. }
  298. $this->error(__('Parameter %s can not be empty', ''));
  299. }
  300. // return $this->view->fetch();
  301. }
  302. /**
  303. * 判断是大盒还是小盒
  304. * 条盒:条盒、条包装纸
  305. * 小盒:小盒、盒包装纸、烟盒、商标
  306. */
  307. public function isBigOrSmall($name){
  308. $result = -1;
  309. if (!empty($name)){
  310. $boxName = ['条盒','条包装纸'];
  311. $smallName = ['小盒','盒包装纸','烟盒','商标'];
  312. $smallCover = ['小盒上盖'];
  313. $lining = ['内衬纸'];
  314. $sticker = ['内贴'];
  315. $vegetable = ['硫酸纸'];
  316. foreach ($boxName as $key=>$value){
  317. $res = strpos($name,$value);
  318. if ($res !== false){
  319. $result = 1;
  320. }
  321. }
  322. foreach ($smallName as $k=>$val){
  323. $res = strpos($name,$val);
  324. if ($res !== false){
  325. $result = 2;
  326. }
  327. }
  328. foreach ($smallCover as $i=>$item){
  329. $res = strpos($name,$item);
  330. if ($res !== false){
  331. $result = 3;
  332. }
  333. }
  334. foreach ($lining as $j=>$vol){
  335. $res = strpos($name,$vol);
  336. if ($res !== false){
  337. $result = 4;
  338. }
  339. }
  340. foreach ($sticker as $p=>$v){
  341. $res = strpos($name,$v);
  342. if ($res !== false){
  343. $result = 5;
  344. }
  345. }
  346. foreach ($vegetable as $o=>$vo){
  347. $res = strpos($name,$vo);
  348. if ($res !== false){
  349. $result = 6;
  350. }
  351. }
  352. return $result;
  353. }
  354. return $result;
  355. }
  356. /**
  357. * 计算原墨数量,值去重&相加
  358. */
  359. function sumWeight($arr,$str1,$str2) {
  360. $item = array();
  361. foreach ($arr as $k => $v) {
  362. if (!isset($item[$v[$str1]])) {
  363. $item[$v[$str1]] = $v;
  364. } else {
  365. $item[$v[$str1]][$str2] += $v[$str2];
  366. }
  367. }
  368. return array_values($item);
  369. }
  370. }