Browse Source

新写售后接口

wufeng 2 years ago
parent
commit
38729a56f6
2 changed files with 1008 additions and 23 deletions
  1. 1007 23
      app/admin/controller/After.php
  2. 1 0
      app/admin/route/app.php

+ 1007 - 23
app/admin/controller/After.php

@@ -3,6 +3,7 @@
 
 namespace app\admin\controller;
 use app\admin\model\ActionLog;
+use app\admin\model\GoodLog;
 use app\admin\model\ProcessOrder;
 use think\App;
 use think\Exception;
@@ -1187,29 +1188,1012 @@ class After extends Base
     }
 
 
+    //根据审核完成的售后申请单,创建新的销售单和采购单
+    public function createSaleAndCgdByAfter()
+    {
+
+        $param = $this->request->only(['token', 'returnCode'], 'post', 'trim');
+
+        $val = Validate::rule(['token' => 'require', 'returnCode|售后申请单编号' => 'require']);
+
+        if (!$val->check($param)) return error_show(1005, $val->getError());
+
+        //查询售后申请单详情
+        $info = Db::name('order_return')
+            ->where('returnCode', $param['returnCode'])
+            ->where('is_del', 0)
+            ->find();
+
+        if (empty($info)) return error_show(1005, '该售后申请单不存在');
+
+        if ($info['status'] != 5) return error_show(1005, '该售后申请单尚未审核完成');
+
+        $apply_id = GetUserInfo($param['token']);
+        if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
+
+        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
+        $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
+
+        //原本销售单信息
+        $old_sale = Db::name('sale')->where(['is_del' => 0, 'orderCode' => $info['orderCode']])->find();
+        if (empty($old_sale)) return error_show(1005, '没有对应的销售单信息');
+
+        //发货单信息
+        $old_out = Db::name('order_out')->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])->find();
+        if (empty($old_out)) return error_show(1005, '没有对应的发货单信息');
+
+
+        //根据是否咨询类商品,分别处理
+        if ($info['order_type'] == 3) return $this->createSaleZixun($info['good_code'], $info, $old_sale, $rm, $ri, $old_out['addrid'], $param['token']);
+        else return $this->createSaleNotZixun($info['good_code'], $info, $old_sale, $rm, $ri, $old_out, $param['token']);
+
+    }
+
+    /**
+     * 非咨询类商品处理
+     * @param string $spuCode 商品的spuCode
+     * @param array $info 售后申请单的详情
+     * @param array $old_sale 原始销售订单信息
+     * @param int $rm 操作人id
+     * @param string $ri 操作人名称
+     * @param array $old_out 原始发货单信息
+     * @param string $token 当前用户的token
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private function createSaleNotZixun(string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', array $old_out = [], string $token = '')
+    {
+        $goodinfo = Db::name('good')
+            ->alias('g')
+            ->field('g.*,gp.skuCode')
+            ->leftJoin('good_platform gp', 'gp.spuCode=g.spuCode AND gp.exam_status=6')
+            ->where('g.spuCode', $spuCode)
+            ->find();
+
+        if (empty($goodinfo)) return error_show(1005, '查不到这个商品');
+
+        $is_activity = $old_sale['is_activity'];
+        $actcode = $old_sale['activity_code'];
+        $good_num = $info['error_num'];//数量,取售后申请单中的异常数量
+        $supplierNo = $old_sale['supplierNo'];
+        $sale_price = $old_sale['sale_price'];
+        $origin_price = $old_sale['origin_price'];
+        $goodtype = $old_sale['good_type'];
+        $is_stock = $goodinfo['is_stock'];
+        $skuCode = $goodinfo['skuCode'];
+        $orderCode = makeNo("QR");
+
+        if ($is_stock == 1) {
+            $stock = Db::name("good_stock")
+                ->alias("a")
+                ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
+                ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" => $supplierNo])
+                ->field("a.id,a.wait_in_stock,a.usable_stock,a.wait_out_stock")
+                ->find();
+            if ($stock == false || $stock['usable_stock'] < $good_num) return error_show(1003, "库存数量不足");
+
+        } else {
+            if ($is_activity == 0) {
+                $origin = Db::name("good_nake")
+                    ->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])
+                    ->order("min_num desc")
+                    ->find();
+
+                if (empty($origin)) return error_show(1003, "未找到相关阶梯成本价格");
+
+                $origin_price = $origin['nake_total'];
+            }
+        }
+
+        $goodinfo['cgd_gold_price'] = 0;
+        if ($goodtype == 1) {
+            if ($is_activity == 1) {
+                $act = Db::name("activity_info")
+                    ->alias("a")
+                    ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
+                    ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
+                    ->find();
+                if ($act == false) return error_show(1003, "未找到相关活动价");
+
+                if ($act['moq_num'] > $good_num) return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
+
+                if ($act['activity_stock'] < $good_num) return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
+
+                $origin_price = $act['cost_price'];
+            } else {
+                $good = Db::name("good_ladder")
+                    ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
+                    ->where([["min_num", "<=", $good_num]])
+                    ->order("min_num desc")
+                    ->find();
+
+                if ($good == false) return error_show(1003, "未找到相关阶梯价格");
+
+                if ($goodinfo['is_gold_price'] == 1 && $is_stock != 1) {
+                    $gold = Db::name("gold_price1")
+                        ->field('id,price')
+                        ->where(["type" => $goodinfo['noble_metal'], "is_del" => 0, "status" => 1])
+                        ->order("addtime desc")
+                        ->find();
+
+                    $goodinfo['cgd_gold_price'] = $gold["price"];
+                }
+            }
+
+        }
+
+        //原有采购单信息
+        $old_cgd = Db::name('order_num')
+            ->alias('on')
+            ->field('on.id onid,po.*')
+            ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
+            ->where('on.orderCode', $old_sale['orderCode'])
+            ->find();
+
+        $cgd = array_merge($old_cgd, [
+            'id' => null,
+//            "supplierNo" => $ct['supplierNo'],
+//            "companyNo" => $supplierNo,
+            "orderCode" => $orderCode,
+            "spuCode" => $spuCode,
+            "skuCode" => $skuCode,
+//            "good_name" => $ct['good_name'],
+            "sale_price" => $sale_price,
+            "total_fee" => $sale_price * $good_num,
+//            "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
+//            "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
+//            "open_fee" => $ct['open_fee'],
+//            "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
+            'cost_fee' => $old_cgd['teach_fee'],
+//            "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
+//            "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
+//            "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
+//            "demo_fee" => $ct['demo_fee'],
+            "good_num" => $good_num,
+//            "good_type" => $goodtype,
+//            "weight" => $ct['noble_weight'],
+//            "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
+//            "order_type" => $is_stock == 1 ? 1 : 2,
+//            "order_source" => 1,//1直接下单
+            "createrid" => $rm,
+            "creater" => $ri,
+//            'send_way' => 2,
+            'token' => $token,
+            'addtime' => date('Y-m-d H:i:s'),
+            'updatetime' => date('Y-m-d H:i:s'),
+        ]);
+//        $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
+        $send_num = 0;
+//        if($sendtype==1){
+//            if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
+//                return error_show(1004,"参数addrlist不能为空");
+//            }
+//            $send_num = array_sum(array_column($addrlist,"receipt_quantity"));
+//        }
+//        if($send_num>$good_num){
+//            return error_show(1004,"发货数量超出订单总数量");
+//        }
+//        $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
+//        $proof_id =isset($this->post['proof_id']) && $this->post['proof_id'] !=="" ?intval($this->post['proof_id']):0;
+
+//        $apply_id = GetUserInfo($param['token']);
+//        if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
+//        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
+//        $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
+        Db::startTrans();
+        try {
+            $data = array_merge($old_sale, [
+                'id' => null,
+                "orderCode" => $orderCode,
+                "good_code" => $spuCode,
+                "skuCode" => $skuCode,
+//                "customer_code" => $info['customer_code'],
+//                "good_name" => (isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '') ? $goodinfo['good_name'] : '',
+                "good_num" => $good_num,
+//                "cat_id" => $goodinfo['cat_id'],
+                "apply_id" => $rm,
+                "apply_name" => $ri,
+//                "origin_price" => $origin_price,
+                "sale_price" => $sale_price,
+//                "post_fee" => 0,
+//                "status" => 0,
+//                "send_num" => 0,
+                "wsend_num" => $good_num,
+//                "send_status" => 0,
+//                "good_type" => $goodtype,
+//                "send_type" => $old_sale['send_type'],
+//                "supplierNo" => $supplierNo,
+//                "is_del" => 0,
+//                "zxNo" => "",
+//                "platform_order" => $old_sale['platform_order'],
+//                "platform_id" => $old_sale['platform_id'],
+//                "remark" => $old_sale['remark'] . $old_sale['orderCode'],
+//                "is_stock" => $is_stock,
+                "is_activity" => $is_activity,
+//                "order_type" => $is_stock == 1 ? 1 : 2,
+//                "order_source" => 1,//1直接下单
+//                "poNo"=>$poNo,
+//                'good_weight' => $ct['noble_weight'],
+//                'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
+//                'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
+//                'diff_weight' => 0,
+//                'diff_fee' => 0,
+//                "workNo" => '',
+                "addtime" => date("Y-m-d H:i:s"),
+                "updatetime" => date("Y-m-d H:i:s"),
+//                'total_price' => round($sale_price * $good_num, 2),
+//                'proof_id' => $old_sale['proof_id'],
+//                'paytime' => $old_sale['paytime'],
+
+            ]);
+
+            $datainfo = Db::name('sale')->insertGetId($data);
+            if ($datainfo > 0) {
+
+                //修改状态,添加待办
+                ActionLog::logAdd($token, [
+                    "order_code" => $orderCode,//销售单code
+                    "status" => 0,//这里的status是之前的值
+                    "action_remark" => '',//备注
+                    "action_type" => "create"//新建create,编辑edit,更改状态status
+                ], "XSQRD", 0, $this->post);
+
+                ProcessOrder::AddProcess($token, [
+                    "order_type" => 'XSQRD',
+                    "order_code" => $orderCode,//销售单code
+                    "order_id" => $datainfo,
+                    "order_status" => 0, "before_status" => 0
+                ]);
+
+                if ($is_activity == 1) {
+                    $actup = [
+                        "activity_stock" => $act['activity_stock'] - $good_num,
+                        "updatetime" => date("Y-m-d H:i:s")
+                    ];
+                    $actupp = Db::name("activity_info")
+                        ->where(["skuCode" => $skuCode, "activity_code" => $actcode, "is_del" => 0, "status" => 1])
+                        ->save($actup);
+                    if ($actupp == false) throw new Exception('活动库存修改失败');
+
+                }
+                $outstatus = 0;
+                $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $old_sale['customer_code'], 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
+                if ($is_stock == 0) $this->createCgd($cgd, $standing_book_da);
+                else {
+                    $outstatus = 1;
+                    $bol = $this->RelaCgd([
+                        'orderCode' => $orderCode,
+                        "good_num" => $good_num,
+                        "spuCode" => $spuCode,
+                        "companyNo" => $supplierNo,
+                        "cost_fee" => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
+                        'order_type' => $data['order_type'],
+                        'order_source' => $data['order_source'],
+                    ], $standing_book_da);
+
+                    if (isset($stock)) {
+
+                        $stck = [
+                            "usable_stock" => $stock['usable_stock'] - $good_num,
+                            "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
+                            "updatetime" => date("Y-m-d H:i:s")
+                        ];
+
+                        $upad = Db::name("good_stock")->where($stock)->update($stck);
+                        if ($upad == false) throw new Exception('库存商品更新库存失败');
+
+                        //商品变动日志表,good_log_code字段存储采购单号
+                        $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
+                        $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
+                        GoodLog::LogAdd($token, $good_data, "XSQRD");
+                    }
+
+                }
+
+//                $old_out = Db::name('order_out')
+//                    ->field('id,addrid,wsm_code')
+//                    ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])
+//                    ->find();
+                $outCode = makeNo("DF");
+                $out = array_merge($old_out, [
+                    'id' => null,
+                    "orderCode" => $orderCode,
+                    "outCode" => $outCode,
+                    "apply_id" => $rm,
+                    "apply_name" => $ri,
+                    "addrid" => $old_out['addrid'],
+                    "post_name" => "",
+                    "post_code" => "",
+                    "post_fee" => 0,
+                    "sendtime" => date("Y-m-d H:i:s"),
+                    "send_num" => $good_num,
+                    "check_num" => 0,
+                    "error_num" => 0,
+                    "wsm_code" => isset($old_out['wsm_code']) ? $old_out['wsm_code'] : '',
+                    "order_type" => $is_stock == 1 ? 1 : 2,
+                    "status" => $outstatus,
+                    "addtime" => date("Y-m-d H:i:s"),
+                    "updatetime" => date("Y-m-d H:i:s")
+                ]);
+
+                Db::name("order_out")->insert($out);
+                $standing_book_da['outCode'] = $outCode;
+
+                if (!isset($standing_book_da['returnCode'])) $standing_book_da['returnCode'] = '';
+                if (!isset($standing_book_da['thNo'])) $standing_book_da['thNo'] = '';
+                if (!isset($standing_book_da['returnGoodCode'])) $standing_book_da['returnGoodCode'] = '';
+                if (!isset($standing_book_da['wsm_in_code'])) $standing_book_da['wsm_in_code'] = '';
+                if (!isset($standing_book_da['cgdReturnCode'])) $standing_book_da['cgdReturnCode'] = '';
+
+                //处理台账
+                Db::name('standing_book')->insert(array_merge($standing_book_da, ['addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo('IO')]));
+
+                Db::commit();
+                return error_show(0, "操作成功");
+            } else throw new Exception('咨询订单创建失败');
+        } catch (\Exception $e) {
+            Db::rollback();
+            return error_show(1005, $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
+        }
+    }
+
+
+
     /**
-     * @todo 售后功能新写的接口,暂存
-     * //根据审核完成的售后申请单,创建新的销售单和采购单
-     * public function newCreateAfter(){
-     *
-     * $param = $this->request->only(['token','returnCode'],'post','trim');
-     *
-     * $val = Validate::rule(['token'=>'require','returnCode|售后申请单编号'=>'require']);
-     *
-     * if(!$val->check($param)) return error_show(1005,$val->getError());
-     *
-     * //查询售后申请单详情
-     * $info = Db::name('order_return')
-     * ->where('returnCode',$param['returnCode'])
-     * ->where('is_del',0)
-     * ->find();
-     *
-     * if(empty($info)) return error_show(1005,'该售后申请单不存在');
-     *
-     * if($info['status']!=5) return error_show(1005,'该售后申请单尚未审核完成');
-     *
-     * }
-     *
-     **/
+     * 咨询类商品处理
+     * @param string $spuCode 商品的spuCode
+     * @param array $info 售后申请单的详情
+     * @param array $old_sale 原始销售订单
+     * @param int $rm 操作人id
+     * @param string $ri 操作人名称
+     * @param int $addrid 原始发货单的地址id
+     * @param string $token  当前用户的token
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private function createSaleZixun(string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', int $addrid = 0, string $token = '')
+    {
+//        $bidNo =isset($this->post['bidNo'])&&$this->post['bidNo']!="" ?  trim($this->post['bidNo']):"";
+//        if($bidNo==""){
+//            return error_show(1003,"参数bidNo不能为空");
+//        }
+
+        $zxinfo = Db::name("consult_bids")->where(["bidNo" => $old_sale['zxNo'], "is_del" => 0])->find();
+        if ($zxinfo == false) return error_show(1005, '未找到咨询单商品信息');
+
+//        if($zxinfo['status']!=6){
+//            return error_show(1003,"咨询单状态有误无法转单");
+//        }
+//        $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->find();
+//        if($zx==false){
+//            return error_show(1003,"未找到咨询单信息");
+//        }
+//        if($zx['status']!=4){
+//            return error_show(1003,"咨询单状态有误无法转单");
+//        }
+//        $zxorder =Db::name("consult_order")->where(["zxNo"=>$zx["zxNo"],"is_del"=>0])->find();
+//        if($zxorder==false){
+//            return error_show(1003,"未找到咨询单信息");
+//        }
+//        $good_num =isset($this->post['good_num'])&&$this->post['good_num']!="" ?  intval($this->post['good_num']):"";
+//        if($good_num===""){
+//            return error_show(1003,"参数 good_num 不能为空");
+//        }
+        $good_num = $info['error_num'];
+        $sale_price = 0;
+//        $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
+//        if ($sale_price === "") {
+//            return error_show(1003, "参数 sale_price 销售单价 不能为空");
+//        }
+//        if ($sale_price < $zxinfo['sale_price']) {
+//            return error_show(1003, "修改的销售单价不能低于原来的销售单价");
+//        }
+//        $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
+//        if($sendtype==""){
+//            return error_show(1003,"参数sendtype不能为空");
+//        }
+
+//        $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
+
+        $orderCode = makeNo("QR");
+//        $spuCode = $zxinfo['spuCode'];
+        $skuCode = "";
+//        $is_stock=0;
+
+        //原有采购单信息
+        $old_cgd = Db::name('order_num')
+            ->alias('on')
+            ->field('on.id onid,po.*')
+            ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
+            ->where('on.orderCode', $old_sale['orderCode'])
+            ->find();
+
+        $cgd = array_merge($old_cgd, [
+            'id' => null,
+//            "supplierNo"=>$zxinfo['supplierNo'],
+//            "companyNo"=>$zxorder['companyNo'],
+            "spuCode" => $spuCode,
+            "skuCode" => $skuCode,
+            "orderCode" => $orderCode,
+//            "good_name"=>$zxinfo['good_name'],
+            "sale_price" => $sale_price,
+//            "total_fee"=>round($zxinfo['total_fee']* $good_num, 2),
+//            "pakge_fee"=>$zxinfo['pakge_fee'],
+//            "cert_fee"=>$zxinfo['cert_fee'],
+//            "open_fee"=>$zxinfo['open_fee'],
+            'cost_fee' => $old_cgd['teach_fee'],
+//            $old_sale['cost_fee']=$old_sale['teach_fee'];
+//            "mark_fee"=>$zxinfo['mark_fee'],
+//            "demo_fee"=>$zxinfo['demo_fee'],
+//            "nake_fee"=>$zxinfo['nake_fee'],
+//            "delivery_fee"=>$zxinfo['delivery_fee'],
+            "good_num" => $good_num,
+            "createrid" => $rm,
+            "creater" => $ri,
+            "good_type" => 1,
+//            "weight"=>$zxinfo['good_weight'],
+//            "gold_price"=>$zxinfo['gold_price'],
+//            "is_diff"=>$zxinfo['is_diff'],
+            "order_type" => 3,//咨询商品
+            "order_source" => 2,//咨询
+//            'send_way'=>$zxinfo['send_way']
+            'token' => $token
+        ]);
+//        $token=isset($this->post['token'])&&$this->post['token']!=""? trim($this->post['token']):"";
+//        if($token==""){
+//            return error_show(102, "参数token不能为空");
+//        }
+//        $apply_id = GetUserInfo($token);
+//        if (empty($apply_id) || $apply_id['code'] != 0) {
+//            return error_show(102, "申请人数据不存在");
+//        }
+//        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
+//        $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
+
+//        $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
+//        $send_num=0;
+//        if($sendtype==1){
+//            if ($va == "") {
+//                return error_show(1002, "参数order_addr不能为空");
+//            }
+//            $send_num = array_sum(array_column($va,"receipt_quantity"));
+//        }
+//        if($good_num<$send_num){
+//            return error_show(1002, "发货数量不能超过购买数量");
+//        }
+//        $arrtime=isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
+//        if($arrtime==""){
+//            return error_show(1002, "参数arrtime不能为空");
+//        }
+
+        //新加字段
+//        $platform_order=isset($this->post['platform_order'])&&$this->post['platform_order']!="" ?$this->post['platform_order']:"";
+//        $workNo=isset($this->post['workNo'])&&$this->post['workNo']!="" ?$this->post['workNo']:"";
+//        $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
+//        $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
+
+
+        Db::startTrans();
+        try {
+//            $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->lock(true)->find();
+//            if($zx==false){
+//                Db::rollback();
+//                return error_show(1003,"未找到咨询单信息");
+//            }
+
+            $data = array_merge($old_sale, [
+                'id' => null,
+                "orderCode" => $orderCode,
+                "good_code" => $spuCode,
+//                "skuCode"=>$skuCode,
+//                "customer_code"=>$zxorder['khNo'],
+//                "good_name"=>isset($zxinfo['good_name'])&&$zxinfo['good_name']!==''?$zxinfo['good_name']:'',
+                "good_num" => $good_num,
+//                "cat_id"=>$zxinfo['cat_id'],
+                "apply_id" => $rm,
+                "apply_name" => $ri,
+//                "origin_price"=>$zxinfo['total_fee'],
+                "sale_price" => $sale_price,//销售单价支持修改
+//                "post_fee"=>0,
+                "status" => 0,
+                "send_num" => 0,
+                "wsend_num" => $good_num,
+//                "send_status"=>1,
+                "good_type" => 1,
+//                "send_type"=>$sendtype,
+//                "supplierNo"=>$zxorder['companyNo'],
+                "is_del" => 0,
+//                "zxNo"=>$bidNo,
+//                "platform_order"=>$platform_order,
+//                "platform_id"=> $zxorder['platform_code'],
+                "remark" => $old_sale['orderCode'],
+//                "arrive_time"=>$arrtime,
+//                "is_stock"=>0,
+//                "is_activity"=>0,
+//                "proof_id"=>0,
+                "order_type" => 3,
+                "order_source" => 2,//2咨询
+//                'good_weight'=>$zxinfo['good_weight'],
+//                'gold_price'=>$zxinfo['gold_price'],
+//                'cost_price'=>$zxinfo['sale_cost_fee'],
+//                'diff_weight'=>0,
+//                'diff_fee'=>0,
+                "addtime" => date("Y-m-d H:i:s"),
+                "updatetime" => date("Y-m-d H:i:s"),
+//                'total_price' => round($sale_price * $good_num, 2),
+//                'workNo'=>$workNo,
+            ]);
+
+            $datainfo = Db::name('sale')->insertGetId($data);
+            if ($datainfo > 0) {
+
+                $standing_book_data = [
+                    'sale_id' => $datainfo,
+                    'infoNo' => $zxinfo['infoNo'],
+                    'updatetime' => date('Y-m-d H:i:s')
+                ];
+
+                //修改状态,添加待办
+                ActionLog::logAdd($this->post['token'], [
+                    "order_code" => $orderCode,//销售单code
+                    "status" => 0,//这里的status是之前的值
+                    "action_remark" => '',//备注
+                    "action_type" => "create"//新建create,编辑edit,更改状态status
+                ], "XSQRD", $data['status'], $data);
+
+                ProcessOrder::AddProcess($this->post['token'], [
+                    "order_type" => 'XSQRD',
+                    "order_code" => $orderCode,//销售单code
+                    "order_id" => $datainfo,
+                    "order_status" => $data['status'], "before_status" => 0
+                ]);
+//                $old_zx_status = $zx['status'];
+//                $zx['status']=5;
+//                $zx['updatetime']=date("Y-m-d H:i:s");
+//                $sa=Db::name("consult_info")->save($zx);
+//                if($sa==false){
+//                    Db::rollback();
+//                    return error_show(1002,"咨询单修改失败");
+//                }else{
+//                    //修改状态,添加待办
+//                    ActionLog::logAdd($this->post['token'], [
+//                        "order_code" => $zx['infoNo'],//咨询单详情编号
+//                        "status" => $old_zx_status,//这里的status是之前的值
+//                        "action_remark" => '',//备注
+//                        "action_type" => "status"//新建create,编辑edit,更改状态status
+//                    ], "ZXD", 5, $zx);
+//
+//                }
+                $this->createCgd($cgd, $standing_book_data);
+//                if($bol==false){
+//                    Db::rollback();
+//                    return error_show(1002,"咨询订单创建失败");
+//                }
+//                $limt=[
+//                    "spuCode"=>$zxinfo['spuCode'],
+//                    "good_name"=>$zxinfo['good_name'],
+//                    "brand_id"=>$zxinfo['brand_id'],
+//                    "good_unit"=>$zxinfo['unit_id'],
+//                    "cat_id"=>$zxinfo['cat_id'],
+//                    "good_type"=>0,
+//                    "moq"=>1,
+//                    "customized"=>$zxinfo['work_day'],
+//                    "tax"=>$zxinfo['tax'],
+//                    "supplierNo"=>$zxinfo["supplierNo"],
+//                    "is_auth"=>0,
+//                    "craft_desc"=>$zxinfo['good_name'],
+//                    "good_remark"=>"",
+//                    "platform_id"=>$zxorder['platform_id'],
+//                    "good_img"=>$zxinfo['good_img'],
+//                    "good_thumb_img"=>"",
+//                    "good_info_img"=>"",
+//                    "specinfo"=>$zxinfo['specinfo'],
+//                    "work_day"=>$zxinfo['work_day'],
+//                    "noble_metal"=>$zxinfo['metal_id'],
+//                    "is_gold_price"=>$zxinfo['is_gold_price'],
+//                    "config"=>$zxinfo['config'],
+//                    "other_config"=>$zxinfo['other_config'],
+//                    "weight"=>$zxinfo['weight'],
+//                    "good_weight"=>$zxinfo['good_weight'],
+//                    "is_diff"=>$zxinfo['is_diff'],
+//                    "supply_area"=>$zxinfo['supply_area'],
+//                    "pay_way"=>$zxinfo['pay_way'],
+//                    "send_way"=>$zxinfo['send_way'],
+//                    "companyNo"=>$zxorder['companyNo'],
+//                    "status"=>1,
+//                    "is_del"=>0,
+//                    "createrid"=>$zxinfo['createrid'],
+//                    "creater"=>$zxinfo['creater'],
+//                    "addtime"=>date("Y-m-d H:i:s"),
+//                    "updatetime"=>date("Y-m-d H:i:s"),
+//                    'proof_type' => $proof_type,//凭证类型
+//                    'proof_url' => $proof_url,//凭证文件
+//                ];
+//                $good = Db::name("good_zixun")->insert($limt);
+//                if(!$good){
+//                    Db::rollback();
+//                    return error_show(1006,"咨询商品录入失败");
+//                }
+//                if(!empty($va)){
+                $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
+                if (empty($order)) throw new Exception('采购单关联数据未找到');
+
+//                    foreach ($va as $value){
+//                        $temp=[];
+//                        $addrs=[];
+//                        if($value['addr_code']!==''&&is_array($value['addr_code'])&&!empty($value['addr_code'])){
+//                            $addrs['provice_code'] = $value['addr_code'][0];
+//                            $addrs['city_code'] = $value['addr_code'][1];
+//                            $addrs['area_code'] = $value['addr_code'][2];
+//                            $addr = json_encode($addrs);
+//                        }else{
+//                            $addr = isset($value['addr_code'])?$value['addr_code']:'';
+//                        }
+//                        $temp['orderCode']=$orderCode;
+//                        $temp['contactor']=$value['contactor'];
+//                        $temp['mobile'] = $value['mobile'];
+//                        $temp['addr'] = $value['addr'];
+//                        $temp['addr_code']=$addr;
+//                        $temp['customer_code'] =$zxorder['khNo'];
+//                        $temp['receipt_quantity']=$value['receipt_quantity'];
+//                        $temp['post_fee'] =0;
+//                        $temp['is_del'] =0;
+//                        $temp['addtime'] =date("Y-m-d H:i:s");
+//                        $temp['updatetime'] =date("Y-m-d H:i:s");
+//                        $temp['arrive_time']=$arrtime;
+//                        $vmp = Db::name('order_addr')->insert($temp,true);
+//                        if($vmp>0){
+                $outCode = makeNo("DF");
+                $out = [
+                    "orderCode" => $orderCode,
+                    "outCode" => $outCode,
+                    "apply_id" => $rm,
+                    "apply_name" => $ri,
+                    'addrid' => $addrid,
+                    "post_name" => "",
+                    "post_code" => "",
+                    "post_fee" => 0,
+                    "sendtime" => date("Y-m-d H:i:s"),
+                    "send_num" => $good_num,
+                    "check_num" => 0,
+                    "error_num" => 0,
+                    "wsm_code" => "",
+                    "order_type" => 3,
+                    "status" => 0,
+                    "addtime" => date("Y-m-d H:i:s"),
+                    "updatetime" => date("Y-m-d H:i:s")
+                ];
+                $ou = Db::name("order_out")->insert($out);
+                if ($ou == false) throw new Exception('发货地址添加创建失败');
+                else {
+                    //修改状态,添加待办
+                    ActionLog::logAdd($this->post['token'], [
+                        "order_code" => $outCode,//出库单号
+                        "status" => 0,//这里的status是之前的值
+                        "action_remark" => '',//备注
+                        "action_type" => "create"//新建create,编辑edit,更改状态status
+                    ], "CKD", 0, $out);
+
+                    ProcessOrder::AddProcess($this->post['token'], [
+                        "order_type" => 'CKD',
+                        "order_code" => $outCode,//出库单号
+                        "order_id" => Db::name("order_out")->getLastInsID(),
+                        "order_status" => 0, "before_status" => 0
+                    ]);
+
+                    //将发货编号添加到台账中
+                    $standing_book_data['outCode'][] = $outCode;
+                }
+                $order['send_num'] += $good_num;
+                $order['wsend_num'] -= $good_num;
+                if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
+                $ups = Db::name("order_num")->save($order);
+                if ($ups) {
+                    $sen = Db::name("order_send")->save([
+                        "cgdNo" => $order['cgdNo'],
+                        "outCode" => $outCode,
+                        "send_num" => $good_num,
+                        "status" => 1,
+                        "addtime" => date("Y-m-d H:i:s"),
+                        "updatetime" => date("Y-m-d H:i:s")
+                    ]);
+                    if ($sen == false) throw new Exception('发货地址添加创建失败');
+                }
+
+//                        }else{
+//                            Db::rollback();
+//                            return error_show(1002,"发货地址添加创建失败");
+//                        }
+//                    }
+
+
+                //处理台账
+                if (isset($standing_book_data['outCode'])) {
+                    Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(`outCode`,'," . implode(',', $standing_book_data['outCode']) . "'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `infoNo`='{$standing_book_data['infoNo']}'");
+                    unset($standing_book_data['outCode']);
+                }
+
+                Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
+
+                Db::commit();
+                return app_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
+            }
+            Db::rollback();
+            return error_show(1002, "咨询订单创建失败");
+        } catch (Exception $e) {
+            Db::rollback();
+            return error_show(1003, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
+        }
+    }
+
+
+    //创建采购单
+    private function createCgd($data,array &$standing_book_da=[]){
+        $cgdCode = makeNo("CG");
+        $supplier=Db::name("supplier")->where(["code"=>$data['supplierNo'],"is_del"=>0])->find();
+        if($supplier==false) throw new Exception('该供应商不存在');
+
+
+        $wsm= Db::name("warehouse_info")
+            ->where(["supplierNo"=>$data["supplierNo"],"companyNo"=>$data['companyNo'],"wsm_type"=>2,"is_del"=>0])
+            ->find();
+        if($wsm==false){
+            $wsm_code = makeNo("WSM");
+            $inwsm=[
+                "wsm_code"=>$wsm_code,
+                "name"=>$supplier['name'],
+                "wsm_type"=>2,
+                "supplierNo"=>$supplier['code'],
+                "addr"=>"",
+                "addrs_code"=>"",
+                "contactor"=>$data['order_type']==1? $supplier['personid']:$data['createrid'],
+                "contactor_name"=>$data['order_type']==1? $supplier['person']:$data['creater'],
+                "mobile"=>"",
+                "position"=>"",
+                "companyNo"=>$data['companyNo'],
+                "status"=>1,
+                "is_del"=>0,
+                "addtime"=>date("Y-m-d H:i:s"),
+                "updatetime"=>date("Y-m-d H:i:s")
+            ];
+            $in = Db::name("warehouse_info")->insert($inwsm);
+            if($in==false) throw new Exception('仓库新增失败');
+
+        }else $wsm_code =$wsm['wsm_code'];
+
+
+        $cg =[
+            "cgdNo"=>$cgdCode,
+            "bkcode"=>"",
+            "wsm_code"=>$wsm_code,
+            "cgder"=>$data['creater'],
+            "cgder_id"=>$data['createrid'],
+            "spuCode"=>$data['spuCode'],
+            "skuCode"=>$data['skuCode'],
+            "good_name"=>$data['good_name'],
+            "good_num"=>$data['good_num'],
+            "good_price"=>$data['sale_price'],
+            "total_fee"=>round($data['sale_price']*$data['good_num'],2),
+            "pakge_fee"=>$data['pakge_fee'],
+            "cert_fee"=>$data['cert_fee'],
+            "open_fee"=>$data['open_fee'],
+            "teach_fee"=>$data['cost_fee'],
+            "mark_fee"=>$data['mark_fee'],
+            "demo_fee"=>$data['demo_fee'],
+            "nake_fee"=>$data['nake_fee'],
+            "weight"=>$data['weight'],
+            "delivery_fee"=>$data['delivery_fee'],
+            "gold_price"=>$data['gold_price'],
+            "diff_weight"=>"0",
+            "diff_fee"=>"0",
+            "supplierNo"=>$data['supplierNo'],
+            "supplier_name"=>$supplier['name'],
+            "companyNo"=>$data['companyNo'],
+            "send_status"=>1,
+            "send_num"=>0,
+            "wsend_num"=>$data['good_num'],
+            "remark"=>"",
+            "status"=>0,//0初始化
+            "lasttime"=>date("Y-m-d H:i:s"),
+            "is_del"=>0,
+            "order_type"=>$data['order_type'],
+            "order_source"=>$data['order_source'],
+            "good_type"=>$data['good_type'],
+            "addtime"=>date("Y-m-d H:i:s"),
+            "updatetime"=>date("Y-m-d H:i:s")
+        ];
+        $up =Db::name("purchease_order")->insertGetId($cg);
+        if($up){
+            //修改状态,添加待办
+            ActionLog::logAdd($this->post['token'], [
+                "order_code" => $cg['cgdNo'],//销售单code
+                "status" => 0,//这里的status是之前的值
+                "action_remark" => '',//备注
+                "action_type" => "create"//新建create,编辑edit,更改状态status
+            ], "CGD", $cg['status'], $cg);
+
+            ProcessOrder::AddProcess($this->post['token'], [
+                "order_type" => 'CGD',
+                "order_code" => $cg['cgdNo'],//销售单code
+                "order_id" => $up,
+                "order_status" => $cg['status'],"before_status"=> 0
+            ]);
+
+            $standing_book_da = array_merge($standing_book_da, [
+                'orderCode' => $data['orderCode'],
+                'cgdNo' => $cgdCode,
+                'spuCode' => $data['spuCode'],
+                'skuCode' => $data['skuCode'],
+                'order_type' => $data['order_type'],
+                'order_source' => $data['order_source'],
+                'supplierNo' => $data['supplierNo'],
+                "companyNo" => $data['companyNo'],
+            ]);
+
+            $rela=[
+                "orderCode"=>$data['orderCode'],
+                "cgdNo"=>$cgdCode,
+                "spuCode"=>$data['spuCode'],
+                "good_num"=>$data['good_num'],
+                "wsend_num"=>$data['good_num'],
+                "send_num"=>0,
+                "wait_num"=>0,
+                "status"=>1,
+                "source"=>2
+            ];
+
+            Db::name("order_num")->save($rela);
+
+            $stokc =Db::name("good_stock")
+                ->where(['spuCode'=>$data['spuCode'],"wsm_code"=>$wsm_code, "is_del"=>0])
+                ->find();
+                if($stokc==false){
+                    $stokc=[
+                        "spuCode"=>$data['spuCode'],
+                        "wsm_code"=>$wsm_code,
+                        "wait_in_stock"=>$data['good_num'],
+                        "wait_out_stock"=>0,
+                        "usable_stock"=>0,
+                        "intra_stock"=>0,
+                        "total_stock"=>0,
+                        "status"=>1,
+                        "addtime"=>date("Y-m-d H:i:s"),
+                        "updatetime"=>date("Y-m-d H:i:s")
+                    ];
+                }else{
+                    $stokc['wait_in_stock']+=$data['good_num'];
+                    $stokc['updatetime']=date("Y-m-d H:i:s");
+                }
+                $stoc= Db::name("good_stock")->save($stokc);
+                if($stoc==false) throw new Exception('商品仓库库存修改失败');
+
+
+                $good_data[] = ['good_log_code' =>$cgdCode, "stock_id" => isset($stoc['id'])?$stoc['id']:Db::name("good_stock")->getLastInsID(), "type" => 1,'stock'=>$data['good_num'], "stock_name" => "wait_in_stock"];
+                GoodLog::LogAdd($data['token'],$good_data,"CGD");
+                return true;
+            }else  throw new Exception('商品仓库库存修改失败');
+
+    }
+
+    //创建采购单
+    public function RelaCgd($outinfo,array &$standing_book_da=[]){
+        $cgd = Db::name("order_bk")->where([["spuCode","=",$outinfo['spuCode']],["is_del","=",0],["balance_num",">=",$outinfo['good_num']],['companyNo',"=",$outinfo['companyNo']]])->lock(true)->find();
+        if($cgd==false) throw new  Exception('未查询到备库单信息');
+
+
+        $good=Db::name("good")
+            ->where(["spuCode"=>$outinfo['spuCode'],"is_del"=>0])
+            ->find();
+        if($good==false) throw new  Exception('未查询到商品信息');
+
+        $cgdinfo =Db::name("purchease_order")
+            ->where(['cgdNo'=>$cgd['cgdNo'],"is_del"=>0])
+            ->find();
+        if($cgdinfo==false) throw new  Exception('未查询到采购单信息');
+
+        $QrdCgd=[
+            "cgdNo"=>makeNo("CG"),
+            "bkcode" => $cgdinfo['bkcode'],
+            'wsm_code'=>$cgdinfo['wsm_code'],
+            "cgder_id"=>$cgdinfo['cgder_id'],
+            "cgder"=>$cgdinfo['cgder'],
+            "spuCode"=>$cgdinfo['spuCode'],
+            "good_name"=>$cgdinfo['good_name'],
+            "good_num"=>$outinfo['good_num'],
+            "good_price"=>$cgdinfo['good_price'],
+            "total_fee"=>round($cgdinfo['good_price']*$outinfo['good_num'],2),
+            "pakge_fee"=>$cgdinfo['pakge_fee'],
+            "cert_fee"=>$cgdinfo['cert_fee'],
+            "open_fee"=>$cgdinfo['open_fee'],
+            "delivery_fee"=>$cgdinfo['delivery_fee'],
+            "mark_fee"=>$cgdinfo['mark_fee'],
+            "teach_fee"=>$cgdinfo['teach_fee'],
+            "nake_fee"=>$cgdinfo['nake_fee'],
+            "demo_fee"=>$cgdinfo['demo_fee'],
+            "weight"=>$cgdinfo['weight'],
+            "diff_weight"=>$cgdinfo['diff_weight'],
+            "diff_fee"=>$cgdinfo['diff_fee'],
+            "gold_price"=>$cgdinfo['gold_price'],
+            "supplierNo"=>$cgdinfo['supplierNo'],
+            "supplier_name"=>$cgdinfo['supplier_name'],
+            "companyNo"=>$cgdinfo['companyNo'],
+            "send_status"=>3,
+            "send_num"=>$outinfo['good_num'],
+            "wsend_num"=>0,
+            "remark"=>$cgdinfo['remark'],
+            "status"=>3,
+            "lasttime"=>$cgdinfo['lasttime'],
+            "is_del"=>0,
+            "order_type"=>$outinfo['order_type'],
+            "order_source"=>$outinfo['order_source'],
+            "good_type"=>$cgdinfo['good_type'],
+            "addtime"=>date("Y-m-d H:i:s"),
+            "updatetime"=>date("Y-m-d H:i:s")
+        ];
+        $insetrCgd=Db::name("purchease_order")->insert($QrdCgd);
+        if($insetrCgd==false) throw new  Exception('新增采购单失败');
+        else{
+            $standing_book_da = array_merge($standing_book_da, [
+                'orderCode' => $outinfo['orderCode'],
+                'cgdNo' => $cgdinfo['cgdNo'],
+                'spuCode' => $outinfo['spuCode'],
+                'order_type' => $QrdCgd['order_type'],
+                'order_source' => $QrdCgd['order_source'],
+                'supplierNo' => $cgdinfo['supplierNo'],
+                'companyNo' => $cgdinfo['companyNo'],
+                'bk_code' => $cgdinfo['bkcode'],
+                'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id',0),
+            ]);
+        }
+        if($good['is_gold_price']==1 && $good['is_stock']==1){
+            $gold = Db::name("gold_price1")
+                ->field('id,price')
+                ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
+                ->order("addtime desc")
+                ->find();
+
+            $ct['cgd_gold_price']=$gold["price"];
+            $updat=[
+                "sale_price"=>0,
+                "total_price"=>0,
+                "origin_price"=>$cgdinfo['good_price'],
+                "gold_price"=>$gold["price"],
+            ];
+            Db::name("sale")->where(["orderCode"=>$outinfo['orderCode']])->update($updat);
+//            if($upsale==false){
+//                return false;
+//            }
+        }
+        $merge_num = Db::name("purchease_order")
+            ->where(["bkcode"=>$cgdinfo['bkcode'],"order_type"=>1,"is_del"=>0])
+            ->where("order_source","<>",0)
+            ->field("sum(send_num)-sum(th_num) as num")
+            ->find();
+        $cgd['balance_num']=$cgd['total_num']-$merge_num['num'];
+        $cgd['merge_num']=$merge_num['num'];
+        $cgd['updatetime']=date("Y-m-d H:i:s");
+        $up=Db::name("order_bk")->save($cgd);
+        if($up==false) throw new  Exception('修改备库单失败');
+
+        $data=[
+            "orderCode"=>$outinfo['orderCode'],
+            "cgdNo"=>$QrdCgd['cgdNo'],
+            "spuCode"=>$outinfo['spuCode'],
+            "companyNo"=>$outinfo['companyNo'],
+            "good_num"=>$outinfo['good_num'],
+            "wsend_num"=>$outinfo['good_num'],
+            "send_num"=>0,
+            "wait_num"=>$outinfo['good_num'],
+            "status"=>1,
+            "source"=>1,
+        ];
+        $order =Db::name("order_num")->save($data);
+        if($order==false) throw new  Exception('修改采购单销售单关联失败');
+    }
+
+
+
 
 }

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

@@ -252,6 +252,7 @@ Route::rule('afterpost','admin/After/addpost');
 Route::rule('aftergys','admin/After/GysFeed');
 Route::rule('afterwsm','admin/After/GetWsm');
 Route::rule('aftercancel','admin/After/Cancel');
+Route::rule('createSaleAndCgdByAfter','admin/After/createSaleAndCgdByAfter');
 //Route::rule('aftersetwsm','admin/After/setWsm');
 
 Route::rule('processolist','admin/Proorder/list');