Reorder.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ProcessOrder;
  4. use think\facade\Db;
  5. use think\App;
  6. use app\admin\model\ActionLog;
  7. //销售单退货
  8. class Reorder 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. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  18. if($ordeCode==''){
  19. return error_show(1004,"参数orderCode 不能为空");
  20. }
  21. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  22. if(empty($order)){
  23. return error_show(1005,"未找到订单数据");
  24. }
  25. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  26. if($errorCode==''){
  27. return error_show(1004,"参数errorCode 不能为空");
  28. }
  29. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  30. if(empty($error)){
  31. return error_show(1005,"未找到退货原因数据");
  32. }
  33. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  34. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  35. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  36. if($token==''){
  37. return error_show(105,"参数token不能为空");
  38. }
  39. $user =GetUserInfo($token);
  40. if(empty($user)||$user['code']!=0){
  41. return error_show(1002,"申请人数据不存在");
  42. }
  43. $is_addr=0;
  44. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  45. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  46. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ?
  47. $this->post['returnAddr']:"";
  48. if($returnadr!=''){
  49. $thnum=array_sum(array_column($returnadr,"return_num"));
  50. $is_addr=1;
  51. }
  52. $returnCode=makeNo("RS");
  53. Db::startTrans();
  54. try{
  55. $in = [
  56. "returnCode"=>$returnCode,
  57. "orderCode"=>$ordeCode,
  58. "good_code"=>$order['good_code'],
  59. "good_name"=>$order['good_name'],
  60. "apply_id"=>$rm,
  61. "apply_name"=>$ri,
  62. "error_code"=>$errorCode,
  63. "num"=>$thnum,
  64. "remark"=>$remark,
  65. "order_type"=>$order['order_type'],
  66. "is_addr"=>$is_addr,
  67. "status"=>0,
  68. "is_del"=>0,
  69. "addtime"=>date("Y-m-d H:i:s"),
  70. "updatetime"=>date("Y-m-d H:i:s")
  71. ];
  72. $create = Db::name("sale_return")->insert($in,true);
  73. if($create>0){
  74. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  75. ActionLog::logAdd($this->post['token'],$stn,"QRTHD",0,$stn);
  76. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'QRTHD'];
  77. ProcessOrder::AddProcess($this->post['token'],$process);
  78. if($returnadr!=""){
  79. $inf=[];
  80. foreach ($returnadr as $val){
  81. $temp=[];
  82. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  83. if($addrinfo==false){
  84. Db::rollback();
  85. return error_show(1005,"地址信息未找到");
  86. }
  87. $send =Db::name('order_out')->where([["addrid","=",$val['id']],["status",">=",2]])->find();
  88. if($send!=false && $send['send_num']<$val['return_num']){
  89. Db::rollback();
  90. return error_show(1005,"地址已发货");
  91. }
  92. $temp['returnCode']=$returnCode;
  93. $temp['orderCode']=$ordeCode;
  94. $temp['outCode']=$send['outCode'];
  95. $temp['addrid']=$val['id'];
  96. $temp['send_num']=$addrinfo['receipt_quantity'];
  97. $temp['return_num']=$val['return_num'];
  98. $temp['is_del']=0;
  99. $temp['addtime']=date("Y-m-d H:i:s");
  100. $temp['updatetime']=date("Y-m-d H:i:s");
  101. $inf[]=$temp;
  102. }
  103. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  104. if($inadd==0){
  105. Db::rollback();
  106. return error_show(1005,"退货单新建失败");
  107. }
  108. }
  109. Db::commit();
  110. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  111. }
  112. Db::rollback();
  113. return error_show(1005,"退货单新建失败");
  114. }catch (\Exception $e){
  115. Db::rollback();
  116. return error_show(1005,$e->getMessage());
  117. }
  118. }
  119. public function list(){
  120. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  121. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  122. $where =[['is_del',"=",0]];
  123. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  124. if($bkcode!=""){
  125. $where[]=['returnCode',"like", "%{$bkcode}%"];
  126. }
  127. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  128. if($status!==""){
  129. $where[]=['status',"=", $status];
  130. }
  131. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  132. :"";
  133. if($orderCode!=""){
  134. $where[]=['orderCode',"like", "%{$orderCode}%"];
  135. }
  136. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  137. :"";
  138. if($apply_name!=""){
  139. $where[]=['apply_name',"like", "%{$apply_name}%"];
  140. }
  141. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  142. if($start!==""){
  143. $where[]=['addtime',">=", $start];
  144. }
  145. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  146. if($end!==""){
  147. $where[]=['addtime',"<=", $end];
  148. }
  149. $count=Db::name("sale_return")->where($where)->count();
  150. $total = ceil($count/$size);
  151. $page = $total>=$page ? $page :$total;
  152. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  153. // echo Db::name("sale_return")->getLastSql();
  154. $data=[];
  155. foreach ($list as $value){
  156. $value['error_msg']='';
  157. if($value['error_code']!=''){
  158. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  159. $value['error_msg']= isset($error['result'])?$error['result']:"";
  160. }
  161. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  162. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  163. $value['return_total'] =$value['sale_price']*$value['num'] ;
  164. $value['total_num'] =$order['good_num'] ;
  165. $data[]=$value ;
  166. }
  167. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  168. }
  169. public function info(){
  170. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  171. :"";
  172. if($code==""){
  173. return error_show(1004,"参数returnCode不能为空");
  174. }
  175. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  176. if(empty($info)){
  177. return error_show(1004,"未找到退货数据");
  178. }
  179. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  180. if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){
  181. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  182. }else {
  183. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  184. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  185. }
  186. if ($goon==false) {
  187. return error_show(1003, "未找到商品数据");
  188. }
  189. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  190. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  191. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  192. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  193. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  194. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  195. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  196. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  197. $info['return_total'] = $info['sale_price']*$info['num'] ;
  198. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  199. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  200. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  201. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  202. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  203. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  204. $info['customer_name']='';
  205. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  206. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  207. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  208. }
  209. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  210. $info['supplier_name']='';
  211. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  212. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  213. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  214. }
  215. $info['platform_name']='';
  216. $info['platform_id']=$orderinfo['platform_id'];
  217. if($orderinfo['platform_id']!=0){
  218. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  219. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  220. }
  221. $info['cgd_wsend']="";
  222. $info['cgd_send']="";
  223. $info['cgd_total']="";
  224. if($orderinfo['order_type']==2){
  225. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  226. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  227. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  228. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  229. }
  230. $info['error_msg']='';
  231. if($info['error_code']!=''){
  232. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  233. $info['error_msg']= isset($error['result'])?$error['result']:"";
  234. }
  235. if($info['return_wsm']!=""){
  236. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  237. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  238. }
  239. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  240. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  241. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  242. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  243. $wsm=[];
  244. if(!empty($wsm_return)){
  245. foreach ($wsm_return as $value){
  246. $value['wsm_name']="";
  247. $value['wsm_supplier']="";
  248. $value['wsm_supplierNo']="";
  249. if($value['wsm_code']!=""){
  250. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  251. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  252. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  253. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  254. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  255. }
  256. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  257. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  258. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  259. $value['wsm_send'] = $send ?? 0;
  260. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  261. $wsm[]=$value;
  262. }
  263. }
  264. $info['wsminfo']=$wsm;
  265. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  266. $addrinfo=[];
  267. if(!empty($addr)){
  268. foreach ( $addr as $value){
  269. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  270. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  271. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  272. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  273. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  274. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  275. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  276. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  277. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  278. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode']])->sum("send_num");
  279. $value['addr_send'] = $send ?? 0;
  280. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  281. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  282. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  283. $addrinfo[]=$value;
  284. }
  285. }
  286. $info['addrinfo']=$addrinfo;
  287. $info['can']=$int;
  288. return app_show(0,"获取成功",$info);
  289. }
  290. /**
  291. * @return \think\response\Json|void
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function delete(){
  297. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  298. :"";
  299. if($code==""){
  300. return error_show(1004,"参数returnCode不能为空");
  301. }
  302. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  303. if(empty($info)){
  304. return error_show(1004,"未找到退货数据");
  305. }
  306. $info["is_del"]=1;
  307. $info["updatetime"]=date("Y-m-d H:i:s");
  308. $del = Db::name("sale_return")->save($info);
  309. if($del){
  310. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  311. ActionLog::logAdd($this->post['token'],$ste,$info['order_type'] == 2?"ZXTHD":"QRTHD",0,$ste);
  312. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>$info['order_type'] == 2?"ZXTHD":"QRTHD"];
  313. ProcessOrder::workdel($process);
  314. return app_show(0,"删除成功");
  315. }else{
  316. return error_show(1004,"删除失败");
  317. }
  318. }
  319. /**审核
  320. * @return \think\response\Json|void
  321. * @throws \think\db\exception\DataNotFoundException
  322. * @throws \think\db\exception\DbException
  323. * @throws \think\db\exception\ModelNotFoundException
  324. */
  325. public function exam(){
  326. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  327. :"";
  328. if($code==""){
  329. return error_show(1004,"参数returnCode不能为空");
  330. }
  331. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  332. if(empty($info)){
  333. return error_show(1004,"未找到退货数据");
  334. }
  335. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  336. :"";
  337. if($status===""){
  338. return error_show(1004,"参数status不能为空");
  339. }
  340. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  341. $var = $info['status'];
  342. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  343. $apply_id =GetUserInfo($token);
  344. if(empty($apply_id)||$apply_id['code']!=0){
  345. return error_show(1002,"申请人数据不存在");
  346. }
  347. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  348. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  349. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  350. if($orderinfo==false){
  351. return error_show(1004,"未找到订单数据");
  352. }
  353. if($status==4){
  354. $cgd=Db::name("order_num")->where(['orderCode'=>$info["orderCode"],"is_del"=>0])->find();
  355. if(empty($cgd)){
  356. return error_show(1004,"未找到商品库存数据");
  357. }
  358. $wsm =Db::name("purchease_order")->where(["cgdNo"=>$cgd['cgdNo'],"is_del"=>0])->find();
  359. if(empty($wsm)){
  360. return error_show(1004,"未找到商品仓库库存数据");
  361. }
  362. $good_stock = Db::name("good_stock")->where(["spuCode"=>$orderinfo['good_code'],"wsm_code"=>$wsm,"is_del"=>0])
  363. ->select()->toArray();
  364. if(empty($good_stock)){
  365. return error_show(1004,"未找到商品库存数据");
  366. }
  367. if($orderinfo['is_stock']==1){
  368. $bk =Db::name("purchease_order")->where(["bkcode"=>$cgd['bkcode'],"order_type"=>5,"is_del"=>0])
  369. ->find();
  370. if($bk==false){
  371. return error_show(1004,"未找到备库库存数据");
  372. }
  373. $bkorder=Db::name("order_bk")->where(["cgdNo"=>$bk['cgdNo']])->find();
  374. if($bk==false){
  375. return error_show(1004,"未找到备库库存数据");
  376. }
  377. }
  378. if($info['is_addr']==1){
  379. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  380. ->toArray();
  381. if(empty($addr)){
  382. return error_show(1004,"未找到发货单地址数据");
  383. }
  384. }
  385. }
  386. if($status==3){
  387. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  388. if($is_th===""){
  389. return error_show(1004,"参数is_th不能为空");
  390. }
  391. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  392. if($return_wsm===""){
  393. return error_show(1004,"参数return_wsm 不能为空");
  394. }
  395. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  396. if($wsmcode==false){
  397. return error_show(1004,"为找到仓库数据");
  398. }
  399. $info['return_wsm'] =$return_wsm ;
  400. $info['is_th'] =$is_th ;
  401. }
  402. Db::startTrans();
  403. try{
  404. $temp= $info['status'];
  405. $info['status'] =$status;
  406. $remark!=""? $info['remark'] =$remark:"";
  407. $info["updatetime"]=date("Y-m-d H:i:s");
  408. $up = Db::name("sale_return")->save($info);
  409. if($up){
  410. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>$info['order_type'] == 2?"ZXTHD":"QRTHD"];
  411. ProcessOrder::AddProcess($this->post['token'],$process);
  412. $ste = ["order_code"=>$code,"status"=>$temp,"action_remark"=>'',"action_type"=>"status"];
  413. ActionLog::logAdd($this->post['token'],$ste,$info['order_type'] == 2?"ZXTHD":"QRTHD",$status,$info);
  414. if($status==4){
  415. if($info['is_addr']==0){
  416. $orderinfo['wsend_num']-=$info['num'];
  417. }
  418. $orderinfo['th_num']+=$info['num'];
  419. $orderinfo['th_fee']+=round($info['num']*$orderinfo['sale_price'],2);
  420. $orderinfo['send_status'] = $orderinfo['send_num'] == 0 ? 1 : $orderinfo['wsend_num'] == 0 ? 3 : 2;
  421. $old_status = $orderinfo['status'];
  422. $orderinfo['status'] = $orderinfo['send_num'] == 0 ?0 : $orderinfo['wsend_num'] == 0 ? 2 : 1;
  423. $orderinfo['updatetime']=date("Y-m-d H:i:s");
  424. $return = Db::name("sale")->save($orderinfo);
  425. if($return==false){
  426. Db::rollback();
  427. return error_show(1004,"更新失败");
  428. }else{
  429. //修改状态,添加待办
  430. ActionLog::logAdd($this->post['token'], [
  431. "order_code" => $orderinfo['orderCode'],//销售单code
  432. "status" => $old_status,//这里的status是之前的值
  433. "action_remark" => '',//备注
  434. "action_type" => "status"//新建create,编辑edit,更改状态status
  435. ], "XSQRD", $orderinfo['status'], $orderinfo);
  436. ProcessOrder::AddProcess($this->post['token'], [
  437. "order_type" => 'XSQRD',
  438. "order_code" => $orderinfo['orderCode'],//销售单code
  439. "order_id" => $orderinfo['id'],
  440. "order_status" => $orderinfo['status']
  441. ]);
  442. }
  443. if($info['is_th']==1 && $orderinfo['is_stock']==0){
  444. }
  445. foreach ($good_stock as $value){
  446. if($value['wait_out_stock']>=$info['num']){
  447. $value['usable_stock']+=$info['num'];
  448. $value['wait_out_stock']-=$info['num'];
  449. $value['updatetime']=date("Y-m-d H:i:s");
  450. $info["num"]=0;
  451. }else{
  452. $value['usable_stock']+=$value['wait_out_stock'];
  453. $value['wait_out_stock']=0;
  454. $value['updatetime']=date("Y-m-d H:i:s");
  455. $info["num"]-=$value['wait_out_stock'];
  456. }
  457. $stock = Db::name("good_stock")->save($value);
  458. if($stock==false){
  459. Db::rollback();
  460. return error_show(1004,"更新失败");
  461. }
  462. if($info['num']==0){
  463. break;
  464. }
  465. }
  466. }
  467. Db::commit();
  468. return app_show(0,"更新成功");
  469. }
  470. Db::rollback();
  471. return error_show(1004,"更新失败");
  472. }catch (\Exception $e){
  473. Db::rollback();
  474. return error_show(1004,"更新失败");
  475. }
  476. }
  477. public function zxcreate(){
  478. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  479. if($ordeCode==''){
  480. return error_show(1004,"参数orderCode 不能为空");
  481. }
  482. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  483. if(empty($order)){
  484. return error_show(1005,"未找到订单数据");
  485. }
  486. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  487. if($errorCode==''){
  488. return error_show(1004,"参数errorCode 不能为空");
  489. }
  490. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  491. if(empty($error)){
  492. return error_show(1005,"未找到退货原因数据");
  493. }
  494. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  495. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  496. if($token==''){
  497. return error_show(105,"参数token不能为空");
  498. }
  499. $user =GetUserInfo($token);
  500. if(empty($user)||$user['code']!=0){
  501. return error_show(1002,"申请人数据不存在");
  502. }
  503. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  504. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  505. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  506. if($num==''){
  507. return error_show(1005,"参数num不能为空");
  508. }
  509. if($order['wsend_num']<$num){
  510. return error_show(1002,"仓库未发货数量不足退货");
  511. }
  512. $returnCode=makeNo("RS");
  513. Db::startTrans();
  514. try{
  515. $in = [
  516. "returnCode"=>$returnCode,
  517. "orderCode"=>$ordeCode,
  518. "good_code"=>$order['good_code'],
  519. "good_name"=>$order['good_name'],
  520. "apply_id"=>$rm,
  521. "apply_name"=>$ri,
  522. "error_code"=>$errorCode,
  523. "num"=>$num,
  524. "remark"=>$remark,
  525. "order_type"=>2,
  526. "status"=>0,
  527. "is_del"=>0,
  528. "addtime"=>date("Y-m-d H:i:s"),
  529. "updatetime"=>date("Y-m-d H:i:s")
  530. ];
  531. $create = Db::name("sale_return")->insert($in,true);
  532. if($create>0) {
  533. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'ZXTHD'];
  534. ProcessOrder::AddProcess($this->post['token'],$process);
  535. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  536. "action_type"=>"create"];
  537. ActionLog::logAdd($this->post['token'],$ste,"ZXTHD",3,$in);
  538. if ($order['send_type'] == 1) {
  539. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"
  540. =>2])->select();
  541. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  542. if(!empty($wsend)){
  543. foreach ($wsend as $value){
  544. $tt = $value['status'];
  545. $value['status']=0;
  546. $value['updatetime']=date("Y-m-d H:i:s");
  547. $up =Db::name("order_out")->save($value);
  548. if($up){
  549. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD'];
  550. ProcessOrder::AddProcess($this->post['token'],$process);
  551. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  552. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  553. }else{
  554. Db::rollback();
  555. return error_show(1005,"退货单新建失败");
  556. }
  557. }
  558. }
  559. }
  560. Db::commit();
  561. return error_show(0,"退货单新建成功");
  562. }
  563. Db::rollback();
  564. return error_show(1005,"退货单新建失败");
  565. }catch (\Exception $e){
  566. Db::rollback();
  567. return error_show(1005,$e->getMessage());
  568. }
  569. }
  570. }