<?php
class Voucher_list{
    private General $general;
    private DB $db;
    public function __construct(public SMT $smt,public ACC $acc){
        $this->general=$smt->general;
        $this->db=$smt->db;
    }
    public function get_list($request){
        $general=$this->general;
        $db=$this->db;
        $acc=$this->acc;
        
        if(!isset($request['dRange'])){
            return ['status'=>0,'msg'=>'Invalid date range'];
        }
        $dRange = $request['dRange'];
        $user_id=0;
        $head_id=0;
        $type=0;
        if(isset($request['user_id'])){
            $user_id    = intval($request['user_id']);
        }
        if(isset($request['head_id'])){
            $head_id    = intval($request['head_id']);
        }
        if(isset($request['type'])){
            $type    = intval($request['type']);
        }
        $reportInfo=["Date $dRange"];
        
        
        $general->getFromToFromString($dRange,$from,$to);
        $q=[];
        $q[]="time between $from and $to";
        if($user_id>0){
            $q[]="user_id=$user_id";
        }
        if($head_id>0){
            $q[]="head_id=$head_id";
        }

        $dStatus=$db->permission(90);
        $transactions=$db->selectAll('a_head_entry','where '.implode(' and ',$q),'','array',$jArray);
        $rData=[];
        if(!empty($transactions)){
            $head_id=[];
            $general->getIDsFromArray($transactions,'voucher_id,head_id',$voucher_ids,$head_id);
            $ledgers=$db->selectAll('a_heads','where id in('.implode(',',$head_id).')');
            $general->arrayIndexChange($ledgers);
            $voucher_entry = $db->selectAll('a_voucher_entry','where id in('.implode(',',$voucher_ids).')');
            $general->arrayIndexChange($voucher_entry);
            $general->getIDsFromArray($voucher_entry,'createdBy',$user_ids);
            $users = $db->allUsers(' and id in('.implode(',',$user_ids).')');
            $voucher_wise_data=[];
            $all_voucher_type = $acc->all_voucher_type();
            $base = $db->allBase_for_voucher();
            foreach($transactions as $t){
                if(!isset($voucher_wise_data[$t['voucher_id']])){
                    $v = $voucher_entry[$t['voucher_id']];
                    if($type>0&&$type!=$v['type']){
                        continue;
                    }
                    $u = $users[$t['createdBy']]??[];
                    $voucher_wise_data[$t['voucher_id']]=[
                        'credit'=>[],
                        'debit'=>[],
                        'amount'=>0,
                        'base'=>$base[$t['base_id']]['title']??'',
                        'user'=>$u['name']??'',
                        'note'=>$t['note']??'',
                        'time'=>$v['time'],
                        'code'=>$v['code'],
                        'type'=>$v['type'],
                        'type_title'=>$all_voucher_type[$v['type']]['title']??'',
                    ];
                }
                $ledger = @$ledgers[$t['head_id']];
                $voucher_wise_data[$t['voucher_id']]['amount']+=$t['debit'];
                ($t['debit']>0)?@$voucher_wise_data[$t['voucher_id']]['debit'][]=$ledger['title']:@$voucher_wise_data[$t['voucher_id']]['credit'][]=$ledger['title'];
            }
            $sr=1;
            $jArray[fl()]=$voucher_wise_data;
            $total_amount=0;
            foreach($voucher_wise_data as $id=>$v){
                $total_amount+=$v['amount'];
                $delete_html='';
                if($dStatus){
                    if(in_array($v['type'],REMOVE_ABLE_VOUCHER_TYPE)){
                        $delete_html='<button class="btn btn-danger delete_voucher_' . $id . '" onclick="are_you_sure(1, \'Are you sure?\', ' . $id . ', delete_voucher)">Remove</button>';
                    }
                    else{
                        $delete_html=''.$v['type'];
                    }
                }
                $rData[]=[
                    's'=>$sr++,
                    'id'=>$id,
                    'c'=>$v['code'],
                    't'=>$v['type_title'],
                    'd'=>$general->make_date($v['time']),
                    'ti'=>$general->getHourMint($v['time']),
                    'tm'=>$v['time'],
                    'b'=>$v['base'],
                    'u'=>$v['user'],
                    'n'=>$general->content_show($v['note']),
                    'dr'=>implode(' ,',$v['debit']),
                    'cr'=>implode(' ,',$v['credit']),
                    'a'=>$general->numberFormat($v['amount']),
                    'r'=>$delete_html,
                    'table_tr_id'=>"delete_voucher_$id",
                ];
            }
            $general->arraySortByColumn($rData,'tm',SORT_DESC);
            $rData[]=[
                    's'=>'',
                    'id'=>'',
                    'c'=>'',
                    't'=>'',
                    'd'=>'',
                    'ti'=>'',
                    'b'=>'',
                    'u'=>'',
                    'n'=>'',
                    'dr'=>['t'=>'Total','al'=>'r','b'=>1],
                    'cr'=>['t'=>$general->numberFormat($total_amount),'b'=>1,'col'=>2,'al'=>'r'],
                    'a'=>['t'=>false],
                    'r'=>'',
                    'table_tr_id'=>"delete_voucher",
            ];
        }
        $fileName='voucher_list_'.TIME.rand(0,999).'.txt';
        $report_data=[
            'name'      => 'voucher_list',
            'title'     => 'Voucher list',
            'info'      => $reportInfo,
            'fileName'  => $fileName,
            'head'=>[
                ['title'=>"SL"          ,'key'=>'s','hw'=>5],
                ['title'=>"Code"        ,'key'=>'c'],
                ['title'=>"Type"        ,'key'=>'t'],
                ['title'=>"Date"        ,'key'=>'d'],
                ['title'=>"Time"        ,'key'=>'ti'],
                ['title'=>"Base"        ,'key'=>'b'],
                ['title'=>"User"        ,'key'=>'u'],
                ['title'=>"Note"        ,'key'=>'n'],
                ['title'=>"Debit"       ,'key'=>'dr'],
                ['title'=>"credit"      ,'key'=>'cr'],
                ['title'=>"Amount"      ,'key'=>'a','al'=>'r'],
                ['title'=>"Remove"      ,'key'=>'r','noForExcel'=>1],

            ],
            'data'=>$rData
        ];
        $gAr['report_data']= $report_data;
        textFileWrite(json_encode($report_data),ROOT_DIR.'/print_file/'.$fileName);
        $html     = $general->fileToVariable(ROOT_DIR.'/ajax/report.phtml',$gAr);
        return [
            'status'=>1,
            'html'=>$html
        ];
    }
}