Reorder.php 76 KB

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