handleYzOrderData.php 45 KB

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