handleYzOrderData.php 34 KB

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