Browse Source

采购单批量入库,保持同之前供应商端接口一致。其他细节优化

wufeng 2 years ago
parent
commit
13d7857d92
2 changed files with 406 additions and 288 deletions
  1. 1 1
      app/admin/controller/After.php
  2. 405 287
      app/admin/controller/Purchin.php

+ 1 - 1
app/admin/controller/After.php

@@ -473,7 +473,7 @@ class After extends Base
 
             $val = Validate::rule([
                 'returnCode' => 'require',
-                'status' => 'require|number|in:1,2,4,5,6,9,10',
+                'status' => 'require|number|in:1,2,4,5,6,9,10,11',
                 'remark' => 'requireIf:status,6|requireIf:status,2|requireIf:status,10',
 //                'contactor|收货人' => 'requireIf:status,4',
 //                'mobile|联系电话' => 'requireIf:status,4',

+ 405 - 287
app/admin/controller/Purchin.php

@@ -625,338 +625,456 @@ class Purchin extends Base
     public function addBatch()
     {
 
-        $param = $this->request->only(['cgdNo', 'list'], 'post', 'trim');
+        $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
 
-        $val = Validate::rule([
-            'cgdNo|采购单编号' => 'require',
-            'list' => 'require|array|max:100',
-        ]);
+        $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
 
-        if ($val->check($param) == false) return json_show(1004, $val->getError());
+        //入库方式 供应商包邮,
+        //运费 0
 
-        $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'
-        ]);
+        if (!$val->check($param)) return json_show(1004, $val->getError());
 
         $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, "未找到仓库信息");
+            ->whereIn('cgdNo', $param['cgdNo'])
+            ->where(['is_del' => 0, 'supplierNo' => $this->request->user['supplierNo']])
+            ->where('status', 'in', [1, 2])//1待入库,2部分入库
+            ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status,supplierNo', 'cgdNo');
 
-        $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 (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
 
-        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, '已开通供应商账号,当前账号不能操作');
-        }
+        $all_wsm_code = array_column($cgdinfo, 'wsm_code');
 
-        $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('只能由供应商负责人操作');
+        $recep = Db::name('warehouse_addr')
+            ->where('is_del', 0)
+            ->whereIn('wsm_code', $all_wsm_code)
+            ->column('wsm_name', 'wsm_code');
 
-        $is_stock = Db::name('good_basic')
-            ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
-            ->value('is_stock');
+        $uid = $this->request->user['uid'];
+        $nickname = $this->request->user['nickname'];
 
         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;
+//            $supplier_cgderid = Db::name('supplier')
+//                ->where('is_del',0)
+//                ->whereIn('code',array_column($cgdinfo,'supplierNo'))
+//                ->column('personid,person','code');
+            $codes = array_unique(array_column($cgdinfo, 'supplierNo'));
+            $temp = \app\admin\common\User::getIns()->handle('sGetList', ['more_code' => $codes, 'size' => count($codes)]);
+
+            $supplier_cgderid = [];
+            foreach ($temp['data']['list'] as $da) {
+                if (!isset($supplier_cgderid[$da['code']])) $supplier_cgderid[$da['code']] = [
+                    'personid' => $da['personid'],
+                    'person' => $da['person'],
+                ];
+            }
 
