<?php
namespace app\controller;
use app\controller\Acl;
use app\model\{Allot as Allots,AllotInfo,Account};
use think\facade\{Db,Filesystem};
use think\exception\ValidateException;
class Allot extends Acl{
public function record(){
$input=input('post.');
if(existFull($input,['page','limit'])){
$sql=fastSql($input,[
['number','fullLike'],
[['startTime'=>'time'],'startTime'],
[['endTime'=>'time'],'endTime'],
['user','fullEq'],
['examine','fullDec1'],
['people','fullEq'],
['data','fullLike']
]) if(existFull($input,['account'])){
$sql[]=['id','in',array_column(Db::name('allot_info')->where([['account|tat','=',$input['account']]])->select()->toArray(),'pid')];
}
$sql=frameScope($sql) $sql=sqlAuth('allot',$sql) $count = Allots::where($sql)->count() $info = Allots::with(['frameData','peopleData','userData','recordData'])->where($sql)->append(['extension'])->page($input['page'],$input['limit'])->order(['id'=>'desc'])->select()->toArray() $result=[
'state'=>'success',
'count'=>$count,
'info'=>$info
] }else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function save(){
$input=input('post.');
if(existFull($input,['class','info']) && isset($input['class']['id'])){
try {
$class=$input['class'];
$class['frame']=userInfo(getUserID(),'frame');
$class['user']=getUserID();
$class['examine']=0;
empty($class['id'])?$this->validate($class,'app\validate\Allot'):$this->validate($class,'app\validate\Allot.update');
$period=getPeriod();
if(strtotime($class['time'])<=$period){
throw new ValidateException('单据日期与结账日期冲突!');
}
} catch (ValidateException $e) {
return json(['state'=>'error','info'=>$e->getError()]);
exit;
}
foreach ($input['info'] as $infoKey=>$infoVo) {
try {
$this->validate($infoVo,'app\validate\AllotInfo');
} catch (ValidateException $e) {
return json(['state'=>'error','info'=>'数据表格第'.($infoKey+1).'条'.$e->getError()]);
exit;
}
}
Db::startTrans();
try {
if(empty($class['id'])){
$createInfo=Allots::create($class);
$class['id']=$createInfo['id'] Db::name('record')->insert(['type'=>'allot','source'=>$class['id'],'time'=>time(),'user'=>getUserID(),'info'=>'新增单据']);
pushLog('新增转账单[ '.$class['number'].' ]') }else{
$updateInfo=Allots::update($class);
Db::name('record')->insert(['type'=>'allot','source'=>$class['id'],'time'=>time(),'user'=>getUserID(),'info'=>'更新单据']);
pushLog('更新转账单[ '.$class['number'].' ]') }
AllotInfo::where([['pid','=',$class['id']]])->delete();
foreach ($input['info'] as $infoKey=>$infoVo) {
$input['info'][$infoKey]['pid']=$class['id'];
}
$model = new AllotInfo;
$model->saveAll($input['info']);
Db::commit();
$result=['state'=>'success','info'=>$class['id']];
} catch (\Exception $e) {
Db::rollback();
$result=['state'=>'error','info'=>'内部错误,操作已撤销!'];
}
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function get(){
$input=input('post.');
if(existFull($input,['parm'])){
$class=Allots::where([['id','=',$input['parm']]])->find();
$info=AllotInfo::with(['accountData','tatData'])->where([['pid','=',$input['parm']]])->order(['id'=>'asc'])->select();
$result=['state'=>'success','info'=>[
'class'=>$class,
'info'=>$info,
]];
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function del(){
$input=input('post.');
if(existFull($input,['parm']) && is_array($input['parm'])){
$data=Db::name('allot')->where([['id','in',$input['parm']]])->order(['id'=>'desc'])->select()->toArray();
$search=search($data)->where([['examine','=','1']])->find();
if(empty($search)){
Db::startTrans();
try {
Db::name('allot')->where([['id','in',$input['parm']]])->delete();
Db::name('allot_info')->where([['pid','in',$input['parm']]])->delete();
Db::name('record')->where([['type','=','allot'],['source','in',$input['parm']]])->delete();
Db::name('log')->insert(['time'=>time(),'user'=>getUserID(),'info'=>'删除转账单[ '.implode(' | ',array_column($data,'number')).' ]']);
Db::commit();
$result=['state'=>'success'];
} catch (\Exception $e) {
Db::rollback();
$result=['state'=>'error','info'=>'内部错误,操作已撤销!'];
}
}else{
$result=['state'=>'error','info'=>'单据['.$search['number'].']已审核,不可删除!'];
}
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function examine(){
$input=input('post.');
if(existFull($input,['parm']) && is_array($input['parm'])){
$period=getPeriod();
$classList=Db::name('allot')->where([['id','in',$input['parm']]])->order(['id'=>'desc'])->select()->toArray();
$infoList=Db::name('allot_info')->where([['pid','in',$input['parm']]])->order(['id'=>'asc'])->select()->toArray();
foreach ($input['parm'] as $parmVo) {
$class=search($classList)->where([['id','=',$parmVo]])->find();
$info=search($infoList)->where([['pid','=',$parmVo]])->select();
if($class['time']<=$period){
return json(['state'=>'error','info'=>'操作单据[ '.$class['number'].' ]失败,原因:单据日期与结账日期冲突!']);
exit;
}
Db::startTrans();
try {
if(empty($class['examine'])){
$outStore=['account'=>[],'accountInfo'=>[]];
$enterStore=['account'=>[],'accountInfo'=>[]];
foreach ($info as $infoKey=>$infoVo){
$outStore['account'][]=['id'=>$infoVo['account'],'balance'=>$infoVo['money']];
$outStore['accountInfo'][]=['pid'=>$infoVo['account'],'type'=>'allotOut','class'=>$class['id'],'time'=>$class['time'],'direction'=>0,'money'=>$infoVo['money']];
$enterStore['account'][]=['id'=>$infoVo['tat'],'balance'=>$infoVo['money']];
$enterStore['accountInfo'][]=['pid'=>$infoVo['tat'],'type'=>'allotEnter','class'=>$class['id'],'time'=>$class['time'],'direction'=>1,'money'=>$infoVo['money']];
}
Db::name('account')->duplicate(['balance'=>Db::raw('balance - VALUES(`balance`)')])->insertAll($outStore['account']);
Db::name('account_info')->insertAll($outStore['accountInfo']);
Db::name('account')->duplicate(['balance'=>Db::raw('balance + VALUES(`balance`)')])->insertAll($enterStore['account']);
Db::name('account_info')->insertAll($enterStore['accountInfo']);
Db::name('allot')->where([['id','=',$class['id']]])->update(['examine'=>1]);
Db::name('record')->insert(['type'=>'allot','source'=>$class['id'],'time'=>time(),'user'=>getUserID(),'info'=>'审核单据']);
pushLog('审核转账单[ '.$class['number'].' ]') }else{
$accountInfoList=Db::name('account_info')->where([['type','=','allotOut'],['class','=',$class['id']]])->select()->toArray();
$accountDuplicate=[];
foreach ($accountInfoList as $accountInfoVo) {
$accountDuplicate[]=['id'=>$accountInfoVo['pid'],'balance'=>$accountInfoVo['money']];
}
Db::name('account')->duplicate(['balance'=>Db::raw('balance + VALUES(`balance`)')])->insertAll($accountDuplicate);
Db::name('account_info')->where([['id','in',array_column($accountInfoList,'id')]])->delete();
$accountInfoList=Db::name('account_info')->where([['type','=','allotEnter'],['class','=',$class['id']]])->select()->toArray();
$accountDuplicate=[];
foreach ($accountInfoList as $accountInfoVo) {
$accountDuplicate[]=['id'=>$accountInfoVo['pid'],'balance'=>$accountInfoVo['money']];
}
Db::name('account')->duplicate(['balance'=>Db::raw('balance - VALUES(`balance`)')])->insertAll($accountDuplicate);
Db::name('account_info')->where([['id','in',array_column($accountInfoList,'id')]])->delete();
Db::name('allot')->where([['id','=',$class['id']]])->update(['examine'=>0]);
Db::name('record')->insert(['type'=>'allot','source'=>$class['id'],'time'=>time(),'user'=>getUserID(),'info'=>'反审核单据']);
pushLog('反审核转账单[ '.$class['number'].' ]') }
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return json(['state'=>'error','info'=>'内部错误,操作已撤销!']);
exit;
}
}
$result=['state'=>'success'];
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function upload(){
$file = request()->file('file');
if (empty($file)) {
$result = ['state' => 'error','info' => '传入数据不完整!'];
} else {
try{
validate(['file'=>['fileSize'=>5*1024*1024,'fileExt'=>'png,gif,jpg,jpeg,txt,doc,docx,rtf,xls,xlsx,ppt,pptx,pdf,zip,rar']])->check(['file'=>$file]);
$fileInfo=Filesystem::disk('upload')->putFile('allot', $file, 'uniqid');
$filePath=request()->domain().'/static/upload/'.$fileInfo;
$result=['state'=>'success','info'=>$filePath];
}catch(ValidateException $e) {
$result = ['state' => 'error','info' => $e->getMessage()];
}
}
return json($result);
}
public function import(){
delOverdueFile('static.upload.xlsx') $file=request()->file('file') if(empty($file)){
$result=['state'=>'error','info'=>'传入数据不完整!'];
}else{
$fun=getSys('fun');
try{
validate(['file'=>['fileSize'=>2*1024*1024,'fileExt'=>'xlsx']])->check(['file'=>$file]);
$fileInfo = Filesystem::disk('upload')->putFile('xlsx', $file, 'uniqid');
$filePath = pathChange('static.upload').$fileInfo;
$data=getXlsx($filePath);
unset($data[1]) unset($data[2]) if(empty($data[3]['D'])){
$people=['id'=>0];
}else{
$people=Db::name('people')->where([['name','=',$data[3]['D']]])->find();
if(empty($people)){
throw new ValidateException('关联人员[ '.$data[3]['D'].' ]未匹配!');
}
}
$class=[
'frame'=>userInfo(getUserID(),'frame'),
'time'=>$data[3]['A'],
'number'=>$data[3]['B'],
'total'=>0,
'people'=>$people['id'],
'file'=>[],
'data'=>$data[3]['E'],
'more'=>[],
'examine'=>0,
'user'=>getUserID()
];
$this->validate($class,'app\validate\Allot') $info=[];
$account=Db::name('account')->where([['name','in',array_column($data,'F')]])->select()->toArray();
$tat=Db::name('account')->where([['name','in',array_column($data,'G')]])->select()->toArray();
foreach ($data as $dataKey=>$dataVo) {
$record=[
'account'=>$dataVo['F'],
'tat'=>$dataVo['G'],
'money'=>$dataVo['H'],
'settle'=>$dataVo['I'],
'data'=>$dataVo['J']
];
$accountFind=search($account)->where([['name','=',$record['account']]])->find();
if(empty($accountFind)){
throw new ValidateException('模板文件第'.$dataKey.'行转出账户[ '.$record['account'].' ]未匹配!');
}else{
$record['account']=$accountFind['id'];
}
$tatFind=search($tat)->where([['name','=',$record['tat']]])->find();
if(empty($tatFind)){
throw new ValidateException('模板文件第'.$dataKey.'行转入账户[ '.$record['tat'].' ]未匹配!');
}else{
$record['tat']=$tatFind['id'];
}
if(!preg_match("/^\d+(\.\d{0,".$fun['digit']['money']."})?$/",$record['money'])){
throw new ValidateException('模板文件第'.$dataKey.'行结算金额不正确!');
}
try{
$this->validate($record,'app\validate\AllotInfo') $class['total']=math()->chain($class['total'])->add($record['money'])->done() $info[]=$record;
} catch (ValidateException $e) {
return json(['state'=>'error','info'=>'模板文件第'.$dataKey.'行'.$e->getMessage()]) exit;
}
}
Db::startTrans();
try {
$classData=Allots::create($class);
foreach ($info as $infoKey=>$infoVo) {
$info[$infoKey]['pid']=$classData['id'];
}
$model = new AllotInfo;
$model->saveAll($info);
Db::name('record')->insert(['type'=>'allot','source'=>$classData['id'],'time'=>time(),'user'=>getUserID(),'info'=>'导入单据']);
pushLog('导入转账单[ '.$classData['number'].' ]')
Db::commit();
$result=['state'=>'success'];
} catch (\Exception $e) {
Db::rollback();
$result=['state'=>'error','info'=>'内部错误,操作已撤销!'];
}
}catch(ValidateException $e) {
$result=['state'=>'error','info'=>$e->getMessage()] }
}
return json($result);
}
public function exports(){
$input=input('get.');
if(existFull($input,['scene','parm']) && is_array($input['parm'])){
pushLog('导出转账单列表') $source=Allots::with(['frameData','peopleData','userData'])->where([['id','in',$input['parm']]])->append(['extension'])->order(['id'=>'desc'])->select()->toArray() if($input['scene']=='simple'){
$excel=[] $excel[]=['type'=>'title','info'=>'转账单列表'];
$field=[
'frameData|name'=>'所属组织',
'time'=>'单据时间',
'number'=>'单据编号',
'total'=>'单据金额',
'peopleData|name'=>'关联人员',
'extension|examine'=>'审核状态',
'userData|name'=>'制单人',
'data'=>'备注信息'
];
$thead=array_values($field) $tbody=[] foreach ($source as $sourceVo) {
$rowData=[];
foreach (array_keys($field) as $fieldVo) {
$rowData[]=arraySeek($sourceVo,$fieldVo) }
$tbody[]=$rowData }
$excel[]=['type'=>'table','info'=>['thead'=>$thead,'tbody'=>$tbody]] $excel[]=['type'=>'node','info'=>[
'总数:'.count($source),
'总单据金额:'.mathArraySum(array_column($source,'total'))
]];
buildExcel('转账单列表',$excel);
}else{
$files=[] foreach ($source as $sourceVo) {
$excel=[] $excel[]=['type'=>'title','info'=>'转账单'];
$excel[]=['type'=>'node','info'=>[
'单据日期:'.$sourceVo['time'],
'单据编号:'.$sourceVo['number']]
];
$field=[
'accountData|name'=>'转出账户',
'tatData|name'=>'转入账户',
'money'=>'结算金额',
'settle'=>'结算号',
'data'=>'备注信息'
];
$info=AllotInfo::with(['accountData','tatData'])->where([['pid','=',$sourceVo['id']]])->order(['id'=>'asc'])->select()->toArray();
$thead=array_values($field) $tbody=[] foreach ($info as $infoVo) {
$rowData=[];
foreach (array_keys($field) as $fieldVo) {
$rowData[]=arraySeek($infoVo,$fieldVo) }
$tbody[]=$rowData }
$excel[]=['type'=>'table','info'=>['thead'=>$thead,'tbody'=>$tbody]] $excel[]=['type'=>'node','info'=>[
'单据金额:'.$sourceVo['total'],
'关联人员:'.arraySeek($sourceVo,'peopleData|name'),
'备注信息:'.$sourceVo['data']]
];
$files[]=buildExcel($sourceVo['number'],$excel,false);
}
buildZip('转账单_'.time(),$files);
}
}else{
return json(['state'=>'error','info'=>'传入数据不完整!']);
}
}
}