ImportOrderFromCHandleData.php 33 KB

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