-                $orin = [
+            $i = 0;
+            $status = 4;//4入库完成
+            foreach ($param['cgdNo'] as $cgdNo) {
+                $isreturn = cgd_sale_return($cgdNo);
+                if ($isreturn) {
+                    throw new Exception("{$cgdNo}关联销售单存在退货单正在进行");
+                }
+                if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
+
+                $wsm_in_code = makeNo("CF");
+                $bn_code = makeNo("BN");
+                $send_num = $cgdinfo[$cgdNo]['good_num'];
+                //改变编码规则,将原编码后两位换成序列号
+                //str_pad字符串填充
+                $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
+                $i++;
+
+                //入库单
+                $win = Db::name("purchease_in")->insertGetId([
                     "wsm_in_code" => $wsm_in_code,
-                    "cgdNo" => $param['cgdNo'],
+                    "cgdNo" => $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,
+                    "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
+                    "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
+                    "send_num" => $cgdinfo[$cgdNo]['good_num'],
+                    "post_company" => '',
+                    "post_code" => '',
+                    "post_fee" => 0,//运费固定值:0
+                    "sendtime" => $date,
+                    "apply_id" => $this->request->user['uid'],
+                    "apply_name" => $this->request->user['nickname'],
                     "wait_num" => 0,
                     "wsm_num" => 0,
-                    "sendtype" => $item['sendtype'],
-                    "status" => $item['status'],
+                    "sendtype" => 2,//发货方式,固定值,供应商包邮2
+                    "status" => $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);
+                    $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
+                    ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
+                    $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status, 'holder_id' => $this->request->user['uid']];
+                    ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
+                    $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
 
-//                	  $uid = Db::name('user_role')
-//                                ->where([
-//                                    ['is_del', '=', 0],
-//                                    ['roleid', 'in', [31, 41]],
-//                                    ['status', '=', 1]
-//                                ])->column('uid');
-//                	  $handle_user_list = implode(',', $uid);
+                    //维护台账信息
+                    Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
+
+                    //////////////////
+                    $good = Db::name("good_stock")
+                        ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
+                        ->lock(true)
+                        ->find();
+                    if (empty($good)) {
+                        $good = [
+                            "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
+                            "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
+                            "usable_stock" => 0,
+                            "wait_out_stock" => 0,
+                            "wait_in_stock" => $send_num,
+                            "total_stock" => 0,
+                            "addtime" => date("Y-m-d H:i:s"),
+                            "updatetime" => date("Y-m-d H:i:s"),
+                        ];
+//                        $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
+                    } else {
+//                        $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
+                    }
+                    $good['wait_in_stock'] -= $send_num;
+//                    if ($good['presale_stock'] > 0) {
+//                        if ($good['presale_stock'] >= $send_num) {
+//                            $good['presale_stock'] -= $send_num;
+//                            $good['wait_out_stock'] += $send_num;
+//                        } else {
+//                            $good['presale_stock'] = 0;
+//                            $good['usable_stock'] += $send_num - $good['presale_stock'];
+//                            $good['wait_out_stock'] += $good['presale_stock'];
+//                        }
+//                    } else {
+                    $good['usable_stock'] += $send_num;
+//                    }
 
-                    $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"];
+                    $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
+                    $good['updatetime'] = date("Y-m-d H:i:s");
+                    $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' => $send_num, "stock_name" => "usable_stock"];
+                    $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
+                    if ($upd) {
+                        GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
+                        if ($cgdinfo[$cgdNo]['order_type'] == 1) {
+                            $bnin = GoodStockInfo::AddBn($stockid, $bn_code, $send_num, $cgdinfo[$cgdNo]['good_price']);
+                            if ($bnin == false) {
+                                Db::rollback();
+                                return error_show(1004, "库存bn数据新增失败");
+                            }
+                            $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
+                            if ($bk == false) {
+                                $bk = [
+                                    "cgdNo" => $cgdNo,
+                                    "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
+                                    "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
+                                    "total_num" => $send_num,
+                                    "merge_num" => 0,
+                                    "balance_num" => $send_num,
+                                    "status" => 1,
+                                    "is_del" => 0,
+                                    'addtime' => date("Y-m-d H:i:s"),
+                                    "updatetime" => date("Y-m-d H:i:s")
+                                ];
+                                $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'] += $send_num;
+                                $bk['total_num'] += $send_num;
+                                $bk['updatetime'] = date("Y-m-d H:i:s");
+                                $upcgd = Db::name("order_bk")->where($where)->update($bk);
+                            }
 
-                    //维护台账信息
-                    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,
-                            ];
+                            if ($upcgd == false) throw new Exception('采购单数量更新失败');
                         }
-                        $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('采购单数量更新失败');
+                        if (in_array($cgdinfo[$cgdNo]['send_status'], [1, 2]) && $cgdinfo[$cgdNo]['order_type'] != 1) {
+                            $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
+                            if (!empty($send)) {
+                                $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()->toArray();
+                                if (!empty($out)) {
+                                    $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update(["status" => 1, 'wsm_code' => $cgdinfo[$cgdNo]['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
+                                    if ($up == false) throw new Exception('出库单出库失败');
+                                    else {
 
-                                //商品表里的'可用总库存数'增加
-                                Db::name('good')
-                                    ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
-                                    ->inc('usable_stock', $item['send_num'])
-                                    ->update(['updatetime' => $date]);
+                                        //查询是否有非库存品订单
+                                        $is_stock = Db::name('sale')
+                                            ->where([
+                                                ['is_del', '=', 0],
+                                                ['orderCode', 'in', array_column($out, 'orderCode')]
+                                            ])->column('is_stock', 'orderCode');
 
-                            }
-                            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');
+                                        foreach ($out as $k => $v_outCode) {
+                                            //修改状态,添加待办
+                                            ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
+                                                "order_code" => $v_outCode['outCode'],//出库单号
+                                                "status" => 0,//这里的status是之前的值
+                                                "action_remark" => '',//备注
+                                                "action_type" => "status"//新建create,编辑edit,更改状态status
+                                            ], "CKD", 1, $param);
 
-//                                        $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], [
+                                            //库存品,推给库管和库管-张凯旋
+                                            if ($is_stock[$v_outCode['orderCode']] == 1) {
+                                                $roleid = config('app.wsm_cgder_role');
+                                                $uids = Db::name('user_role')
+                                                    ->where('is_del', 0)
+                                                    ->whereIn('roleid', $roleid)
+                                                    ->column('uid');
+                                                ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
+                                                    "order_type" => 'CKD',
                                                     "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,
-                                                    ]);
-                                                }
-
+                                                    "order_id" => $v_outCode['id'],
+                                                    "order_status" => 1,
+                                                    "before_status" => 0,
+//                                                    'wait_id' => $wait['personid'],
+//                                                    'wait_name' => $wait['person'],
+//                                                    'holder_id' => $wait['personid'],
+                                                    'handle_user_list' => implode(',', $uids)
+                                                ]);
+                                            } else {
+                                                //推给供应商负责人
+                                                $wait = $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']];
+                                                ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
+                                                    "order_type" => 'CKD',
+                                                    "order_code" => $v_outCode['outCode'],//出库单号
+                                                    "order_id" => $v_outCode['id'],
+                                                    "order_status" => 1,
+                                                    "before_status" => 0,
+                                                    'wait_id' => $wait['personid'],
+                                                    'wait_name' => $wait['person'],
+                                                    'holder_id' => $wait['personid'],
+                                                ]);
                                             }
