Reorder.php 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\GoodStockInfo;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){
  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']])->field("b.creater,b.createrid,b.supplierNo")->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("RN");
  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"=>$goon['createrid'],
  79. "cgder"=>$goon['creater'],
  80. "person"=>$supplier['person']??'',
  81. "person_id"=>$supplier['personid']??0,
  82. "error_code"=>$errorCode,
  83. "num"=>$thnum,
  84. "total_fee"=>round($order['sale_price']*$thnum,2),
  85. "good_price"=>$order['sale_price'],
  86. "platform_id"=>$order['platform_id'],
  87. "remark"=>$remark,
  88. "order_type"=>$order['order_type'],
  89. "is_addr"=>$is_addr,
  90. "status"=>$order['is_stock']==1?4:1,
  91. "is_del"=>0,
  92. "addtime"=>date("Y-m-d H:i:s"),
  93. "updatetime"=>date("Y-m-d H:i:s")
  94. ];
  95. $create = Db::name("sale_return")->insert($in,true);
  96. if($create>0){
  97. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  98. ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in);
  99. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  100. ProcessOrder::AddProcess($this->post['token'],$process);
  101. //维护台账信息
  102. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  103. if($returnadr!=""){
  104. $inf=[];
  105. foreach ($returnadr as $val){
  106. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  107. $temp=[];
  108. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  109. if($addrinfo==false){
  110. Db::rollback();
  111. return error_show(1005,"地址信息未找到");
  112. }
  113. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  114. if($send['status']>=2){
  115. Db::rollback();
  116. return error_show(1005,"地址已发货");
  117. }
  118. if($order['is_stock']==1){
  119. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  120. Db::rollback();
  121. return error_show(1004, "地址发货数量不足");
  122. }
  123. $addrinfo['receipt_quantity'] -= $val['return_num'];
  124. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  125. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  126. $addrup = Db::name("order_addr")->save($addrinfo);
  127. if ($addrup == false) {
  128. Db::rollback();
  129. return error_show(1004, "地址发货数量更新失败");
  130. }
  131. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  132. if ($out == false) {
  133. Db::rollback();
  134. return error_show(1004, "地址发货单数据未找到");
  135. }
  136. // if ($send!=false) {
  137. if ($out['status'] >= 2) {
  138. Db::rollback();
  139. return error_show(1004, "地址发货单已发货");
  140. }
  141. if ($out['send_num'] < $val['return_num']) {
  142. Db::rollback();
  143. return error_show(1004, "地址发货单发货数量不足");
  144. }
  145. $out['send_num'] -= $val['return_num'];
  146. $out['is_del'] = $out['send_num']==0?1:0;
  147. $out['updatetime'] = date("Y-m-d H:i:s");
  148. $outup = Db::name("order_out")->save($out);
  149. if ($outup == false) {
  150. Db::rollback();
  151. return error_show(1004, "地址发货单更新失败");
  152. }
  153. // $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  154. // if ($ordersend['send_num'] < $val['return_num']) {
  155. // Db::rollback();
  156. // return error_show(1004, "发货单发货数量不足");
  157. // }
  158. // $ordersend['send_num'] -= $val['return_num'];
  159. // $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  160. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  161. // $sendip = Db::name("order_send")->save($ordersend);
  162. // if ($sendip == false) {
  163. // Db::rollback();
  164. // return error_show(1004, "发货单更新失败");
  165. // }
  166. // }
  167. }
  168. $temp['returnCode']=$returnCode;
  169. $temp['orderCode']=$ordeCode;
  170. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  171. $temp['addrid']=$val['id'];
  172. $temp['send_num']=$addrinfo['receipt_quantity'];
  173. $temp['return_num']=$val['return_num'];
  174. $temp['is_del']=0;
  175. $temp['addtime']=date("Y-m-d H:i:s");
  176. $temp['updatetime']=date("Y-m-d H:i:s");
  177. $inf[]=$temp;
  178. }
  179. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  180. if($inadd==0){
  181. Db::rollback();
  182. return error_show(1005,"退货单新建失败");
  183. }
  184. }
  185. if($order['is_stock']==1){
  186. if ($order['wsend_num'] < $thnum) {
  187. Db::rollback();
  188. return error_show(1004, "销售单未发货数量不足退货");
  189. }
  190. $lor=$order['status'] ;
  191. $order['wsend_num'] -= $thnum;
  192. $order['send_num'] += $thnum;
  193. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  194. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  195. $order['th_num'] += $thnum;
  196. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  197. $order['status']=3;
  198. }
  199. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  200. $order['updatetime'] = date("Y-m-d H:i:s");
  201. $uap = Db::name("sale")->save($order);
  202. if ($uap == false) {
  203. Db::rollback();
  204. return error_show(1005, '销售单订单更新失败');
  205. }
  206. ActionLog::logAdd($this->post['token'], [
  207. "order_code" => $order["orderCode"],//出库单号
  208. "status" => $lor,//这里的status是之前的值
  209. "action_remark" => '',//备注
  210. "action_type" => "status"//新建create,编辑edit,更改状态status
  211. ], "XSQRD", $order['status'], $order);
  212. ProcessOrder::AddProcess($this->post['token'], [
  213. "order_type" => 'XSQRD',
  214. "order_code" =>$order["orderCode"],//出库单号
  215. "order_id" => $order["id"],
  216. "order_status" =>$order['status'],"before_status"=>$lor
  217. ]);
  218. // $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  219. // if ($ordernum == false) {
  220. // Db::rollback();
  221. // return error_show(1005, '未找到关联采购单');
  222. // }
  223. // $ordernum['send_num'] -= $thnum;
  224. // $orderup = Db::name("order_num")->save($ordernum);
  225. // if ($orderup == false) {
  226. // Db::rollback();
  227. // return error_show(1005, '关联数据更新失败');
  228. // }
  229. // $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  230. // if ($cgd == false) {
  231. // Db::rollback();
  232. // return error_show(1005, '未找到采购单数据');
  233. // }
  234. // $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  235. // $cgd['th_num'] += $thnum;
  236. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  237. // $cgdup = Db::name("purchease_order")->save($cgd);
  238. // if ($cgdup == false) {
  239. // Db::rollback();
  240. // return error_show(1005, '采购单数据更新失败');
  241. // }
  242. // if ($cgd['bkcode'] != "") {
  243. // $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  244. // ->find();
  245. // if ($bk == false) {
  246. // Db::rollback();
  247. // return error_show(1005, '未找到备库单数据');
  248. // }
  249. // $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  250. // if ($orderbk == false) {
  251. // Db::rollback();
  252. // return error_show(1005, '备库单未完全入库');
  253. // }
  254. // $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>
  255. // 1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  256. // ->find();
  257. //
  258. // $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  259. // $orderbk['merge_num'] = $merge_num['num'];
  260. // $orderbk['updatetime'] = date("Y-m-d H:i:s");
  261. // $orderbkup = Db::name("order_bk")->save($orderbk);
  262. // if ($orderbkup == false) {
  263. // Db::rollback();
  264. // return error_show(1005, '备库单库存数据释放失败');
  265. // }
  266. // }
  267. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$ordeCode],["num",">",0]])->select()
  268. ->toArray();
  269. if(empty($saleinfo)) {
  270. Db::rollback();
  271. return error_show(1005, '商品批次数据未找到');
  272. }
  273. $tempnum =$thnum;
  274. foreach ( $saleinfo as $va){
  275. if($tempnum ==0) break;
  276. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])
  277. ->find();
  278. if($stock==false) {
  279. Db::rollback();
  280. return error_show(1005, '商品库存数据未找到');
  281. }
  282. if($va['num']>=$tempnum) {
  283. $tnm = $tempnum;
  284. $va['num']-= $tempnum;
  285. $tempnum=0;
  286. }else{
  287. $tnm = $va['num'];
  288. $tempnum -=$va['num'];
  289. $va['num'] =0;
  290. }
  291. $stock['usable_stock']+=$tnm;
  292. $stock['wait_out_stock'] -=$tnm;
  293. $stock['updatetime'] = date("Y-m-d H:i:s");
  294. $st_up = Db::name("good_stock")->save($stock);
  295. if ($st_up == false) {
  296. return error_show(1005, '可售商品入库失败');
  297. }
  298. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  299. if ($ps == false) {
  300. return error_show(1005, '商品批次退货入库失败');
  301. }
  302. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  303. if ($ret == false) {
  304. return error_show(1005, '商品批次退货入库失败');
  305. }
  306. $va['updatetime']=date("Y-m-d H:i:s");
  307. $sal= Db::name("sale_info")->save($va);
  308. if ($sal == false) {
  309. return error_show(1005, '商品批次退货入库失败');
  310. }
  311. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  312. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  313. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  314. }
  315. $data=[
  316. "orderCode"=>$ordeCode,
  317. "th_type"=>1,
  318. "th_num"=>$thnum,
  319. "th_fee"=>round($order['sale_price']*$thnum,2),
  320. "thCode"=>$returnCode,
  321. "spuCode"=>$order['good_code'],
  322. "good_name"=>$order['good_name'],
  323. "cat_id"=>$order['cat_id'],
  324. "apply_id"=>$rm,
  325. "apply_name"=>$ri,
  326. "addtime"=>date("Y-m-d H:i:s"),
  327. "status"=>1,
  328. "is_del"=>0
  329. ];
  330. $inse=Db::name("th_data")->insert($data);
  331. if($inse==false){
  332. Db::rollback();
  333. return error_show(1004,"退货单更新失败");
  334. }
  335. }
  336. Db::commit();
  337. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  338. }
  339. Db::rollback();
  340. return error_show(1005,"退货单新建失败");
  341. }catch (\Exception $e){
  342. Db::rollback();
  343. return error_show(1005,$e->getMessage());
  344. }
  345. }
  346. public function list(){
  347. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  348. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  349. $where =[['sr.is_del',"=",0]];
  350. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  351. if($bkcode!=""){
  352. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  353. }
  354. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  355. if($status!==""){
  356. $where[]=['sr.status',"=", $status];
  357. }
  358. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  359. :"";
  360. if($orderCode!=""){
  361. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  362. }
  363. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  364. :"";
  365. if($apply_name!=""){
  366. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  367. }
  368. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  369. if($start!==""){
  370. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  371. }
  372. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  373. if($end!==""){
  374. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  375. }
  376. $role=$this->checkRole();
  377. $condition='';
  378. if(!empty($role['write']) && $this->uid!=""){
  379. // $where[]=["sr.apply_id","in",$role['write']];
  380. $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).") or sr.person_id={$this->uid}";
  381. }
  382. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  383. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  384. $count=Db::name("sale_return")->alias('sr')->where($where)->where($condition)->count();
  385. $total = ceil($count/$size);
  386. $page = $total>=$page ? $page :$total;
  387. $list = Db::name("sale_return")
  388. ->alias('sr')
  389. ->field('sr.*,u.itemid')
  390. ->leftJoin("depart_user u", "u.uid=sr.apply_id AND u.is_del=0")
  391. ->where($where)
  392. ->where($condition)
  393. ->order("addtime desc")
  394. ->page($page,$size)
  395. ->cursor();
  396. // echo Db::name("sale_return")->getLastSql();
  397. $data=[];
  398. foreach ($list as $value){
  399. $value['error_msg']='';
  400. if($value['error_code']!=''){
  401. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  402. $value['error_msg']= isset($error['result'])?$error['result']:"";
  403. }
  404. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  405. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  406. $value['return_total'] =$value['sale_price']*$value['num'] ;
  407. $value['total_num'] =$order['good_num'] ;
  408. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  409. $data[]=$value ;
  410. }
  411. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  412. }
  413. public function info(){
  414. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  415. :"";
  416. if($code==""){
  417. return error_show(1004,"参数returnCode不能为空");
  418. }
  419. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  420. if(empty($info)){
  421. return error_show(1004,"未找到退货数据");
  422. }
  423. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  424. if($orderinfo['order_type']==3){
  425. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  426. }else {
  427. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  428. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  429. }
  430. if ($goon==false) {
  431. return error_show(1003, "未找到商品数据");
  432. }
  433. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  434. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  435. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  436. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  437. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  438. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  439. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  440. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  441. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  442. $info['return_total'] = $info['sale_price']*$info['num'] ;
  443. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  444. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  445. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  446. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  447. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  448. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  449. $info['customer_name']='';
  450. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  451. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  452. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  453. }
  454. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  455. $info['supplier_name']='';
  456. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  457. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  458. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  459. }
  460. $info['platform_name']='';
  461. $info['platform_id']=$orderinfo['platform_id'];
  462. if($orderinfo['platform_id']!=0){
  463. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  464. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  465. }
  466. $info['cgd_wsend']="";
  467. $info['cgd_send']="";
  468. $info['cgd_total']="";
  469. if($orderinfo['order_type']==2){
  470. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  471. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  472. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  473. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  474. }
  475. $info['error_msg']='';
  476. if($info['error_code']!=''){
  477. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  478. $info['error_msg']= isset($error['result'])?$error['result']:"";
  479. }
  480. if($info['return_wsm']!=""){
  481. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  482. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  483. }
  484. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  485. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  486. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  487. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  488. $wsm=[];
  489. if(!empty($wsm_return)){
  490. foreach ($wsm_return as $value){
  491. $value['wsm_name']="";
  492. $value['wsm_supplier']="";
  493. $value['wsm_supplierNo']="";
  494. if($value['wsm_code']!=""){
  495. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  496. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  497. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  498. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  499. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  500. }
  501. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  502. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  503. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  504. $value['wsm_send'] = $send ?? 0;
  505. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  506. $wsm[]=$value;
  507. }
  508. }
  509. $info['wsminfo']=$wsm;
  510. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  511. $addrinfo=[];
  512. if(!empty($addr)){
  513. foreach ( $addr as $value){
  514. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  515. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  516. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  517. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  518. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  519. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  520. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  521. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  522. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  523. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  524. $value['addr_send'] = $send ?? 0;
  525. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  526. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  527. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  528. $addrinfo[]=$value;
  529. }
  530. }
  531. $info['addrinfo']=$addrinfo;
  532. $info['can']=$int;
  533. return app_show(0,"获取成功",$info);
  534. }
  535. /**
  536. * @return \think\response\Json|void
  537. * @throws \think\db\exception\DataNotFoundException
  538. * @throws \think\db\exception\DbException
  539. * @throws \think\db\exception\ModelNotFoundException
  540. */
  541. public function delete(){
  542. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  543. :"";
  544. if($code==""){
  545. return error_show(1004,"参数returnCode不能为空");
  546. }
  547. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  548. if(empty($info)){
  549. return error_show(1004,"未找到退货数据");
  550. }
  551. $info["is_del"]=1;
  552. $info["updatetime"]=date("Y-m-d H:i:s");
  553. $del = Db::name("sale_return")->save($info);
  554. if($del){
  555. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  556. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  557. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  558. ProcessOrder::workdel($process);
  559. return app_show(0,"删除成功");
  560. }else{
  561. return error_show(1004,"删除失败");
  562. }
  563. }
  564. /**审核
  565. * @return \think\response\Json|void
  566. * @throws \think\db\exception\DataNotFoundException
  567. * @throws \think\db\exception\DbException
  568. * @throws \think\db\exception\ModelNotFoundException
  569. */
  570. public function exam(){
  571. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']):"";
  572. if($code==""){
  573. return error_show(1004,"参数returnCode不能为空");
  574. }
  575. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  576. if(empty($info)){
  577. return error_show(1004,"未找到退货数据");
  578. }
  579. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  580. if($status===""){
  581. return error_show(1004,"参数status不能为空");
  582. }
  583. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  584. $var = $info['status'];
  585. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  586. if($orderinfo==false){
  587. return error_show(1004,"未找到订单数据");
  588. }
  589. if($status==4){
  590. if($info['is_addr']==1){
  591. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  592. ->toArray();
  593. if(empty($addr)){
  594. return error_show(1004,"未找到发货单地址数据");
  595. }
  596. }
  597. }
  598. if($status==3){
  599. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  600. if($is_th===""){
  601. return error_show(1004,"参数is_th不能为空");
  602. }
  603. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  604. if($is_th==0){
  605. if($return_wsm===""){
  606. return error_show(1004,"参数return_wsm 不能为空");
  607. }
  608. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  609. if($wsmcode==false){
  610. return error_show(1004,"为找到仓库数据");
  611. }
  612. }
  613. $info['return_wsm'] =$return_wsm ;
  614. $info['is_th'] =$is_th ;
  615. }
  616. Db::startTrans();
  617. try{
  618. $temp= $info['status'];
  619. $info['status'] =$status;
  620. $remark!=""? $info['remark'] =$remark:"";
  621. $info["updatetime"]=date("Y-m-d H:i:s");
  622. $up = Db::name("sale_return")->save($info);
  623. if($up) {
  624. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp,'holder_id'=>$info['apply_id']];
  625. ProcessOrder::AddProcess($this->post['token'], $process);
  626. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  627. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  628. if ($status == 4) {
  629. if ($info['is_addr'] == 1) {
  630. if (isset($addr) && !empty($addr)) {
  631. foreach ($addr as $value) {
  632. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  633. if ($addrinfo == false) {
  634. Db::rollback();
  635. return error_show(1004, "地址数据未找到");
  636. }
  637. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  638. Db::rollback();
  639. return error_show(1004, "地址发货数量不足");
  640. }
  641. $addrinfo['receipt_quantity'] -= $value['return_num'];
  642. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  643. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  644. $addrup = Db::name("order_addr")->save($addrinfo);
  645. if ($addrup == false) {
  646. Db::rollback();
  647. return error_show(1004, "地址发货数量更新失败");
  648. }
  649. if ($value['outCode'] != "") {
  650. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->find();
  651. if ($out == false) {
  652. Db::rollback();
  653. return error_show(1004, "地址发货单数据未找到");
  654. }
  655. if ($out['status'] >= 2) {
  656. Db::rollback();
  657. return error_show(1004, "地址发货单已发货");
  658. }
  659. if ($out['send_num'] < $value['return_num']) {
  660. Db::rollback();
  661. return error_show(1004, "地址发货单发货数量不足");
  662. }
  663. $out['send_num'] -= $value['return_num'];
  664. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  665. $out['updatetime'] = date("Y-m-d H:i:s");
  666. $outup = Db::name("order_out")->save($out);
  667. if ($outup == false) {
  668. Db::rollback();
  669. return error_show(1004, "地址发货单更新失败");
  670. }
  671. // $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find();
  672. // if ($ordersend['send_num'] < $value['return_num']) {
  673. // Db::rollback();
  674. // return error_show(1004, "发货单发货数量不足");
  675. // }
  676. // $ordersend['send_num'] -= $value['return_num'];
  677. // $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  678. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  679. // $sendip = Db::name("order_send")->save($ordersend);
  680. // if ($sendip == false) {
  681. // Db::rollback();
  682. // return error_show(1004, "发货单更新失败");
  683. // }
  684. }
  685. }
  686. }
  687. }
  688. if ($orderinfo['wsend_num'] < $info['num']) {
  689. Db::rollback();
  690. return error_show(1004, "销售单未发货数量不足退货");
  691. }
  692. $lor=$orderinfo['status'];
  693. $orderinfo['wsend_num'] -= $info['num'];
  694. $orderinfo['send_num'] += $info['num'];
  695. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  696. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  697. if ($orderinfo['is_stock'] == 1) {
  698. $orderinfo['th_num'] += $info['num'];
  699. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  700. $orderinfo['status']=3;
  701. }
  702. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  703. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  704. $uap = Db::name("sale")->save($orderinfo);
  705. if ($uap == false) {
  706. Db::rollback();
  707. return error_show(1005, '销售单订单更新失败');
  708. }
  709. // $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find();
  710. // if ($ordernum == false) {
  711. // Db::rollback();
  712. // return error_show(1005, '未找到关联采购单');
  713. // }
  714. // $ordernum['send_num'] -= $info['num'];
  715. // $orderup = Db::name("order_num")->save($ordernum);
  716. // if ($orderup == false) {
  717. // Db::rollback();
  718. // return error_show(1005, '关联数据更新失败');
  719. // }
  720. // $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  721. // if ($cgd == false) {
  722. // Db::rollback();
  723. // return error_show(1005, '未找到采购单数据');
  724. // }
  725. // $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2);
  726. // $cgd['th_num'] += $info['num'];
  727. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  728. // $cgd['status']=4;
  729. // }
  730. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  731. // $cgdup = Db::name("purchease_order")->save($cgd);
  732. // if ($cgdup == false) {
  733. // Db::rollback();
  734. // return error_show(1005, '采购单数据更新失败');
  735. // }
  736. // if ($cgd['bkcode'] != "") {
  737. // $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1,"order_source"=>0, "is_del" => 0])
  738. // ->find();
  739. // if ($bk == false) {
  740. // Db::rollback();
  741. // return error_show(1005, '未找到备库单数据');
  742. // }
  743. // $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  744. // if ($orderbk == false) {
  745. // Db::rollback();
  746. // return error_show(1005, '备库单未完全入库');
  747. // }
  748. // $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type"
  749. // => 1, "is_del" => 0])->where("order_source","<>",0)
  750. // ->field("sum(send_num)-sum(th_num) as num")->find();
  751. //
  752. // $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  753. // $orderbk['merge_num'] = $merge_num['num'];
  754. // $orderbk['updatetime'] = date("Y-m-d H:i:s");
  755. // $orderbkup = Db::name("order_bk")->save($orderbk);
  756. // if ($orderbkup == false) {
  757. // Db::rollback();
  758. // return error_show(1005, '备库单库存数据释放失败');
  759. // }
  760. // }
  761. } else {
  762. if ($info['is_th'] == 1) {
  763. $orderinfo['th_num'] += $info['num'];
  764. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  765. $orderinfo['status']=3;
  766. }
  767. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  768. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  769. $uap = Db::name("sale")->save($orderinfo);
  770. if ($uap == false) {
  771. Db::rollback();
  772. return error_show(1005, '销售单订单更新失败');
  773. }
  774. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  775. if ($ordernum == false) {
  776. Db::rollback();
  777. return error_show(1005, '未找到关联采购单');
  778. }
  779. $ordernum['send_num'] -= $info['num'];
  780. $orderup = Db::name("order_num")->save($ordernum);
  781. if ($orderup == false) {
  782. Db::rollback();
  783. return error_show(1005, '关联数据更新失败');
  784. }
  785. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  786. if ($cgd == false) {
  787. Db::rollback();
  788. return error_show(1005, '未找到采购单数据');
  789. }
  790. if( $info['is_all']==1 && $cgd['send_status']!=1){
  791. Db::rollback();
  792. return error_show(1005, '采购单已入库无法全部退货');
  793. }
  794. $lor=$cgd['status'];
  795. if($cgd['wsend_num']<$info['num']){
  796. $cgd['send_num'] += $cgd['wsend_num']==0?0 :($info['num']-$cgd['wsend_num']);
  797. $cgd['wsend_num']=0;
  798. }else{
  799. $cgd['wsend_num'] -= $info['num'];
  800. $cgd['send_num'] += $info['num'];
  801. }
  802. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  803. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  804. $cgd['th_num'] += $info['num'];
  805. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  806. $cgd['status']=4;
  807. }
  808. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  809. $cgd['updatetime'] = date("Y-m-d H:i:s");
  810. $cgdup = Db::name("purchease_order")->save($cgd);
  811. if ($cgdup == false) {
  812. Db::rollback();
  813. return error_show(1005, '采购单数据更新失败');
  814. }
  815. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  816. if ($stock==false) {
  817. Db::rollback();
  818. return error_show(1005, '商品仓库未找到');
  819. }
  820. if($stock['wait_in_stock']<$info['num']){
  821. $stock['wait_in_stock']=0;
  822. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  823. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  824. }else{
  825. $stock['usable_stock'] = 0;
  826. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  827. }
  828. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  829. }else{
  830. $stock['wait_in_stock']-=$info['num'];
  831. }
  832. $stock['updatetime'] = date("Y-m-d H:i:s");
  833. $st_up = Db::name("good_stock")->save($stock);
  834. if ($st_up == false) {
  835. Db::rollback();
  836. return error_show(1005, '可售商品入库失败');
  837. }
  838. } else {
  839. $orderinfo['th_num'] += $info['num'];
  840. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  841. $orderinfo['status']=3;
  842. }
  843. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  844. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  845. $uap = Db::name("sale")->save($orderinfo);
  846. if ($uap == false) {
  847. Db::rollback();
  848. return error_show(1005, '销售单订单更新失败');
  849. }
  850. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  851. if ($ordernum == false) {
  852. Db::rollback();
  853. return error_show(1005, '未找到关联采购单');
  854. }
  855. $ordernum['send_num'] -= $info['num'];
  856. $ordernum['wsend_num'] = $info['num'];
  857. $orderup = Db::name("order_num")->save($ordernum);
  858. if ($orderup == false) {
  859. Db::rollback();
  860. return error_show(1005, '关联数据更新失败');
  861. }
  862. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  863. if ($cgd == false) {
  864. Db::rollback();
  865. return error_show(1005, '未找到采购单数据');
  866. }
  867. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  868. if ($stock==false) {
  869. Db::rollback();
  870. return error_show(1005, '商品仓库未找到');
  871. }
  872. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  873. Db::rollback();
  874. return error_show(1005, '商品可用库存不足退回数量');
  875. }else{
  876. if($stock['usable_stock']>$info['num']){
  877. $stock['usable_stock'] -= $info['num'];
  878. }else{
  879. $stock['usable_stock'] = 0;
  880. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  881. }
  882. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  883. }
  884. $stock['updatetime'] = date("Y-m-d H:i:s");
  885. $st_up = Db::name("good_stock")->save($stock);
  886. if ($st_up == false) {
  887. Db::rollback();
  888. return error_show(1005, '可售商品入库失败');
  889. }
  890. }
  891. }
  892. ActionLog::logAdd($this->post['token'], [
  893. "order_code" => $orderinfo["orderCode"],//出库单号
  894. "status" => $lor,//这里的status是之前的值
  895. "action_remark" => '',//备注
  896. "action_type" => "status"//新建create,编辑edit,更改状态status
  897. ], "XSQRD", $orderinfo['status'], $orderinfo);
  898. ProcessOrder::AddProcess($this->post['token'], [
  899. "order_type" => 'XSQRD',
  900. "order_code" =>$orderinfo["orderCode"],//出库单号
  901. "order_id" => $orderinfo["id"],
  902. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  903. ]);
  904. //对应采购单也要处理
  905. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  906. "order_type" => 'CGD',
  907. "order_code" => $cgd['cgdNo'],
  908. "order_id" => $cgd["id"],
  909. "order_status" => $cgd['status'],
  910. "before_status" => $lor,
  911. 'holder_id' => Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0)
  912. ]);
  913. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  914. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  915. if ($wsmcode == "") {
  916. Db::rollback();
  917. return error_show(1005, '未找到退货仓库');
  918. }
  919. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  920. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  921. if (empty($stock)) {
  922. $stock = [
  923. "spuCode" => $info['good_code'],
  924. "wsm_code" => $wsmcode,
  925. "usable_stock" => 0,
  926. "wait_out_stock" => 0,
  927. "wait_in_stock" => 0,
  928. "total_stock" => 0,
  929. "addtime" => date("Y-m-d H:i:s"),
  930. "updatetime" => date("Y-m-d H:i:s"),
  931. ];
  932. }
  933. $stock['usable_stock'] += $info['num'];
  934. $stock['updatetime'] = date("Y-m-d H:i:s");
  935. $st_up = Db::name("good_stock")->save($stock);
  936. if ($st_up == false) {
  937. Db::rollback();
  938. return error_show(1005, '可售商品入库失败');
  939. }
  940. $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  941. $sabebn =Db::name("sale_info")->where(["orderCode"=>$orderinfo["orderCode"]])->select()->toArray();
  942. if(!empty($sabebn)){
  943. $total_num =$info['num'];
  944. foreach ($sabebn as $ve){
  945. $tempnum=0;
  946. if ($total_num==0) break;
  947. if($total_num>=$ve['num']){
  948. $tempnum = $ve['num'];
  949. $total_num-=$ve['num'];
  950. $ve['num']=0;
  951. }else{
  952. $tempnum = $total_num;
  953. $total_num=0;
  954. $ve['num']-=$total_num;
  955. }
  956. $bnin=GoodStockInfo::AddBn($stockid,$ve['bnCode'],$tempnum,$ve['origin_price']);
  957. if($bnin==false){
  958. Db::rollback();
  959. return error_show(1005, '可售商品Bn库存数入库失败');
  960. }
  961. $up=Db::name("sale_info")->save($ve);
  962. if($up==false){
  963. Db::rollback();
  964. return error_show(1005, '可售商品Bn库存数入库失败');
  965. }
  966. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  967. if($bnin==false){
  968. Db::rollback();
  969. return error_show(1005, '可售商品Bn库存数入库失败');
  970. }
  971. }
  972. }
  973. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock'=> $info['num'], "stock_name" => "usable_stock"];
  974. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  975. }
  976. $data=[
  977. "orderCode"=>$info['orderCode'],
  978. "th_type"=>1,
  979. "th_num"=>$info['num'],
  980. "th_fee"=>round($info['num']*$orderinfo['sale_price'],2),
  981. "thCode"=>$info['returnCode'],
  982. "spuCode"=>$orderinfo['good_code'],
  983. "good_name"=>$orderinfo['good_name'],
  984. "cat_id"=>$orderinfo['cat_id'],
  985. "apply_id"=>$info['apply_id'],
  986. "apply_name"=>$info['apply_name'],
  987. "addtime"=>date("Y-m-d H:i:s"),
  988. "status"=>1,
  989. "is_del"=>0
  990. ];
  991. $inse=Db::name("th_data")->insert($data);
  992. if($inse==false){
  993. Db::rollback();
  994. return error_show(1004,"退货单更新失败");
  995. }
  996. }
  997. }
  998. Db::commit();
  999. return app_show(0,"更新成功");
  1000. }catch (\Exception $e){
  1001. Db::rollback();
  1002. return error_show(1004,$e->getMessage()."|".$e->getLine());
  1003. }
  1004. }
  1005. public function zxcreate(){
  1006. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  1007. if($ordeCode==''){
  1008. return error_show(1004,"参数orderCode 不能为空");
  1009. }
  1010. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  1011. if(empty($order)){
  1012. return error_show(1005,"未找到订单数据");
  1013. }
  1014. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1015. if($errorCode==''){
  1016. return error_show(1004,"参数errorCode 不能为空");
  1017. }
  1018. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1019. if(empty($error)){
  1020. return error_show(1005,"未找到退货原因数据");
  1021. }
  1022. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1023. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  1024. if($token==''){
  1025. return error_show(105,"参数token不能为空");
  1026. }
  1027. $user =GetUserInfo($token);
  1028. if(empty($user)||$user['code']!=0){
  1029. return error_show(1002,"申请人数据不存在");
  1030. }
  1031. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  1032. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  1033. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  1034. if($num==''){
  1035. return error_show(1005,"参数num不能为空");
  1036. }
  1037. if($order['wsend_num']<$num){
  1038. return error_show(1002,"仓库未发货数量不足退货");
  1039. }
  1040. $returnCode=makeNo("RS");
  1041. Db::startTrans();
  1042. try{
  1043. $in = [
  1044. "returnCode"=>$returnCode,
  1045. "orderCode"=>$ordeCode,
  1046. "good_code"=>$order['good_code'],
  1047. "good_name"=>$order['good_name'],
  1048. "apply_id"=>$rm,
  1049. "apply_name"=>$ri,
  1050. "error_code"=>$errorCode,
  1051. "num"=>$num,
  1052. "remark"=>$remark,
  1053. "order_type"=>2,
  1054. "status"=>0,
  1055. "is_del"=>0,
  1056. "addtime"=>date("Y-m-d H:i:s"),
  1057. "updatetime"=>date("Y-m-d H:i:s")
  1058. ];
  1059. $create = Db::name("sale_return")->insert($in,true);
  1060. if($create>0) {
  1061. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1062. ProcessOrder::AddProcess($this->post['token'],$process);
  1063. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1064. "action_type"=>"create"];
  1065. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  1066. //维护台账记录
  1067. // Db::name('standing_book')
  1068. // ->where('ordeCode', $ordeCode)
  1069. // ->update([
  1070. // 'returnGoodCode' => $returnCode,
  1071. // 'updatetime' => date("Y-m-d H:i:s")
  1072. // ]);
  1073. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1074. if ($order['send_type'] == 1) {
  1075. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1076. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1077. if(!empty($wsend)){
  1078. foreach ($wsend as $value){
  1079. $tt = $value['status'];
  1080. $value['status']=0;
  1081. $value['updatetime']=date("Y-m-d H:i:s");
  1082. $up =Db::name("order_out")->save($value);
  1083. if($up){
  1084. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1085. ProcessOrder::AddProcess($this->post['token'],$process);
  1086. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1087. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  1088. }else{
  1089. Db::rollback();
  1090. return error_show(1005,"退货单新建失败");
  1091. }
  1092. }
  1093. }
  1094. }
  1095. Db::commit();
  1096. return error_show(0,"退货单新建成功");
  1097. }
  1098. Db::rollback();
  1099. return error_show(1005,"退货单新建失败");
  1100. }catch (\Exception $e){
  1101. Db::rollback();
  1102. return error_show(1005,$e->getMessage());
  1103. }
  1104. }
  1105. public function allReturn(){
  1106. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1107. if($orderCode==''){
  1108. return error_show(1004,"参数orderCode 不能为空");
  1109. }
  1110. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1111. if(empty($order)){
  1112. return error_show(1005,"未找到订单数据");
  1113. }
  1114. if($order['wsend_num']!=$order['good_num']){
  1115. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1116. }
  1117. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])->count();
  1118. if($retrun>0){
  1119. return error_show(1005,"存在退货订单数据");
  1120. }
  1121. if($order['order_type']==3){
  1122. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  1123. }else {
  1124. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1125. ->where(['a.skuCode' => $order['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  1126. }
  1127. if($goon==false){
  1128. return error_show(1005,"未找到商品数据");
  1129. }
  1130. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1131. if($supplier==false){
  1132. return error_show(1005,"未找到商品供应商数据");
  1133. }
  1134. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1135. if($errorCode==''){
  1136. return error_show(1004,"参数errorCode 不能为空");
  1137. }
  1138. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1139. if(empty($error)){
  1140. return error_show(1005,"未找到退货原因数据");
  1141. }
  1142. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1143. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1144. if($order['is_stock']==0){
  1145. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1146. if ($ordernum == false) {
  1147. return error_show(1005, '未找到关联采购单');
  1148. }
  1149. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1150. if ($cgd == false) {
  1151. return error_show(1005, '未找到采购单数据');
  1152. }
  1153. }
  1154. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1155. return error_show(1005, '采购单已发起入库');
  1156. }
  1157. $returnCode=makeNo("RN");
  1158. Db::startTrans();
  1159. try{
  1160. $in = [
  1161. "returnCode"=>$returnCode,
  1162. "orderCode"=>$orderCode,
  1163. "good_code"=>$order['good_code'],
  1164. "good_name"=>$order['good_name'],
  1165. "apply_id"=>$this->uid,
  1166. "apply_name"=>$this->uname,
  1167. "cgderid"=>$goon['createrid'],
  1168. "cgder"=>$goon['creater'],
  1169. "person"=>$supplier['person']??'',
  1170. "person_id"=>$supplier['personid']??0,
  1171. "error_code"=>$errorCode,
  1172. "num"=>$order['wsend_num'],
  1173. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1174. "good_price"=>$order['sale_price'],
  1175. "platform_id"=>$order['platform_id'],
  1176. "remark"=>$remark,
  1177. "order_type"=>$order['order_type'],
  1178. "is_addr"=>count($addr)>0 ?1:0,
  1179. "status"=>$order['is_stock']==1?4:1,
  1180. "is_del"=>0,
  1181. "is_all"=>1,
  1182. "addtime"=>date("Y-m-d H:i:s"),
  1183. "updatetime"=>date("Y-m-d H:i:s")
  1184. ];
  1185. $create = Db::name("sale_return")->insert($in,true);
  1186. if($create>0){
  1187. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1188. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1189. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1190. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1191. //维护台账信息
  1192. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1193. if(count($addr)!=0){
  1194. $inf=[];
  1195. foreach ($addr as $val){
  1196. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1197. $temp=[];
  1198. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1199. if ($send == false) {
  1200. Db::rollback();
  1201. return error_show(1004, "地址发货单未找到");
  1202. }
  1203. if($send['status']>=2){
  1204. Db::rollback();
  1205. return error_show(1005,"地址已发货");
  1206. }
  1207. if($order['is_stock']==1){
  1208. $val['is_del'] = 1;
  1209. $val['updatetime'] = date("Y-m-d H:i:s");
  1210. $addrup = Db::name("order_addr")->save($val);
  1211. if ($addrup == false) {
  1212. Db::rollback();
  1213. return error_show(1004, "地址更新失败");
  1214. }
  1215. $send['is_del'] = 1;
  1216. $send['updatetime'] = date("Y-m-d H:i:s");
  1217. $outup = Db::name("order_out")->save($send);
  1218. if ($outup == false) {
  1219. Db::rollback();
  1220. return error_show(1004, "地址发货单更新失败");
  1221. }
  1222. //处理发货申请单流程
  1223. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1224. "order_type" => 'CKD',
  1225. "order_code" => $send["outCode"],//出库单号
  1226. "order_id" => $send["id"],
  1227. "order_status" => 4,//全部退货
  1228. "before_status" => $send['status'],
  1229. 'holder_id=' => $send['apply_id']
  1230. ]);
  1231. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1232. // if($ordersend==false){
  1233. // Db::rollback();
  1234. // return error_show(1004, "发货单关联数据未找到");
  1235. // }
  1236. // $ordersend['status'] = 0;
  1237. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1238. // $sendip = Db::name("order_send")->save($ordersend);
  1239. // if ($sendip == false) {
  1240. // Db::rollback();
  1241. // return error_show(1004, "发货单更新失败");
  1242. // }
  1243. }
  1244. $temp['returnCode']=$returnCode;
  1245. $temp['orderCode']=$orderCode;
  1246. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1247. $temp['addrid']=$val['id'];
  1248. $temp['send_num']=$val['receipt_quantity'];
  1249. $temp['return_num']=$val['receipt_quantity'];
  1250. $temp['is_del']=0;
  1251. $temp['addtime']=date("Y-m-d H:i:s");
  1252. $temp['updatetime']=date("Y-m-d H:i:s");
  1253. $inf[]=$temp;
  1254. }
  1255. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1256. if($inadd==0){
  1257. Db::rollback();
  1258. return error_show(1005,"退货单新建失败");
  1259. }
  1260. }
  1261. if($order['is_stock']==1){
  1262. $lor=$order['status'];
  1263. $thnum = $order['wsend_num'];
  1264. $order['send_num'] += $thnum;
  1265. $order['th_num'] += $thnum;
  1266. $order['wsend_num'] =0;
  1267. $order['status'] = 3;
  1268. $order['send_status']=3;
  1269. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1270. $order['updatetime'] = date("Y-m-d H:i:s");
  1271. $uap = Db::name("sale")->save($order);
  1272. if ($uap == false) {
  1273. Db::rollback();
  1274. return error_show(1005, '销售单订单更新失败');
  1275. }
  1276. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1277. "order_code" => $order["orderCode"],//出库单号
  1278. "status" => $lor,//这里的status是之前的值
  1279. "action_remark" => '',//备注
  1280. "action_type" => "status"//新建create,编辑edit,更改状态status
  1281. ], "XSQRD", $order['status'], $order);
  1282. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1283. "order_type" => 'XSQRD',
  1284. "order_code" =>$order["orderCode"],//出库单号
  1285. "order_id" => $order["id"],
  1286. "order_status" =>$order['status'],
  1287. "before_status"=>$lor,
  1288. 'holder_id=' => $order['apply_id']
  1289. ]);
  1290. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1291. if(empty($saleinfo)) {
  1292. Db::rollback();
  1293. return error_show(1005, '商品批次数据未找到');
  1294. }
  1295. $tempnum =$thnum;
  1296. foreach ( $saleinfo as $va){
  1297. if($tempnum ==0) break;
  1298. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1299. if($stock==false) {
  1300. Db::rollback();
  1301. return error_show(1005, '商品库存数据未找到');
  1302. }
  1303. if($va['num']>=$tempnum) {
  1304. $tnm = $tempnum;
  1305. $va['num']-= $tempnum;
  1306. $tempnum=0;
  1307. }else{
  1308. $tnm = $va['num'];
  1309. $tempnum -=$va['num'];
  1310. $va['num'] =0;
  1311. }
  1312. $stock['usable_stock']+=$tnm;
  1313. $stock['wait_out_stock'] -=$tnm;
  1314. $stock['updatetime'] = date("Y-m-d H:i:s");
  1315. $st_up = Db::name("good_stock")->save($stock);
  1316. if ($st_up == false) {
  1317. return error_show(1005, '可售商品入库失败');
  1318. }
  1319. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1320. if ($ps == false) {
  1321. return error_show(1005, '商品批次退货入库失败');
  1322. }
  1323. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1324. if ($ret == false) {
  1325. return error_show(1005, '商品批次退货入库失败');
  1326. }
  1327. $va['updatetime']=date("Y-m-d H:i:s");
  1328. $sal= Db::name("sale_info")->save($va);
  1329. if ($sal == false) {
  1330. return error_show(1005, '商品批次退货入库失败');
  1331. }
  1332. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  1333. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  1334. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  1335. }
  1336. $data=[
  1337. "orderCode"=>$orderCode,
  1338. "th_type"=>1,
  1339. "th_num"=>$thnum,
  1340. "th_fee"=>round($order['sale_price']*$thnum,2),
  1341. "thCode"=>$returnCode,
  1342. "spuCode"=>$order['good_code'],
  1343. "good_name"=>$order['good_name'],
  1344. "cat_id"=>$order['cat_id'],
  1345. "apply_id"=>$this->uid,
  1346. "apply_name"=>$this->uname,
  1347. "addtime"=>date("Y-m-d H:i:s"),
  1348. "status"=>1,
  1349. "is_del"=>0
  1350. ];
  1351. $inse=Db::name("th_data")->insert($data);
  1352. if($inse==false){
  1353. Db::rollback();
  1354. return error_show(1004,"退货单更新失败");
  1355. }
  1356. }
  1357. Db::commit();
  1358. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1359. }
  1360. Db::rollback();
  1361. return error_show(1005,"退货单新建失败");
  1362. }catch (\Exception $e){
  1363. Db::rollback();
  1364. return error_show(1005, $e->getMessage());
  1365. }
  1366. }
  1367. }