handleYzOrderData.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php declare (strict_types=1);
  2. namespace app\command;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\Output;
  9. use think\Exception;
  10. use think\facade\Cache;
  11. use think\facade\Config;
  12. use think\facade\Db;
  13. //处理有赞订单数据
  14. class handleYzOrderData extends Command
  15. {
  16. protected function configure()
  17. {
  18. // 指令配置
  19. $this->setName('handle_yz_order_data')->setDescription('处理有赞订单数据');
  20. }
  21. //处理有赞订单数据
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $c_data = Cache::store('redis')->handler()->rpop(Config::get('app.handle_queue_key'));
  25. if ($c_data){
  26. $c_data = json_decode($c_data, true);
  27. Db::startTrans();
  28. try {
  29. //判断价格是否符合系统售价
  30. $platform_yz_info = Db::name('platform_youzan')
  31. ->field('id,sale_price,platform_id')
  32. ->where(['skuCode' => $c_data['skuCode'], 'is_del' => 0])
  33. ->findOrEmpty();
  34. if ($c_data['discount_price'] < $platform_yz_info['sale_price'] && $c_data['is_approved'] == '0') {
  35. Db::rollback();
  36. $this->callbackYz(['status' => 2, 'id' => $c_data['id'], 'reason' => '价格低于系统售价' . $platform_yz_info['sale_price']]);
  37. return false;//停止执行后续代码
  38. }
  39. $standing_book_data = [];
  40. // //先找用户确认信息
  41. // $extend_data = Db::name('order_import_from_c_extend')
  42. // ->where(['order_import_from_c_id' => $c_data['id'], 'is_del' => 0, 'type' => 2])
  43. // ->find();
  44. // if (empty($extend_data)) {
  45. // $extend_data = Db::name('order_import_from_c_extend')
  46. // ->where(['order_import_from_c_id' => $c_data['id'], 'is_del' => 0, 'type' => 1])
  47. // ->find();
  48. // }
  49. //
  50. // if (empty($extend_data)) throw new Exception('没有对应的解析数据');
  51. //复用sale::create()方法 -- start
  52. $orderCode = makeNo("QR");
  53. //企业客户
  54. $ct = Db::name('platform_youzan')
  55. ->alias('py')
  56. ->field('py.id,py.skuCode,py.spuCode,py.plat_code,co.id org1_id,co.name,py.sale_price,g.*,py.platform_id')
  57. ->leftJoin('platform p', 'p.id=py.platform_id AND p.is_del=0')
  58. ->leftJoin('customer_org1 co', 'co.name=p.platform_name AND co.is_del=0')
  59. ->leftJoin('good g', 'g.spuCode=py.spuCode AND g.is_del=0')
  60. ->where('py.skuCode', $c_data['skuCode'])
  61. ->where('py.exam_status', 6)//exam_status==6已上线
  62. ->find();
  63. if ($ct) {
  64. $customer_code = Db::name('customer_info')
  65. ->where(['itemid' => $ct['org1_id'], 'companyName' => $ct['name'] . '客户', 'is_del' => 0])
  66. ->value('companyNo', '');
  67. if (!$customer_code) throw new Exception('在企业客户管理组织中找不到对应的企业');
  68. } else throw new Exception('未找到商品数据');
  69. // $customer_code = $customer_code = Db::name('customer_info')
  70. // ->where(['itemid' => $ct['id'], 'companyName' => $ct['name'] . '客户', 'is_del' => 0])
  71. // ->value('companyNo', '');
  72. //
  73. // $customer = Db::name("customer_info")
  74. // ->where(["companyNo" => $customer_code])
  75. // ->find();
  76. // if ($customer == false) throw new Exception('未找到客户数据');
  77. $supplierNo = $ct['companyNo'];
  78. // $supplier = Db::name("business")
  79. // ->where(["companyNo" => $supplierNo])
  80. // ->find();
  81. // if ($supplier == false) throw new Exception('未找到平台供应商数据');
  82. $goodtype = 1;//1正常商品
  83. $sendtype = 1;//直接发货
  84. $platform_id = $ct['platform_id'];
  85. $platform_order = $c_data['tid'];
  86. $good_num = $c_data['num'];
  87. $arrtime = $paytime = $c_data['addtime'];
  88. // $paytime = $c_data['addtime'];
  89. // $workNo = $extend_data['po_code'];
  90. // $ct = Db::name('good_platform')
  91. // ->alias('a')
  92. // ->join('good b', 'b.spuCode=a.spuCode', 'left')
  93. // ->where(['a.skuCode' => $extend_data['skuCode'], 'a.exam_status' => 6])//exam_status==6已上线
  94. // ->field("b.*,a.skuCode,a.spuCode,a.platform_code,a.plat_code")
  95. // ->find();
  96. // if ($ct == false) throw new Exception('未找到商品数据');
  97. $goodinfo = $ct;
  98. $is_stock = $ct['is_stock'];
  99. $order_type = $is_stock == 1 ? 1 : 2;//1备库(库存品)2非库存品
  100. $order_source = 5;//有赞
  101. $spuCode = $ct['spuCode'];
  102. $skuCode = $ct['skuCode'];
  103. // $is_activity = empty($extend_data['activity_name']) ? 0 : 1;
  104. if ($is_stock == 1) {
  105. $stock = Db::name("good_stock")
  106. ->alias("a")
  107. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  108. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, 'b.wsm_type' => 5, "b.companyNo" => $supplierNo])
  109. ->field("a.id,a.usable_stock,a.wait_out_stock")
  110. ->find();
  111. if ($stock == false || $stock['usable_stock'] < $good_num) throw new Exception('库存数量不足');
  112. $origin_price = 0;
  113. } else {
  114. $origin = Db::name("good_nake")
  115. ->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])
  116. ->order("min_num desc")
  117. ->find();
  118. if ($origin == false) throw new Exception('未找到相关成本价格');
  119. $origin_price = $origin['nake_total'];
  120. }
  121. //活动相关信息
  122. $act = Db::name('activity_info')
  123. ->alias('ai')
  124. ->field('ai.id,ai.activity_stock,ai.moq_num,ai.activity_stock')
  125. ->join('good_activity ga', 'ga.activity_code=ai.activity_code')
  126. ->where([
  127. 'ai.skuCode' => $skuCode,
  128. 'ai.spuCode' => $spuCode,
  129. 'ai.is_del' => 0,
  130. 'ai.status' => 1,
  131. 'ga.status' => 6,
  132. 'ga.is_del' => 0,
  133. 'ga.platform_code' => $platform_id,
  134. 'ga.is_stock' => $is_stock
  135. ])
  136. ->where(function ($query) {
  137. return $query->where('ga.start', '<=', date('Y-m-d H:i:s'))->where('ga.end', '>=', date('Y-m-d H:i:s'));
  138. })
  139. ->find();
  140. $is_activity = 0;
  141. if (!empty($act)) {
  142. $is_activity = 1;
  143. if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
  144. if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
  145. }
  146. // $is_activity = (int)(!empty($act));
  147. // if ($is_activity == 1) {
  148. // $act = Db::name("activity_info")
  149. // ->alias("a")
  150. // ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  151. // ->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])
  152. // ->find();
  153. // if ($act == false) throw new Exception('未找到相关活动价');
  154. // if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']);
  155. // if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']);
  156. //$sale_price = $act['activity_price'];//不能改动价格
  157. // }
  158. $sale_price = $c_data['discount_price'];
  159. // if ($goodtype == 1) {
  160. // $good = Db::name("good_ladder")
  161. // ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
  162. // ->where([["min_num", "<=", $good_num]])
  163. // ->order("min_num desc")
  164. // ->find();
  165. // if ($good == false) throw new Exception('未找到相关阶梯价格');
  166. //$sale_price = $good['sale_price']; //不改动售价
  167. //理论上不会出现实时金价的订单
  168. // if ($ct['is_gold_price'] == 1 && $is_stock != 1) {
  169. // $gold = Db::name("gold_price1")
  170. // ->field('id,price')
  171. // ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  172. // ->order("addtime desc")
  173. // ->find();
  174. //
  175. // //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  176. // $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'];
  177. // if ($sale_price < $gold_sale_price) throw new Exception('价格不符合根据实时金价计算出的最终售价');
  178. //
  179. // $ct['cgd_gold_price'] = $gold['price'];
  180. //
  181. // }
  182. // }
  183. $remark = $c_data['buyer_messages'];
  184. $user = Config::get('app.uid_platform_youzan.' . $platform_yz_info['platform_id']);
  185. $rm = isset($user['id']) ? $user['id'] : 0;
  186. $ri = isset($user['nickname']) ? $user['nickname'] : 'youzan';
  187. $supplier_temp_info = Db::name('supplier')
  188. ->field('id,person,personid')
  189. ->where('code',$ct['supplierNo'])
  190. ->findOrEmpty();
  191. $data = [
  192. "orderCode" => $orderCode,
  193. "good_code" => $spuCode,
  194. "skuCode" => $skuCode,
  195. "customer_code" => $customer_code,
  196. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  197. "good_num" => $good_num,
  198. "cat_id" => $goodinfo['cat_id'],
  199. "apply_id" => $rm,
  200. "apply_name" => $ri,
  201. "origin_price" => $origin_price,
  202. "sale_price" => $sale_price,
  203. "post_fee" => 0,
  204. "status" => 0,
  205. "send_num" => 0,
  206. "wsend_num" => $good_num,
  207. "send_status" => 1,
  208. "good_type" => $goodtype,
  209. "send_type" => $sendtype,
  210. "supplierNo" => $goodinfo['companyNo'],
  211. "is_del" => 0,
  212. "zxNo" => "",
  213. "platform_order" => $platform_order,
  214. "platform_id" => $platform_id,
  215. "remark" => $remark,
  216. "is_stock" => $is_stock,
  217. "is_activity" => $is_activity,
  218. 'activity_code' => $is_activity ? $act['activity_code'] : '',
  219. "order_type" => $order_type,
  220. "order_source" => $order_source,
  221. // "poNo"=>$poNo,
  222. 'good_weight' => $ct['weight'],
  223. 'gold_price' => $ct['cgd_gold_price'],
  224. 'cost_price' => $ct['cost_fee'],
  225. 'diff_weight' => 0,
  226. 'diff_fee' => 0,
  227. "workNo" => '',
  228. "addtime" => date("Y-m-d H:i:s"),
  229. "updatetime" => date("Y-m-d H:i:s"),
  230. 'total_price' => bcmul((string)$sale_price, (string)$good_num, 2),
  231. 'cgderid' => $supplier_temp_info['personid'],
  232. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  233. 'good_createrid' => $goodinfo['createrid'],
  234. 'good_creater' => $goodinfo['creater'],//商品创建人
  235. ];
  236. $paytime == "" ? "" : $data['paytime'] = $paytime;
  237. $datainfo = Db::name('sale')->insertGetId($data);
  238. if ($datainfo > 0) {
  239. if ($is_activity == 1) {
  240. $actup = [
  241. "activity_stock" => $act['activity_stock'] - $good_num,
  242. "updatetime" => date("Y-m-d H:i:s")
  243. ];
  244. $actupp = Db::name("activity_info")
  245. ->where('id', $act['id'])
  246. ->save($actup);
  247. if ($actupp == false) throw new Exception('活动库存修改失败');
  248. }
  249. //补充台账数据
  250. $standing_book_data = array_merge($standing_book_data, [
  251. 'orderCode' => $orderCode,
  252. 'spuCode' => $data['good_code'],
  253. 'skuCode' => $data['skuCode'],
  254. 'order_type' => $data['order_type'],
  255. 'order_source' => $data['order_source'],
  256. 'supplierNo' => $data['supplierNo'],
  257. 'companyNo' => $ct['companyNo'],
  258. 'customer_code' => $data['customer_code'],
  259. ]);
  260. if ($is_stock == 0) {
  261. $cgd = [
  262. "supplierNo" => $ct['supplierNo'],
  263. "companyNo" => $supplierNo,
  264. "orderCode" => $orderCode,
  265. "spuCode" => $spuCode,
  266. "skuCode" => $skuCode,
  267. "good_name" => $ct['good_name'],
  268. "sale_price" => $origin_price,
  269. "total_fee" => $origin_price * $good_num,
  270. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  271. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  272. "open_fee" => $ct['open_fee'],
  273. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  274. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  275. "demo_fee" => $ct['demo_fee'],
  276. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  277. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  278. "good_num" => $good_num,
  279. "good_type" => $goodtype,
  280. "order_type" => $order_type,
  281. "order_source" => $order_source,
  282. "createrid" => $ct['createrid'],
  283. "creater" => $ct['creater'],
  284. 'send_way' => 2,
  285. 'gold_price' => $ct['cgd_gold_price'],
  286. 'good_createrid' => $goodinfo['createrid'],
  287. 'good_creater' => $goodinfo['creater'],//商品创建人
  288. 'weight' => $ct['noble_weight'],//商品创建人
  289. ];
  290. //非库存品
  291. $bol = $this->createCgd($cgd, $rm, $ri, $standing_book_data);
  292. if ($bol == false) throw new Exception('订单创建失败');
  293. } else {
  294. //库存品
  295. // $cgd = [
  296. // 'orderCode' => $orderCode,
  297. // "good_num" => $good_num,
  298. // "spuCode" => $spuCode,
  299. // "companyNo" => $supplierNo,
  300. // 'order_type' => $order_type,
  301. // 'order_source' => $order_source,
  302. // 'good_createrid' => $goodinfo['createrid'],
  303. // 'good_creater' => $goodinfo['creater'],//商品创建人
  304. // ];
  305. // $bol = $this->RelaCgd($cgd, $standing_book_data);
  306. // if ($bol == false) throw new Exception('库存商品关联采购单失败');
  307. if (isset($stock)) {
  308. $stck = [
  309. "usable_stock" => $stock['usable_stock'] - $good_num,
  310. "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  311. "updatetime" => date("Y-m-d H:i:s")
  312. ];
  313. $upad = Db::name("good_stock")
  314. ->where($stock)
  315. ->update($stck);
  316. if ($upad == false) throw new Exception('库存商品更新库存失败');
  317. $stockinfo =GoodStockInfo::OrderBn($orderCode,$stock['id'],$good_num);
  318. if($stockinfo==false){
  319. Db::rollback();
  320. return error_show(1002, "Bn库存更新库存失败");
  321. }
  322. //商品变动日志表,good_log_code字段存储采购单号
  323. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  324. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  325. GoodLog::LogAdd(['id' => $rm, 'nickname' => $ri], $good_data, "XSQRD");
  326. }
  327. }
  328. if ($sendtype == 1) {
  329. $p_c_a = get_address($c_data['delivery_province'] . $c_data['delivery_city'] . $c_data['delivery_district']);
  330. $province = empty($p_c_a['province']) ? $p_c_a['province'] : Db::name('province')->whereLike('name', '%' . $p_c_a['province'] . '%')->value('province_code');
  331. $city = empty($p_c_a['city']) ? $p_c_a['city'] : Db::name('city')->whereLike('name', '%' . $p_c_a['city'] . '%')->value('city_code');
  332. $area = empty($p_c_a['district']) ? $p_c_a['district'] : Db::name('area')->whereLike('name', '%' . $p_c_a['district'] . '%')->value('area_code');
  333. $addr_code = implode([$province, $city, $area], ',');
  334. $temp = [
  335. 'orderCode' => $orderCode,
  336. 'contactor' => $c_data['receiver_name'],
  337. 'mobile' => $c_data['receiver_tel'],
  338. 'addr' => $c_data['delivery_address'],
  339. 'addr_code' => $addr_code,
  340. 'customer_code' => $customer_code,
  341. 'receipt_quantity' => $c_data['num'],//收货数量,
  342. 'post_fee' => $c_data['post_fee'],
  343. 'is_del' => 0,
  344. 'addtime' => date("Y-m-d H:i:s"),
  345. 'updatetime' => date("Y-m-d H:i:s"),
  346. 'arrive_time' => $arrtime,
  347. ];
  348. $vmp = Db::name('order_addr')->insertGetId($temp);
  349. if ($vmp > 0) {
  350. $order = Db::name("order_num")
  351. ->where(["orderCode" => $orderCode, "status" => 1])
  352. ->where([["wsend_num", ">=", 0]])
  353. ->lock(true)
  354. ->find();
  355. if ($order == false) throw new Exception('未找到可以发货得采购单数据');
  356. $num = $c_data['num'];
  357. $outCode = makeNo("DF");
  358. $order['wsend_num'] -= $num;
  359. $order['send_num'] += $num;
  360. $or = Db::name("order_num")->save($order);
  361. if ($or == false) throw new Exception('发货地址更新失败');
  362. $tep = [
  363. "cgdNo" => $order['cgdNo'],
  364. "outCode" => $outCode,
  365. "send_num" => $num,
  366. "status" => 1,
  367. "addtime" => date("Y-m-d H:i:s"),
  368. "updatetime" => date("Y-m-d H:i:s")
  369. ];
  370. $sen = Db::name("order_send")->save($tep);
  371. if ($sen == false) throw new Exception('发货地址添加创建失败');
  372. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  373. if ($cgdinfo == false) throw new Exception('未匹配到采购数据');
  374. $out = [
  375. "orderCode" => $orderCode,
  376. "outCode" => $outCode,
  377. "apply_id" => $rm,
  378. "apply_name" => $ri,
  379. "addrid" => $vmp,
  380. "post_name" => "",
  381. "post_code" => "",
  382. "post_fee" => 0,
  383. "sendtime" => date("Y-m-d H:i:s"),
  384. "send_num" => $num,
  385. "check_num" => 0,
  386. "error_num" => 0,
  387. "wsm_code" => $cgdinfo['wsm_code'],
  388. "order_type" => $order_type,
  389. "status" => $is_stock == 1 ? 1 : 0,
  390. "addtime" => date("Y-m-d H:i:s"),
  391. "updatetime" => date("Y-m-d H:i:s")
  392. ];
  393. $ou = Db::name("order_out")->insert($out);
  394. if ($ou == false) throw new Exception('发货地址添加创建失败');
  395. else {
  396. //修改状态,添加待办
  397. ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [
  398. "order_code" => $outCode,//出库单号
  399. "status" => 0,//这里的status是之前的值
  400. "action_remark" => '',//备注
  401. "action_type" => "create"//新建create,编辑edit,更改状态status
  402. ], "CKD", 0, $out);
  403. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  404. "order_type" => 'CKD',
  405. "order_code" => $outCode,//出库单号
  406. "order_id" => 0,
  407. "order_status" => 0,
  408. "before_status" => 0
  409. ]);
  410. // $standing_book_data['outCode'] = $outCode;
  411. }
  412. } else throw new Exception('发货地址添加失败');
  413. }
  414. }
  415. //复用sale::create()方法 -- end
  416. //处理完成
  417. // $order_import_from_c_db
  418. // ->where('id', $c_data['id'])
  419. // ->where('is_del', OIFCModel::$is_del_normal)
  420. // ->save([
  421. // 'status' => $order_import_from_c_db::$status_success,
  422. // 'updatetime' => date('Y-m-d H:i:s'),
  423. // 'remark' => '',
  424. // 'updateid' => 0,
  425. // 'updater' => 'system',
  426. // 'orderCode' => $orderCode
  427. // ]);
  428. //维护台账记录
  429. 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")]));
  430. //回调更新有赞订单数据
  431. $callback_rs = $this->callbackYz(['status' => 6, 'id' => $c_data['id'], 'orderCode' => $orderCode]);
  432. if ($callback_rs['code'] != 0) throw new Exception($callback_rs['message']);
  433. Db::commit();
  434. $output->writeln(date('Y-m-d H:i:s') . '|处理成功,orderCode:' . $orderCode);
  435. } catch (\think\Exception $exception) {
  436. Db::rollback();
  437. $message[] = $exception->getMessage();
  438. //回调更新有赞订单数据
  439. $callback_rs = $this->callbackYz(['status' => 8, 'id' => $c_data['id'], 'reason' => $exception->getMessage()]);
  440. if ($callback_rs['code'] != 0) $message[] = $callback_rs['message'];
  441. $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . implode(',', $message));
  442. }
  443. }
  444. //没有需要处理的数据
  445. }
  446. //创建采购单
  447. private function createCgd($data = [], $rm = '0', $ri = '', array &$standing_book_data = [])
  448. {
  449. $cgdCode = makeNo("CG");
  450. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  451. if ($supplier == false) return false;
  452. $wsm = Db::name("warehouse_info")
  453. ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0])
  454. ->find();
  455. if ($wsm == false) {
  456. $wsm_code = makeNo("WSM");
  457. $inwsm = [
  458. "wsm_code" => $wsm_code,
  459. "name" => $supplier['name'],
  460. "wsm_type" => 2,
  461. "supplierNo" => $supplier['code'],
  462. "addr" => "",
  463. "addrs_code" => "",
  464. "contactor" => 0,
  465. "contactor_name" => 0,
  466. "mobile" => "",
  467. "position" => "",
  468. "companyNo" => $data['companyNo'],
  469. "status" => 1,
  470. "is_del" => 0,
  471. "addtime" => date("Y-m-d H:i:s"),
  472. "updatetime" => date("Y-m-d H:i:s")
  473. ];
  474. $in = Db::name("warehouse_info")->insert($inwsm);
  475. if ($in == false) return false;
  476. } else {
  477. $wsm_code = $wsm['wsm_code'];
  478. }
  479. $cg = [
  480. "cgdNo" => $cgdCode,
  481. "bkcode" => "",
  482. "wsm_code" => $wsm_code,
  483. "cgder" => $data['creater'],
  484. "cgder_id" => $data['createrid'],
  485. "spuCode" => $data['spuCode'],
  486. "skuCode" => $data['skuCode'],
  487. "good_name" => $data['good_name'],
  488. "good_num" => $data['good_num'],
  489. "good_price" => $data['sale_price'],
  490. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  491. "pakge_fee" => $data['pakge_fee'],
  492. "cert_fee" => $data['cert_fee'],
  493. "open_fee" => $data['open_fee'],
  494. "teach_fee" => $data['cost_fee'],
  495. "mark_fee" => $data['mark_fee'],
  496. "demo_fee" => $data['demo_fee'],
  497. "nake_fee" => $data['nake_fee'],
  498. "delivery_fee" => $data['delivery_fee'],
  499. "weight"=>$data['weight'],
  500. "diff_weight" => "0",
  501. "diff_fee" => "0",
  502. "gold_price" => $data['gold_price'],
  503. "supplierNo" => $data['supplierNo'],
  504. "supplier_name" => $supplier['name'],
  505. "companyNo" => $data['companyNo'],
  506. "send_status" => 1,
  507. "send_num" => 0,
  508. "wsend_num" => $data['good_num'],
  509. "remark" => "",
  510. "status" => 0,//0初始化
  511. "lasttime" => date("Y-m-d H:i:s"),
  512. "is_del" => 0,
  513. "order_type" => $data['order_type'],
  514. "order_source" => $data['order_source'],
  515. "good_type" => $data['good_type'],
  516. "addtime" => date("Y-m-d H:i:s"),
  517. "updatetime" => date("Y-m-d H:i:s"),
  518. 'good_createrid' => $data['good_createrid'],
  519. 'good_creater' => $data['good_creater'],//商品创建人
  520. ];
  521. $up = Db::name("purchease_order")->insert($cg, true);
  522. if ($up) {
  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. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [
  531. "order_type" => 'CGD',
  532. "order_code" => $cg['cgdNo'],//销售单code
  533. "order_id" => $up,
  534. "order_status" => $cg['status'],
  535. "before_status" => 0
  536. ]);
  537. $standing_book_data['cgdNo'] = $cgdCode;
  538. $rela = [
  539. "orderCode" => $data['orderCode'],
  540. "cgdNo" => $cgdCode,
  541. "spuCode" => $data['spuCode'],
  542. "good_num" => $data['good_num'],
  543. "wsend_num" => $data['good_num'],
  544. "send_num" => 0,
  545. "wait_num" => 0,
  546. "status" => 1,
  547. "source" => 2
  548. ];
  549. $re = Db::name("order_num")->save($rela);
  550. if ($re == false) return false;
  551. else {
  552. $stokc = Db::name("good_stock")
  553. ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])
  554. ->find();
  555. if ($stokc == false) {
  556. $stokc = [
  557. "spuCode" => $data['spuCode'],
  558. "wsm_code" => $wsm_code,
  559. "wait_in_stock" => $data['good_num'],
  560. "wait_out_stock" => 0,
  561. "usable_stock" => 0,
  562. "intra_stock" => 0,
  563. "total_stock" => 0,
  564. "status" => 1,
  565. "addtime" => date("Y-m-d H:i:s"),
  566. "updatetime" => date("Y-m-d H:i:s")
  567. ];
  568. } else {
  569. $stokc['wait_in_stock'] += $data['good_num'];
  570. $stokc['updatetime'] = date("Y-m-d H:i:s");
  571. }
  572. $stoc = Db::name("good_stock")->save($stokc);
  573. if ($stoc == false) return false;
  574. $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"];
  575. GoodLog::LogAdd(['id' => 0, 'nickname' => 'system'], $good_data, "CGD");
  576. return true;
  577. }
  578. } else return false;
  579. }
  580. //创建.....
  581. private function RelaCgd(array $outinfo = [], array &$standing_book_data = [])
  582. {
  583. //备库单
  584. $cgd = Db::name("order_bk")
  585. ->where([
  586. ["spuCode", "=", $outinfo['spuCode']],
  587. ["is_del", "=", 0],
  588. ["balance_num", ">=", $outinfo['good_num']],
  589. ['companyNo', "=", $outinfo['companyNo']]
  590. ])
  591. ->lock(true)
  592. ->find();
  593. if ($cgd == false) return false;
  594. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  595. if ($cgdinfo == false) return false;
  596. $QrdCgd = [
  597. "cgdNo" => makeNo("CG"),
  598. "bkcode" => $cgdinfo['bkcode'],
  599. 'wsm_code' => $cgdinfo['wsm_code'],
  600. "cgder_id" => $cgdinfo['cgder_id'],
  601. "cgder" => $cgdinfo['cgder'],
  602. "spuCode" => $cgdinfo['spuCode'],
  603. "good_name" => $cgdinfo['good_name'],
  604. "good_num" => $outinfo['good_num'],
  605. "good_price" => $cgdinfo['good_price'],
  606. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  607. "pakge_fee" => $cgdinfo['pakge_fee'],
  608. "cert_fee" => $cgdinfo['cert_fee'],
  609. "open_fee" => $cgdinfo['open_fee'],
  610. "delivery_fee" => $cgdinfo['delivery_fee'],
  611. "mark_fee" => $cgdinfo['mark_fee'],
  612. "teach_fee" => $cgdinfo['teach_fee'],
  613. "nake_fee" => $cgdinfo['nake_fee'],
  614. "demo_fee" => $cgdinfo['demo_fee'],
  615. "weight" => $cgdinfo['weight'],
  616. "diff_weight" => $cgdinfo['diff_weight'],
  617. "diff_fee" => $cgdinfo['diff_fee'],
  618. "gold_price" => $cgdinfo['gold_price'],
  619. "supplierNo" => $cgdinfo['supplierNo'],
  620. "supplier_name" => $cgdinfo['supplier_name'],
  621. "companyNo" => $cgdinfo['companyNo'],
  622. "send_status" => 3,
  623. "send_num" => $outinfo['good_num'],
  624. "wsend_num" => 0,
  625. "remark" => $cgdinfo['remark'],
  626. "status" => 3,//入库完成
  627. "lasttime" => $cgdinfo['lasttime'],
  628. "is_del" => 0,
  629. "order_type" => $outinfo['order_type'],
  630. "order_source" => $outinfo['order_source'],
  631. "good_type" => $cgdinfo['good_type'],
  632. "addtime" => date("Y-m-d H:i:s"),
  633. "updatetime" => date("Y-m-d H:i:s"),
  634. 'good_createrid' => $outinfo['good_createrid'],
  635. 'good_creater' => $outinfo['good_creater'],//商品创建人
  636. ];
  637. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  638. if ($insetrCgd == false) return false;
  639. $standing_book_data = array_merge($standing_book_data, [
  640. 'cgdNo' => $QrdCgd['cgdNo'],
  641. 'bk_code' => $QrdCgd['bkcode'],
  642. ]);
  643. $merge_num = Db::name("purchease_order")
  644. ->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => $outinfo['order_type'], "is_del" => 0])
  645. ->where('order_source', '<>', 0)//0备库单不计算在内
  646. ->field("sum(send_num)-sum(th_num) as num")
  647. ->find();
  648. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  649. $cgd['merge_num'] = $merge_num['num'];
  650. $cgd['updatetime'] = date("Y-m-d H:i:s");
  651. $up = Db::name("order_bk")->save($cgd);
  652. if ($up == false) return false;
  653. $data = [
  654. "orderCode" => $outinfo['orderCode'],
  655. "cgdNo" => $QrdCgd['cgdNo'],
  656. "spuCode" => $outinfo['spuCode'],
  657. "companyNo" => $outinfo['companyNo'],
  658. "good_num" => $outinfo['good_num'],
  659. "wsend_num" => $outinfo['good_num'],
  660. "send_num" => 0,
  661. "wait_num" => $outinfo['good_num'],
  662. "status" => 1,
  663. "source" => 1,
  664. ];
  665. $order = Db::name("order_num")->save($data);
  666. if ($order == false) return false;
  667. return true;
  668. }
  669. //与有赞项目回调
  670. private function callbackYz(array $data = [])
  671. {
  672. $host = Config::get('app.yz_domain');
  673. $response = curl_request($host . 'api/callback_order_by_stock', $data);
  674. return json_decode($response, true);
  675. }
  676. }