handleYzOrderData.php 35 KB

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