ImportOrderFromCHandleData.php 31 KB

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