ImportOrderFromCHandleData.php 32 KB

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