| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- use think\Exception;
- use think\Request;
- /**
- * 油墨领用退还管理
- *
- * @icon fa fa-circle-o
- */
- class CollarUse extends Backend
- {
- /**
- * CollarUse模型对象
- * @var \app\admin\model\CollarUse
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\CollarUse;
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("isScrapList", $this->model->getIsScrapList());
- $this->view->assign("lStatusList", $this->model->getLStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- //专墨
- public function index(){
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- // $params = $this->request->request();
- // print_r($params);die;
- //如果发送的来源是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();
- $list = collection($list)->toArray();
- foreach ($list as $key=>$value){
- if ($value['status'] == 1){
- $ink = Db::name('warehousing_detail')->where('bach_number',$value['warehousing'])->find();
- }else{
- $ink = Db::name('warehousing_detail')->where('bach_number',$value['back'])->find();
- }
- $list[$key]['color'] = $ink['color'];
- $list[$key]['formula'] = $ink['formula'];
- $list[$key]['weight'] = $value['weight']/1000;
- $list[$key]['back_weight'] = $value['back_weight']/1000;
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- //原墨
- public function ink(){
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- // $params = $this->request->request();
- // print_r($params);die;
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = Db::name('matter_use')
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = Db::name('matter_use')
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $list = collection($list)->toArray();
- foreach ($list as $key=>$value){
- $list[$key]['out_weight'] = $value['out_weight']/1000;
- $list[$key]['back_weight'] = $value['back_weight']/1000;
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- public function add(){
- $params = input('bach');
- if ($params == '' || empty($params) || !is_numeric($params)){
- return array('status'=>0,'msg'=>'工单输入错误');
- }
- //根据工单批次号获取对应配方、库存信息
- $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
- if (empty($product)){
- return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
- }
- $product_number = rtrim($product[0]['Gd_cpdh']);
- $product_name = trim($product[0]['Gd_cpmc']);
- $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,a.product_number)")];
- //专墨颜色
- $colorResult = Db::name('formula')->alias('a')
- ->join('warehousing b','a.id=b.cid','left')
- ->where($where)
- ->field('a.*,b.weight')
- ->select();
- // print_r($colorResult);die;
- foreach ($colorResult as $key=>$value){
- if (empty($value['weight'])){
- $colorResult[$key]['weight'] = 0;
- }else{
- $colorResult[$key]['weight'] = $value['weight'] / 1000;
- }
- }
- //原墨
- $map[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
- $inkData = Db::name('formula_consume')->where($map)->where('code','neq','')->field('id,ink,code')->select();
- $inkData = $this->assoc_unique($inkData,'ink');
- $jar = Db::name('jar')->order('sort')->select();
- return $this->view->fetch('',compact('params','product_name','colorResult','jar','inkData'));
- }
- //退料
- public function back(){
- if ($this->request->isAjax()){
- $data = input('data/a');
- $code = input('code');
- $order_number = input('bach');
- // print_r($order_number);die;
- $idArray = array();
- if ($code == 1){//代表扫描的是工单号
- foreach ($data as $key=>$value){
- if (strlen($value['cid']) >= 9){ //大于等于9代表退的是原墨
- if (!empty($value['weight'])){
- $back = array();
- $back['order_number'] = $order_number;
- $back['code'] = $value['cid'];
- $back['ink_name'] = $value['name'];
- $back['status'] = 2;
- $bach = Db::name('matter_use')->where('order_number',$order_number)->value('out');
- $back['back'] = $bach;
- $back['back_weight'] = $value['weight'] * 1000;
- $back['machine_number'] = substr($value['machine'],0,1);
- $back['create'] = date('Y-m-d H:i:s');
- $res = Db::name('matter_use')->insert($back);
- if ($res){
- $matter = Db::name('matter_detail')->where('code',$back['code'])->order('id')->find();
- $matterWeight = $matter['weight'] + $back['back_weight'];
- Db::name('matter_detail')->where('id',$matter['id'])->setField('weight',$matterWeight);
- $t_matter = Db::name('matter')->where('code',$back['code'])->find();
- $t_weight = $t_matter['weight'] + $back['back_weight'];
- Db::name('matter')->where('id',$t_matter['id'])->setField('weight',$t_weight);
- }
- }
- }else{ //长度小于9代表退的是专色墨 //先退回到专墨,再插入一条退回数据
- if (!empty($value['weight'])){
- $warehousing = Db::name('warehousing_detail')->where('cid',$value['cid'])->order('id desc')->find();
- $total = Db::name('warehousing')->where('cid',$value['cid'])->find();
- if (empty($warehousing) || empty($total)){
- return array('status'=>0);
- }
- $warehousingWeight = $warehousing['weight'] + $value['weight'] * 1000;//详情表退回重量
- $updateWarehousing = Db::name('warehousing_detail')->where('id',$warehousing['id'])->setField('weight',$warehousingWeight);
- $totalWeight = $total['weight'] + $value['weight'] * 1000;
- $updateWeight = Db::name('warehousing')->where('id',$total['id'])->setField('weight',$totalWeight);
- $insert = array();
- $insert['order_number'] = $order_number;
- $insert['product_name'] = Db::name('collar_use')->where('order_number',$order_number)->value('product_name');
- $insert['status'] = 2;
- $insert['cid'] = $value['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
- $insert['back'] = $warehousing['bach_number'];
- $insert['back_weight'] = $value['weight'] * 1000;
- $insert['machine_number'] = substr($value['machine'],0,1);
- $insert['create'] = date('Y-m-d H:i:s');
- $id = Db::name('collar_use')->insertGetId($insert);
- $idArray[$key] = $id;
- }
- }
- }
- $result = Db::name('collar_use')->where('id','in',$idArray)->select();
- return array('status'=>1,'data'=>$result);
- }else{//扫描的是专墨批次号
- foreach ($data as $key=>$value){
- $warehousing = Db::name('warehousing_detail')->where('bach_number',$value['bach'])->find();
- $weight = $warehousing['weight'] + $value['weight'] * 1000;
- Db::name('warehousing_detail')->where('id',$warehousing['id'])->setField('weight',$weight);
- $total = Db::name('warehousing')->where('cid',$warehousing['cid'])->find();
- $totalWeight = $total['weight'] + $value['weight'] * 1000;
- Db::name('warehousing')->where('id',$total['id'])->setField('weight',$totalWeight);
- $insert = array();
- $insert['order_number'] = $warehousing['order_number'];
- $insert['product_name'] = $warehousing['product_name'];
- $insert['status'] = 2;
- $insert['cid'] = $warehousing['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
- $insert['back'] = $value['bach'];
- $insert['back_weight'] = $value['weight'] * 1000;
- $insert['machine_number'] = substr($value['machine'],0,1);
- $insert['create'] = date('Y-m-d H:i:s');
- $id = Db::name('collar_use')->insertGetId($insert);
- $idArray[$key] = $id;
- }
- $result = Db::name('collar_use')->alias('a')
- ->join('warehousing_detail b','a.back = b.bach_number')
- ->where('a.id','in',$idArray)
- ->field('a.*,b.color')
- ->select();
- return array('status'=>1,'data'=>$result);
- }
- }
- $jar = Db::name('jar')->order('sort')->select();
- return $this->view->fetch('',compact('jar'));
- }
- //出库
- public function out(){
- // return array('status'=>1,'msg'=>'出库成功');
- $params = input('data/a');
- $inkData = input('ink/a');
- // print_r($params);die;
- //专墨出库
- if (!empty($params)){
- foreach ($params as $key=>$value){
- if (empty($value['weight']) || $value['weight'] == 0){
- continue;
- }else{
- //根据工单批次号获取对应配方、库存信息
- $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$value["order_number"]}' limit 1");
- if (empty($product)){
- return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
- }
- // $product_number = rtrim($product[0]['Gd_cpdh']);
- $product_name = rtrim($product[0]['成品名称']);
- $warehousing = Db::name('warehousing_detail')->where('cid',$value['cid'])->where('weight','>',0)->order('id')->select();
- if (empty($warehousing)){
- return array('status'=>0,'msg'=>'库存重量为0,出库失败');
- }
- //插入一条出库数据
- $collarData = array();
- $collarData['order_number'] = $value['order_number'];
- $collarData['product_name'] = $product_name;
- $collarData['warehousing'] = $warehousing[0]['bach_number'];
- $collarData['status'] = 1;
- $collarData['cid'] = $value['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
- $collarData['weight'] = $value['weight'] * 1000;
- $collarData['l_status'] = 2;
- $collarData['machine_number'] = $value['machine_number'];
- $collarData['create'] = date('Y-m-d H:i:s');
- $collarId = Db::name('collar_use')->insertGetId($collarData);
- //扣减库存,递归模式,直到$warehousing[x]['weight] - $weight(这个值是各个详细重量减去后的值) > 0, 返回数量,扣减这个数量之前所有的库存
- if(count($warehousing) == 1){
- $newWeight = $warehousing[0]['weight'] - $collarData['weight'];
- if ($newWeight < 0){
- Db::name('warehousing_detail')->where('id',$warehousing[0]['id'])->setField('weight',0);
- }else{
- Db::name('warehousing_detail')->where('id',$warehousing[0]['id'])->setField('weight',$newWeight);
- }
- }else{
- $key = $this->whatNumber($collarData['weight'],$warehousing);
- $weight = 0;
- $idArray = [];
- $collarBach = '';//领走的批次号
- $collarWeight= '';//领走的批次号重量
- for ($i=0;$i<$key;$i++){
- $weight = $weight + $warehousing[$i]['weight'];
- $idArray[$i] = $warehousing[$i]['id'];
- //2022年3月26日11:09:38 新增
- $collarBach = $warehousing[$i]['bach_number'].','.$collarBach;//将领走的各个批次号做成字符串
- $collarWeight = $warehousing[$i]['weight'].','.$collarWeight;//将领走的各个批次号重量做成字符串
- }
- $weight = $collarData['weight'] - $weight ; //这个值是大于0的 将前面的库存扣减为0 这个按值扣减
- $newWeight = $warehousing[$key]['weight'] - $weight;
- //2022年3月26日11:09:44 新增
- $collarBach = $warehousing[$key]['bach_number'].','.$collarBach;
- $collarWeight = $weight.','.$collarWeight;
- // $collarData['process'] = $collarBach;//暂时将此字段作为领走时的专墨批次号字段
- // $collarData['current_process'] = $collarWeight;//暂时将此字段作为领走时的专墨批次号字段
- Db::name('collar_use')->where('id',$collarId)->setField('process',$collarBach);
- Db::name('collar_use')->where('id',$collarId)->setField('current_process',$collarWeight);
- if ($newWeight < 0){
- $newWeight = 0;
- }
- Db::name('warehousing_detail')->where('id','in',$idArray)->setField('weight',0);
- Db::name('warehousing_detail')->where('id',$warehousing[$key]['id'])->setField('weight',$newWeight);
- }
- //扣减总库存
- $warehousingWeight = Db::name('warehousing')->where('cid',$value['cid'])->value('weight');
- $totalWeight = $warehousingWeight - $collarData['weight'];
- if ($totalWeight < 0){
- Db::name('warehousing')->where('cid',$value['cid'])->setField('weight',0);
- }else{
- Db::name('warehousing')->where('cid',$value['cid'])->setField('weight',$totalWeight);
- }
- }
- }
- }
- if (!empty($inkData)){
- foreach ($inkData as $k=>$v){
- //插入一条领用数据
- $ink = array();
- $ink['order_number'] = $v['order_number'];
- $ink['code'] = $v['code'];
- $ink['ink_name'] = $v['ink'];
- $ink['machine_number'] = $v['machine_number'];
- $ink['out_weight'] = $v['weight'] * 1000;
- $ink['status'] = 1;
- $ink['create'] = date('Y-m-d H:i:s');
- $bach = Db::name('matter_detail')->where('code',$v['code'])->value('bach');
- if (!empty($bach)){
- $ink['out'] = $bach;
- }else{
- $ink['out'] = '';
- }
- $insert = Db::name('matter_use')->insert($ink);
- //扣减库存
- if ($insert){
- $matter = Db::name('matter_detail')->where('code',$v['code'])->order('id')->find();
- $matterWeight = $matter['weight'] - $ink['out_weight'];
- Db::name('matter_detail')->where('id',$matter['id'])->setField('weight',$matterWeight);
- $t_matter = Db::name('matter')->where('code',$v['code'])->find();
- $t_weight = $t_matter['weight'] - $ink['out_weight'];
- Db::name('matter')->where('id',$t_matter['id'])->setField('weight',$t_weight);
- }
- }
- }
- return array('status'=>1,'msg'=>'出库成功');
- }
- //根据生产批次去线上库获取领用记录
- public function getOrder(){
- $params = input('order_number');
- if (!empty($params)){
- $where['a.status'] = 1;
- $time = date('Y-m-d H:i:s');
- $lastTime = date('Y-m-d H:i:s',strtotime($time) - 60);//上一分钟
- $data = Db::name('collar_use')->alias('a')
- ->join('warehousing_detail b','a.warehousing = b.bach_number')
- ->where('a.order_number',$params)
- ->where($where)
- ->where('a.create','between',[$lastTime,$time])
- ->field('a.*,b.color')
- ->select();
- return array('status'=>1,'data'=>$data);
- }
- }
- //获取机台号
- public function getWeight(){
- $params = input('bach');
- if (empty($params)){
- return array('status'=>0,'msg'=>'数据不能为空');
- }
- $data = Db::name('collar_use')->where('warehousing',$params)->order('id desc')->find();
- $machine = $data['machine_number'].'#';
- return array('status'=>1,'data'=>$machine);
- }
- //根据工单号获取配方
- public function getColor(){
- $params = input('order');
- if (empty($params)){
- return array('status'=>0,'msg'=>'数据不能为空');
- }
- //根据工单批次号获取对应配方、库存信息
- $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
- if (empty($product)){
- return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
- }
- $product_number = rtrim($product[0]['Gd_cpdh']);
- $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
- $colorResult = Db::name('formula')
- ->where($where)
- ->select();
- $inkResult = Db::name('formula_consume')
- ->where($where)
- ->where('code','neq','')
- ->field('id,ink,code')
- ->select();
- $inkResult = $this->assoc_unique($inkResult,'code');
- $machine = Db::name('collar_use')->where('order_number',$params)->order('id asc')->find();
- $machine_number = $machine['machine_number'].'#';
- return array('status'=>1,'data'=>$colorResult,'machine_number'=>$machine_number,'ink'=>$inkResult);
- }
- //二维数组去重
- function assoc_unique($arr, $key) {
- $tmp_arr = array();
- foreach ($arr as $k => $v) {
- if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
- unset($arr[$k]);
- } else {
- $tmp_arr[] = $v[$key];
- }
- }
- sort($arr); //sort函数对数组进行排序
- return $arr;
- }
- //扣减库存递归
- public function whatNumber($weight,$array=[],$i=0){
- $surplusWeight =$weight - $array[$i]['weight'] ;
- if ($surplusWeight > 0){
- $i = $i + 1;
- return $this->whatNumber($surplusWeight,$array,$i);
- }else{
- return $i;
- }
- }
- }
|