-                                        }
-                                        $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("库存发货失败");
 
+                                        }
                                     }
-                                }
+                                    $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['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("Y-m-d H:i:s");
+                                    $upd = Db::name("good_stock")->save($good1);
+                                    if ($upd == false) throw new Exception('库存数量更新失败');
 
-                                //商品表里的'可用总库存数'增加
-                                Db::name('good_zixun')
-                                    ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
-                                    ->inc('usable_stock', $item['send_num'])
-                                    ->update(['updatetime' => $date]);
+                                    $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
+                                    $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
+                                    GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
+                                    $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
+                                    if ($sendp == false) throw new Exception('库存发货失败');
+
+                                }
                             }
+                        }
+                        ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $order, "CGD", $cgdinfo[$cgdNo]['status'], $cgdinfo[$cgdNo]);
+                        $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo[$cgdNo]['id'], "order_status" => $cgdinfo[$cgdNo]['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => isset($supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']]['personid']) ? $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']]['personid'] : 0];
+                        ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
+//                        Db::commit();
+//                        return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
+                    } else throw new Exception('商品入库失败');
+                    //////////////////
 
-                            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) {
+                } else throw new Exception('新建入库单失败');
+            }
 
-//                            $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];
+            //维护原有采购单信息
+            Db::name("purchease_order")
+                ->whereIn('id', array_column($cgdinfo, 'id'))
+                ->update([
+                    'wsend_num' => 0,
+                    'send_num' => Db::raw('good_num'),
+                    'send_status' => 3,
+                    'status' => 3,
+                    'updatetime' => date("Y-m-d H:i:s"),
+                ]);
 
-                            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, '新建入库单成功');
 
+//            $win = Db::name("purchease_in")->insert($orin, true);
+//            if ($win) {
+//                $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
+//                ActionLog::logAdd($this->post['token'], $sto, "RKD", $status, $this->post);
+//                $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
+//                ProcessOrder::AddProcess($this->post['token'], $process);
+//                $order = ["order_code" => $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('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgdNo}'");
 
