Reorder.php 95 KB

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