Reorder.php 93 KB

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