handleYzOrderData.php 42 KB

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