ImportOrderFromCHandleData.php 34 KB

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