123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904 |
- <?php declare (strict_types=1);
- namespace app\command;
- use app\admin\model\ActionLog;
- use app\admin\model\GoodLog;
- use app\admin\model\GoodStockInfo;
- use app\admin\model\ProcessOrder;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Exception;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Db;
- //处理有赞订单数据
- class handleYzOrderData extends Command
- {
- private $cgd_data = [];//存储采购单的数据,目前是为了做供应商api推送而准备的
- protected function configure()
- {
- // 指令配置
- $this->setName('handle_yz_order_data')->setDescription('处理有赞订单数据');
- }
- //处理有赞订单数据
- protected function execute(Input $input, Output $output)
- {
- $limit = Cache::store('redis')->get("YzTimeLimit");
- if ($limit == 1) {
- return '';
- }
- $c_data = Cache::store('redis')->handler()->rpop(Config::get('app.handle_queue_key'));
- Cache::store('redis')->set("YzTimeLimit", 1, 60);
- if ($c_data) {
- $c_data = json_decode($c_data, true);
- Db::startTrans();
- try {
- // //判断价格是否符合系统售价
- // $platform_yz_info = Db::name('platform_youzan')
- // ->field('id,sale_price,platform_id')
- // ->where(['skuCode' => $c_data['skuCode'], 'is_del' => 0])
- // ->findOrEmpty();
- // if ($c_data['discount_price'] < $platform_yz_info['sale_price'] && $c_data['is_approved'] == '0') {
- // Db::rollback();
- // $this->callbackYz(['status' => 2, 'id' => $c_data['id'], 'reason' => '价格低于系统售价' . $platform_yz_info['sale_price']]);
- // Cache::store('redis')->set("YzTimeLimit",0);
- // return false;//停止执行后续代码
- // }
- $standing_book_data = [];
- // //先找用户确认信息
- // $extend_data = Db::name('order_import_from_c_extend')
- // ->where(['order_import_from_c_id' => $c_data['id'], 'is_del' => 0, 'type' => 2])
- // ->find();
- // if (empty($extend_data)) {
- // $extend_data = Db::name('order_import_from_c_extend')
- // ->where(['order_import_from_c_id' => $c_data['id'], 'is_del' => 0, 'type' => 1])
- // ->find();
- // }
- //
- // if (empty($extend_data)) throw new Exception('没有对应的解析数据');
- //复用sale::create()方法 -- start
- $orderCode = makeNo("QR");
- //企业客户
- $ct = Db::name('platform_youzan')
- ->alias('py')
- ->field('py.id,py.skuCode,py.spuCode,py.plat_code,co.id org1_id,co.name,py.sale_price,g.*,py.platform_id,p.platform_name')
- ->leftJoin('platform p', 'p.id=py.platform_id AND p.is_del=0')
- ->leftJoin('customer_org1 co', 'co.name=p.platform_name AND co.is_del=0')
- ->leftJoin('good g', 'g.spuCode=py.spuCode AND g.is_del=0')
- ->where('py.skuCode', $c_data['skuCode'])
- ->where('py.exam_status', 6)//exam_status==6已上线
- ->find();
- if ($ct) {
- $customer_code = Db::name('customer_info')
- ->where(['itemid' => $ct['org1_id'], 'companyName' => $ct['name'] . '客户', 'is_del' => 0])
- ->value('companyNo', '');
- if (!$customer_code) throw new Exception('在企业客户管理组织中找不到对应的企业');
- } else throw new Exception('未找到skuCode:' . $c_data['skuCode'] . '的上线商品数据');
- // $customer_code = $customer_code = Db::name('customer_info')
- // ->where(['itemid' => $ct['id'], 'companyName' => $ct['name'] . '客户', 'is_del' => 0])
- // ->value('companyNo', '');
- //
- // $customer = Db::name("customer_info")
- // ->where(["companyNo" => $customer_code])
- // ->find();
- // if ($customer == false) throw new Exception('未找到客户数据');
- $supplierNo = $ct['companyNo'];
- // $supplier = Db::name("business")
- // ->where(["companyNo" => $supplierNo])
- // ->find();
- // if ($supplier == false) throw new Exception('未找到平台供应商数据');
- $goodtype = 1;//1正常商品
- $sendtype = 1;//直接发货
- $platform_id = $ct['platform_id'];
- $platform_order = $c_data['tid'];
- $good_num = $c_data['num'];
- $arrtime = $paytime = $c_data['addtime'];
- // $paytime = $c_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 = 5;//有赞
- $spuCode = $ct['spuCode'];
- $skuCode = $ct['skuCode'];
- // $is_activity = empty($extend_data['activity_name']) ? 0 : 1;
- 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.usable_stock,a.wait_out_stock")
- // ->find();
- //
- // if ($stock == false || $stock['usable_stock'] < $good_num) throw new Exception('库存数量不足');
- $stock_num = Db::name("good_stock")
- ->alias("a")
- ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
- ->leftJoin("good_stock_info c", "a.id=c.stockid and c.balance_num>0")
- ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" => $supplierNo])
- ->sum('balance_num');
- if ($stock_num < $good_num) {
- throw new Exception('bn库存数量不足,库存数只有' . $stock_num);
- }
- $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('未找到spuCode:' . $spuCode . ',最小起订量为' . $good_num . '的相关成本价格');
- $origin_price = $origin['nake_total'];
- }
- //活动相关信息
- // $act = Db::name('activity_info')
- // ->alias('ai')
- // ->field('ai.id,ai.activity_stock,ai.moq_num,ai.activity_stock')
- // ->join('good_activity ga', 'ga.activity_code=ai.activity_code')
- // ->where([
- // 'ai.skuCode' => $skuCode,
- // 'ai.spuCode' => $spuCode,
- // 'ai.is_del' => 0,
- // 'ai.status' => 1,
- // 'ga.status' => 6,
- // 'ga.is_del' => 0,
- // 'ga.platform_code' => $platform_id,
- // 'ga.is_stock' => $is_stock
- // ])
- // ->where(function ($query) {
- // return $query->where('ga.start', '<=', date('Y-m-d H:i:s'))->where('ga.end', '>=', date('Y-m-d H:i:s'));
- // })
- // ->find();
- $is_activity = 0;
- $sale_price = $c_data['discount_price'];
- if (isset($c_data['coupon']) && is_discount($c_data['coupon'])) {
- //如果是泰行销的单子,要取原始价格(即优惠前的价格)
- $is_activity = 2;//txx活动标识
- $sale_price = $c_data['price'];
- }
- $act = check_activity((string)$spuCode, (string)$skuCode, (int)$platform_id, (int)$is_stock, (float)$sale_price, (int)$good_num);
- if (!empty($act)) {
- $is_activity = 1;
- $sale_price = $act['final_price'];//取活动的相关价格
- // if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
- // if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
- } else {
- if ($sale_price < $ct['sale_price'] && $c_data['is_approved'] == '0') {
- Db::rollback();
- $this->callbackYz(['status' => 2, 'id' => $c_data['id'], 'reason' => '价格低于系统售价' . $ct['sale_price']]);
- Cache::store('redis')->set("YzTimeLimit", 0);
- return false;//停止执行后续代码
- }
- }
- // $is_activity = (int)(!empty($act));
- // 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'];//不能改动价格
- // }
- // 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'];
- //
- // }
- // }
- $remark = $c_data['buyer_messages'];
- $user = Config::get('app.uid_platform_youzan.' . $ct['platform_id']);
- $rm = isset($user['id']) ? $user['id'] : 0;
- $ri = isset($user['nickname']) ? $user['nickname'] : 'youzan';
- if ($rm == 0) throw new Exception("[平台id:{$ct['platform_id']}][平台名称:{$ct['platform_name']}]未找到对应的平台账户");
- $supplier_temp_info = Db::name('supplier')
- ->field('id,person,personid')
- ->where('code', $ct['supplierNo'])
- ->findOrEmpty();
- $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" => $goodinfo['companyNo'],
- "is_del" => 0,
- "zxNo" => "",
- "platform_order" => $platform_order,
- "platform_id" => $platform_id,
- "remark" => $remark,
- "is_stock" => $is_stock,
- "is_activity" => $is_activity,
- 'activity_code' => $is_activity == 1 ? $act['activity_code'] : ($is_activity == 2 ? "txx" : ""),
- "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" => '',
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s"),
- 'total_price' => bcmul((string)$sale_price, (string)$good_num, 2),
- 'cgderid' => $supplier_temp_info['personid'],
- 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
- 'good_createrid' => $goodinfo['createrid'],
- 'good_creater' => $goodinfo['creater'],//商品创建人
- ];
- $paytime == "" ? "" : $data['paytime'] = $paytime;
- $datainfo = Db::name('sale')->insertGetId($data);
- 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('id', $act['id'])
- ->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' => $ct['supplierNo'],
- 'companyNo' => $ct['companyNo'],
- 'customer_code' => $data['customer_code'],
- ]);
- if ($is_stock == 0) {
- $cgd = [
- "supplierNo" => $ct['supplierNo'],
- "companyNo" => $supplierNo,
- "orderCode" => $orderCode,
- "spuCode" => $spuCode,
- "skuCode" => $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" => $supplier_temp_info['personid'],
- "creater" => $supplier_temp_info['person'],
- 'send_way' => 2,
- 'gold_price' => $ct['cgd_gold_price'],
- 'good_createrid' => $goodinfo['createrid'],
- 'good_creater' => $goodinfo['creater'],//商品创建人
- 'weight' => $ct['noble_weight'],//商品创建人
- ];
- //非库存品
- $bol = $this->createCgd($cgd, $rm, $ri, $standing_book_data);
- if ($bol !== true) throw new Exception($bol);
- } else {
- //库存品
- // $cgd = [
- // 'orderCode' => $orderCode,
- // "good_num" => $good_num,
- // "spuCode" => $spuCode,
- // "companyNo" => $supplierNo,
- // 'order_type' => $order_type,
- // 'order_source' => $order_source,
- // 'good_createrid' => $goodinfo['createrid'],
- // 'good_creater' => $goodinfo['creater'],//商品创建人
- // ];
- // $bol = $this->RelaCgd($cgd, $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('库存商品更新库存失败');
- // $stockinfo =GoodStockInfo::OrderBn($orderCode,$stock['id'],$good_num);
- // if($stockinfo==false){
- // Db::rollback();
- // return error_show(1002, "Bn库存更新库存失败");
- // }
- // //商品变动日志表,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");
- // }
- $stockid = 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])
- ->column('a.id');
- $stockinfo = GoodStockInfo::OrderBn($orderCode, $stockid, intval($good_num));
- if ($stockinfo == false) {
- throw new Exception('库存商品更新库存失败');
- }
- }
- if ($sendtype == 1) {
- // $p_c_a = get_address($c_data['delivery_province'] . $c_data['delivery_city'] . $c_data['delivery_district']);
- $province = Db::name('province')
- ->where('name', $c_data['delivery_province'])
- ->value('province_code', '');
- //地级市可能有重复的,例如'省直辖县'
- $city = Db::name('city')
- ->where('name', $c_data['delivery_city'])
- ->where('province_code', $province)
- ->value('city_code', '');
- //县区名称也有可能重复,例如'桥东区'
- $area = Db::name('area')
- ->where('name', $c_data['delivery_district'])
- ->where('city_code', $city)
- ->value('area_code', '');
- $addr_code = implode([$province, $city, $area], ',');
- $temp = [
- 'orderCode' => $orderCode,
- 'contactor' => $c_data['receiver_name'],
- 'mobile' => $c_data['receiver_tel'],
- 'addr' => $c_data['delivery_address'],
- 'addr_code' => $addr_code,
- 'customer_code' => $customer_code,
- 'receipt_quantity' => $c_data['num'],//收货数量,
- 'post_fee' => $c_data['post_fee'],
- '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')->insertGetId($temp);
- if ($vmp > 0) {
- $num = $c_data['num'];
- $outCode = makeNo("DF");
- if ($is_stock != 1) {
- $order = Db::name("order_num")
- ->where(["orderCode" => $orderCode, "status" => 1])
- ->where([["wsend_num", ">=", 0]])
- ->lock(true)
- ->find();
- if ($order == false) throw new Exception('未找到可以发货的采购单数据');
- $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('未匹配到编码为' . $order['cgdNo'] . '采购单数据');
- }
- $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" => $is_stock == 1 ? '' : $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")->insertGetId($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);
- if ($out['status'] == 1) {
- if ($is_stock == 1) {
- //库存品,待办数据推给库管
- $uids = Db::name('user_role')
- ->where(['is_del' => 0, 'roleid' => config('app.wsm_cgder_role'), 'status' => 1])
- ->column('uid');
- ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
- "order_type" => 'CKD',
- "order_code" => $outCode,//出库单号
- "order_id" => $ou,
- "order_status" => $out['status'],
- "before_status" => 0,
- 'holder_id' => $data['apply_id'],
- 'handle_user_list' => implode(',', $uids),
- ]);
- } else {
- // //非库存品推给供应商负责人
- $supp = Db::name('supplier')
- ->field('person,personid')
- ->where('code', $ct['supplierNo'])
- ->findOrEmpty();
- ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
- "order_type" => 'CKD',
- "order_code" => $outCode,//出库单号
- "order_id" => $ou,
- "order_status" => $out['status'],
- "before_status" => 0,
- 'holder_id' => $data['apply_id'],
- 'wait_id' => $supp['personid'],
- 'wait_name' => $supp['person'],
- ]);
- }
- } else ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
- "order_type" => 'CKD',
- "order_code" => $outCode,//出库单号
- "order_id" => $ou,
- "order_status" => $out['status'],
- "before_status" => 0,
- 'holder_id' => $data['apply_id']
- ]);
- // $standing_book_data['outCode'] = $outCode;
- }
- } else throw new Exception('发货地址添加失败');
- }
- }
- //复用sale::create()方法 -- end
- //处理完成
- // $order_import_from_c_db
- // ->where('id', $c_data['id'])
- // ->where('is_del', OIFCModel::$is_del_normal)
- // ->save([
- // 'status' => $order_import_from_c_db::$status_success,
- // 'updatetime' => date('Y-m-d H:i:s'),
- // 'remark' => '',
- // 'updateid' => 0,
- // 'updater' => 'system',
- // 'orderCode' => $orderCode
- // ]);
- //维护台账记录
- Db::name('standing_book')->insert(array_merge($standing_book_data, ['addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo("IO")]));
- //回调更新有赞订单数据
- $callback_rs = $this->callbackYz(['status' => 6, 'id' => $c_data['id'], 'orderCode' => $orderCode]);
- if ($callback_rs['code'] != 0) throw new Exception($callback_rs['message']);
- //将采购单数据塞入到队列中
- if ($this->cgd_data) {
- $push_data = json_encode([
- 'supplierNo' => $this->cgd_data['supplierNo'],
- 'type' => 1,//1销售订单(采销的采购单),2上线结果
- 'data' => [
- 'cgdNo' => $this->cgd_data['cgdNo'],
- 'spuCode' => $this->cgd_data['spuCode'],
- 'good_name' => $this->cgd_data['good_name'],
- 'good_num' => $this->cgd_data['good_num'],
- 'good_price' => $this->cgd_data['good_price'],
- 'total_fee' => $this->cgd_data['total_fee'],
- 'weight' => $this->cgd_data['weight'],
- 'addtime' => $this->cgd_data['addtime'],
- ],
- ], JSON_UNESCAPED_UNICODE);
- // Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
- }
- Db::commit();
- Cache::store('redis')->set("YzTimeLimit", 0);
- // $output->writeln(date('Y-m-d H:i:s') . '|处理成功,orderCode:' . $orderCode);
- } catch (\think\Exception $exception) {
- Db::rollback();
- $message[] = $exception->getMessage();
- //回调更新有赞订单数据
- $callback_rs = $this->callbackYz(['status' => 8, 'id' => $c_data['id'], 'reason' => $exception->getMessage()]);
- if ($callback_rs['code'] != 0) $message[] = $callback_rs['message'];
- Cache::store('redis')->set("YzTimeLimit", 0);
- $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . implode(',', $message));
- }
- }
- Cache::store('redis')->set("YzTimeLimit", 0, 60);
- //没有需要处理的数据
- }
- //创建采购单
- private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
- {
- $cgdCode = makeNo("CG");
- $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
- if ($supplier == false) return '未找到供应商';
- $wsm = Db::name("warehouse_info")
- ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "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" => 0,
- "contactor_name" => 0,
- "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) return '仓库数据创建失败';
- } else {
- $wsm_code = $wsm['wsm_code'];
- }
- //判断该供应商是否开通了供应商账号
- $from_tag = checkHasAccountBySupplierNos([$data['supplierNo']]);
- $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'],
- "delivery_fee" => $data['delivery_fee'],
- "weight" => $data['weight'],
- "diff_weight" => "0",
- "diff_fee" => "0",
- "gold_price" => $data['gold_price'],
- "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"),
- 'good_createrid' => $data['good_createrid'],
- 'good_creater' => $data['good_creater'],//商品创建人
- 'from_tag' => isset($from_tag[$data['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
- ];
- $up = Db::name("purchease_order")->insert($cg, true);
- if ($up) {
- //修改状态,添加待办
- ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
- "order_code" => $cg['cgdNo'],//销售单code
- "status" => 0,//这里的status是之前的值
- "action_remark" => '',//备注
- "action_type" => "create"//新建create,编辑edit,更改状态status
- ], "CGD", $cg['status'], $cg);
- //当采购单是节点0待与供应商确认,推给供应商负责人
- ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
- "order_type" => 'CGD',
- "order_code" => $cg['cgdNo'],//销售单code
- "order_id" => $up,
- "order_status" => $cg['status'],
- "before_status" => 0,
- 'holder_id' => $supplier['personid'],
- 'wait_id' => $supplier['personid'],
- 'wait_name' => $supplier['person'],
- ]);
- $standing_book_data['cgdNo'] = $cgdCode;
- $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
- ];
- $re = Db::name("order_num")->save($rela);
- if ($re == false) return '销售订单和采购单关联失败';
- else {
- $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) return '商品库存操作失败';
- $good_data=[];
- $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(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
- $this->cgd_data = $cg;
- return true;
- }
- } else return '创建采购单失败';
- }
- //创建.....
- private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
- {
- //备库单
- $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) return false;
- $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
- if ($cgdinfo == false) return false;
- //判断该供应商是否开通了供应商账号
- $from_tag = checkHasAccountBySupplierNos([$cgdinfo['supplierNo']]);
- $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"),
- 'good_createrid' => $outinfo['good_createrid'],
- 'good_creater' => $outinfo['good_creater'],//商品创建人
- 'from_tag' => isset($from_tag[$cgdinfo['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
- ];
- $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
- if ($insetrCgd == false) return false;
- $standing_book_data = array_merge($standing_book_data, [
- 'cgdNo' => $QrdCgd['cgdNo'],
- 'bk_code' => $QrdCgd['bkcode'],
- ]);
- $merge_num = Db::name("purchease_order")
- ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
- ->where('order_source', '<>', 0)//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) return false;
- $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) return false;
- $this->cgd_data = $QrdCgd;
- return true;
- }
- //与有赞项目回调
- private function callbackYz(array $data = [])
- {
- $host = Config::get('app.yz_domain');
- $response = curl_request($host . 'api/callback_order_by_stock', $data);
- return json_decode($response, true);
- }
- }
|