ImportOrderFromCHandleData.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use app\admin\model\ActionLog;
  5. use app\admin\model\GoodLog;
  6. use app\admin\model\OrderImportFromC as OIFCModel;
  7. use app\admin\model\ProcessOrder;
  8. use app\admin\model\Test1;
  9. use think\console\Command;
  10. use think\console\Input;
  11. use think\console\input\Argument;
  12. use think\console\input\Option;
  13. use think\console\Output;
  14. use think\Exception;
  15. use think\facade\Db;
  16. //C端订单导入-处理
  17. class ImportOrderFromCHandleData extends Command
  18. {
  19. protected function configure()
  20. {
  21. // 指令配置
  22. $this->setName('handleData')
  23. ->setDescription('批量处理C端导入的订单数据');
  24. }
  25. //【脚本2】批量处理C端导入的订单数据
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $order_import_from_c_db = new OIFCModel();
  29. $c_id = $order_import_from_c_db
  30. ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_relation])
  31. ->lock(true)
  32. ->value('id', 0);
  33. if (!$c_id) $output->writeln('没有符合条件的记录');
  34. else {
  35. Db::startTrans();
  36. try {
  37. //先找用户确认信息
  38. $extend_data = Db::name('order_import_from_c_extend')
  39. ->where(['order_import_from_c_id' => $c_id['id'], 'is_del' => 0, 'type' => 2])
  40. ->find();
  41. if (empty($extend_data)) {
  42. $extend_data = Db::name('order_import_from_c_extend')
  43. ->where(['order_import_from_c_id' => $c_id['id'], 'is_del' => 0, 'type' => 1])
  44. ->find();
  45. }
  46. if (empty($extend_data)) throw new Exception('没有对应的解析数据');
  47. //复用sale::create()方法 -- start
  48. $orderCode = makeNo("QR");
  49. $customer_code = $extend_data['customer_code'];
  50. $customer = Db::name("customer_info")
  51. ->where(["companyNo" => $customer_code])
  52. ->find();
  53. if ($customer == false) throw new Exception('未找到客户数据');
  54. $supplierNo = $extend_data['supplierNo'];
  55. $supplier = Db::name("supplier")
  56. ->where(["code" => $supplierNo])
  57. ->find();
  58. if ($supplier == false) throw new Exception('未找到平台供应商数据');
  59. $goodtype = 1;//1正常商品
  60. $order_type = 3;//1销售 2咨询 3平台(C端)导入
  61. $sendtype = 1;//直接发货
  62. $platform_id = $extend_data['platform_codes'];
  63. $platform_order = '';
  64. $good_num = $extend_data['num'];
  65. $arrtime = $extend_data['platform_time'];
  66. $paytime = $extend_data['addtime'];
  67. $workNo = '';//业管单号
  68. $ct = Db::name('good_platform')
  69. ->alias('a')
  70. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  71. ->where(['a.spuCode' => $extend_data['spuCode']])
  72. ->field("b.*,a.skuCode,a.platform_code,a.plat_code")
  73. ->find();
  74. if ($ct == false) throw new Exception('未找到商品数据');
  75. $goodinfo = $ct;
  76. $is_stock = $ct['is_stock'];
  77. $spuCode = $ct['spuCode'];
  78. $skuCode = $ct['skuCode'];
  79. $is_activity = empty($extend_data['activity_name']) ? 0 : 1;
  80. if ($goodinfo['is_stock'] == 1) {
  81. $stock = Db::name("good_stock")
  82. ->alias("a")
  83. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  84. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.supplierNo" => $supplierNo])
  85. ->field("a.id,a.usable_stock,a.wait_out_stock")
  86. ->find();
  87. if ($stock == false || $stock['usable_stock'] < $good_num) throw new Exception('库存数量不足');
  88. }
  89. $origin = Db::name("good_nake")
  90. ->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])
  91. ->order("min_num desc")
  92. ->find();
  93. if ($origin == false) throw new Exception('未找到相关阶梯成本价格');
  94. $origin_price = $origin['nake_total'];
  95. $sale_price = $extend_data['price'];
  96. if ($goodtype == 1) {
  97. $good = Db::name("good_ladder")
  98. ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
  99. ->where([["min_num", "<=", $good_num]])
  100. ->order("min_num desc")
  101. ->find();
  102. if ($good == false) throw new Exception('未找到相关阶梯价格');
  103. $sale_price = $good['sale_price'];
  104. if ($is_activity == 1) {
  105. $act = Db::name("activity_info")
  106. ->alias("a")
  107. ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  108. ->where(["a.skuCode" => $skuCode, "a.id" => $extend_data['activity_id'], "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  109. ->find();
  110. if ($act == false) throw new Exception('未找到相关活动价');
  111. if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
  112. if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
  113. $sale_price = $act['activity_price'];
  114. }
  115. }
  116. $cgd = [
  117. "supplierNo" => $ct['supplierNo'],
  118. "companyNo" => $supplierNo,
  119. "orderCode" => $orderCode,
  120. "spuCode" => $ct['spuCode'],
  121. "skuCode" => $ct['skuCode'],
  122. "good_name" => $ct['good_name'],
  123. "sale_price" => $origin_price,
  124. "total_fee" => $origin_price * $good_num,
  125. "pakge_fee" => $ct['packing_fee'],
  126. "cert_fee" => $ct['cert_fee'],
  127. "open_fee" => $ct['open_fee'],
  128. "cost_fee" => $ct['cost_fee'],
  129. "mark_fee" => $ct['mark_fee'],
  130. "demo_fee" => $ct['demo_fee'],
  131. "good_num" => $good_num,
  132. "good_type" => $goodtype,
  133. "order_type" => $is_stock == 1 ? 1 : 2,
  134. "createrid" => $ct['createrid'],
  135. "creater" => $ct['creater'],
  136. 'send_way' => 2
  137. ];
  138. $send_num = $extend_data['num'];
  139. $remark = $extend_data['order_remark'];
  140. $rm = $extend_data['createrid'];
  141. $ri = $extend_data['creater'];
  142. $data = [
  143. "orderCode" => $orderCode,
  144. "good_code" => $spuCode,
  145. "skuCode" => $skuCode,
  146. "customer_code" => $customer_code,
  147. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  148. "good_num" => $good_num,
  149. "cat_id" => $goodinfo['cat_id'],
  150. "apply_id" => $rm,
  151. "apply_name" => $ri,
  152. "origin_price" => $origin_price,
  153. "sale_price" => $sale_price,
  154. "post_fee" => 0,
  155. "status" => $send_num == 0 ? 3 : ($good_num == $send_num ? 5 : 4),
  156. "send_num" => $send_num,
  157. "wsend_num" => $good_num - $send_num,
  158. "send_status" => $good_num == $send_num ? 3 : ($send_num == 0 ? 1 : 2),
  159. "status" => 0,
  160. "send_num" => 0,
  161. "wsend_num" => $good_num,
  162. "send_status" => 1,
  163. "good_type" => $goodtype,
  164. "send_type" => $sendtype,
  165. "supplierNo" => $supplierNo,
  166. "is_del" => 0,
  167. "zxNo" => "",
  168. "platform_order" => $platform_order,
  169. "platform_id" => $platform_id,
  170. "remark" => $remark,
  171. "is_stock" => $is_stock,
  172. "is_activity" => $is_activity === "" ? 0 : $is_activity,
  173. "order_type" => $order_type,
  174. // "poNo"=>$poNo,
  175. 'good_weight' => $ct['weight'],
  176. 'gold_price' => $ct['cgd_gold_price'],
  177. 'cost_price' => $ct['cost_fee'],
  178. 'diff_weight' => 0,
  179. 'diff_fee' => 0,
  180. "workNo" => $workNo,
  181. "addtime" => date("Y-m-d H:i:s"),
  182. "updatetime" => date("Y-m-d H:i:s"),
  183. 'total_price' => round($sale_price * $good_num, 2),
  184. ];
  185. $paytime == "" ? "" : $data['paytime'] = $paytime;
  186. $datainfo = Db::name('sale')->insert($data, true);
  187. if ($datainfo > 0) {
  188. if ($is_activity == 1) {
  189. $actup = [
  190. "activity_stock" => $act['activity_stock'] - $good_num,
  191. "updatetime" => date("Y-m-d H:i:s")
  192. ];
  193. $actupp = Db::name("activity_info")
  194. ->where(["skuCode" => $skuCode, "id" => $extend_data['activity_id'], "is_del" => 0, "status" => 1])
  195. ->save($actup);
  196. if ($actupp == false) throw new Exception('活动库存修改失败');
  197. }
  198. if ($is_stock == 0) {
  199. $bol = $this->createCgd($cgd, $rm, $ri);
  200. if ($bol == false) throw new Exception('订单创建失败');
  201. } else {
  202. $bol = $this->RelaCgd(['orderCode' => $orderCode, "good_num" => $good_num, "spuCode" => $spuCode, "companyNo" => $supplierNo]);
  203. if ($bol == false) throw new Exception('库存商品关联采购单失败');
  204. if (isset($stock)) {
  205. $stck = [
  206. "usable_stock" => $stock['usable_stock'] - $good_num,
  207. "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  208. "updatetime" => date("Y-m-d H:i:s")
  209. ];
  210. $upad = Db::name("good_stock")
  211. ->where($stock)
  212. ->update($stck);
  213. if ($upad == false) throw new Exception('库存商品更新库存失败');
  214. //商品变动日志表,good_log_code字段存储采购单号
  215. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  216. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  217. GoodLog::LogAdd(['id' => $rm, 'nickname' => $ri], $good_data, "XSQRD");
  218. }
  219. }
  220. if ($sendtype == 1) {
  221. $temp = [
  222. 'orderCode' => $orderCode,
  223. 'contactor' => $extend_data['contactor'],
  224. 'mobile' => $extend_data['mobile'],
  225. 'addr' => $extend_data['addr'],
  226. 'addr_code' => $extend_data['addr_code'],
  227. 'customer_code' => $customer_code,
  228. 'receipt_quantity' => $extend_data['num'],//收货数量,
  229. 'post_fee' => 0,
  230. 'is_del' => 0,
  231. 'addtime' => date("Y-m-d H:i:s"),
  232. 'updatetime' => date("Y-m-d H:i:s"),
  233. 'arrive_time' => $arrtime,
  234. ];
  235. $vmp = Db::name('order_addr')->insert($temp, true);
  236. if ($vmp > 0) {
  237. if ($is_stock == 1) {
  238. $order = Db::name("order_num")
  239. ->where(["orderCode" => $orderCode, "status" => 1])
  240. ->where([["wsend_num", ">=", 0]])
  241. ->lock(true)
  242. ->find();
  243. if ($order == false) throw new Exception('未找到可以发货得采购单数据');
  244. $num = $extend_data['num'];
  245. $outCode = makeNo("DF");
  246. $order['wsend_num'] -= $num;
  247. $order['send_num'] += $num;
  248. $or = Db::name("order_num")->save($order);
  249. if ($or == false) throw new Exception('发货地址更新失败');
  250. $tep = [
  251. "cgdNo" => $order['cgdNo'],
  252. "outCode" => $outCode,
  253. "send_num" => $num,
  254. "status" => 1,
  255. "addtime" => date("Y-m-d H:i:s"),
  256. "updatetime" => date("Y-m-d H:i:s")
  257. ];
  258. $sen = Db::name("order_send")->save($tep);
  259. if ($sen == false) throw new Exception('发货地址添加创建失败');
  260. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  261. if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
  262. $out = [
  263. "orderCode" => $orderCode,
  264. "outCode" => $outCode,
  265. "apply_id" => $rm,
  266. "apply_name" => $ri,
  267. "addrid" => $vmp,
  268. "post_name" => "",
  269. "post_code" => "",
  270. "post_fee" => 0,
  271. "sendtime" => date("Y-m-d H:i:s"),
  272. "send_num" => $num,
  273. "check_num" => 0,
  274. "error_num" => 0,
  275. "wsm_code" => $cgdinfo['wsm_code'],
  276. "order_type" => 1,
  277. "status" => 1,
  278. "addtime" => date("Y-m-d H:i:s"),
  279. "updatetime" => date("Y-m-d H:i:s")
  280. ];
  281. $ou = Db::name("order_out")->insert($out);
  282. if ($ou == false) throw new Exception('发货地址添加创建失败');
  283. else {
  284. //修改状态,添加待办
  285. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  286. "order_code" => $outCode,//出库单号
  287. "status" => 0,//这里的status是之前的值
  288. "action_remark" => '',//备注
  289. "action_type" => "create"//新建create,编辑edit,更改状态status
  290. ], "CKD", 0, $out);
  291. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  292. "order_type" => 'CKD',
  293. "order_code" => $outCode,//出库单号
  294. "order_id" => 0,
  295. "order_status" => 0
  296. ]);
  297. }
  298. }
  299. } else throw new Exception('发货地址添加失败');
  300. }
  301. }
  302. //复用sale::create()方法 -- end
  303. //处理完成
  304. $order_import_from_c_db
  305. ->where('id', $c_id)
  306. ->save([
  307. 'status' => $order_import_from_c_db::$status_success,
  308. 'updatetime' => date('Y-m-d H:i:s'),
  309. 'remark' => '',
  310. 'updateid' => 0,
  311. 'updater' => 'system'
  312. ]);
  313. Db::commit();
  314. $output->writeln(date('Y-m-d H:i:s') . '|处理成功');
  315. } catch (\think\Exception $exception) {
  316. Db::rollback();
  317. $order_import_from_c_db
  318. ->where('id', $c_data['id'])
  319. ->save([
  320. 'status' => $order_import_from_c_db::$status_stock_not_enough,
  321. 'updatetime' => date('Y-m-d H:i:s'),
  322. 'remark' => $exception->getMessage(),
  323. 'updateid' => 0,
  324. 'updater' => 'system'
  325. ]);
  326. // 指令输出
  327. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  328. }
  329. }
  330. }
  331. //创建采购单
  332. private function createCgd($data = [], $rm = '0', $ri = '')
  333. {
  334. $cgdCode = makeNo("CG");
  335. $supplier = Db::name("supplier")
  336. ->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  337. if ($supplier == false) return false;
  338. $wsm = Db::name("warehouse_info")
  339. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  340. ->find();
  341. if ($wsm == false) {
  342. $wsm_code = makeNo("WSM");
  343. $inwsm = [
  344. "wsm_code" => $wsm_code,
  345. "name" => $supplier['name'],
  346. "wsm_type" => 2,
  347. "supplierNo" => $supplier['code'],
  348. "addr" => "",
  349. "addrs_code" => "",
  350. "contactor" => 0,
  351. "contactor_name" => 0,
  352. "mobile" => "",
  353. "position" => "",
  354. "companyNo" => $data['companyNo'],
  355. "status" => 1,
  356. "is_del" => 0,
  357. "addtime" => date("Y-m-d H:i:s"),
  358. "updatetime" => date("Y-m-d H:i:s")
  359. ];
  360. $in = Db::name("warehouse_info")->insert($inwsm);
  361. if ($in == false) return false;
  362. } else {
  363. $wsm_code = $wsm['wsm_code'];
  364. }
  365. $cg = [
  366. "cgdNo" => $cgdCode,
  367. "bkcode" => "",
  368. "wsm_code" => $wsm_code,
  369. "cgder" => $supplier['person'],
  370. "cgder_id" => $supplier['personid'],
  371. "spuCode" => $data['spuCode'],
  372. "skuCode" => $data['skuCode'],
  373. "good_name" => $data['good_name'],
  374. "good_num" => $data['good_num'],
  375. "good_price" => $data['sale_price'],
  376. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  377. "pakge_fee" => $data['pakge_fee'],
  378. "cert_fee" => $data['cert_fee'],
  379. "open_fee" => $data['open_fee'],
  380. "teach_fee" => $data['cost_fee'],
  381. "mark_fee" => $data['mark_fee'],
  382. "demo_fee" => $data['demo_fee'],
  383. #"weight"=>$data['weight'],
  384. "diff_weight" => "0",
  385. "diff_fee" => "0",
  386. "gold_price" => "0",
  387. "supplierNo" => $data['supplierNo'],
  388. "supplier_name" => $supplier['name'],
  389. "companyNo" => $data['companyNo'],
  390. "send_status" => 1,
  391. "send_num" => 0,
  392. "wsend_num" => $data['good_num'],
  393. "remark" => "",
  394. "status" => 0,//0初始化
  395. "lasttime" => date("Y-m-d H:i:s"),
  396. "is_del" => 0,
  397. "order_type" => $data['order_type'],
  398. "good_type" => $data['good_type'],
  399. "addtime" => date("Y-m-d H:i:s"),
  400. "updatetime" => date("Y-m-d H:i:s")
  401. ];
  402. $up = Db::name("purchease_order")->insert($cg, true);
  403. if ($up) {
  404. //修改状态,添加待办
  405. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  406. "order_code" => $cg['cgdNo'],//销售单code
  407. "status" => 0,//这里的status是之前的值
  408. "action_remark" => '',//备注
  409. "action_type" => "create"//新建create,编辑edit,更改状态status
  410. ], "CGD", $cg['status'], $cg);
  411. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  412. "order_type" => 'CGD',
  413. "order_code" => $cg['cgdNo'],//销售单code
  414. "order_id" => $up,
  415. "order_status" => $cg['status']
  416. ]);
  417. $rela = [
  418. "orderCode" => $data['orderCode'],
  419. "cgdNo" => $cgdCode,
  420. "spuCode" => $data['spuCode'],
  421. "good_num" => $data['good_num'],
  422. "wsend_num" => $data['good_num'],
  423. "send_num" => 0,
  424. "wait_num" => 0,
  425. "status" => 1,
  426. "source" => 2
  427. ];
  428. $re = Db::name("order_num")->save($rela);
  429. if ($re == false) return false;
  430. else {
  431. $stokc = Db::name("good_stock")
  432. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  433. ->find();
  434. if ($stokc == false) {
  435. $stokc = [
  436. "spuCode" => $data['spuCode'],
  437. "wsm_code" => $wsm_code,
  438. "wait_in_stock" => 0,
  439. "wait_out_stock" => 0,
  440. "usable_stock" => 0,
  441. "intra_stock" => 0,
  442. "total_stock" => 0,
  443. "status" => 1,
  444. "addtime" => date("Y-m-d H:i:s"),
  445. "updatetime" => date("Y-m-d H:i:s")
  446. ];
  447. $stoc = Db::name("good_stock")->save($stokc);
  448. if ($stoc == false) return false;
  449. }
  450. return true;
  451. }
  452. } else return false;
  453. }
  454. //创建.....
  455. private function RelaCgd(array $outinfo = [])
  456. {
  457. $cgd = Db::name("order_bk")
  458. ->where([
  459. ["spuCode", "=", $outinfo['spuCode']],
  460. ["is_del", "=", 0],
  461. ["balance_num", ">=", $outinfo['good_num']],
  462. ['companyNo', "=", $outinfo['companyNo']]
  463. ])
  464. ->lock(true)
  465. ->find();
  466. if ($cgd == false) return false;
  467. $cgd['balance_num'] -= $outinfo['good_num'];
  468. $cgd['merge_num'] += $outinfo['good_num'];
  469. $cgd['updatetime'] = date("Y-m-d H:i:s");
  470. $up = Db::name("order_bk")->save($cgd);
  471. if ($up == false) return false;
  472. $data = [
  473. "orderCode" => $outinfo['orderCode'],
  474. "cgdNo" => $cgd['cgdNo'],
  475. "spuCode" => $outinfo['spuCode'],
  476. "companyNo" => $outinfo['companyNo'],
  477. "good_num" => $outinfo['good_num'],
  478. "wsend_num" => $outinfo['good_num'],
  479. "send_num" => 0,
  480. "wait_num" => $outinfo['good_num'],
  481. "status" => 1,
  482. "source" => 1,
  483. ];
  484. $order = Db::name("order_num")->save($data);
  485. if ($order == false) return false;
  486. return true;
  487. }
  488. }