Salezx.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\facade\Db;
  5. use app\admin\model\ActionLog;
  6. class Salezx extends \app\BaseController
  7. {
  8. public $post="";
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post=$this->request->post();
  13. }
  14. public function create(){
  15. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  16. $orderCode=makeNo("CX");
  17. $zxNo=isset($this->post['zxNo']) && $this->post['zxNo'] !=="" ?trim($this->post['zxNo']):"";
  18. if($zxNo==''){
  19. return error_show(1004,"参数zxNo不能为空");
  20. }
  21. $zxinfo = Db::name("consult")->where(["zxNo"=>$zxNo,"is_del"=>0])->find();
  22. if($zxinfo==false){
  23. return error_show(1004,"未找到咨询单数据");
  24. }
  25. $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  26. if($customer_code==""){
  27. return error_show(1002,"客户code不能为空");
  28. }
  29. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  30. if($customer==false){
  31. return error_show(1004,"未找到客户数据");
  32. }
  33. $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  34. if($supplierNo==""){
  35. return error_show(1002,"参数supplierNo不能为空");
  36. }
  37. $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  38. if($supplier==false){
  39. return error_show(1004,"未找到平台供应商数据");
  40. }
  41. $wsm=Db::name("warehouse_info")->where(["supplierNo"=>$zxinfo['gysNo'],"wsm_type"=>2])->find();
  42. if(empty($wsm)){
  43. return error_show(1004,"未找到供应商仓库数据");
  44. }
  45. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  46. if($sendtype==''){
  47. return error_show(1004,"参数sendtype不能为空");
  48. }
  49. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  50. if($good_num===''){
  51. return error_show(1004,"参数good_num不能为空");
  52. }
  53. $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  54. if($goodtype===''){
  55. return error_show(1004,"参数goodtype不能为空");
  56. }
  57. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  58. if($sendtype!=2){
  59. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  60. if($addrlist=="" || empty($addrlist)){
  61. return error_show(1004,"参数addrlist不能为空");
  62. }
  63. }
  64. $apply_id =GetUserInfo($token);
  65. if(empty($apply_id)||$apply_id['code']!=0){
  66. return error_show(1002,"申请人数据不存在");
  67. }
  68. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  69. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  70. Db::startTrans();
  71. try {
  72. $data=[
  73. "orderCode"=>$orderCode,
  74. "good_code"=>isset($zxinfo['cpNo'])&&$zxinfo['cpNo']!==''?$zxinfo['cpNo']:'',
  75. "customer_code"=>$customer_code,
  76. "good_name"=>isset($zxinfo['cpName'])&&$zxinfo['cpName']!==''?$zxinfo['cpName']:'',
  77. "good_num"=>$good_num,
  78. "apply_id"=>$rm,
  79. "apply_name"=>$ri,
  80. "origin_price"=>$zxinfo['sale_fee'],
  81. "sale_price"=>$zxinfo['last_price'],
  82. "post_fee"=>$zxinfo['delivery_fee'],
  83. "status"=>0,
  84. "send_num"=>0,
  85. "wsend_num"=>$good_num,
  86. "send_status"=>1,
  87. "good_type"=>$goodtype,
  88. "send_type"=>$sendtype,
  89. "supplierNo"=>$supplierNo,
  90. "is_del"=>0,
  91. "zxNo"=>$zxNo,
  92. "remark"=>$remark,
  93. "order_type"=>2,
  94. "addtime"=>date("Y-m-d H:i:s"),
  95. "updatetime"=>date("Y-m-d H:i:s"),
  96. 'total_price'=>round($zxinfo['last_price']*$good_num,2),
  97. ];
  98. $datainfo = Db::name('sale')->insert($data,true);
  99. $vat=[];
  100. if($datainfo>0){
  101. $st = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  102. ActionLog::logAdd($this->post['token'],$st,"zxd",0,$st);
  103. $addgood=$this->addGood($zxinfo);
  104. if($addgood==false){
  105. Db::rollback();
  106. return error_show(1002,"咨询订单创建商品失败");
  107. }
  108. $saleinfo=[
  109. "orderCode"=>$orderCode,
  110. "wsm_code"=>$wsm['wsm_code'],
  111. "num"=>$good_num,
  112. "sale_price"=>$zxinfo['last_price'],
  113. "total_price"=>round($zxinfo['last_price']*$good_num,2),
  114. "addtime"=>date("Y-m-d H:i:s"),
  115. "updatetime"=>date("Y-m-d H:i:s"),
  116. ];
  117. $info = Db::name("sale_info")->insert($saleinfo);
  118. if($info==false){
  119. Db::rollback();
  120. return error_show(1002,"咨询订单创建失败");
  121. }
  122. if($sendtype!=2){
  123. foreach ($addrlist as $value){
  124. $temp=[];
  125. $addrs=[];
  126. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  127. $addrs['provice_code'] = $value['addr_code'][0];
  128. $addrs['city_code'] = $value['addr_code'][1];
  129. $addrs['area_code'] = $value['addr_code'][2];
  130. $addr = json_encode($addrs);
  131. }else{
  132. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  133. }
  134. $temp['orderCode']=$orderCode;
  135. $temp['contactor']=$value['contactor'];
  136. $temp['mobile'] = $value['mobile'];
  137. $temp['addr'] = $value['addr'];
  138. $temp['addr_code']=$addr;
  139. $temp['customer_code'] =$customer_code;
  140. $temp['receipt_quantity']=$value['receipt_quantity'];
  141. $temp['post_fee'] =0;
  142. $temp['is_del'] =0;
  143. $temp['addtime'] =date("Y-m-d H:i:s");
  144. $temp['updatetime'] =date("Y-m-d H:i:s");
  145. $temp['arrive_time']=$value['arrive_time'];
  146. $vat[]=$temp;
  147. }
  148. $vmp = Db::name('order_addr')->insertAll($vat);
  149. if($vmp){
  150. Db::commit();
  151. return error_show(0,"咨询订单创建成功");
  152. }
  153. }else{
  154. Db::commit();
  155. return error_show(0,"咨询订单创建成功");
  156. }
  157. }
  158. Db::rollback();
  159. return error_show(1002,"咨询订单创建失败");
  160. }catch(\Exception $e){
  161. Db::rollback();
  162. return error_show(1005,$e->getMessage());
  163. }
  164. }
  165. /**咨询新建商品
  166. * @param $data
  167. * @return bool
  168. */
  169. public function addGood($data){
  170. $isgood = Db::name("good")->where(["good_code"=>"GD-".$data["cpNo"]])->find();
  171. if($isgood){
  172. return true;
  173. }
  174. $good=[
  175. "good_code"=>"GD-".$data["cpNo"],
  176. "item"=>'咨询订单',
  177. "good_name"=>$data["cpName"],
  178. "good_desc"=>$data["desc"],
  179. "brand"=>$data["brank"],
  180. "original_price"=>$data["sale_fee"],
  181. "cg_saler"=>$data["saler"],
  182. "good_type"=>$data["zxtype"],
  183. "unit"=>$data["unit"],
  184. "good_addr"=>$data["addr"],
  185. "gys_code"=>$data["gysNo"],
  186. "market_fee"=>$data["sale_fee"],
  187. "is_online"=>'产品上线',
  188. "sort_f"=>$data["cat_f"],
  189. "sort_s"=>$data["cat_s"],
  190. "cat_id"=>$data["cat_id"],
  191. "status"=>1,
  192. "is_del"=>0,
  193. "addtime"=>date("Y-m-d H:i:s"),
  194. "updatetime"=>date("Y-m-d H:i:s")
  195. ];
  196. $insert = Db::name("good")->insert($good);
  197. if($insert){
  198. $st = ["order_code"=>$data["cpNo"],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  199. ActionLog::logAdd($this->post['token'],$st,"resign_info",1,$st);
  200. $type=[
  201. "type_code"=>$data['cpNo'],
  202. "good_code"=>"GD-".$data["cpNo"],
  203. "color"=>$data["color"],
  204. "material"=>$data["material"],
  205. "attribute"=>$data["model"],
  206. "specs"=>$data["specs"],
  207. "is_diff"=>$data["is_diff"],
  208. "cert_fee"=>$data["cert_fee"],
  209. "packing_fee"=>$data["package_fee"],
  210. "mark_fee"=>$data["mark_fee"],
  211. "cost_fee"=>$data["bare_fee"],
  212. "logistics_fee"=>$data["delivery_fee"],
  213. "logistics"=>$data["delivery_day"],
  214. "min_num"=>$data["min_num"],
  215. "unit_price"=>$data["sale_price"],
  216. "status"=>1,
  217. "is_del"=>0,
  218. "addtime"=>date("Y-m-d H:i:s"),
  219. "updatetime"=>date("Y-m-d H:i:s"),
  220. ];
  221. $ert = Db::name("good_type")->insert($type);
  222. if($ert){
  223. $sti = ["order_code"=>$data["cpNo"],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  224. ActionLog::logAdd($this->post['token'],$sti,"resign_info",1,$sti);
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. public function edit(){
  231. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  232. $zxNo=isset($this->post['id']) && $this->post['id'] !=="" ?intval($this->post['id']):"";
  233. if($zxNo==''){
  234. return error_show(1004,"参数id不能为空");
  235. }
  236. $saleinfo = Db::name("sale")->where(["id"=>$zxNo,"is_del"=>0])->find();
  237. if($saleinfo==false){
  238. return error_show(1003,"未找到咨询单数据");
  239. }
  240. $zxinfo = Db::name("consult")->where(["zxNo"=>$saleinfo['zxNo'],"is_del"=>0])->find();
  241. if($zxinfo==false){
  242. return error_show(1004,"未找到咨询单数据");
  243. }
  244. $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  245. if($customer_code==""){
  246. return error_show(1002,"客户code不能为空");
  247. }
  248. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  249. if($customer==false){
  250. return error_show(1004,"未找到客户数据");
  251. }
  252. $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  253. if($supplierNo==""){
  254. return error_show(1002,"参数supplierNo不能为空");
  255. }
  256. $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  257. if($supplier==false){
  258. return error_show(1004,"未找到平台供应商数据");
  259. }
  260. $wsm=Db::name("warehouse_info")->where(["supplierNo"=>$zxinfo['gsyNo'],"wsm_type"=>2])->find();
  261. if(empty($wsm)){
  262. return error_show(1004,"未找到供应商仓库数据");
  263. }
  264. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  265. if($sendtype==''){
  266. return error_show(1004,"参数sendtype不能为空");
  267. }
  268. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  269. if($good_num===''){
  270. return error_show(1004,"参数good_num不能为空");
  271. }
  272. $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  273. if($goodtype===''){
  274. return error_show(1004,"参数goodtype不能为空");
  275. }
  276. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  277. if($sendtype!=2){
  278. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  279. if($addrlist=="" || empty($addrlist)){
  280. return error_show(1004,"参数addrlist不能为空");
  281. }
  282. }
  283. $apply_id =GetUserInfo($token);
  284. if(empty($apply_id)||$apply_id['code']!=0){
  285. return error_show(1002,"申请人数据不存在");
  286. }
  287. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  288. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  289. Db::startTrans();
  290. try {
  291. $data=[
  292. "customer_code"=>$customer_code,
  293. "good_num"=>$good_num,
  294. "apply_id"=>$rm,
  295. "apply_name"=>$ri,
  296. "wsend_num"=>$good_num,
  297. "good_type"=>$goodtype,
  298. "send_type"=>$sendtype,
  299. "supplierNo"=>$supplierNo,
  300. "remark"=>$remark,
  301. "updatetime"=>date("Y-m-d H:i:s"),
  302. 'total_price'=>round($zxinfo['last_price']*$good_num,2),
  303. ];
  304. $datainfo = Db::name('sale')->where(["id"=>$zxNo])->save($data);
  305. if($datainfo!=false){
  306. $st = ["order_code"=>$customer_code,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  307. ActionLog::logAdd($this->post['token'],$st,"resign_info",0,$st);
  308. $saleinfos=[
  309. "wsm_code"=>$wsm['wsm_code'],
  310. "num"=>$good_num,
  311. "sale_price"=>$zxinfo['last_price'],
  312. "total_price"=>round($zxinfo['last_price']*$good_num,2),
  313. "updatetime"=>date("Y-m-d H:i:s"),
  314. ];
  315. $info = Db::name("sale_info")->where(["orderCode"=>$saleinfo['orderCode']])->save($saleinfos);
  316. if($info==false){
  317. Db::rollback();
  318. return error_show(1002,"咨询订单创建失败");
  319. }
  320. if($sendtype!=2){
  321. foreach ($addrlist as $value){
  322. $temp=[];
  323. $addrs=[];
  324. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  325. $addrs['provice_code'] = $value['addr_code'][0];
  326. $addrs['city_code'] = $value['addr_code'][1];
  327. $addrs['area_code'] = $value['addr_code'][2];
  328. $addr = json_encode($addrs);
  329. }else{
  330. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  331. }
  332. isset($value["id"])&&$value["id"]!=""? $temp['id']=$value['id']:"";
  333. $temp['contactor']=$value['contactor'];
  334. $temp['mobile'] = $value['mobile'];
  335. $temp['addr'] = $value['addr'];
  336. $temp['addr_code']=$addr;
  337. $temp['orderCode']=$saleinfo['orderCode'];
  338. $temp['customer_code'] =$customer_code;
  339. $temp['receipt_quantity']=$value['receipt_quantity'];
  340. $temp['post_fee'] =0;
  341. $temp['is_del'] =$value['is_del'];
  342. $temp['updatetime'] =date("Y-m-d H:i:s");
  343. isset($value["id"])&&$value["id"]!=""?"":$temp['addtime'] =date("Y-m-d H:i:s");
  344. $temp['arrive_time']=$value['arrive_time'];
  345. $vmp = Db::name('order_addr')->save($temp);
  346. if(!$vmp){
  347. Db::rollback();
  348. return error_show(0,"咨询订单地址更新失败");
  349. }
  350. }
  351. }else{
  352. if($saleinfo['send_type']==1 && $sendtype==2){
  353. $up=Db::name('order_addr')->where(["orderCode"=>$saleinfo['orderCode']])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  354. if(!$up){
  355. Db::rollback();
  356. return error_show(0,"咨询订单地址更新失败");
  357. }
  358. }
  359. }
  360. Db::commit();
  361. return error_show(0,"咨询订单更新成功");
  362. }
  363. Db::rollback();
  364. return error_show(1002,"咨询订单更新失败");
  365. }catch(\Exception $e){
  366. Db::rollback();
  367. return error_show(1005,$e->getMessage());
  368. }
  369. }
  370. public function out(){
  371. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  372. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode']) :"";
  373. if($outCode==""){
  374. return error_show(1002,"参数outCode不能为空");
  375. }
  376. $der = Db::name('order_out')->where(['outCode'=>$outCode])->find();
  377. if($der==""){
  378. return error_show(1002,"未找到出库单数据");
  379. }
  380. $order= Db::name('sale')->where(['orderCode'=>$der['orderCode']])->find();
  381. if(empty($order)){
  382. return error_show(1002,"未找到咨询订单信息");
  383. }
  384. $stock= Db::name("good_stock")->where(["good_type_code"=>$order['good_code'],"wsm_code"=>$der['wsm_code']])
  385. ->find();
  386. // echo Db::name("good_stock")->getLastSql();
  387. if(empty($stock)){
  388. return error_show(1002,"未找到仓库库存信息");
  389. }
  390. if($der['send_num'] > $stock['wait_out_stock']){
  391. return error_show(1002,"仓库库存不足");
  392. }
  393. $post_name= isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  394. if($post_name==""){
  395. return error_show(1002,"物流公司不能为空");
  396. }
  397. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  398. if($post_code==""){
  399. return error_show(1002,"物流单号不能为空");
  400. }
  401. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? intval($this->post['post_fee']) :"";
  402. if($post_fee==""){
  403. return error_show(1002,"物流费不能为空");
  404. }
  405. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  406. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  407. Db::startTrans();
  408. try {
  409. $var = $der['status'];
  410. $der['post_name'] = $post_name;
  411. $der['post_code'] = $post_code;
  412. $der['post_fee'] = $post_fee;
  413. $der['sendtime'] = $sendtime;
  414. $der['status'] = 1;
  415. $der['updatetime'] = date("Y-m-d H:i:s");
  416. $datainfo = Db::name('order_out')->save($der);
  417. $st = ["order_code"=>$outCode,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  418. ActionLog::logAdd($this->post['token'],$st,"resign_info",$der['status'] ,$st);
  419. if($datainfo) {
  420. // $send =Db::name("order_out")->where(["wsm_code"=>$der['wsm_code'],'orderCode'=>$der['orderCode']])->sum("send_num");
  421. // $order['send_num']+= $der['send_num'];
  422. // $order['wsend_num']-=$der['send_num'] ;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  423. // $order['send_status']=$order['send_num']==0 ? 1 :($order['wsend_num']==0?3:2);
  424. // $order['status']=$order['send_num']==0 ? 2 :($order['wsend_num']==0?4:3);
  425. // $order['updatetime'] = date("Y-m-d H:i:s");
  426. // $si = Db::name('sale')->save($order);
  427. // if($si==false){
  428. // Db::rollback();
  429. // return error_show(1003,"更新失败");
  430. // }
  431. $str = Db::name('good_stock')->where(['wsm_code' => $der['wsm_code'],'good_type_code'=>$order['good_code']])->find();
  432. if ($str == false) {
  433. Db::rollback();
  434. return error_show(1002, "商品数据未找到");
  435. }
  436. if ($der['send_num'] > $str['wait_out_stock']) {
  437. Db::rollback();
  438. return error_show(1002, "超出库存数量");
  439. }
  440. $str['wait_out_stock'] -= $der['send_num'];
  441. $str['intra_stock'] += $der['send_num'];
  442. $str['updatetime'] = date("Y-m-d H:i:s");
  443. $stre = Db::name('good_stock')->save($str);
  444. if ($stre == false) {
  445. Db::rollback();
  446. return error_show(1002, "状态更新失败");
  447. }
  448. Db::commit();
  449. return error_show(0,"创建成功");
  450. }else{
  451. Db::rollback();
  452. return error_show(1003,"创建失败");
  453. }
  454. }catch (\Exception $e) {
  455. Db::rollback();
  456. return error_show(1005, $e->getMessage());
  457. }
  458. }
  459. public function outaddr(){
  460. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  461. $outCode=makeNo("CK");
  462. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  463. if($orderCode==""){
  464. return error_show(1002,"参数orderCoder不能为空");
  465. }
  466. $der = Db::name('sale')->where(['orderCode'=>$orderCode])->find();
  467. if($der==""){
  468. return error_show(1002,"未找到出库订单单号");
  469. }
  470. if($der['order_type']!=2){
  471. return error_show(1002,"订单不是咨询单无法出库");
  472. }
  473. $wsm= Db::name('sale_info')->where(['orderCode'=>$der['orderCode']])->find();
  474. if(empty($wsm)){
  475. return error_show(1002,"未找到仓库信息");
  476. }
  477. $apply_id =GetUserInfo($token);
  478. if(empty($apply_id)||$apply_id['code']!=0){
  479. return error_show(1002,"申请人数据不存在");
  480. }
  481. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  482. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  483. if($der['send_type']==2){
  484. $contactor= isset($this->post['contactor']) && $this->post['contactor'] !=="" ? trim($this->post['contactor'])
  485. :"";
  486. if($contactor==""){
  487. return error_show(1002,"收货人不能为空");
  488. }
  489. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !=="" ? trim($this->post['mobile'])
  490. :"";
  491. if($mobile==""){
  492. return error_show(1002,"收货人联系方式不能为空");
  493. }
  494. $addr = isset($this->post['addr']) && $this->post['addr'] !=="" ? trim($this->post['addr']) :"";
  495. if($addr==""){
  496. return error_show(1002,"参数addr不能为空");
  497. }
  498. $receipt_quantity= isset($this->post['receipt_quantity']) && $this->post['receipt_quantity'] !=="" ? intval
  499. ($this->post['receipt_quantity']) :"";
  500. if($receipt_quantity===""){
  501. return error_show(1002,"参数receipt_quantity不能为空");
  502. }
  503. $addrcode = isset($this->post['addr_code']) && $this->post['addr_code'] !=="" ? $this->post['addr_code'] :"";
  504. if($addrcode==""){
  505. return error_show(1002,"参数addr_code不能为空");
  506. }
  507. $addrs=[];
  508. if(is_array($addrcode)){
  509. $addrs['provice_code'] = $addrcode['addr_code'][0];
  510. $addrs['city_code'] = $addrcode['addr_code'][1];
  511. $addrs['area_code'] = $addrcode['addr_code'][2];
  512. $addrstr = json_encode($addrs);
  513. }else{
  514. $addrstr = $addrcode;
  515. }
  516. }else{
  517. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !=="" ? intval($this->post['addrid']) :"";
  518. $dr = Db::name('order_addr')->where(['id'=>$addrid,"orderCode"=>$orderCode,'is_del'=>0])->find();
  519. if(empty($dr)){
  520. return error_show(1003,"收货人信息未找到");
  521. }
  522. $receipt_quantity = $dr['receipt_quantity'];
  523. }
  524. // if($send_num>$stock['usable_stock']){
  525. // return error_show(1002,"超出可发货数量");
  526. // }
  527. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  528. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  529. Db::startTrans();
  530. try {
  531. if($der['send_type']==2){
  532. $addr=[
  533. "orderCode"=>$orderCode,
  534. "addr"=>$addr,
  535. "addr_code"=>$addrstr,
  536. "contactor"=>$contactor,
  537. "mobile"=>$mobile,
  538. "customer_code"=>$der['customer_code'],
  539. "post_fee"=>0,
  540. "is_del"=>0,
  541. "receipt_quantity"=>$receipt_quantity,
  542. "addtime"=>date("Y-m-d H:i:s"),
  543. "updatetime"=>date("Y-m-d H:i:s"),
  544. "arrive_time"=>date("Y-m-d H:i:s"),
  545. ];
  546. $addrid = Db::name('order_addr')->insert($addr,true);
  547. if($addrid<=0){
  548. $st = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  549. ActionLog::logAdd($this->post['token'],$st,"zxd",0,$st);
  550. Db::rollback();
  551. return error_show(1003,"创建失败");
  552. }
  553. }
  554. $data=[
  555. "wsm_code"=>$wsm['wsm_code'],
  556. "orderCode"=>$orderCode,
  557. "outCode"=>$outCode,
  558. "order_type"=>$der['order_type'],
  559. "apply_id"=>$rm,
  560. "apply_name"=>$ri,
  561. "addrid"=>$addrid,
  562. "post_name"=>'',
  563. "post_code"=>'',
  564. "post_fee"=>'',
  565. "sendtime"=>$sendtime,
  566. "send_num"=>$receipt_quantity,
  567. "check_num"=>0,
  568. "error_num"=>0,
  569. "status"=>0,
  570. "addtime"=>date("Y-m-d H:i:s"),
  571. "updatetime"=>date("Y-m-d H:i:s")
  572. ];
  573. $datainfo = Db::name('order_out')->insert($data);
  574. if(!$datainfo){
  575. $sio = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  576. ActionLog::logAdd($this->post['token'],$sio,"zxd",0,$sio);
  577. Db::rollback();
  578. return error_show(1003,"创建失败");
  579. }
  580. $item = $der['status'];
  581. $der['send_num']+= $receipt_quantity;
  582. $der['wsend_num']-=$receipt_quantity;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  583. $der['send_status']=$der['send_num']==0 ? 1 :($der['wsend_num']==0?3:2);
  584. $der['status']=$der['send_num']==0 ? 2 :($der['wsend_num']==0?4:3);
  585. $der['updatetime'] = date("Y-m-d H:i:s");
  586. $si = Db::name('sale')->save($der);
  587. if($si==false){
  588. $sio = ["order_code"=>$orderCode,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  589. ActionLog::logAdd($this->post['token'],$sio,"zxd",$der['status'],$sio);
  590. Db::rollback();
  591. return error_show(1003,"创建失败");
  592. }
  593. Db::commit();
  594. return error_show(0,"创建成功");
  595. }catch (\Exception $e) {
  596. Db::rollback();
  597. return error_show(1005, $e->getMessage());
  598. }
  599. }
  600. }