handleYzOrderData.php 35 KB

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