Browse Source

批量添加入库单

wufeng 2 years ago
parent
commit
be5049f9f8
3 changed files with 362 additions and 1 deletions
  1. 343 0
      app/admin/controller/Purchin.php
  2. 18 1
      app/admin/controller/Sale.php
  3. 1 0
      app/admin/route/app.php

+ 343 - 0
app/admin/controller/Purchin.php

@@ -11,6 +11,7 @@ use app\BaseController;
 use think\Exception;
 use think\facade\Db;
 use think\App;
+use think\facade\Validate;
 
 //采购单入库
 class Purchin extends Base
@@ -620,6 +621,348 @@ class Purchin extends Base
         }
     }
 
+    //批量添加入库单
+    public function addBatch()
+    {
+
+        $param = $this->request->only(['cgdNo', 'list'], 'post', 'trim');
+
+        $val = Validate::rule([
+            'cgdNo|采购单编号' => 'require',
+            'list' => 'require|array|max:100',
+        ]);
+
+        if ($val->check($param) == false) return json_show(1004, $val->getError());
+
+        $val_child = Validate::rule([
+            'send_num|入库数量' => 'require|number|gt:0',
+            'sendtype|发货方式' => 'require|number|in:1,2',
+            'post_time|发货时间' => 'require|date',
+            'post_fee|物流费用' => 'require|float|max:99999999.99',
+            'status|状态' => 'require|number|in:1,4'
+        ]);
+
+        $cgdinfo = Db::name("purchease_order")
+            ->where(['cgdNo' => $param['cgdNo'], 'is_del' => 0])
+            ->findOrEmpty();
+        if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据");
+        if ($cgdinfo['status'] == 0) return json_show(1005, "采购单未确认");
+        $isreturn = cgd_sale_return($param['cgdNo']);
+        if ($isreturn) return error_show(1005, "关联销售单存在退货单正在进行");
+
+        $wsminfo = Db::name("warehouse_info")
+            ->where(["wsm_code" => $cgdinfo['wsm_code'], "is_del" => 0])
+            ->findOrEmpty();
+        if (empty($wsminfo)) return error_show(1004, "未找到仓库信息");
+
+        $recep = Db::name('warehouse_addr')
+            ->field('wsm_name')
+            ->where(["wsm_code" => $cgdinfo['wsm_code'], "is_del" => 0])
+            ->findOrEmpty();
+
+        $wsmin_num = Db::name("purchease_in")
+            ->where([["cgdNo", "=", $param['cgdNo']], ["is_del", "=", 0], ["status", "not in", [4, 6]]])
+            ->sum("send_num");
+        if (array_sum(array_column($param['list'], 'send_num')) > $cgdinfo['wsend_num'] - $wsmin_num) return error_show(1009, "采购单剩余未发货数量不足");
+
+        if ($this->level == 2) {
+            //如果开通了供应商账号,则只允许level3账号操作,level2账号不允许操作
+            $all_has_supplier_account = checkHasAccountBySupplierNos([$cgdinfo['supplierNo']]);
+            if (isset($all_has_supplier_account[$cgdinfo['supplierNo']])) return error_show(1009, '已开通供应商账号,当前账号不能操作');
+        }
+
+        $userCommon = \app\admin\common\User::getIns();
+        $supplier_temp = $userCommon->handle('sInfo', ['code' => $cgdinfo['supplierNo']]);
+        $person = $supplier_temp['data'];
+
+        if ($this->level == 2 && $this->uid != $person['personid']) throw new Exception('只能由供应商负责人操作');
+
+        $is_stock = Db::name('good_basic')
+            ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
+            ->value('is_stock');
+
+        Db::startTrans();
+        try {
+
+            $date = date('Y-m-d H:i:s');
+
+            $i = 0;
+            foreach ($param['list'] as $item) {
+
+                if ($val_child->check($item)) throw new Exception($val_child->getError());
+
+                $wsm_in_code = substr(makeNo("CF"), 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
+                $bn_code = substr(makeNo("BN"), 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
+
+                if ($item['sendtype'] == 2 || $cgdinfo['order_type'] != 1) $item['status'] = 4;
+
+                $orin = [
+                    "wsm_in_code" => $wsm_in_code,
+                    "cgdNo" => $param['cgdNo'],
+                    "bnCode" => $bn_code,
+                    "wsm_code" => $cgdinfo['wsm_code'],
+                    "wsm_reaper" => $recep['wsm_name'] ?? '',
+                    "send_num" => $item['send_num'],
+                    "post_company" => $item['post_company'],
+                    "post_code" => $item['post_code'],
+                    "post_fee" => $item['post_fee'],
+                    "sendtime" => $item['post_time'],
+                    "apply_id" => $this->uid,
+                    "apply_name" => $this->uname,
+                    "wait_num" => 0,
+                    "wsm_num" => 0,
+                    "sendtype" => $item['sendtype'],
+                    "status" => $item['status'],
+                    "addtime" => $date,
+                    "updatetime" => $date
+                ];
+
+                $win = Db::name("purchease_in")->insertGetId($orin);
+                if ($win) {
+                    $sto = ["order_code" => $wsm_in_code, "status" => $item['status'], "action_remark" => '', "action_type" => "edit"];
+                    ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $sto, "RKD", $item['status'], $param);
+
+//                	  $uid = Db::name('user_role')
+//                                ->where([
+//                                    ['is_del', '=', 0],
+//                                    ['roleid', 'in', [31, 41]],
+//                                    ['status', '=', 1]
+//                                ])->column('uid');
+//                	  $handle_user_list = implode(',', $uid);
+
+                    $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $item['status'], "order_type" => 'RKD', "before_status" => $item['status'], 'holder_id' => $orin['apply_id'], "handle_user_list" => $handle_user_list ?? ""];
+                    ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
+                    $order = ["order_code" => $param['cgdNo'], "status" => $cgdinfo['status'], "action_remark" => '', "action_type" => "edit"];
+
+                    //维护台账信息
+                    Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$param['cgdNo']}'");
+
+                    if ($item['status'] == 1) continue;
+                    elseif ($item['status'] == 4) {
+                        $cgdinfo['wsend_num'] -= $item['send_num'];
+                        $cgdinfo['send_num'] += $item['send_num'];
+                        $cgdinfo['send_status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
+                        $cgdinfo['status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
+                        $cgdinfo['updatetime'] = $date;
+                        $up = Db::name("purchease_order")->save($cgdinfo);
+                        if ($up == false) throw new Exception('更新采购单失败');
+                        $good = Db::name("good_stock")
+                            ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
+                            ->lock(true)->findOrEmpty();
+                        if (empty($good)) {
+                            $good = [
+                                "spuCode" => $cgdinfo['spuCode'],
+                                "wsm_code" => $cgdinfo['wsm_code'],
+                                "usable_stock" => 0,
+                                "wait_out_stock" => 0,
+                                "wait_in_stock" => $item['send_num'],
+                                "total_stock" => 0,
+                                "addtime" => $date,
+                                "updatetime" => $date,
+                            ];
+                        }
+                        $good['wait_in_stock'] -= $item['send_num'];
+                        $good['usable_stock'] += $item['send_num'];
+                        $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
+                        $good['updatetime'] = $date;
+                        $upd = Db::name("good_stock")->save($good);
+                        $stockid = $good['id'] ?? Db::name("good_stock")->getLastInsID();
+                        //商品变动日志表,good_log_code字段存储采购单单号
+                        $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 1, 'stock' => $item['send_num'], "stock_name" => "usable_stock"];
+                        $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 2, 'stock' => $item['send_num'], "stock_name" => "wait_in_stock"];
+                        if ($upd) {
+                            GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'RKD');
+
+                            $bnin = GoodStockInfo::AddBn($stockid, $bn_code, $item['send_num'], $cgdinfo['good_price']);
+                            if ($bnin == false) throw new Exception('库存bn数据新增失败');
+
+                            //todo:: 新增bn编号库存数据修改
+                            if ($cgdinfo['order_type'] == 1) {
+//                        	$bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo['good_price']);
+//	                        if($bnin==false){
+//	                            Db::rollback();
+//	                            return error_show(1004,"库存bn数据新增失败");
+//	                        }
+                                $bk = Db::name("order_bk")->where(['cgdNo' => $cgdinfo['cgdNo']])->find();
+                                if ($bk == false) {
+                                    $bk = [
+                                        "cgdNo" => $cgdinfo['cgdNo'],
+                                        "spuCode" => $cgdinfo['spuCode'],
+                                        "companyNo" => $cgdinfo['companyNo'],
+                                        "total_num" => $item['send_num'],
+                                        "merge_num" => 0,
+                                        "balance_num" => $item['send_num'],
+                                        "status" => 1,
+                                        "is_del" => 0,
+                                        'addtime' => $date,
+                                        "updatetime" => $date
+                                    ];
+                                    $upcgd = Db::name("order_bk")->save($bk);
+                                } else {
+                                    $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
+                                    $bk['balance_num'] += $item['send_num'];
+                                    $bk['total_num'] += $item['send_num'];
+                                    $bk['updatetime'] = $date;
+                                    $upcgd = Db::name("order_bk")->where($where)->update($bk);
+                                }
+
+                                if ($upcgd == false) throw new Exception('采购单数量更新失败');
+
+                                //商品表里的'可用总库存数'增加
+                                Db::name('good')
+                                    ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
+                                    ->inc('usable_stock', $item['send_num'])
+                                    ->update(['updatetime' => $date]);
+
+                            }
+                            if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
+                                $send = Db::name("order_send")->where(["cgdNo" => $param['cgdNo'], "status" => 1])->column("outCode");
+                                if (!empty($send)) {
+                                    $out = Db::name("order_out")
+                                        ->where(["outCode" => $send, "status" => 0, "is_del" => 0])
+                                        ->select()
+                                        ->toArray();
+                                    if (!empty($out)) {
+                                        $up = Db::name("order_out")
+                                            ->where(["outCode" => $send, "status" => 0, "is_del" => 0])
+                                            ->update(["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => $date]);
+                                        if ($up == false) throw new Exception('出库单出库失败');
+                                        else {
+
+                                            //尝试拆单
+                                            \app\admin\model\OrderOutChild::makeChild($send);
+
+                                            //查询是否有非库存品订单
+//                                        $is_stock = Db::name('sale')
+//                                            ->where([
+//                                                ['is_del','=',0],
+//                                                ['orderCode','in',array_column($out,'orderCode')]
+//                                            ])->column('is_stock','orderCode');
+
+//                                        $supplier = Db::name('supplier')
+//                                            ->field('person,personid')
+//                                            ->where(['code'=>$cgdinfo['supplierNo'],'is_del'=>0])
+//                                            ->findOrEmpty();
+
+                                            foreach ($out as $k => $v_outCode) {
+                                                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+                                                    "order_code" => $v_outCode['outCode'],//出库单号
+                                                    "status" => 0,//这里的status是之前的值
+                                                    "action_remark" => '',//备注
+                                                    "action_type" => "status"//新建create,编辑edit,更改状态status
+                                                ], "CKD", 1, $this->post);
+
+                                                if ($is_stock == 1) {
+                                                    //库存品,推给库管和库管-张凯旋
+                                                    $roleid = config('app.wsm_cgder_role');
+                                                    $uids = Db::name('user_role')
+                                                        ->where('is_del', 0)
+                                                        ->whereIn('roleid', $roleid)
+                                                        ->column('uid');
+                                                    ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+                                                        "order_type" => 'CKD',
+                                                        "order_code" => $v_outCode['outCode'],//出库单号
+                                                        "order_id" => $v_outCode['id'],
+                                                        "order_status" => 1,
+                                                        "before_status" => 0,
+//                                                    'wait_id' => $supplier['personid'],
+//                                                    'wait_name' => $supplier['person'],
+//                                                    'holder_id' => $supplier['personid'],
+                                                        'handle_user_list' => implode(',', $uids)
+                                                    ]);
+                                                } else {
+                                                    //非库存品和采返商品,推给供应商负责人
+                                                    ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+                                                        "order_type" => 'CKD',
+                                                        "order_code" => $v_outCode['outCode'],//出库单号
+                                                        "order_id" => $v_outCode['id'],
+                                                        "order_status" => 1,
+                                                        "before_status" => 0,
+                                                        'wait_id' => $person['personid'] ?? 0,
+                                                        'wait_name' => $person['person'] ?? '',
+                                                        'holder_id' => $person['personid'] ?? 0,
+                                                    ]);
+                                                }
+
+                                            }
+                                        }
+                                        $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
+                                        if ($good1 == false) throw new Exception('未找到对应商品库存');
+                                        $out_num = intval(array_sum(array_column($out, "send_num")));
+                                        if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
+                                        $good1['usable_stock'] -= $out_num;
+                                        $good1['wait_out_stock'] += $out_num;
+                                        $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
+                                        $good1['updatetime'] = $date;
+                                        $upd = Db::name("good_stock")->save($good1);
+                                        if ($upd == false) throw new Exception('库存数量更新失败');
+                                        $stockid = isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID();
+
+                                        $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 1, 'stock' => $item['send_num'], "stock_name" => "wait_out_stock"];
+                                        $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 1, 'stock' => $item['send_num'], "stock_name" => "usable_stock"];
+                                        GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'RKD');
+                                        $sendp = Db::name("order_send")
+                                            ->where(["cgdNo" => $param['cgdNo'], "outCode" => $send, "status" => 1])
+                                            ->save(["status" => 2]);
+                                        if ($sendp == false) throw new Exception("库存发货失败");
+
+                                    }
+                                }
+
+                                //商品表里的'可用总库存数'增加
+                                Db::name('good_zixun')
+                                    ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
+                                    ->inc('usable_stock', $item['send_num'])
+                                    ->update(['updatetime' => $date]);
+                            }
+
+                            ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $order, "CGD", $cgdinfo['status'], $cgdinfo);
+
+//                        $personid = Db::name('supplier')
+//                            ->where('is_del',0)
+//                            ->where('code',$cgdinfo['supplierNo'])
+//                            ->value('personid');
+
+                            if ($cgdinfo['status'] == 1 || $cgdinfo['status'] == 2) {
+
+//                            $is_stock = Db::name('good_basic')
+//                                ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
+//                                ->value('is_stock');
+//                            if($is_stock==1){
+//                                //库存品,推给31库管人员、41库管-张凯旋
+//                                $uid = Db::name('user_role')
+//                                    ->where([
+//                                        ['is_del', '=', 0],
+//                                        ['roleid', 'in', [31, 41]],
+//                                        ['status', '=', 1]
+//                                    ])->column('uid');
+//                                if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
+////                                $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid,'handle_user_list'=>implode(',', $uid)];
+//
+//                            }else{
+//                            }
+                            }//else $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid];
+
+                            ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], ["order_code" => $param['cgdNo'], "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $person['personid'] ?? 0]);
+//                            Db::commit();
+//                            return app_show(0,'商品入库成功',["wsm_in_code"=>$wsm_in_code]);
+                        } else throw new Exception('商品入库失败');
+                    }
+//                    Db::commit();
+//                    return app_show(0,'商品入库成功',["wsm_in_code"=>$wsm_in_code]);
+                }
+
+
+            }
+
+            Db::commit();
+            return json_show(0, "商品入库成功");
+        } catch (Exception $e) {
+            Db::rollback();
+            return json_show(1004, $e->getMessage());
+        }
+    }
+
     public function info(){
         $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
         if($incode==""){

+ 18 - 1
app/admin/controller/Sale.php

@@ -5084,7 +5084,7 @@ class Sale extends Base
 
         $list = Db::name('sale')
             ->alias('a')
-            ->field('a.id sale_id,a.order_type,a.status sale_status,a.orderCode,a.cat_id,c.id cgd_id,c.status cgd_status')
+            ->field('a.id sale_id,a.order_type,a.status sale_status,a.orderCode,a.cat_id,a.good_createrid,c.id cgd_id,c.status cgd_status')
             ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
             ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
             ->where(['a.is_del' => 0, 'a.orderCode' => $orderCode])
@@ -5115,6 +5115,23 @@ class Sale extends Base
                 } else {
                     if (in_array($sale['cgd_status'], [0, 1]) == false) throw new \think\Exception($sale['orderCode'] . '采购单已入库,无法取消');
                 }
+
+                //修改状态,添加待办
+                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+                    "order_code" => $sale['orderCode'],//销售单号
+                    "status" => $sale['sale_status'],//这里的status是之前的值
+                    "action_remark" => '',//备注
+                    "action_type" => "edit"//新建create,编辑edit,更改状态status
+                ], "XSQRD", 3, ['orderCode' => $orderCode]);
+
+                ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+                    "order_type" => 'XSQRD',
+                    "order_code" => $sale['orderCode'],
+                    "order_id" => $sale['sale_id'],
+                    "order_status" => 3,
+                    "before_status" => $sale['sale_status'],
+                    'holder_id' => $sale['good_createrid'],
+                ]);
             }
             Db::name('sale')
                 ->where(['is_del' => 0, 'id' => array_column($list, 'sale_id')])

+ 1 - 0
app/admin/route/app.php

@@ -240,6 +240,7 @@ Route::rule("cgdgetchangesupplierlist", "admin/Purch/getChangeSupplierList");//
 
 Route::rule("orderinlist", "admin/Purchin/list");
 Route::rule("orderinadd", "admin/Purchin/add");//新建入库单
+Route::rule("orderin_add_batch", "admin/Purchin/addBatch");//批量添加入库单
 Route::rule("orderininfo", "admin/Purchin/info");
 Route::rule("orderincheck", "admin/Purchin/check");
 Route::rule("orderinstatus", "admin/Purchin/status");