Reorder.php 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\DataGroup as DataGroupModel;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\App;
  9. use app\admin\model\ActionLog;
  10. use think\facade\Validate;
  11. //销售单退货
  12. class Reorder extends Base
  13. {
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. }
  18. public function create(){
  19. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  20. if($ordeCode==''){
  21. return error_show(1004,"参数orderCode 不能为空");
  22. }
  23. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  24. if(empty($order)){
  25. return error_show(1005,"未找到订单数据");
  26. }
  27. $retrun =Db::name("sale_return")->where([["orderCode","=",$ordeCode],["is_del","=",0],["status", "in", [1,2,3,7,9,10]]])
  28. ->count();
  29. if($retrun>0){
  30. return error_show(1005,"存在未完成退货订单数据");
  31. }
  32. if($order['order_type']==3 ||$order['order_type']==4){
  33. $goon = Db::name("good_zixun")
  34. ->where(["spuCode"=>$order['good_code'],"is_del"=>0])
  35. ->findOrEmpty();
  36. }else {
  37. $goon = Db::name('good_platform')
  38. ->alias('a')
  39. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  40. ->where(['a.skuCode' => $order['skuCode']])
  41. ->field("b.creater,b.createrid,b.supplierNo")
  42. ->findOrEmpty();
  43. }
  44. if($goon==false){
  45. return error_show(1005,"未找到商品数据");
  46. }
  47. $userCommon = \app\admin\common\User::getIns();
  48. $tmp = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  49. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  50. $supplier = $tmp['data'];
  51. if (empty($supplier)) return json_show(1005, "未找到商品供应商数据");
  52. // $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  53. // if($supplier==false){
  54. // return error_show(1005,"未找到商品供应商数据");
  55. // }
  56. // $names = $userCommon->handle('getCodeAndName',['code'=>[$order['supplierNo'],$order['customer_code']]]);
  57. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  58. if($errorCode==''){
  59. return error_show(1004,"参数errorCode 不能为空");
  60. }
  61. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  62. if(empty($error)){
  63. return error_show(1005,"未找到退货原因数据");
  64. }
  65. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  66. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  67. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  68. // if($token==''){
  69. // return error_show(105,"参数token不能为空");
  70. // }
  71. // $user =GetUserInfo($token);
  72. // if(empty($user)||$user['code']!=0){
  73. // return error_show(1002,"申请人数据不存在");
  74. // }
  75. $is_addr=0;
  76. $rm= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  77. $ri= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  78. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:"";
  79. if($returnadr!=''){
  80. $thnum=array_sum(array_column($returnadr,"return_num"));
  81. $is_addr=1;
  82. }
  83. $returnCode=makeNo("RN");
  84. Db::startTrans();
  85. try{
  86. $in = [
  87. "returnCode"=>$returnCode,
  88. "orderCode"=>$ordeCode,
  89. "good_code"=>$order['good_code'],
  90. "good_name"=>$order['good_name'],
  91. "apply_id"=>$rm,
  92. "apply_name"=>$ri,
  93. "cgderid"=>$goon['createrid'],
  94. "cgder"=>$goon['creater'],
  95. "person"=>$supplier['person']??'',
  96. "person_id"=>$supplier['personid']??0,
  97. "error_code"=>$errorCode,
  98. "num"=>$thnum,
  99. "total_fee"=>round($order['sale_price']*$thnum,2),
  100. "good_price"=>$order['sale_price'],
  101. "platform_id"=>$order['platform_id'],
  102. "remark"=>$remark,
  103. "order_type"=>$order['order_type'],
  104. "is_addr"=>$is_addr,
  105. "status"=>$order['is_stock']==1?4:1,
  106. "is_del"=>0,
  107. "addtime"=>date("Y-m-d H:i:s"),
  108. "updatetime"=>date("Y-m-d H:i:s"),
  109. 'supplierNo' => $goon['supplierNo'],
  110. 'supplierName' => $supplier['name'],
  111. 'companyNo' => $order['supplierNo'],
  112. 'companyName' => $order['supplierName'],//$names['data'][$order['supplierNo']] ?? '',
  113. 'customer_code' => $order['customer_code'],
  114. 'customer_name' => $order['customerName'],//$names['data'][$order['customer_code']] ?? '',
  115. ];
  116. $create = Db::name("sale_return")->insert($in,true);
  117. if($create>0){
  118. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  119. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  120. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  121. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  122. //维护台账信息
  123. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  124. if($returnadr!=""){
  125. $inf=[];
  126. foreach ($returnadr as $val){
  127. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  128. $temp=[];
  129. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  130. if($addrinfo==false){
  131. Db::rollback();
  132. return error_show(1005,"地址信息未找到");
  133. }
  134. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  135. if($send['status']>=2){
  136. Db::rollback();
  137. return error_show(1005,"地址已发货");
  138. }
  139. if($order['is_stock']==1){
  140. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  141. Db::rollback();
  142. return error_show(1004, "地址发货数量不足");
  143. }
  144. $addrinfo['receipt_quantity'] -= $val['return_num'];
  145. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  146. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  147. $addrup = Db::name("order_addr")->save($addrinfo);
  148. if ($addrup == false) {
  149. Db::rollback();
  150. return error_show(1004, "地址发货数量更新失败");
  151. }
  152. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  153. if ($out == false) {
  154. Db::rollback();
  155. return error_show(1004, "地址发货单数据未找到");
  156. }
  157. // if ($send!=false) {
  158. if ($out['status'] >= 2) {
  159. Db::rollback();
  160. return error_show(1004, "地址发货单已发货");
  161. }
  162. if ($out['send_num'] < $val['return_num']) {
  163. Db::rollback();
  164. return error_show(1004, "地址发货单发货数量不足");
  165. }
  166. $out['send_num'] -= $val['return_num'];
  167. $out['is_del'] = $out['send_num']==0?1:0;
  168. $out['updatetime'] = date("Y-m-d H:i:s");
  169. $outup = Db::name("order_out")->save($out);
  170. if ($outup == false) {
  171. Db::rollback();
  172. return error_show(1004, "地址发货单更新失败");
  173. }
  174. // $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  175. // if ($ordersend['send_num'] < $val['return_num']) {
  176. // Db::rollback();
  177. // return error_show(1004, "发货单发货数量不足");
  178. // }
  179. // $ordersend['send_num'] -= $val['return_num'];
  180. // $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  181. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  182. // $sendip = Db::name("order_send")->save($ordersend);
  183. // if ($sendip == false) {
  184. // Db::rollback();
  185. // return error_show(1004, "发货单更新失败");
  186. // }
  187. // }
  188. }
  189. $temp['returnCode']=$returnCode;
  190. $temp['orderCode']=$ordeCode;
  191. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  192. $temp['addrid']=$val['id'];
  193. $temp['send_num']=$addrinfo['receipt_quantity'];
  194. $temp['return_num']=$val['return_num'];
  195. $temp['is_del']=0;
  196. $temp['addtime']=date("Y-m-d H:i:s");
  197. $temp['updatetime']=date("Y-m-d H:i:s");
  198. $inf[]=$temp;
  199. }
  200. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  201. if($inadd==0){
  202. Db::rollback();
  203. return error_show(1005,"退货单新建失败");
  204. }
  205. }
  206. if($order['is_stock']==1){
  207. if ($order['wsend_num'] < $thnum) {
  208. Db::rollback();
  209. return error_show(1004, "销售单未发货数量不足退货");
  210. }
  211. $lor=$order['status'] ;
  212. $order['wsend_num'] -= $thnum;
  213. $order['send_num'] += $thnum;
  214. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  215. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  216. $order['th_num'] += $thnum;
  217. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  218. $order['status']=3;
  219. }
  220. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  221. $order['updatetime'] = date("Y-m-d H:i:s");
  222. $uap = Db::name("sale")->save($order);
  223. if ($uap == false) {
  224. Db::rollback();
  225. return error_show(1005, '销售单订单更新失败');
  226. }
  227. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  228. "order_code" => $order["orderCode"],//出库单号
  229. "status" => $lor,//这里的status是之前的值
  230. "action_remark" => '',//备注
  231. "action_type" => "status"//新建create,编辑edit,更改状态status
  232. ], "XSQRD", $order['status'], $order);
  233. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  234. "order_type" => 'XSQRD',
  235. "order_code" =>$order["orderCode"],//出库单号
  236. "order_id" => $order["id"],
  237. "order_status" =>$order['status'],"before_status"=>$lor
  238. ]);
  239. // $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  240. // if ($ordernum == false) {
  241. // Db::rollback();
  242. // return error_show(1005, '未找到关联采购单');
  243. // }
  244. // $ordernum['send_num'] -= $thnum;
  245. // $orderup = Db::name("order_num")->save($ordernum);
  246. // if ($orderup == false) {
  247. // Db::rollback();
  248. // return error_show(1005, '关联数据更新失败');
  249. // }
  250. // $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  251. // if ($cgd == false) {
  252. // Db::rollback();
  253. // return error_show(1005, '未找到采购单数据');
  254. // }
  255. // $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  256. // $cgd['th_num'] += $thnum;
  257. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  258. // $cgdup = Db::name("purchease_order")->save($cgd);
  259. // if ($cgdup == false) {
  260. // Db::rollback();
  261. // return error_show(1005, '采购单数据更新失败');
  262. // }
  263. // if ($cgd['bkcode'] != "") {
  264. // $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  265. // ->find();
  266. // if ($bk == false) {
  267. // Db::rollback();
  268. // return error_show(1005, '未找到备库单数据');
  269. // }
  270. // $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  271. // if ($orderbk == false) {
  272. // Db::rollback();
  273. // return error_show(1005, '备库单未完全入库');
  274. // }
  275. // $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>
  276. // 1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  277. // ->find();
  278. //
  279. // $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  280. // $orderbk['merge_num'] = $merge_num['num'];
  281. // $orderbk['updatetime'] = date("Y-m-d H:i:s");
  282. // $orderbkup = Db::name("order_bk")->save($orderbk);
  283. // if ($orderbkup == false) {
  284. // Db::rollback();
  285. // return error_show(1005, '备库单库存数据释放失败');
  286. // }
  287. // }
  288. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$ordeCode],["num",">",0]])->select()
  289. ->toArray();
  290. if(empty($saleinfo)) {
  291. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  292. if ($ordernum == false) {
  293. Db::rollback();
  294. return error_show(1005, '未找到关联采购单');
  295. }
  296. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  297. if ($cgd == false) {
  298. Db::rollback();
  299. return error_show(1005, '未找到采购单数据');
  300. }
  301. $bn =makeNo("BN");
  302. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code'=>$cgd['wsm_code'],"is_del"=>0,"status"=>1])->find();
  303. if($stock==false){
  304. Db::rollback();
  305. return error_show(1005, '商品库存数据未找到');
  306. }
  307. $stock['usable_stock']+=$thnum;
  308. $stock['wait_out_stock'] -=$thnum;
  309. $stock['updatetime'] = date("Y-m-d H:i:s");
  310. $st_up = Db::name("good_stock")->save($stock);
  311. if ($st_up == false) {
  312. return error_show(1005, '可售商品入库失败');
  313. }
  314. $yp=GoodStockInfo::AddBn($stock['id'],$bn,$thnum,$cgd['good_price']);
  315. if($yp==false){
  316. Db::rollback();
  317. return error_show(1005, '商品批次退货入库失败');
  318. }
  319. }else{
  320. $tempnum =$thnum;
  321. foreach ( $saleinfo as $va){
  322. if($tempnum ==0) break;
  323. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])
  324. ->find();
  325. if($stock==false) {
  326. Db::rollback();
  327. return error_show(1005, '商品库存数据未找到');
  328. }
  329. if($va['num']>=$tempnum) {
  330. $tnm = $tempnum;
  331. $va['num']-= $tempnum;
  332. $va['th_num']+= $tempnum;
  333. $tempnum=0;
  334. }else{
  335. $tnm = $va['num'];
  336. $tempnum -=$va['num'];
  337. $va['th_num']+= $va['num'];
  338. $va['num'] =0;
  339. }
  340. $stock['usable_stock']+=$tnm;
  341. $stock['wait_out_stock'] -=$tnm;
  342. $stock['updatetime'] = date("Y-m-d H:i:s");
  343. $st_up = Db::name("good_stock")->save($stock);
  344. if ($st_up == false) {
  345. return error_show(1005, '可售商品入库失败');
  346. }
  347. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  348. if ($ps == false) {
  349. return error_show(1005, '商品批次退货入库失败');
  350. }
  351. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  352. if ($ret == false) {
  353. return error_show(1005, '商品批次退货入库失败');
  354. }
  355. $va['updatetime']=date("Y-m-d H:i:s");
  356. $sal= Db::name("sale_info")->save($va);
  357. if ($sal == false) {
  358. return error_show(1005, '商品批次退货入库失败');
  359. }
  360. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  361. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  362. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'XSTHD');
  363. }
  364. }
  365. $data=[
  366. "orderCode"=>$ordeCode,
  367. "th_type"=>1,
  368. "th_num"=>$thnum,
  369. "th_fee"=>round($order['sale_price']*$thnum,2),
  370. "thCode"=>$returnCode,
  371. "spuCode"=>$order['good_code'],
  372. "good_name"=>$order['good_name'],
  373. "cat_id"=>$order['cat_id'],
  374. "apply_id"=>$rm,
  375. "apply_name"=>$ri,
  376. "addtime"=>date("Y-m-d H:i:s"),
  377. "status"=>1,
  378. "is_del"=>0
  379. ];
  380. $inse=Db::name("th_data")->insert($data);
  381. if($inse==false){
  382. Db::rollback();
  383. return error_show(1004,"退货单更新失败");
  384. }
  385. }
  386. Db::commit();
  387. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  388. }
  389. Db::rollback();
  390. return error_show(1005,"退货单新建失败");
  391. }catch (\Exception $e){
  392. Db::rollback();
  393. return error_show(1005,$e->getMessage());
  394. }
  395. }
  396. //退货单列表
  397. public function list(){
  398. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  399. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  400. $where =[['sr.is_del',"=",0]];
  401. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  402. if($bkcode!=""){
  403. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  404. }
  405. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  406. if($status!==""){
  407. $where[]=['sr.status',"=", $status];
  408. }
  409. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']) :"";
  410. if($orderCode!=""){
  411. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  412. }
  413. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']) :"";
  414. if($apply_name!=""){
  415. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  416. }
  417. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  418. if($start!==""){
  419. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  420. }
  421. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  422. if($end!==""){
  423. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  424. }
  425. //商品成本编码搜索
  426. $good_code = isset($this->post['good_code']) && $this->post['good_code'] != "" ? trim($this->post['good_code']) : "";
  427. if ($good_code != "") {
  428. $where[] = ['sr.good_code', "like", "%{$good_code}%"];
  429. }
  430. //商品上线编码搜索
  431. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  432. if ($skuCode != "") {
  433. $where[] = ['b.skuCode', "like", "%{$skuCode}%"];
  434. }
  435. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
  436. if ($relaComNo != "") $where[] = ['sr.companyNo', '=', $relaComNo];
  437. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  438. if ($supplierNo !== "") $where[] = ['sr.supplierNo', "like", '%' . $supplierNo . '%'];
  439. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] != "" ? trim($this->post['companyNo']) : "";
  440. if ($companyNo !== "") $where[] = ['sr.companyNo', "like", '%' . $companyNo . '%'];
  441. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] != "" ? trim($this->post['customer_code']) : "";
  442. if ($customer_code !== "") $where[] = ['sr.customer_code', "like", '%' . $customer_code . '%'];
  443. $order_source = $order_source = isset($this->post['order_source']) && $this->post['order_source'] != "" ? trim($this->post['order_source']) : "";
  444. if ($order_source !== "") $where[] = ['b.order_source', "=", $order_source];
  445. $condition=[];
  446. // $role=$this->checkRole();
  447. // if(!empty($role['write']) && $this->uid!=""){
  448. // // $where[]=["sr.apply_id","in",$role['write']];
  449. // $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).")";
  450. // }
  451. //只有level2的账号过滤数据权限
  452. if ($this->level == 2) {
  453. $role = $this->checkDataShare();
  454. $hand = resign_hand_user($this->uid, 0);
  455. if (!empty($role[DataGroupModel::$type_全部])) {
  456. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  457. // $uidim =implode(",",$hand);
  458. // $condition .= "sr.cgderid in ($uidim) or sr.person_id in ($uidim) or sr.apply_id in (" . implode(',',$arr) .
  459. // ")";
  460. $condition[] = ["sr.apply_id", "in", $arr];
  461. $condition[] = ["sr.cgderid", "in", $hand];
  462. $condition[] = ["sr.person_id", "in", $hand];
  463. }
  464. }
  465. //供应商账号不允许看到库存品数据
  466. if ($this->level == 3) $where[]=['b.is_stock','<>',1];
  467. // $role = $this->checkDataShare();
  468. // if (!empty($role[DataGroupModel::$type_全部])) $condition .= "sr.cgderid = {$this->uid} or sr.person_id = {$this->uid} or sr.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . ")";
  469. if(in_array($this->roleid,config('app.wsm_cgder_role'))){
  470. $where[]=["b.order_type","=",1];
  471. }
  472. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  473. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  474. // $customer_code = trim($this->post['customer_code']??'');
  475. // $where[]=['b.customer_code', "like", "%{$customer_code}%"];
  476. $count=Db::name("sale_return")
  477. ->alias('sr')
  478. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  479. ->where($where)
  480. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  481. ->count('sr.id');
  482. $total = ceil($count/$size);
  483. $page = $total>=$page ? $page :$total;
  484. $list = Db::name("sale_return")
  485. ->alias('sr')
  486. ->field('sr.*,b.skuCode,b.sale_price,b.good_num total_num,b.order_source')
  487. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  488. ->where($where)
  489. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  490. ->order("sr.addtime desc")
  491. ->page($page,$size)
  492. ->select()
  493. ->toArray();
  494. // echo Db::name("sale_return")->getLastSql();
  495. $all_createrid = array_column($list,'apply_id');
  496. $item = get_company_name_by_uid($all_createrid);
  497. // $userCommon = \app\admin\common\User::getIns();
  498. // $names = $userCommon->handle('getCodeAndName',['code'=>array_merge(array_column($list,'supplierNo'),array_column($list,'customer_code'))]);
  499. $data=[];
  500. foreach ($list as $value){
  501. $value['error_msg']='';
  502. if($value['error_code']!=''){
  503. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  504. $value['error_msg']= isset($error['result'])?$error['result']:"";
  505. }
  506. // $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  507. // $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  508. $value['return_total'] =$value['sale_price']*$value['num'] ;
  509. // $value['total_num'] =$order['good_num'] ;
  510. $value['company_name'] = $item[$value['apply_id']]??'';
  511. // $value['supplierName'] = $names['data'][$value['supplierNo']] ?? '';//isset($wsm['company']) ? $wsm['company'] : "";
  512. // $value['customerName'] = $names['data'][$value['customer_code']] ?? '';//isset($wsm['company']) ? $wsm['company'] : "";
  513. //是否具有编辑权限
  514. // $value['is_allow_update'] = 0;
  515. // if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  516. $data[]=$value ;
  517. }
  518. return app_show(0,"获取成功",["count"=>$count,'list'=>$data]);
  519. }
  520. public function info(){
  521. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']) :"";
  522. if($code==""){
  523. return error_show(1004,"参数returnCode不能为空");
  524. }
  525. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  526. if(empty($info)){
  527. return error_show(1004,"未找到退货数据");
  528. }
  529. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  530. if($orderinfo['order_type']==3 ||$orderinfo['order_type']==4){
  531. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  532. }else {
  533. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  534. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  535. }
  536. if ($goon==false) {
  537. return error_show(1003, "未找到商品数据");
  538. }
  539. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  540. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  541. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  542. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  543. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  544. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  545. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  546. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  547. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  548. $info['return_total'] = $info['sale_price']*$info['num'] ;
  549. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  550. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  551. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  552. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  553. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  554. // $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  555. $userCommon = \app\admin\common\User::getIns();
  556. if($info['return_wsm']!=""){
  557. $wsmcode = Db::name("warehouse_info")
  558. ->alias("a")
  559. // ->leftJoin("supplier b","a.supplierNo=b.code")
  560. ->where(["a.wsm_code"=>$info['return_wsm']])
  561. ->field("a.name as wsm_name,a.supplierNo")
  562. ->find();
  563. $tmp = $userCommon->handle('getCodeAndName', ['code' => [
  564. // $orderinfo['supplierNo'],
  565. // $orderinfo['customer_code'],
  566. $wsmcode['supplierNo']??'',
  567. ]]);
  568. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  569. $info['wsm_supplier'] =$tmp['data'][$wsmcode['supplierNo']]??'';//isset($wsmcode['name']) ? $wsmcode['name']:"";
  570. $info['wsm_supplierNo'] =$wsmcode['supplierNo']??'';//isset($wsmcode['code']) ? $wsmcode['code']:"";
  571. }
  572. // if(!empty($wsmcode['supplierNo'])){
  573. // $info['wsm_supplier'] =$tmp['data'][$wsmcode['supplierNo']]??'';//isset($wsmcode['name']) ? $wsmcode['name']:"";
  574. // $info['wsm_supplierNo'] =$wsmcode['supplierNo']??'';//isset($wsmcode['code']) ? $wsmcode['code']:"";
  575. // }
  576. // $info['customer_name']='';
  577. // if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  578. // $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  579. // $info['customer_name'] = $tmp['data'][$orderinfo['customer_code']]??'';//isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  580. // }
  581. // $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  582. // $info['supplier_name']='';
  583. // if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  584. //// $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  585. //// $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  586. // $info['supplier_name'] = $tmp['data'][$orderinfo['supplierNo']] ?? '';
  587. //
  588. // }
  589. $info['platform_name']='';
  590. $info['platform_id']=$orderinfo['platform_id'];
  591. if($orderinfo['platform_id']!=0){
  592. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  593. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  594. }
  595. $info['cgd_wsend']="";
  596. $info['cgd_send']="";
  597. $info['cgd_total']="";
  598. if($orderinfo['order_type']==2){
  599. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  600. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  601. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  602. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  603. }
  604. $info['error_msg']='';
  605. if($info['error_code']!=''){
  606. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  607. $info['error_msg']= isset($error['result'])?$error['result']:"";
  608. }
  609. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  610. $all_wsm_code = array_column($wsm_return, 'wsm_code');
  611. $all_wsmcode_info = Db::name("warehouse_info")
  612. ->where(["wsm_code" => $all_wsm_code])
  613. ->column("name,supplierNo", 'wsm_code');
  614. $userCommon = \app\admin\common\User::getIns();
  615. $all_supplier_name = $userCommon->handle('getCodeAndName', ['code' => array_unique(array_column($all_wsmcode_info, 'supplierNo'))]);
  616. $wsm=[];
  617. if(!empty($wsm_return)){
  618. foreach ($wsm_return as $value){
  619. $value['wsm_name']="";
  620. $value['wsm_supplier']="";
  621. $value['wsm_supplierNo']="";
  622. if($value['wsm_code']!=""){
  623. // $wsmcode = Db::name("warehouse_info")
  624. // ->alias("a")
  625. //// ->leftJoin("supplier b","a.supplierNo=b.code")
  626. // ->where(["a.wsm_code"=>$value['wsm_code']])
  627. // ->field("a.name as wsm_name,b.name,b.code")
  628. // ->find();
  629. $value['wsm_name'] = $all_wsmcode_info[$value['wsm_code']]['name'] ?? '';//isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  630. $value['wsm_supplier'] = $all_wsmcode_info[$value['wsm_code']]['supplierNo'] ?? '';//isset($wsmcode['name']) ? $wsmcode['name']:"";
  631. $value['wsm_supplierNo'] = $all_supplier_name[$all_wsmcode_info[$value['wsm_code']]['supplierNo'] ?? ''] ?? '';//isset($wsmcode['code']) ? $wsmcode['code']:"";
  632. }
  633. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  634. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  635. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  636. $value['wsm_send'] = $send ?? 0;
  637. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  638. $wsm[]=$value;
  639. }
  640. }
  641. $info['wsminfo']=$wsm;
  642. $addr =Db::name("sale_returnaddr")
  643. ->alias('a')
  644. ->field('a.*,b.addr,b.addr_code,b.contactor,b.mobile,b.post_fee,b.arrive_time,b.customer_code,b.receipt_quantity,b.orderCode')
  645. ->leftJoin('order_addr b','b.id=a.addrid')
  646. ->where(["a.returnCode"=>$info["returnCode"],"a.is_del"=>0])
  647. ->select()
  648. ->toArray();
  649. $addrinfo=[];
  650. if(!empty($addr)){
  651. $userCommon=\app\admin\common\User::getIns();
  652. $customer_name = $userCommon->handle('getCodeAndName',['code'=>array_column($addr,'customer_code')]);
  653. foreach ( $addr as $value){
  654. // $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  655. $value['addr']=isset($value['addr'])?$value['addr']:"";
  656. $value['addr_code']=isset($value['addr_code'])?$value['addr_code']:"";
  657. $value['contactor']=isset($value['contactor'])?$value['contactor']:"";
  658. $value['mobile']=isset($value['mobile'])?$value['mobile']:"";
  659. $value['post_fee']=isset($value['post_fee'])?$value['post_fee']:"";
  660. $value['addive_time']=isset($value['addive_time'])?$value['addive_time']:"";
  661. $value['customer_code']=isset($value['customer_code']) ?$value['customer_code']:"" ;
  662. $value['receipt_quantity']=isset($value['receipt_quantity']) ?$value['receipt_quantity']:"" ;
  663. $send = Db::name("order_out")->where(['addrid' => $value['addrid'], 'orderCode' => $value['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  664. $value['addr_send'] = $send ?? 0;
  665. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  666. // $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  667. $value['customer_name'] = $customer_name['data'][$value['customer_code']]??'';//isset($customer['companyName']) ? $customer['companyName']:"";
  668. $addrinfo[]=$value;
  669. }
  670. }
  671. $info['addrinfo']=$addrinfo;
  672. $info['can']=$int;
  673. return app_show(0,"获取成功",$info);
  674. }
  675. /**
  676. * @return \think\response\Json|void
  677. * @throws \think\db\exception\DataNotFoundException
  678. * @throws \think\db\exception\DbException
  679. * @throws \think\db\exception\ModelNotFoundException
  680. */
  681. public function delete(){
  682. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']) :"";
  683. if($code==""){
  684. return error_show(1004,"参数returnCode不能为空");
  685. }
  686. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  687. if(empty($info)){
  688. return error_show(1004,"未找到退货数据");
  689. }
  690. $info["is_del"]=1;
  691. $info["updatetime"]=date("Y-m-d H:i:s");
  692. $del = Db::name("sale_return")->save($info);
  693. if($del){
  694. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  695. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",0,$ste);
  696. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  697. ProcessOrder::workdel($process);
  698. return app_show(0,"删除成功");
  699. }else{
  700. return error_show(1004,"删除失败");
  701. }
  702. }
  703. /**审核
  704. * 1待业务审批
  705. * 2待专员审批(该节点废除)
  706. * 3待主管审批(该节点废除)
  707. * 4退货完成
  708. * 5业务驳回
  709. * 6采购驳回(该节点废除)
  710. * 7专员审批不通过(该节点废除)
  711. * 9待供应商审核
  712. * 8供应商已驳回待采购审核
  713. * 10业务公司修改待供应商确认
  714. * 11待设置退货工单
  715. * 12待库管发货
  716. */
  717. public function exam()
  718. {
  719. $param = $this->request->only(['returnCode', 'status', 'remark' => '', 'return_wsm' => ''], 'post', 'trim');
  720. $val = Validate::rule([
  721. 'returnCode|退货单编号' => 'require',
  722. 'status|状态' => 'require|in:5,9,8,10,4',
  723. 'remark|备注' => 'requireIf:status,5|requireIf:status,8|requireIf:status,10',
  724. ]);
  725. if ($val->check($param) == false) return json_show(1004, $val->getError());
  726. $code = $param['returnCode'];
  727. $status = $param['status'];
  728. $remark = $param['remark'];
  729. // $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']):"";
  730. // if($code==""){
  731. // return error_show(1004,"参数returnCode不能为空");
  732. // }
  733. $info = Db::name("sale_return")->where(["returnCode" => $code, "is_del" => 0])->findOrEmpty();
  734. if (empty($info)) return error_show(1004, "未找到退货数据");
  735. switch ($info['status']) {
  736. case 1:
  737. if (in_array($param['status'], [5, 9]) == false) return json_show(1004, '选项错误');
  738. break;
  739. case 9:
  740. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  741. break;
  742. case 8:
  743. if (in_array($param['status'], [4, 10]) == false) return json_show(1004, '选项错误');
  744. if ($param['status'] == 4) {
  745. if ($param['return_wsm'] == '') return json_show(1004, '仓库编码不能为空');
  746. $tmp = Db::name("warehouse_info")
  747. ->field('id')
  748. ->where(['wsm_code' => $param['return_wsm']])
  749. ->findOrEmpty();
  750. if (empty($tmp)) return json_show(1004, '未找到仓库数据');
  751. else $info['return_wsm'] = $param['return_wsm'];
  752. }
  753. break;
  754. case 10:
  755. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  756. break;
  757. }
  758. //当处于以下节点时,level2账号必须是供应商负责人操作,level3账号不做限制
  759. if(in_array($info['status'],[9,10]) && ($this->level==2) && ($this->uid!=$info['person_id'])) return json_show(1004,'您不是供应商负责人,此时无权操作');
  760. if ($param['status'] == 10) $info['loop_total'] += 1;//只要走到节点10(业务公司修改,待供应商确认),就增加次数
  761. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  762. // if($status===""){
  763. // return error_show(1004,"参数status不能为空");
  764. // }
  765. // $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  766. $var = $info['status'];
  767. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->findOrEmpty();
  768. if (empty($orderinfo)) return error_show(1004, "未找到订单数据");
  769. if ($status == 4) {
  770. if ($info['is_addr'] == 1) {
  771. $addr = Db::name("sale_returnaddr")
  772. ->where(['returnCode' => $info['returnCode'], "is_del" => 0])
  773. ->select()
  774. ->toArray();
  775. if (empty($addr)) return error_show(1004, "未找到发货单地址数据");
  776. }
  777. }
  778. // if($status==3){
  779. // $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  780. // if($is_th===""){
  781. // return error_show(1004,"参数is_th不能为空");
  782. // }
  783. // $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  784. // if($is_th==0){
  785. // if($return_wsm===""){
  786. // return error_show(1004,"参数return_wsm 不能为空");
  787. // }
  788. // $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  789. // if($wsmcode==false){
  790. // return error_show(1004,"为找到仓库数据");
  791. // }
  792. // }
  793. // $info['return_wsm'] =$return_wsm ;
  794. // $info['is_th'] =$is_th ;
  795. // }
  796. if ($info['status'] == 9 && $param['status'] == 4) $info['is_th'] = 1;
  797. Db::startTrans();
  798. try {
  799. $date = date("Y-m-d H:i:s");
  800. $userCommon = \app\admin\common\User::getIns();
  801. $temp = $info['status'];
  802. $info['status'] = $status;
  803. $remark != "" ? $info['remark'] = $remark : "";
  804. $info["updatetime"] = $date;
  805. $up = Db::name("sale_return")->save($info);
  806. if ($up) {
  807. //如果是节点2(待专员审核),要将待办数据推给供应商负责人
  808. if ($info['status'] == 2) $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'wait_id' => $info['person_id'], 'wait_name' => $info['person']];
  809. else $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'holder_id' => $info['apply_id']];
  810. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  811. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  812. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $ste, "XSTHD", $status, $info);
  813. if ($status == 4) {
  814. if ($info['is_addr'] == 1) {
  815. if (isset($addr) && !empty($addr)) {
  816. foreach ($addr as $value) {
  817. $addrinfo = Db::name("order_addr")
  818. ->where(['id' => $value['addrid'], "is_del" => 0])
  819. ->find();
  820. if ($addrinfo == false) {
  821. Db::rollback();
  822. return error_show(1004, "地址数据未找到");
  823. }
  824. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  825. Db::rollback();
  826. return error_show(1004, "地址发货数量不足");
  827. }
  828. $addrinfo['receipt_quantity'] -= $value['return_num'];
  829. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  830. $addrinfo['updatetime'] = $date;
  831. $addrup = Db::name("order_addr")->save($addrinfo);
  832. if ($addrup == false) throw new Exception('地址发货数量更新失败');
  833. if ($value['outCode'] != "") {
  834. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->findOrEmpty();
  835. if (empty($out)) throw new Exception('地址发货单数据未找到');
  836. if ($out['status'] >= 2) throw new Exception('地址发货单已发货');
  837. if ($out['send_num'] < $value['return_num']) throw new Exception('地址发货单发货数量不足');
  838. $out['send_num'] -= $value['return_num'];
  839. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  840. $out['updatetime'] = $date;
  841. $outup = Db::name("order_out")->save($out);
  842. if ($outup == false) throw new Exception('地址发货单更新失败');
  843. }
  844. }
  845. }
  846. }
  847. if ($orderinfo['wsend_num'] < $info['num']) throw new Exception('销售单未发货数量不足退货');
  848. $lor = $orderinfo['status'];
  849. $orderinfo['wsend_num'] -= $info['num'];
  850. $orderinfo['send_num'] += $info['num'];
  851. $orderinfo['status'] = $orderinfo['wsend_num'] == 0 ? 2 : ($orderinfo['send_num'] == 0 ? 0 : 1);
  852. $orderinfo['send_status'] = $orderinfo['wsend_num'] == 0 ? 3 : ($orderinfo['send_num'] == 0 ? 1 : 2);
  853. if ($orderinfo['is_stock'] == 1) {
  854. $orderinfo['th_num'] += $info['num'];
  855. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  856. $orderinfo['status'] = 3;
  857. }
  858. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  859. $orderinfo['updatetime'] = $date;
  860. $uap = Db::name("sale")->save($orderinfo);
  861. if ($uap == false) throw new Exception('销售单订单更新失败');
  862. } else {
  863. if ($info['is_th'] == 1) {
  864. $orderinfo['th_num'] += $info['num'];
  865. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  866. $orderinfo['status'] = 3;
  867. }
  868. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  869. $orderinfo['updatetime'] = $date;
  870. $uap = Db::name("sale")->save($orderinfo);
  871. if ($uap == false) throw new Exception('销售单订单更新失败');
  872. $ordernum = Db::name("order_num")
  873. ->where(['orderCode' => $orderinfo['orderCode']])
  874. ->findOrEmpty();
  875. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  876. $ordernum['send_num'] -= $info['num'];
  877. $orderup = Db::name("order_num")->save($ordernum);
  878. if ($orderup == false) throw new Exception('关联数据更新失败');
  879. $cgd = Db::name("purchease_order")
  880. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  881. ->findOrEmpty();
  882. if ($cgd == false) throw new Exception('未找到采购单数据');
  883. if ($info['is_all'] == 1 && $cgd['send_status'] != 1) throw new Exception('采购单已入库无法全部退货');
  884. $lor = $cgd['status'];
  885. if ($cgd['wsend_num'] < $info['num']) {
  886. $cgd['send_num'] += $cgd['wsend_num'] == 0 ? 0 : ($info['num'] - $cgd['wsend_num']);
  887. $cgd['wsend_num'] = 0;
  888. } else {
  889. $cgd['wsend_num'] -= $info['num'];
  890. $cgd['send_num'] += $info['num'];
  891. }
  892. $cgd['status'] = $cgd['wsend_num'] == 0 ? 2 : ($cgd['send_num'] == 0 ? 0 : 1);
  893. $cgd['send_status'] = $cgd['wsend_num'] == 0 ? 3 : ($cgd['send_num'] == 0 ? 1 : 2);
  894. $cgd['th_num'] += $info['num'];
  895. if ($cgd['th_num'] == $cgd['send_num'] && $cgd['wsend_num'] == 0) {
  896. $cgd['status'] = 4;
  897. }
  898. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  899. $cgd['updatetime'] = $date;
  900. $cgdup = Db::name("purchease_order")->save($cgd);
  901. if ($cgdup == false) throw new Exception('采购单数据更新失败');
  902. $stock = Db::name("good_stock")
  903. ->where(['is_del' => 0, "spuCode" => $info['good_code'], 'wsm_code' => $cgd['wsm_code']])
  904. ->findOrEmpty();
  905. if (empty($stock)) throw new Exception('商品仓库未找到');
  906. if ($stock['wait_in_stock'] < $info['num']) {
  907. $stock['wait_in_stock'] = 0;
  908. if ($stock['usable_stock'] > $info['num'] - $stock['wait_in_stock']) {
  909. $stock['usable_stock'] -= $info['num'] - $stock['wait_in_stock'];
  910. } else {
  911. $stock['usable_stock'] = 0;
  912. $stock['wait_out_stock'] -= $info['num'] - $stock['wait_in_stock'] - $stock['usable_stock'];
  913. }
  914. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  915. } else {
  916. $stock['wait_in_stock'] -= $info['num'];
  917. }
  918. $stock['updatetime'] = $date;
  919. $st_up = Db::name("good_stock")->save($stock);
  920. if ($st_up == false) throw new Exception('可售商品入库失败');
  921. } else {
  922. $orderinfo['th_num'] += $info['num'];
  923. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  924. $orderinfo['status'] = 3;
  925. }
  926. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  927. $orderinfo['updatetime'] = $date;
  928. $uap = Db::name("sale")->save($orderinfo);
  929. if ($uap == false) throw new Exception('销售单订单更新失败');
  930. $ordernum = Db::name("order_num")
  931. ->where(['orderCode' => $orderinfo['orderCode']])
  932. ->findOrEmpty();
  933. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  934. $ordernum['send_num'] -= $info['num'];
  935. $ordernum['wsend_num'] = $info['num'];
  936. $orderup = Db::name("order_num")->save($ordernum);
  937. if ($orderup == false) throw new Exception('关联数据更新失败');
  938. $cgd = Db::name("purchease_order")
  939. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  940. ->findOrEmpty();
  941. if (empty($cgd)) throw new Exception('未找到采购单数据');
  942. $stock = Db::name("good_stock")
  943. ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code']])
  944. ->findOrEmpty();
  945. if (empty($stock)) throw new Exception('商品仓库未找到');
  946. if ($info['is_all'] == 1) {
  947. if ($stock['wait_in_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  948. else $stock['wait_in_stock'] -= $info['num'];
  949. } else {
  950. if ($stock['usable_stock'] + $stock['wait_out_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  951. else {
  952. if ($stock['usable_stock'] > $info['num']) {
  953. $stock['usable_stock'] -= $info['num'];
  954. } else {
  955. $stock['usable_stock'] = 0;
  956. $stock['wait_out_stock'] -= $info['num'] - $stock['usable_stock'];
  957. }
  958. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  959. }
  960. }
  961. $stock['updatetime'] = $date;
  962. $st_up = Db::name("good_stock")->save($stock);
  963. if ($st_up == false) throw new Exception('可售商品入库失败');
  964. }
  965. }
  966. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  967. "order_code" => $orderinfo["orderCode"],//出库单号
  968. "status" => $lor,//这里的status是之前的值
  969. "action_remark" => '',//备注
  970. "action_type" => "status"//新建create,编辑edit,更改状态status
  971. ], "XSQRD", $orderinfo['status'], $orderinfo);
  972. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  973. "order_type" => 'XSQRD',
  974. "order_code" => $orderinfo["orderCode"],//出库单号
  975. "order_id" => $orderinfo["id"],
  976. "order_status" => $orderinfo['status'], "before_status" => $lor
  977. ]);
  978. //对应采购单也要处理
  979. // $holder_id = Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0);
  980. $holder = $userCommon->handle('sInfo', ['code' => $cgd['supplierNo']]);
  981. if (!isset($holder['code']) || $holder['code'] != 0) throw new Exception($holder['message']);
  982. $holder_id = $holder['data']['personid'];
  983. if (in_array($cgd['status'], [4])) {
  984. if ($orderinfo['is_stock'] == 1) {
  985. //库存品,推给31库管人员、41库管-张凯旋
  986. $uid = Db::name('user_role')
  987. ->where([
  988. ['is_del', '=', 0],
  989. ['roleid', 'in', [31, 41]],
  990. ['status', '=', 1]
  991. ])->column('uid');
  992. // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  993. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  994. "order_type" => 'CGD',
  995. "order_code" => $cgd['cgdNo'],
  996. "order_id" => $cgd["id"],
  997. "order_status" => $cgd['status'],
  998. "before_status" => $lor,
  999. 'holder_id' => $holder_id,
  1000. 'handle_user_list' => implode(',', $uid),
  1001. ]);
  1002. } else {
  1003. // if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1004. //非库存品和采返商品,推给供应商负责人
  1005. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  1006. "order_type" => 'CGD',
  1007. "order_code" => $cgd['cgdNo'],
  1008. "order_id" => $cgd["id"],
  1009. "order_status" => $cgd['status'],
  1010. "before_status" => $lor,
  1011. 'holder_id' => $holder_id,
  1012. 'handle_user_list' => $orderinfo['cgderid'],
  1013. ]);
  1014. }
  1015. }
  1016. // ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  1017. // "order_type" => 'CGD',
  1018. // "order_code" => $cgd['cgdNo'],
  1019. // "order_id" => $cgd["id"],
  1020. // "order_status" => $cgd['status'],
  1021. // "before_status" => $lor,
  1022. // 'holder_id' => $holder_id
  1023. // ]);
  1024. //以下判断是当供应商不同意要退回到业务公司的时候触发的,在3.0系统中不需要了2023-02-09@by武
  1025. // if ($orderinfo['is_stock'] == 1 || $info['is_th'] == 0) {
  1026. //
  1027. // if ($orderinfo['order_type'] != 1) {
  1028. // if ($orderinfo['order_type'] == 3) {
  1029. // $goon = Db::name("good_zixun")
  1030. // ->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])
  1031. // ->findOrEmpty();
  1032. // $isZx = 1;
  1033. // } else {
  1034. // $goon = Db::name('good_basic')
  1035. // ->where(['spuCode' => $orderinfo['good_code']])
  1036. // ->findOrEmpty();
  1037. // $isZx = 2;
  1038. // }
  1039. // $spuCode = $this->CheckGoodZx($goon, $isZx, $code);
  1040. // $wsmcode = $info['return_wsm'];
  1041. // if ($wsmcode == "") throw new Exception('未找到退货仓库');
  1042. //
  1043. // $stock = Db::name("good_stock")
  1044. // ->where(['is_del' => 0, "spuCode" => $spuCode, 'wsm_code' => $wsmcode])
  1045. // ->findOrEmpty();
  1046. // if (empty($stock)) {
  1047. // $stock = [
  1048. // "spuCode" => $spuCode,
  1049. // "wsm_code" => $wsmcode,
  1050. // "usable_stock" => 0,
  1051. // "wait_out_stock" => 0,
  1052. // "wait_in_stock" => 0,
  1053. // "total_stock" => 0,
  1054. // "addtime" => $date,
  1055. // "updatetime" => $date,
  1056. // ];
  1057. // }
  1058. // $stock['usable_stock'] += $info['num'];
  1059. // $stock['updatetime'] = $date;
  1060. // $st_up = Db::name("good_stock")->save($stock);
  1061. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1062. //
  1063. // $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  1064. // $sabebn = Db::name("sale_info")
  1065. // ->where(["orderCode" => $orderinfo["orderCode"]])
  1066. // ->select()
  1067. // ->toArray();
  1068. // if (!empty($sabebn)) {
  1069. // $total_num = $info['num'];
  1070. // foreach ($sabebn as $ve) {
  1071. // $tempnum = 0;
  1072. // if ($total_num == 0) break;
  1073. // if ($total_num >= $ve['num']) {
  1074. // $tempnum = $ve['num'];
  1075. // $total_num -= $ve['num'];
  1076. // $ve['th_num'] += $ve['num'];
  1077. // $ve['num'] = 0;
  1078. // } else {
  1079. // $tempnum = $total_num;
  1080. // $ve['num'] -= $total_num;
  1081. // $ve['th_num'] += $total_num;
  1082. // $total_num = 0;
  1083. // }
  1084. // $bnin = GoodStockInfo::AddBn($stockid, $ve['bnCode'], $tempnum, $ve['origin_price']);
  1085. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1086. //
  1087. // $ve['updatetime'] = $date;
  1088. // $up = Db::name("sale_info")->save($ve);
  1089. // if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1090. //
  1091. // $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1092. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1093. //
  1094. // }
  1095. // } else {
  1096. // $bn = makeNo("BN");
  1097. // $bnin = GoodStockInfo::AddBn($stockid, $bn, $info['num'], $cgd['good_price'] ?? 0);
  1098. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1099. //
  1100. // }
  1101. // $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  1102. // GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'XSTHD');
  1103. // } else {
  1104. // $sabebn = Db::name("sale_info")
  1105. // ->where(["orderCode" => $orderinfo["orderCode"]])
  1106. // ->select()
  1107. // ->toArray();
  1108. // if (!empty($sabebn)) {
  1109. // $total_num = $info['num'];
  1110. // foreach ($sabebn as $ve) {
  1111. // $stock = Db::name("good_stock")
  1112. // ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'id' => $ve['stockid']])
  1113. // ->findOrEmpty();
  1114. // if ($stock == false) throw new Exception('商品库存数据未找到');
  1115. //
  1116. // $tempnum = 0;
  1117. // if ($total_num == 0) break;
  1118. // if ($total_num >= $ve['num']) {
  1119. // $tempnum = $ve['num'];
  1120. // $total_num -= $ve['num'];
  1121. // $ve['th_num'] += $ve['num'];
  1122. // $ve['num'] = 0;
  1123. // } else {
  1124. // $tempnum = $total_num;
  1125. // $ve['num'] -= $total_num;
  1126. // $ve['th_num'] += $total_num;
  1127. // $total_num = 0;
  1128. // }
  1129. // $stock['usable_stock'] += $tempnum;
  1130. // $stock['wait_out_stock'] -= $tempnum;
  1131. // $stock['updatetime'] = $date;
  1132. // $st_up = Db::name("good_stock")->save($stock);
  1133. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1134. //
  1135. //
  1136. // $bnin = GoodStockInfo::AddBn($ve['stock_id'], $ve['bnCode'], $tempnum, $ve['origin_price']);
  1137. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1138. //
  1139. // $ve['updatetime'] = $date;
  1140. // $up = Db::name("sale_info")->save($ve);
  1141. // if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1142. //
  1143. // $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1144. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1145. //
  1146. // }
  1147. // } else {
  1148. //
  1149. // $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo["orderCode"]])->findOrEmpty();
  1150. // if ($ordernum == false) throw new Exception('未找到关联采购单');
  1151. //
  1152. // $cgd = Db::name("purchease_order")
  1153. // ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  1154. // ->findOrEmpty();
  1155. // if ($cgd == false) throw new Exception('未找到采购单数据');
  1156. //
  1157. // $bn = makeNo("BN");
  1158. // $stock = Db::name("good_stock")
  1159. // ->where(["spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code'], "is_del" => 0, "status" => 1])
  1160. // ->findOrEmpty();
  1161. // if ($stock == false) throw new Exception('商品库存数据未找到');
  1162. //
  1163. // $stock['usable_stock'] += $info['num'];
  1164. // $stock['wait_out_stock'] -= $info['num'];
  1165. // $stock['updatetime'] = $date;
  1166. // $st_up = Db::name("good_stock")->save($stock);
  1167. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1168. //
  1169. // $yp = GoodStockInfo::AddBn($stock['id'], $bn, $info['num'], $cgd['good_price']);
  1170. // if ($yp == false) throw new Exception('商品批次退货入库失败');
  1171. //
  1172. // }
  1173. // }
  1174. // }
  1175. $data = [
  1176. "orderCode" => $info['orderCode'],
  1177. "th_type" => 1,
  1178. "th_num" => $info['num'],
  1179. "th_fee" => round($info['num'] * $orderinfo['sale_price'], 2),
  1180. "thCode" => $info['returnCode'],
  1181. "spuCode" => $orderinfo['good_code'],
  1182. "good_name" => $orderinfo['good_name'],
  1183. "cat_id" => $orderinfo['cat_id'],
  1184. "apply_id" => $info['apply_id'],
  1185. "apply_name" => $info['apply_name'],
  1186. "addtime" => $date,
  1187. "status" => 1,
  1188. "is_del" => 0
  1189. ];
  1190. $inse = Db::name("th_data")->insert($data);
  1191. if ($inse == false) throw new Exception('退货单更新失败');
  1192. }
  1193. }
  1194. Db::commit();
  1195. return app_show(0, "更新成功");
  1196. } catch (Exception $e) {
  1197. Db::rollback();
  1198. return error_show(1004, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1199. }
  1200. }
  1201. public function zxcreate(){
  1202. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  1203. if($ordeCode==''){
  1204. return error_show(1004,"参数orderCode 不能为空");
  1205. }
  1206. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  1207. if(empty($order)){
  1208. return error_show(1005,"未找到订单数据");
  1209. }
  1210. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1211. if($errorCode==''){
  1212. return error_show(1004,"参数errorCode 不能为空");
  1213. }
  1214. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1215. if(empty($error)){
  1216. return error_show(1005,"未找到退货原因数据");
  1217. }
  1218. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1219. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  1220. // if($token==''){
  1221. // return error_show(105,"参数token不能为空");
  1222. // }
  1223. // $user =GetUserInfo($token);
  1224. // if(empty($user)||$user['code']!=0){
  1225. // return error_show(1002,"申请人数据不存在");
  1226. // }
  1227. $rm= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  1228. $ri= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  1229. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  1230. if($num==''){
  1231. return error_show(1005,"参数num不能为空");
  1232. }
  1233. if($order['wsend_num']<$num){
  1234. return error_show(1002,"仓库未发货数量不足退货");
  1235. }
  1236. if($order['order_type']==3 ||$order['order_type']==4) $goon = Db::name("good_zixun")->field('supplierNo')->where(["spuCode"=>$order['good_code'],"is_del"=>0])->findOrEmpty();
  1237. else $goon = Db::name('good_basic')
  1238. ->where(['spuCode' => $order['good_code']])->findOrEmpty();
  1239. if(empty($goon)) return json_show(1004,'该商品不存在');
  1240. $userCommon = \app\admin\common\User::getIns();
  1241. $names = $userCommon->handle('getCodeAndName',['code'=>[$order['supplierNo'],$order['customer_code'],$goon['supplierNo']]]);
  1242. $returnCode=makeNo("RS");
  1243. Db::startTrans();
  1244. try{
  1245. $in = [
  1246. "returnCode"=>$returnCode,
  1247. "orderCode"=>$ordeCode,
  1248. "good_code"=>$order['good_code'],
  1249. "good_name"=>$order['good_name'],
  1250. "apply_id"=>$rm,
  1251. "apply_name"=>$ri,
  1252. "error_code"=>$errorCode,
  1253. "num"=>$num,
  1254. "remark"=>$remark,
  1255. "order_type"=>2,
  1256. "status"=>0,
  1257. "is_del"=>0,
  1258. "addtime"=>date("Y-m-d H:i:s"),
  1259. "updatetime"=>date("Y-m-d H:i:s"),
  1260. 'supplierNo' => $goon['supplierNo'],
  1261. 'supplierName' => $names['data'][$goon['supplierNo']] ?? '',
  1262. 'companyNo' => $order['supplierNo'],
  1263. 'companyName' => $names['data'][$order['supplierNo']] ?? '',
  1264. 'customer_code' => $order['customer_code'],
  1265. 'customer_name' => $names['data'][$order['customer_code']] ?? '',
  1266. ];
  1267. $create = Db::name("sale_return")->insert($in,true);
  1268. if($create>0) {
  1269. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1270. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1271. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1272. "action_type"=>"create"];
  1273. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",3,$in);
  1274. //维护台账记录
  1275. // Db::name('standing_book')
  1276. // ->where('ordeCode', $ordeCode)
  1277. // ->update([
  1278. // 'returnGoodCode' => $returnCode,
  1279. // 'updatetime' => date("Y-m-d H:i:s")
  1280. // ]);
  1281. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1282. if ($order['send_type'] == 1) {
  1283. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1284. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1285. if(!empty($wsend)){
  1286. foreach ($wsend as $value){
  1287. $tt = $value['status'];
  1288. $value['status']=0;
  1289. $value['updatetime']=date("Y-m-d H:i:s");
  1290. $up =Db::name("order_out")->save($value);
  1291. if($up){
  1292. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1293. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1294. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1295. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"CKD",0,$value);
  1296. }else{
  1297. Db::rollback();
  1298. return error_show(1005,"退货单新建失败");
  1299. }
  1300. }
  1301. }
  1302. }
  1303. Db::commit();
  1304. return error_show(0,"退货单新建成功");
  1305. }
  1306. Db::rollback();
  1307. return error_show(1005,"退货单新建失败");
  1308. }catch (\Exception $e){
  1309. Db::rollback();
  1310. return error_show(1005,$e->getMessage());
  1311. }
  1312. }
  1313. //全部退货
  1314. public function allReturn(){
  1315. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1316. if($orderCode==''){
  1317. return error_show(1004,"参数orderCode 不能为空");
  1318. }
  1319. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1320. if(empty($order)){
  1321. return error_show(1005,"未找到订单数据");
  1322. }
  1323. if($order['wsend_num']!=$order['good_num']){
  1324. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1325. }
  1326. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status", "in", [1,2,3,7,9,10]]])->count();
  1327. if($retrun>0){
  1328. return error_show(1005,"存在退货订单数据");
  1329. }
  1330. if($order['order_type']==3 ||$order['order_type']==4){
  1331. $goon = Db::name("good_zixun")
  1332. ->where(["spuCode"=>$order['good_code'],"is_del"=>0])
  1333. ->findOrEmpty();
  1334. }else {
  1335. $goon = Db::name('good_platform')
  1336. ->alias('a')
  1337. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  1338. ->where(['a.skuCode' => $order['skuCode']])
  1339. ->field("b.creater,b.createrid,b.supplierNo")
  1340. ->findOrEmpty();
  1341. }
  1342. if(empty($goon)){
  1343. return error_show(1005,"未找到商品数据");
  1344. }
  1345. // $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1346. // if($supplier==false){
  1347. // return error_show(1005,"未找到商品供应商数据");
  1348. // }
  1349. $userCommon = \app\admin\common\User::getIns();
  1350. $tmp = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  1351. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  1352. $supplier = $tmp['data'];
  1353. if (empty($supplier)) return json_show(1005, "未找到商品供应商数据");
  1354. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1355. if($errorCode==''){
  1356. return error_show(1004,"参数errorCode 不能为空");
  1357. }
  1358. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1359. if(empty($error)){
  1360. return error_show(1005,"未找到退货原因数据");
  1361. }
  1362. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1363. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1364. if($order['is_stock']==0){
  1365. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1366. if ($ordernum == false) {
  1367. return error_show(1005, '未找到关联采购单');
  1368. }
  1369. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1370. if ($cgd == false) {
  1371. return error_show(1005, '未找到采购单数据');
  1372. }
  1373. }
  1374. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1375. return error_show(1005, '采购单已发起入库');
  1376. }
  1377. $userCommon = \app\admin\common\User::getIns();
  1378. $names = $userCommon->handle('getCodeAndName',['code'=>[$order['supplierNo'],$order['customer_code']]]);
  1379. $returnCode=makeNo("RN");
  1380. Db::startTrans();
  1381. try{
  1382. $in = [
  1383. "returnCode"=>$returnCode,
  1384. "orderCode"=>$orderCode,
  1385. "good_code"=>$order['good_code'],
  1386. "good_name"=>$order['good_name'],
  1387. "apply_id"=>$this->uid,
  1388. "apply_name"=>$this->uname,
  1389. "cgderid"=>$goon['createrid'],
  1390. "cgder"=>$goon['creater'],
  1391. "person"=>$supplier['person']??'',
  1392. "person_id"=>$supplier['personid']??0,
  1393. "error_code"=>$errorCode,
  1394. "num"=>$order['wsend_num'],
  1395. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1396. "good_price"=>$order['sale_price'],
  1397. "platform_id"=>$order['platform_id'],
  1398. "remark"=>$remark,
  1399. "order_type"=>$order['order_type'],
  1400. "is_addr"=>count($addr)>0 ?1:0,
  1401. "status"=>$order['is_stock']==1?4:1,
  1402. "is_del"=>0,
  1403. "is_all"=>1,
  1404. "addtime"=>date("Y-m-d H:i:s"),
  1405. "updatetime"=>date("Y-m-d H:i:s"),
  1406. 'supplierNo' => $supplier['code'],
  1407. 'supplierName' => $supplier['name'],
  1408. 'companyNo' => $order['supplierNo'],
  1409. 'companyName' => $names['data'][$order['supplierNo']] ?? '',
  1410. 'customer_code' => $order['customer_code'],
  1411. 'customer_name' => $names['data'][$order['customer_code']] ?? '',
  1412. ];
  1413. $create = Db::name("sale_return")->insert($in,true);
  1414. if($create>0){
  1415. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1416. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1417. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1418. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1419. //维护台账信息
  1420. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1421. if(count($addr)!=0){
  1422. $inf=[];
  1423. foreach ($addr as $val){
  1424. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1425. $temp=[];
  1426. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1427. if ($send == false) {
  1428. Db::rollback();
  1429. return error_show(1004, "地址发货单未找到");
  1430. }
  1431. if($send['status']>=2){
  1432. Db::rollback();
  1433. return error_show(1005,"地址已发货");
  1434. }
  1435. if($order['is_stock']==1){
  1436. $val['is_del'] = 1;
  1437. $val['updatetime'] = date("Y-m-d H:i:s");
  1438. $addrup = Db::name("order_addr")->save($val);
  1439. if ($addrup == false) {
  1440. Db::rollback();
  1441. return error_show(1004, "地址更新失败");
  1442. }
  1443. $send['is_del'] = 1;
  1444. $send['remark'] ='全退';
  1445. $send['updatetime'] = date("Y-m-d H:i:s");
  1446. $outup = Db::name("order_out")->save($send);
  1447. if ($outup == false) {
  1448. Db::rollback();
  1449. return error_show(1004, "地址发货单更新失败");
  1450. }
  1451. //处理发货申请单流程
  1452. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1453. "order_type" => 'CKD',
  1454. "order_code" => $send["outCode"],//出库单号
  1455. "order_id" => $send["id"],
  1456. "order_status" => 4,//全部退货
  1457. "before_status" => $send['status'],
  1458. 'holder_id=' => $send['apply_id']
  1459. ]);
  1460. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1461. // if($ordersend==false){
  1462. // Db::rollback();
  1463. // return error_show(1004, "发货单关联数据未找到");
  1464. // }
  1465. // $ordersend['status'] = 0;
  1466. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1467. // $sendip = Db::name("order_send")->save($ordersend);
  1468. // if ($sendip == false) {
  1469. // Db::rollback();
  1470. // return error_show(1004, "发货单更新失败");
  1471. // }
  1472. }
  1473. $temp['returnCode']=$returnCode;
  1474. $temp['orderCode']=$orderCode;
  1475. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1476. $temp['addrid']=$val['id'];
  1477. $temp['send_num']=$val['receipt_quantity'];
  1478. $temp['return_num']=$val['receipt_quantity'];
  1479. $temp['is_del']=0;
  1480. $temp['addtime']=date("Y-m-d H:i:s");
  1481. $temp['updatetime']=date("Y-m-d H:i:s");
  1482. $inf[]=$temp;
  1483. }
  1484. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1485. if($inadd==0){
  1486. Db::rollback();
  1487. return error_show(1005,"退货单新建失败");
  1488. }
  1489. }
  1490. if($order['is_stock']==1){
  1491. $lor=$order['status'];
  1492. $thnum = $order['wsend_num'];
  1493. $order['send_num'] += $thnum;
  1494. $order['th_num'] += $thnum;
  1495. $order['wsend_num'] =0;
  1496. $order['status'] = 3;
  1497. $order['send_status']=3;
  1498. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1499. $order['updatetime'] = date("Y-m-d H:i:s");
  1500. $uap = Db::name("sale")->save($order);
  1501. if ($uap == false) {
  1502. Db::rollback();
  1503. return error_show(1005, '销售单订单更新失败');
  1504. }
  1505. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1506. "order_code" => $order["orderCode"],//出库单号
  1507. "status" => $lor,//这里的status是之前的值
  1508. "action_remark" => '',//备注
  1509. "action_type" => "status"//新建create,编辑edit,更改状态status
  1510. ], "XSQRD", $order['status'], $order);
  1511. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1512. "order_type" => 'XSQRD',
  1513. "order_code" =>$order["orderCode"],//出库单号
  1514. "order_id" => $order["id"],
  1515. "order_status" =>$order['status'],
  1516. "before_status"=>$lor,
  1517. 'holder_id=' => $order['apply_id']
  1518. ]);
  1519. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1520. if(empty($saleinfo)) {
  1521. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1522. if ($ordernum == false) {
  1523. Db::rollback();
  1524. return error_show(1005, '未找到关联采购单');
  1525. }
  1526. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1527. if ($cgd == false) {
  1528. Db::rollback();
  1529. return error_show(1005, '未找到采购单数据');
  1530. }
  1531. $bn =makeNo("BN");
  1532. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code'=>$cgd['wsm_code'],"is_del"=>0,"status"=>1])->find();
  1533. if($stock==false){
  1534. Db::rollback();
  1535. return error_show(1005, '商品库存数据未找到');
  1536. }
  1537. $stock['usable_stock']+=$thnum;
  1538. $stock['wait_out_stock'] -=$thnum;
  1539. $stock['updatetime'] = date("Y-m-d H:i:s");
  1540. $st_up = Db::name("good_stock")->save($stock);
  1541. if ($st_up == false) {
  1542. return error_show(1005, '可售商品入库失败');
  1543. }
  1544. $yp=GoodStockInfo::AddBn($stock['id'],$bn,$thnum,$cgd['good_price']);
  1545. if($yp==false){
  1546. Db::rollback();
  1547. return error_show(1005, '商品批次退货入库失败');
  1548. }
  1549. }else{
  1550. $tempnum =$thnum;
  1551. foreach ($saleinfo as $va){
  1552. if($tempnum ==0) break;
  1553. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1554. if($stock==false) {
  1555. Db::rollback();
  1556. return error_show(1005, '商品库存数据未找到');
  1557. }
  1558. if($va['num']>=$tempnum) {
  1559. $tnm = $tempnum;
  1560. $va['num']-= $tempnum;
  1561. $va['th_num']+= $tempnum;
  1562. $tempnum=0;
  1563. }else{
  1564. $tnm = $va['num'];
  1565. $tempnum -=$va['num'];
  1566. $va['th_num']+= $va['num'];
  1567. $va['num'] =0;
  1568. }
  1569. $stock['usable_stock']+=$tnm;
  1570. $stock['wait_out_stock'] -=$tnm;
  1571. $stock['updatetime'] = date("Y-m-d H:i:s");
  1572. $st_up = Db::name("good_stock")->save($stock);
  1573. if ($st_up == false) {
  1574. return error_show(1005, '可售商品入库失败');
  1575. }
  1576. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1577. if ($ps == false) {
  1578. return error_show(1005, '商品批次退货入库失败');
  1579. }
  1580. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1581. if ($ret == false) {
  1582. return error_show(1005, '商品批次退货入库失败');
  1583. }
  1584. $va['updatetime']=date("Y-m-d H:i:s");
  1585. $sal= Db::name("sale_info")->save($va);
  1586. if ($sal == false) {
  1587. return error_show(1005, '商品批次退货入库失败');
  1588. }
  1589. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $tnm, "stock_name" => "usable_stock"];
  1590. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $tnm, "stock_name" => "wait_out_stock"];
  1591. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'XSTHD');
  1592. }
  1593. }
  1594. $data=[
  1595. "orderCode"=>$orderCode,
  1596. "th_type"=>1,
  1597. "th_num"=>$thnum,
  1598. "th_fee"=>round($order['sale_price']*$thnum,2),
  1599. "thCode"=>$returnCode,
  1600. "spuCode"=>$order['good_code'],
  1601. "good_name"=>$order['good_name'],
  1602. "cat_id"=>$order['cat_id'],
  1603. "apply_id"=>$this->uid,
  1604. "apply_name"=>$this->uname,
  1605. "addtime"=>date("Y-m-d H:i:s"),
  1606. "status"=>1,
  1607. "is_del"=>0
  1608. ];
  1609. $inse=Db::name("th_data")->insert($data);
  1610. if($inse==false){
  1611. Db::rollback();
  1612. return error_show(1004,"退货单更新失败");
  1613. }
  1614. }
  1615. Db::commit();
  1616. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1617. }
  1618. Db::rollback();
  1619. return error_show(1005,"退货单新建失败");
  1620. }catch (\Exception $e){
  1621. Db::rollback();
  1622. return error_show(1005, $e->getMessage());
  1623. }
  1624. }
  1625. //导出
  1626. public function export()
  1627. {
  1628. $param = $this->request->only([
  1629. 'returnCode'=>'',
  1630. 'status'=>'',
  1631. 'orderCode'=>'',
  1632. 'apply_name'=>'',
  1633. 'start'=>'',
  1634. 'end'=>'',
  1635. 'good_code'=>'',
  1636. 'skuCode'=>'',
  1637. 'relaComNo'=>'',
  1638. 'order_source' => '',
  1639. 'supplierNo' => '',
  1640. 'customer_code' => '',
  1641. 'companyNo' => '',
  1642. 'company_name' => '',//部门名称
  1643. ],'post','trim');
  1644. $where = [['sr.is_del', "=", 0]];
  1645. if (!empty($param['returnCode'])) $where[] = ['sr.returnCode', "in", $param['returnCode']];
  1646. if ($param['status'] !== "") $where[] = ['sr.status', "=", $param['status']];
  1647. if ($param['orderCode'] != "") $where[] = ['sr.orderCode', "like", "%{$param['orderCode']}%"];
  1648. if ($param['apply_name'] != "") $where[] = ['sr.apply_name', "like", "%{$param['apply_name']}%"];
  1649. if ($param['start'] !== "") $where[] = ['sr.addtime', ">=", $param['start'] . ' 00:00:00'];
  1650. if ($param['end'] !== "") $where[] = ['sr.addtime', "<=", $param['end'] . ' 23:59:59'];
  1651. if ($param['good_code'] != "") $where[] = ['sr.good_code', "like", "%{$param['good_code']}%"]; //商品成本编码搜索
  1652. if ($param['skuCode'] != "") $where[] = ['b.skuCode', "like", "%{$param['skuCode']}%"];//商品上线编码搜索
  1653. if ($param['relaComNo'] != "") $where[] = ['a.supplierNo', '=', $param['relaComNo']];
  1654. if ($param['company_name'] !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($param['company_name'])];
  1655. if ($param['order_source'] !== "") $where[] = ["b.order_source", '=', $param['order_source']];
  1656. if ($param['supplierNo'] != "") $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  1657. if ($param['customer_code'] != "") $where[] = ['a.customer_code', "like", '%' . $param['customer_code'] . '%'];//商品上线编码搜索
  1658. if ($param['companyNo'] != "") $where[] = ['a.companyNo', "like", '%' . $param['companyNo'] . '%'];//商品上线编码搜索
  1659. $condition = [];
  1660. //只有level2的账号过滤数据权限
  1661. if ($this->level == 2) {
  1662. $role = $this->checkDataShare();
  1663. $hand = resign_hand_user($this->uid, 0);
  1664. if (!empty($role[DataGroupModel::$type_全部])) {
  1665. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  1666. $condition[] = ["sr.apply_id", "in", $arr];
  1667. $condition[] = ["sr.cgderid", "in", $hand];
  1668. $condition[] = ["sr.person_id", "in", $hand];
  1669. }
  1670. }
  1671. //供应商账号不允许看到库存品数据
  1672. if ($this->level == 3) $where[] = ['b.is_stock', '<>', 1];
  1673. if (in_array($this->roleid, config('app.wsm_cgder_role'))) $where[] = ["b.order_type", "=", 1];
  1674. $list = Db::name("sale_return")
  1675. ->alias('sr')
  1676. ->field('sr.returnCode 退货单编号,sr.orderCode 销售订单编号,sr.apply_id,sr.apply_name 申请人,"" 申请人部门,sr.cgder 采购员,sr.person 供应商负责人,c.result 错误原因,sr.good_code 商品成本编码,b.skuCode 上线商品编码,sr.good_name 商品名称,sr.good_price 单价,sr.num 退货数量,sr.total_fee 退货金额,d.platform_name 平台,sr.remark 备注,sr.order_type 退货单类型,sr.status 状态,sr.is_all 是否全退货,sr.is_th 供应商是否同意退货,sr.is_addr,e.addr_code,e.addr 发货地址,sr.return_wsm 退货仓库,sr.loop_total 循环次数')
  1677. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  1678. ->leftJoin("result_info c", "c.result_code=sr.error_code")
  1679. ->leftJoin("platform d", "d.id=sr.platform_id")
  1680. ->leftJoin("order_addr e", "e.id=sr.is_addr")
  1681. ->where($where)
  1682. ->where(function ($query) use ($condition) {
  1683. $query->whereOr($condition);
  1684. })
  1685. ->order("sr.addtime desc")
  1686. ->select()
  1687. ->toArray();
  1688. $all_createrid = array_column($list, 'apply_id');
  1689. $item = get_company_name_by_uid($all_createrid);
  1690. $order_type = [1 => '直接下单', 2 => '咨询', 3 => '项目', 4 => '平台', 5 => '有赞', 6 => '售后补换货', 7 => '报备转单', 8 => '支付渠道'];
  1691. $bool = [0 => '否', 1 => '是'];
  1692. $status = [1 => '待业务审批', 4 => '退货完成', 5 => '业务驳回', 9 => '待供应商审核', 8 => '供应商已驳回待采购审核', 10 => '业务公司修改待供应商确认'];
  1693. foreach ($list as &$value) {
  1694. // $value['退货金额'] = round($value['sale_price'] * $value['退货数量'],2);
  1695. $value['申请人部门'] = $item[$value['apply_id']] ?? '';
  1696. $value['是否全退货'] = $bool[$value['是否全退货']]??'';
  1697. $value['供应商是否同意退货'] = $bool[$value['供应商是否同意退货']]??'';
  1698. $value['退货单类型'] = $order_type[$value['退货单类型']]??'';
  1699. $value['状态'] = $status[$value['状态']]??'';
  1700. if($value['is_addr']==0) $value['发货地址']='无地址';
  1701. else $value['发货地址']=GetAddr($value['addr_code']).$value['发货地址'];
  1702. unset($value['apply_id']);
  1703. unset($value['good_price']);
  1704. unset($value['addr_code']);
  1705. unset($value['is_addr']);
  1706. }
  1707. if (empty($list)) $list[] = ['没有相关可导出的数据'];
  1708. excelSave('退货单' . date('YmdHis'), array_keys($list[0]), $list);
  1709. }
  1710. }