handleYzOrderData.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php declare (strict_types=1);
  2. namespace app\command;
  3. use app\admin\common\User;
  4. use app\admin\model\ActionLog;
  5. use app\admin\model\GoodLog;
  6. use app\admin\model\GoodStockInfo;
  7. use app\admin\model\OrderOutChild;
  8. use app\admin\model\OrderTax;use app\admin\model\ProcessOrder;
  9. use app\admin\model\SaleCgdPrice;use think\console\Command;
  10. use think\console\Input;
  11. use think\console\Output;
  12. use think\Exception;
  13. use think\facade\Cache;
  14. use think\facade\Config;
  15. use think\facade\Db;
  16. //处理有赞订单数据
  17. class handleYzOrderData extends Command
  18. {
  19. private $cgd_data = [];//存储采购单的数据,目前是为了做供应商api推送而准备的
  20. protected function configure()
  21. {
  22. // 指令配置
  23. $this->setName('handle_yz_order_data')->setDescription('处理有赞订单数据');
  24. }
  25. //处理有赞订单数据
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $limit = Cache::store('redis')->get("YzTimeLimit");
  29. if($limit==1){
  30. return '' ;
  31. }
  32. Cache::store('redis')->set("YzTimeLimit",1,60);
  33. $c_data = Cache::store('redis')->handler()->rpop(Config::get('app.handle_queue_key'));
  34. if ($c_data){
  35. $c_data = json_decode($c_data, true);
  36. Db::startTrans();
  37. try {
  38. $standing_book_data = [];
  39. $orderCode = makeNo("QR");
  40. //企业客户
  41. $ct = Db::name('platform_youzan')
  42. ->alias('py')
  43. ->field('py.id,py.skuCode,py.spuCode,py.plat_code,p.platform_name,py.sale_price,g.*,py.platform_id')
  44. ->leftJoin('platform p', 'p.id=py.platform_id AND p.is_del=0')
  45. ->leftJoin('good g', 'g.spuCode=py.spuCode AND g.is_del=0')
  46. ->where('py.skuCode', $c_data['skuCode'])
  47. ->where('py.exam_status', 6)//exam_status==3已上线
  48. ->find();
  49. if ($ct) {
  50. $customer_code = Db::connect('mysql_sys')
  51. ->name('customer_info')->alias("a")
  52. ->leftJoin("customer_org1 b" ,"a.itemid=b.id and b.is_del=0")
  53. ->where(['companyName' => $ct['platform_name'] . '客户', "b.name"=>$ct['platform_name'], 'a.is_del' => 0])
  54. ->value('companyNo', '');
  55. if (!$customer_code) throw new Exception('在企业客户管理组织中找不到对应的企业');
  56. } else throw new Exception('未找到商品数据');
  57. $supplierNo = $ct['companyNo'];
  58. $goodtype = 1;//1正常商品
  59. $sendtype = 1;//直接发货
  60. $platform_id = $ct['platform_id'];
  61. $platform_order = $c_data['tid'];
  62. $good_num = $c_data['num'];
  63. $arrtime = $paytime = $c_data['addtime'];
  64. $goodinfo = $ct;
  65. $is_stock = $ct['is_stock'];
  66. $order_type = $is_stock == 1 ? 1 : 2;//1备库(库存品)2非库存品
  67. $order_source = 5;//有赞
  68. $spuCode = $ct['spuCode'];
  69. $skuCode = $ct['skuCode'];
  70. $origin = Db::name('good_nake')
  71. ->where([['spuCode', '=', $spuCode], ['min_num', '<=', $good_num], ['is_del', '=', 0]])
  72. ->order('min_num desc')
  73. ->find();
  74. $origin_price = $goodinfo['is_stock'] == 1?0:($origin['nake_total']??0);
  75. $cgd_origin_price = $origin['supplier_origin_price']??0;
  76. if ($is_stock == 1) {
  77. if($ct['usable_stock'] < $good_num){
  78. throw new Exception('商品库存数量不足');
  79. }
  80. $origin_price = 0;
  81. }else{
  82. if ($origin == false) throw new \Exception('未找到相关成本价格');
  83. }
  84. $is_activity = 0;
  85. $sale_price = $c_data['discount_price'];
  86. $actCode='';
  87. if (isset($c_data['coupon']['actCode']) && $c_data['coupon']['actCode']!="") {
  88. //如果是泰行销的单子,要取原始价格(即优惠前的价格)
  89. $is_activity=2;//txx活动标识
  90. $actCode=$c_data['coupon']['actCode'];
  91. $sale_price = $c_data['coupon']['cent_value']/100;
  92. }
  93. $act = check_activity((string)$spuCode, (string)$skuCode, (int)$platform_id, (int)$is_stock, (float)$sale_price, (int)$good_num);
  94. if (!empty($act)) {
  95. $is_activity = 1;
  96. $sale_price = $act['final_price'];//取活动的相关价格
  97. }else{
  98. if ($sale_price < $ct['sale_price'] && $c_data['is_approved'] == '0') {
  99. Db::rollback();
  100. $this->callbackYz(['status' => 2, 'id' => $c_data['id'], 'reason' => '价格低于系统售价' . $ct['sale_price']]);
  101. Cache::store('redis')->set("YzTimeLimit",0);
  102. return false;//停止执行后续代码
  103. }
  104. }
  105. $remark = $c_data['buyer_messages'];
  106. $user = Config::get('app.uid_platform_youzan.' . $ct['platform_id']);
  107. $rm = isset($user['id']) ? $user['id'] : 0;
  108. $ri = isset($user['nickname']) ? $user['nickname'] : 'youzan';
  109. if($rm==0)throw new Exception('平台转单未找到对应的平台账户');
  110. $userCommon = User::getIns();
  111. $tmp = $userCommon->handle('sInfo',['code'=>$ct['supplierNo']]);
  112. $supplier_temp_info = $tmp['data']??[];
  113. $names = $userCommon->handle('getCodeAndName',['code'=>[$goodinfo['companyNo']]]);
  114. $data = [
  115. "orderCode" => $orderCode,
  116. "good_code" => $spuCode,
  117. "skuCode" => $skuCode,
  118. "customer_code" => $customer_code,
  119. "customerName" => $ct['platform_name'] . '客户',
  120. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  121. "good_num" => $good_num,
  122. "cat_id" => $goodinfo['cat_id'],
  123. "apply_id" => $rm,
  124. "apply_name" => $ri,
  125. "origin_price" => $origin_price,
  126. "sale_price" => $sale_price,
  127. "post_fee" => 0,
  128. "status" => 0,
  129. "send_num" => 0,
  130. "wsend_num" => $good_num,
  131. "send_status" => 1,
  132. "good_type" => $goodtype,
  133. "send_type" => $sendtype,
  134. "supplierNo" => $goodinfo['companyNo'],
  135. "supplierName" => $names['data'][$goodinfo['companyNo']]??"",
  136. 'supNo'=>$goodinfo['supplierNo'],
  137. 'supName'=>$supplier_temp_info['name'],
  138. "is_del" => 0,
  139. "zxNo" => "",
  140. "platform_order" => $platform_order,
  141. "platform_id" => $platform_id,
  142. "remark" => $remark,
  143. "is_stock" => $is_stock,
  144. "is_activity" => $is_activity,
  145. 'activity_code' => $is_activity==1 ? $act['activity_code'] : ($is_activity==2?$actCode:""),
  146. "order_type" => $order_type,
  147. "order_source" => $order_source,
  148. 'open_type'=>$goodinfo['open_type'],
  149. 'good_weight' => $ct['weight'],
  150. 'gold_price' => $ct['cgd_gold_price'],
  151. 'cost_price' => $ct['cost_fee'],
  152. 'diff_weight' => 0,
  153. 'diff_fee' => 0,
  154. "workNo" => '',
  155. "addtime" => date("Y-m-d H:i:s"),
  156. "updatetime" => date("Y-m-d H:i:s"),
  157. 'total_price' => bcmul((string)$sale_price, (string)$good_num, 2),
  158. 'cgderid' => $supplier_temp_info['personid']??'',
  159. 'cgder' => $supplier_temp_info['person']??'',//采购员(供应商负责人)
  160. 'good_createrid' => $goodinfo['createrid'],
  161. 'good_creater' => $goodinfo['creater'],//商品创建人
  162. 'manager'=>$ri,
  163. 'managerid'=>$rm,
  164. ];
  165. $paytime == "" ? "" : $data['paytime'] = $paytime;
  166. $datainfo = Db::name('sale')->insertGetId($data);
  167. if ($datainfo > 0) {
  168. if ($is_activity == 1) {
  169. $actup = [
  170. "activity_stock" => $act['activity_stock'] - $good_num,
  171. "updatetime" => date("Y-m-d H:i:s")
  172. ];
  173. $actupp = Db::name("activity_info")
  174. ->where('id', $act['id'])
  175. ->save($actup);
  176. if ($actupp == false) throw new Exception('活动库存修改失败');
  177. }
  178. //补充台账数据
  179. $standing_book_data = array_merge($standing_book_data, [
  180. 'orderCode' => $orderCode,
  181. 'spuCode' => $data['good_code'],
  182. 'skuCode' => $data['skuCode'],
  183. 'order_type' => $data['order_type'],
  184. 'order_source' => $data['order_source'],
  185. 'supplierNo' => $data['supplierNo'],
  186. 'companyNo' => $ct['companyNo'],
  187. 'customer_code' => $data['customer_code'],
  188. ]);
  189. if ($is_stock == 0) {
  190. $cgd = [
  191. "supplierNo" => $ct['supplierNo'],
  192. "supplier_name" => $supplier_temp_info['name'],
  193. "companyNo" => $supplierNo,
  194. "companyName" => $names['data'][$supplierNo]??'',
  195. "orderCode" => $orderCode,
  196. "spuCode" => $spuCode,
  197. "skuCode" => $skuCode,
  198. "good_name" => $ct['good_name'],
  199. "sale_price" => $origin_price,
  200. "total_fee" => $origin_price * $good_num,
  201. "origin_total" => $origin_price * $good_num,
  202. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  203. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  204. "open_fee" => $ct['open_fee'],
  205. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  206. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  207. "demo_fee" => $ct['demo_fee'],
  208. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  209. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  210. "good_num" => $good_num,
  211. "good_type" => $goodtype,
  212. "open_type"=>$goodinfo['open_type'],
  213. "order_type" => $order_type,
  214. "order_source" => $order_source,
  215. "createrid" =>$supplier_temp_info['personid']??'',
  216. "creater" =>$supplier_temp_info['person']??'',
  217. 'send_way' => 2,
  218. 'gold_price' => $ct['cgd_gold_price'],
  219. 'good_createrid' => $goodinfo['createrid'],
  220. 'good_creater' => $goodinfo['creater'],//商品创建人
  221. 'weight' => $ct['noble_weight'],//商品创建人
  222. 'supplier_origin_price'=>$cgd_origin_price,//供应商采购成本
  223. 'cgd_supplier_code'=>$goodinfo['cgd_supplier_code'],//采购供应商编号
  224. 'cgd_supplier_name'=>$goodinfo['cgd_supplier_name'],//采购供应商编号
  225. 'cgd_apply_id'=>$goodinfo['cgd_apply_id'],//竞单人
  226. 'cgd_apply_name'=>$goodinfo['cgd_apply_name'],//竞单人
  227. ];
  228. //非库存品
  229. $bol = $this->createCgd($cgd, $rm, $ri, $standing_book_data);
  230. if ($bol == false) throw new Exception('订单创建失败');
  231. } else {
  232. $stockUp =[
  233. "usable_stock"=>$ct['usable_stock']-$good_num,
  234. "updatetime"=>date("Y-m-d H:i:s"),
  235. ];
  236. $update=Db::name("good")->where(["spuCode"=>$ct['spuCode'],"usable_stock"=>$ct['usable_stock']])->update($stockUp);
  237. if($update==false){
  238. throw new Exception('商品更新可用库存失败');
  239. }
  240. SaleCgdPrice::GetPrice($datainfo);
  241. }
  242. $taxInfo = OrderTax::SpuCat( $spuCode);
  243. if (!empty($taxInfo)) {
  244. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($orderCode){
  245. $temp=[];
  246. $temp['code']=$orderCode;
  247. $temp['spuCode']=$item['spuCode'];
  248. $temp['tax_code']=$item['cat_code'];
  249. $temp['tax_name']=$item['cat_name'];
  250. $temp['tax_short_name']=$item['short_name'];
  251. $temp['inv_tax']=$item['tax'];
  252. $temp['inv_good_name']=$item['inv_good_name'];
  253. return $temp;
  254. },$taxInfo));
  255. }
  256. if ($sendtype == 1) {
  257. // $p_c_a = get_address($c_data['delivery_province'] . $c_data['delivery_city'] . $c_data['delivery_district']);
  258. $province = Db::name('province')
  259. ->where('name', $c_data['delivery_province'])
  260. ->value('province_code', '');
  261. //地级市可能有重复的,例如'省直辖县'
  262. $city = Db::name('city')
  263. ->where('name', $c_data['delivery_city'])
  264. ->where('province_code', $province)
  265. ->value('city_code', '');
  266. //县区名称也有可能重复,例如'桥东区'
  267. $area = Db::name('area')
  268. ->where('name', $c_data['delivery_district'])
  269. ->where('city_code', $city)
  270. ->value('area_code', '');
  271. $addr_code = implode([$province, $city, $area], ',');
  272. $temp = [
  273. 'orderCode' => $orderCode,
  274. 'contactor' => $c_data['receiver_name'],
  275. 'mobile' => $c_data['receiver_tel'],
  276. 'addr' => $c_data['delivery_address'],
  277. 'addr_code' => $addr_code,
  278. 'customer_code' => $customer_code,
  279. 'receipt_quantity' => $c_data['num'],//收货数量,
  280. 'post_fee' => $c_data['post_fee'],
  281. 'is_del' => 0,
  282. 'addtime' => date("Y-m-d H:i:s"),
  283. 'updatetime' => date("Y-m-d H:i:s"),
  284. 'arrive_time' => $arrtime,
  285. ];
  286. $vmp = Db::name('order_addr')->insertGetId($temp);
  287. if ($vmp > 0) {
  288. $num = $c_data['num'];
  289. $outCode = makeNo("DF");
  290. if ($is_stock != 1) {
  291. $order = Db::name("order_num")
  292. ->where(["orderCode" => $orderCode, "status" => 1])
  293. ->find();
  294. if ($order == false) throw new Exception('未找到可以发货得采购单数据');
  295. $tep = [
  296. "cgdNo" => $order['cgdNo'],
  297. "outCode" => $outCode,
  298. "send_num" => $num,
  299. "status" => 1,
  300. "addtime" => date("Y-m-d H:i:s"),
  301. "updatetime" => date("Y-m-d H:i:s")
  302. ];
  303. $sen = Db::name("order_send")->save($tep);
  304. if ($sen == false) throw new Exception('发货地址添加创建失败');
  305. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  306. if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
  307. }
  308. $out = [
  309. "orderCode" => $orderCode,
  310. "outCode" => $outCode,
  311. "apply_id" => $rm,
  312. "apply_name" => $ri,
  313. "addrid" => $vmp,
  314. "post_name" => "",
  315. "post_code" => "",
  316. "post_fee" => 0,
  317. "sendtime" => date("Y-m-d H:i:s"),
  318. "send_num" => $num,
  319. "check_num" => 0,
  320. "error_num" => 0,
  321. "wsm_code" => $is_stock==1? '':$cgdinfo['wsm_code'],
  322. "order_type" => $order_type,
  323. "status" => $is_stock == 1 ? 1 : 0,
  324. "send_status" => $is_stock == 1 ? 1 : 0,
  325. "addtime" => date("Y-m-d H:i:s"),
  326. "updatetime" => date("Y-m-d H:i:s")
  327. ];
  328. $ou = Db::name("order_out")->insertGetId($out);
  329. if ($ou == false) throw new Exception('发货地址添加创建失败');
  330. else {
  331. if ($is_stock == 1) OrderOutChild::makeChild($outCode);
  332. //修改状态,添加待办
  333. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  334. "order_code" => $outCode,//出库单号
  335. "status" => 0,//这里的status是之前的值
  336. "action_remark" => '',//备注
  337. "action_type" => "create"//新建create,编辑edit,更改状态status
  338. ], "CKD", 0, $out);
  339. if ($out['status'] == 1) {
  340. if ($is_stock == 1) {
  341. //库存品,待办数据推给库管
  342. $uids = Db::name('user_role')
  343. ->where(['is_del' => 0, 'roleid' => config('app.wsm_cgder_role'), 'status' => 1])
  344. ->column('uid');
  345. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  346. "order_type" => 'CKD',
  347. "order_code" => $outCode,//出库单号
  348. "order_id" => $ou,
  349. "order_status" => $out['status'],
  350. "before_status" => 0,
  351. 'holder_id' => $data['apply_id'],
  352. 'handle_user_list' => implode(',', $uids),
  353. ]);
  354. } else {
  355. // //非库存品推给供应商负责人
  356. // $supp = Db::name('supplier')
  357. // ->field('person,personid')
  358. // ->where('code', $ct['supplierNo'])
  359. // ->findOrEmpty();
  360. $supp = $userCommon->handle('sInfo',['code'=>$ct['supplierNo']]);
  361. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  362. "order_type" => 'CKD',
  363. "order_code" => $outCode,//出库单号
  364. "order_id" => $ou,
  365. "order_status" => $out['status'],
  366. "before_status" => 0,
  367. 'holder_id' => $data['apply_id'],
  368. 'wait_id' => $supp['data']['personid'],
  369. 'wait_name' => $supp['data']['person'],
  370. ]);
  371. }
  372. }else ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  373. "order_type" => 'CKD',
  374. "order_code" => $outCode,//出库单号
  375. "order_id" => $ou,
  376. "order_status" => $out['status'],
  377. "before_status" => 0,
  378. 'holder_id'=>$data['apply_id']
  379. ]);
  380. // $standing_book_data['outCode'] = $outCode;
  381. }
  382. } else throw new Exception('发货地址添加失败');
  383. }
  384. }
  385. //维护台账记录
  386. 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")]));
  387. //回调更新有赞订单数据
  388. $callback_rs = $this->callbackYz(['status' => 6, 'id' => $c_data['id'], 'orderCode' => $orderCode]);
  389. if ($callback_rs['code'] != 0) throw new \Exception($callback_rs['message']);
  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. }
  407. Db::commit();
  408. Cache::store('redis')->set("YzTimeLimit",0);
  409. $output->writeln(date('Y-m-d H:i:s') . '|处理成功,orderCode:' . $orderCode);
  410. } catch (\Exception $exception) {
  411. Db::rollback();
  412. $message[] = $exception->getMessage();
  413. //回调更新有赞订单数据
  414. $callback_rs = $this->callbackYz(['status' => 8, 'id' => $c_data['id'], 'reason' => $exception->getMessage()]);
  415. if ($callback_rs['code'] != 0) $message[] = $callback_rs['message'];
  416. Cache::store('redis')->set("YzTimeLimit",0);
  417. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . implode(',', $message));
  418. }
  419. }
  420. Cache::store('redis')->set("YzTimeLimit",0,60);
  421. //没有需要处理的数据
  422. }
  423. //创建采购单
  424. private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
  425. {
  426. $cgdCode = makeNo("CG");
  427. $wsm = Db::name("warehouse_info")
  428. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  429. ->find();
  430. if ($wsm == false) {
  431. $wsm_code = makeNo("WSM");
  432. $inwsm = [
  433. "wsm_code" => $wsm_code,
  434. "name" => $data['supplier_name'],
  435. "wsm_type" => 2,
  436. "supplierNo" => $data['supplierNo'],
  437. "addr" => "",
  438. "addrs_code" => "",
  439. "contactor" => 0,
  440. "contactor_name" => 0,
  441. "mobile" => "",
  442. "position" => "",
  443. "companyNo" => $data['companyNo'],
  444. "status" => 1,
  445. "is_del" => 0,
  446. "addtime" => date("Y-m-d H:i:s"),
  447. "updatetime" => date("Y-m-d H:i:s")
  448. ];
  449. $in = Db::name("warehouse_info")->insert($inwsm);
  450. if ($in == false) return false;
  451. } else {
  452. $wsm_code = $wsm['wsm_code'];
  453. }
  454. //判断该供应商是否开通了供应商账号
  455. $from_tag=checkHasAccountBySupplierNos([$data['supplierNo']]);
  456. $cg = [
  457. "cgdNo" => $cgdCode,
  458. "bkcode" => "",
  459. "wsm_code" => $wsm_code,
  460. "cgder" => $data['creater'],
  461. "cgder_id" => $data['createrid'],
  462. "spuCode" => $data['spuCode'],
  463. "skuCode" => $data['skuCode'],
  464. "good_name" => $data['good_name'],
  465. "good_num" => $data['good_num'],
  466. "good_price" => $data['sale_price'],
  467. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  468. "origin_total" => round($data['sale_price'] * $data['good_num'], 2),
  469. "pakge_fee" => $data['pakge_fee'],
  470. "cert_fee" => $data['cert_fee'],
  471. "open_fee" => $data['open_fee'],
  472. "teach_fee" => $data['cost_fee'],
  473. "mark_fee" => $data['mark_fee'],
  474. "demo_fee" => $data['demo_fee'],
  475. "nake_fee" => $data['nake_fee'],
  476. "delivery_fee" => $data['delivery_fee'],
  477. "weight"=>$data['weight'],
  478. "diff_weight" => "0",
  479. "diff_fee" => "0",
  480. "gold_price" => $data['gold_price'],
  481. "supplierNo" => $data['supplierNo'],
  482. "supplier_name" => $data['supplier_name'],
  483. "companyNo" => $data['companyNo'],
  484. "companyName" => $data['companyName'],
  485. "send_status" => 1,
  486. "send_num" => 0,
  487. "wsend_num" => $data['good_num'],
  488. "remark" => "",
  489. "status" => 0,//0初始化
  490. "lasttime" => date("Y-m-d H:i:s"),
  491. "is_del" => 0,
  492. "order_type" => $data['order_type'],
  493. "open_type"=>$data['open_type'],
  494. "order_source" => $data['order_source'],
  495. "good_type" => $data['good_type'],
  496. "addtime" => date("Y-m-d H:i:s"),
  497. "updatetime" => date("Y-m-d H:i:s"),
  498. 'good_createrid' => $data['good_createrid'],
  499. 'good_creater' => $data['good_creater'],//商品创建人
  500. 'from_tag' => isset($from_tag[$data['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  501. 'supplier_origin_price'=>$data['supplier_origin_price'],//供应商采购成本
  502. 'cgd_supplier_code'=>$data['cgd_supplier_code'],//采购供应商编号
  503. 'cgd_supplier_name'=>$data['cgd_supplier_name'],//采购供应商编号
  504. 'cgd_apply_id'=>$data['cgd_apply_id'],//竞单人
  505. 'cgd_apply_name'=>$data['cgd_apply_name'],//竞单人
  506. ];
  507. $up = Db::name("purchease_order")->insert($cg, true);
  508. if ($up) {
  509. $taxInfo = OrderTax::SpuCat( $data['spuCode'],1,1);
  510. if (!empty($taxInfo)) {
  511. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($cgdCode){
  512. $temp=[];
  513. $temp['code']=$cgdCode;
  514. $temp['spuCode']=$item['spuCode'];
  515. $temp['tax_code']=$item['cat_code'];
  516. $temp['tax_name']=$item['cat_name'];
  517. $temp['tax_short_name']=$item['short_name'];
  518. $temp['inv_tax']=$item['tax'];
  519. $temp['inv_good_name']=$item['inv_good_name'];
  520. return $temp;
  521. },$taxInfo));
  522. }
  523. //修改状态,添加待办
  524. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  525. "order_code" => $cg['cgdNo'],//销售单code
  526. "status" => 0,//这里的status是之前的值
  527. "action_remark" => '',//备注
  528. "action_type" => "create"//新建create,编辑edit,更改状态status
  529. ], "CGD", $cg['status'], $cg);
  530. //当采购单是节点0待与供应商确认,推给供应商负责人
  531. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  532. "order_type" => 'CGD',
  533. "order_code" => $cg['cgdNo'],//销售单code
  534. "order_id" => $up,
  535. "order_status" => $cg['status'],
  536. "before_status" => 0,
  537. 'holder_id'=>$data['createrid'],
  538. 'wait_id'=>$data['createrid'],
  539. 'wait_name'=>$data['creater'],
  540. ]);
  541. $standing_book_data['cgdNo'] = $cgdCode;
  542. $rela = [
  543. "orderCode" => $data['orderCode'],
  544. "cgdNo" => $cgdCode,
  545. "spuCode" => $data['spuCode'],
  546. "good_num" => $data['good_num'],
  547. "wsend_num" => $data['good_num'],
  548. "send_num" => 0,
  549. "wait_num" => 0,
  550. "status" => 1,
  551. "source" => 2
  552. ];
  553. $re = Db::name("order_num")->save($rela);
  554. if ($re == false) return false;
  555. else {
  556. $stokc = Db::name("good_stock")
  557. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  558. ->find();
  559. if ($stokc == false) {
  560. $stokc = [
  561. "spuCode" => $data['spuCode'],
  562. "wsm_code" => $wsm_code,
  563. "wait_in_stock" => $data['good_num'],
  564. "wait_out_stock" => 0,
  565. "usable_stock" => 0,
  566. "intra_stock" => 0,
  567. "total_stock" => 0,
  568. "status" => 1,
  569. "addtime" => date("Y-m-d H:i:s"),
  570. "updatetime" => date("Y-m-d H:i:s")
  571. ];
  572. } else {
  573. $stokc['wait_in_stock'] += $data['good_num'];
  574. $stokc['updatetime'] = date("Y-m-d H:i:s");
  575. }
  576. $stoc = Db::name("good_stock")->save($stokc);
  577. if ($stoc == false) return false;
  578. $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"];
  579. GoodLog::LogAdd(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
  580. $this->cgd_data = $cg;
  581. return true;
  582. }
  583. } else return false;
  584. }
  585. //创建.....
  586. // private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
  587. // {
  588. // //备库单
  589. // $cgd = Db::name("order_bk")
  590. // ->where([
  591. // ["spuCode", "=", $outinfo['spuCode']],
  592. // ["is_del", "=", 0],
  593. // ["balance_num", ">=", $outinfo['good_num']],
  594. // ['companyNo', "=", $outinfo['companyNo']]
  595. // ])
  596. // ->lock(true)
  597. // ->find();
  598. //
  599. // if ($cgd == false) return false;
  600. //
  601. // $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->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. // "send_status" => 3,
  634. // "send_num" => $outinfo['good_num'],
  635. // "wsend_num" => 0,
  636. // "remark" => $cgdinfo['remark'],
  637. // "status" => 3,//入库完成
  638. // "lasttime" => $cgdinfo['lasttime'],
  639. // "is_del" => 0,
  640. // "order_type" => $outinfo['order_type'],
  641. // "order_source" => $outinfo['order_source'],
  642. // "good_type" => $cgdinfo['good_type'],
  643. // "addtime" => date("Y-m-d H:i:s"),
  644. // "updatetime" => date("Y-m-d H:i:s"),
  645. // 'good_createrid' => $outinfo['good_createrid'],
  646. // 'good_creater' => $outinfo['good_creater'],//商品创建人
  647. // 'from_tag' => isset($from_tag[$cgdinfo['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  648. // ];
  649. // $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  650. // if ($insetrCgd == false) return false;
  651. //
  652. // $standing_book_data = array_merge($standing_book_data, [
  653. // 'cgdNo' => $QrdCgd['cgdNo'],
  654. // 'bk_code' => $QrdCgd['bkcode'],
  655. // ]);
  656. //
  657. // $merge_num = Db::name("purchease_order")
  658. // ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
  659. // ->where('order_source', '<>', 0)//0备库单不计算在内
  660. // ->field("sum(send_num)-sum(th_num) as num")
  661. // ->find();
  662. // $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  663. // $cgd['merge_num'] = $merge_num['num'];
  664. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  665. // $up = Db::name("order_bk")->save($cgd);
  666. // if ($up == false) return false;
  667. //
  668. // $data = [
  669. // "orderCode" => $outinfo['orderCode'],
  670. // "cgdNo" => $QrdCgd['cgdNo'],
  671. // "spuCode" => $outinfo['spuCode'],
  672. // "companyNo" => $outinfo['companyNo'],
  673. // "good_num" => $outinfo['good_num'],
  674. // "wsend_num" => $outinfo['good_num'],
  675. // "send_num" => 0,
  676. // "wait_num" => $outinfo['good_num'],
  677. // "status" => 1,
  678. // "source" => 1,
  679. // ];
  680. // $order = Db::name("order_num")->save($data);
  681. // if ($order == false) return false;
  682. //
  683. // $this->cgd_data = $QrdCgd;
  684. //
  685. // return true;
  686. // }
  687. //与有赞项目回调
  688. private function callbackYz(array $data = [])
  689. {
  690. $host = Config::get('app.yz_domain');
  691. $response = curl_request($host . 'api/callback_order_by_stock', $data);
  692. return json_decode($response, true);
  693. }
  694. }