Picking.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. $data = input('temp');
  120. $gdbh = array_filter(explode(',',$data));
  121. if (empty($gdbh)){
  122. $this->error('请求参数错误');
  123. }
  124. $list = [];
  125. foreach ($gdbh as $key=>$value){
  126. $sql = "SELECT GD_gdbh AS gdbh,成品名称 as name,Gd_cpdh AS cpdh,计划投料 AS num FROM `工单_基本资料` WHERE Gd_gdbh = {$value} AND `行号`= 1";
  127. $res = Db::connect('db2')->query($sql);
  128. $list[$key] = $res[0];
  129. }
  130. $consumeList = [];//原墨数据
  131. $formulaList = [];
  132. $total = [];
  133. foreach ($list as $k=>$val){
  134. $box = $this->isBigOrSmall(trim($val['name']));
  135. if ($box == -1){
  136. $this->error('系统找不到产品对应大小盒');
  137. }
  138. $productNumber = trim($val['cpdh']);
  139. $where = [];
  140. $where[] = ['exp',Db::raw("FIND_IN_SET($productNumber,product_number)")];
  141. $consume = Db::name('formula_consume')->where($where)->where('number','neq','')->where('box',$box)->field('ink,number,code')->select();
  142. $formmula= Db::name('formula_detail')->where($where)->field('color,ink,ink_number,number,total,code')->select();
  143. // dump($consume);die;
  144. if (empty($consume)){
  145. $this->error('产品代码未更新,请联系开发人员');
  146. }
  147. $num = count($consume);
  148. for ($i=0;$i<$num;$i++){
  149. $weight = round(trim($consume[$i]['number']) * $val['num'] / 10000,3);
  150. $consume[$i]['number'] = $weight;
  151. if (empty($consume[$i]['code'])){
  152. foreach ($formmula as $j=>$v){
  153. if ($v['color'] == $consume[$i]['ink']){
  154. $formulaList['ink'] = $v['ink'];
  155. $formulaList['number'] = round($weight / $v['number'] * $v['total'] /1000,3); //计划用量/固定数值*总计用量
  156. $formulaList['code'] = $v['code'];
  157. array_push($consume,$formulaList);
  158. }
  159. }
  160. unset($consume[$i]);
  161. }
  162. }
  163. //得到专墨分解成原墨的数据 、 原墨与专墨分解的原墨相加
  164. $consume = array_values($consume);
  165. foreach ($consume as &$item){
  166. $sql = "";
  167. $total[] = $item;//做为返回总量数据数组
  168. }
  169. $consumeList[$k]['gdbh'] = $val['gdbh'];
  170. $consumeList[$k]['cpdh'] = $val['cpdh'];
  171. $consumeList[$k]['name'] = trim($val['name']);
  172. $consumeList[$k]['num'] = $val['num'];
  173. $consumeList[$k]['inkData'] = $this->sumWeight($consume,'code','number');
  174. $consumeList[$k]['total'] = count($consumeList[$k]['inkData']);
  175. }
  176. $bach = date('Ymd').rand(100,1000);
  177. $totalList = $this->sumWeight($total,'code','number');
  178. return $this->view->fetch('add',compact('consumeList','totalList','bach'));
  179. }
  180. /**
  181. * 新增领料单数据保存
  182. */
  183. public function doAdd()
  184. {
  185. if ($this->request->isPost()) {
  186. $headData = $this->request->post("headData/a");
  187. $footData = $this->request->post("footData/a");
  188. if ($headData && $footData) {
  189. $headData = $this->preExcludeFields($headData);
  190. //处理每个工单领料的数据
  191. $headList = [];
  192. foreach ($headData as $key =>$value){
  193. if (count($value) == 5){
  194. $headList[$key] = $value;
  195. unset($headData[$key]);
  196. }
  197. }
  198. $headRes = [];//定义插入数据库的数组
  199. $bach = $footData[0][0];//获取单据编号用于详细数据
  200. $order = [];
  201. foreach ($headList as $k=>$v){
  202. $headData = array_values($headData);
  203. $number = $v[4];
  204. $order[$k] = $v[0];
  205. foreach ($headData as $i=>$j){
  206. if ($i < $number){
  207. $res = [];
  208. $res['picking'] = $bach;
  209. $res['order'] = $v[0];
  210. $res['product_name'] = $v[1];
  211. $res['number'] = $v[2];
  212. $res['matter'] = $j[1];
  213. $res['matter_code'] = $j[0];
  214. $res['weight'] = $j[2];
  215. $res['create'] = date('Y-m-d H:i:s');
  216. array_push($headRes,$res);
  217. unset($headData[$i]);
  218. }
  219. }
  220. }
  221. //处理领料单统计数据
  222. $orderNumber = implode(',',$order);
  223. $footRes = [];//定义插入数据库的数组
  224. foreach ($footData as $key1=>$value1){
  225. if ($key1 > 0){
  226. $result = [];
  227. $result['picking'] = $bach;
  228. $result['order'] = $orderNumber;
  229. $result['matter'] = $value1[1];
  230. $result['code'] = $value1[0];
  231. $result['weight'] = $value1[2];
  232. $result['create'] = date('Y-m-d H:i:s');
  233. array_push($footRes,$result);
  234. }
  235. }
  236. $result = false;
  237. Db::startTrans();
  238. try {
  239. $resOne = Db::name('picking')->insertAll($footRes);
  240. $resTwo = Db::name('picking_detail')->insertAll($headRes);
  241. Db::commit();
  242. if ($resOne && $resTwo){
  243. $result = true;
  244. }
  245. } catch (ValidateException $e) {
  246. Db::rollback();
  247. $this->error($e->getMessage());
  248. } catch (PDOException $e) {
  249. Db::rollback();
  250. $this->error($e->getMessage());
  251. } catch (Exception $e) {
  252. Db::rollback();
  253. $this->error($e->getMessage());
  254. }
  255. if ($result !== false) {
  256. // $this->success('保存成功','picking/index');
  257. return array('code'=>1,'msg'=>'保存成功');
  258. } else {
  259. $this->error(__('No rows were inserted'));
  260. }
  261. }
  262. $this->error(__('Parameter %s can not be empty', ''));
  263. }
  264. // return $this->view->fetch();
  265. }
  266. /**
  267. * 判断是大盒还是小盒
  268. * 条盒:条盒、条包装纸
  269. * 小盒:小盒、盒包装纸、烟盒、商标
  270. */
  271. public function isBigOrSmall($name){
  272. $result = -1;
  273. if (!empty($name)){
  274. $boxName = ['条盒','条包装纸'];
  275. $smallName = ['小盒','盒包装纸','烟盒','商标'];
  276. $smallCover = ['小盒上盖'];
  277. $lining = ['内衬纸'];
  278. $sticker = ['内贴'];
  279. $vegetable = ['硫酸纸'];
  280. foreach ($boxName as $key=>$value){
  281. $res = strpos($name,$value);
  282. if ($res !== false){
  283. $result = 1;
  284. }
  285. }
  286. foreach ($smallName as $k=>$val){
  287. $res = strpos($name,$val);
  288. if ($res !== false){
  289. $result = 2;
  290. }
  291. }
  292. foreach ($smallCover as $i=>$item){
  293. $res = strpos($name,$item);
  294. if ($res !== false){
  295. $result = 3;
  296. }
  297. }
  298. foreach ($lining as $j=>$vol){
  299. $res = strpos($name,$vol);
  300. if ($res !== false){
  301. $result = 4;
  302. }
  303. }
  304. foreach ($sticker as $p=>$v){
  305. $res = strpos($name,$v);
  306. if ($res !== false){
  307. $result = 5;
  308. }
  309. }
  310. foreach ($vegetable as $o=>$vo){
  311. $res = strpos($name,$vo);
  312. if ($res !== false){
  313. $result = 6;
  314. }
  315. }
  316. return $result;
  317. }
  318. return $result;
  319. }
  320. /**
  321. * 计算原墨数量,值去重&相加
  322. */
  323. function sumWeight($arr,$str1,$str2) {
  324. $item = array();
  325. foreach ($arr as $k => $v) {
  326. if (!isset($item[$v[$str1]])) {
  327. $item[$v[$str1]] = $v;
  328. } else {
  329. $item[$v[$str1]][$str2] += $v[$str2];
  330. }
  331. }
  332. return array_values($item);
  333. }
  334. }