handleYzOrderData.php 37 KB

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