Reorder.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\facade\Db;
  6. use think\App;
  7. use app\admin\model\ActionLog;
  8. //销售单退货
  9. class Reorder extends Base
  10. {
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. }
  15. public function create(){
  16. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  17. if($ordeCode==''){
  18. return error_show(1004,"参数orderCode 不能为空");
  19. }
  20. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  21. if(empty($order)){
  22. return error_show(1005,"未找到订单数据");
  23. }
  24. $retrun =Db::name("sale_return")->where([["orderCode","=",$ordeCode],["is_del","=",0],["status","<",4]])
  25. ->count();
  26. if($retrun>0){
  27. return error_show(1005,"存在未完成退货订单数据");
  28. }
  29. if($order['order_type']==3|| $order['order_type']==4){
  30. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  31. }else {
  32. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  33. ->where(['a.skuCode' => $order['skuCode']])->find();
  34. }
  35. if($goon==false){
  36. return error_show(1005,"未找到商品数据");
  37. }
  38. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  39. if($supplier==false){
  40. return error_show(1005,"未找到商品供应商数据");
  41. }
  42. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  43. if($errorCode==''){
  44. return error_show(1004,"参数errorCode 不能为空");
  45. }
  46. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  47. if(empty($error)){
  48. return error_show(1005,"未找到退货原因数据");
  49. }
  50. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  51. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  52. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  53. if($token==''){
  54. return error_show(105,"参数token不能为空");
  55. }
  56. $user =GetUserInfo($token);
  57. if(empty($user)||$user['code']!=0){
  58. return error_show(1002,"申请人数据不存在");
  59. }
  60. $is_addr=0;
  61. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  62. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  63. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:"";
  64. if($returnadr!=''){
  65. $thnum=array_sum(array_column($returnadr,"return_num"));
  66. $is_addr=1;
  67. }
  68. $returnCode=makeNo("RS");
  69. Db::startTrans();
  70. try{
  71. $in = [
  72. "returnCode"=>$returnCode,
  73. "orderCode"=>$ordeCode,
  74. "good_code"=>$order['good_code'],
  75. "good_name"=>$order['good_name'],
  76. "apply_id"=>$rm,
  77. "apply_name"=>$ri,
  78. "cgderid"=>$supplier['personid'],
  79. "cgder"=>$supplier['person'],
  80. "error_code"=>$errorCode,
  81. "num"=>$thnum,
  82. "total_fee"=>round($order['sale_price']*$thnum,2),
  83. "good_price"=>$order['sale_price'],
  84. "platform_id"=>$order['platform_id'],
  85. "remark"=>$remark,
  86. "order_type"=>$order['order_type'],
  87. "is_addr"=>$is_addr,
  88. "status"=>$order['is_stock']==1?4:1,
  89. "is_del"=>0,
  90. "addtime"=>date("Y-m-d H:i:s"),
  91. "updatetime"=>date("Y-m-d H:i:s")
  92. ];
  93. $create = Db::name("sale_return")->insert($in,true);
  94. if($create>0){
  95. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  96. ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in);
  97. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0];
  98. ProcessOrder::AddProcess($this->post['token'],$process);
  99. if($returnadr!=""){
  100. $inf=[];
  101. foreach ($returnadr as $val){
  102. $temp=[];
  103. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  104. if($addrinfo==false){
  105. Db::rollback();
  106. return error_show(1005,"地址信息未找到");
  107. }
  108. $send =Db::name('order_out')->where([["addrid","=",$val['id']],["status",">=",2]])->find();
  109. if($send!=false){
  110. Db::rollback();
  111. return error_show(1005,"地址已发货");
  112. }
  113. if($order['is_stock']==1){
  114. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  115. Db::rollback();
  116. return error_show(1004, "地址发货数量不足");
  117. }
  118. $addrinfo['receipt_quantity'] -= $val['return_num'];
  119. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  120. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  121. $addrup = Db::name("order_addr")->save($addrinfo);
  122. if ($addrup == false) {
  123. Db::rollback();
  124. return error_show(1004, "地址发货数量更新失败");
  125. }
  126. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  127. if ($out == false) {
  128. Db::rollback();
  129. return error_show(1004, "地址发货单数据未找到");
  130. }
  131. // if ($send!=false) {
  132. if ($out['status'] >= 2) {
  133. Db::rollback();
  134. return error_show(1004, "地址发货单已发货");
  135. }
  136. if ($out['send_num'] < $val['return_num']) {
  137. Db::rollback();
  138. return error_show(1004, "地址发货单发货数量不足");
  139. }
  140. $out['send_num'] -= $val['return_num'];
  141. $out['updatetime'] = date("Y-m-d H:i:s");
  142. $outup = Db::name("order_out")->save($out);
  143. if ($outup == false) {
  144. Db::rollback();
  145. return error_show(1004, "地址发货单更新失败");
  146. }
  147. $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  148. if ($ordersend['send_num'] < $val['return_num']) {
  149. Db::rollback();
  150. return error_show(1004, "发货单发货数量不足");
  151. }
  152. $ordersend['send_num'] -= $val['return_num'];
  153. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  154. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  155. $sendip = Db::name("order_send")->save($ordersend);
  156. if ($sendip == false) {
  157. Db::rollback();
  158. return error_show(1004, "发货单更新失败");
  159. }
  160. // }
  161. }
  162. $temp['returnCode']=$returnCode;
  163. $temp['orderCode']=$ordeCode;
  164. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  165. $temp['addrid']=$val['id'];
  166. $temp['send_num']=$addrinfo['receipt_quantity'];
  167. $temp['return_num']=$val['return_num'];
  168. $temp['is_del']=0;
  169. $temp['addtime']=date("Y-m-d H:i:s");
  170. $temp['updatetime']=date("Y-m-d H:i:s");
  171. $inf[]=$temp;
  172. }
  173. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  174. if($inadd==0){
  175. Db::rollback();
  176. return error_show(1005,"退货单新建失败");
  177. }
  178. }
  179. if($order['is_stock']==1){
  180. if ($order['wsend_num'] < $thnum) {
  181. Db::rollback();
  182. return error_show(1004, "销售单未发货数量不足退货");
  183. }
  184. $lor=$order['status'] ;
  185. $order['wsend_num'] -= $thnum;
  186. $order['send_num'] += $thnum;
  187. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  188. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  189. $order['th_num'] += $thnum;
  190. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  191. $order['status']=3;
  192. }
  193. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  194. $order['updatetime'] = date("Y-m-d H:i:s");
  195. $uap = Db::name("sale")->save($order);
  196. if ($uap == false) {
  197. Db::rollback();
  198. return error_show(1005, '销售单订单更新失败');
  199. }
  200. ActionLog::logAdd($this->post['token'], [
  201. "order_code" => $order["orderCode"],//出库单号
  202. "status" => $lor,//这里的status是之前的值
  203. "action_remark" => '',//备注
  204. "action_type" => "status"//新建create,编辑edit,更改状态status
  205. ], "XSQRD", $order['status'], $order);
  206. ProcessOrder::AddProcess($this->post['token'], [
  207. "order_type" => 'XSQRD',
  208. "order_code" =>$order["orderCode"],//出库单号
  209. "order_id" => $order["id"],
  210. "order_status" =>$order['status'],"before_status"=>$lor
  211. ]);
  212. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  213. if ($ordernum == false) {
  214. Db::rollback();
  215. return error_show(1005, '未找到关联采购单');
  216. }
  217. $ordernum['send_num'] -= $thnum;
  218. $orderup = Db::name("order_num")->save($ordernum);
  219. if ($orderup == false) {
  220. Db::rollback();
  221. return error_show(1005, '关联数据更新失败');
  222. }
  223. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  224. if ($cgd == false) {
  225. Db::rollback();
  226. return error_show(1005, '未找到采购单数据');
  227. }
  228. $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  229. $cgd['th_num'] += $thnum;
  230. $cgd['updatetime'] = date("Y-m-d H:i:s");
  231. $cgdup = Db::name("purchease_order")->save($cgd);
  232. if ($cgdup == false) {
  233. Db::rollback();
  234. return error_show(1005, '采购单数据更新失败');
  235. }
  236. if ($cgd['bkcode'] != "") {
  237. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0])
  238. ->find();
  239. if ($bk == false) {
  240. Db::rollback();
  241. return error_show(1005, '未找到备库单数据');
  242. }
  243. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  244. if ($orderbk == false) {
  245. Db::rollback();
  246. return error_show(1005, '备库单未完全入库');
  247. }
  248. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 5, "is_del" => 0])
  249. ->field("sum(send_num)-sum(th_num) as num")->find();
  250. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  251. $orderbk['merge_num'] = $merge_num['num'];
  252. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  253. $orderbkup = Db::name("order_bk")->save($orderbk);
  254. if ($orderbkup == false) {
  255. Db::rollback();
  256. return error_show(1005, '备库单库存数据释放失败');
  257. }
  258. }
  259. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']])
  260. ->find();
  261. if (empty($stock)) {
  262. $stock = [
  263. "spuCode" => $order['good_code'],
  264. "wsm_code" => $cgd['wsm_code'],
  265. "usable_stock" => 0,
  266. "wait_out_stock" => 0,
  267. "wait_in_stock" => 0,
  268. "total_stock" => 0,
  269. "addtime" => date("Y-m-d H:i:s"),
  270. "updatetime" => date("Y-m-d H:i:s"),
  271. ];
  272. }
  273. $stock['usable_stock'] += $thnum;
  274. $stock['wait_out_stock'] -= $thnum;
  275. $stock['updatetime'] = date("Y-m-d H:i:s");
  276. $st_up = Db::name("good_stock")->save($stock);
  277. if ($st_up == false) {
  278. return error_show(1005, '可售商品入库失败');
  279. }
  280. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  281. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  282. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  283. }
  284. Db::commit();
  285. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  286. }
  287. Db::rollback();
  288. return error_show(1005,"退货单新建失败");
  289. }catch (\Exception $e){
  290. Db::rollback();
  291. return error_show(1005,$e->getMessage());
  292. }
  293. }
  294. public function list(){
  295. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  296. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  297. $where =[['is_del',"=",0]];
  298. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  299. if($bkcode!=""){
  300. $where[]=['returnCode',"like", "%{$bkcode}%"];
  301. }
  302. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  303. if($status!==""){
  304. $where[]=['status',"=", $status];
  305. }
  306. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  307. :"";
  308. if($orderCode!=""){
  309. $where[]=['orderCode',"like", "%{$orderCode}%"];
  310. }
  311. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  312. :"";
  313. if($apply_name!=""){
  314. $where[]=['apply_name',"like", "%{$apply_name}%"];
  315. }
  316. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  317. if($start!==""){
  318. $where[]=['addtime',">=", $start.' 00:00:00'];
  319. }
  320. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  321. if($end!==""){
  322. $where[]=['addtime',"<=", $end.' 23:59:59'];
  323. }
  324. $role=$this->checkRole();
  325. $condition='';
  326. if(!empty($role['write']) && $this->uid!=""){
  327. // $where[]=["a.apply_id","in",$role['write']];
  328. $condition .="cgderid = {$this->uid} or apply_id in (".implode(',',$role['write']).")";
  329. }
  330. $count=Db::name("sale_return")->where($where)->where($condition)->count();
  331. $total = ceil($count/$size);
  332. $page = $total>=$page ? $page :$total;
  333. $list = Db::name("sale_return")->where($where)->where($condition)->order("addtime desc")->page($page,$size)->select();
  334. // echo Db::name("sale_return")->getLastSql();
  335. $data=[];
  336. foreach ($list as $value){
  337. $value['error_msg']='';
  338. if($value['error_code']!=''){
  339. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  340. $value['error_msg']= isset($error['result'])?$error['result']:"";
  341. }
  342. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  343. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  344. $value['return_total'] =$value['sale_price']*$value['num'] ;
  345. $value['total_num'] =$order['good_num'] ;
  346. $data[]=$value ;
  347. }
  348. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  349. }
  350. public function info(){
  351. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  352. :"";
  353. if($code==""){
  354. return error_show(1004,"参数returnCode不能为空");
  355. }
  356. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  357. if(empty($info)){
  358. return error_show(1004,"未找到退货数据");
  359. }
  360. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  361. if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){
  362. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  363. }else {
  364. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  365. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  366. }
  367. if ($goon==false) {
  368. return error_show(1003, "未找到商品数据");
  369. }
  370. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  371. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  372. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  373. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  374. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  375. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  376. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  377. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  378. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  379. $info['return_total'] = $info['sale_price']*$info['num'] ;
  380. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  381. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  382. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  383. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  384. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  385. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  386. $info['customer_name']='';
  387. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  388. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  389. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  390. }
  391. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  392. $info['supplier_name']='';
  393. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  394. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  395. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  396. }
  397. $info['platform_name']='';
  398. $info['platform_id']=$orderinfo['platform_id'];
  399. if($orderinfo['platform_id']!=0){
  400. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  401. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  402. }
  403. $info['cgd_wsend']="";
  404. $info['cgd_send']="";
  405. $info['cgd_total']="";
  406. if($orderinfo['order_type']==2){
  407. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  408. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  409. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  410. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  411. }
  412. $info['error_msg']='';
  413. if($info['error_code']!=''){
  414. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  415. $info['error_msg']= isset($error['result'])?$error['result']:"";
  416. }
  417. if($info['return_wsm']!=""){
  418. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  419. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  420. }
  421. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  422. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  423. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  424. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  425. $wsm=[];
  426. if(!empty($wsm_return)){
  427. foreach ($wsm_return as $value){
  428. $value['wsm_name']="";
  429. $value['wsm_supplier']="";
  430. $value['wsm_supplierNo']="";
  431. if($value['wsm_code']!=""){
  432. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  433. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  434. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  435. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  436. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  437. }
  438. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  439. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  440. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  441. $value['wsm_send'] = $send ?? 0;
  442. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  443. $wsm[]=$value;
  444. }
  445. }
  446. $info['wsminfo']=$wsm;
  447. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  448. $addrinfo=[];
  449. if(!empty($addr)){
  450. foreach ( $addr as $value){
  451. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  452. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  453. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  454. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  455. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  456. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  457. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  458. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  459. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  460. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode']])->sum("send_num");
  461. $value['addr_send'] = $send ?? 0;
  462. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  463. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  464. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  465. $addrinfo[]=$value;
  466. }
  467. }
  468. $info['addrinfo']=$addrinfo;
  469. $info['can']=$int;
  470. return app_show(0,"获取成功",$info);
  471. }
  472. /**
  473. * @return \think\response\Json|void
  474. * @throws \think\db\exception\DataNotFoundException
  475. * @throws \think\db\exception\DbException
  476. * @throws \think\db\exception\ModelNotFoundException
  477. */
  478. public function delete(){
  479. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  480. :"";
  481. if($code==""){
  482. return error_show(1004,"参数returnCode不能为空");
  483. }
  484. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  485. if(empty($info)){
  486. return error_show(1004,"未找到退货数据");
  487. }
  488. $info["is_del"]=1;
  489. $info["updatetime"]=date("Y-m-d H:i:s");
  490. $del = Db::name("sale_return")->save($info);
  491. if($del){
  492. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  493. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  494. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  495. ProcessOrder::workdel($process);
  496. return app_show(0,"删除成功");
  497. }else{
  498. return error_show(1004,"删除失败");
  499. }
  500. }
  501. /**审核
  502. * @return \think\response\Json|void
  503. * @throws \think\db\exception\DataNotFoundException
  504. * @throws \think\db\exception\DbException
  505. * @throws \think\db\exception\ModelNotFoundException
  506. */
  507. public function exam(){
  508. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  509. :"";
  510. if($code==""){
  511. return error_show(1004,"参数returnCode不能为空");
  512. }
  513. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  514. if(empty($info)){
  515. return error_show(1004,"未找到退货数据");
  516. }
  517. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  518. :"";
  519. if($status===""){
  520. return error_show(1004,"参数status不能为空");
  521. }
  522. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  523. $var = $info['status'];
  524. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  525. $apply_id =GetUserInfo($token);
  526. if(empty($apply_id)||$apply_id['code']!=0){
  527. return error_show(1002,"申请人数据不存在");
  528. }
  529. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  530. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  531. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  532. if($orderinfo==false){
  533. return error_show(1004,"未找到订单数据");
  534. }
  535. if($status==4){
  536. if($info['is_addr']==1){
  537. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  538. ->toArray();
  539. if(empty($addr)){
  540. return error_show(1004,"未找到发货单地址数据");
  541. }
  542. }
  543. }
  544. if($status==3){
  545. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  546. if($is_th===""){
  547. return error_show(1004,"参数is_th不能为空");
  548. }
  549. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  550. if($is_th==0){
  551. if($return_wsm===""){
  552. return error_show(1004,"参数return_wsm 不能为空");
  553. }
  554. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  555. if($wsmcode==false){
  556. return error_show(1004,"为找到仓库数据");
  557. }
  558. }
  559. $info['return_wsm'] =$return_wsm ;
  560. $info['is_th'] =$is_th ;
  561. }
  562. Db::startTrans();
  563. try{
  564. $temp= $info['status'];
  565. $info['status'] =$status;
  566. $remark!=""? $info['remark'] =$remark:"";
  567. $info["updatetime"]=date("Y-m-d H:i:s");
  568. $up = Db::name("sale_return")->save($info);
  569. if($up) {
  570. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp];
  571. ProcessOrder::AddProcess($this->post['token'], $process);
  572. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  573. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  574. if ($status == 4) {
  575. if ($info['is_addr'] == 1) {
  576. if (isset($addr) && !empty($addr)) {
  577. foreach ($addr as $value) {
  578. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  579. if ($addrinfo == false) {
  580. Db::rollback();
  581. return error_show(1004, "地址数据未找到");
  582. }
  583. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  584. Db::rollback();
  585. return error_show(1004, "地址发货数量不足");
  586. }
  587. $addrinfo['receipt_quantity'] -= $value['return_num'];
  588. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  589. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  590. $addrup = Db::name("order_addr")->save($addrinfo);
  591. if ($addrup == false) {
  592. Db::rollback();
  593. return error_show(1004, "地址发货数量更新失败");
  594. }
  595. if ($value['outCode'] != "") {
  596. $out = Db::name("order_out")->where(["outCode" => $value['outCode'], "is_del" => 0])
  597. ->find();
  598. if ($out == false) {
  599. Db::rollback();
  600. return error_show(1004, "地址发货单数据未找到");
  601. }
  602. if ($out['status'] >= 2) {
  603. Db::rollback();
  604. return error_show(1004, "地址发货单已发货");
  605. }
  606. if ($out['send_num'] < $value['return_num']) {
  607. Db::rollback();
  608. return error_show(1004, "地址发货单发货数量不足");
  609. }
  610. $out['send_num'] -= $value['return_num'];
  611. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  612. $out['updatetime'] = date("Y-m-d H:i:s");
  613. $outup = Db::name("order_out")->save($out);
  614. if ($outup == false) {
  615. Db::rollback();
  616. return error_show(1004, "地址发货单更新失败");
  617. }
  618. $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find();
  619. if ($ordersend['send_num'] < $value['return_num']) {
  620. Db::rollback();
  621. return error_show(1004, "发货单发货数量不足");
  622. }
  623. $ordersend['send_num'] -= $value['return_num'];
  624. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  625. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  626. $sendip = Db::name("order_send")->save($ordersend);
  627. if ($sendip == false) {
  628. Db::rollback();
  629. return error_show(1004, "发货单更新失败");
  630. }
  631. }
  632. }
  633. }
  634. }
  635. if ($orderinfo['wsend_num'] < $info['num']) {
  636. Db::rollback();
  637. return error_show(1004, "销售单未发货数量不足退货");
  638. }
  639. $lor=$orderinfo['status'];
  640. $orderinfo['wsend_num'] -= $info['num'];
  641. $orderinfo['send_num'] += $info['num'];
  642. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  643. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  644. if ($orderinfo['is_stock'] == 1) {
  645. $orderinfo['th_num'] += $info['num'];
  646. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  647. $orderinfo['status']=3;
  648. }
  649. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  650. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  651. $uap = Db::name("sale")->save($orderinfo);
  652. if ($uap == false) {
  653. Db::rollback();
  654. return error_show(1005, '销售单订单更新失败');
  655. }
  656. $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find();
  657. if ($ordernum == false) {
  658. Db::rollback();
  659. return error_show(1005, '未找到关联采购单');
  660. }
  661. $ordernum['send_num'] -= $info['num'];
  662. $orderup = Db::name("order_num")->save($ordernum);
  663. if ($orderup == false) {
  664. Db::rollback();
  665. return error_show(1005, '关联数据更新失败');
  666. }
  667. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  668. if ($cgd == false) {
  669. Db::rollback();
  670. return error_show(1005, '未找到采购单数据');
  671. }
  672. $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2);
  673. $cgd['th_num'] += $info['num'];
  674. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  675. $cgd['status']=4;
  676. }
  677. $cgd['updatetime'] = date("Y-m-d H:i:s");
  678. $cgdup = Db::name("purchease_order")->save($cgd);
  679. if ($cgdup == false) {
  680. Db::rollback();
  681. return error_show(1005, '采购单数据更新失败');
  682. }
  683. if ($cgd['bkcode'] != "") {
  684. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0])
  685. ->find();
  686. if ($bk == false) {
  687. Db::rollback();
  688. return error_show(1005, '未找到备库单数据');
  689. }
  690. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  691. if ($orderbk == false) {
  692. Db::rollback();
  693. return error_show(1005, '备库单未完全入库');
  694. }
  695. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 5, "is_del" => 0])
  696. ->field("sum(send_num)-sum(th_num) as num")->find();
  697. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  698. $orderbk['merge_num'] = $merge_num['num'];
  699. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  700. $orderbkup = Db::name("order_bk")->save($orderbk);
  701. if ($orderbkup == false) {
  702. Db::rollback();
  703. return error_show(1005, '备库单库存数据释放失败');
  704. }
  705. }
  706. } else {
  707. if ($info['is_th'] == 1) {
  708. $orderinfo['th_num'] += $info['num'];
  709. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  710. $orderinfo['status']=3;
  711. }
  712. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  713. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  714. $uap = Db::name("sale")->save($orderinfo);
  715. if ($uap == false) {
  716. Db::rollback();
  717. return error_show(1005, '销售单订单更新失败');
  718. }
  719. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  720. if ($ordernum == false) {
  721. Db::rollback();
  722. return error_show(1005, '未找到关联采购单');
  723. }
  724. $ordernum['send_num'] -= $info['num'];
  725. $orderup = Db::name("order_num")->save($ordernum);
  726. if ($orderup == false) {
  727. Db::rollback();
  728. return error_show(1005, '关联数据更新失败');
  729. }
  730. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  731. if ($cgd == false) {
  732. Db::rollback();
  733. return error_show(1005, '未找到采购单数据');
  734. }
  735. $lor=$cgd['status'];
  736. if($cgd['wsend_num']<$info['num']){
  737. $cgd['wsend_num']=0;
  738. $cgd['send_num'] += $info['num']-$cgd['wsend_num'];
  739. }else{
  740. $cgd['wsend_num'] -= $info['num'];
  741. $cgd['send_num'] += $info['num'];
  742. }
  743. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  744. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  745. $cgd['th_num'] += $info['num'];
  746. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  747. $cgd['status']=4;
  748. }
  749. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  750. $cgd['updatetime'] = date("Y-m-d H:i:s");
  751. $cgdup = Db::name("purchease_order")->save($cgd);
  752. if ($cgdup == false) {
  753. Db::rollback();
  754. return error_show(1005, '采购单数据更新失败');
  755. }
  756. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  757. if ($stock==false) {
  758. Db::rollback();
  759. return error_show(1005, '商品仓库未找到');
  760. }
  761. if($stock['wait_in_stock']<$info['num']){
  762. $stock['wait_in_stock']=0;
  763. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  764. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  765. }else{
  766. $stock['usable_stock'] = 0;
  767. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  768. }
  769. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  770. }else{
  771. $stock['wait_in_stock']-=$info['num'];
  772. }
  773. $stock['updatetime'] = date("Y-m-d H:i:s");
  774. $st_up = Db::name("good_stock")->save($stock);
  775. if ($st_up == false) {
  776. return error_show(1005, '可售商品入库失败');
  777. }
  778. } else {
  779. $orderinfo['th_num'] += $info['num'];
  780. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  781. $orderinfo['status']=3;
  782. }
  783. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  784. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  785. $uap = Db::name("sale")->save($orderinfo);
  786. if ($uap == false) {
  787. Db::rollback();
  788. return error_show(1005, '销售单订单更新失败');
  789. }
  790. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  791. if ($ordernum == false) {
  792. Db::rollback();
  793. return error_show(1005, '未找到关联采购单');
  794. }
  795. $ordernum['send_num'] -= $info['num'];
  796. $ordernum['wsend_num'] = $info['num'];
  797. $orderup = Db::name("order_num")->save($ordernum);
  798. if ($orderup == false) {
  799. Db::rollback();
  800. return error_show(1005, '关联数据更新失败');
  801. }
  802. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  803. if ($cgd == false) {
  804. Db::rollback();
  805. return error_show(1005, '未找到采购单数据');
  806. }
  807. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  808. if ($stock==false) {
  809. Db::rollback();
  810. return error_show(1005, '商品仓库未找到');
  811. }
  812. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  813. Db::rollback();
  814. return error_show(1005, '商品可用库存不足退回数量');
  815. }else{
  816. if($stock['usable_stock']>$info['num']){
  817. $stock['usable_stock'] -= $info['num'];
  818. }else{
  819. $stock['usable_stock'] = 0;
  820. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  821. }
  822. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  823. }
  824. $stock['updatetime'] = date("Y-m-d H:i:s");
  825. $st_up = Db::name("good_stock")->save($stock);
  826. if ($st_up == false) {
  827. return error_show(1005, '可售商品入库失败');
  828. }
  829. }
  830. }
  831. ActionLog::logAdd($this->post['token'], [
  832. "order_code" => $orderinfo["orderCode"],//出库单号
  833. "status" => $lor,//这里的status是之前的值
  834. "action_remark" => '',//备注
  835. "action_type" => "status"//新建create,编辑edit,更改状态status
  836. ], "XSQRD", $orderinfo['status'], $orderinfo);
  837. ProcessOrder::AddProcess($this->post['token'], [
  838. "order_type" => 'XSQRD',
  839. "order_code" =>$orderinfo["orderCode"],//出库单号
  840. "order_id" => $orderinfo["id"],
  841. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  842. ]);
  843. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  844. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  845. if ($wsmcode == "") {
  846. Db::rollback();
  847. return error_show(1005, '未找到退货仓库');
  848. }
  849. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  850. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  851. if (empty($stock)) {
  852. $stock = [
  853. "spuCode" => $info['good_code'],
  854. "wsm_code" => $wsmcode,
  855. "usable_stock" => 0,
  856. "wait_out_stock" => 0,
  857. "wait_in_stock" => 0,
  858. "total_stock" => 0,
  859. "addtime" => date("Y-m-d H:i:s"),
  860. "updatetime" => date("Y-m-d H:i:s"),
  861. ];
  862. }
  863. $stock['usable_stock'] += $info['num'];
  864. $stock['updatetime'] = date("Y-m-d H:i:s");
  865. $st_up = Db::name("good_stock")->save($stock);
  866. if ($st_up == false) {
  867. return error_show(1005, '可售商品入库失败');
  868. }
  869. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  870. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  871. }
  872. }
  873. }
  874. Db::commit();
  875. return app_show(0,"更新成功");
  876. }catch (\Exception $e){
  877. Db::rollback();
  878. return error_show(1004,$e->getMessage()."|".$e->getLine());
  879. }
  880. }
  881. public function zxcreate(){
  882. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  883. if($ordeCode==''){
  884. return error_show(1004,"参数orderCode 不能为空");
  885. }
  886. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  887. if(empty($order)){
  888. return error_show(1005,"未找到订单数据");
  889. }
  890. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  891. if($errorCode==''){
  892. return error_show(1004,"参数errorCode 不能为空");
  893. }
  894. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  895. if(empty($error)){
  896. return error_show(1005,"未找到退货原因数据");
  897. }
  898. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  899. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  900. if($token==''){
  901. return error_show(105,"参数token不能为空");
  902. }
  903. $user =GetUserInfo($token);
  904. if(empty($user)||$user['code']!=0){
  905. return error_show(1002,"申请人数据不存在");
  906. }
  907. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  908. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  909. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  910. if($num==''){
  911. return error_show(1005,"参数num不能为空");
  912. }
  913. if($order['wsend_num']<$num){
  914. return error_show(1002,"仓库未发货数量不足退货");
  915. }
  916. $returnCode=makeNo("RS");
  917. Db::startTrans();
  918. try{
  919. $in = [
  920. "returnCode"=>$returnCode,
  921. "orderCode"=>$ordeCode,
  922. "good_code"=>$order['good_code'],
  923. "good_name"=>$order['good_name'],
  924. "apply_id"=>$rm,
  925. "apply_name"=>$ri,
  926. "error_code"=>$errorCode,
  927. "num"=>$num,
  928. "remark"=>$remark,
  929. "order_type"=>2,
  930. "status"=>0,
  931. "is_del"=>0,
  932. "addtime"=>date("Y-m-d H:i:s"),
  933. "updatetime"=>date("Y-m-d H:i:s")
  934. ];
  935. $create = Db::name("sale_return")->insert($in,true);
  936. if($create>0) {
  937. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0];
  938. ProcessOrder::AddProcess($this->post['token'],$process);
  939. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  940. "action_type"=>"create"];
  941. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  942. if ($order['send_type'] == 1) {
  943. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"
  944. =>2])->select();
  945. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  946. if(!empty($wsend)){
  947. foreach ($wsend as $value){
  948. $tt = $value['status'];
  949. $value['status']=0;
  950. $value['updatetime']=date("Y-m-d H:i:s");
  951. $up =Db::name("order_out")->save($value);
  952. if($up){
  953. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt];
  954. ProcessOrder::AddProcess($this->post['token'],$process);
  955. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  956. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  957. }else{
  958. Db::rollback();
  959. return error_show(1005,"退货单新建失败");
  960. }
  961. }
  962. }
  963. }
  964. Db::commit();
  965. return error_show(0,"退货单新建成功");
  966. }
  967. Db::rollback();
  968. return error_show(1005,"退货单新建失败");
  969. }catch (\Exception $e){
  970. Db::rollback();
  971. return error_show(1005,$e->getMessage());
  972. }
  973. }
  974. }