handleYzOrderData.php 44 KB

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