123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\ReportCode;use think\App;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- class OrderPay extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- }
- //新建资金认领数据
- public function create(){
- $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
- if($tradNo==""){
- return error_show(1004,"参数 tradNo 不能为空");
- }
- $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
- if(empty($orderArr)){
- return error_show(1004,"参数 orderArr 不能为空");
- }
- Db::startTrans();
- try{
- $trade =Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->lock(true)->findOrEmpty();
- if(empty($trade)){
- Db::rollback();
- return error_show(1004,"未找到资金信息");
- }
- $total_fee =array_sum(array_column($orderArr,"trad_fee"));
- if ($trade['balance']<$total_fee){
- Db::rollback();
- return error_show(1004,"资金余额不足核销");
- }
- $assoc=[];
- foreach ($orderArr as $value){
- $logNo=makeNo("TRC");
- if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
- Db::rollback();
- return error_show(1004,"销售单编号不能为空");
- }
- if(!isset($value['trad_fee'])||$value['trad_fee']==""){
- Db::rollback();
- return error_show(1004,"销售单核销金额不能为空");
- }
- $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])
- ->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice,is_comon,platform_type")
- ->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1004,"销售单信息未找到");
- }
- if($qrd['is_comon']==1) throw new \Exception("{$value['sequenceNo']} 通用订单不可认领资金");
- if($qrd['wpay_fee']<$value['trad_fee']){
- Db::rollback();
- return error_show(1004,"销售单未付款金额不足核销金额");
- }
- $updt=[
- "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
- "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
- "pay_status"=>2,
- "status"=>1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
- if($qrdup==false){
- Db::rollback();
- return error_show(1004,"销售单核销金额失败");
- }
- $temp=[
- "assocNo"=>makeNo("AS"),
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "type"=>2,
- "orderCode"=>$value['sequenceNo'],
- "customerNo"=>$qrd['customerNo'],
- "viceCode"=>$logNo,
- "order_total"=>$qrd['totalPrice'],
- "vice_total"=>$value['trad_fee'],
- "cancel_fee"=>$value['trad_fee'],
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assoc[]=$temp;
- $create = [
- "logNo"=>$logNo,
- "tradNo"=>$tradNo,
- "platform_type"=>$qrd['platform_type']??0,
- "companyNo"=>$trade['companyNo'],
- "customerNo"=>$qrd['customerNo'],
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "trade_time"=>$trade['trade_time'],
- "total_fee"=>$value['trad_fee'],
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $tradchild =Db::name("trade_pool")->insert($create);
- if($tradchild ==false){
- Db::rollback();
- return error_show(1004,"资金认领失败");
- }
- $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
- if($report)$report->setField("logNo",$logNo);
- if($report)$report->setField("tradNo",$tradNo);
- }
- $asscin= Db::name("assoc")->insertAll($assoc);
- if($asscin!=count($assoc)){
- Db::rollback();
- return error_show(1004,"销售单核销金额关联失败");
- }
- $update=[
- "balance"=>$trade['balance']-$total_fee,
- "used_fee"=>$trade['used_fee']+$total_fee,
- "status"=>($trade['balance']-$total_fee)==0 ? 3:2,
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $tradup=Db::name("trade")->where($trade)->update($update);
- if($tradup){
- Db::commit();
- return app_show(0,"资金认领成功");
- }
- Db::rollback();
- return error_show(1004,"资金认领失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- // 1待审批2审批通过3审批驳回4退款5解除认领
- public function status(){
- $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
- if($logNo==""){
- return error_show(1004,"参数logNo不能为空");
- }
- $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
- if($logingo==false){
- return error_show(1004,"资金认领信息未找到");
- }
- $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
- if($status==""){
- return error_show(1004,"参数 status 不能为空");
- }
- if($status==5 &&$logingo['status']!=1 ){
- return error_show(1004,"认领资金状态有误");
- }
- $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
- $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
- if(empty($trade)){
- return error_show(1005,"资金信息未找到");
- }
- Db::startTrans();
- try {
- $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
- $logup = Db::name("trade_pool")->where($logingo)->update($up);
- if($logup){
- if($status==2){
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
- if(!empty($qrdArr)){
- foreach ($qrdArr as $value){
- $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,is_comon,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,cxCode")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1005,"未找到销售单数据");
- }
- if($qrd['pay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单待付金额不足");
- }
- $update =[
- "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
- "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
- "paytime"=>date("Y-m-d H:i:s"),
- "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
- if($qrdup==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- if($update['pay_status']==3 && $qrd['is_comon']==0){
- (new \app\admin\model\ComonOrder())->where(["cxCode"=>$qrd['cxCode'],"status"=>-1])->update(["status"=>0]);
- }
- $asscup =[
- "status"=>2,
- "assoc_time"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assc=Db::name("assoc")->where($value)->update($asscup);
- if($assc==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- }
- }
- }
- if($status==3 || $status==5){
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
- if(!empty($qrdArr)){
- foreach ($qrdArr as $value){
- $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1005,"未找到销售单数据");
- }
- if($qrd['pay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单待付金额不足");
- }
- $update =[
- "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
- "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
- "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
- "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
- if($qrdup==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $asscup =[
- "status"=>3,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assc=Db::name("assoc")->where($value)->update($asscup);
- if($assc==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
- if($report)$report->rmField("logNo",$logNo);
- }
- }
- if($trade['used_fee']<$logingo['total_fee']){
- Db::rollback();
- return error_show(1005,"资金信息已认领金额不足");
- }
- $tradup =[
- "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
- "balance"=>$trade['balance']+$logingo['total_fee'],
- "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 1 :2,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $updaT=Db::name("trade")->where($trade)->update($tradup);
- if($updaT==false){
- Db::rollback();
- return error_show(1005,"资金信息更新失败");
- }
- }
- Db::commit();
- return app_show(0,"审核成功");
- }
- Db::rollback();
- return error_show(1004,"审核失败");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- //资金信息列表
- public function list(){
- $condition=[['is_del',"=",0]];
- $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
- $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
- $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
- $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
- $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
- $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
- $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- $used_lower =isset($this->post['userd_lower'])&&$this->post['userd_lower']!==""? floor($this->post['userd_lower']) :"0";
- $used_upper =isset($this->post['used_upper'])&&$this->post['used_upper']!==""? floor($this->post['used_upper']):"";
- if($used_lower!=="")$condition[]=["used_fee",">=",$used_lower];
- if($used_upper!=="")$condition[]=["used_fee","<=",$used_upper];
- $total_lower =isset($this->post['total_lower'])&&$this->post['total_lower']!==""? floor($this->post['total_lower']) :"0";
- $total_upper =isset($this->post['total_upper'])&&$this->post['total_upper']!==""? floor($this->post['total_upper']):"";
- if($total_lower!=="")$condition[]=["total_fee",">=",$total_lower];
- if($total_upper!=="")$condition[]=["total_fee","<=",$total_upper];
- if($companyNo!==""){
- $condition[]=["companyNo","=",$companyNo];
- }
- $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["companyNo","=",$relaComNo];
- }
- if($tradNo!==""){
- $condition[]=["tradNo","like","%$tradNo%"];
- }
- if($bank!=""){
- $condition[]=["trade_bank","like","%$bank%"];
- }
- if($name!=""){
- $condition[]=["trade_out","like","%$name%"];
- }
- if($start!=""){
- $condition[]=["trade_time",">=",$start." 00:00:00"];
- }
- if($end!=""){
- $condition[]=["trade_time","<=",$end." 23:59:59"];
- }
- if($status!==""){
- $condition[]=["status","=",$status];
- }
- $count=Db::name("trade")->where($condition)->count();
- $total=ceil($count/$size);
- $page=$page>$total? intval($total):$page;
- $list = Db::name("trade")
- ->where($condition)
- ->page($page, $size)
- ->order(['addtime' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- foreach ($list as &$value) {
- $data = Db::name("trade_pool")
- ->alias("a")
- ->leftJoin("assoc b", "a.logNo=b.viceCode")
- ->leftJoin("qrd_info c", "b.orderCode=c.sequenceNo")
- ->where(["a.tradNo" => $value['tradNo'], "a.is_del" => 0])
- ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
- ->field("a.*,b.orderCode,c.cxCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.customerName,c.customerNo")
- ->select()
- ->toArray();
- $value['child'] = $data;
- $value['companyName'] = Db::name('company_info')->where(['companyNo' => $value['companyNo']])->value('company_name', '');
- }
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- public function tradeDelete(){
- $id =isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']):"";
- if($id==""){
- return error_show(1004,"参数错误");
- }
- $trade = Db::name("trade")->where(["id"=>$id])->find();
- if($trade){
- $updaT=Db::name("trade")->where(["id"=>$id])->update(["is_del"=>1]);
- if($updaT) return app_show(0,"删除成功");
- }
- return error_show(1004,"删除失败");
- }
- // 认领资金列表明细
- public function tradeList(){
- $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
- $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
- $condition =[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0],["d.is_del","=",0]];
- $roleid = $this->roleid;
- $check = checkRole($roleid,'87');
- if($check){
- $condition[]=["a.apply_id","=",$this->uid];
- }
- $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
- $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
- $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
- $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
- $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
- $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
- $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
- $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- if($companyNo!==""){
- $condition[]=["a.companyNo","=",$companyNo];
- }
- if($platform_type!==""){
- $condition[]=["a.platform_type","=",$platform_type];
- }
- $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["a.companyNo","=",$relaComNo];
- }
- if($tradNo!==""){
- $condition[]=["a.tradNo","like","%$tradNo%"];
- }
- if($orderCode!==""){
- $condition[]=["d.orderCode","like","%$orderCode%"];
- }
- $cxCode= isset($this->post['cxCode'])&&$this->post['cxCode']!="" ? trim($this->post['cxCode']) :"";
- if($cxCode!=""){
- $condition[]=["c.cxCode","like","%$cxCode%"];
- }
- $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
- if($logNo!==""){
- $condition[]=["a.logNo","like","%$logNo%"];
- }
- if($bank!=""){
- $condition[]=["b.trade_bank","like","%$bank%"];
- }
- if($name!=""){
- $condition[]=["b.trade_out","like","%$name%"];
- }
- if($start!=""){
- $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
- }
- if($end!=""){
- $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($end))];
- }
- if($status!==""){
- $condition[]=["a.status","=",$status];
- }
- if($apply_id!==""){
- $condition[]=["a.apply_id","=",$apply_id];
- }
- if($apply_name!==""){
- $condition[]=["a.apply_name","like","%$apply_name%"];
- }
- $count=Db::name("trade_pool")->alias("a")
- ->leftJoin("trade b","a.tradNo=b.tradNo")
- ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
- ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
- ->where($condition)->count();
- $total=ceil($count/$size);
- $page=$page>$total? intval($total):$page;
- $list =Db::name("trade_pool")->alias("a")
- ->leftJoin("trade b","a.tradNo=b.tradNo")
- ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
- ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
- ->where($condition)
- ->page($page,$size)
- ->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,c.customerName,c.customerNo,
- d.orderCode,c.qrdSource,c.cxCode,c.goodNo,c.goodName,c.qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,c.totalPrice,d.cancel_fee")
- ->order("a.addtime desc")->select();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- // 获取资金下面的认领信息
- public function tradeQuery(){
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
- $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
- $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
- $condition=[['is_del','=',0]];
- if($companyNo!==""){
- $condition[]=["companyNo","=",$companyNo];
- }
- if($platform_type!==""){
- $condition[]=["platform_type","=",$platform_type];
- }
- $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["companyNo","=",$relaComNo];
- }
- if($apply_id!==""){
- $condition[]=["apply_id","=",$apply_id];
- }
- if($apply_name!==""){
- $condition[]=["apply_name","like","%$apply_name%"];
- }
- if($status!==""){
- $condition[]=["status","=",$status];
- }
- if($tradNo!==""){
- $condition[]=["tradNo","like","%$tradNo%"];
- }
- $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
- return app_show(0,"获取成功",$list);
- }
- //资金详情
- public function tradeInfo(){
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
- if($tradNo==""){
- return error_show(1004,"参数 tradNo 不能为空");
- }
- $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
- if($tradinfo==false){
- return error_show(1004,"资金信息未找到");
- }
- return app_show(0,"获取成功",$tradinfo);
- }
- //资金认领详情
- public function logInfo(){
- $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
- if($logNo==""){
- return error_show(1004,"参数 logNo 不能为空");
- }
- $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
- if($tradinfo==false){
- return error_show(1004,"资金信息未找到");
- }
- $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
- $tradinfo['trade_out']= $trade['trade_out']??"";
- $tradinfo['trade_in']= $trade['trade_in']??"";
- $tradinfo['trade_bank']= $trade['trade_bank']??"";
- $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
- $tradinfo['companyNo']= $trade['companyNo']??"";
- $tradinfo['total_fee']= $trade['total_fee']??"";
- $tradinfo['balance']= $trade['balance']??"";
- $tradinfo['used_fee']= $trade['used_fee']??"";
- $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
- ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
- ->order("a.addtime desc")
- ->field("c.*,a.cancel_fee")
- ->findOrEmpty();
- $tradinfo['orderinfo']=$orderinfo;
- return app_show(0,"获取成功",$tradinfo);
- }
- //认领资金退回或退款 type 1 退款 2 解除资金认领
- public function ReturnPay(){
- $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
- if($logNo==''){
- return error_show(1004,"参数 logNo 不能为空");
- }
- $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
- if($type==""){
- return error_show(1004,"参数 type 不能为空");
- }
- $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
- if($loginfo==false){
- return error_show(1004,"认领资金信息未找到");
- }
- if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
- if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
- $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
- if($tradinfo==false){
- return error_show(1004,"资金信息未找到");
- }
- $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
- ->findOrEmpty();
- if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
- $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
- $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
- $returnCode =makeNo("RTA");
- $data=[
- "returnCode"=>$returnCode,
- "logNo"=>$logNo,
- "companyNo"=>$tradinfo['companyNo'],
- "tradNo"=>$loginfo['tradNo'],
- "return_img"=>'',
- "type"=>$type,
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- "return_reason"=>$reason,
- "remark"=>$remark,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $in = Db::name("trade_return")->insert($data);
- if($in){
- return app_show(0,"申请新建成功");
- }else{
- return error_show(1004,"申请新建失败");
- }
- }
- /**退款列表
- * //page size tradNo logNo apply_id apply_name type status returnCode
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function returnList(){
- $condition=[["a.is_del","=",0]];
- $roleid = $this->roleid;
- $check = checkRole($roleid,'90');
- if($check){
- $condition[]=["a.apply_id","=",$this->uid];
- }
- $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
- $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
- $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
- $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
- $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
- $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
- $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
- $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
- $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
- $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
- $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
- if($companyNo!==""){
- $condition[]=["a.companyNo","=",$companyNo];
- }
- $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
- if($relaComNo!=""){
- $condition[]=["a.companyNo","=",$relaComNo];
- }
- if($tradNo!=''){
- $condition[]=["a.tradNo","like","%$tradNo%"];
- }
- if($orderCode!=''){
- $condition[]=["b.orderCode","like","%$orderCode%"];
- }
- if($logNo!=''){
- $condition[]=["a.logNo","like","%$logNo%"];
- }
- if($apply_id!=''){
- $condition[]=["a.apply_id","=",$apply_id];
- }
- if($apply_name!=''){
- $condition[]=["a.apply_name","like","%$apply_name%"];
- }
- if($returnCode!=''){
- $condition[]=["a.returnCode","like","%$returnCode%"];
- }
- if($type!=''){
- $condition[]=["a.type","=",$type];
- }
- if($status!==''){
- $condition[]=["a.status","=",$status];
- }
- $count =Db::name("trade_return")->alias("a")
- ->leftJoin("assoc b","a.logNo=b.viceCode and b.type=2")
- ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode and c.is_del=0")
- ->where($condition)->count();
- $total=ceil($count/$size);
- $page = $page>$total ? intval($total):$page;
- $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode and b.type=2")
- ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode and c.is_del=0")
- ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName,c.customerNo")
- ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
- return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
- }
- //退款状态审核 0 待审核 1 财务审核 2 财务驳回
- public function returnStatus(){
- $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
- if($returnCode==''){
- return error_show(1004,"参数 returnCode 不能为空");
- }
- $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
- if($returninfo==false){
- return error_show(1004,"退款申请数据未找到");
- }
- $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
- if($status==''){
- return error_show(1004,"参数 status 不能为空");
- }
- $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
- $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
- $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
- if($loginfo==false){
- return error_show(1004,"认领资金信息未找到");
- }
- if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
- Db::startTrans();
- try{
- $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
- $up =Db::name("trade_return")->where($returninfo)->update($update);
- if($up){
- if($status==1){
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
- ->select()->toArray();
- if(!empty($qrdArr)){
- $cxCodeArr=[];
- foreach ($qrdArr as $value){
- $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,cxCode")->findOrEmpty();
- if(empty($qrd)){
- Db::rollback();
- return error_show(1005,"未找到销售单数据");
- }
- if($returninfo['type']==2){
- if($qrd['apay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单已付金额不足");
- }
- $update =[
- "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
- "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
- "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
- "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- }else{
- if($qrd['pay_fee']<$value['cancel_fee']){
- Db::rollback();
- return error_show(1005,"销售单付款中金额不足");
- }
- $update =[
- "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
- "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
- "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
- "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- }
- $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
- if($qrdup==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $asscup =[
- "status"=>3,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $assc=Db::name("assoc")->where($value)->update($asscup);
- if($assc==false){
- Db::rollback();
- return error_show(1005,"销售单更新失败");
- }
- $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
- if($report)$report->setField("returnTrad",$returnCode);
- if($qrd['cxCode']!='' && $update['pay_status']==1){
- $cxCodeArr[]=$qrd['cxCode'];
- }
- }
- if(!empty($cxCodeArr)){
- \app\admin\model\ComonOrder::CheckCxCode($cxCodeArr);
- }
- }
- $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
- $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
- if($upde==false){
- Db::rollback();
- return error_show(1005,"资金认领信息更新失败");
- }
- // if($returninfo['type']==1){
- $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
- if($trade==false){
- Db::rollback();
- return error_show(1005,"资金信息未找到");
- }
- if($trade['used_fee']<$loginfo['total_fee']){
- Db::rollback();
- return error_show(1005,"资金信息已认领金额不足");
- }
- $tradup =[
- "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
- "balance"=>$trade['balance']+$loginfo['total_fee'],
- "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $updaT=Db::name("trade")->where($trade)->update($tradup);
- if($updaT==false){
- Db::rollback();
- return error_show(1005,"资金信息更新失败");
- }
- // }
- }
- Db::commit();
- return app_show(0,"退款申请审核成功");
- }
- Db::rollback();
- return error_show(1004,'审核状态失败');
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- //退款申请详情
- public function returnInfo(){
- $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
- if($returnCode==''){
- return error_show(1004,"参数 returnCode 不能为空");
- }
- $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
- if($returninfo==false){
- return error_show(1004,"退款申请数据未找到");
- }
- $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
- $returninfo['trade_out']= $trade['trade_out']??"";
- $returninfo['trade_in']= $trade['trade_in']??"";
- $returninfo['trade_bank']= $trade['trade_bank']??"";
- $returninfo['trade_time']= $trade['trade_time']??"";
- $returninfo['customerNo']= $trade['customerNo']??"";
- $returninfo['companyNo']= $trade['companyNo']??"";
- $returninfo['total_fee']= $trade['total_fee']??"";
- $returninfo['balance']= $trade['balance']??"";
- $returninfo['used_fee']= $trade['used_fee']??"";
- $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
- $returninfo['log_total_fee']= $pool['total_fee']??"";
- $returninfo['log_apply_id']= $pool['apply_id']??"";
- $returninfo['log_apply_name']= $pool['apply_name']??"";
- $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
- ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
- ->order("a.addtime desc")
- ->field("c.*")
- ->findOrEmpty();
- $returninfo['orderinfo'] = $orderinfo;
- return app_show(0,"获取成功",$returninfo);
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function importTrade()
- {
- $files = $this->request->file("excel");
- $data = upload($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, '第'.($key+1).'行收款方公司编号不能为空!');
- }
- if (!isset($value[4]) || $value[4] == '') {
- // return ['code' => 1003, "msg" => '交易行名不能为空'];
- return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
- }
- if (!isset($value[8]) || $value[8] == '') {
- // return ['code' => 1003, "msg" => '对方账户不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
- }
- if (!isset($value[9]) || $value[9] == '') {
- // return ['code' => 1003, "msg" => '对方户名不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
- }
- if ($key == 0) {
- if ($value[0] != '收款方公司编号') {
- // 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[8] != '对方账号') {
- // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
- return error_show( 1003, '模板第九列为必须为对方账号!');
- }
- if ($value[9] != '对方户名') {
- // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
- return error_show( 1003, '模板第十列为必须为对方户名!');
- }
- continue;
- }
- $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
- if(empty($company)){
- return error_show( 1003, "业务公司编号不存在{$value[0]}");
- }
- $total_fee = $value[4];
- $type = 0;
- $time = explode(" ", $value[3]);
- $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[6];
- $list[$key]["trade_account"] = $value[8];
- $list[$key]["trade_out"] = $value[9];
- $list[$key]["trade_in"] = $value[1]??$company['company_name'];
- $list[$key]["trade_type"] = $type;
- $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
- $list[$key]["trade_remark"] = '';
- $list[$key]["companyNo"] = $value[0];
- $list[$key]["trade_in_account"] = $value[2]??"";
- }
- if(empty($list)){
- return error_show( 1003, '导入数据不能为空!');
- }
- Db::startTrans();
- try{
- $tra=[];
- foreach ($list as $value) {
- $temp = [];
- $temp["tradNo"] = makeStr('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_in_account'] = $value['trade_in_account'];
- $temp['companyNo'] = $value['companyNo'];
- $temp['trade_used'] = $value['trade_used'];
- $temp['trade_remark'] = $value['trade_remark'];
- $temp['balance'] =$value['trade_fee'];
- $temp['apply_id'] = $this->uid;
- $temp['apply_name'] = $this->uname;
- $temp['addtime'] = date("Y-m-d H:i:s");
- $temp['updatetime'] = date("Y-m-d H:i:s");
- $tra[]=$temp;
- }
- $list = Db::name('trade')->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());
- }
- }
- /**
- * 资金导入
- * @return \think\response\Json|void
- */
- public function importTradeByArr()
- {
- $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
- if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
- $list = [];
- foreach ( $trade as $key => $value) {
- if (!isset($value[0]) || $value[0] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
- }
- if (!isset($value[2]) || $value[2] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
- }
- if (!isset($value[3]) || $value[3] == '') {
- //return ['code' => 1003, "msg" => '交易时间不能为空'];
- return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
- }
- if (!isset($value[4]) || $value[4] == '') {
- // return ['code' => 1003, "msg" => '交易行名不能为空'];
- return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
- }
- if (!isset($value[6]) || $value[6] == '') {
- // return ['code' => 1003, "msg" => '交易行名不能为空'];
- return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
- }
- if (!isset($value[8]) || $value[8] == '') {
- // return ['code' => 1003, "msg" => '对方账户不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
- }
- if (!isset($value[9]) || $value[9] == '') {
- // return ['code' => 1003, "msg" => '对方户名不能为空'];
- return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
- }
- $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
- if(empty($company)){
- return error_show( 1003, "业务公司编号不存在{$value[0]}");
- }
- $total_fee = $value[4];
- $type = 0;
- $time = explode(" ", $value[3]);
- $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[6];
- $list[$key]["trade_account"] = $value[8];
- $list[$key]["trade_out"] = $value[9];
- $list[$key]["trade_in"] = $value[1]??$company['company_name'];
- $list[$key]["trade_type"] = $type;
- $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
- $list[$key]["trade_remark"] = '';
- $list[$key]["companyNo"] = $value[0];
- $list[$key]["trade_in_account"] = $value[2]??"";
- }
- if(empty($list)){
- return error_show( 1003, '导入数据不能为空!');
- }
- Db::startTrans();
- try{
- $tra=[];
- $i=0;
- foreach ($list as $value) {
- $temp = [];
- $i++;
- $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
- $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_in_account'] = $value['trade_in_account'];
- $temp['companyNo'] = $value['companyNo'];
- $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')->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());
- }
- }
- //批量认领资金,跟订单相关
- public function importTradeByBatchOrderCode()
- {
- $list = $this->request->post('list', [], 'trim');
- $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
- if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
- $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
- $val_item = Validate::rule([
- 'companyNo|卖出方公司编号' => 'require|max:255',
- 'tradNo|资金编号' => 'require|max:255',
- 'orderCode|订单编号' => 'require|max:255',
- 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
- ]);
- foreach ($list as $key => $value) {
- if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
- if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
- }
- //判断绑定公司
- $companyNo = Db::name('user_role')
- ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
- ->whereIn('companyNo', $company)
- ->column('companyNo');
- $tmp = array_diff($company, $companyNo);//二者取差集
- if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
- Db::startTrans();
- try {
- $i=1;
- //批量资金认领
- foreach ($list as $key=>$value){
- //资金明细判断
- $i++;
- $all_trade = Db::name('trade')
- ->where('is_del', 0)
- ->whereIn('status', [1, 2])
- ->where('tradNo', $value['tradNo'])
- ->field('id,balance,used_fee,companyNo,trade_time')
- ->findOrEmpty();
- if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
- if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
- if($all_trade['companyNo']!=$value['companyNo'])throw new Exception("{$value['tradNo']}资金与卖出方公司不一致");
- //订单明细判断
- $all_order=Db::name('qrd_info')
- ->where('is_del', 0)
- ->whereIn('sequenceNo',$value['orderCode'])
- ->field('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode')
- ->findOrEmpty();
- if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
- if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
- if($all_order['companyNo']!=$value['companyNo'])throw new Exception("{$value['orderCode']}资金与卖出方公司不一致");
- $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
- $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
- //资金数据更新处理
- $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
- $tradeUp=Db::name('trade')
- ->where($all_trade)
- ->update([
- 'balance' => $balance,
- 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
- 'status' => $balance == 0 ? 3 : 2,
- 'updatetime' =>date("Y-m-d H:i:s"),
- ]);
- if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
- //订单数据更新处理
- $update=[
- 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
- 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
- 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
- 'status' => 1,
- 'updatetime' => date('Y-m-d H:i:s')
- ];
- $orderUp=Db::name('qrd_info')
- ->where($all_order)
- ->update($update);
- if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
- if($update['pay_status']==3){
- (new \app\admin\model\ComonOrder())->where(['cxCode'=>$all_order['cxCode'],'status'=>-1])->update(['status'=>0]);
- }
- //资金订单关联数据
- $assoc_insert_data[]=[
- 'assocNo' => $OrderCodeToAssocNo,
- 'apply_id' => $this->uid,
- 'apply_name' => $this->uname,
- 'type' => 2,
- 'orderCode' => $value['orderCode'],
- 'customerNo' => $all_order['customerNo'],
- 'viceCode' => $OrderCodeToLogNo,
- 'order_total' => $all_order['totalPrice'],
- 'vice_total' => $value['trad_fee'],
- 'cancel_fee' => $value['trad_fee'],
- 'status' => 2,//认领通过
- 'addtime' => date("Y-m-d H:i:s"),
- 'updatetime' => date("Y-m-d H:i:s"),
- ];
- //资金认领数据
- $trade_pool_insert_data[]=[
- 'logNo' => $OrderCodeToLogNo,
- 'tradNo' => $value['tradNo'],
- 'platform_type' => $all_order['platform_type'],
- 'companyNo' => $value['companyNo'],
- 'customerNo' => $all_order['customerNo'],
- 'apply_id' => $this->uid,
- 'apply_name' => $this->uname,
- 'trade_time' => $all_trade['trade_time'],
- 'total_fee' => $value['trad_fee'],
- 'status' => 2,//2审核通过
- 'addtime' => date("Y-m-d H:i:s"),
- 'updatetime' => date("Y-m-d H:i:s"),
- ];
- $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
- if($report){
- $report->setField("logNo",$OrderCodeToLogNo);
- $report->setField("tradNo",$value['tradNo']);
- }
- }
- if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
- if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
- Db::commit();
- return app_show(0, '批量认领资金成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
- }
- }
- public function exportTrade(){
- $param = $this->request->param(["name"=>"","bank"=>'',"start"=>'',"end"=>'',"status"=>'',"tradNo"=>'',
- "companyNo"=>'',"total_lower"=>'','total_upper'=>'','pcCode'=>'',"create_start"=>"","create_end"=>""],"post","trim");
- $condition=[["is_del","=",0]];
- $param['name']==""?: $condition[]=["trade_out","like","%{$param['name']}%"];
- $param['bank']==""?: $condition[]=['trade_bank','like',"%{$param['bank']}%"];;
- $param['start']==""?: $condition[]=['trade_time','>=',startTime($param['start'])];
- $param['end']==""?: $condition[]=['trade_time','<=',endTime($param['end'])];
- $param['status']===""?: $condition[]=["status","=",$param['status']];
- $param['tradNo']==""?: $condition[]=["a.tradNo","like","%{$param['tradNo']}%"];
- $param['companyNo']==""?: $condition[]=['companyNo','=',$param['companyNo']];
- $param['total_lower']==""?: $condition[]=['total_fee','>=',$param['total_lower']];
- $param['total_upper']==""?: $condition[]=['total_fee','<=',$param['total_upper']];
- $param['pcCode']==""?: $condition[]=['pcCode',"like","%{$param['pcCode']}%"];
- $param['create_start']==""?: $condition[]=['a.addtime','>=',startTime($param['create_start'])];
- $param['create_end']==""?: $condition[]=['a.addtime','<=',endTime($param['create_end'])];
- $list= \app\admin\model\Trade::alias("a")
- ->where($condition)
- ->leftJoin("trade_lib b","a.tradNo=b.tradNo")
- ->field(["a.tradNo","companyNo","companyName","trade_in_code","trade_in",'trade_in_account','trade_time',
- 'total_fee',"if(trade_type=0,'自营','平台') plat","if(status=1,'未认领',if(status=2,'部分认领','已认领')) status",
- "used_fee","balance",'trade_bank','trade_account','trade_out','trade_used',"poCode","NdCode","customerName",
- "pcCode","palt_po","skuCode","goodNo","goodName","goodNum","platPrice","plat_total","tax","unit","cat_code","cat_name","comPrice",
- "com_total","apply_name","addtime"])
- ->select();
- $header = ["资金编号","业务公司编号","业务公司名称","收款方公司编码","收款方公司名称","收款方账户","交易时间","业务公司收入总价",
- "回款类型","状态","已认领金额","未认领金额","客户付款银行","客户付款银行账号","客户付款银行户名","交易用途","客户PO","客户DN",
- "客户机构名称","资金批次号","平台采购PO号","泰e购SKU",'商品唯一编号',"平台物料名称","产品数量","平台收入单价","平台收入总价","税率",
- "单位","类目编号","类目名称","业务公司收入单价","业务公司收入总价","创建人","创建时间"];
- if($list->isEmpty()){
- $header=["暂无数据"];
- }
- excelExport("资金管理导出",$header,$list->toArray());
- }
- }
|