model = new \app\admin\model\Picking; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ public function order(){ $data = $this->request->request(); if ($this->request->isAjax()) { $params['pagesize'] = is_null($data['limit']) ? 10 : $data['limit'];//初始查询位置 if ($data['limit'] == 0){ $params['page'] = 1; }else{ $params['page'] = $data['offset'] / 10 + 1; } $search = array(); if (!empty($data['filter'])){ $search = json_decode($data['filter'],true); } // print_r($search);die; /** * year(Gy0_sj1)=1900 表明尚未排单 * year(Gy0_sj1)>1900 表明在排单中, * Pd_wg为真,表明工序结束 * */ if (!empty($search)){ $gdbh = isset($search['Gd_gdbh'])?$search['Gd_gdbh']:''; $jtbh = isset($search['Gy0_sbbh'])?'%'.$search['Gy0_sbbh'].'%':'%JY%'; if ($gdbh){ $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 LEFT JOIN `工单_工艺资料` b ON a.Gd_gdbh = b.Gy0_gdbh WHERE (a.gd_statu = '2-生产中' OR a.gd_statu='3-计划中') AND a.Gd_cpmc !='' AND a.行号 = 1 AND b.Gy0_sbbh LIKE '{$jtbh}' AND YEAR(b.Gy0_sj1) > 1900 AND YEAR(b.Gy0_sj1)<2099 AND YEAR(b.PD_WG) = 1900 AND a.Gd_gdbh = {$gdbh} ORDER BY b.Gy0_sbbh ASC ,b.Gy0_sj1 ASC "; }else{ $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 LEFT JOIN `工单_工艺资料` b ON a.Gd_gdbh = b.Gy0_gdbh WHERE (a.gd_statu = '2-生产中' OR a.gd_statu='3-计划中') AND a.Gd_cpmc !='' AND a.行号 = 1 AND b.Gy0_sbbh LIKE '%{$jtbh}%' AND YEAR(b.Gy0_sj1) > 1900 AND YEAR(b.Gy0_sj1)<2099 AND YEAR(b.PD_WG) = 1900 ORDER BY b.Gy0_sbbh ASC ,b.Gy0_sj1 ASC "; } $res = Db::connect('db2') ->query($sql); }else{ $res = Db::connect('db2') ->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 LEFT JOIN `工单_工艺资料` b ON a.Gd_gdbh = b.Gy0_gdbh WHERE (a.gd_statu = '2-生产中' OR a.gd_statu='3-计划中') AND a.Gd_cpmc !='' AND a.行号 = 1 AND b.Gy0_sbbh LIKE '%JY%' AND YEAR(b.Gy0_sj1) > 1900 AND YEAR(b.Gy0_sj1)<2099 AND YEAR(b.PD_WG) = 1900 ORDER BY b.Gy0_sbbh ASC ,b.Gy0_sj1 ASC "); } foreach ($res as $key=>$val){ $id = $val['Gd_gdbh']; $res[$key]['id'] = $id; } $total = count($res); // $res = array_slice($res,$data['offset'],$data['limit']);//分页 $result = array("total" => $total, "rows" => $res); return json($result); } return $this->view->fetch(); } /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); // dump($list);die; $list = collection($list)->toArray(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 领料单界面数据 */ public function picking(){ $data = input('temp'); $gdbh = array_filter(explode(',',$data)); if (empty($gdbh)){ $this->error('请求参数错误'); } $list = []; foreach ($gdbh as $key=>$value){ $sql = "SELECT GD_gdbh AS gdbh,成品名称 as name,Gd_cpdh AS cpdh,计划投料 AS num FROM `工单_基本资料` WHERE Gd_gdbh = {$value} AND `行号`= 1"; $res = Db::connect('db2')->query($sql); $list[$key] = $res[0]; } $consumeList = [];//原墨数据 $formulaList = []; $total = []; foreach ($list as $k=>$val){ $box = $this->isBigOrSmall(trim($val['name'])); if ($box == -1){ $this->error('系统找不到产品对应大小盒'); } $productNumber = trim($val['cpdh']); $where = []; $where[] = ['exp',Db::raw("FIND_IN_SET($productNumber,product_number)")]; $consume = Db::name('formula_consume')->where($where)->where('number','neq','')->where('box',$box)->field('ink,number,code')->select(); $formmula= Db::name('formula_detail')->where($where)->field('color,ink,ink_number,number,total,code')->select(); // dump($consume);die; if (empty($consume)){ $this->error('产品代码未更新,请联系开发人员'); } $num = count($consume); for ($i=0;$i<$num;$i++){ $weight = round(trim($consume[$i]['number']) * $val['num'] / 10000,3); $consume[$i]['number'] = $weight; if (empty($consume[$i]['code'])){ foreach ($formmula as $j=>$v){ if ($v['color'] == $consume[$i]['ink']){ $formulaList['ink'] = $v['ink']; $formulaList['number'] = round($weight / $v['number'] * $v['total'] /1000,3); //计划用量/固定数值*总计用量 $formulaList['code'] = $v['code']; array_push($consume,$formulaList); } } unset($consume[$i]); } } //得到专墨分解成原墨的数据 、 原墨与专墨分解的原墨相加 $consume = array_values($consume); foreach ($consume as &$item){ $sql = ""; $total[] = $item;//做为返回总量数据数组 } $consumeList[$k]['gdbh'] = $val['gdbh']; $consumeList[$k]['cpdh'] = $val['cpdh']; $consumeList[$k]['name'] = trim($val['name']); $consumeList[$k]['num'] = $val['num']; $consumeList[$k]['inkData'] = $this->sumWeight($consume,'code','number'); $consumeList[$k]['total'] = count($consumeList[$k]['inkData']); } $bach = date('Ymd').rand(100,1000); $totalList = $this->sumWeight($total,'code','number'); return $this->view->fetch('add',compact('consumeList','totalList','bach')); } /** * 新增领料单数据保存 */ public function doAdd() { if ($this->request->isPost()) { $headData = $this->request->post("headData/a"); $footData = $this->request->post("footData/a"); if ($headData && $footData) { $headData = $this->preExcludeFields($headData); //处理每个工单领料的数据 $headList = []; foreach ($headData as $key =>$value){ if (count($value) == 5){ $headList[$key] = $value; unset($headData[$key]); } } $headRes = [];//定义插入数据库的数组 $bach = $footData[0][0];//获取单据编号用于详细数据 $order = []; foreach ($headList as $k=>$v){ $headData = array_values($headData); $number = $v[4]; $order[$k] = $v[0]; foreach ($headData as $i=>$j){ if ($i < $number){ $res = []; $res['picking'] = $bach; $res['order'] = $v[0]; $res['product_name'] = $v[1]; $res['number'] = $v[2]; $res['matter'] = $j[1]; $res['matter_code'] = $j[0]; $res['weight'] = $j[2]; $res['create'] = date('Y-m-d H:i:s'); array_push($headRes,$res); unset($headData[$i]); } } } //处理领料单统计数据 $orderNumber = implode(',',$order); $footRes = [];//定义插入数据库的数组 foreach ($footData as $key1=>$value1){ if ($key1 > 0){ $result = []; $result['picking'] = $bach; $result['order'] = $orderNumber; $result['matter'] = $value1[1]; $result['code'] = $value1[0]; $result['weight'] = $value1[2]; $result['create'] = date('Y-m-d H:i:s'); array_push($footRes,$result); } } $result = false; Db::startTrans(); try { $resOne = Db::name('picking')->insertAll($footRes); $resTwo = Db::name('picking_detail')->insertAll($headRes); Db::commit(); if ($resOne && $resTwo){ $result = true; } } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { // $this->success('保存成功','picking/index'); return array('code'=>1,'msg'=>'保存成功'); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } // return $this->view->fetch(); } /** * 判断是大盒还是小盒 * 条盒:条盒、条包装纸 * 小盒:小盒、盒包装纸、烟盒、商标 */ public function isBigOrSmall($name){ $result = -1; if (!empty($name)){ $boxName = ['条盒','条包装纸']; $smallName = ['小盒','盒包装纸','烟盒','商标']; $smallCover = ['小盒上盖']; $lining = ['内衬纸']; $sticker = ['内贴']; $vegetable = ['硫酸纸']; foreach ($boxName as $key=>$value){ $res = strpos($name,$value); if ($res !== false){ $result = 1; } } foreach ($smallName as $k=>$val){ $res = strpos($name,$val); if ($res !== false){ $result = 2; } } foreach ($smallCover as $i=>$item){ $res = strpos($name,$item); if ($res !== false){ $result = 3; } } foreach ($lining as $j=>$vol){ $res = strpos($name,$vol); if ($res !== false){ $result = 4; } } foreach ($sticker as $p=>$v){ $res = strpos($name,$v); if ($res !== false){ $result = 5; } } foreach ($vegetable as $o=>$vo){ $res = strpos($name,$vo); if ($res !== false){ $result = 6; } } return $result; } return $result; } /** * 计算原墨数量,值去重&相加 */ function sumWeight($arr,$str1,$str2) { $item = array(); foreach ($arr as $k => $v) { if (!isset($item[$v[$str1]])) { $item[$v[$str1]] = $v; } else { $item[$v[$str1]][$str2] += $v[$str2]; } } return array_values($item); } }