-            Db::commit();
-            return json_show(0, "商品入库成功");
+//                if ($status == 1) {
+//                    Db::commit();
+//                    return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
+//                }
+//                if ($status == 4) {
+//                    $cgdinfo['wsend_num'] -= $send_num;
+//                    $cgdinfo['send_num'] += $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("Y-m-d H:i:s");
+//                    $up = Db::name("purchease_order")->save($cgdinfo);
+//                    if ($up == false) {
+//                        Db::rollback();
+//                        return json_show(1004, "新建失败");
+//                    }
+//            $good = Db::name("good_stock")
+//                ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
+//                ->lock(true)
+//                ->find();
+//            if (empty($good)) {
+//                $good = [
+//                    "spuCode" => $cgdinfo['spuCode'],
+//                    "wsm_code" => $cgdinfo['wsm_code'],
+//                    "usable_stock" => 0,
+//                    "wait_out_stock" => 0,
+//                    "wait_in_stock" => $send_num,
+//                    "total_stock" => 0,
+//                    "addtime" => date("Y-m-d H:i:s"),
+//                    "updatetime" => date("Y-m-d H:i:s"),
+//                ];
+//                //   $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
+//                // "action_type"=>"create"];
+//            } else {
+//                //   $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
+//                // "action_type"=>"edit"];
+//            }
+//            $good['wait_in_stock'] -= $send_num;
+//            if ($good['presale_stock'] > 0) {
+//                if ($good['presale_stock'] >= $send_num) {
+//                    $good['presale_stock'] -= $send_num;
+//                    $good['wait_out_stock'] += $send_num;
+//                } else {
+//                    $good['presale_stock'] = 0;
+//                    $good['usable_stock'] += $send_num - $good['presale_stock'];
+//                    $good['wait_out_stock'] += $good['presale_stock'];
+//                }
+//            } else {
+//                $good['usable_stock'] += $send_num;
+//            }
+//
+//            $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
+//            $good['updatetime'] = date("Y-m-d H:i:s");
+//            $upd = Db::name("good_stock")->save($good);
+//            //商品变动日志表,good_log_code字段存储采购单单号
+//            $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
+//            $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
+//            if ($upd) {
+//                GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
+//                if ($cgdinfo['order_type'] == 1) {
+//                    $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" => $send_num,
+//                            "merge_num" => 0,
+//                            "balance_num" => $send_num,
+//                            "status" => 1,
+//                            "is_del" => 0,
+//                            'addtime' => date("Y-m-d H:i:s"),
+//                            "updatetime" => date("Y-m-d H:i:s")
+//                        ];
+//                        $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'] += $send_num;
+//                        $bk['total_num'] += $send_num;
+//                        $bk['updatetime'] = date("Y-m-d H:i:s");
+//                        $upcgd = Db::name("order_bk")->where($where)->update($bk);
+//                    }
+//
+//                    if ($upcgd == false) {
+//                        Db::rollback();
+//                        return json_show(1004, "采购单数量更新失败");
+//                    }
+//                }
+//                if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
+//                    $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
+//                    if (!empty($send)) {
+//                        $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()
+//                            ->toArray();
+//                        if (!empty($out)) {
+//                            $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update
+//                            (["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
+//                            if ($up == false) {
+//                                Db::rollback();
+//                                return json_show(1004, "出库单出库失败");
+//                            } else {
+//                                foreach ($out as $k => $v_outCode) {
+//                                    //修改状态,添加待办
+//                                    ActionLog::logAdd($this->post['token'], [
+//                                        "order_code" => $v_outCode['outCode'],//出库单号
+//                                        "status" => 0,//这里的status是之前的值
+//                                        "action_remark" => '',//备注
+//                                        "action_type" => "status"//新建create,编辑edit,更改状态status
+//                                    ], "CKD", 1, $this->post);
+//
+//                                    ProcessOrder::AddProcess($this->post['token'], [
+//                                        "order_type" => 'CKD',
+//                                        "order_code" => $v_outCode['outCode'],//出库单号
+//                                        "order_id" => $v_outCode['id'],
+//                                        "order_status" => 1, "before_status" => 0
+//                                    ]);
+//                                }
+//                            }
+//                            $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
+//                            if ($good1 == false) {
+//                                Db::rollback();
+//                                return json_show(1004, "未找到对应商品库存");
+//                            }
+//                            $out_num = intval(array_sum(array_column($out, "send_num")));
+//                            if ($out_num > $good1['usable_stock']) {
+//                                Db::rollback();
+//                                return json_show(1004, "库存数量不足发货");
+//                            }
+//                            $good1['usable_stock'] -= $out_num;
+//                            $good1['wait_out_stock'] += $out_num;
+//                            $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
+//                            $good1['updatetime'] = date("Y-m-d H:i:s");
+//                            $upd = Db::name("good_stock")->save($good1);
+//                            if ($upd == false) {
+//                                Db::rollback();
+//                                return json_show(1004, "库存数量更新失败");
+//                            }
+//                            $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
+//                            $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
+//                            GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
+//                            $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
+//                            if ($sendp == false) {
+//                                Db::rollback();
+//                                return json_show(1004, "库存发货失败");
+//                            }
+//                        }
+//                    }
+//                }
+//                ActionLog::logAdd($this->post['token'], $order, "CGD", $cgdinfo['status'], $cgdinfo);
+//                $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status']];
+//                ProcessOrder::AddProcess($this->post['token'], $process);
+//                Db::commit();
+//                return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
+//            } else {
+//                Db::rollback();
+//                return json_show(1002, "商品入库失败");
+//            }
+//                }
+//                Db::commit();
+//                return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
+//            }
+//            Db::rollback();
+//            return json_show(1004, "新建失败");
         } catch (Exception $e) {
             Db::rollback();
             return json_show(1004, $e->getMessage());