Reorder.php 95 KB

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