handleYzOrderData.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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);
  241. if (!empty($taxInfo)) {
  242. (new \app\admin\model\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['tax_short_name']=$item['short_name'];
  249. $temp['inv_tax']=$item['tax'];
  250. $temp['inv_good_name']=$item['inv_good_name'];
  251. return $temp;
  252. },$taxInfo));
  253. }
  254. if ($sendtype == 1) {
  255. // $p_c_a = get_address($c_data['delivery_province'] . $c_data['delivery_city'] . $c_data['delivery_district']);
  256. $province = Db::name('province')
  257. ->where('name', $c_data['delivery_province'])
  258. ->value('province_code', '');
  259. //地级市可能有重复的,例如'省直辖县'
  260. $city = Db::name('city')
  261. ->where('name', $c_data['delivery_city'])
  262. ->where('province_code', $province)
  263. ->value('city_code', '');
  264. //县区名称也有可能重复,例如'桥东区'
  265. $area = Db::name('area')
  266. ->where('name', $c_data['delivery_district'])
  267. ->where('city_code', $city)
  268. ->value('area_code', '');
  269. $addr_code = implode([$province, $city, $area], ',');
  270. $temp = [
  271. 'orderCode' => $orderCode,
  272. 'contactor' => $c_data['receiver_name'],
  273. 'mobile' => $c_data['receiver_tel'],
  274. 'addr' => $c_data['delivery_address'],
  275. 'addr_code' => $addr_code,
  276. 'customer_code' => $customer_code,
  277. 'receipt_quantity' => $c_data['num'],//收货数量,
  278. 'post_fee' => $c_data['post_fee'],
  279. 'is_del' => 0,
  280. 'addtime' => date("Y-m-d H:i:s"),
  281. 'updatetime' => date("Y-m-d H:i:s"),
  282. 'arrive_time' => $arrtime,
  283. ];
  284. $vmp = Db::name('order_addr')->insertGetId($temp);
  285. if ($vmp > 0) {
  286. $num = $c_data['num'];
  287. $outCode = makeNo("DF");
  288. if ($is_stock != 1) {
  289. $order = Db::name("order_num")
  290. ->where(["orderCode" => $orderCode, "status" => 1])
  291. ->find();
  292. if ($order == false) throw new Exception('未找到可以发货得采购单数据');
  293. $tep = [
  294. "cgdNo" => $order['cgdNo'],
  295. "outCode" => $outCode,
  296. "send_num" => $num,
  297. "status" => 1,
  298. "addtime" => date("Y-m-d H:i:s"),
  299. "updatetime" => date("Y-m-d H:i:s")
  300. ];
  301. $sen = Db::name("order_send")->save($tep);
  302. if ($sen == false) throw new Exception('发货地址添加创建失败');
  303. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  304. if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
  305. }
  306. $out = [
  307. "orderCode" => $orderCode,
  308. "outCode" => $outCode,
  309. "apply_id" => $rm,
  310. "apply_name" => $ri,
  311. "addrid" => $vmp,
  312. "post_name" => "",
  313. "post_code" => "",
  314. "post_fee" => 0,
  315. "sendtime" => date("Y-m-d H:i:s"),
  316. "send_num" => $num,
  317. "check_num" => 0,
  318. "error_num" => 0,
  319. "wsm_code" => $is_stock==1? '':$cgdinfo['wsm_code'],
  320. "order_type" => $order_type,
  321. "status" => $is_stock == 1 ? 1 : 0,
  322. "send_status" => $is_stock == 1 ? 1 : 0,
  323. "addtime" => date("Y-m-d H:i:s"),
  324. "updatetime" => date("Y-m-d H:i:s")
  325. ];
  326. $ou = Db::name("order_out")->insertGetId($out);
  327. if ($ou == false) throw new Exception('发货地址添加创建失败');
  328. else {
  329. if ($is_stock == 1) OrderOutChild::makeChild($outCode);
  330. //修改状态,添加待办
  331. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  332. "order_code" => $outCode,//出库单号
  333. "status" => 0,//这里的status是之前的值
  334. "action_remark" => '',//备注
  335. "action_type" => "create"//新建create,编辑edit,更改状态status
  336. ], "CKD", 0, $out);
  337. if ($out['status'] == 1) {
  338. if ($is_stock == 1) {
  339. //库存品,待办数据推给库管
  340. $uids = Db::name('user_role')
  341. ->where(['is_del' => 0, 'roleid' => config('app.wsm_cgder_role'), 'status' => 1])
  342. ->column('uid');
  343. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  344. "order_type" => 'CKD',
  345. "order_code" => $outCode,//出库单号
  346. "order_id" => $ou,
  347. "order_status" => $out['status'],
  348. "before_status" => 0,
  349. 'holder_id' => $data['apply_id'],
  350. 'handle_user_list' => implode(',', $uids),
  351. ]);
  352. } else {
  353. // //非库存品推给供应商负责人
  354. // $supp = Db::name('supplier')
  355. // ->field('person,personid')
  356. // ->where('code', $ct['supplierNo'])
  357. // ->findOrEmpty();
  358. $supp = $userCommon->handle('sInfo',['code'=>$ct['supplierNo']]);
  359. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  360. "order_type" => 'CKD',
  361. "order_code" => $outCode,//出库单号
  362. "order_id" => $ou,
  363. "order_status" => $out['status'],
  364. "before_status" => 0,
  365. 'holder_id' => $data['apply_id'],
  366. 'wait_id' => $supp['data']['personid'],
  367. 'wait_name' => $supp['data']['person'],
  368. ]);
  369. }
  370. }else ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  371. "order_type" => 'CKD',
  372. "order_code" => $outCode,//出库单号
  373. "order_id" => $ou,
  374. "order_status" => $out['status'],
  375. "before_status" => 0,
  376. 'holder_id'=>$data['apply_id']
  377. ]);
  378. // $standing_book_data['outCode'] = $outCode;
  379. }
  380. } else throw new Exception('发货地址添加失败');
  381. }
  382. }
  383. //复用sale::create()方法 -- end
  384. //处理完成
  385. // $order_import_from_c_db
  386. // ->where('id', $c_data['id'])
  387. // ->where('is_del', OIFCModel::$is_del_normal)
  388. // ->save([
  389. // 'status' => $order_import_from_c_db::$status_success,
  390. // 'updatetime' => date('Y-m-d H:i:s'),
  391. // 'remark' => '',
  392. // 'updateid' => 0,
  393. // 'updater' => 'system',
  394. // 'orderCode' => $orderCode
  395. // ]);
  396. //维护台账记录
  397. 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")]));
  398. //回调更新有赞订单数据
  399. $callback_rs = $this->callbackYz(['status' => 6, 'id' => $c_data['id'], 'orderCode' => $orderCode]);
  400. if ($callback_rs['code'] != 0) throw new \Exception($callback_rs['message']);
  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. Cache::store('redis')->set("YzTimeLimit",0);
  421. $output->writeln(date('Y-m-d H:i:s') . '|处理成功,orderCode:' . $orderCode);
  422. } catch (\Exception $exception) {
  423. Db::rollback();
  424. $message[] = $exception->getMessage();
  425. //回调更新有赞订单数据
  426. $callback_rs = $this->callbackYz(['status' => 8, 'id' => $c_data['id'], 'reason' => $exception->getMessage()]);
  427. if ($callback_rs['code'] != 0) $message[] = $callback_rs['message'];
  428. Cache::store('redis')->set("YzTimeLimit",0);
  429. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . implode(',', $message));
  430. }
  431. }
  432. Cache::store('redis')->set("YzTimeLimit",0,60);
  433. //没有需要处理的数据
  434. }
  435. //创建采购单
  436. private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
  437. {
  438. $cgdCode = makeNo("CG");
  439. $wsm = Db::name("warehouse_info")
  440. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  441. ->find();
  442. if ($wsm == false) {
  443. $wsm_code = makeNo("WSM");
  444. $inwsm = [
  445. "wsm_code" => $wsm_code,
  446. "name" => $data['supplier_name'],
  447. "wsm_type" => 2,
  448. "supplierNo" => $data['supplierNo'],
  449. "addr" => "",
  450. "addrs_code" => "",
  451. "contactor" => 0,
  452. "contactor_name" => 0,
  453. "mobile" => "",
  454. "position" => "",
  455. "companyNo" => $data['companyNo'],
  456. "status" => 1,
  457. "is_del" => 0,
  458. "addtime" => date("Y-m-d H:i:s"),
  459. "updatetime" => date("Y-m-d H:i:s")
  460. ];
  461. $in = Db::name("warehouse_info")->insert($inwsm);
  462. if ($in == false) return false;
  463. } else {
  464. $wsm_code = $wsm['wsm_code'];
  465. }
  466. //判断该供应商是否开通了供应商账号
  467. $from_tag=checkHasAccountBySupplierNos([$data['supplierNo']]);
  468. $cg = [
  469. "cgdNo" => $cgdCode,
  470. "bkcode" => "",
  471. "wsm_code" => $wsm_code,
  472. "cgder" => $data['creater'],
  473. "cgder_id" => $data['createrid'],
  474. "spuCode" => $data['spuCode'],
  475. "skuCode" => $data['skuCode'],
  476. "good_name" => $data['good_name'],
  477. "good_num" => $data['good_num'],
  478. "good_price" => $data['sale_price'],
  479. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  480. "origin_total" => round($data['sale_price'] * $data['good_num'], 2),
  481. "pakge_fee" => $data['pakge_fee'],
  482. "cert_fee" => $data['cert_fee'],
  483. "open_fee" => $data['open_fee'],
  484. "teach_fee" => $data['cost_fee'],
  485. "mark_fee" => $data['mark_fee'],
  486. "demo_fee" => $data['demo_fee'],
  487. "nake_fee" => $data['nake_fee'],
  488. "delivery_fee" => $data['delivery_fee'],
  489. "weight"=>$data['weight'],
  490. "diff_weight" => "0",
  491. "diff_fee" => "0",
  492. "gold_price" => $data['gold_price'],
  493. "supplierNo" => $data['supplierNo'],
  494. "supplier_name" => $data['supplier_name'],
  495. "companyNo" => $data['companyNo'],
  496. "companyName" => $data['companyName'],
  497. "send_status" => 1,
  498. "send_num" => 0,
  499. "wsend_num" => $data['good_num'],
  500. "remark" => "",
  501. "status" => 0,//0初始化
  502. "lasttime" => date("Y-m-d H:i:s"),
  503. "is_del" => 0,
  504. "order_type" => $data['order_type'],
  505. "open_type"=>$data['open_type'],
  506. "order_source" => $data['order_source'],
  507. "good_type" => $data['good_type'],
  508. "addtime" => date("Y-m-d H:i:s"),
  509. "updatetime" => date("Y-m-d H:i:s"),
  510. 'good_createrid' => $data['good_createrid'],
  511. 'good_creater' => $data['good_creater'],//商品创建人
  512. 'from_tag' => isset($from_tag[$data['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  513. 'supplier_origin_price'=>$data['supplier_origin_price'],//供应商采购成本
  514. 'cgd_supplier_code'=>$data['cgd_supplier_code'],//采购供应商编号
  515. 'cgd_supplier_name'=>$data['cgd_supplier_name'],//采购供应商编号
  516. 'cgd_apply_id'=>$data['cgd_apply_id'],//竞单人
  517. 'cgd_apply_name'=>$data['cgd_apply_name'],//竞单人
  518. ];
  519. $up = Db::name("purchease_order")->insert($cg, true);
  520. if ($up) {
  521. $taxInfo = OrderTax::SpuCat( $data['spuCode'],1,1);
  522. if (!empty($taxInfo)) {
  523. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($cgdCode){
  524. $temp=[];
  525. $temp['code']=$cgdCode;
  526. $temp['spuCode']=$item['spuCode'];
  527. $temp['tax_code']=$item['cat_code'];
  528. $temp['tax_name']=$item['cat_name'];
  529. $temp['tax_short_name']=$item['short_name'];
  530. $temp['inv_tax']=$item['tax'];
  531. $temp['inv_good_name']=$item['inv_good_name'];
  532. return $temp;
  533. },$taxInfo));
  534. }
  535. //修改状态,添加待办
  536. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  537. "order_code" => $cg['cgdNo'],//销售单code
  538. "status" => 0,//这里的status是之前的值
  539. "action_remark" => '',//备注
  540. "action_type" => "create"//新建create,编辑edit,更改状态status
  541. ], "CGD", $cg['status'], $cg);
  542. //当采购单是节点0待与供应商确认,推给供应商负责人
  543. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  544. "order_type" => 'CGD',
  545. "order_code" => $cg['cgdNo'],//销售单code
  546. "order_id" => $up,
  547. "order_status" => $cg['status'],
  548. "before_status" => 0,
  549. 'holder_id'=>$data['createrid'],
  550. 'wait_id'=>$data['createrid'],
  551. 'wait_name'=>$data['creater'],
  552. ]);
  553. $standing_book_data['cgdNo'] = $cgdCode;
  554. $rela = [
  555. "orderCode" => $data['orderCode'],
  556. "cgdNo" => $cgdCode,
  557. "spuCode" => $data['spuCode'],
  558. "good_num" => $data['good_num'],
  559. "wsend_num" => $data['good_num'],
  560. "send_num" => 0,
  561. "wait_num" => 0,
  562. "status" => 1,
  563. "source" => 2
  564. ];
  565. $re = Db::name("order_num")->save($rela);
  566. if ($re == false) return false;
  567. else {
  568. $stokc = Db::name("good_stock")
  569. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  570. ->find();
  571. if ($stokc == false) {
  572. $stokc = [
  573. "spuCode" => $data['spuCode'],
  574. "wsm_code" => $wsm_code,
  575. "wait_in_stock" => $data['good_num'],
  576. "wait_out_stock" => 0,
  577. "usable_stock" => 0,
  578. "intra_stock" => 0,
  579. "total_stock" => 0,
  580. "status" => 1,
  581. "addtime" => date("Y-m-d H:i:s"),
  582. "updatetime" => date("Y-m-d H:i:s")
  583. ];
  584. } else {
  585. $stokc['wait_in_stock'] += $data['good_num'];
  586. $stokc['updatetime'] = date("Y-m-d H:i:s");
  587. }
  588. $stoc = Db::name("good_stock")->save($stokc);
  589. if ($stoc == false) return false;
  590. $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"];
  591. GoodLog::LogAdd(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
  592. $this->cgd_data = $cg;
  593. return true;
  594. }
  595. } else return false;
  596. }
  597. //创建.....
  598. // private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
  599. // {
  600. // //备库单
  601. // $cgd = Db::name("order_bk")
  602. // ->where([
  603. // ["spuCode", "=", $outinfo['spuCode']],
  604. // ["is_del", "=", 0],
  605. // ["balance_num", ">=", $outinfo['good_num']],
  606. // ['companyNo', "=", $outinfo['companyNo']]
  607. // ])
  608. // ->lock(true)
  609. // ->find();
  610. //
  611. // if ($cgd == false) return false;
  612. //
  613. // $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  614. // if ($cgdinfo == false) return false;
  615. //
  616. // //判断该供应商是否开通了供应商账号
  617. // $from_tag=checkHasAccountBySupplierNos([$cgdinfo['supplierNo']]);
  618. //
  619. // $QrdCgd = [
  620. // "cgdNo" => makeNo("CG"),
  621. // "bkcode" => $cgdinfo['bkcode'],
  622. // 'wsm_code' => $cgdinfo['wsm_code'],
  623. // "cgder_id" => $cgdinfo['cgder_id'],
  624. // "cgder" => $cgdinfo['cgder'],
  625. // "spuCode" => $cgdinfo['spuCode'],
  626. // "good_name" => $cgdinfo['good_name'],
  627. // "good_num" => $outinfo['good_num'],
  628. // "good_price" => $cgdinfo['good_price'],
  629. // "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  630. // "pakge_fee" => $cgdinfo['pakge_fee'],
  631. // "cert_fee" => $cgdinfo['cert_fee'],
  632. // "open_fee" => $cgdinfo['open_fee'],
  633. // "delivery_fee" => $cgdinfo['delivery_fee'],
  634. // "mark_fee" => $cgdinfo['mark_fee'],
  635. // "teach_fee" => $cgdinfo['teach_fee'],
  636. // "nake_fee" => $cgdinfo['nake_fee'],
  637. // "demo_fee" => $cgdinfo['demo_fee'],
  638. // "weight" => $cgdinfo['weight'],
  639. // "diff_weight" => $cgdinfo['diff_weight'],
  640. // "diff_fee" => $cgdinfo['diff_fee'],
  641. // "gold_price" => $cgdinfo['gold_price'],
  642. // "supplierNo" => $cgdinfo['supplierNo'],
  643. // "supplier_name" => $cgdinfo['supplier_name'],
  644. // "companyNo" => $cgdinfo['companyNo'],
  645. // "send_status" => 3,
  646. // "send_num" => $outinfo['good_num'],
  647. // "wsend_num" => 0,
  648. // "remark" => $cgdinfo['remark'],
  649. // "status" => 3,//入库完成
  650. // "lasttime" => $cgdinfo['lasttime'],
  651. // "is_del" => 0,
  652. // "order_type" => $outinfo['order_type'],
  653. // "order_source" => $outinfo['order_source'],
  654. // "good_type" => $cgdinfo['good_type'],
  655. // "addtime" => date("Y-m-d H:i:s"),
  656. // "updatetime" => date("Y-m-d H:i:s"),
  657. // 'good_createrid' => $outinfo['good_createrid'],
  658. // 'good_creater' => $outinfo['good_creater'],//商品创建人
  659. // 'from_tag' => isset($from_tag[$cgdinfo['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  660. // ];
  661. // $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  662. // if ($insetrCgd == false) return false;
  663. //
  664. // $standing_book_data = array_merge($standing_book_data, [
  665. // 'cgdNo' => $QrdCgd['cgdNo'],
  666. // 'bk_code' => $QrdCgd['bkcode'],
  667. // ]);
  668. //
  669. // $merge_num = Db::name("purchease_order")
  670. // ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
  671. // ->where('order_source', '<>', 0)//0备库单不计算在内
  672. // ->field("sum(send_num)-sum(th_num) as num")
  673. // ->find();
  674. // $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  675. // $cgd['merge_num'] = $merge_num['num'];
  676. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  677. // $up = Db::name("order_bk")->save($cgd);
  678. // if ($up == false) return false;
  679. //
  680. // $data = [
  681. // "orderCode" => $outinfo['orderCode'],
  682. // "cgdNo" => $QrdCgd['cgdNo'],
  683. // "spuCode" => $outinfo['spuCode'],
  684. // "companyNo" => $outinfo['companyNo'],
  685. // "good_num" => $outinfo['good_num'],
  686. // "wsend_num" => $outinfo['good_num'],
  687. // "send_num" => 0,
  688. // "wait_num" => $outinfo['good_num'],
  689. // "status" => 1,
  690. // "source" => 1,
  691. // ];
  692. // $order = Db::name("order_num")->save($data);
  693. // if ($order == false) return false;
  694. //
  695. // $this->cgd_data = $QrdCgd;
  696. //
  697. // return true;
  698. // }
  699. //与有赞项目回调
  700. private function callbackYz(array $data = [])
  701. {
  702. $host = Config::get('app.yz_domain');
  703. $response = curl_request($host . 'api/callback_order_by_stock', $data);
  704. return json_decode($response, true);
  705. }
  706. }