handleYzOrderData.php 35 KB

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