handleYzOrderData.php 42 KB

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