ImportOrderFromCHandleData.php 35 KB

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