123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299 |
- <?php
- declare (strict_types = 1);
- namespace app\Admin\controller;
- use app\BaseController;
- use think\facade\Db;
- class Trade extends BaseController
- {
- /**
- * 显示资源列表
- *
- * @return \think\Response
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- 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 = [["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;
- }
- }
|