Salezx.php 27 KB

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