ImportOrderFromCHandleData.php 37 KB

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