ImportOrderFromCHandleData.php 35 KB

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