ImportOrderFromCHandleData.php 36 KB

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