wugg 2 years ago
parent
commit
dc048497d1
1 changed files with 165 additions and 79 deletions
  1. 165 79
      app/admin/controller/Sale.php

+ 165 - 79
app/admin/controller/Sale.php

@@ -21,9 +21,6 @@ use think\facade\Validate;
 class Sale extends Base
 {
 
-
-
-    
     public $noble = [];
 
     public function __construct(App $app)
@@ -372,6 +369,7 @@ class Sale extends Base
                                 "error_num" => 0,
                                 "wsm_code" => $is_stock == 1 ?'':$cgdinfo['wsm_code'],
                                 "order_type" => $is_stock == 1 ? 1 : 2,
+                                "send_status" => $outstatus,
                                 "status" => $outstatus,
                                 "addtime" => date("Y-m-d H:i:s"),
                                 "updatetime" => date("Y-m-d H:i:s")
@@ -719,12 +717,12 @@ class Sale extends Base
                             $status = 0;
                         } else {
                             if (($goodStock_num - $num) < 0) throw new Exception("非库存品可用库存不足");
-                            $goodStock_num = -$num;
+                            $goodStock_num -= $num;
                             $status = 1;
                         }
                     } else {
                         if ($goodStock_num == 0 || ($goodStock_num - $num) < 0) throw new Exception("库存品可用库存不足");
-                        $goodStock_num = -$num;
+                        $goodStock_num -= $num;
                         $status = 1;
                     }
 
@@ -2137,8 +2135,8 @@ class Sale extends Base
         $all_createrid = array_column($list,'apply_id');
         $item = get_company_name_by_uid($all_createrid);
 
-        $userCommon = \app\admin\common\User::getIns();
-        $names = $userCommon->handle('getCodeAndName',['code'=>array_merge(array_column($list,'customer_code'),array_column($list,'supplierNo'))]);
+//        $userCommon = \app\admin\common\User::getIns();
+//        $names = $userCommon->handle('getCodeAndName',['code'=>array_merge(array_column($list,'customer_code'),array_column($list,'supplierNo'))]);
 
         $data = [];
         foreach ($list as $value) {
@@ -3442,7 +3440,7 @@ class Sale extends Base
         }
         $supplier_name = isset($this->post['supplier_name']) && $this->post['supplier_name'] !== "" ? trim($this->post['supplier_name']) : "";
         if ($supplier_name !== "") {
-            $where[] = ['wpo.supplier_name', "like", "%$supplier_name%"];
+            $where[] = ['b.supName', "like", "%$supplier_name%"];
         }
         $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
         if ($status !== "") {
@@ -3492,37 +3490,49 @@ class Sale extends Base
 
         //只有level2的账号过滤数据权限
         if ($this->level == 2) {
-            $hand = resign_hand_user($this->uid, 0);
-            $uidarr = implode(",", $hand);
-            //库管只能看到库存品订单,供应商负责人只能看到非库存品订单
-            if (!in_array($this->roleid, [1, 33])) {
-                //库管看到所有的库存品发货申请单
-                if (in_array($this->roleid, config('app.wsm_cgder_role'))) $condition .= " b.is_stock=1";
-                else {
-                    $role = $this->checkDataShare();
-                    if (!empty($role[DataGroupModel::$type_全部])) {
-                        $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
-                        if ($condition != '') $condition .= " or ";
-                        $condition .= " a.apply_id in (" . implode(',', $arr) . ")";
-                    }
-                }
-//                $personid = Db::name('supplier')->field('id')->where(['is_del' => 0, 'personid' => $hand])->findOrEmpty();
-                if (!empty($hand)) {
-                    if ($condition != '') $condition .= " or ";
-//                    $condition .= "(b.is_stock=0 and sip.personid in ($uidarr))";
-
-                    $person_supplier = Db::connect('mysql_sys')
-                        ->name('supplier')
-                        ->where(['is_del'=>0,'personid'=>$uidarr])
-                        ->column('code');
 
-                    $condition .= "(b.is_stock=0 and wpo.supplierNo in ('" . implode('\',\'', $person_supplier) . "'))";
-                }
+            //如果是供应商负责人,根据供应商筛选
+            $person_supplier = Db::connect('mysql_sys')
+                ->name('supplier')
+                ->where(['is_del' => 0, 'personid' =>$this->uid])
+                ->column('code');
+
+            if(!empty($person_supplier)) $condition = "b.supNo in ('" . implode('\',\'', $person_supplier) . "')";
+            else{
+                //不是供应商负责人,根据数据权限筛选申请人
+                $role = $this->checkDataShare();
+                if (!empty($role[DataGroupModel::$type_全部])) $condition = " a.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . ")";
             }
-        }
 
-        //供应商账号只能看到非库存品的订单
-        if ($this->level == 3) $where[] = ['b.is_stock', '=', 0];
+
+//            $hand = resign_hand_user($this->uid, 0);
+//            $uidarr = implode(",", $hand);
+//            //库管只能看到库存品订单,供应商负责人只能看到非库存品订单
+//            if (!in_array($this->roleid, [1, 33])) {
+//                //库管看到所有的库存品发货申请单
+//                if (in_array($this->roleid, config('app.wsm_cgder_role'))) $condition .= " b.is_stock=1";
+//                else {
+//                    $role = $this->checkDataShare();
+//                    if (!empty($role[DataGroupModel::$type_全部])) {
+//                        $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
+//                        if ($condition != '') $condition .= " or ";
+//                        $condition .= " a.apply_id in (" . implode(',', $arr) . ")";
+//                    }
+//                }
+////                $personid = Db::name('supplier')->field('id')->where(['is_del' => 0, 'personid' => $hand])->findOrEmpty();
+//                if (!empty($hand)) {
+//                    if ($condition != '') $condition .= " or ";
+////                    $condition .= "(b.is_stock=0 and sip.personid in ($uidarr))";
+//
+//                    $person_supplier = Db::connect('mysql_sys')
+//                        ->name('supplier')
+//                        ->where(['is_del'=>0,'personid'=>$uidarr])
+//                        ->column('code');
+//
+//                    $condition .= "(b.is_stock=0 and wpo.supplierNo in ('" . implode('\',\'', $person_supplier) . "'))";
+//                }
+//            }
+        }
 
 //        if(!empty($role['platform']) ){
 //            $where[]=["b.platform_id","in",$role['platform']];
