Reorder.php 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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 = new \app\admin\common\User();
  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' => $names['data'][$order['supplierNo']] ?? '',
  113. 'customer_code' => $order['customer_code'],
  114. 'customer_name' => $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 = new \app\admin\common\User();
  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 = new \app\admin\common\User();
  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. if (!isset($userCommon)) $userCommon = new \app\admin\common\User();
  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. if(!isset($userCommon)) $userCommon=new \app\admin\common\User();
  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. */
  715. public function exam()
  716. {
  717. $param = $this->request->only(['returnCode', 'status', 'remark' => '', 'return_wsm' => ''], 'post', 'trim');
  718. $val = Validate::rule([
  719. 'returnCode|退货单编号' => 'require',
  720. 'status|状态' => 'require|in:5,9,8,10,4',
  721. 'remark|备注' => 'requireIf:status,5|requireIf:status,8|requireIf:status,10',
  722. ]);
  723. if ($val->check($param) == false) return json_show(1004, $val->getError());
  724. $code = $param['returnCode'];
  725. $status = $param['status'];
  726. $remark = $param['remark'];
  727. // $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']):"";
  728. // if($code==""){
  729. // return error_show(1004,"参数returnCode不能为空");
  730. // }
  731. $info = Db::name("sale_return")->where(["returnCode" => $code, "is_del" => 0])->findOrEmpty();
  732. if (empty($info)) return error_show(1004, "未找到退货数据");
  733. switch ($info['status']) {
  734. case 1:
  735. if (in_array($param['status'], [5, 9]) == false) return json_show(1004, '选项错误');
  736. break;
  737. case 9:
  738. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  739. break;
  740. case 8:
  741. if (in_array($param['status'], [4, 10]) == false) return json_show(1004, '选项错误');
  742. if ($param['status'] == 4) {
  743. if ($param['return_wsm'] == '') return json_show(1004, '仓库编码不能为空');
  744. $tmp = Db::name("warehouse_info")
  745. ->field('id')
  746. ->where(['wsm_code' => $param['return_wsm']])
  747. ->findOrEmpty();
  748. if (empty($tmp)) return json_show(1004, '未找到仓库数据');
  749. else $info['return_wsm'] = $param['return_wsm'];
  750. }
  751. break;
  752. case 10:
  753. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  754. if ($param['status'] == 8) $info['loop_total'] += 1;
  755. break;
  756. }
  757. //当处于以下节点时,level2账号必须是供应商负责人操作,level3账号不做限制
  758. if(in_array($info['status'],[9,10]) && ($this->level==2) && ($this->uid!=$info['person_id'])) return json_show(1004,'您不是供应商负责人,此时无权操作');
  759. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  760. // if($status===""){
  761. // return error_show(1004,"参数status不能为空");
  762. // }
  763. // $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  764. $var = $info['status'];
  765. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->findOrEmpty();
  766. if (empty($orderinfo)) return error_show(1004, "未找到订单数据");
  767. if ($status == 4) {
  768. if ($info['is_addr'] == 1) {
  769. $addr = Db::name("sale_returnaddr")
  770. ->where(['returnCode' => $info['returnCode'], "is_del" => 0])
  771. ->select()
  772. ->toArray();
  773. if (empty($addr)) return error_show(1004, "未找到发货单地址数据");
  774. }
  775. }
  776. // if($status==3){
  777. // $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  778. // if($is_th===""){
  779. // return error_show(1004,"参数is_th不能为空");
  780. // }
  781. // $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  782. // if($is_th==0){
  783. // if($return_wsm===""){
  784. // return error_show(1004,"参数return_wsm 不能为空");
  785. // }
  786. // $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  787. // if($wsmcode==false){
  788. // return error_show(1004,"为找到仓库数据");
  789. // }
  790. // }
  791. // $info['return_wsm'] =$return_wsm ;
  792. // $info['is_th'] =$is_th ;
  793. // }
  794. if ($info['status'] == 9 && $param['status'] == 4) $info['is_th'] = 1;
  795. Db::startTrans();
  796. try {
  797. $date = date("Y-m-d H:i:s");
  798. $userCommon = new \app\admin\common\User();
  799. $temp = $info['status'];
  800. $info['status'] = $status;
  801. $remark != "" ? $info['remark'] = $remark : "";
  802. $info["updatetime"] = $date;
  803. $up = Db::name("sale_return")->save($info);
  804. if ($up) {
  805. //如果是节点2(待专员审核),要将待办数据推给供应商负责人
  806. 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']];
  807. else $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'holder_id' => $info['apply_id']];
  808. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  809. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  810. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $ste, "XSTHD", $status, $info);
  811. if ($status == 4) {
  812. if ($info['is_addr'] == 1) {
  813. if (isset($addr) && !empty($addr)) {
  814. foreach ($addr as $value) {
  815. $addrinfo = Db::name("order_addr")
  816. ->where(['id' => $value['addrid'], "is_del" => 0])
  817. ->find();
  818. if ($addrinfo == false) {
  819. Db::rollback();
  820. return error_show(1004, "地址数据未找到");
  821. }
  822. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  823. Db::rollback();
  824. return error_show(1004, "地址发货数量不足");
  825. }
  826. $addrinfo['receipt_quantity'] -= $value['return_num'];
  827. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  828. $addrinfo['updatetime'] = $date;
  829. $addrup = Db::name("order_addr")->save($addrinfo);
  830. if ($addrup == false) throw new Exception('地址发货数量更新失败');
  831. if ($value['outCode'] != "") {
  832. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->findOrEmpty();
  833. if (empty($out)) throw new Exception('地址发货单数据未找到');
  834. if ($out['status'] >= 2) throw new Exception('地址发货单已发货');
  835. if ($out['send_num'] < $value['return_num']) throw new Exception('地址发货单发货数量不足');
  836. $out['send_num'] -= $value['return_num'];
  837. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  838. $out['updatetime'] = $date;
  839. $outup = Db::name("order_out")->save($out);
  840. if ($outup == false) throw new Exception('地址发货单更新失败');
  841. }
  842. }
  843. }
  844. }
  845. if ($orderinfo['wsend_num'] < $info['num']) throw new Exception('销售单未发货数量不足退货');
  846. $lor = $orderinfo['status'];
  847. $orderinfo['wsend_num'] -= $info['num'];
  848. $orderinfo['send_num'] += $info['num'];
  849. $orderinfo['status'] = $orderinfo['wsend_num'] == 0 ? 2 : ($orderinfo['send_num'] == 0 ? 0 : 1);
  850. $orderinfo['send_status'] = $orderinfo['wsend_num'] == 0 ? 3 : ($orderinfo['send_num'] == 0 ? 1 : 2);
  851. if ($orderinfo['is_stock'] == 1) {
  852. $orderinfo['th_num'] += $info['num'];
  853. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  854. $orderinfo['status'] = 3;
  855. }
  856. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  857. $orderinfo['updatetime'] = $date;
  858. $uap = Db::name("sale")->save($orderinfo);
  859. if ($uap == false) throw new Exception('销售单订单更新失败');
  860. } else {
  861. if ($info['is_th'] == 1) {
  862. $orderinfo['th_num'] += $info['num'];
  863. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  864. $orderinfo['status'] = 3;
  865. }
  866. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  867. $orderinfo['updatetime'] = $date;
  868. $uap = Db::name("sale")->save($orderinfo);
  869. if ($uap == false) throw new Exception('销售单订单更新失败');
  870. $ordernum = Db::name("order_num")
  871. ->where(['orderCode' => $orderinfo['orderCode']])
  872. ->findOrEmpty();
  873. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  874. $ordernum['send_num'] -= $info['num'];
  875. $orderup = Db::name("order_num")->save($ordernum);
  876. if ($orderup == false) throw new Exception('关联数据更新失败');
  877. $cgd = Db::name("purchease_order")
  878. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  879. ->findOrEmpty();
  880. if ($cgd == false) throw new Exception('未找到采购单数据');
  881. if ($info['is_all'] == 1 && $cgd['send_status'] != 1) throw new Exception('采购单已入库无法全部退货');
  882. $lor = $cgd['status'];
  883. if ($cgd['wsend_num'] < $info['num']) {
  884. $cgd['send_num'] += $cgd['wsend_num'] == 0 ? 0 : ($info['num'] - $cgd['wsend_num']);
  885. $cgd['wsend_num'] = 0;
  886. } else {
  887. $cgd['wsend_num'] -= $info['num'];
  888. $cgd['send_num'] += $info['num'];
  889. }
  890. $cgd['status'] = $cgd['wsend_num'] == 0 ? 2 : ($cgd['send_num'] == 0 ? 0 : 1);
  891. $cgd['send_status'] = $cgd['wsend_num'] == 0 ? 3 : ($cgd['send_num'] == 0 ? 1 : 2);
  892. $cgd['th_num'] += $info['num'];
  893. if ($cgd['th_num'] == $cgd['send_num'] && $cgd['wsend_num'] == 0) {
  894. $cgd['status'] = 4;
  895. }
  896. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  897. $cgd['updatetime'] = $date;
  898. $cgdup = Db::name("purchease_order")->save($cgd);
  899. if ($cgdup == false) throw new Exception('采购单数据更新失败');
  900. $stock = Db::name("good_stock")
  901. ->where(['is_del' => 0, "spuCode" => $info['good_code'], 'wsm_code' => $cgd['wsm_code']])
  902. ->findOrEmpty();
  903. if (empty($stock)) throw new Exception('商品仓库未找到');
  904. if ($stock['wait_in_stock'] < $info['num']) {
  905. $stock['wait_in_stock'] = 0;
  906. if ($stock['usable_stock'] > $info['num'] - $stock['wait_in_stock']) {
  907. $stock['usable_stock'] -= $info['num'] - $stock['wait_in_stock'];
  908. } else {
  909. $stock['usable_stock'] = 0;
  910. $stock['wait_out_stock'] -= $info['num'] - $stock['wait_in_stock'] - $stock['usable_stock'];
  911. }
  912. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  913. } else {
  914. $stock['wait_in_stock'] -= $info['num'];
  915. }
  916. $stock['updatetime'] = $date;
  917. $st_up = Db::name("good_stock")->save($stock);
  918. if ($st_up == false) throw new Exception('可售商品入库失败');
  919. } else {
  920. $orderinfo['th_num'] += $info['num'];
  921. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  922. $orderinfo['status'] = 3;
  923. }
  924. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  925. $orderinfo['updatetime'] = $date;
  926. $uap = Db::name("sale")->save($orderinfo);
  927. if ($uap == false) throw new Exception('销售单订单更新失败');
  928. $ordernum = Db::name("order_num")
  929. ->where(['orderCode' => $orderinfo['orderCode']])
  930. ->findOrEmpty();
  931. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  932. $ordernum['send_num'] -= $info['num'];
  933. $ordernum['wsend_num'] = $info['num'];
  934. $orderup = Db::name("order_num")->save($ordernum);
  935. if ($orderup == false) throw new Exception('关联数据更新失败');
  936. $cgd = Db::name("purchease_order")
  937. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  938. ->findOrEmpty();
  939. if (empty($cgd)) throw new Exception('未找到采购单数据');
  940. $stock = Db::name("good_stock")
  941. ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code']])
  942. ->findOrEmpty();
  943. if (empty($stock)) throw new Exception('商品仓库未找到');
  944. if ($info['is_all'] == 1) {
  945. if ($stock['wait_in_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  946. else $stock['wait_in_stock'] -= $info['num'];
  947. } else {
  948. if ($stock['usable_stock'] + $stock['wait_out_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  949. else {
  950. if ($stock['usable_stock'] > $info['num']) {
  951. $stock['usable_stock'] -= $info['num'];
  952. } else {
  953. $stock['usable_stock'] = 0;
  954. $stock['wait_out_stock'] -= $info['num'] - $stock['usable_stock'];
  955. }
  956. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  957. }
  958. }
  959. $stock['updatetime'] = $date;
  960. $st_up = Db::name("good_stock")->save($stock);
  961. if ($st_up == false) throw new Exception('可售商品入库失败');
  962. }
  963. }
  964. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  965. "order_code" => $orderinfo["orderCode"],//出库单号
  966. "status" => $lor,//这里的status是之前的值
  967. "action_remark" => '',//备注
  968. "action_type" => "status"//新建create,编辑edit,更改状态status
  969. ], "XSQRD", $orderinfo['status'], $orderinfo);
  970. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  971. "order_type" => 'XSQRD',
  972. "order_code" => $orderinfo["orderCode"],//出库单号
  973. "order_id" => $orderinfo["id"],
  974. "order_status" => $orderinfo['status'], "before_status" => $lor
  975. ]);
  976. //对应采购单也要处理
  977. // $holder_id = Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0);
  978. $holder = $userCommon->handle('sInfo', ['code' => $cgd['supplierNo']]);
  979. if (!isset($holder['code']) || $holder['code'] != 0) throw new Exception($holder['message']);
  980. $holder_id = $holder['data']['personid'];
  981. if (in_array($cgd['status'], [4])) {
  982. if ($orderinfo['is_stock'] == 1) {
  983. //库存品,推给31库管人员、41库管-张凯旋
  984. $uid = Db::name('user_role')
  985. ->where([
  986. ['is_del', '=', 0],
  987. ['roleid', 'in', [31, 41]],
  988. ['status', '=', 1]
  989. ])->column('uid');
  990. // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  991. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  992. "order_type" => 'CGD',
  993. "order_code" => $cgd['cgdNo'],
  994. "order_id" => $cgd["id"],
  995. "order_status" => $cgd['status'],
  996. "before_status" => $lor,
  997. 'holder_id' => $holder_id,
  998. 'handle_user_list' => implode(',', $uid),
  999. ]);
  1000. } else {
  1001. // if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1002. //非库存品和采返商品,推给供应商负责人
  1003. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  1004. "order_type" => 'CGD',
  1005. "order_code" => $cgd['cgdNo'],
  1006. "order_id" => $cgd["id"],
  1007. "order_status" => $cgd['status'],
  1008. "before_status" => $lor,
  1009. 'holder_id' => $holder_id,
  1010. 'handle_user_list' => $orderinfo['cgderid'],
  1011. ]);
  1012. }
  1013. }
  1014. // ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  1015. // "order_type" => 'CGD',
  1016. // "order_code" => $cgd['cgdNo'],
  1017. // "order_id" => $cgd["id"],
  1018. // "order_status" => $cgd['status'],
  1019. // "before_status" => $lor,
  1020. // 'holder_id' => $holder_id
  1021. // ]);
  1022. //以下判断是当供应商不同意要退回到业务公司的时候触发的,在3.0系统中不需要了2023-02-09@by武
  1023. // if ($orderinfo['is_stock'] == 1 || $info['is_th'] == 0) {
  1024. //
  1025. // if ($orderinfo['order_type'] != 1) {
  1026. // if ($orderinfo['order_type'] == 3) {
  1027. // $goon = Db::name("good_zixun")
  1028. // ->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])
  1029. // ->findOrEmpty();
  1030. // $isZx = 1;
  1031. // } else {
  1032. // $goon = Db::name('good_basic')
  1033. // ->where(['spuCode' => $orderinfo['good_code']])
  1034. // ->findOrEmpty();
  1035. // $isZx = 2;
  1036. // }
  1037. // $spuCode = $this->CheckGoodZx($goon, $isZx, $code);
  1038. // $wsmcode = $info['return_wsm'];
  1039. // if ($wsmcode == "") throw new Exception('未找到退货仓库');
  1040. //
  1041. // $stock = Db::name("good_stock")
  1042. // ->where(['is_del' => 0, "spuCode" => $spuCode, 'wsm_code' => $wsmcode])
  1043. // ->findOrEmpty();
  1044. // if (empty($stock)) {
  1045. // $stock = [
  1046. // "spuCode" => $spuCode,
  1047. // "wsm_code" => $wsmcode,
  1048. // "usable_stock" => 0,
  1049. // "wait_out_stock" => 0,
  1050. // "wait_in_stock" => 0,
  1051. // "total_stock" => 0,
  1052. // "addtime" => $date,
  1053. // "updatetime" => $date,
  1054. // ];
  1055. // }
  1056. // $stock['usable_stock'] += $info['num'];
  1057. // $stock['updatetime'] = $date;
  1058. // $st_up = Db::name("good_stock")->save($stock);
  1059. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1060. //
  1061. // $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  1062. // $sabebn = Db::name("sale_info")
  1063. // ->where(["orderCode" => $orderinfo["orderCode"]])
  1064. // ->select()
  1065. // ->toArray();
  1066. // if (!empty($sabebn)) {
  1067. // $total_num = $info['num'];
  1068. // foreach ($sabebn as $ve) {
  1069. // $tempnum = 0;
  1070. // if ($total_num == 0) break;
  1071. // if ($total_num >= $ve['num']) {
  1072. // $tempnum = $ve['num'];
  1073. // $total_num -= $ve['num'];
  1074. // $ve['th_num'] += $ve['num'];
  1075. // $ve['num'] = 0;
  1076. // } else {
  1077. // $tempnum = $total_num;
  1078. // $ve['num'] -= $total_num;
  1079. // $ve['th_num'] += $total_num;
  1080. // $total_num = 0;
  1081. // }
  1082. // $bnin = GoodStockInfo::AddBn($stockid, $ve['bnCode'], $tempnum, $ve['origin_price']);
  1083. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1084. //
  1085. // $ve['updatetime'] = $date;
  1086. // $up = Db::name("sale_info")->save($ve);
  1087. // if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1088. //
  1089. // $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1090. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1091. //
  1092. // }
  1093. // } else {
  1094. // $bn = makeNo("BN");
  1095. // $bnin = GoodStockInfo::AddBn($stockid, $bn, $info['num'], $cgd['good_price'] ?? 0);
  1096. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1097. //
  1098. // }
  1099. // $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  1100. // GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'XSTHD');
  1101. // } else {
  1102. // $sabebn = Db::name("sale_info")
  1103. // ->where(["orderCode" => $orderinfo["orderCode"]])
  1104. // ->select()
  1105. // ->toArray();
  1106. // if (!empty($sabebn)) {
  1107. // $total_num = $info['num'];
  1108. // foreach ($sabebn as $ve) {
  1109. // $stock = Db::name("good_stock")
  1110. // ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'id' => $ve['stockid']])
  1111. // ->findOrEmpty();
  1112. // if ($stock == false) throw new Exception('商品库存数据未找到');
  1113. //
  1114. // $tempnum = 0;
  1115. // if ($total_num == 0) break;
  1116. // if ($total_num >= $ve['num']) {
  1117. // $tempnum = $ve['num'];
  1118. // $total_num -= $ve['num'];
  1119. // $ve['th_num'] += $ve['num'];
  1120. // $ve['num'] = 0;
  1121. // } else {
  1122. // $tempnum = $total_num;
  1123. // $ve['num'] -= $total_num;
  1124. // $ve['th_num'] += $total_num;
  1125. // $total_num = 0;
  1126. // }
  1127. // $stock['usable_stock'] += $tempnum;
  1128. // $stock['wait_out_stock'] -= $tempnum;
  1129. // $stock['updatetime'] = $date;
  1130. // $st_up = Db::name("good_stock")->save($stock);
  1131. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1132. //
  1133. //
  1134. // $bnin = GoodStockInfo::AddBn($ve['stock_id'], $ve['bnCode'], $tempnum, $ve['origin_price']);
  1135. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1136. //
  1137. // $ve['updatetime'] = $date;
  1138. // $up = Db::name("sale_info")->save($ve);
  1139. // if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1140. //
  1141. // $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1142. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1143. //
  1144. // }
  1145. // } else {
  1146. //
  1147. // $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo["orderCode"]])->findOrEmpty();
  1148. // if ($ordernum == false) throw new Exception('未找到关联采购单');
  1149. //
  1150. // $cgd = Db::name("purchease_order")
  1151. // ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  1152. // ->findOrEmpty();
  1153. // if ($cgd == false) throw new Exception('未找到采购单数据');
  1154. //
  1155. // $bn = makeNo("BN");
  1156. // $stock = Db::name("good_stock")
  1157. // ->where(["spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code'], "is_del" => 0, "status" => 1])
  1158. // ->findOrEmpty();
  1159. // if ($stock == false) throw new Exception('商品库存数据未找到');
  1160. //
  1161. // $stock['usable_stock'] += $info['num'];
  1162. // $stock['wait_out_stock'] -= $info['num'];
  1163. // $stock['updatetime'] = $date;
  1164. // $st_up = Db::name("good_stock")->save($stock);
  1165. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1166. //
  1167. // $yp = GoodStockInfo::AddBn($stock['id'], $bn, $info['num'], $cgd['good_price']);
  1168. // if ($yp == false) throw new Exception('商品批次退货入库失败');
  1169. //
  1170. // }
  1171. // }
  1172. // }
  1173. $data = [
  1174. "orderCode" => $info['orderCode'],
  1175. "th_type" => 1,
  1176. "th_num" => $info['num'],
  1177. "th_fee" => round($info['num'] * $orderinfo['sale_price'], 2),
  1178. "thCode" => $info['returnCode'],
  1179. "spuCode" => $orderinfo['good_code'],
  1180. "good_name" => $orderinfo['good_name'],
  1181. "cat_id" => $orderinfo['cat_id'],
  1182. "apply_id" => $info['apply_id'],
  1183. "apply_name" => $info['apply_name'],
  1184. "addtime" => $date,
  1185. "status" => 1,
  1186. "is_del" => 0
  1187. ];
  1188. $inse = Db::name("th_data")->insert($data);
  1189. if ($inse == false) throw new Exception('退货单更新失败');
  1190. }
  1191. }
  1192. Db::commit();
  1193. return app_show(0, "更新成功");
  1194. } catch (Exception $e) {
  1195. Db::rollback();
  1196. return error_show(1004, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1197. }
  1198. }
  1199. public function zxcreate(){
  1200. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  1201. if($ordeCode==''){
  1202. return error_show(1004,"参数orderCode 不能为空");
  1203. }
  1204. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  1205. if(empty($order)){
  1206. return error_show(1005,"未找到订单数据");
  1207. }
  1208. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1209. if($errorCode==''){
  1210. return error_show(1004,"参数errorCode 不能为空");
  1211. }
  1212. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1213. if(empty($error)){
  1214. return error_show(1005,"未找到退货原因数据");
  1215. }
  1216. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1217. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  1218. // if($token==''){
  1219. // return error_show(105,"参数token不能为空");
  1220. // }
  1221. // $user =GetUserInfo($token);
  1222. // if(empty($user)||$user['code']!=0){
  1223. // return error_show(1002,"申请人数据不存在");
  1224. // }
  1225. $rm= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  1226. $ri= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  1227. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  1228. if($num==''){
  1229. return error_show(1005,"参数num不能为空");
  1230. }
  1231. if($order['wsend_num']<$num){
  1232. return error_show(1002,"仓库未发货数量不足退货");
  1233. }
  1234. 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();
  1235. else $goon = Db::name('good_basic')
  1236. ->where(['spuCode' => $order['good_code']])->findOrEmpty();
  1237. if(empty($goon)) return json_show(1004,'该商品不存在');
  1238. $userCommon = new \app\admin\common\User();
  1239. $names = $userCommon->handle('getCodeAndName',['code'=>[$order['supplierNo'],$order['customer_code'],$goon['supplierNo']]]);
  1240. $returnCode=makeNo("RS");
  1241. Db::startTrans();
  1242. try{
  1243. $in = [
  1244. "returnCode"=>$returnCode,
  1245. "orderCode"=>$ordeCode,
  1246. "good_code"=>$order['good_code'],
  1247. "good_name"=>$order['good_name'],
  1248. "apply_id"=>$rm,
  1249. "apply_name"=>$ri,
  1250. "error_code"=>$errorCode,
  1251. "num"=>$num,
  1252. "remark"=>$remark,
  1253. "order_type"=>2,
  1254. "status"=>0,
  1255. "is_del"=>0,
  1256. "addtime"=>date("Y-m-d H:i:s"),
  1257. "updatetime"=>date("Y-m-d H:i:s"),
  1258. 'supplierNo' => $goon['supplierNo'],
  1259. 'supplierName' => $names['data'][$goon['supplierNo']] ?? '',
  1260. 'companyNo' => $order['supplierNo'],
  1261. 'companyName' => $names['data'][$order['supplierNo']] ?? '',
  1262. 'customer_code' => $order['customer_code'],
  1263. 'customer_name' => $names['data'][$order['customer_code']] ?? '',
  1264. ];
  1265. $create = Db::name("sale_return")->insert($in,true);
  1266. if($create>0) {
  1267. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1268. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1269. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1270. "action_type"=>"create"];
  1271. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",3,$in);
  1272. //维护台账记录
  1273. // Db::name('standing_book')
  1274. // ->where('ordeCode', $ordeCode)
  1275. // ->update([
  1276. // 'returnGoodCode' => $returnCode,
  1277. // 'updatetime' => date("Y-m-d H:i:s")
  1278. // ]);
  1279. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1280. if ($order['send_type'] == 1) {
  1281. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1282. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1283. if(!empty($wsend)){
  1284. foreach ($wsend as $value){
  1285. $tt = $value['status'];
  1286. $value['status']=0;
  1287. $value['updatetime']=date("Y-m-d H:i:s");
  1288. $up =Db::name("order_out")->save($value);
  1289. if($up){
  1290. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1291. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1292. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1293. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"CKD",0,$value);
  1294. }else{
  1295. Db::rollback();
  1296. return error_show(1005,"退货单新建失败");
  1297. }
  1298. }
  1299. }
  1300. }
  1301. Db::commit();
  1302. return error_show(0,"退货单新建成功");
  1303. }
  1304. Db::rollback();
  1305. return error_show(1005,"退货单新建失败");
  1306. }catch (\Exception $e){
  1307. Db::rollback();
  1308. return error_show(1005,$e->getMessage());
  1309. }
  1310. }
  1311. //全部退货
  1312. public function allReturn(){
  1313. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1314. if($orderCode==''){
  1315. return error_show(1004,"参数orderCode 不能为空");
  1316. }
  1317. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1318. if(empty($order)){
  1319. return error_show(1005,"未找到订单数据");
  1320. }
  1321. if($order['wsend_num']!=$order['good_num']){
  1322. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1323. }
  1324. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status", "in", [1,2,3,7,9,10]]])->count();
  1325. if($retrun>0){
  1326. return error_show(1005,"存在退货订单数据");
  1327. }
  1328. if($order['order_type']==3 ||$order['order_type']==4){
  1329. $goon = Db::name("good_zixun")
  1330. ->where(["spuCode"=>$order['good_code'],"is_del"=>0])
  1331. ->findOrEmpty();
  1332. }else {
  1333. $goon = Db::name('good_platform')
  1334. ->alias('a')
  1335. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  1336. ->where(['a.skuCode' => $order['skuCode']])
  1337. ->field("b.creater,b.createrid,b.supplierNo")
  1338. ->findOrEmpty();
  1339. }
  1340. if(empty($goon)){
  1341. return error_show(1005,"未找到商品数据");
  1342. }
  1343. // $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1344. // if($supplier==false){
  1345. // return error_show(1005,"未找到商品供应商数据");
  1346. // }
  1347. $userCommon = new \app\admin\common\User();
  1348. $tmp = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  1349. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  1350. $supplier = $tmp['data'];
  1351. if (empty($supplier)) return json_show(1005, "未找到商品供应商数据");
  1352. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1353. if($errorCode==''){
  1354. return error_show(1004,"参数errorCode 不能为空");
  1355. }
  1356. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1357. if(empty($error)){
  1358. return error_show(1005,"未找到退货原因数据");
  1359. }
  1360. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1361. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1362. if($order['is_stock']==0){
  1363. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1364. if ($ordernum == false) {
  1365. return error_show(1005, '未找到关联采购单');
  1366. }
  1367. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1368. if ($cgd == false) {
  1369. return error_show(1005, '未找到采购单数据');
  1370. }
  1371. }
  1372. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1373. return error_show(1005, '采购单已发起入库');
  1374. }
  1375. $userCommon = new \app\admin\common\User();
  1376. $names = $userCommon->handle('getCodeAndName',['code'=>[$order['supplierNo'],$order['customer_code']]]);
  1377. $returnCode=makeNo("RN");
  1378. Db::startTrans();
  1379. try{
  1380. $in = [
  1381. "returnCode"=>$returnCode,
  1382. "orderCode"=>$orderCode,
  1383. "good_code"=>$order['good_code'],
  1384. "good_name"=>$order['good_name'],
  1385. "apply_id"=>$this->uid,
  1386. "apply_name"=>$this->uname,
  1387. "cgderid"=>$goon['createrid'],
  1388. "cgder"=>$goon['creater'],
  1389. "person"=>$supplier['person']??'',
  1390. "person_id"=>$supplier['personid']??0,
  1391. "error_code"=>$errorCode,
  1392. "num"=>$order['wsend_num'],
  1393. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1394. "good_price"=>$order['sale_price'],
  1395. "platform_id"=>$order['platform_id'],
  1396. "remark"=>$remark,
  1397. "order_type"=>$order['order_type'],
  1398. "is_addr"=>count($addr)>0 ?1:0,
  1399. "status"=>$order['is_stock']==1?4:1,
  1400. "is_del"=>0,
  1401. "is_all"=>1,
  1402. "addtime"=>date("Y-m-d H:i:s"),
  1403. "updatetime"=>date("Y-m-d H:i:s"),
  1404. 'supplierNo' => $supplier['code'],
  1405. 'supplierName' => $supplier['name'],
  1406. 'companyNo' => $order['supplierNo'],
  1407. 'companyName' => $names['data'][$order['supplierNo']] ?? '',
  1408. 'customer_code' => $order['customer_code'],
  1409. 'customer_name' => $names['data'][$order['customer_code']] ?? '',
  1410. ];
  1411. $create = Db::name("sale_return")->insert($in,true);
  1412. if($create>0){
  1413. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1414. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1415. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1416. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1417. //维护台账信息
  1418. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1419. if(count($addr)!=0){
  1420. $inf=[];
  1421. foreach ($addr as $val){
  1422. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1423. $temp=[];
  1424. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1425. if ($send == false) {
  1426. Db::rollback();
  1427. return error_show(1004, "地址发货单未找到");
  1428. }
  1429. if($send['status']>=2){
  1430. Db::rollback();
  1431. return error_show(1005,"地址已发货");
  1432. }
  1433. if($order['is_stock']==1){
  1434. $val['is_del'] = 1;
  1435. $val['updatetime'] = date("Y-m-d H:i:s");
  1436. $addrup = Db::name("order_addr")->save($val);
  1437. if ($addrup == false) {
  1438. Db::rollback();
  1439. return error_show(1004, "地址更新失败");
  1440. }
  1441. $send['is_del'] = 1;
  1442. $send['remark'] ='全退';
  1443. $send['updatetime'] = date("Y-m-d H:i:s");
  1444. $outup = Db::name("order_out")->save($send);
  1445. if ($outup == false) {
  1446. Db::rollback();
  1447. return error_show(1004, "地址发货单更新失败");
  1448. }
  1449. //处理发货申请单流程
  1450. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1451. "order_type" => 'CKD',
  1452. "order_code" => $send["outCode"],//出库单号
  1453. "order_id" => $send["id"],
  1454. "order_status" => 4,//全部退货
  1455. "before_status" => $send['status'],
  1456. 'holder_id=' => $send['apply_id']
  1457. ]);
  1458. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1459. // if($ordersend==false){
  1460. // Db::rollback();
  1461. // return error_show(1004, "发货单关联数据未找到");
  1462. // }
  1463. // $ordersend['status'] = 0;
  1464. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1465. // $sendip = Db::name("order_send")->save($ordersend);
  1466. // if ($sendip == false) {
  1467. // Db::rollback();
  1468. // return error_show(1004, "发货单更新失败");
  1469. // }
  1470. }
  1471. $temp['returnCode']=$returnCode;
  1472. $temp['orderCode']=$orderCode;
  1473. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1474. $temp['addrid']=$val['id'];
  1475. $temp['send_num']=$val['receipt_quantity'];
  1476. $temp['return_num']=$val['receipt_quantity'];
  1477. $temp['is_del']=0;
  1478. $temp['addtime']=date("Y-m-d H:i:s");
  1479. $temp['updatetime']=date("Y-m-d H:i:s");
  1480. $inf[]=$temp;
  1481. }
  1482. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1483. if($inadd==0){
  1484. Db::rollback();
  1485. return error_show(1005,"退货单新建失败");
  1486. }
  1487. }
  1488. if($order['is_stock']==1){
  1489. $lor=$order['status'];
  1490. $thnum = $order['wsend_num'];
  1491. $order['send_num'] += $thnum;
  1492. $order['th_num'] += $thnum;
  1493. $order['wsend_num'] =0;
  1494. $order['status'] = 3;
  1495. $order['send_status']=3;
  1496. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1497. $order['updatetime'] = date("Y-m-d H:i:s");
  1498. $uap = Db::name("sale")->save($order);
  1499. if ($uap == false) {
  1500. Db::rollback();
  1501. return error_show(1005, '销售单订单更新失败');
  1502. }
  1503. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1504. "order_code" => $order["orderCode"],//出库单号
  1505. "status" => $lor,//这里的status是之前的值
  1506. "action_remark" => '',//备注
  1507. "action_type" => "status"//新建create,编辑edit,更改状态status
  1508. ], "XSQRD", $order['status'], $order);
  1509. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1510. "order_type" => 'XSQRD',
  1511. "order_code" =>$order["orderCode"],//出库单号
  1512. "order_id" => $order["id"],
  1513. "order_status" =>$order['status'],
  1514. "before_status"=>$lor,
  1515. 'holder_id=' => $order['apply_id']
  1516. ]);
  1517. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1518. if(empty($saleinfo)) {
  1519. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1520. if ($ordernum == false) {
  1521. Db::rollback();
  1522. return error_show(1005, '未找到关联采购单');
  1523. }
  1524. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1525. if ($cgd == false) {
  1526. Db::rollback();
  1527. return error_show(1005, '未找到采购单数据');
  1528. }
  1529. $bn =makeNo("BN");
  1530. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code'=>$cgd['wsm_code'],"is_del"=>0,"status"=>1])->find();
  1531. if($stock==false){
  1532. Db::rollback();
  1533. return error_show(1005, '商品库存数据未找到');
  1534. }
  1535. $stock['usable_stock']+=$thnum;
  1536. $stock['wait_out_stock'] -=$thnum;
  1537. $stock['updatetime'] = date("Y-m-d H:i:s");
  1538. $st_up = Db::name("good_stock")->save($stock);
  1539. if ($st_up == false) {
  1540. return error_show(1005, '可售商品入库失败');
  1541. }
  1542. $yp=GoodStockInfo::AddBn($stock['id'],$bn,$thnum,$cgd['good_price']);
  1543. if($yp==false){
  1544. Db::rollback();
  1545. return error_show(1005, '商品批次退货入库失败');
  1546. }
  1547. }else{
  1548. $tempnum =$thnum;
  1549. foreach ($saleinfo as $va){
  1550. if($tempnum ==0) break;
  1551. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1552. if($stock==false) {
  1553. Db::rollback();
  1554. return error_show(1005, '商品库存数据未找到');
  1555. }
  1556. if($va['num']>=$tempnum) {
  1557. $tnm = $tempnum;
  1558. $va['num']-= $tempnum;
  1559. $va['th_num']+= $tempnum;
  1560. $tempnum=0;
  1561. }else{
  1562. $tnm = $va['num'];
  1563. $tempnum -=$va['num'];
  1564. $va['th_num']+= $va['num'];
  1565. $va['num'] =0;
  1566. }
  1567. $stock['usable_stock']+=$tnm;
  1568. $stock['wait_out_stock'] -=$tnm;
  1569. $stock['updatetime'] = date("Y-m-d H:i:s");
  1570. $st_up = Db::name("good_stock")->save($stock);
  1571. if ($st_up == false) {
  1572. return error_show(1005, '可售商品入库失败');
  1573. }
  1574. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1575. if ($ps == false) {
  1576. return error_show(1005, '商品批次退货入库失败');
  1577. }
  1578. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1579. if ($ret == false) {
  1580. return error_show(1005, '商品批次退货入库失败');
  1581. }
  1582. $va['updatetime']=date("Y-m-d H:i:s");
  1583. $sal= Db::name("sale_info")->save($va);
  1584. if ($sal == false) {
  1585. return error_show(1005, '商品批次退货入库失败');
  1586. }
  1587. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $tnm, "stock_name" => "usable_stock"];
  1588. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $tnm, "stock_name" => "wait_out_stock"];
  1589. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'XSTHD');
  1590. }
  1591. }
  1592. $data=[
  1593. "orderCode"=>$orderCode,
  1594. "th_type"=>1,
  1595. "th_num"=>$thnum,
  1596. "th_fee"=>round($order['sale_price']*$thnum,2),
  1597. "thCode"=>$returnCode,
  1598. "spuCode"=>$order['good_code'],
  1599. "good_name"=>$order['good_name'],
  1600. "cat_id"=>$order['cat_id'],
  1601. "apply_id"=>$this->uid,
  1602. "apply_name"=>$this->uname,
  1603. "addtime"=>date("Y-m-d H:i:s"),
  1604. "status"=>1,
  1605. "is_del"=>0
  1606. ];
  1607. $inse=Db::name("th_data")->insert($data);
  1608. if($inse==false){
  1609. Db::rollback();
  1610. return error_show(1004,"退货单更新失败");
  1611. }
  1612. }
  1613. Db::commit();
  1614. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1615. }
  1616. Db::rollback();
  1617. return error_show(1005,"退货单新建失败");
  1618. }catch (\Exception $e){
  1619. Db::rollback();
  1620. return error_show(1005, $e->getMessage());
  1621. }
  1622. }
  1623. //导出
  1624. public function export()
  1625. {
  1626. $param = $this->request->only([
  1627. 'returnCode'=>'',
  1628. 'status'=>'',
  1629. 'orderCode'=>'',
  1630. 'apply_name'=>'',
  1631. 'start'=>'',
  1632. 'end'=>'',
  1633. 'good_code'=>'',
  1634. 'skuCode'=>'',
  1635. 'relaComNo'=>'',
  1636. 'order_source' => '',
  1637. 'supplierNo' => '',
  1638. 'customer_code' => '',
  1639. 'companyNo' => '',
  1640. 'company_name' => '',//部门名称
  1641. ],'post','trim');
  1642. $where = [['sr.is_del', "=", 0]];
  1643. if (!empty($param['returnCode'])) $where[] = ['sr.returnCode', "in", $param['returnCode']];
  1644. if ($param['status'] !== "") $where[] = ['sr.status', "=", $param['status']];
  1645. if ($param['orderCode'] != "") $where[] = ['sr.orderCode', "like", "%{$param['orderCode']}%"];
  1646. if ($param['apply_name'] != "") $where[] = ['sr.apply_name', "like", "%{$param['apply_name']}%"];
  1647. if ($param['start'] !== "") $where[] = ['sr.addtime', ">=", $param['start'] . ' 00:00:00'];
  1648. if ($param['end'] !== "") $where[] = ['sr.addtime', "<=", $param['end'] . ' 23:59:59'];
  1649. if ($param['good_code'] != "") $where[] = ['sr.good_code', "like", "%{$param['good_code']}%"]; //商品成本编码搜索
  1650. if ($param['skuCode'] != "") $where[] = ['b.skuCode', "like", "%{$param['skuCode']}%"];//商品上线编码搜索
  1651. if ($param['relaComNo'] != "") $where[] = ['a.supplierNo', '=', $param['relaComNo']];
  1652. if ($param['company_name'] !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($param['company_name'])];
  1653. if ($param['order_source'] !== "") $where[] = ["b.order_source", '=', $param['order_source']];
  1654. if ($param['supplierNo'] != "") $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  1655. if ($param['customer_code'] != "") $where[] = ['a.customer_code', "like", '%' . $param['customer_code'] . '%'];//商品上线编码搜索
  1656. if ($param['companyNo'] != "") $where[] = ['a.companyNo', "like", '%' . $param['companyNo'] . '%'];//商品上线编码搜索
  1657. $condition = [];
  1658. //只有level2的账号过滤数据权限
  1659. if ($this->level == 2) {
  1660. $role = $this->checkDataShare();
  1661. $hand = resign_hand_user($this->uid, 0);
  1662. if (!empty($role[DataGroupModel::$type_全部])) {
  1663. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  1664. $condition[] = ["sr.apply_id", "in", $arr];
  1665. $condition[] = ["sr.cgderid", "in", $hand];
  1666. $condition[] = ["sr.person_id", "in", $hand];
  1667. }
  1668. }
  1669. //供应商账号不允许看到库存品数据
  1670. if ($this->level == 3) $where[] = ['b.is_stock', '<>', 1];
  1671. if (in_array($this->roleid, config('app.wsm_cgder_role'))) $where[] = ["b.order_type", "=", 1];
  1672. $list = Db::name("sale_return")
  1673. ->alias('sr')
  1674. ->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 循环次数')
  1675. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  1676. ->leftJoin("result_info c", "c.result_code=sr.error_code")
  1677. ->leftJoin("platform d", "d.id=sr.platform_id")
  1678. ->leftJoin("order_addr e", "e.id=sr.is_addr")
  1679. ->where($where)
  1680. ->where(function ($query) use ($condition) {
  1681. $query->whereOr($condition);
  1682. })
  1683. ->order("sr.addtime desc")
  1684. ->select()
  1685. ->toArray();
  1686. $all_createrid = array_column($list, 'apply_id');
  1687. $item = get_company_name_by_uid($all_createrid);
  1688. $order_type = [1 => '直接下单', 2 => '咨询', 3 => '项目', 4 => '平台', 5 => '有赞', 6 => '售后补换货', 7 => '报备转单', 8 => '支付渠道'];
  1689. $bool = [0 => '否', 1 => '是'];
  1690. $status = [1 => '待业务审批', 4 => '退货完成', 5 => '业务驳回', 9 => '待供应商审核', 8 => '供应商已驳回待采购审核', 10 => '业务公司修改待供应商确认'];
  1691. foreach ($list as &$value) {
  1692. // $value['退货金额'] = round($value['sale_price'] * $value['退货数量'],2);
  1693. $value['申请人部门'] = $item[$value['apply_id']] ?? '';
  1694. $value['是否全退货'] = $bool[$value['是否全退货']]??'';
  1695. $value['供应商是否同意退货'] = $bool[$value['供应商是否同意退货']]??'';
  1696. $value['退货单类型'] = $order_type[$value['退货单类型']]??'';
  1697. $value['状态'] = $status[$value['状态']]??'';
  1698. if($value['is_addr']==0) $value['发货地址']='无地址';
  1699. else $value['发货地址']=GetAddr($value['addr_code']).$value['发货地址'];
  1700. unset($value['apply_id']);
  1701. unset($value['good_price']);
  1702. unset($value['addr_code']);
  1703. unset($value['is_addr']);
  1704. }
  1705. if (empty($list)) $list[] = ['没有相关可导出的数据'];
  1706. excelSave('退货单' . date('YmdHis'), array_keys($list[0]), $list);
  1707. }
  1708. }