handleYzOrderData.php 45 KB

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