@@ -3548,11 +3558,11 @@ class Sale extends Base
 //            ->join("customer_info v", "v.companyNo=b.customer_code", "left")
             ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
             ->leftJoin("order_send os", "os.outCode=a.outCode")
-            ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
+//            ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
 //	        ->leftJoin('supplier sip', 'sip.code=wpo.supplierNo')
             ->where($where)
             ->where($condition)
-            ->count();
+            ->count('a.id');
         $total = ceil($count / $size);
         $page = $page >= $total ? $total : $page;
         $list = Db::name('order_out')
@@ -3561,9 +3571,9 @@ class Sale extends Base
 //            ->join("customer_info v", "v.companyNo=b.customer_code", "left")
             ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
             ->leftJoin("order_send os", "os.outCode=a.outCode")
-            ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
+//            ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
 //	        ->leftJoin('supplier sip', 'sip.code=wpo.supplierNo')
-            ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.customer_code,b.supplierNo companyNo,b.supplierName companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,wpo.supplierNo,n.supplierNo wsm_supplierNo")
+            ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.customer_code,b.supplierNo companyNo,b.supplierName companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,b.supNo supplierNo,n.supplierNo wsm_supplierNo")
             ->where($where)
             ->where($condition)
             ->order("addtime desc")
@@ -3579,6 +3589,8 @@ class Sale extends Base
 //        $userCommon = \app\admin\common\User::getIns();
 //        $names = $userCommon->handle('getCodeAndName',['code'=>array_column($list,'customer_code')]);
 
+        $has_account = checkHasAccountBySupplierNos(array_unique(array_column($list,'wsm_supplierNo')));
+
         $data = [];
         foreach ($list as $value) {
 
@@ -3609,7 +3621,7 @@ class Sale extends Base
             } else {
                 $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find();
             }
-            $value['wsm_has_account'] = 0;//(int)isset(checkHasAccountBySupplierNos([$wsmcode['wsm_supplierNo']])[$wsmcode['wsm_supplierNo']]);
+            $value['wsm_has_account'] = (int)isset($has_account[$value['wsm_supplierNo']]);
             $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'] . $addr['addr'] : "";
             $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
             $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
