Reorder.php 95 KB

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