ImportOrderFromCHandleData.php 36 KB

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