123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- <?php
- declare (strict_types = 1);
- namespace app\Admin\controller;
- use app\BaseController;
- use think\facade\Db;
- use think\Request;
- class Refund extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function list()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $condition = [['a.is_del',"=","0"]];
- $guserinfo =GetUserInfo($token);
- if(isset($guserinfo['code']) && $guserinfo['code']!=0){
- return error_show($guserinfo['code'],$guserinfo['message']);
- }
- $check = checkRole($guserinfo['data']['roleid'],52);
- if($check){
- $condition[]=["a.apply_id","=", $guserinfo['data']['id']];
- }
- $type=isset($post['type'])&&$post['type']!="" ?$post['type'] :1;
- $condition[]=["refund_type","=",$type];
- $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']) : "";
- if($companyNo!=''){
- $condition[]=['customerNo',"like","%{$companyNo}%"];
- }
- $relaNo = isset($post['relaNo']) && $post['relaNo'] !== "" ? trim($post['relaNo']) : "";
- if($relaNo!=''){
- $condition[]=['relaNo',"like","%{$relaNo}%"];
- }
- $start = isset($post['start']) && $post['start'] !== "" ? $post['start'] : "";
- if($start!=''){
- $condition[]=['addtime',">=",$start];
- }
- $end = isset($post['end']) && $post['end'] !== "" ? $post['end'] : "";
- if($end!=''){
- $condition[]=['addtime',"<=",$end];
- }
- $refund_status = isset($post['refund_status']) && $post['refund_status'] !== "" ? $post['refund_status'] : "";
- if($refund_status!=''){
- $condition[]=['a.refund_status',"=",$refund_status];
- }
- $inv_type = isset($post['inv_type']) && $post['inv_type'] !== "" ? $post['inv_type'] : "";
- if($inv_type!='' &&$type==2){
- $condition[]=['b.inv_type',"=",$inv_type];
- }
- $back_type = isset($post['back_type']) && $post['back_type'] !== "" ? $post['back_type'] : "";
- if($back_type!='' &&$type==2){
- $condition[]=['a.back_type',"=",$back_type];
- }
- $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
- $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
- $count = Db::name("refund")->alias("a")->leftJoin("invoice_pool b","a.relaNo=b.invNo")->where($condition)->count();
- // echo Db::name("refund")->getLastSql();
- $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
- $page = $page >= $total ? intval($total) : $page;
- $list= Db::name("refund")->alias("a")->leftJoin("invoice_pool b","a.relaNo=b.invNo")->field("a.*,ifnull(b.type_check,0) as inv_type")
- ->where($condition)->order("addtime desc")->page($page,$size)->select()->toArray();
- $data=[];
- foreach($list as $value){
- $temp=[];
- $temp['id'] = $value['id'];
- $temp['refundNo'] = $value['refundNo'];
- $temp['customerNo'] = $value['customerNo'];
- $customer = Db::name("customer_info")->where("companyNo","=",$value['customerNo'])->find();
- $temp['customer'] = isset($customer['companyName']) ?$customer['companyName']:"" ;
- if($value['refund_type']==2){
- $temp['invNo'] = $value['relaNo'];
- }else{
- $temp['traNo'] = $value['relaNo'];
- }
- $temp['refund_status'] = $value['refund_status'];
- $temp['back_type'] = $value['back_type'];
- $temp['inv_type'] = $value['inv_type'];
- $temp['apply_name'] = $value['apply_name'];
- $temp['refund_fee'] = $value['refund_fee'];
- $temp['back_fee'] = $value['back_fee'];
- $temp['addtime'] = $value['addtime'];
- $data[]=$temp;
- }
- return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function create()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $guserinfo =GetUserInfo($token);
- if(isset($guserinfo['code']) && $guserinfo['code']!=0){
- return error_show($guserinfo['code'],$guserinfo['message']);
- }
- $type=isset($post['type'])&&$post['type']!="" ?$post['type'] :1;
- $relaNo = isset($post['relaNo'])&&$post['relaNo']!="" ? trim($post['relaNo']) :"";
- if($relaNo==""){
- return error_show(1004,"参数relaNo 不能为空");
- }
- $back_type = isset($post['backType'])&&$post['backType']!="" ? $post['backType'] : 0;
- if($type==2){
- if($back_type==0){
- return error_show(1004,"参数backType 不能为空");
- }
- $inv= Db::name("invoice_pool")->where([["is_del","=",0],['invNo',"=",$relaNo]])->find();
- if(empty($inv)){
- return error_show(1004,"未找到数据");
- }
- if($inv['refund_status']!=0){
- return error_show(1004,"发票已在退票中");
- }
- $customer = $inv['inv_in'];
- $total_fee= $inv['inv_value'];
- }else{
- $trade =Db::name("trade_pool")->where([['is_del',"=",0],['traNo',"=",$relaNo]])->find();
- if(empty($trade)){
- return error_show(1004,"未找到数据");
- }
- if($trade['refund_status']!=0){
- return error_show(1004,"资金已在退款中");
- }
- $customer = $trade['companyNo'];
- $total_fee= $trade['total_fee'];
- }
- $data=[
- "refundNo"=>makeNo("TH"),
- "customerNo"=> $customer,
- "refund_type"=>$type,
- "relaNo"=>$relaNo,
- "refund_fee"=>$total_fee,
- "apply_id"=>$guserinfo['data']['id'],
- "apply_name"=>$guserinfo['data']['nickname'],
- "refund_status"=>1,
- "backNo"=>"",
- "back_type"=>$back_type,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- Db::startTrans();
- try{
- $assoc = Db::name("assoc_list")->where([["is_del","=",0],["codeNo|viceNo","=",$relaNo],["bstatus","=",1]])->select()->toArray();
- if(!empty($assoc)){
- foreach ($assoc as $value){
- if($value['status']!=3){
- Db::rollback();
- return error_show(1006,"存在未完成的流程");
- }
- if($type==1){
- if(in_array($value['rela_form'],[4,5])){
- Db::rollback();
- return error_show(1006,"存在关联的发票,请先走退票流程");
- }
- }
- if($value['rela_form']==2 || $value['rela_form']==1){
- $orderlist = Db::name("order_info")->where([['orderNo',"=",$value['codeNo']],['status',"=",
- 1]])->select()->toArray();
- if(!empty($orderlist)){
- $qrd = array_column($orderlist,"qrdNo");
- $qrdcp = array_column($orderlist,"goodNo");
- Db::name("qrd")->where([['sequenceNo',"in",$qrd]])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
- Db::name("qrd_info")->where([['sequenceNo',"in",$qrdcp]])->save(["status"=>2]);
- }
- }
- if($value['rela_form']==3){
- $orderlist = Db::name("order_info")->where([['orderNo',"=",$value['viceNo']],['status',"=",
- 1]])->select()->toArray();
- if(!empty($orderlist)){
- $qrd = array_column($orderlist,"qrdNo");
- $qrdcp = array_column($orderlist,"goodNo");
- Db::name("qrd")->where([['sequenceNo',"in",$qrd]])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
- Db::name("qrd_info")->where([['sequenceNo',"in",$qrdcp]])->save(["status"=>2]);
- }
- }
- }
- }
- $return = Db::name("refund")->insert($data);
- if($return){
- if($type==1){
- $trade['refund_status']=1;
- $trade['updatetime']=date('Y-m-d H:i:s');
- $up = Db::name('trade_pool')->save($trade);
- }else{
- $inv['refund_status']=1;
- $inv['updatetime']=date('Y-m-d H:i:s');
- $up = Db::name('invoice_pool')->save($inv);
- }
- if($up){
- Db::commit();
- return app_show(0,"新建成功");
- }
- }
- Db::rollback();
- return error_show(1004,"新建失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- /**
- * 保存新建的资源
- *
- * @param \think\Request $request
- * @return \think\Response
- */
- public function ExamStatus()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $relaNo = isset($post['refundNo'])&&$post['refundNo']!=""?trim($post['refundNo']):"";
- if($relaNo==""){
- return error_show(1004,"参数refundNo 不能为空");
- }
- $refund = Db::name("refund")->where([['refundNo',"=",$relaNo],["is_del","=",0]])->find();
- if(empty($refund)){
- return error_show(1005,"未找到数据");
- }
- $status=isset($post['status'])&&$post['status']!=="" ? $post['status']:"";
- if($status===""){
- return error_show(1004,"参数status 不能为空");
- }
- $remark= isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- if($refund['refund_type']==2){
- if(!in_array($status,[1,2,3,4])){
- return error_show(1004,"参数status 无效");
- }
- if(($status==2&& $refund['refund_status']!=1) || ($status==3&& $refund['refund_status']!=2)|| ($status==4&&
- $refund['refund_status']!=3)){
- return error_show(1003,"数据状态有误");
- }
- } else{
- if(!in_array($status,[1,2,3,5,6])){
- return error_show(1004,"参数status 无效");
- }
- if(($status==2&& !in_array($refund['refund_status'],[1,5])) || ($status==3&& !in_array($refund['refund_status'],[2,6]))){
- return error_show(1003,"数据状态有误");
- }
- }
- Db::startTrans();
- try{
- $refund['refund_status']=$status;
- $refund['remark']=$remark;
- $refund['updatetime']=date("Y-m-d H:i:s");
- $update= Db::name("refund")->save($refund);
- if($update){
- if($refund['refund_type']==2&&$status==4){
- $invpol = Db::name("invoice_pool")->where("invNo","=",$refund['relaNo'])->save(["refund_status"=>3,"updatetime"=>date("Y-m-d H:i:s")]);
- if($invpol){
- $assoc= Db::name("assoc_list")->where([["is_del","=",0],["viceNo|codeNo","=",$refund['relaNo']],["bstatus","=",1]])->select()->toArray();
- if(!empty($assoc)){
- foreach ($assoc as $key=>$value){
- if($value['rela_form']==1){
- Db::name("order_pool")->where([["orderNo", "=", $value['codeNo']], ["is_del", "=", 0]])->save(['is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
- $order= $this->qrd_delet($value['codeNo'],$value['cancel_fee']);
- if(!$order){
- Db::rollback();
- return error_show(1005,"状态修改失败");
- }
- }
- if($value['rela_form']==4){
- $trade = Db::name("trade_pool")->where([['traNo',"=",$value['viceNo']],["is_del","=",0]])->find();
- if($trade){
- $trade['inv_fee'] -=$value['cancel_fee'];
- $trade['winv_fee'] +=$value['cancel_fee'];
- $trade['inv_status'] =$trade['winv_fee']==$trade['total_fee'] ? 1:2;
- $trade['updatetime'] =date("Y-m-d H:i:s");
- Db::name("trade_pool")->save($trade);
- }
- }
- if($value['rela_form']==5){
- $trade = Db::name("trade_pool")->where([['traNo',"=",$value['codeNo']],["is_del","=",0]])->find();
- if($trade){
- $trade['inv_fee'] -=$value['cancel_fee'];
- $trade['winv_fee'] +=$value['cancel_fee'];
- $trade['inv_status'] =$trade['winv_fee']==$trade['total_fee'] ? 1:2;
- $trade['updatetime'] =date("Y-m-d H:i:s");
- Db::name("trade_pool")->save($trade);
- }
- }
- Db::name("assoc_rela")->where([["assocNo","=",$value['assocNo']],["viceNo","=",
- $value['viceNo']],["status","=",1]])->save(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- }
- }
- Db::commit();
- return app_show(0,"发票退票审核通过");
- }
- Db::rollback();
- return error_show(1005,"状态修改失败");
- }
- if($refund['refund_type']==1&&$status==3){
- $tradepool = Db::name("trade_pool")->where("traNo","=",$refund['relaNo'])->save
- (["refund_status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
- if($tradepool){
- $assoc= Db::name("assoc_list")->where([["is_del","=",0],["viceNo|codeNo","=",$refund['relaNo']],["bstatus","=",1]])->select()->toArray();
- if(!empty($assoc)){
- foreach ($assoc as $key=>$value){
- Db::name("assoc_rela")->where([["assocNo","=",$value['assocNo']],["viceNo","=",
- $value['viceNo']],["status","=",1]])->save(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- }
- }
- Db::commit();
- return app_show(0,"资金退款审核通过");
- }
- Db::rollback();
- return error_show(1005,"状态修改失败");
- }
- Db::commit();
- return app_show(0,"审核通过");
- }
- Db::rollback();
- return error_show(1005,"状态修改失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * 显示指定的资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function read()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $relaNo = isset($post['refundNo'])&&$post['refundNo']!=""?trim($post['refundNo']):"";
- if($relaNo==""){
- return error_show(1004,"参数relaNo 不能为空");
- }
- $refund = Db::name("refund")->where([['refundNo',"=",$relaNo],["is_del","=",0]])->find();
- if(empty($refund)){
- return error_show(1005,"未找到数据");
- }
- $customer = Db::name('customer_info')->where("companyNo","=",$refund['customerNo'])->find();
- $refund['companyName']= isset($customer['companyName']) ? $customer['companyName']:"";
- $refund['invinfo']=[];
- $refund['tradeinfo']=[];
- if($refund['refund_type']==1){
- $tradeinfo = Db::name("trade_list")->where([['traNo',"=",$refund['relaNo']]])->find();
- if(empty($tradeinfo)){
- return error_show(1005,"未找到资金数据");
- }
- $customer = Db::name('customer_info')->where("companyNo","=",$tradeinfo['companyNo'])->find();
- $tradeinfo['companyName']= isset($customer['companyName']) ? $customer['companyName']:"";
- $refund['tradeinfo'] = $tradeinfo;
- }
- if($refund['refund_type']==2){
- $invinfo = Db::name("invoice_pool")->where([['invNo',"=",$refund['relaNo']]])->find();
- if(empty($invinfo)){
- return error_show(1005,"未找到资金数据");
- }
- $customer = Db::name('customer_info')->where("companyNo","=",$invinfo['inv_in'])->find();
- $invinfo['companyName']= isset($customer['companyName']) ? $customer['companyName']:"";
- $refund['invinfo'] = $invinfo;
- }
- return app_show(0,"获取成功",$refund);
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function isRefund()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $type=isset($post['type'])&&$post['type']!="" ?$post['type'] :1;
- $relaNo = isset($post['relaNo'])&&$post['relaNo']!="" ? trim($post['relaNo']) :"";
- if($relaNo==""){
- return error_show(1004,"参数relaNo 不能为空");
- }
- if($type==2){
- $inv= Db::name("invoice_pool")->where([["is_del","=",0],['invNo',"=",$relaNo]])->find();
- if(empty($inv)){
- return error_show(1004,"未找到数据");
- }
- }else{
- $trade =Db::name("trade_pool")->where([['is_del',"=",0],['traNo',"=",$relaNo]])->find();
- if(empty($trade)){
- return error_show(1004,"未找到数据");
- }
- }
- $assoc = Db::name("assoc_list")->where([["is_del","=",0],["codeNo|viceNo","=",$relaNo]])->select()->toArray();
- if(!empty($assoc)){
- foreach ($assoc as $value){
- if($value['status']!=3){
- return error_show(1006,"存在未完成的流程");
- }
- if($type==1){
- if(in_array($value['rela_form'],[4,5])){
- return error_show(1006,"存在关联的发票,请先走退票流程");
- }
- }
- }
- }
- return app_show(0,"验证成功");
- }
- /**
- * 保存更新的资源
- *
- * @param \think\Request $request
- * @param int $id
- * @return \think\Response
- */
- public function update(Request $request, $id)
- {
- //
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function delete()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $relaNo = isset($post['refundNo'])&&$post['refundNo']!=""?trim($post['refundNo']):"";
- if($relaNo==""){
- return error_show(1004,"参数relaNo 不能为空");
- }
- $refund = Db::name("refund")->where([['refundNo',"=",$relaNo],["is_del","=",0]])->find();
- if(empty($refund)){
- return error_show(1005,"未找到数据");
- }
- $remark= isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
- Db::rollback();
- try{
- $refund['is_del']=1;
- $refund['refund_status']=0;
- $refund['updatetime']=date("Y-m-d H:i:s");
- $del = Db::name("refund")->save($refund);
- if($del){
- if($refund['refund_type']==1){
- $trad = Db::name("trade_pool")->where([['traNo',"=",$refund['relaNo']],['is_del',"=",0]])->save(["refund_status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- if($trad){
- Db::commit();
- return app_show(0,"退款申请已取消");
- }
- };
- if($refund['refund_type']==2){
- $inv = Db::name("invoice_pool")->where([['invNo',"=",$refund['relaNo']],['is_del',"=",0]])->save(["refund_status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- if($inv){
- Db::commit();
- return app_show(0,"退票申请已取消");
- }
- }
- }
- Db::rollback();
- return error_show(1005,"申请取消失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- private function qrd_delet($orderNo,$balance){
- $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=",1]])->find();
- if (empty($orderinfo)) {
- return false;
- }
- $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
- if (empty($orderlist)) {
- return false;
- }
- foreach ($orderlist as $value) {
- if($balance==0){
- break;
- }
- if($value['ainv_fee'] == 0){
- continue;
- }
- $order_fee=0;
- if($value['ainv_fee']<=$balance){
- $order_fee = $value['ainv_fee'];
- $balance -=$value['ainv_fee'];
- $value['winv_fee'] += $value['ainv_fee'];
- $value['ainv_fee'] = 0;
- }else{
- $value['ainv_fee'] -= $balance;
- $value['winv_fee']+= $balance;
- $order_fee = $balance;
- $balance=0;
- }
- $value['updatetime'] = date("Y-m-d H:i:s");
- $orde = Db::name("order_info")->save($value);
- if(!$orde){
- return false;
- }
- $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,Number1618248813613,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
- if ($order_fee <= $qrdinfo['Number1618249202608']) {
- $qrdinfo['Number1618249202608'] -= $order_fee;
- $qrdinfo['Number1618249205231'] += $order_fee;
- $qrdinfo['number1618249258956'] -= $value['good_num'];
- $qrdinfo['status'] =0;
- $qrdinfo['ShortText1618559274859'] = $qrdinfo['Number1618248813613'] == $qrdinfo['Number1618249205231'] ? "1" : "2";
- $qinfo = Db::name("qrd_info")->save($qrdinfo);
- if ($qinfo) {
- $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
- if ($qrdp['ainv_fee'] >= $order_fee) {
- $qrdp['winv_fee'] += $order_fee;
- $qrdp['ainv_fee'] -= $order_fee;
- $qrdinfo['status'] =1;
- $qrdp['inv_status'] = $qrdp['winv_fee'] ==$qrdp['total_fee'] ?1 : 2;
- $qrd = Db::name("qrd")->save($qrdp);
- if(!$qrd){
- return false;
- }
- }else {
- return false;
- }
- }else {
- return false;
- }
- } else {
- return false;
- }
- }
- return true;
- }
- }
|