handleYzOrderData.php 42 KB

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