@@ -3716,7 +3728,7 @@ class Sale extends Base
             return error_show(1002, "参数orderCoder不能为空");
         }
         $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","in",[1,2,3,
-        7,9,10]]])->count();
+        7,9,10,8,11,12]]])->count();
         if($retrun>0){
             return error_show(1005,"存在未完成退货订单数据");
         }
@@ -3861,6 +3873,7 @@ class Sale extends Base
                     "send_num" => $send_num,
                     "check_num" => 0,
                     "error_num" => 0,
+                    "send_status" => 1,
                     "status" => 1,
                     "addtime" => date("Y-m-d H:i:s"),
                     "updatetime" => date("Y-m-d H:i:s")
@@ -3878,21 +3891,22 @@ class Sale extends Base
                         if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 2) {
                             $data['status'] = 0;
                         } else {
-                            $up = Db::name('good_stock')
-                                ->where('id', $temp['id'])
-                                ->update([
-                                    'usable_stock' => $temp['usable_stock'] - $send_num,
-                                    'wait_out_stock' => $temp['wait_out_stock'] + $send_num,
-                                    'updatetime' => date('Y-m-d H:i:s'),
-                                ]);
-                            if ($up == false) {
-                                Db::rollback();
-                                return error_show(1004, '库存更新失败');
-                            }
+                            //这个地方不需要更新,因为后面尝试拆单的时候会维护库存数
+//                            $up = Db::name('good_stock')
+//                                ->where('id', $temp['id'])
+//                                ->update([
+//                                    'usable_stock' => $temp['usable_stock'] - $send_num,
+//                                    'wait_out_stock' => $temp['wait_out_stock'] + $send_num,
+//                                    'updatetime' => date('Y-m-d H:i:s'),
+//                                ]);
+//                            if ($up == false) {
+//                                Db::rollback();
+//                                return error_show(1004, '库存更新失败');
+//                            }
                             // ::todo
-                            $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
-                            $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "usable_stock"];
-                            GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, "CKD");
+//                            $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
+//                            $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "usable_stock"];
+//                            GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, "CKD");
                         }
 
                     }
@@ -3911,6 +3925,9 @@ class Sale extends Base
                     //是否库存品,待办数据推送到的人有区别,
                     $process = [];
                     if ($data['status'] == 1) {
+
+                        if ($data['send_status'] == 1) OrderOutChild::makeChild($outCode);//尝试拆单
+
                         if ($der['is_stock'] == 1) {
                             //库存品,推给库管和库管-张凯旋
                             $roleid = config('app.wsm_cgder_role');
@@ -4386,7 +4403,9 @@ class Sale extends Base
         if ($ct['is_stock'] == 1) {
 //            $good_stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
 //            (["spuCode" => $ct['spuCode'], "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
-            $stock = Db::name("good")->where(["spuCode"=>$ct['spuCode']])->value("usable_stock",0);
+
+            //  $good_stock = Db::name("good_stock")->where(["spuCode" => $ct['spuCode'], "is_del" => 0])->find();
+             $stock = Db::name("good")->where(["spuCode"=>$ct['spuCode']])->value("usable_stock",0);
         }
         if ($is_activity == 1) {
             $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
@@ -4579,12 +4598,12 @@ class Sale extends Base
             ->where('is_del',0)
             ->where('spuCode',$order['spuCode'])
             ->findOrEmpty();
-        
+
         //补充商品创建人字段
         //  2022-9-7号确认 creater创建人即采购单的采购员 咨询cgder 只是供应商负责人
         $order['good_createrid']=$good_info['createrid']??$order['createrid'];
         $order['good_creater']=$good_info['creater']??$order['creater'];
-        
+
         return app_show(0, "获取成功", $order);
     }
 
@@ -4704,6 +4723,8 @@ class Sale extends Base
                 ->where(['id' => $rs['id'], 'status' => 2, "is_del" => 0])
                 ->update(['status' => 3, 'updatetime' => date('Y-m-d H:i:s')]);
 
+            //发货工单不做更新,发货工单只给库管人员看,不关心是否收货,只保留 待发货/已发货/已取消 这几个状态就可以了
+
             //查询该发货单所属的销售单,是否还有未收货的其他发货单
             $other_res_sale = Db::name('order_out')
                 ->where(['orderCode' => $rs['orderCode'], 'status' => 2, "is_del" => 0])
@@ -5106,7 +5127,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,a.good_createrid,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,c.cgdNo,c.cgder_id')
             ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
             ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
             ->where(['a.is_del' => 0, 'a.orderCode' => $orderCode])
@@ -5141,21 +5162,37 @@ class Sale extends Base
 //                }
 
                 //修改状态,添加待办
-//                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'],
-//                ]);
+                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+                    "order_code" => $sale['orderCode'],//销售单号
+                    "status" => $sale['sale_status'],//这里的status是之前的值
+                    "action_remark" => '',//备注
+                    "action_type" => "status"//新建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'],
+                ]);
+
+                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+                    "order_code" => $sale['cgdNo'],//销售单号
+                    "status" => $sale['cgd_status'],//这里的status是之前的值
+                    "action_remark" => '',//备注
+                    "action_type" => "status"//新建create,编辑edit,更改状态status
+                ], "CGD", 4, ['orderCode' => $sale['cgdNo']]);
+
+                ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+                    "order_type" => 'CGD',
+                    "order_code" => $sale['cgdNo'],
+                    "order_id" => $sale['cgd_id'],
+                    "order_status" => 4,
+                    "before_status" => $sale['cgd_status'],
+                    'holder_id' => $sale['cgder_id'],
+                ]);
             }
             Db::name('sale')
                 ->where(['is_del' => 0, 'id' => array_column($list, 'sale_id')])
