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 = [["is_del","=",0]]; $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']): ""; if ($status != "") { $condition[] = ["status", "=",$status]; } $name = isset($post['name']) && $post['name'] !== "" ? trim($post['name']): ""; if ($name != "") { $condition[] = ["trade_out", "like","%{$name}%"]; } $logNo = isset($post['logNo']) && $post['logNo'] !== "" ? trim($post['logNo']) : ""; if ($logNo != "") { $condition[] = ["logNo", "like","%{$logNo}%"]; } $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']) : ""; if ($companyNo != "") { $logall = Db::name("trade_pool")->where([["companyNo","=",$companyNo],['is_del',"=",0]])->column('logNo'); $condition[] = ["logNo", "in",$logall]; } $traNo = isset($post['traNo']) && $post['traNo'] !== "" ? trim($post['traNo']) : ""; if ($traNo != "") { $logall = Db::name("trade_pool")->where([["traNo","=",$traNo],['is_del',"=",0]])->column('logNo'); $condition[] = ["logNo", "in",$logall]; } $total_s = isset($post['total_low']) && $post['total_low'] !== "" ? trim($post['total_low']) : ""; if ($total_s != "") { $condition[] = ["total_fee", ">=",$total_s]; } $total_e = isset($post['total_up']) && $post['total_up'] !== "" ? trim($post['total_up']) : ""; if ($total_e != "") { $condition[] = ["total_fee", "<=",$total_e]; } $trade_type = isset($post['trade_type']) && $post['trade_type'] !== "" ? trim($post['trade_type']) : ""; if ($trade_type != "") { $condition[] = ["trade_type", "=",$trade_type]; } $end = isset($post["end"])&&$post['end']!="" ? $post['end'] : ""; $start = isset($post["start"])&&$post['start']!="" ? $post['start'] : ""; if($start!=""){ $condition[] = ["trade_time", ">=",$start]; } if($end!=""){ $condition[] = ["trade_time", "<=",$end]; } $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1; $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10; $count = Db::name("trade_log")->where($condition)->count(); $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1; $page = $page >= $total ? intval($total) : $page; $list = Db::name("trade_log")->where($condition)->field('*,used_fee+tra_fee as amount')->order("addtime desc") ->page(intval($page), $size) ->select(); $data=[]; foreach ($list as $value){ $value['trade'] =Db::name("trade_pool")->alias("a")->leftJoin("cfp_customer_info b","a.companyNo=b.companyNo")->where ([["logNo","=",$value['logNo']],['a.is_del',"=",0],['refund_status',"=",0]])->field("a.*,b.companyName")->select(); $data[]=$value; } 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']); } $files = $this->request->file("execl"); $data = upload_ll($files,$files->getOriginalExtension()); if($data['code']!=0){ return error_show(1004,$data['msg']); } $trade = $data['data']; $list = []; foreach ( $trade as $key => $value) { if (!isset($value[0]) || $value[0] == '') { //return ['code' => 1003, "msg" => '交易时间不能为空']; return error_show( 1003, '交易时间不能为空!'); } if ((!isset($value[1]) || $value[1] == '') && (!isset($value[2]) || $value[2] == '')) { //return ['code' => 1003, "msg" => '收入/支出金额不能为空']; return error_show( 1003, '收入/支出金额不能为空!'); } if (!isset($value[4]) || $value[4] == '') { // return ['code' => 1003, "msg" => '交易行名不能为空']; return error_show( 1003, '交易行名不能为空!'); } if (!isset($value[6]) || $value[6] == '') { // return ['code' => 1003, "msg" => '对方账户不能为空']; return error_show( 1003, '对方账户不能为空!'); } if (!isset($value[7]) || $value[7] == '') { // return ['code' => 1003, "msg" => '对方户名不能为空']; return error_show( 1003, '对方户名不能为空!'); } if ($key == 0) { if ($value[0] != '交易时间') { // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!']; return error_show( 1003, '模板第一列为必须为交易时间!'); } if ($value[1] != '收入金额') { // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!']; return error_show( 1003, '模板第二列为必须为收入金额!'); } if ($value[2] != '支出金额') { // return ['code' => 1003, "msg" => '模板第二列为必须为支出金额!']; return error_show( 1003, '模板第二列为必须为支出金额!'); } if ($value[4] != '交易行名') { //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!']; return error_show( 1003, '模板第五列为必须为交易行名!'); } if ($value[6] != '对方账号') { // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!']; return error_show( 1003, '模板第七列为必须为对方账号!'); } if ($value[7] != '对方户名') { // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!']; return error_show( 1003, '模板第八列为必须为对方户名!'); } continue; } $total_fee = $value[1]==0&&$value[2]!=0 ? $value[2]: $value[1]; $type = $value[1]==0&&$value[2]!=0 ? 1: 0; $time = explode(" ", $value[0]); $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00"); $list[$key]["trade_fee"] = str_replace(",","",$total_fee); $list[$key]["trade_bank"] = $value[4]; $list[$key]["trade_account"] = $value[6]; $list[$key]["trade_out"] = $value[7]; $list[$key]["trade_in"] = ''; $list[$key]["trade_type"] = $type; $list[$key]["trade_used"] = isset($value[8]) ? $value[8] : ""; $list[$key]["trade_remark"] = ''; } if(empty($list)){ return error_show( 1003, '导入数据不能为空!'); } Db::startTrans(); try{ $tra=[]; foreach ($list as $value) { $temp = []; $temp["logNo"] = makeNo('S'); $temp['trade_time'] = $value['tradeTime']; $temp['total_fee'] = $value['trade_fee']; $temp['trade_bank'] = $value['trade_bank']; $temp['trade_account'] = $value['trade_account']; $temp['trade_type'] =$value['trade_type']; $temp['trade_out'] = $value['trade_out']; $temp['trade_in'] = $value['trade_in']; $temp['trade_used'] = $value['trade_used']; $temp['trade_remark'] = $value['trade_remark']; $temp['balance'] =$value['trade_fee']; $temp['addtime'] = date("Y-m-d H:i:s"); $temp['updatetime'] = date("Y-m-d H:i:s"); $tra[]=$temp; } $list = Db::name('trade_log')->insertAll($tra); if($list==count($tra)){ Db::commit(); return app_show(0, "资金导入成功"); }else{ Db::rollback(); return app_show(1004, "资金导入失败"); } }catch (\Exception $e){ Db::rollback(); return app_show(1004, $e->getMessage()); } } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function save() { $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']); } $userinfo = GetUserInfo($token); $username=""; if(isset($userinfo['code'])&&$userinfo['code']==0){ $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: ""; } $data = isset($post['id']) && $post['id']!="" ? $post['id']:""; if($data==""){ return error_show(1004,"参数id 不能为空"); } $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:""; if($companyNo==""){ return error_show(1004,"参数companyNo 不能为空"); } $trade = Db::name("trade_log")->where([["id","=",$data],["is_del","=",0]])->find(); if(empty($trade)){ return error_show(1005,"未找到资金数据"); } $total_fee = isset($post['total_fee'])&&$post['total_fee']!="" ? $post['total_fee']:""; if($total_fee==""){ return error_show(1004,"参数total_fee 不能为空"); } if($trade['balance']<$total_fee){ return error_show(1005,"资金余额不足"); } $invid = isset($post['invid']) && $post['invid']!="" ? intval($post['invid']) :""; if($invid!=""){ $invlog = Db::name("invoice_pool")->where([["id","=",$invid],["type_check","=",2],["is_del","=",0]]) ->find(); if(empty($invlog)){ return error_show(1005,"未找到发票数据"); } } $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find(); if(empty($customer)){ return error_show(1005,"未找到用户数据"); } Db::startTrans(); try{ $trade['is_all'] = $total_fee == $trade['total_fee'] ? 1 :0; $trade['balance'] -= $total_fee; $trade['tra_fee'] += $total_fee; $trade['updatetime'] = date("Y-m-d H:i:s"); $tradlog=Db::name("trade_log")->save($trade); if($tradlog){ $count = Db::name("trade_pool")->where("logNo","=",$trade['logNo'])->count(); $tradepool = [ "traNo" =>$trade["logNo"]."-".sprintf('%03d',$count+1), "companyNo" => $companyNo, "logNo" => $trade["logNo"], "trade_time" => $trade['trade_time'], "total_fee" => $total_fee, "order_fee" => 0, "inv_fee" => 0, "inv_used" => 0, "order_used" => 0, "winv_fee" => $total_fee, "worder_fee" => $total_fee, "order_status" => 1, "inv_status" => 1, "remark" => "", "status" => 2, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $tra = Db::name("trade_pool")->insert($tradepool,true); if ($invid != "" && !empty($invlog) && $tra>0) { $balance = $invlog['wtrade_fee'] > $tradepool['winv_fee'] ? $tradepool['winv_fee'] : $invlog['wtrade_fee']; if ($invlog['wtrade_fee'] <= 0) { Db::rollback(); return error_show(1004, "发票可核销余额为0"); } $assocNo = makeNo("ASC"); $asslog = [ "assocNo" => $assocNo, "type" => 3, "apply_id" => $effetc['data']['user']['id'], "apply_name" => $username, "companyNo" => $companyNo, "codeNo" => $tradepool["traNo"], "rela_form" => 5, "total_fee" => $tradepool["total_fee"], "balance" => $tradepool['winv_fee'] - $balance, "cancel_total" => $balance, "status" => 1, "source" => 2, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $logassc = Db::name("assoc_key")->insert($asslog); if ($logassc) { $assrela = [ "assocNo" => $assocNo, "viceNo" => $invlog["invNo"], "vice_total" => $invlog["wtrade_fee"], "vice_fee" => $invlog['wtrade_fee'] - $balance, "cancel_fee" => $balance, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; Db::name("assoc_rela")->insert($assrela); $invlog['wtrade_fee'] = $invlog['wtrade_fee'] - $balance; $invlog['fund_used'] = $invlog['fund_used'] + $balance; $invlog["updatetime"] = date("Y-m-d H:i:s"); Db::name("invoice_pool")->save($invlog); // $trade['winv_fee'] = $tradepool['winv_fee'] - $balance; // $trade['inv_used'] = $tradepool['inv_used'] + $balance; // $trade['updatetime'] = date("Y-m-d H:i:s"); $tard = [ "winv_fee"=> $tradepool['winv_fee'] - $balance, "inv_used"=> $balance, "updatetime"=> date("Y-m-d H:i:s") ]; Db::name("trade_pool")->where('traNo',"=",$tradepool['traNo'])->save($tard); Db::commit(); return error_show(0, "资金认领成功"); } else { Db::rollback(); return error_show(0, "关联关系创建失败"); } } Db::commit(); return error_show(0, "资金认领成功"); } Db::rollback(); return error_show(1005, "资金认领失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1005,$e->getMessage()); } } // public function save() // { // $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']); //// } // // $data = isset($post['id']) && $post['id']!="" ? $post['id']:""; // if($data==""){ // return error_show(1004,"参数id 不能为空"); // } // $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:""; // if($companyNo==""){ // return error_show(1004,"参数companyNo 不能为空"); // } // $trade = Db::name("trade_list")->where([["id","=",$data],["is_del","=",0]])->find(); // if(empty($trade)){ // return error_show(1005,"未找到资金数据"); // } // $invid = isset($post['invid']) && $post['invid']!="" ? intval($post['invid']) :""; // if($invid!=""){ // $invlog = Db::name("inv_list")->where([["id","=",$invid],["type_check","=",2]])->find(); // if(empty($invlog)){ // return error_show(1005,"未找到发票数据"); // } // } // $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find(); // if(empty($customer)){ // return error_show(1005,"未找到用户数据"); // } // Db::startTrans(); // try{ // // $tradeinfo=[ // "companyNo"=>$companyNo, // "status"=>1, // "updatetime"=>date("Y-m-d H:i:s") // ]; // $tra = Db::name("trade_pool")->where("traNo","=",$trade['traNo'])->save($tradeinfo); // if($invid!="" && !empty($invlog) && $tra){ // $data=[ // "temp_type"=>2, // "mainNo"=>$tradeinfo["id"], // "viceNo"=>$invid, // "total_fee"=>$invlog['used_fee']>$tradeinfo['used_fee'] ? $tradeinfo['used_fee']:$invlog['used_fee'], // "status"=>0, // "exam_remark"=> "", // "addtime"=>date("Y-m-d H:i:s") // ]; // $temp = Db::name("temp_exam")->insert($data); // if($temp){ // Db::commit(); // return error_show(0,"资金认领成功"); // }else{ // Db::rollback(); // return error_show(1003,"资金认领失败"); // } // } // // Db::commit(); // return error_show(0,"资金认领成功"); // }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']); // } $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):""; if($tradeNo==""){ return error_show(1004,"参数traNo 不能为空"); } $info = Db::name("trade_list")->where([["is_del","=",0],["traNo","=",$tradeNo]])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } $info['userinfo']=[]; if($info['companyNo']!=''){ $info['userinfo'] = Db::name("customer_info")->where('companyNo',"=",$info['companyNo'])->find(); } $info['refundinfo']=[]; if($info['trade_type']==1){ $refund=Db::name("refund")->where([["backNo","=",$tradeNo],['is_del',"=",0]])->select()->toArray(); if(!empty($refund)){ foreach ($refund as $value){ $trade=Db::name("trade_list")->where([['traNo',"=",$value['relaNo']]])->find(); $customer = Db::name("customer_info")->where('companyNo',"=",$trade['companyNo'])->find(); $trade['companyName'] = isset($customer['companyName']) ? $customer['companyName']:""; $info['refundinfo'][]=$trade; } }; } $list = Db::name("assoc_list")->where("(codeNo='{$tradeNo}' or viceNo ='{$tradeNo}' ) and is_del=0")->select(); $info["invNo"]=[]; $info["orderNo"]=[]; foreach ($list as $value){ if($value["rela_form"]==2){ $info["orderNo"][]=$value["codeNo"]; } if($value["rela_form"]==3){ $info["orderNo"][]=$value["viceNo"]; } if($value["rela_form"]==4){ $info["invNo"][]=$value["codeNo"]; } if($value["rela_form"]==5){ $info["invNo"][]=$value["viceNo"]; } } $info["invNo"]= implode(",",$info["invNo"]); $info["orderNo"]= implode(",",$info["orderNo"]); return app_show(0,"获取成功",$info); } public function info(){ $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']); } $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):""; if($tradeNo==""){ return error_show(1004,"参数traNo 不能为空"); } $info = Db::name("trade_list")->where([["is_del","=",0],["traNo","=",$tradeNo]])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } $info['userinfo']=[]; if($info['companyNo']!=''){ $info['userinfo'] = Db::name("customer_info")->where('companyNo',"=",$info['companyNo'])->find(); } $list = Db::name("assoc_list")->where("codeNo='{$tradeNo}' and is_del=0 and type=3 and rela_form=5 and source=2")->find(); if($list){ $inv = Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$list['viceNo']]])->find(); if($inv){ $info['inv'] = $inv; }else{ return error_show(1004,"未找到关联发票"); } }else{ $info['inv']=[]; } return app_show(0,"获取成功",$info); } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function TradeInfo(){ $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']); } $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):""; if($tradeNo==""){ return error_show(1004,"参数traNo 不能为空"); } $info = Db::name("trade_log")->field('*,used_fee+tra_fee as amount')->where([["is_del","=",0],["logNo","=",$tradeNo]])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } $tradelist = Db::name("trade_pool")->where([["is_del","=",0],["logNo","=",$tradeNo]])->select(); $info['tradelist']=$tradelist; return app_show(0,"获取成功",$info); } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ // public function Poollist() // { // $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 = [["is_del","=",0],['refund_status',"=",0]]; // // $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']): ""; // if ($status != "") { // $condition[] = ["status", "=",$status]; // } // $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']): ""; // if ($companyNo != "") { // $condition[] = ["companyNo", "=",$companyNo]; // } // $order_status = isset($post['order_status']) && $post['order_status'] !== "" ? intval($post['order_status']) : ""; // if ($order_status != "") { // // $condition[] = ["order_status", "=",$order_status]; // if($order_status==4){ // $condition[] = ["order_status", "in", [1,2]]; // }else{ // $condition[] = ["order_status", "=", "{$order_status}"]; // } // } // // $inv_status = isset($post['inv_status']) && $post['inv_status'] !== "" ? intval($post['inv_status']) : ""; // if ($inv_status != "") { // // $condition[] = ["inv_status", "=",$inv_status]; // if($inv_status==4){ // $condition[] = ["inv_status", "in", [1,2]]; // }else{ // $condition[] = ["inv_status", "=", "{$inv_status}"]; // } // } // $tradeNo = isset($post['traNo']) && $post['traNo'] !== "" ? trim($post['traNo']) : ""; // if ($tradeNo != "") { // $condition[] = ["traNo", "like","%{$tradeNo}%"]; // } // $total_s = isset($post['total_low']) && $post['total_low'] !== "" ? trim($post['total_low']) : ""; // if ($total_s != "") { // $condition[] = ["total_fee", ">=",$total_s]; // } // $total_e = isset($post['total_up']) && $post['total_up'] !== "" ? trim($post['total_up']) : ""; // if ($total_e != "") { // $condition[] = ["total_fee", ">=",$total_e]; // } // $end = isset($post["end"])&&$post['end']!="" ? $post['end'] : ""; // $start = isset($post["start"])&&$post['start']!="" ? $post['start'] : ""; // if($start!=""){ // $condition[] = ["trade_time", ">=",$start]; // } // if($end!=""){ // $condition[] = ["trade_time", "<=",$end]; // } // $trade_type = isset($post['trade_type']) && $post['trade_type'] !== "" ? trim($post['trade_type']) : 0; // // $condition[] = ["trade_type", "=",$trade_type]; // // $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1; // $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10; // // $count = Db::name("trade_list")->where($condition)->count(); // $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1; // $page = $page >= $total ? intval($total) : $page; // $list = Db::name("trade_list")->where($condition)->order("addtime desc")->page(intval($page), $size)->select(); // $data=[]; // foreach ($list as $value){ // $customer = Db::name("customer_info")->where("companyNo","=",$value['companyNo'])->find(); // $value['customer'] = $customer['companyName']; // $data[]=$value; // } // // return app_show(0,"获取成功",['list'=>$data,"count"=>$count]); // } public function Poollist() { $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],['a.refund_status',"=",0]]; $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']): ""; if ($status != "") { $condition[] = ["a.status", "=",$status]; } $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']): ""; if ($companyNo != "") { $condition[] = ["a.companyNo", "=",$companyNo]; } $order_status = isset($post['order_status']) && $post['order_status'] !== "" ? intval($post['order_status']) : ""; if ($order_status != "") { // $condition[] = ["order_status", "=",$order_status]; if($order_status==4){ $condition[] = ["a.order_status", "in", [1,2]]; }else{ $condition[] = ["a.order_status", "=", "{$order_status}"]; } } $inv_status = isset($post['inv_status']) && $post['inv_status'] !== "" ? intval($post['inv_status']) : ""; if ($inv_status != "") { // $condition[] = ["inv_status", "=",$inv_status]; if($inv_status==4){ $condition[] = ["a.inv_status", "in", [1,2]]; }else{ $condition[] = ["a.inv_status", "=", "{$inv_status}"]; } } $tradeNo = isset($post['traNo']) && $post['traNo'] !== "" ? trim($post['traNo']) : ""; if ($tradeNo != "") { $condition[] = ["a.traNo", "like","%{$tradeNo}%"]; } $total_s = isset($post['total_low']) && $post['total_low'] !== "" ? trim($post['total_low']) : ""; if ($total_s != "") { $condition[] = ["a.total_fee", ">=",$total_s]; } $total_e = isset($post['total_up']) && $post['total_up'] !== "" ? trim($post['total_up']) : ""; if ($total_e != "") { $condition[] = ["a.total_fee", ">=",$total_e]; } $end = isset($post["end"])&&$post['end']!="" ? $post['end'] : ""; $start = isset($post["start"])&&$post['start']!="" ? $post['start'] : ""; if($start!=""){ $condition[] = ["b.trade_time", ">=",$start]; } if($end!=""){ $condition[] = ["b.trade_time", "<=",$end]; } $trade_type = isset($post['trade_type']) && $post['trade_type'] !== "" ? trim($post['trade_type']) : 0; $condition[] = ["b.trade_type", "=",$trade_type]; $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1; $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10; $count = Db::name("trade_pool")->alias("a")->leftJoin("trade_log b","a.logNo = b.logNo")->where($condition)->count(); $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1; $page = $page >= $total ? intval($total) : $page; $list = Db::name("trade_pool")->alias("a")->leftJoin("trade_log b","a.logNo = b.logNo")->where($condition)->field("`a`.`id` AS `id`, `a`.`traNo` AS `traNo`, `a`.`companyNo` AS `companyNo`, `a`.`logNo` AS `logNo`, `a`.`total_fee` AS `total_fee`, `a`.`order_fee` AS `order_fee`, `a`.`inv_fee` AS `inv_fee`, `a`.`inv_used` AS `inv_used`, `a`.`winv_fee` AS `winv_fee`, `a`.`order_used` AS `order_used`, `a`.`worder_fee` AS `worder_fee`, `a`.`order_status` AS `order_status`, `a`.`inv_status` AS `inv_status`, `a`.`remark` AS `remark`, `a`.`is_del` AS `is_del`, `a`.`status` AS `status`, `a`.`exam_remark` AS `exam_remark`, `a`.`is_order` AS `is_order`, `a`.`addtime` AS `addtime`, `a`.`updatetime` AS `updatetime`, `b`.`trade_bank` AS `trade_bank`, `b`.`trade_account` AS `trade_account`, `b`.`trade_type` AS `trade_type`, `b`.`trade_time` AS `trade_time`, `b`.`trade_used` AS `trade_used`, `b`.`trade_out` AS `trade_out`, `b`.`trade_in` AS `trade_in`, `b`.`trade_remark` AS `trade_remark`, `b`.`total_fee` AS `btotal_fee`, `b`.`used_fee` AS `bused_fee`, `b`.`tra_fee` AS `btra_fee`, `b`.`balance` AS `bbalance`, `b`.`status` AS `bstatus`, `b`.`is_all` AS `is_all`,( `b`.`used_fee` + `b`.`tra_fee` ) AS `amount`, `a`.`refund_status` AS `refund_status`") ->order("a.addtime desc")->page(intval($page), $size)->select(); $data=[]; foreach ($list as $value){ $customer = Db::name("customer_info")->where("companyNo","=",$value['companyNo'])->find(); $value['customer'] = $customer['companyName']; $data[]=$value; } return app_show(0,"获取成功",['list'=>$data,"count"=>$count]); } /** * 保存更新的资源 * * @param \think\Request $request * @param int $id * @return \think\Response */ public function update() { $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']); } $traNo = isset($post['traNo']) &&$post['traNo']!="" ? trim($post['traNo']):""; if($traNo==""){ return error_show(1005,"参数traNo 不能为空"); } $status= isset($post["status"])&&$post["status"]!=="" ? $post["status"] :""; if($status===""){ return error_show(1005,"参数status 不能为空"); } $remark= isset($post["remark"])&&$post["remark"]!="" ? $post["remark"] :""; $info = Db::name("trade_pool")->where([["traNo","=",$traNo],['is_del',"=",0]])->find(); if(!$info){ return error_show(1005,"未找到资金数据"); } if($info['status']==3){ return error_show(1005,"资金已审核通过"); } $tradeinfo=[ "status"=> $status, "exam_remark"=>$remark, "updatetime"=>date("Y-m-d H:i:s") ]; Db::startTrans(); try{ $tra = Db::name("trade_pool")->where([["traNo","=",$traNo],['is_del',"=",0]])->save($tradeinfo); if($tra && $status==3){ $tradelog = Db::name("trade_log")->where([["logNo","=",$info['logNo']],["is_del","=",0]])->find(); if($tradelog){ $tradelog['tra_fee'] -=$info['total_fee']; $tradelog['used_fee'] +=$info['total_fee']; $tradelog['status'] =$tradelog['used_fee']==$tradelog['total_fee'] ? 3:2; $tradelog['is_all'] =$tradelog['used_fee']==$tradelog['total_fee'] ? 1:0; $tradelog['updatetime'] =date("Y-m-d H:i:s"); Db::name("trade_log")->save($tradelog); }else{ Db::rollback(); return app_show(1004,"资金数据更新失败"); } $assoc = Db::name("assoc_list")->where([["source","=",2],["is_del","=",0],['rela_form',"=",5], ['codeNo',"=",$traNo]])->find(); if($assoc){ $tra = Db::name("trade_pool")->where("traNo","=",$traNo)->save (['inv_fee'=>$info['inv_fee']+$assoc['cancel_total'],'inv_used'=>$info['inv_used']-$assoc['cancel_total'], 'inv_status'=>$info['winv_fee']==0 ? 3:2]); $inv = Db::name("invoice_pool")->where([["is_del","=",0],['invNo',"=",$assoc['viceNo']]])->find(); if($inv){ $inv['atrade_fee']+=$assoc['cancel_fee']; $inv['fund_used']-=$assoc['cancel_fee']; $inv['updatetime']=date("Y-m-d H:i:s"); $inv['trade_status']=$inv['wtrade_fee']==0?3:2; Db::name("invoice_pool")->save($inv); Db::name("assoc_key")->where([["source","=",2],["is_del","=",0],['rela_form',"=",5], ['codeNo',"=",$traNo]])->save(["status"=>3]); }else{ Db::rollback(); return app_show(1004,"未找到发票数据"); } } Db::commit(); return app_show(0,"审核通过"); } if($tra && $status==4){ Db::commit(); return app_show(0,"审核未通过"); } Db::rollback(); return app_show(1004,"审核失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1003,$e->getMessage()); } } /** * 删除指定资源 * * @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']); } $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):""; if($tradeNo==""){ return error_show(1004,"参数traNo 不能为空"); } $info = Db::name("trade_pool")->where([["is_del","=",0],["traNo","=",$tradeNo]])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } if($info['status']==3&&$info['trade_user']!=''){ return error_show(1004,"款项已通过审核"); } Db::startTrans(); try { $assck= Db::name("assoc_list")->where([["type","=",3],["codeNo","=",$tradeNo],["is_del","=",0],["rela_form","=",5]])->find(); if( $assck && $assck['status']!=3){ $aa =Db::name("assoc_key")->where([["type","=",3],["codeNo","=",$tradeNo],["is_del","=",0],["rela_form","=",5]])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]); if($aa){ $data=[ "is_del"=>1, "updatetime"=>date("Y-m-d H:i:s") ]; $trapool = Db::name("trade_pool")->where([["is_del","=",0],["traNo","=",$tradeNo]])->save($data); if($trapool){ $tralog = Db::name('trade_log')->where('logNo',"=",$info['logNo'])->find(); if($tralog){ $tralog['tra_fee']-=$info['total_fee']; $tralog['balance']+=$info['total_fee']; $tralog['status']=$tralog['balance']==$tralog['total_fee'] ?1 :2; $tralog['updatetime']=date("Y-m-d H:i:s"); Db::name('trade_log')->save($tralog); }else{ Db::rollback(); return app_show(1004,"资金认领取消失败"); } }else{ Db::rollback(); return app_show(1004,"资金认领取消失败"); } $inv = Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$assck['viceNo']]])->find(); if($inv){ $inv['wtrade_fee']+=$assck['cancel_fee']; $inv['fund_used']-=$assck['cancel_fee']; $inv['updatetime']=date("Y-m-d H:i:s"); Db::name("invoice_pool")->save($inv); } Db::commit(); return app_show(0,"取消认领成功"); }else{ Db::rollback(); return error_show(1003,"取消认领失败"); } }else{ $data=[ "is_del"=>1, "updatetime"=>date("Y-m-d H:i:s") ]; $trapool = Db::name("trade_pool")->where([["is_del","=",0],["traNo","=",$tradeNo]])->save($data); if($trapool){ $tralog = Db::name('trade_log')->where('logNo',"=",$info['logNo'])->find(); if($tralog){ $tralog['tra_fee']-=$info['total_fee']; $tralog['balance']+=$info['total_fee']; $tralog['status']=$tralog['balance']==$tralog['total_fee'] ?1 :2; $tralog['updatetime']=date("Y-m-d H:i:s"); Db::name('trade_log')->save($tralog); Db::commit(); return app_show(0,"取消认领成功"); }else{ Db::rollback(); return app_show(1004,"资金认领取消失败"); } }else{ Db::rollback(); return app_show(1004,"资金认领取消失败"); } } Db::rollback(); return error_show(1003,"取消认领失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1004,$e->getMessage()); } } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function SaveRefund(){ $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']); } $userinfo = GetUserInfo($token); $username = ""; if (isset($userinfo['code']) && $userinfo['code'] == 0) { $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : ""; } $data = isset($post['id']) && $post['id']!="" ? $post['id']:""; if($data==""){ return error_show(1004,"参数id 不能为空"); } $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:""; if($companyNo==""){ return error_show(1004,"参数companyNo 不能为空"); } $trade = Db::name("trade_log")->where([["id","=",$data],["is_del","=",0]])->find(); if(empty($trade)){ return error_show(1005,"未找到资金数据"); } // $total_fee = isset($post['total_fee'])&&$post['total_fee']!="" ? $post['total_fee']:""; // if($total_fee==""){ // return error_show(1004,"参数total_fee 不能为空"); // } // if($trade['balance']<$total_fee){ // return error_show(1005,"资金余额不足"); // } $refundid = isset($post['refundid']) && $post['refundid']!="" ? $post['refundid'] :""; if($refundid==""){ return error_show(1006,"参数refundid 不能为空"); } $refund =Db::name("refund")->where([["is_del","=",0],['refund_status',"=",3],['refundNo',"in",$refundid]]) ->select()->toArray(); if(empty($refund)){ return error_show(1006,"退款资金数据未找到"); } $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find(); if(empty($customer)){ return error_show(1005,"未找到用户数据"); } $refund_fee = array_sum(array_column($refund,"refund_fee")); if($refund_fee>$trade['balance']){ return error_show(1005,"资金退款余额不足"); } Db::startTrans(); try{ $trade['is_all'] = $refund_fee == $trade['total_fee'] ? 1 :0; $trade['balance'] -= $refund_fee; $trade['tra_fee'] += $refund_fee; $trade['updatetime'] = date("Y-m-d H:i:s"); $tradlog=Db::name("trade_log")->save($trade); if($tradlog){ $count = Db::name("trade_pool")->where("logNo","=",$trade['logNo'])->count(); $tradepool = [ "traNo" =>$trade["logNo"]."-".sprintf('%03d',$count+1), "companyNo" => $companyNo, "logNo" => $trade["logNo"], "trade_time" => $trade['trade_time'], "total_fee" => $refund_fee, "order_fee" => 0, "inv_fee" => 0, "inv_used" => 0, "order_used" => 0, "winv_fee" => $refund_fee, "worder_fee" => $refund_fee, "order_status" => 1, "inv_status" => 1, "remark" => "", "status" => 2, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $tra = Db::name("trade_pool")->insert($tradepool,true); if($tra>0) { $good = []; foreach ($refund as $value) { $value['backNo'] = $tradepool['traNo']; $value['back_fee'] = $value['refund_fee']; $value['refund_status'] = 4; $value['updatetime'] = date("Y-m-d H:i:s"); $ref = Db::name("refund")->save($value); if ($ref) { $tadpool = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=", $value['relaNo']], ['refund_status', "=", 2]])->save(["refund_status" => 3, "updatetime" => date("Y-m-d H:i:s")]); if (!$tadpool) { Db::rollback(); return error_show(1007, "资金退款状态更新失败"); } $tarlog = Db::name("trade_list")->where([["is_del", "=", 0], ["traNo", "=",$value['relaNo']]])->find(); if($tarlog){ $dat=[ 'balance'=>$tarlog['bbalance']+$tarlog['total_fee'], "used_fee"=>$tarlog['bused_fee']-$tarlog['total_fee'], "status"=>$tarlog['bused_fee']==$tarlog['total_fee'] ? 1:2, "is_all"=>0 ]; Db::name("trade_log")->where("logNo","=",$tarlog['logNo'])->save($dat); } $rela = Db::name("assoc_list")->where([["codeNo|viceNo", "=", $value['relaNo']], ["is_del", "=", 0]])->find(); if ($rela) { $corderNo= $rela['type']==1 ? $rela['codeNo'] : $rela['viceNo']; $orderlist = Db::name("order_info")->where([['orderNo', "=",$corderNo], ["status", "=", 1]])->select(); $bala = 0; foreach ($orderlist as $key => $val) { $temp = []; if ($rela['cancel_fee'] <= 0) { break; } if ($rela['cancel_fee'] >= $val['afund_fee']) { $rela['cancel_fee'] -= $val['afund_fee']; $bala = $val['afund_fee']; } else { $bala = $rela['cancel_fee']; $rela['cancel_fee'] = 0; } $temp['goodno'] = $val['goodNo']; $temp['qrdno'] = $val['qrdNo']; $temp['fund_fee'] = $bala; $temp['num'] = $val['good_num']; $good[] = $temp; } } } else { Db::rollback(); return error_show(1007, "资金退款状态更新失败"); } } if (!empty($good)) { $total=0; $orderNo = makeNo('ORD'); foreach ($good as $value) { $temp = []; $temp['orderNo'] = $orderNo; $temp['goodNo'] = $value['goodno']; $goocp = Db::name("qrd_list")->where("sequenceNo", "=", $value['goodno'])->find(); if (!$goocp) { continue; } $temp['good_name'] = $goocp['goodname']; $temp['qrdNo'] = $goocp['qrdNo']; $temp['brand'] = $goocp['brand']; $temp['unit'] = $goocp['unit']; $temp['good_num'] = $value['num']; $temp['price'] = $goocp['price']; $temp['total_fee'] = $value['fund_fee']; $temp['wfund_fee'] = 0; $temp['afund_fee'] = $value['fund_fee']; $temp['winv_fee'] = $value['fund_fee']; $temp['other_fee'] = 0; $temp['status'] = 1; $temp['remark'] = ""; $temp['addtime'] = date("Y-m-d H:i:s"); $temp['updatetime'] = date("Y-m-d H:i:s"); $total += $value['fund_fee']; $orderinfo[] = $temp; } $orderins = Db::name("order_info")->insertAll($orderinfo); if ($orderins) { $order = [ "orderNo" => $orderNo, "companyNo" => $companyNo, "sales_id" => $effetc['data']['user']['id'], "sales_name" => $username, "total_amount" => $total, "inv_status" => 1, "fund_status" => 3, "fund_fee" => $total, "wfund_fee" => 0, "inv_fee" => 0, "winv_fee" => $total, "status" => 0, "type" =>4, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $orderpool = Db::name("order_pool")->insert($order, true); if ($orderpool > 0) { $rela = []; $assNo = makeNo("ASC"); $cancel = 0; $balance = $tradepool["worder_fee"] > $total ? $total : $tradepool["worder_fee"]; $total -= $balance; $sav = [ "worder_fee" => $tradepool['worder_fee'] - $balance, "order_fee" => $balance, "is_order" => 1, "updatetime" => date("Y-m-d H:i:s") ]; Db::name("trade_pool")->where([["traNo", "=", $tradepool['traNo']]])->save($sav); $rela = [ "assocNo" => $assNo, "viceNo" => $orderNo, "vice_total" => $order['total_amount'], "vice_fee" => $total, "cancel_fee" => $balance, "status" => 1, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $assoc = Db::name("assoc_rela")->insert($rela); if ($assoc) { $data = [ "assocNo" => $assNo, "companyNo" => $companyNo, "type" => 3, "rela_form" => 3, "codeNo" => $tradepool["traNo"], "total_fee" => $tradepool['total_fee'], "balance" => $tradepool['worder_fee'] - $balance, "cancel_total" => $balance, "status" => 3, "source" => 3, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $realkey = Db::name("assoc_key")->insert($data); if ($realkey) { $qrdup=$this->qrd_sure($orderNo,$balance); if(!$qrdup){ Db::rollback(); return error_show(1004, "新建关联申请失败"); } Db::commit(); return app_show(0, "欠款资金认领成功"); } } } } } } Db::commit(); return app_show(0,"欠款资金认领成功"); } Db::rollback(); return error_show(1007,"资金退款认领失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1006,$e->getMessage()); } } /** * @param $orderNo * @param $balance * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function qrd_sure($orderNo,$balance){ $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 0]])->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) { $order_fee= $value['afund_fee']; $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find(); if($orderinfo['type']==4 && $order_fee <= $qrdinfo['Number1618249146997']){ $qrdinfo['Number1618249146997']-= $order_fee; $qrdinfo['number1618249149738'] += $order_fee; $qrdinfo['ShortText1618559043560'] = $qrdinfo['Number1618249146997']>0 ? "2" : "1"; }else{ if($order_fee<=$qrdinfo['paying_fee']){ $qrdinfo['paying_fee'] -= $order_fee; $qrdinfo['Number1618249146997'] += $order_fee; $qrdinfo['ShortText1618559043560'] = $qrdinfo['number1618249149738']>0 ? "2" : "3"; }else{ return false; } } $qrdinfo['status']=1; $qinfo = Db::name("qrd_info")->save($qrdinfo); if ($qinfo) { $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find(); if($orderinfo['type']==4 &&$qrdp['apay_fee'] >= $order_fee ){ $qrdp['apay_fee'] -= $order_fee; $qrdp['wpay_fee'] += $order_fee; $qrdp['pay_status'] = $qrdp['apay_fee'] == 0 ? 1 : 2; }else{ if ($qrdp['pay_fee'] >= $order_fee) { $qrdp['pay_fee'] -= $order_fee; $qrdp['apay_fee'] += $order_fee; $qrdp['pay_status'] = $qrdp['wpay_fee'] == 0 ? 3 : 2; }else{ return false; } } $qrdp['status'] =1; $qrd = Db::name("qrd")->save($qrdp); if(!$qrd){ return false; } }else { return false; } } return true; } }