ImportOrderFromCHandleData.php 36 KB

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