@@ -5165,13 +5202,62 @@ class Sale extends Base
                 ->where(['is_del' => 0, 'id' => array_column($list, 'cgd_id')])
                 ->update(['status' => 4, 'updatetime' => $date]);
 
-            Db::name('order_out')
+            $out_list = Db::name('order_out')
                 ->where(['is_del' => 0, 'orderCode' => $orderCode])
-                ->update(['status' => 5, 'updatetime' => $date]);
+                ->select()
+                ->toArray();
+            if (!empty($out_list)) {
+                Db::name('order_out')
+                    ->field('id,outCode,status')
+                    ->where(['is_del' => 0, 'orderCode' => $orderCode])
+                    ->update(['status' => 5, 'updatetime' => $date]);
+                foreach ($out_list as $out) {
+                    ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+                        "order_code" => $out['outCode'],//销售单号
+                        "status" => $out['status'],//这里的status是之前的值
+                        "action_remark" => '',//备注
+                        "action_type" => "status"//新建create,编辑edit,更改状态status
+                    ], "CKD", 5, $out);
+
+                    ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+                        "order_type" => 'CKD',
+                        "order_code" => $out['outCode'],
+                        "order_id" => $out['id'],
+                        "order_status" => 5,
+                        "before_status" => $out['status'],
+                        'holder_id' => $out['apply_id'],
+                    ]);
+                }
+            }
+
 
-            Db::name('order_out_child')
+            $ooc_list = Db::name('order_out_child')
                 ->where(['is_del' => 0, 'orderCode' => $orderCode])
-                ->update(['status' => 5, 'updatetime' => $date]);
+                ->select()
+                ->toArray();
+            if (!empty($ooc_list)) {
+                Db::name('order_out_child')
+                    ->where(['is_del' => 0, 'orderCode' => $orderCode])
+                    ->update(['status' => 5, 'updatetime' => $date]);
+                foreach ($ooc_list as $ooc) {
+                    ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
+                        "order_code" => $ooc['outChildCode'],//销售单号
+                        "status" => $ooc['status'],//这里的status是之前的值
+                        "action_remark" => '',//备注
+                        "action_type" => "status"//新建create,编辑edit,更改状态status
+                    ], "FHGD", 5, $ooc);
+
+                    ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
+                        "order_type" => 'FHGD',
+                        "order_code" => $ooc['outChildCode'],
+                        "order_id" => $ooc['id'],
+                        "order_status" => 5,
+                        "before_status" => $ooc['status'],
+                        'holder_id' => $ooc['apply_id'],
+                    ]);
+                }
+            }
+
 
             Db::commit();
             return json_show(0, '操作成功');