Reorder.php 94 KB

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