ImportOrderFromCHandleData.php 38 KB

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