Prechádzať zdrojové kódy

Merge branch 'dev_wf' of wugg/phpstock into version1.5

wufeng 2 rokov pred
rodič
commit
7183d15bd2

+ 37 - 0
app/youzan/controller/Index.php

@@ -4,8 +4,10 @@ namespace app\youzan\controller;
 
 use app\admin\controller\Base;
 use app\youzan\logic\Cat;
+use app\youzan\logic\Order;
 use app\youzan\logic\Tag;
 use app\youzan\logic\Goodup;
+use think\facade\Db;
 use think\facade\Validate;
 
 //有赞对接类
@@ -113,5 +115,40 @@ class Index extends Base
 
     }
 
+    //获取有赞订单列表
+    public function getYzOrderList()
+    {
+
+        $param = $this->request->filter('trim')->only(['item_no' => '', 'handle_status' => '', 'addtime_start' => '', 'addtime_end' => '', 'confirmer' => '', 'orderCode' => '', 'page' => 1, 'size' => 15], 'post');
+
+        return Order::getYzOrderList($param);
+
+    }
+
+    //处理有赞订单(确认或取消)
+    public function checkHandleStatus()
+    {
+        $param = $this->request->filter('trim')->only(['token', 'good_info_id', 'handle_status'], 'post');
+
+        $val = Validate::rule([
+            'token' => 'require',
+            'good_info_id|订单id' => 'require|number|gt:0',
+            'handle_status|处理状态' => 'require|number|in:1,2',
+        ]);
+
+        if ($val->check($param)) return Order::checkHandleStatus($param);
+        else return error_show(1005, $val->getError());
+
+    }
+
+    //获取有赞平台修改商品日志
+    public function getYzUpdateLog(){
+
+        $param = $this->request->only(['page' => 1, 'size' => 15, 'msg_type' => 0], 'post', 'trim');
+
+        return curl_request(config('app.yz_domain') . 'api/yz_get_youzan_update_log', $param);
+
+    }
+
 
 }

+ 452 - 0
app/youzan/logic/Order.php

