handleYzOrderData.php 39 KB

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