handleYzOrderData.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <?php declare (strict_types=1);
  2. namespace app\command;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\Output;
  9. use think\Exception;
  10. use think\facade\Cache;
  11. use think\facade\Config;
  12. use think\facade\Db;
  13. //处理有赞订单数据
  14. class handleYzOrderData extends Command
  15. {
  16. private $cgd_data = [];//存储采购单的数据,目前是为了做供应商api推送而准备的
  17. protected function configure()
  18. {
  19. // 指令配置
  20. $this->setName('handle_yz_order_data')->setDescription('处理有赞订单数据');
  21. }
  22. //处理有赞订单数据
  23. protected function execute(Input $input, Output $output)
  24. {
  25. $c_data = Cache::store('redis')->handler()->rpop(Config::get('app.handle_queue_key'));
  26. if ($c_data){
  27. $c_data = json_decode($c_data, true);
  28. Db::startTrans();
  29. try {
  30. //判断价格是否符合系统售价
  31. $platform_yz_info = Db::name('platform_youzan')
  32. ->field('id,sale_price,platform_id')
  33. ->where(['skuCode' => $c_data['skuCode'], 'is_del' => 0])
  34. ->findOrEmpty();
  35. if ($c_data['discount_price'] < $platform_yz_info['sale_price'] && $c_data['is_approved'] == '0') {
  36. Db::rollback();
  37. $this->callbackYz(['status' => 2, 'id' => $c_data['id'], 'reason' => '价格低于系统售价' . $platform_yz_info['sale_price']]);
  38. return false;//停止执行后续代码
  39. }
  40. $standing_book_data = [];
  41. // //先找用户确认信息
  42. // $extend_data = Db::name('order_import_from_c_extend')
  43. // ->where(['order_import_from_c_id' => $c_data['id'], 'is_del' => 0, 'type' => 2])
  44. // ->find();
  45. // if (empty($extend_data)) {
  46. // $extend_data = Db::name('order_import_from_c_extend')
  47. // ->where(['order_import_from_c_id' => $c_data['id'], 'is_del' => 0, 'type' => 1])
  48. // ->find();
  49. // }
  50. //
  51. // if (empty($extend_data)) throw new Exception('没有对应的解析数据');
  52. //复用sale::create()方法 -- start
  53. $orderCode = makeNo("QR");
  54. //企业客户
  55. $ct = Db::name('platform_youzan')
  56. ->alias('py')
  57. ->field('py.id,py.skuCode,py.spuCode,py.plat_code,co.id org1_id,co.name,py.sale_price,g.*,py.platform_id')
  58. ->leftJoin('platform p', 'p.id=py.platform_id AND p.is_del=0')
  59. ->leftJoin('customer_org1 co', 'co.name=p.platform_name AND co.is_del=0')
  60. ->leftJoin('good g', 'g.spuCode=py.spuCode AND g.is_del=0')
  61. ->where('py.skuCode', $c_data['skuCode'])
  62. ->where('py.exam_status', 6)//exam_status==6已上线
  63. ->find();
  64. if ($ct) {
  65. $customer_code = Db::name('customer_info')
  66. ->where(['itemid' => $ct['org1_id'], 'companyName' => $ct['name'] . '客户', 'is_del' => 0])
  67. ->value('companyNo', '');
  68. if (!$customer_code) throw new Exception('在企业客户管理组织中找不到对应的企业');
  69. } else throw new Exception('未找到商品数据');
  70. // $customer_code = $customer_code = Db::name('customer_info')
  71. // ->where(['itemid' => $ct['id'], 'companyName' => $ct['name'] . '客户', 'is_del' => 0])
  72. // ->value('companyNo', '');
  73. //
  74. // $customer = Db::name("customer_info")
  75. // ->where(["companyNo" => $customer_code])
  76. // ->find();
  77. // if ($customer == false) throw new Exception('未找到客户数据');
  78. $supplierNo = $ct['companyNo'];
  79. // $supplier = Db::name("business")
  80. // ->where(["companyNo" => $supplierNo])
  81. // ->find();
  82. // if ($supplier == false) throw new Exception('未找到平台供应商数据');
  83. $goodtype = 1;//1正常商品
  84. $sendtype = 1;//直接发货
  85. $platform_id = $ct['platform_id'];
  86. $platform_order = $c_data['tid'];
  87. $good_num = $c_data['num'];
  88. $arrtime = $paytime = $c_data['addtime'];
  89. // $paytime = $c_data['addtime'];
  90. // $workNo = $extend_data['po_code'];
  91. // $ct = Db::name('good_platform')
  92. // ->alias('a')
  93. // ->join('good b', 'b.spuCode=a.spuCode', 'left')
  94. // ->where(['a.skuCode' => $extend_data['skuCode'], 'a.exam_status' => 6])//exam_status==6已上线
  95. // ->field("b.*,a.skuCode,a.spuCode,a.platform_code,a.plat_code")
  96. // ->find();
  97. // if ($ct == false) throw new Exception('未找到商品数据');
  98. $goodinfo = $ct;
  99. $is_stock = $ct['is_stock'];
  100. $order_type = $is_stock == 1 ? 1 : 2;//1备库(库存品)2非库存品
  101. $order_source = 5;//有赞
  102. $spuCode = $ct['spuCode'];
  103. $skuCode = $ct['skuCode'];
  104. // $is_activity = empty($extend_data['activity_name']) ? 0 : 1;
  105. if ($is_stock == 1) {
  106. $stock = Db::name("good_stock")
  107. ->alias("a")
  108. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  109. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, 'b.wsm_type' => 5, "b.companyNo" => $supplierNo])
  110. ->field("a.id,a.usable_stock,a.wait_out_stock")
  111. ->find();
  112. if ($stock == false || $stock['usable_stock'] < $good_num) throw new Exception('库存数量不足');
  113. $origin_price = 0;
  114. } else {
  115. $origin = Db::name("good_nake")
  116. ->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])
  117. ->order("min_num desc")
  118. ->find();
  119. if ($origin == false) throw new Exception('未找到相关成本价格');
  120. $origin_price = $origin['nake_total'];
  121. }
  122. //活动相关信息
  123. $act = Db::name('activity_info')
  124. ->alias('ai')
  125. ->field('ai.id,ai.activity_stock,ai.moq_num,ai.activity_stock')
  126. ->join('good_activity ga', 'ga.activity_code=ai.activity_code')
  127. ->where([
  128. 'ai.skuCode' => $skuCode,
  129. 'ai.spuCode' => $spuCode,
  130. 'ai.is_del' => 0,
  131. 'ai.status' => 1,
  132. 'ga.status' => 6,
  133. 'ga.is_del' => 0,
  134. 'ga.platform_code' => $platform_id,
  135. 'ga.is_stock' => $is_stock
  136. ])
  137. ->where(function ($query) {
  138. return $query->where('ga.start', '<=', date('Y-m-d H:i:s'))->where('ga.end', '>=', date('Y-m-d H:i:s'));
  139. })
  140. ->find();
  141. $is_activity = 0;
  142. if (!empty($act)) {
  143. $is_activity = 1;
  144. if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
  145. if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
  146. }
  147. // $is_activity = (int)(!empty($act));
  148. // if ($is_activity == 1) {
  149. // $act = Db::name("activity_info")
  150. // ->alias("a")
  151. // ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  152. // ->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])
  153. // ->find();
  154. // if ($act == false) throw new Exception('未找到相关活动价');
  155. // if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
  156. // if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
  157. //$sale_price = $act['activity_price'];//不能改动价格
  158. // }
  159. $sale_price = $c_data['discount_price'];
  160. // if ($goodtype == 1) {
  161. // $good = Db::name("good_ladder")
  162. // ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
  163. // ->where([["min_num", "<=", $good_num]])
  164. // ->order("min_num desc")
  165. // ->find();
  166. // if ($good == false) throw new Exception('未找到相关阶梯价格');
  167. //$sale_price = $good['sale_price']; //不改动售价
  168. //理论上不会出现实时金价的订单
  169. // if ($ct['is_gold_price'] == 1 && $is_stock != 1) {
  170. // $gold = Db::name("gold_price1")
  171. // ->field('id,price')
  172. // ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  173. // ->order("addtime desc")
  174. // ->find();
  175. //
  176. // //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  177. // $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'];
  178. // if ($sale_price < $gold_sale_price) throw new Exception('价格不符合根据实时金价计算出的最终售价');
  179. //
  180. // $ct['cgd_gold_price'] = $gold['price'];
  181. //
  182. // }
  183. // }
  184. $remark = $c_data['buyer_messages'];
  185. $user = Config::get('app.uid_platform_youzan.' . $platform_yz_info['platform_id']);
  186. $rm = isset($user['id']) ? $user['id'] : 0;
  187. $ri = isset($user['nickname']) ? $user['nickname'] : 'youzan';
  188. $supplier_temp_info = Db::name('supplier')
  189. ->field('id,person,personid')
  190. ->where('code', $ct['supplierNo'])
  191. ->findOrEmpty();
  192. $data = [
  193. "orderCode" => $orderCode,
  194. "good_code" => $spuCode,
  195. "skuCode" => $skuCode,
  196. "customer_code" => $customer_code,
  197. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  198. "good_num" => $good_num,
  199. "cat_id" => $goodinfo['cat_id'],
  200. "apply_id" => $rm,
  201. "apply_name" => $ri,
  202. "origin_price" => $origin_price,
  203. "sale_price" => $sale_price,
  204. "post_fee" => 0,
  205. "status" => 0,
  206. "send_num" => 0,
  207. "wsend_num" => $good_num,
  208. "send_status" => 1,
  209. "good_type" => $goodtype,
  210. "send_type" => $sendtype,
  211. "supplierNo" => $goodinfo['companyNo'],
  212. "is_del" => 0,
  213. "zxNo" => "",
  214. "platform_order" => $platform_order,
  215. "platform_id" => $platform_id,
  216. "remark" => $remark,
  217. "is_stock" => $is_stock,
  218. "is_activity" => $is_activity,
  219. 'activity_code' => $is_activity ? $act['activity_code'] : '',
  220. "order_type" => $order_type,
  221. "order_source" => $order_source,
  222. // "poNo"=>$poNo,
  223. 'good_weight' => $ct['weight'],
  224. 'gold_price' => $ct['cgd_gold_price'],
  225. 'cost_price' => $ct['cost_fee'],
  226. 'diff_weight' => 0,
  227. 'diff_fee' => 0,
  228. "workNo" => '',
  229. "addtime" => date("Y-m-d H:i:s"),
  230. "updatetime" => date("Y-m-d H:i:s"),
  231. 'total_price' => bcmul((string)$sale_price, (string)$good_num, 2),
  232. 'cgderid' => $supplier_temp_info['personid'],
  233. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  234. 'good_createrid' => $goodinfo['createrid'],
  235. 'good_creater' => $goodinfo['creater'],//商品创建人
  236. ];
  237. $paytime == "" ? "" : $data['paytime'] = $paytime;
  238. $datainfo = Db::name('sale')->insertGetId($data);
  239. if ($datainfo > 0) {
  240. if ($is_activity == 1) {
  241. $actup = [
  242. "activity_stock" => $act['activity_stock'] - $good_num,
  243. "updatetime" => date("Y-m-d H:i:s")
  244. ];
  245. $actupp = Db::name("activity_info")
  246. ->where('id', $act['id'])
  247. ->save($actup);
  248. if ($actupp == false) throw new Exception('活动库存修改失败');
  249. }
  250. //补充台账数据
  251. $standing_book_data = array_merge($standing_book_data, [
  252. 'orderCode' => $orderCode,
  253. 'spuCode' => $data['good_code'],
  254. 'skuCode' => $data['skuCode'],
  255. 'order_type' => $data['order_type'],
  256. 'order_source' => $data['order_source'],
  257. 'supplierNo' => $data['supplierNo'],
  258. 'companyNo' => $ct['companyNo'],
  259. 'customer_code' => $data['customer_code'],
  260. ]);
  261. if ($is_stock == 0) {
  262. $cgd = [
  263. "supplierNo" => $ct['supplierNo'],
  264. "companyNo" => $supplierNo,
  265. "orderCode" => $orderCode,
  266. "spuCode" => $spuCode,
  267. "skuCode" => $skuCode,
  268. "good_name" => $ct['good_name'],
  269. "sale_price" => $origin_price,
  270. "total_fee" => $origin_price * $good_num,
  271. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  272. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  273. "open_fee" => $ct['open_fee'],
  274. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  275. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  276. "demo_fee" => $ct['demo_fee'],
  277. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  278. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  279. "good_num" => $good_num,
  280. "good_type" => $goodtype,
  281. "order_type" => $order_type,
  282. "order_source" => $order_source,
  283. "createrid" => $ct['createrid'],
  284. "creater" => $ct['creater'],
  285. 'send_way' => 2,
  286. 'gold_price' => $ct['cgd_gold_price'],
  287. 'good_createrid' => $goodinfo['createrid'],
  288. 'good_creater' => $goodinfo['creater'],//商品创建人
  289. 'weight' => $ct['noble_weight'],//商品创建人
  290. ];
  291. //非库存品
  292. $bol = $this->createCgd($cgd, $rm, $ri, $standing_book_data);
  293. if ($bol == false) throw new Exception('订单创建失败');
  294. } else {
  295. //库存品
  296. // $cgd = [
  297. // 'orderCode' => $orderCode,
  298. // "good_num" => $good_num,
  299. // "spuCode" => $spuCode,
  300. // "companyNo" => $supplierNo,
  301. // 'order_type' => $order_type,
  302. // 'order_source' => $order_source,
  303. // 'good_createrid' => $goodinfo['createrid'],
  304. // 'good_creater' => $goodinfo['creater'],//商品创建人
  305. // ];
  306. // $bol = $this->RelaCgd($cgd, $standing_book_data);
  307. // if ($bol == false) throw new Exception('库存商品关联采购单失败');
  308. if (isset($stock)) {
  309. $stck = [
  310. "usable_stock" => $stock['usable_stock'] - $good_num,
  311. "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  312. "updatetime" => date("Y-m-d H:i:s")
  313. ];
  314. $upad = Db::name("good_stock")
  315. ->where($stock)
  316. ->update($stck);
  317. if ($upad == false) throw new Exception('库存商品更新库存失败');
  318. $stockinfo =GoodStockInfo::OrderBn($orderCode,$stock['id'],$good_num);
  319. if($stockinfo==false){
  320. Db::rollback();
  321. return error_show(1002, "Bn库存更新库存失败");
  322. }
  323. //商品变动日志表,good_log_code字段存储采购单号
  324. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  325. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  326. GoodLog::LogAdd(['id' => $rm, 'nickname' => $ri], $good_data, "XSQRD");
  327. }
  328. }
  329. if ($sendtype == 1) {
  330. $p_c_a = get_address($c_data['delivery_province'] . $c_data['delivery_city'] . $c_data['delivery_district']);
  331. $province = empty($p_c_a['province']) ? $p_c_a['province'] : Db::name('province')->whereLike('name', '%' . $p_c_a['province'] . '%')->value('province_code');
  332. $city = empty($p_c_a['city']) ? $p_c_a['city'] : Db::name('city')->whereLike('name', '%' . $p_c_a['city'] . '%')->value('city_code');
  333. $area = empty($p_c_a['district']) ? $p_c_a['district'] : Db::name('area')->whereLike('name', '%' . $p_c_a['district'] . '%')->value('area_code');
  334. $addr_code = implode([$province, $city, $area], ',');
  335. $temp = [
  336. 'orderCode' => $orderCode,
  337. 'contactor' => $c_data['receiver_name'],
  338. 'mobile' => $c_data['receiver_tel'],
  339. 'addr' => $c_data['delivery_address'],
  340. 'addr_code' => $addr_code,
  341. 'customer_code' => $customer_code,
  342. 'receipt_quantity' => $c_data['num'],//收货数量,
  343. 'post_fee' => $c_data['post_fee'],
  344. 'is_del' => 0,
  345. 'addtime' => date("Y-m-d H:i:s"),
  346. 'updatetime' => date("Y-m-d H:i:s"),
  347. 'arrive_time' => $arrtime,
  348. ];
  349. $vmp = Db::name('order_addr')->insertGetId($temp);
  350. if ($vmp > 0) {
  351. $order = Db::name("order_num")
  352. ->where(["orderCode" => $orderCode, "status" => 1])
  353. ->where([["wsend_num", ">=", 0]])
  354. ->lock(true)
  355. ->find();
  356. if ($order == false) throw new Exception('未找到可以发货得采购单数据');
  357. $num = $c_data['num'];
  358. $outCode = makeNo("DF");
  359. $order['wsend_num'] -= $num;
  360. $order['send_num'] += $num;
  361. $or = Db::name("order_num")->save($order);
  362. if ($or == false) throw new Exception('发货地址更新失败');
  363. $tep = [
  364. "cgdNo" => $order['cgdNo'],
  365. "outCode" => $outCode,
  366. "send_num" => $num,
  367. "status" => 1,
  368. "addtime" => date("Y-m-d H:i:s"),
  369. "updatetime" => date("Y-m-d H:i:s")
  370. ];
  371. $sen = Db::name("order_send")->save($tep);
  372. if ($sen == false) throw new Exception('发货地址添加创建失败');
  373. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  374. if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
  375. $out = [
  376. "orderCode" => $orderCode,
  377. "outCode" => $outCode,
  378. "apply_id" => $rm,
  379. "apply_name" => $ri,
  380. "addrid" => $vmp,
  381. "post_name" => "",
  382. "post_code" => "",
  383. "post_fee" => 0,
  384. "sendtime" => date("Y-m-d H:i:s"),
  385. "send_num" => $num,
  386. "check_num" => 0,
  387. "error_num" => 0,
  388. "wsm_code" => $cgdinfo['wsm_code'],
  389. "order_type" => $order_type,
  390. "status" => $is_stock == 1 ? 1 : 0,
  391. "addtime" => date("Y-m-d H:i:s"),
  392. "updatetime" => date("Y-m-d H:i:s")
  393. ];
  394. $ou = Db::name("order_out")->insert($out);
  395. if ($ou == false) throw new Exception('发货地址添加创建失败');
  396. else {
  397. //修改状态,添加待办
  398. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  399. "order_code" => $outCode,//出库单号
  400. "status" => 0,//这里的status是之前的值
  401. "action_remark" => '',//备注
  402. "action_type" => "create"//新建create,编辑edit,更改状态status
  403. ], "CKD", 0, $out);
  404. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  405. "order_type" => 'CKD',
  406. "order_code" => $outCode,//出库单号
  407. "order_id" => 0,
  408. "order_status" => 0,
  409. "before_status" => 0
  410. ]);
  411. // $standing_book_data['outCode'] = $outCode;
  412. }
  413. } else throw new Exception('发货地址添加失败');
  414. }
  415. }
  416. //复用sale::create()方法 -- end
  417. //处理完成
  418. // $order_import_from_c_db
  419. // ->where('id', $c_data['id'])
  420. // ->where('is_del', OIFCModel::$is_del_normal)
  421. // ->save([
  422. // 'status' => $order_import_from_c_db::$status_success,
  423. // 'updatetime' => date('Y-m-d H:i:s'),
  424. // 'remark' => '',
  425. // 'updateid' => 0,
  426. // 'updater' => 'system',
  427. // 'orderCode' => $orderCode
  428. // ]);
  429. //维护台账记录
  430. 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")]));
  431. //回调更新有赞订单数据
  432. $callback_rs = $this->callbackYz(['status' => 6, 'id' => $c_data['id'], 'orderCode' => $orderCode]);
  433. if ($callback_rs['code'] != 0) throw new Exception($callback_rs['message']);
  434. //将采购单数据塞入到队列中
  435. if ($this->cgd_data) {
  436. $push_data = json_encode([
  437. 'supplierNo' => $this->cgd_data['supplierNo'],
  438. 'type' => 1,//1销售订单(采销的采购单),2上线结果
  439. 'data' => [
  440. 'cgdNo' => $this->cgd_data['cgdNo'],
  441. 'spuCode' => $this->cgd_data['spuCode'],
  442. 'good_name' => $this->cgd_data['good_name'],
  443. 'good_num' => $this->cgd_data['good_num'],
  444. 'good_price' => $this->cgd_data['good_price'],
  445. 'total_fee' => $this->cgd_data['total_fee'],
  446. 'weight' => $this->cgd_data['weight'],
  447. 'addtime' => $this->cgd_data['addtime'],
  448. ],
  449. ], JSON_UNESCAPED_UNICODE);
  450. Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
  451. }
  452. Db::commit();
  453. $output->writeln(date('Y-m-d H:i:s') . '|处理成功,orderCode:' . $orderCode);
  454. } catch (\think\Exception $exception) {
  455. Db::rollback();
  456. $message[] = $exception->getMessage();
  457. //回调更新有赞订单数据
  458. $callback_rs = $this->callbackYz(['status' => 8, 'id' => $c_data['id'], 'reason' => $exception->getMessage()]);
  459. if ($callback_rs['code'] != 0) $message[] = $callback_rs['message'];
  460. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . implode(',', $message));
  461. }
  462. }
  463. //没有需要处理的数据
  464. }
  465. //创建采购单
  466. private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
  467. {
  468. $cgdCode = makeNo("CG");
  469. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  470. if ($supplier == false) return false;
  471. $wsm = Db::name("warehouse_info")
  472. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  473. ->find();
  474. if ($wsm == false) {
  475. $wsm_code = makeNo("WSM");
  476. $inwsm = [
  477. "wsm_code" => $wsm_code,
  478. "name" => $supplier['name'],
  479. "wsm_type" => 2,
  480. "supplierNo" => $supplier['code'],
  481. "addr" => "",
  482. "addrs_code" => "",
  483. "contactor" => 0,
  484. "contactor_name" => 0,
  485. "mobile" => "",
  486. "position" => "",
  487. "companyNo" => $data['companyNo'],
  488. "status" => 1,
  489. "is_del" => 0,
  490. "addtime" => date("Y-m-d H:i:s"),
  491. "updatetime" => date("Y-m-d H:i:s")
  492. ];
  493. $in = Db::name("warehouse_info")->insert($inwsm);
  494. if ($in == false) return false;
  495. } else {
  496. $wsm_code = $wsm['wsm_code'];
  497. }
  498. $cg = [
  499. "cgdNo" => $cgdCode,
  500. "bkcode" => "",
  501. "wsm_code" => $wsm_code,
  502. "cgder" => $data['creater'],
  503. "cgder_id" => $data['createrid'],
  504. "spuCode" => $data['spuCode'],
  505. "skuCode" => $data['skuCode'],
  506. "good_name" => $data['good_name'],
  507. "good_num" => $data['good_num'],
  508. "good_price" => $data['sale_price'],
  509. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  510. "pakge_fee" => $data['pakge_fee'],
  511. "cert_fee" => $data['cert_fee'],
  512. "open_fee" => $data['open_fee'],
  513. "teach_fee" => $data['cost_fee'],
  514. "mark_fee" => $data['mark_fee'],
  515. "demo_fee" => $data['demo_fee'],
  516. "nake_fee" => $data['nake_fee'],
  517. "delivery_fee" => $data['delivery_fee'],
  518. "weight"=>$data['weight'],
  519. "diff_weight" => "0",
  520. "diff_fee" => "0",
  521. "gold_price" => $data['gold_price'],
  522. "supplierNo" => $data['supplierNo'],
  523. "supplier_name" => $supplier['name'],
  524. "companyNo" => $data['companyNo'],
  525. "send_status" => 1,
  526. "send_num" => 0,
  527. "wsend_num" => $data['good_num'],
  528. "remark" => "",
  529. "status" => 0,//0初始化
  530. "lasttime" => date("Y-m-d H:i:s"),
  531. "is_del" => 0,
  532. "order_type" => $data['order_type'],
  533. "order_source" => $data['order_source'],
  534. "good_type" => $data['good_type'],
  535. "addtime" => date("Y-m-d H:i:s"),
  536. "updatetime" => date("Y-m-d H:i:s"),
  537. 'good_createrid' => $data['good_createrid'],
  538. 'good_creater' => $data['good_creater'],//商品创建人
  539. ];
  540. $up = Db::name("purchease_order")->insert($cg, true);
  541. if ($up) {
  542. //修改状态,添加待办
  543. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  544. "order_code" => $cg['cgdNo'],//销售单code
  545. "status" => 0,//这里的status是之前的值
  546. "action_remark" => '',//备注
  547. "action_type" => "create"//新建create,编辑edit,更改状态status
  548. ], "CGD", $cg['status'], $cg);
  549. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  550. "order_type" => 'CGD',
  551. "order_code" => $cg['cgdNo'],//销售单code
  552. "order_id" => $up,
  553. "order_status" => $cg['status'],
  554. "before_status" => 0
  555. ]);
  556. $standing_book_data['cgdNo'] = $cgdCode;
  557. $rela = [
  558. "orderCode" => $data['orderCode'],
  559. "cgdNo" => $cgdCode,
  560. "spuCode" => $data['spuCode'],
  561. "good_num" => $data['good_num'],
  562. "wsend_num" => $data['good_num'],
  563. "send_num" => 0,
  564. "wait_num" => 0,
  565. "status" => 1,
  566. "source" => 2
  567. ];
  568. $re = Db::name("order_num")->save($rela);
  569. if ($re == false) return false;
  570. else {
  571. $stokc = Db::name("good_stock")
  572. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  573. ->find();
  574. if ($stokc == false) {
  575. $stokc = [
  576. "spuCode" => $data['spuCode'],
  577. "wsm_code" => $wsm_code,
  578. "wait_in_stock" => $data['good_num'],
  579. "wait_out_stock" => 0,
  580. "usable_stock" => 0,
  581. "intra_stock" => 0,
  582. "total_stock" => 0,
  583. "status" => 1,
  584. "addtime" => date("Y-m-d H:i:s"),
  585. "updatetime" => date("Y-m-d H:i:s")
  586. ];
  587. } else {
  588. $stokc['wait_in_stock'] += $data['good_num'];
  589. $stokc['updatetime'] = date("Y-m-d H:i:s");
  590. }
  591. $stoc = Db::name("good_stock")->save($stokc);
  592. if ($stoc == false) return false;
  593. $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"];
  594. GoodLog::LogAdd(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
  595. $this->cgd_data = $cg;
  596. return true;
  597. }
  598. } else return false;
  599. }
  600. //创建.....
  601. private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
  602. {
  603. //备库单
  604. $cgd = Db::name("order_bk")
  605. ->where([
  606. ["spuCode", "=", $outinfo['spuCode']],
  607. ["is_del", "=", 0],
  608. ["balance_num", ">=", $outinfo['good_num']],
  609. ['companyNo', "=", $outinfo['companyNo']]
  610. ])
  611. ->lock(true)
  612. ->find();
  613. if ($cgd == false) return false;
  614. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  615. if ($cgdinfo == false) return false;
  616. $QrdCgd = [
  617. "cgdNo" => makeNo("CG"),
  618. "bkcode" => $cgdinfo['bkcode'],
  619. 'wsm_code' => $cgdinfo['wsm_code'],
  620. "cgder_id" => $cgdinfo['cgder_id'],
  621. "cgder" => $cgdinfo['cgder'],
  622. "spuCode" => $cgdinfo['spuCode'],
  623. "good_name" => $cgdinfo['good_name'],
  624. "good_num" => $outinfo['good_num'],
  625. "good_price" => $cgdinfo['good_price'],
  626. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  627. "pakge_fee" => $cgdinfo['pakge_fee'],
  628. "cert_fee" => $cgdinfo['cert_fee'],
  629. "open_fee" => $cgdinfo['open_fee'],
  630. "delivery_fee" => $cgdinfo['delivery_fee'],
  631. "mark_fee" => $cgdinfo['mark_fee'],
  632. "teach_fee" => $cgdinfo['teach_fee'],
  633. "nake_fee" => $cgdinfo['nake_fee'],
  634. "demo_fee" => $cgdinfo['demo_fee'],
  635. "weight" => $cgdinfo['weight'],
  636. "diff_weight" => $cgdinfo['diff_weight'],
  637. "diff_fee" => $cgdinfo['diff_fee'],
  638. "gold_price" => $cgdinfo['gold_price'],
  639. "supplierNo" => $cgdinfo['supplierNo'],
  640. "supplier_name" => $cgdinfo['supplier_name'],
  641. "companyNo" => $cgdinfo['companyNo'],
  642. "send_status" => 3,
  643. "send_num" => $outinfo['good_num'],
  644. "wsend_num" => 0,
  645. "remark" => $cgdinfo['remark'],
  646. "status" => 3,//入库完成
  647. "lasttime" => $cgdinfo['lasttime'],
  648. "is_del" => 0,
  649. "order_type" => $outinfo['order_type'],
  650. "order_source" => $outinfo['order_source'],
  651. "good_type" => $cgdinfo['good_type'],
  652. "addtime" => date("Y-m-d H:i:s"),
  653. "updatetime" => date("Y-m-d H:i:s"),
  654. 'good_createrid' => $outinfo['good_createrid'],
  655. 'good_creater' => $outinfo['good_creater'],//商品创建人
  656. ];
  657. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  658. if ($insetrCgd == false) return false;
  659. $standing_book_data = array_merge($standing_book_data, [
  660. 'cgdNo' => $QrdCgd['cgdNo'],
  661. 'bk_code' => $QrdCgd['bkcode'],
  662. ]);
  663. $merge_num = Db::name("purchease_order")
  664. ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
  665. ->where('order_source', '<>', 0)//0备库单不计算在内
  666. ->field("sum(send_num)-sum(th_num) as num")
  667. ->find();
  668. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  669. $cgd['merge_num'] = $merge_num['num'];
  670. $cgd['updatetime'] = date("Y-m-d H:i:s");
  671. $up = Db::name("order_bk")->save($cgd);
  672. if ($up == false) return false;
  673. $data = [
  674. "orderCode" => $outinfo['orderCode'],
  675. "cgdNo" => $QrdCgd['cgdNo'],
  676. "spuCode" => $outinfo['spuCode'],
  677. "companyNo" => $outinfo['companyNo'],
  678. "good_num" => $outinfo['good_num'],
  679. "wsend_num" => $outinfo['good_num'],
  680. "send_num" => 0,
  681. "wait_num" => $outinfo['good_num'],
  682. "status" => 1,
  683. "source" => 1,
  684. ];
  685. $order = Db::name("order_num")->save($data);
  686. if ($order == false) return false;
  687. $this->cgd_data = $QrdCgd;
  688. return true;
  689. }
  690. //与有赞项目回调
  691. private function callbackYz(array $data = [])
  692. {
  693. $host = Config::get('app.yz_domain');
  694. $response = curl_request($host . 'api/callback_order_by_stock', $data);
  695. return json_decode($response, true);
  696. }
  697. }