@@ -0,0 +1,452 @@
+<?php
+
+namespace app\youzan\logic;
+
+use app\admin\model\ActionLog;
+use app\admin\model\GoodLog;
+use app\admin\model\ProcessOrder;
+use app\youzan\model\PlatformYouzan;
+use think\Exception;
+use think\facade\Cache;
+use think\facade\Db;
+
+//有赞订单类
+class Order
+{
+
+    //获取有赞订单列表
+    public static function getYzOrderList(array $data = [])
+    {
+
+        return curl_request(config('app.yz_domain') . 'api/yz_order_list', $data);
+
+    }
+
+
+    //处理有赞订单(确认或取消)
+    public static function checkHandleStatus(array $data = [])
+    {
+        $user = GetUserInfo($data['token']);
+        $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
+        $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
+
+        Db::startTrans();
+
+        try {
+
+            //有赞项目中,订单信息
+            $rs = Db::connect('mysql_yz')
+                ->table('yz_order_info')
+                ->alias('oi')
+                ->field('oi.id,oi.handle_status,oi.item_no,oi.num,oi.discount_price,oi.addtime,oi.buyer_messages,oa.receiver_name,oa.receiver_tel,oa.delivery_province,oa.delivery_city,oa.delivery_district,oa.delivery_address')
+                ->leftJoin('order_address oa', 'oa.tid=oi.tid')
+                ->where('oi.id', $data['good_info_id'])
+                ->findOrEmpty();
+
+            if (empty($rs)) throw new Exception('该订单记录不存在');
+            if ($rs['handle_status'] != 0) throw new Exception('该订单不是待确认状态');
+
+
+            //确认
+            if ($data['handle_status'] == 2) {
+
+                //组织数据
+                $extend_data = Db::name('platform_youzan')
+                    ->alias('pyz')
+                    ->field('pyz.id,ci.companyNo customer_code,g.companyNo,pyz.platform_id,p.platform_code')
+                    ->leftJoin('good g', 'g.spuCode=pyz.spuCode')
+                    ->leftJoin('platform p', 'p.id=pyz.platform_id AND p.is_del=0')
+                    ->leftJoin('customer_org1 co', 'co.name=p.platform_name AND co.is_del=0')
+                    ->leftJoin('customer_info ci', 'ci.itemid=co.id AND ci.companyName=co.name AND ci.is_del=0')
+                    ->where('pyz.skuCode', $rs['item_no'])
+                    ->findOrEmpty();
+
+                if (empty($temp)) throw new Exception('该商品的上架记录不存在');
+
+                $extend_data = array_merge($extend_data, [
+                    'num' => $rs['num'],
+                    'platform_time' => $rs['addtime'],
+                    'addtime' => $rs['addtime'],
+                    'po_code' => '',
+                    'skuCode' => $rs['item_no'],
+                    'activity_name' => '',//???????????????????? @todo 有赞商品和采销系统活动待关联
+                    'price' => $rs['discount_price'],
+                    'activity_code' => '',//???????????????????? @todo 有赞商品和采销系统活动待关联
+                    'order_remark' => $rs['buyer_messages'],
+                    'contactor' => $rs['receiver_name'],
+                    'mobile' => $rs['receiver_tel'],
+                    'addr' => $rs['delivery_province'] . $rs['delivery_city'] . $rs['delivery_district'] . $rs['delivery_address'],
+                ]);
+
+                $province = Db::name('province')->whereLike('name', '%' . $rs['delivery_province'] . '%')->value('province_code');
+                $city = Db::name('city')->whereLike('name', '%' . $rs['delivery_city'] . '%')->value('city_code');
+                $area = Db::name('area')->whereLike('name', '%' . $rs['delivery_district'] . '%')->value('area_code');
+
+                $extend_data['addr_code'] = implode([$province, $city, $area], ',');
+
+                //生成销售单和采购单
+                $orderCode = self::createOrderAndPurch($extend_data);
+
+            }
+
+
+            //更新有赞项目相关信息
+            $update_rs = Db::connect('mysql_yz')
+                ->table('yz_order_info')
+                ->where('oi.id', $data['good_info_id'])
+                ->update([
+                    'handle_status' => $data['handle_status'],
+                    'orderCode' => $orderCode,
+                    'updateid' => $uid,
+                    'updater' => $uname,
+                    'updatetime' => date('Y-m-d H:i:s'),
+                ]);
+
+            if (!$update_rs) throw new Exception('修改失败');
+
+            Db::commit();
+
+            return app_show(0, '操作成功');
+
+        } catch (Exception $exception) {
+
+            Db::rollback();
+
+            return error_show(1005, $exception->getMessage());
+        }
+    }
+
+
+    //
+    private static function createOrderAndPurch(array $extend_data = [])
+    {
+        $orderCode = makeNo("QR");
+        $customer_code = $extend_data['customer_code'];
+
+        $customer = Db::name("customer_info")
+            ->where(["companyNo" => $customer_code])
+            ->find();
+
+        if ($customer == false) throw new Exception('未找到客户数据');
+
+        $supplierNo = $extend_data['companyNo'];
+        $supplier = Db::name("business")
+            ->where(["companyNo" => $supplierNo])
+            ->find();
+        if ($supplier == false) throw new Exception('未找到平台供应商数据');
+
+        $goodtype = 1;//1正常商品
+
+        $sendtype = 1;//直接发货
+        $platform_id = $extend_data['platform_id'];
+
+        $platform_order = $extend_data['platform_code'];
+        $good_num = $extend_data['num'];
+        $arrtime = $extend_data['platform_time'];
+        $paytime = $extend_data['addtime'];
+        $workNo = $extend_data['po_code'];
+        $ct = Db::name('good_platform')
+            ->alias('a')
+            ->join('good b', 'b.spuCode=a.spuCode', 'left')
+            ->where(['a.skuCode' => $extend_data['skuCode'], 'a.exam_status' => 6])//exam_status==6已上线
+            ->field("b.*,a.skuCode,a.spuCode,a.platform_code,a.plat_code")
+            ->find();
+        if ($ct == false) throw new Exception('未找到商品数据');
+
+        $goodinfo = $ct;
+        $is_stock = $ct['is_stock'];
+        $order_type = $is_stock == 1 ? 1 : 2;//1备库(库存品)2非库存品
+        $order_source = 4;//4平台
+        $spuCode = $ct['spuCode'];
+        $skuCode = $ct['skuCode'];
+        $is_activity = empty($extend_data['activity_name']) ? 0 : 1;
+
+        if ($goodinfo['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.usable_stock,a.wait_out_stock")
+                ->find();
+
+            if ($stock == false || $stock['usable_stock'] < $good_num) throw new Exception('库存数量不足');
+
+            $origin_price = 0;
+
+        } else {
+
+            $origin = Db::name("good_nake")
+                ->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])
+                ->order("min_num desc")
+                ->find();
+            if ($origin == false) throw new Exception('未找到相关成本价格');
+
+            $origin_price = $origin['nake_total'];
+
+        }
+
+        $sale_price = $extend_data['price'];
+        if ($goodtype == 1) {
+            $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) throw new Exception('未找到相关阶梯价格');
+
+            //$sale_price = $good['sale_price']; //不改动售价
+
+            //理论上不会出现实时金价的订单
+            if ($ct['is_gold_price'] == 1 && $is_stock != 1) {
+                $gold = Db::name("gold_price1")
+                    ->field('id,price')
+                    ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
+                    ->order("addtime desc")
+                    ->find();
+
+                //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
+                $gold_sale_price = $ct['demo_fee'] / $good_num + $ct['open_fee'] / $good_num + $ct['noble_weight'] * $gold["price"] + $good['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
+                if ($sale_price < $gold_sale_price) throw new Exception('价格不符合根据实时金价计算出的最终售价');
+
+                $ct['cgd_gold_price'] = $gold['price'];
+
+//                $order_rate = Db::name("cat")->where(["id" => $ct['cat_id']])->value('order_rate');
+//                $budget = isset($order_rate) ? $order_rate / 100 : 0;
+
+                // $saleprice = $total_fee / (1 - $budget);
+            }
+
+            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" => $extend_data['activity_code'], "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
+                    ->find();
+                if ($act == false) throw new Exception('未找到相关活动价');
+                if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
+                if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
+                //$sale_price = $act['activity_price'];//不能改动价格
+            }
+        }
+        $cgd = [
+            "supplierNo" => $ct['supplierNo'],
+            "companyNo" => $supplierNo,
+            "orderCode" => $orderCode,
+            "spuCode" => $ct['spuCode'],
+            "skuCode" => $ct['skuCode'],
+            "good_name" => $ct['good_name'],
+            "sale_price" => $origin_price,
+            "total_fee" => $origin_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,
+            "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
+            "demo_fee" => $ct['demo_fee'],
+            "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
+            "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
+            "good_num" => $good_num,
+            "good_type" => $goodtype,
+            "order_type" => $order_type,
+            "order_source" => $order_source,
+            "createrid" => $ct['createrid'],
+            "creater" => $ct['creater'],
+            'send_way' => 2,
+            'gold_price' => $ct['cgd_gold_price'],
+        ];
+
+        $send_num = $extend_data['num'];
+        $remark = $extend_data['order_remark'];
+        $rm = $c_data['createrid'];
+        $ri = $c_data['creater'];
+        $data = [
+            "orderCode" => $orderCode,
+            "good_code" => $spuCode,
+            "skuCode" => $skuCode,
+            "customer_code" => $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" => 1,
+            "good_type" => $goodtype,
+            "send_type" => $sendtype,
+            "supplierNo" => $extend_data['companyNo'],
+            "is_del" => 0,
+            "zxNo" => "",
+            "platform_order" => $platform_order,
+            "platform_id" => $platform_id,
+            "remark" => $remark,
+            "is_stock" => $is_stock,
+            "is_activity" => $is_activity === "" ? 0 : $is_activity,
+            'activity_code' => $extend_data['activity_code'],
+            "order_type" => $order_type,
+            "order_source" => $order_source,
+//                "poNo"=>$poNo,
+            'good_weight' => $ct['weight'],
+            'gold_price' => $ct['cgd_gold_price'],
+            'cost_price' => $ct['cost_fee'],
+            'diff_weight' => 0,
+            'diff_fee' => 0,
+            "workNo" => $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),
+        ];
+        $paytime == "" ? "" : $data['paytime'] = $paytime;
+        $datainfo = Db::name('sale')->insert($data, true);
+        if ($datainfo > 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" => $extend_data['activity_code'], "is_del" => 0, "status" => 1])
+                    ->save($actup);
+                if ($actupp == false) throw new Exception('活动库存修改失败');
+
+            }
+
+            //补充台账数据
+            $standing_book_data = array_merge($standing_book_data, [
+                'orderCode' => $orderCode,
+                'spuCode' => $data['good_code'],
+                'skuCode' => $data['skuCode'],
+                'order_type' => $data['order_type'],
+                'order_source' => $data['order_source'],
+                'supplierNo' => $extend_data['supplierNo'],
+                'companyNo' => $extend_data['companyNo'],
+                'customer_code' => $data['customer_code'],
+            ]);
+            if ($is_stock == 0) {
+
+                //非库存品
+                $bol = $this->createCgd($cgd, $rm, $ri, $standing_book_data);
+                if ($bol == false) throw new Exception('订单创建失败');
+
+            } else {
+                //库存品
+                $bol = $this->RelaCgd(['orderCode' => $orderCode, "good_num" => $good_num, "spuCode" => $spuCode, "companyNo" => $supplierNo, 'order_type' => $order_type, 'order_source' => $order_source], $standing_book_data);
+                if ($bol == false) throw new Exception('库存商品关联采购单失败');
+
+                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" => $datainfo, "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
+                    $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
+                    GoodLog::LogAdd(['id' => $rm, 'nickname' => $ri], $good_data, "XSQRD");
+                }
+
+            }
+            if ($sendtype == 1) {
+                $temp = [
+                    'orderCode' => $orderCode,
+                    'contactor' => $extend_data['contactor'],
+                    'mobile' => $extend_data['mobile'],
+                    'addr' => $extend_data['addr'],
+                    'addr_code' => $extend_data['addr_code'],
+                    'customer_code' => $customer_code,
+                    'receipt_quantity' => $extend_data['num'],//收货数量,
+                    'post_fee' => 0,
+                    'is_del' => 0,
+                    'addtime' => date("Y-m-d H:i:s"),
+                    'updatetime' => date("Y-m-d H:i:s"),
+                    'arrive_time' => $arrtime,
+                ];
+                $vmp = Db::name('order_addr')->insert($temp, true);
+                if ($vmp > 0) {
+                    $order = Db::name("order_num")
+                        ->where(["orderCode" => $orderCode, "status" => 1])
+                        ->where([["wsend_num", ">=", 0]])
+                        ->lock(true)
+                        ->find();
+                    if ($order == false) throw new Exception('未找到可以发货得采购单数据');
+
+
+                    $num = $extend_data['num'];
+                    $outCode = makeNo("DF");
+                    $order['wsend_num'] -= $num;
+                    $order['send_num'] += $num;
+                    $or = Db::name("order_num")->save($order);
+                    if ($or == false) throw new Exception('发货地址更新失败');
+
+                    $tep = [
+                        "cgdNo" => $order['cgdNo'],
+                        "outCode" => $outCode,
+                        "send_num" => $num,
+                        "status" => 1,
+                        "addtime" => date("Y-m-d H:i:s"),
+                        "updatetime" => date("Y-m-d H:i:s")
+                    ];
+                    $sen = Db::name("order_send")->save($tep);
+                    if ($sen == false) throw new Exception('发货地址添加创建失败');
+
+                    $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
+                    if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
+
+                    $out = [
+                        "orderCode" => $orderCode,
+                        "outCode" => $outCode,
+                        "apply_id" => $rm,
+                        "apply_name" => $ri,
+                        "addrid" => $vmp,
+                        "post_name" => "",
+                        "post_code" => "",
+                        "post_fee" => 0,
+                        "sendtime" => date("Y-m-d H:i:s"),
+                        "send_num" => $num,
+                        "check_num" => 0,
+                        "error_num" => 0,
+                        "wsm_code" => $cgdinfo['wsm_code'],
+                        "order_type" => $order_type,
+                        "status" => $is_stock == 1 ? 1 : 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(['id' => $rm, 'nickname' => $ri], [
+                            "order_code" => $outCode,//出库单号
+                            "status" => 0,//这里的status是之前的值
+                            "action_remark" => '',//备注
+                            "action_type" => "create"//新建create,编辑edit,更改状态status
+                        ], "CKD", 0, $out);
+
+                        ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
+                            "order_type" => 'CKD',
+                            "order_code" => $outCode,//出库单号
+                            "order_id" => 0,
+                            "order_status" => 0,
+                            "before_status" => 0
+                        ]);
+
+                        $standing_book_data['outCode'] = $outCode;
+                    }
+                } else throw new Exception('发货地址添加失败');
+            }
+        }
+
+        //复用sale::create()方法 -- end
+    }
+
+
+}

+ 8 - 0
app/youzan/route/app.php

@@ -11,3 +11,11 @@ route::rule('yz_goodup_again', 'youzan/Index/goodupOnlineAgain');//商品再次
 route::rule('yz_goodoff', 'youzan/Index/youzanOffline');//将商品从有赞平台下线
 route::rule('yz_goodoff_list', 'youzan/Index/youzanOfflineList');//有赞下线商品列表
 route::rule('yz_goodoff_detail', 'youzan/Index/youzanOfflineDetail');//有赞下线商品详情
+route::rule('yz_order_list', 'youzan/Index/getYzOrderList');//获取有赞订单列表
+route::rule('yz_check_handle_status', 'youzan/Index/checkHandleStatus');//处理有赞订单(确认或取消)
+route::rule('yz_get_youzan_update_log','youzan/Index/getYzUpdateLog');//获取有赞平台修改商品日志
+
+
+
+
+