handleYzOrderData.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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\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. ];
  290. //非库存品
  291. $bol = $this->createCgd($cgd, $rm, $ri, $standing_book_data);
  292. if ($bol == false) throw new Exception('订单创建失败');
  293. } else {
  294. //库存品
  295. $cgd = [
  296. 'orderCode' => $orderCode,
  297. "good_num" => $good_num,
  298. "spuCode" => $spuCode,
  299. "companyNo" => $supplierNo,
  300. 'order_type' => $order_type,
  301. 'order_source' => $order_source,
  302. 'good_createrid' => $goodinfo['createrid'],
  303. 'good_creater' => $goodinfo['creater'],//商品创建人
  304. ];
  305. $bol = $this->RelaCgd($cgd, $standing_book_data);
  306. if ($bol == false) throw new Exception('库存商品关联采购单失败');
  307. if (isset($stock)) {
  308. $stck = [
  309. "usable_stock" => $stock['usable_stock'] - $good_num,
  310. "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  311. "updatetime" => date("Y-m-d H:i:s")
  312. ];
  313. $upad = Db::name("good_stock")
  314. ->where($stock)
  315. ->update($stck);
  316. if ($upad == false) throw new Exception('库存商品更新库存失败');
  317. //商品变动日志表,good_log_code字段存储采购单号
  318. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  319. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  320. GoodLog::LogAdd(['id' => $rm, 'nickname' => $ri], $good_data, "XSQRD");
  321. }
  322. }
  323. if ($sendtype == 1) {
  324. $p_c_a = get_address($c_data['delivery_province'] . $c_data['delivery_city'] . $c_data['delivery_district']);
  325. $province = empty($p_c_a['province']) ? $p_c_a['province'] : Db::name('province')->whereLike('name', '%' . $p_c_a['province'] . '%')->value('province_code');
  326. $city = empty($p_c_a['city']) ? $p_c_a['city'] : Db::name('city')->whereLike('name', '%' . $p_c_a['city'] . '%')->value('city_code');
  327. $area = empty($p_c_a['district']) ? $p_c_a['district'] : Db::name('area')->whereLike('name', '%' . $p_c_a['district'] . '%')->value('area_code');
  328. $addr_code = implode([$province, $city, $area], ',');
  329. $temp = [
  330. 'orderCode' => $orderCode,
  331. 'contactor' => $c_data['receiver_name'],
  332. 'mobile' => $c_data['receiver_tel'],
  333. 'addr' => $c_data['delivery_address'],
  334. 'addr_code' => $addr_code,
  335. 'customer_code' => $customer_code,
  336. 'receipt_quantity' => $c_data['num'],//收货数量,
  337. 'post_fee' => $c_data['post_fee'],
  338. 'is_del' => 0,
  339. 'addtime' => date("Y-m-d H:i:s"),
  340. 'updatetime' => date("Y-m-d H:i:s"),
  341. 'arrive_time' => $arrtime,
  342. ];
  343. $vmp = Db::name('order_addr')->insertGetId($temp);
  344. if ($vmp > 0) {
  345. $order = Db::name("order_num")
  346. ->where(["orderCode" => $orderCode, "status" => 1])
  347. ->where([["wsend_num", ">=", 0]])
  348. ->lock(true)
  349. ->find();
  350. if ($order == false) throw new Exception('未找到可以发货得采购单数据');
  351. $num = $c_data['num'];
  352. $outCode = makeNo("DF");
  353. $order['wsend_num'] -= $num;
  354. $order['send_num'] += $num;
  355. $or = Db::name("order_num")->save($order);
  356. if ($or == false) throw new Exception('发货地址更新失败');
  357. $tep = [
  358. "cgdNo" => $order['cgdNo'],
  359. "outCode" => $outCode,
  360. "send_num" => $num,
  361. "status" => 1,
  362. "addtime" => date("Y-m-d H:i:s"),
  363. "updatetime" => date("Y-m-d H:i:s")
  364. ];
  365. $sen = Db::name("order_send")->save($tep);
  366. if ($sen == false) throw new Exception('发货地址添加创建失败');
  367. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  368. if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
  369. $out = [
  370. "orderCode" => $orderCode,
  371. "outCode" => $outCode,
  372. "apply_id" => $rm,
  373. "apply_name" => $ri,
  374. "addrid" => $vmp,
  375. "post_name" => "",
  376. "post_code" => "",
  377. "post_fee" => 0,
  378. "sendtime" => date("Y-m-d H:i:s"),
  379. "send_num" => $num,
  380. "check_num" => 0,
  381. "error_num" => 0,
  382. "wsm_code" => $cgdinfo['wsm_code'],
  383. "order_type" => $order_type,
  384. "status" => $is_stock == 1 ? 1 : 0,
  385. "addtime" => date("Y-m-d H:i:s"),
  386. "updatetime" => date("Y-m-d H:i:s")
  387. ];
  388. $ou = Db::name("order_out")->insert($out);
  389. if ($ou == false) throw new Exception('发货地址添加创建失败');
  390. else {
  391. //修改状态,添加待办
  392. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  393. "order_code" => $outCode,//出库单号
  394. "status" => 0,//这里的status是之前的值
  395. "action_remark" => '',//备注
  396. "action_type" => "create"//新建create,编辑edit,更改状态status
  397. ], "CKD", 0, $out);
  398. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  399. "order_type" => 'CKD',
  400. "order_code" => $outCode,//出库单号
  401. "order_id" => 0,
  402. "order_status" => 0,
  403. "before_status" => 0
  404. ]);
  405. // $standing_book_data['outCode'] = $outCode;
  406. }
  407. } else throw new Exception('发货地址添加失败');
  408. }
  409. }
  410. //复用sale::create()方法 -- end
  411. //处理完成
  412. // $order_import_from_c_db
  413. // ->where('id', $c_data['id'])
  414. // ->where('is_del', OIFCModel::$is_del_normal)
  415. // ->save([
  416. // 'status' => $order_import_from_c_db::$status_success,
  417. // 'updatetime' => date('Y-m-d H:i:s'),
  418. // 'remark' => '',
  419. // 'updateid' => 0,
  420. // 'updater' => 'system',
  421. // 'orderCode' => $orderCode
  422. // ]);
  423. //维护台账记录
  424. 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")]));
  425. //回调更新有赞订单数据
  426. $callback_rs = $this->callbackYz(['status' => 6, 'id' => $c_data['id'], 'orderCode' => $orderCode]);
  427. if ($callback_rs['code'] != 0) throw new Exception($callback_rs['message']);
  428. //将采购单数据塞入到队列中
  429. if ($this->cgd_data) {
  430. $push_data = json_encode([
  431. 'supplierNo' => $this->cgd_data['supplierNo'],
  432. 'type' => 1,//1销售订单(采销的采购单),2上线结果
  433. 'data' => [
  434. 'cgdNo' => $this->cgd_data['cgdNo'],
  435. 'spuCode' => $this->cgd_data['spuCode'],
  436. 'good_name' => $this->cgd_data['good_name'],
  437. 'good_num' => $this->cgd_data['good_num'],
  438. 'good_price' => $this->cgd_data['good_price'],
  439. 'total_fee' => $this->cgd_data['total_fee'],
  440. 'weight' => $this->cgd_data['weight'],
  441. 'addtime' => $this->cgd_data['addtime'],
  442. ],
  443. ], JSON_UNESCAPED_UNICODE);
  444. Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
  445. }
  446. Db::commit();
  447. $output->writeln(date('Y-m-d H:i:s') . '|处理成功,orderCode:' . $orderCode);
  448. } catch (\think\Exception $exception) {
  449. Db::rollback();
  450. $message[] = $exception->getMessage();
  451. //回调更新有赞订单数据
  452. $callback_rs = $this->callbackYz(['status' => 8, 'id' => $c_data['id'], 'reason' => $exception->getMessage()]);
  453. if ($callback_rs['code'] != 0) $message[] = $callback_rs['message'];
  454. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . implode(',', $message));
  455. }
  456. }
  457. //没有需要处理的数据
  458. }
  459. //创建采购单
  460. private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
  461. {
  462. $cgdCode = makeNo("CG");
  463. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  464. if ($supplier == false) return false;
  465. $wsm = Db::name("warehouse_info")
  466. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  467. ->find();
  468. if ($wsm == false) {
  469. $wsm_code = makeNo("WSM");
  470. $inwsm = [
  471. "wsm_code" => $wsm_code,
  472. "name" => $supplier['name'],
  473. "wsm_type" => 2,
  474. "supplierNo" => $supplier['code'],
  475. "addr" => "",
  476. "addrs_code" => "",
  477. "contactor" => 0,
  478. "contactor_name" => 0,
  479. "mobile" => "",
  480. "position" => "",
  481. "companyNo" => $data['companyNo'],
  482. "status" => 1,
  483. "is_del" => 0,
  484. "addtime" => date("Y-m-d H:i:s"),
  485. "updatetime" => date("Y-m-d H:i:s")
  486. ];
  487. $in = Db::name("warehouse_info")->insert($inwsm);
  488. if ($in == false) return false;
  489. } else {
  490. $wsm_code = $wsm['wsm_code'];
  491. }
  492. $cg = [
  493. "cgdNo" => $cgdCode,
  494. "bkcode" => "",
  495. "wsm_code" => $wsm_code,
  496. "cgder" => $data['creater'],
  497. "cgder_id" => $data['createrid'],
  498. "spuCode" => $data['spuCode'],
  499. "skuCode" => $data['skuCode'],
  500. "good_name" => $data['good_name'],
  501. "good_num" => $data['good_num'],
  502. "good_price" => $data['sale_price'],
  503. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  504. "pakge_fee" => $data['pakge_fee'],
  505. "cert_fee" => $data['cert_fee'],
  506. "open_fee" => $data['open_fee'],
  507. "teach_fee" => $data['cost_fee'],
  508. "mark_fee" => $data['mark_fee'],
  509. "demo_fee" => $data['demo_fee'],
  510. "nake_fee" => $data['nake_fee'],
  511. "delivery_fee" => $data['delivery_fee'],
  512. #"weight"=>$data['weight'],
  513. "diff_weight" => "0",
  514. "diff_fee" => "0",
  515. "gold_price" => $data['gold_price'],
  516. "supplierNo" => $data['supplierNo'],
  517. "supplier_name" => $supplier['name'],
  518. "companyNo" => $data['companyNo'],
  519. "send_status" => 1,
  520. "send_num" => 0,
  521. "wsend_num" => $data['good_num'],
  522. "remark" => "",
  523. "status" => 0,//0初始化
  524. "lasttime" => date("Y-m-d H:i:s"),
  525. "is_del" => 0,
  526. "order_type" => $data['order_type'],
  527. "order_source" => $data['order_source'],
  528. "good_type" => $data['good_type'],
  529. "addtime" => date("Y-m-d H:i:s"),
  530. "updatetime" => date("Y-m-d H:i:s"),
  531. 'good_createrid' => $data['good_createrid'],
  532. 'good_creater' => $data['good_creater'],//商品创建人
  533. ];
  534. $up = Db::name("purchease_order")->insert($cg, true);
  535. if ($up) {
  536. //修改状态,添加待办
  537. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  538. "order_code" => $cg['cgdNo'],//销售单code
  539. "status" => 0,//这里的status是之前的值
  540. "action_remark" => '',//备注
  541. "action_type" => "create"//新建create,编辑edit,更改状态status
  542. ], "CGD", $cg['status'], $cg);
  543. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  544. "order_type" => 'CGD',
  545. "order_code" => $cg['cgdNo'],//销售单code
  546. "order_id" => $up,
  547. "order_status" => $cg['status'],
  548. "before_status" => 0
  549. ]);
  550. $standing_book_data['cgdNo'] = $cgdCode;
  551. $rela = [
  552. "orderCode" => $data['orderCode'],
  553. "cgdNo" => $cgdCode,
  554. "spuCode" => $data['spuCode'],
  555. "good_num" => $data['good_num'],
  556. "wsend_num" => $data['good_num'],
  557. "send_num" => 0,
  558. "wait_num" => 0,
  559. "status" => 1,
  560. "source" => 2
  561. ];
  562. $re = Db::name("order_num")->save($rela);
  563. if ($re == false) return false;
  564. else {
  565. $stokc = Db::name("good_stock")
  566. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  567. ->find();
  568. if ($stokc == false) {
  569. $stokc = [
  570. "spuCode" => $data['spuCode'],
  571. "wsm_code" => $wsm_code,
  572. "wait_in_stock" => $data['good_num'],
  573. "wait_out_stock" => 0,
  574. "usable_stock" => 0,
  575. "intra_stock" => 0,
  576. "total_stock" => 0,
  577. "status" => 1,
  578. "addtime" => date("Y-m-d H:i:s"),
  579. "updatetime" => date("Y-m-d H:i:s")
  580. ];
  581. } else {
  582. $stokc['wait_in_stock'] += $data['good_num'];
  583. $stokc['updatetime'] = date("Y-m-d H:i:s");
  584. }
  585. $stoc = Db::name("good_stock")->save($stokc);
  586. if ($stoc == false) return false;
  587. $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"];
  588. GoodLog::LogAdd(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
  589. $this->cgd_data = $cg;
  590. return true;
  591. }
  592. } else return false;
  593. }
  594. //创建.....
  595. private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
  596. {
  597. //备库单
  598. $cgd = Db::name("order_bk")
  599. ->where([
  600. ["spuCode", "=", $outinfo['spuCode']],
  601. ["is_del", "=", 0],
  602. ["balance_num", ">=", $outinfo['good_num']],
  603. ['companyNo', "=", $outinfo['companyNo']]
  604. ])
  605. ->lock(true)
  606. ->find();
  607. if ($cgd == false) return false;
  608. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  609. if ($cgdinfo == false) return false;
  610. $QrdCgd = [
  611. "cgdNo" => makeNo("CG"),
  612. "bkcode" => $cgdinfo['bkcode'],
  613. 'wsm_code' => $cgdinfo['wsm_code'],
  614. "cgder_id" => $cgdinfo['cgder_id'],
  615. "cgder" => $cgdinfo['cgder'],
  616. "spuCode" => $cgdinfo['spuCode'],
  617. "good_name" => $cgdinfo['good_name'],
  618. "good_num" => $outinfo['good_num'],
  619. "good_price" => $cgdinfo['good_price'],
  620. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  621. "pakge_fee" => $cgdinfo['pakge_fee'],
  622. "cert_fee" => $cgdinfo['cert_fee'],
  623. "open_fee" => $cgdinfo['open_fee'],
  624. "delivery_fee" => $cgdinfo['delivery_fee'],
  625. "mark_fee" => $cgdinfo['mark_fee'],
  626. "teach_fee" => $cgdinfo['teach_fee'],
  627. "nake_fee" => $cgdinfo['nake_fee'],
  628. "demo_fee" => $cgdinfo['demo_fee'],
  629. "weight" => $cgdinfo['weight'],
  630. "diff_weight" => $cgdinfo['diff_weight'],
  631. "diff_fee" => $cgdinfo['diff_fee'],
  632. "gold_price" => $cgdinfo['gold_price'],
  633. "supplierNo" => $cgdinfo['supplierNo'],
  634. "supplier_name" => $cgdinfo['supplier_name'],
  635. "companyNo" => $cgdinfo['companyNo'],
  636. "send_status" => 3,
  637. "send_num" => $outinfo['good_num'],
  638. "wsend_num" => 0,
  639. "remark" => $cgdinfo['remark'],
  640. "status" => 3,//入库完成
  641. "lasttime" => $cgdinfo['lasttime'],
  642. "is_del" => 0,
  643. "order_type" => $outinfo['order_type'],
  644. "order_source" => $outinfo['order_source'],
  645. "good_type" => $cgdinfo['good_type'],
  646. "addtime" => date("Y-m-d H:i:s"),
  647. "updatetime" => date("Y-m-d H:i:s"),
  648. 'good_createrid' => $outinfo['good_createrid'],
  649. 'good_creater' => $outinfo['good_creater'],//商品创建人
  650. ];
  651. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  652. if ($insetrCgd == false) return false;
  653. $standing_book_data = array_merge($standing_book_data, [
  654. 'cgdNo' => $QrdCgd['cgdNo'],
  655. 'bk_code' => $QrdCgd['bkcode'],
  656. ]);
  657. $merge_num = Db::name("purchease_order")
  658. ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
  659. ->where('order_source', '<>', 0)//0备库单不计算在内
  660. ->field("sum(send_num)-sum(th_num) as num")
  661. ->find();
  662. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  663. $cgd['merge_num'] = $merge_num['num'];
  664. $cgd['updatetime'] = date("Y-m-d H:i:s");
  665. $up = Db::name("order_bk")->save($cgd);
  666. if ($up == false) return false;
  667. $data = [
  668. "orderCode" => $outinfo['orderCode'],
  669. "cgdNo" => $QrdCgd['cgdNo'],
  670. "spuCode" => $outinfo['spuCode'],
  671. "companyNo" => $outinfo['companyNo'],
  672. "good_num" => $outinfo['good_num'],
  673. "wsend_num" => $outinfo['good_num'],
  674. "send_num" => 0,
  675. "wait_num" => $outinfo['good_num'],
  676. "status" => 1,
  677. "source" => 1,
  678. ];
  679. $order = Db::name("order_num")->save($data);
  680. if ($order == false) return false;
  681. $this->cgd_data = $QrdCgd;
  682. return true;
  683. }
  684. //与有赞项目回调
  685. private function callbackYz(array $data = [])
  686. {
  687. $host = Config::get('app.yz_domain');
  688. $response = curl_request($host . 'api/callback_order_by_stock', $data);
  689. return json_decode($response, true);
  690. }
  691. }