Salezx.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\facade\Db;
  7. use app\admin\model\ActionLog;
  8. class Salezx extends \app\BaseController
  9. {
  10. public $post="";
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->post=$this->request->post();
  15. }
  16. public function create(){
  17. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  18. $orderCode=makeNo("QR");
  19. $customer_code= isset($this->post['customer_code'])&& $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  20. if($customer_code==""){
  21. return error_show(1002,"客户code不能为空");
  22. }
  23. $zxinfoNo= isset($this->post['zxinfoNo'])&& $this->post['zxinfoNo'] !=="" ? trim($this->post['zxinfoNo']) :"";
  24. if($zxinfoNo==""){
  25. return error_show(1002,"参数zxinfoNo 不能为空");
  26. }
  27. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  28. if($customer==false){
  29. return error_show(1004,"未找到客户数据");
  30. }
  31. $supplierNo= isset($this->post['supplierNo']) && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  32. if($supplierNo==""){
  33. return error_show(1002,"参数supplierNo不能为空");
  34. }
  35. $supplier=Db::name("business")->where(["companyNo"=>$supplierNo])->find();
  36. if($supplier==false){
  37. return error_show(1004,"未找到平台供应商数据");
  38. }
  39. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  40. if($sendtype==''){
  41. return error_show(1004,"参数sendtype不能为空");
  42. }
  43. $platform_id =isset($this->post['platform_id']) && $this->post['platform_id'] !=="" ?intval($this->post['platform_id']):"";
  44. if($platform_id==''){
  45. return error_show(1004,"参数platform_id不能为空");
  46. }
  47. $platform_order =isset($this->post['platform_order']) && $this->post['platform_order'] !=="" ?trim($this->post['platform_order']):"";
  48. if($platform_order==''){
  49. return error_show(1004,"参数platform_order不能为空");
  50. }
  51. $good_code =isset($this->post['good_code']) && $this->post['good_code'] !=="" ?trim($this->post['good_code']):"";
  52. if($good_code==''){
  53. return error_show(1004,"参数good_code不能为空");
  54. }
  55. $good=Db::name("good_zixun")->where(["spuCode"=>$good_code])->find();
  56. if($good==false){
  57. return error_show(1004,"未找到商品数据");
  58. }
  59. $goodinfo = Db::name("consult_bids")->where(["spuCode"=>$good['spuCode'],"is_del"=>0])->find();
  60. if($goodinfo==false){
  61. return error_show(1004,"未找到商品数据");
  62. }
  63. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  64. if($good_num===''){
  65. return error_show(1004,"参数good_num不能为空");
  66. }
  67. $good_num =isset($this->post['p']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  68. if($good_num===''){
  69. return error_show(1004,"参数good_num不能为空");
  70. }
  71. // $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  72. // if($goodtype===''){
  73. // return error_show(1004,"参数goodtype不能为空");
  74. // }
  75. $arrtime = isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  76. if($arrtime==""){
  77. return error_show(1004,"参数arrtime不能为空");
  78. }
  79. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  80. if($sendtype==1){
  81. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  82. if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
  83. return error_show(1004,"参数addrlist不能为空");
  84. }
  85. }
  86. $apply_id =GetUserInfo($token);
  87. if(empty($apply_id)||$apply_id['code']!=0){
  88. return error_show(1002,"申请人数据不存在");
  89. }
  90. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  91. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  92. Db::startTrans();
  93. try {
  94. $data=[
  95. "orderCode"=>$orderCode,
  96. "good_code"=>$good_code,
  97. "customer_code"=>$customer_code,
  98. "good_name"=>isset($good['good_name'])&&$good['good_name']!==''?$good['good_name']:'',
  99. "good_num"=>$good_num,
  100. "cat_id"=>$goodinfo['cat_id'],
  101. "apply_id"=>$rm,
  102. "apply_name"=>$ri,
  103. "origin_price"=>$goodinfo['total_fee'],
  104. "sale_price"=>$goodinfo['sale_price'],
  105. "post_fee"=>0,
  106. "status"=>0,
  107. "send_num"=>0,
  108. "wsend_num"=>$good_num,
  109. "send_status"=>1,
  110. "good_type"=>1,
  111. "send_type"=>$sendtype,
  112. "supplierNo"=>$supplierNo,
  113. "is_del"=>0,
  114. "zxNo"=>$zxinfoNo,
  115. "platform_order"=>$platform_order,
  116. "platform_id"=>$platform_id,
  117. "remark"=>$remark,
  118. "order_type"=>2,//非库存品
  119. "order_source"=>2,//咨询
  120. "addtime"=>date("Y-m-d H:i:s"),
  121. "updatetime"=>date("Y-m-d H:i:s"),
  122. 'total_price'=>round($goodinfo['sale_price']*$good_num,2),
  123. ];
  124. $datainfo = Db::name('sale')->insert($data,true);
  125. $vat=[];
  126. if($datainfo>0){
  127. $standing_bood_data=[
  128. "orderCode"=>$orderCode,
  129. "sale_id"=>$datainfo,
  130. "customer_code"=>$customer_code,
  131. "supplierNo"=>$supplierNo,
  132. "order_type"=>$data['order_type'],
  133. "order_source"=>$data['order_source'],
  134. "spuCode"=>$good_code,
  135. 'updatetime'=>date("Y-m-d H:i:s"),
  136. ];
  137. $bol= $this->createCgd($data,$good,$goodinfo,$standing_bood_data);
  138. if($bol==false){
  139. Db::rollback();
  140. return error_show(1002,"咨询订单创建失败");
  141. }
  142. if($sendtype!=2){
  143. foreach ($addrlist as $value){
  144. $temp=[];
  145. $addrs=[];
  146. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  147. $addrs['provice_code'] = $value['addr_code'][0];
  148. $addrs['city_code'] = $value['addr_code'][1];
  149. $addrs['area_code'] = $value['addr_code'][2];
  150. $addr = json_encode($addrs);
  151. }else{
  152. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  153. }
  154. $temp['orderCode']=$orderCode;
  155. $temp['contactor']=$value['contactor'];
  156. $temp['mobile'] = $value['mobile'];
  157. $temp['addr'] = $value['addr'];
  158. $temp['addr_code']=$addr;
  159. $temp['customer_code'] =$customer_code;
  160. $temp['receipt_quantity']=$value['receipt_quantity'];
  161. $temp['post_fee'] =0;
  162. $temp['is_del'] =0;
  163. $temp['addtime'] =date("Y-m-d H:i:s");
  164. $temp['updatetime'] =date("Y-m-d H:i:s");
  165. $temp['arrive_time']=$value['arrive_time'];
  166. $vmp = Db::name('order_addr')->insert($temp,true);
  167. if($vmp>0){
  168. $outCode=makeNo("DF");
  169. $out=[
  170. "orderCode"=>$orderCode,
  171. "outCode"=>$outCode,
  172. "apply_id"=>$rm,
  173. "apply_name"=>$ri,
  174. "addrid"=>$vmp,
  175. "post_name"=>"",
  176. "post_code"=>"",
  177. "post_fee"=>0,
  178. "sendtime"=>date("Y-m-d H:i:s"),
  179. "send_num"=>$value['receipt_quantity'],
  180. "check_num"=>0,
  181. "error_num"=>0,
  182. "wsm_code"=>"",
  183. "order_type"=>2,
  184. "status"=>0,
  185. "addtime"=>date("Y-m-d H:i:s"),
  186. "updatetime"=>date("Y-m-d H:i:s")
  187. ];
  188. $ou =Db::name("order_out")->insertGetId($out);
  189. if($ou==false){
  190. Db::rollback();
  191. return error_show(1002,"咨询订单创建失败");
  192. }else{
  193. //修改状态,添加待办
  194. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  195. "order_code" => $outCode,//出库单号
  196. "status" => $out['status'],//这里的status是之前的值
  197. "action_remark" => '',//备注
  198. "action_type" => "create"//新建create,编辑edit,更改状态status
  199. ], "CKD", $out['status'], $out);
  200. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  201. "order_type" => 'CKD',
  202. "order_code" => $outCode,//出库单号
  203. "order_id" => $ou,
  204. "order_status" => $out['status'],
  205. 'before_status'=>0
  206. ]);
  207. $standing_bood_data['outCode'][]=$outCode;
  208. }
  209. }else{
  210. Db::rollback();
  211. return error_show(1002,"咨询订单创建失败");
  212. }
  213. }
  214. }
  215. //修改状态,添加待办
  216. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  217. "order_code" => $orderCode,//销售单code
  218. "status" => 0,//这里的status是之前的值
  219. "action_remark" => '',//备注
  220. "action_type" => "create"//新建create,编辑edit,更改状态status
  221. ], "XSQRD", 0, $data);
  222. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  223. "order_type" => 'XSQRD',
  224. "order_code" => $orderCode,//销售单code
  225. "order_id" => $datainfo,
  226. "order_status" => 0
  227. ]);
  228. //根据zxinfoNo查找infoNo,找到则更新台账,否则就新增台账
  229. if (isset($standing_bood_data['outCode'])) $standing_bood_data['outCode'] = implode(',', $standing_bood_data['outCode']);
  230. $infoNo = Db::name('consult_bids')->where('bidNo', $zxinfoNo)->value('infoNo');
  231. if (empty($infoNo)) Db::name('standing_book')->insert(array_merge($standing_bood_data, ['addtime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo("IO")]));
  232. else Db::name('standing_book')->where('infoNo', $infoNo)->update($standing_bood_data);
  233. Db::commit();
  234. return error_show(0,"咨询订单创建成功");
  235. }
  236. Db::rollback();
  237. return error_show(1002,"咨询订单创建失败");
  238. }catch(\Exception $e){
  239. Db::rollback();
  240. return error_show(1005,$e->getMessage());
  241. }
  242. }
  243. public function createCgd($data,$good,$good_info,array &$standing_bood_data=[]){
  244. $cgdCode = makeNo("CG");
  245. $supplier=Db::name("supplier")->where(["code"=>$good['supplierNo'],"is_del"=>0])->find();
  246. if($supplier==false){
  247. return false;
  248. }
  249. $wsm= Db::name("warehouse_info")->where(["supplierNo"=>$good["supplierNo"],"companyNo"=>$data['supplierNo'],"is_del"=>0])->find();
  250. if($wsm==false){
  251. $wsm_code = makeNo("WSM");
  252. $inwsm=[
  253. "wsm_code"=>$wsm_code,
  254. "name"=>$supplier['name'],
  255. "wsm_type"=>2,
  256. "supplierNo"=>$supplier['code'],
  257. "addr"=>"",
  258. "addrs_code"=>"",
  259. "contactor"=>0,
  260. "contactor_name"=>0,
  261. "mobile"=>"",
  262. "position"=>"",
  263. "companyNo"=>$data['supplierNo'],
  264. "status"=>1,
  265. "is_del"=>0,
  266. "addtime"=>date("Y-m-d H:i:s"),
  267. "updatetime"=>date("Y-m-d H:i:s")
  268. ];
  269. $in = Db::name("warehouse_info")->insert($inwsm);
  270. if($in==false){
  271. return false;
  272. }
  273. }else{
  274. $wsm_code =$wsm['wsm_code'];
  275. }
  276. $cg =["cgdNo"=>$cgdCode,
  277. "bkcode"=>"",
  278. "wsm_code"=>$wsm_code,
  279. "cgder"=>$supplier['person'],
  280. "cgder_id"=>$supplier['personid'],
  281. "good_code"=>$good['spuCode'],
  282. "good_name"=>$good['good_name'],
  283. "good_num"=>$data['good_num'],
  284. "good_price"=>$good_info['sale_price'],
  285. "total_fee"=>$good_info['total_fee'],
  286. "pakge_fee"=>$good_info['pakge_fee'],
  287. "cert_fee"=>$good_info['cert_fee'],
  288. "open_fee"=>$good_info['open_fee'],
  289. "delivery_fee"=>$good_info['delivery_fee'],
  290. "mark_fee"=>$good_info['mark_fee'],
  291. "demo_fee"=>$good_info['demo_fee'],
  292. #"weight"=>$data['weight'],
  293. "diff_weight"=>"0",
  294. "diff_fee"=>"0",
  295. "gold_price"=>"0",
  296. "supplierNo"=>$good['supplierNo'],
  297. "supplier_name"=>$supplier['name'],
  298. "send_status"=>1,
  299. "send_num"=>"0",
  300. "wsend_num"=>$data['good_num'],
  301. "remark"=>"",
  302. "status"=>0,
  303. "lasttime"=>date("Y-m-d H:i:s"),
  304. "is_del"=>0,
  305. "order_type"=>$data['order_type'],
  306. "order_source"=>$data['order_source'],
  307. "good_type"=>$good['good_type'],
  308. "addtime"=>date("Y-m-d H:i:s"),
  309. "updatetime"=>date("Y-m-d H:i:s")
  310. ];
  311. $up =Db::name("purchease_order")->insert($cg,true);
  312. if($up){
  313. //修改状态,添加待办
  314. ActionLog::logAdd($this->post['token'], [
  315. "order_code" => $cg['cgdNo'],//销售单code
  316. "status" => 0,//这里的status是之前的值
  317. "action_remark" => '',//备注
  318. "action_type" => "create"//新建create,编辑edit,更改状态status
  319. ], "CGD", $cg['status'], $cg);
  320. ProcessOrder::AddProcess($this->post['token'], [
  321. "order_type" => 'CGD',
  322. "order_code" => $cg['cgdNo'],//销售单code
  323. "order_id" => $up,
  324. "order_status" => $cg['status']
  325. ]);
  326. $standing_bood_data['cgdNo']=$cgdCode;
  327. return true;
  328. }else{
  329. return false;
  330. }
  331. }
  332. // public function edit(){
  333. // $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  334. //
  335. // $zxNo=isset($this->post['id']) && $this->post['id'] !=="" ?intval($this->post['id']):"";
  336. // if($zxNo==''){
  337. // return error_show(1004,"参数id不能为空");
  338. // }
  339. // $saleinfo = Db::name("sale")->where(["id"=>$zxNo,"is_del"=>0])->find();
  340. // if($saleinfo==false){
  341. // return error_show(1003,"未找到咨询单数据");
  342. // }
  343. // $zxinfo = Db::name("consult")->where(["zxNo"=>$saleinfo['zxNo'],"is_del"=>0])->find();
  344. // if($zxinfo==false){
  345. // return error_show(1004,"未找到咨询单数据");
  346. // }
  347. // $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  348. // if($customer_code==""){
  349. // return error_show(1002,"客户code不能为空");
  350. // }
  351. // $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  352. // if($customer==false){
  353. // return error_show(1004,"未找到客户数据");
  354. // }
  355. // $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  356. // if($supplierNo==""){
  357. // return error_show(1002,"参数supplierNo不能为空");
  358. // }
  359. // $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  360. // if($supplier==false){
  361. // return error_show(1004,"未找到平台供应商数据");
  362. // }
  363. // $wsm=Db::name("warehouse_info")->where(["supplierNo"=>$zxinfo['gysNo'],"wsm_type"=>2])->find();
  364. // if(empty($wsm)){
  365. // return error_show(1004,"未找到供应商仓库数据");
  366. // }
  367. // $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  368. // if($sendtype==''){
  369. // return error_show(1004,"参数sendtype不能为空");
  370. // }
  371. // $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  372. // if($good_num===''){
  373. // return error_show(1004,"参数good_num不能为空");
  374. // }
  375. // $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  376. // if($goodtype===''){
  377. // return error_show(1004,"参数goodtype不能为空");
  378. // }
  379. // $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  380. // if($sendtype!=2){
  381. // $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  382. // if($addrlist=="" || empty($addrlist)){
  383. // return error_show(1004,"参数addrlist不能为空");
  384. // }
  385. // }
  386. // $apply_id =GetUserInfo($token);
  387. // if(empty($apply_id)||$apply_id['code']!=0){
  388. // return error_show(1002,"申请人数据不存在");
  389. // }
  390. // $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  391. // $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  392. // Db::startTrans();
  393. // try {
  394. // $data=[
  395. // "customer_code"=>$customer_code,
  396. // "good_num"=>$good_num,
  397. // "apply_id"=>$rm,
  398. // "apply_name"=>$ri,
  399. // "wsend_num"=>$good_num,
  400. // "good_type"=>$goodtype,
  401. // "send_type"=>$sendtype,
  402. // "supplierNo"=>$supplierNo,
  403. // "remark"=>$remark,
  404. // "updatetime"=>date("Y-m-d H:i:s"),
  405. // 'total_price'=>round($zxinfo['last_price']*$good_num,2),
  406. // ];
  407. // $datainfo = Db::name('sale')->where(["id"=>$zxNo])->save($data);
  408. // if($datainfo!=false){
  409. // $st = ["order_code"=>$saleinfo['orderCode'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  410. // ActionLog::logAdd($this->post['token'],$st,"ZXQRD",0,$data);
  411. // $order=["order_type"=>"ZXQRD","order_code"=>$saleinfo['orderCode'],"order_id"=>$saleinfo['id'],"order_status"=>$saleinfo['status']];
  412. // ProcessOrder::AddProcess($this->post['token'],$order);
  413. // $saleinfos=[
  414. // "wsm_code"=>$wsm['wsm_code'],
  415. // "num"=>$good_num,
  416. // "sale_price"=>$zxinfo['last_price'],
  417. // "total_price"=>round($zxinfo['last_price']*$good_num,2),
  418. // "updatetime"=>date("Y-m-d H:i:s"),
  419. // ];
  420. // $info = Db::name("sale_info")->where(["orderCode"=>$saleinfo['orderCode']])->save($saleinfos);
  421. // if($info==false){
  422. // Db::rollback();
  423. // return error_show(1002,"咨询订单创建失败");
  424. // }
  425. // if($sendtype!=2){
  426. // foreach ($addrlist as $value){
  427. // $temp=[];
  428. // $addrs=[];
  429. // if($value['addr_code']!==''&&is_array($value['addr_code'])){
  430. // $addrs['provice_code'] = $value['addr_code'][0];
  431. // $addrs['city_code'] = $value['addr_code'][1];
  432. // $addrs['area_code'] = $value['addr_code'][2];
  433. // $addr = json_encode($addrs);
  434. // }else{
  435. // $addr = isset($value['addr_code'])?$value['addr_code']:'';
  436. // }
  437. // isset($value["id"])&&$value["id"]!=""? $temp['id']=$value['id']:"";
  438. // $temp['contactor']=$value['contactor'];
  439. // $temp['mobile'] = $value['mobile'];
  440. // $temp['addr'] = $value['addr'];
  441. // $temp['addr_code']=$addr;
  442. // $temp['orderCode']=$saleinfo['orderCode'];
  443. // $temp['customer_code'] =$customer_code;
  444. // $temp['receipt_quantity']=$value['receipt_quantity'];
  445. // $temp['post_fee'] =0;
  446. // $temp['is_del'] =$value['is_del'];
  447. // $temp['updatetime'] =date("Y-m-d H:i:s");
  448. // isset($value["id"])&&$value["id"]!=""?"":$temp['addtime'] =date("Y-m-d H:i:s");
  449. // $temp['arrive_time']=$value['arrive_time'];
  450. // $vmp = Db::name('order_addr')->save($temp);
  451. // if(!$vmp){
  452. // Db::rollback();
  453. // return error_show(0,"咨询订单地址更新失败");
  454. // }
  455. // }
  456. // }else{
  457. // if($saleinfo['send_type']==1 && $sendtype==2){
  458. // $up=Db::name('order_addr')->where(["orderCode"=>$saleinfo['orderCode']])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  459. // if(!$up){
  460. // Db::rollback();
  461. // return error_show(0,"咨询订单地址更新失败");
  462. // }
  463. // }
  464. // }
  465. // Db::commit();
  466. // return error_show(0,"咨询订单更新成功");
  467. // }
  468. // Db::rollback();
  469. // return error_show(1002,"咨询订单更新失败");
  470. // }catch(\Exception $e){
  471. // Db::rollback();
  472. // return error_show(1005,$e->getMessage());
  473. // }
  474. // }
  475. public function out(){
  476. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  477. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode']) :"";
  478. if($outCode==""){
  479. return error_show(1002,"参数outCode不能为空");
  480. }
  481. $der = Db::name('order_out')->where(['outCode'=>$outCode])->find();
  482. if($der==""){
  483. return error_show(1002,"未找到出库单数据");
  484. }
  485. $order= Db::name('sale')->where(['orderCode'=>$der['orderCode']])->find();
  486. if(empty($order)){
  487. return error_show(1002,"未找到咨询订单信息");
  488. }
  489. $stock= Db::name("good_stock")->where(["spuCode"=>$order['good_code'],"wsm_code"=>$der['wsm_code']])
  490. ->find();
  491. if(empty($stock)){
  492. return error_show(1002,"未找到仓库库存信息");
  493. }
  494. if($der['send_num'] > $stock['wait_out_stock']){
  495. return error_show(1002,"仓库库存不足");
  496. }
  497. $post_name= isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  498. if($post_name==""){
  499. return error_show(1002,"物流公司不能为空");
  500. }
  501. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  502. if($post_code==""){
  503. return error_show(1002,"物流单号不能为空");
  504. }
  505. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? intval($this->post['post_fee']) :"";
  506. if($post_fee==""){
  507. return error_show(1002,"物流费不能为空");
  508. }
  509. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  510. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  511. Db::startTrans();
  512. try {
  513. $var = $der['status'];
  514. $der['post_name'] = $post_name;
  515. $der['post_code'] = $post_code;
  516. $der['post_fee'] = $post_fee;
  517. $der['sendtime'] = $sendtime;
  518. $der['status'] = 1;
  519. $der['updatetime'] = date("Y-m-d H:i:s");
  520. $datainfo = Db::name('order_out')->save($der);
  521. $st = ["order_code"=>$outCode,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  522. ActionLog::logAdd($this->post['token'],$st,"resign_info",$der['status'] ,$st);
  523. if($datainfo) {
  524. $st = ["order_code"=>$der['outCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  525. ActionLog::logAdd($this->post['token'],$st,"CKD",1,$der);
  526. $ord=["order_type"=>"CKD","order_code"=>$der['outCode'],"order_id"=>$der['id'], "order_status"=>$der['status']];
  527. ProcessOrder::AddProcess($this->post['token'],$ord);
  528. // $send =Db::name("order_out")->where(["wsm_code"=>$der['wsm_code'],'orderCode'=>$der['orderCode']])->sum("send_num");
  529. // $order['send_num']+= $der['send_num'];
  530. // $order['wsend_num']-=$der['send_num'] ;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  531. // $order['send_status']=$order['send_num']==0 ? 1 :($order['wsend_num']==0?3:2);
  532. // $order['status']=$order['send_num']==0 ? 2 :($order['wsend_num']==0?4:3);
  533. // $order['updatetime'] = date("Y-m-d H:i:s");
  534. // $si = Db::name('sale')->save($order);
  535. // if($si==false){
  536. // Db::rollback();
  537. // return error_show(1003,"更新失败");
  538. // }
  539. $str = Db::name('good_stock')->where(['wsm_code' => $der['wsm_code'],'spuCode'=>$order['good_code']])
  540. ->find();
  541. if ($str == false) {
  542. Db::rollback();
  543. return error_show(1002, "商品数据未找到");
  544. }
  545. if ($der['send_num'] > $str['wait_out_stock']) {
  546. Db::rollback();
  547. return error_show(1002, "超出库存数量");
  548. }
  549. $str['wait_out_stock'] -= $der['send_num'];
  550. $str['intra_stock'] += $der['send_num'];
  551. $str['updatetime'] = date("Y-m-d H:i:s");
  552. $stre = Db::name('good_stock')->save($str);
  553. if ($stre == false) {
  554. Db::rollback();
  555. return error_show(1002, "状态更新失败");
  556. }
  557. //商品变动日志表,good_log_code字段存储销售订单编号
  558. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $str['id'], "type" => 1, 'stock' => $der['send_num'], "stock_name" => "intra_stock"];
  559. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $str['id'], "type" => 2, 'stock' => $der['send_num'], "stock_name" => "wait_out_stock"];
  560. GoodLog::LogAdd($this->post['token'], $good_data, 'CKD');
  561. Db::commit();
  562. return error_show(0,"创建成功");
  563. }else{
  564. Db::rollback();
  565. return error_show(1003,"创建失败");
  566. }
  567. }catch (\Exception $e) {
  568. Db::rollback();
  569. return error_show(1005, $e->getMessage());
  570. }
  571. }
  572. public function outaddr(){
  573. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  574. $outCode=makeNo("DF");
  575. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  576. if($orderCode==""){
  577. return error_show(1002,"参数orderCoder不能为空");
  578. }
  579. $der = Db::name('sale')->where(['orderCode'=>$orderCode])->find();
  580. if($der==""){
  581. return error_show(1002,"未找到出库订单单号");
  582. }
  583. if($der['order_type']!=2){
  584. return error_show(1002,"订单不是咨询单无法出库");
  585. }
  586. $wsm= Db::name('sale_info')->where(['orderCode'=>$der['orderCode']])->find();
  587. if(empty($wsm)){
  588. return error_show(1002,"未找到仓库信息");
  589. }
  590. $apply_id =GetUserInfo($token);
  591. if(empty($apply_id)||$apply_id['code']!=0){
  592. return error_show(1002,"申请人数据不存在");
  593. }
  594. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  595. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  596. if($der['send_type']==2){
  597. $contactor= isset($this->post['contactor']) && $this->post['contactor'] !=="" ? trim($this->post['contactor'])
  598. :"";
  599. if($contactor==""){
  600. return error_show(1002,"收货人不能为空");
  601. }
  602. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !=="" ? trim($this->post['mobile'])
  603. :"";
  604. if($mobile==""){
  605. return error_show(1002,"收货人联系方式不能为空");
  606. }
  607. $addr = isset($this->post['addr']) && $this->post['addr'] !=="" ? trim($this->post['addr']) :"";
  608. if($addr==""){
  609. return error_show(1002,"参数addr不能为空");
  610. }
  611. $receipt_quantity= isset($this->post['receipt_quantity']) && $this->post['receipt_quantity'] !=="" ? intval
  612. ($this->post['receipt_quantity']) :"";
  613. if($receipt_quantity===""){
  614. return error_show(1002,"参数receipt_quantity不能为空");
  615. }
  616. $addrcode = isset($this->post['addr_code']) && $this->post['addr_code'] !=="" ? $this->post['addr_code'] :"";
  617. if($addrcode==""){
  618. return error_show(1002,"参数addr_code不能为空");
  619. }
  620. $addrs=[];
  621. if(is_array($addrcode)){
  622. $addrs['provice_code'] = $addrcode['addr_code'][0];
  623. $addrs['city_code'] = $addrcode['addr_code'][1];
  624. $addrs['area_code'] = $addrcode['addr_code'][2];
  625. $addrstr = json_encode($addrs);
  626. }else{
  627. $addrstr = $addrcode;
  628. }
  629. }else{
  630. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !=="" ? intval($this->post['addrid']) :"";
  631. $dr = Db::name('order_addr')->where(['id'=>$addrid,"orderCode"=>$orderCode,'is_del'=>0])->find();
  632. if(empty($dr)){
  633. return error_show(1003,"收货人信息未找到");
  634. }
  635. $receipt_quantity = $dr['receipt_quantity'];
  636. }
  637. // if($send_num>$stock['usable_stock']){
  638. // return error_show(1002,"超出可发货数量");
  639. // }
  640. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  641. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  642. Db::startTrans();
  643. try {
  644. if($der['send_type']==2){
  645. $addr=[
  646. "orderCode"=>$orderCode,
  647. "addr"=>$addr,
  648. "addr_code"=>$addrstr,
  649. "contactor"=>$contactor,
  650. "mobile"=>$mobile,
  651. "customer_code"=>$der['customer_code'],
  652. "post_fee"=>0,
  653. "is_del"=>0,
  654. "receipt_quantity"=>$receipt_quantity,
  655. "addtime"=>date("Y-m-d H:i:s"),
  656. "updatetime"=>date("Y-m-d H:i:s"),
  657. "arrive_time"=>date("Y-m-d H:i:s"),
  658. ];
  659. $addrid = Db::name('order_addr')->insert($addr,true);
  660. if($addrid<=0){
  661. $st = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  662. ActionLog::logAdd($this->post['token'],$st,"zxd",0,$st);
  663. Db::rollback();
  664. return error_show(1003,"创建失败");
  665. }
  666. }
  667. $data=[
  668. "wsm_code"=>$wsm['wsm_code'],
  669. "orderCode"=>$orderCode,
  670. "outCode"=>$outCode,
  671. "order_type"=>$der['order_type'],
  672. "apply_id"=>$rm,
  673. "apply_name"=>$ri,
  674. "addrid"=>$addrid,
  675. "post_name"=>'',
  676. "post_code"=>'',
  677. "post_fee"=>'',
  678. "sendtime"=>$sendtime,
  679. "send_num"=>$receipt_quantity,
  680. "check_num"=>0,
  681. "error_num"=>0,
  682. "status"=>0,
  683. "addtime"=>date("Y-m-d H:i:s"),
  684. "updatetime"=>date("Y-m-d H:i:s")
  685. ];
  686. $datainfo = Db::name('order_out')->insert($data,true);
  687. if($datainfo<=0){
  688. Db::rollback();
  689. return error_show(1003,"创建失败");
  690. }
  691. $sio = ["order_code"=>$outCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  692. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri],$sio,"CKD",0,$data);
  693. $order=["order_type"=>"CKD","order_code"=>$data['outCode'],"order_id"=>$datainfo,"order_status"=>$data['status'],'before_status'=>0];
  694. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri],$order);
  695. $item = $der['status'];
  696. $der['send_num']+= $receipt_quantity;
  697. $der['wsend_num']-=$receipt_quantity;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  698. $der['send_status']=$der['send_num']==0 ? 1 :($der['wsend_num']==0?3:2);
  699. $der['status']=$der['send_num']==0 ? 2 :($der['wsend_num']==0?4:3);
  700. $der['updatetime'] = date("Y-m-d H:i:s");
  701. $si = Db::name('sale')->save($der);
  702. if($si==false){
  703. Db::rollback();
  704. return error_show(1003,"创建失败");
  705. }else{
  706. //修改状态,添加待办
  707. ActionLog::logAdd($this->post['token'], [
  708. "order_code" => $der['orderCode'],//销售单code
  709. "status" => $item,//这里的status是之前的值
  710. "action_remark" => '',//备注
  711. "action_type" => "status"//新建create,编辑edit,更改状态status
  712. ], "XSQRD", $der['status'], $der);
  713. ProcessOrder::AddProcess($this->post['token'], [
  714. "order_type" => 'XSQRD',
  715. "order_code" => $der['orderCode'],//销售单code
  716. "order_id" => $der['id'],
  717. "order_status" => $der['status']
  718. ]);
  719. }
  720. $sio = ["order_code"=>$orderCode,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  721. ActionLog::logAdd($this->post['token'],$sio,"zxd",$der['status'],$der);
  722. $order=["order_type"=>"CKD","order_code"=>$orderCode,"order_id"=>$der['id'],"order_status"=>$der['status']];
  723. ProcessOrder::AddProcess($this->post['token'],$order);
  724. Db::commit();
  725. return error_show(0,"创建成功");
  726. }catch (\Exception $e) {
  727. Db::rollback();
  728. return error_show(1005, $e->getMessage());
  729. }
  730. }
  731. }