handleYzOrderData.php 35 KB

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