ImportOrderFromCHandleData.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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(date('Y-m-d H:i:s') . '未查询到该记录');
  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,intval($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")->insertGetId($out);
  361. if ($ou == false) throw new Exception('发货地址添加创建失败');
  362. else {
  363. //修改状态,添加待办
  364. if ($is_stock==1){
  365. $roleid = Config('app.wsm_cgder_role');
  366. $uids = Db::name('user_role')
  367. ->where('is_del', 0)
  368. ->whereIn('roleid', $roleid)
  369. ->column('uid');
  370. $handle_user_list=implode(',', $uids);
  371. }
  372. //修改状态,添加待办
  373. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  374. "order_code" => $outCode,//出库单号
  375. "status" => 0,//这里的status是之前的值
  376. "action_remark" => '',//备注
  377. "action_type" => "create"//新建create,编辑edit,更改状态status
  378. ], "CKD", 0, $out);
  379. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  380. "order_type" => 'CKD',
  381. "order_code" => $outCode,//出库单号
  382. "order_id" => $ou,
  383. "order_status" => $is_stock == 1 ? 1 : 0,
  384. "before_status" => 0,
  385. 'holder_id' => $rm,
  386. 'handle_user_list' =>$handle_user_list??""
  387. ]);
  388. $standing_book_data['outCode'] = $outCode;
  389. }
  390. } else throw new Exception('发货地址添加失败');
  391. }
  392. }
  393. //复用sale::create()方法 -- end
  394. //处理完成
  395. $order_import_from_c_db
  396. ->where('id', $c_data['id'])
  397. ->where('is_del', OIFCModel::$is_del_normal)
  398. ->save([
  399. 'status' => $order_import_from_c_db::$status_success,
  400. 'updatetime' => date('Y-m-d H:i:s'),
  401. 'remark' => '',
  402. 'updateid' => 0,
  403. 'updater' => 'system',
  404. 'orderCode' => $orderCode
  405. ]);
  406. //维护台账记录
  407. 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")]));
  408. //将采购单数据塞入到队列中
  409. if ($this->cgd_data) {
  410. $push_data = json_encode([
  411. 'supplierNo' => $this->cgd_data['supplierNo'],
  412. 'type' => 1,//1销售订单(采销的采购单),2上线结果
  413. 'data' => [
  414. 'cgdNo' => $this->cgd_data['cgdNo'],
  415. 'spuCode' => $this->cgd_data['spuCode'],
  416. 'good_name' => $this->cgd_data['good_name'],
  417. 'good_num' => $this->cgd_data['good_num'],
  418. 'good_price' => $this->cgd_data['good_price'],
  419. 'total_fee' => $this->cgd_data['total_fee'],
  420. 'weight' => $this->cgd_data['weight'],
  421. 'addtime' => $this->cgd_data['addtime'],
  422. ],
  423. ], JSON_UNESCAPED_UNICODE);
  424. // Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
  425. }
  426. Db::commit();
  427. $output->writeln(date('Y-m-d H:i:s') . '|处理成功');
  428. } catch (\think\Exception $exception) {
  429. Db::rollback();
  430. $order_import_from_c_db
  431. ->where(['id' => $c_data['id'], 'status' => OIFCModel::$status_wait_relation])
  432. ->save([
  433. 'status' => $order_import_from_c_db::$status_stock_not_enough,
  434. 'updatetime' => date('Y-m-d H:i:s'),
  435. 'remark' => $exception->getMessage(),
  436. 'updateid' => 0,
  437. 'updater' => 'system'
  438. ]);
  439. // 指令输出
  440. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  441. }
  442. }
  443. }
  444. }
  445. //创建采购单
  446. private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
  447. {
  448. $cgdCode = makeNo("CG");
  449. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  450. if ($supplier == false) return false;
  451. $wsm = Db::name("warehouse_info")
  452. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  453. ->find();
  454. if ($wsm == false) {
  455. $wsm_code = makeNo("WSM");
  456. $inwsm = [
  457. "wsm_code" => $wsm_code,
  458. "name" => $supplier['name'],
  459. "wsm_type" => 2,
  460. "supplierNo" => $supplier['code'],
  461. "addr" => "",
  462. "addrs_code" => "",
  463. "contactor" => 0,
  464. "contactor_name" => 0,
  465. "mobile" => "",
  466. "position" => "",
  467. "companyNo" => $data['companyNo'],
  468. "status" => 1,
  469. "is_del" => 0,
  470. "addtime" => date("Y-m-d H:i:s"),
  471. "updatetime" => date("Y-m-d H:i:s")
  472. ];
  473. $in = Db::name("warehouse_info")->insert($inwsm);
  474. if ($in == false) return false;
  475. } else {
  476. $wsm_code = $wsm['wsm_code'];
  477. }
  478. //判断该供应商是否开通了供应商账号
  479. $from_tag=checkHasAccountBySupplierNos([$data['supplierNo']]);
  480. $cg = [
  481. "cgdNo" => $cgdCode,
  482. "bkcode" => "",
  483. "wsm_code" => $wsm_code,
  484. "cgder" => $data['creater'],
  485. "cgder_id" => $data['createrid'],
  486. "spuCode" => $data['spuCode'],
  487. "skuCode" => $data['skuCode'],
  488. "good_name" => $data['good_name'],
  489. "good_num" => $data['good_num'],
  490. "good_price" => $data['sale_price'],
  491. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  492. "pakge_fee" => $data['pakge_fee'],
  493. "cert_fee" => $data['cert_fee'],
  494. "open_fee" => $data['open_fee'],
  495. "teach_fee" => $data['cost_fee'],
  496. "mark_fee" => $data['mark_fee'],
  497. "demo_fee" => $data['demo_fee'],
  498. "nake_fee" => $data['nake_fee'],
  499. "delivery_fee" => $data['delivery_fee'],
  500. "weight"=>$data['weight'],
  501. "diff_weight" => "0",
  502. "diff_fee" => "0",
  503. "gold_price" => $data['gold_price'],
  504. "supplierNo" => $data['supplierNo'],
  505. "supplier_name" => $supplier['name'],
  506. "companyNo" => $data['companyNo'],
  507. "send_status" => 1,
  508. "send_num" => 0,
  509. "wsend_num" => $data['good_num'],
  510. "remark" => "",
  511. "status" => 0,//0初始化
  512. "lasttime" => date("Y-m-d H:i:s"),
  513. "is_del" => 0,
  514. "order_type" => $data['order_type'],
  515. "order_source" => $data['order_source'],
  516. "good_type" => $data['good_type'],
  517. "addtime" => date("Y-m-d H:i:s"),
  518. "updatetime" => date("Y-m-d H:i:s"),
  519. 'good_createrid' => $data['good_createrid'],
  520. 'good_creater' => $data['good_createrid'],//商品创建人
  521. 'from_tag' => isset($from_tag[$data['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  522. ];
  523. $up = Db::name("purchease_order")->insert($cg, true);
  524. if ($up) {
  525. //修改状态,添加待办
  526. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  527. "order_code" => $cg['cgdNo'],//销售单code
  528. "status" => 0,//这里的status是之前的值
  529. "action_remark" => '',//备注
  530. "action_type" => "create"//新建create,编辑edit,更改状态status
  531. ], "CGD", $cg['status'], $cg);
  532. //当采购单是节点0待与供应商确认,推给供应商负责人
  533. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  534. "order_type" => 'CGD',
  535. "order_code" => $cg['cgdNo'],//销售单code
  536. "order_id" => $up,
  537. "order_status" => $cg['status'],
  538. "before_status" => 0,
  539. 'holder_id' => $supplier['personid'],
  540. 'wait_id'=>$supplier['personid'],
  541. 'wait_name'=>$supplier['person'],
  542. ]);
  543. $standing_book_data['cgdNo'] = $cgdCode;
  544. $rela = [
  545. "orderCode" => $data['orderCode'],
  546. "cgdNo" => $cgdCode,
  547. "spuCode" => $data['spuCode'],
  548. "good_num" => $data['good_num'],
  549. "wsend_num" => $data['good_num'],
  550. "send_num" => 0,
  551. "wait_num" => 0,
  552. "status" => 1,
  553. "source" => 2
  554. ];
  555. $re = Db::name("order_num")->save($rela);
  556. if ($re == false) return false;
  557. else {
  558. $stokc = Db::name("good_stock")
  559. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  560. ->find();
  561. if ($stokc == false) {
  562. $stokc = [
  563. "spuCode" => $data['spuCode'],
  564. "wsm_code" => $wsm_code,
  565. "wait_in_stock" => $data['good_num'],
  566. "wait_out_stock" => 0,
  567. "usable_stock" => 0,
  568. "intra_stock" => 0,
  569. "total_stock" => 0,
  570. "status" => 1,
  571. "addtime" => date("Y-m-d H:i:s"),
  572. "updatetime" => date("Y-m-d H:i:s")
  573. ];
  574. } else {
  575. $stokc['wait_in_stock'] += $data['good_num'];
  576. $stokc['updatetime'] = date("Y-m-d H:i:s");
  577. }
  578. $stoc = Db::name("good_stock")->save($stokc);
  579. if ($stoc == false) {
  580. return false;
  581. }
  582. $good_data=[];
  583. $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"];
  584. GoodLog::LogAdd(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
  585. $this->cgd_data = $cg;
  586. return true;
  587. }
  588. } else return false;
  589. }
  590. //创建.....
  591. private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
  592. {
  593. //备库单
  594. $cgd = Db::name("order_bk")
  595. ->where([
  596. ["spuCode", "=", $outinfo['spuCode']],
  597. ["is_del", "=", 0],
  598. ["balance_num", ">=", $outinfo['good_num']],
  599. ['companyNo', "=", $outinfo['companyNo']]
  600. ])
  601. // ->lock(true)
  602. ->find();
  603. if ($cgd == false) return false;
  604. $cgdinfo = Db::name("purchease_order")
  605. ->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])
  606. ->find();
  607. if ($cgdinfo == false) return false;
  608. //判断该供应商是否开通了供应商账号
  609. $from_tag=checkHasAccountBySupplierNos([$cgdinfo['supplierNo']]);
  610. $QrdCgd = [
  611. "cgdNo" => makeNo("CG"),
  612. "bkcode" => $cgdinfo['bkcode'],
  613. 'wsm_code' => $cgdinfo['wsm_code'],
  614. "cgder_id" => $cgdinfo['cgder_id'],
  615. "cgder" => $cgdinfo['cgder'],
  616. "spuCode" => $cgdinfo['spuCode'],
  617. "good_name" => $cgdinfo['good_name'],
  618. "good_num" => $outinfo['good_num'],
  619. "good_price" => $cgdinfo['good_price'],
  620. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  621. "pakge_fee" => $cgdinfo['pakge_fee'],
  622. "cert_fee" => $cgdinfo['cert_fee'],
  623. "open_fee" => $cgdinfo['open_fee'],
  624. "delivery_fee" => $cgdinfo['delivery_fee'],
  625. "mark_fee" => $cgdinfo['mark_fee'],
  626. "teach_fee" => $cgdinfo['teach_fee'],
  627. "nake_fee" => $cgdinfo['nake_fee'],
  628. "demo_fee" => $cgdinfo['demo_fee'],
  629. "weight" => $cgdinfo['weight'],
  630. "diff_weight" => $cgdinfo['diff_weight'],
  631. "diff_fee" => $cgdinfo['diff_fee'],
  632. "gold_price" => $cgdinfo['gold_price'],
  633. "supplierNo" => $cgdinfo['supplierNo'],
  634. "supplier_name" => $cgdinfo['supplier_name'],
  635. "companyNo" => $cgdinfo['companyNo'],
  636. "send_status" => 3,
  637. "send_num" => $outinfo['good_num'],
  638. "wsend_num" => 0,
  639. "remark" => $cgdinfo['remark'],
  640. "status" => 3,//入库完成
  641. "lasttime" => $cgdinfo['lasttime'],
  642. "is_del" => 0,
  643. "order_type" => $outinfo['order_type'],
  644. "order_source" => $outinfo['order_source'],
  645. "good_type" => $cgdinfo['good_type'],
  646. "addtime" => date("Y-m-d H:i:s"),
  647. "updatetime" => date("Y-m-d H:i:s"),
  648. 'good_createrid' => $outinfo['good_createrid'],
  649. 'good_creater' => $outinfo['good_creater'],//商品创建人
  650. 'from_tag' => isset($from_tag[$cgdinfo['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  651. ];
  652. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  653. if ($insetrCgd == false) return false;
  654. $standing_book_data = array_merge($standing_book_data, [
  655. 'cgdNo' => $QrdCgd['cgdNo'],
  656. 'bk_code' => $QrdCgd['bkcode'],
  657. ]);
  658. $merge_num = Db::name("purchease_order")
  659. ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
  660. ->where('order_source', '<>', 0)//0备库单不计算在内
  661. ->field("sum(send_num)-sum(th_num) as num")
  662. ->find();
  663. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  664. $cgd['merge_num'] = $merge_num['num'];
  665. $cgd['updatetime'] = date("Y-m-d H:i:s");
  666. $up = Db::name("order_bk")->save($cgd);
  667. if ($up == false) return false;
  668. $data = [
  669. "orderCode" => $outinfo['orderCode'],
  670. "cgdNo" => $QrdCgd['cgdNo'],
  671. "spuCode" => $outinfo['spuCode'],
  672. "companyNo" => $outinfo['companyNo'],
  673. "good_num" => $outinfo['good_num'],
  674. "wsend_num" => $outinfo['good_num'],
  675. "send_num" => 0,
  676. "wait_num" => $outinfo['good_num'],
  677. "status" => 1,
  678. "source" => 1,
  679. ];
  680. $order = Db::name("order_num")->save($data);
  681. if ($order == false) return false;
  682. $this->cgd_data = $QrdCgd;
  683. return true;
  684. }
  685. }