<?php
$dRange         = $_POST['dRange'];
$department_id  = intval($_POST['department_id']);
$type           = @intval($_POST['type'])==1?1:0;
$reportInfo=['Date :'.$dRange];
$general->getFromToFromString($dRange,$from,$to);

$first_time_from = strtotime(date("Y-m-01", $from));
$last_time_to = strtotime(date("Y-m-t 23:59:59", $to)); 

$allowance=$db->selectAll('salary_allowance','where isActive=1');


$expTypes=[
    V_T_EMPLOYEE_PAY
];
$expences=$acc->voucherDetails($expTypes,'',$first_time_from,$last_time_to);
$jArray[__LINE__]=$expences;
$tSalary=0;
if($type==0){
    $empHeads=[];
    $total=0;
    $rData=[];
   /* foreach($expences as $ex){
        if(!isset($empHeads[$ex['debit']])){
            $empHeads[$ex['debit']]=0;
        }
        $empHeads[$ex['debit']]+=$ex['amount'];
        $total+=$ex['amount'];
    }
    if(!empty($empHeads)){
        $employees=$db->selectAll($general->table(74),'where hID in('.implode(',',array_keys($empHeads)).')','','array',$jArray);
        $general->arrayIndexChange($employees,'hID');
        $jArray[__LINE__]=$employees;
        $serial=1;
        $tSalary=0;
        foreach($empHeads as $k=>$eh){
            $e=$employees[$k];
            $tSalary+=@$e['eSalary'];
            $diff=@$e['eSalary']-$eh;
            $rData[]=[
                's'=>$serial++,
                'e'=>@$e['eName'],
                'sl'=>@$e['eSalary'],
                'a'=>$general->numberFormat($eh),
                'd'=>$general->numberFormat($diff),
                'st'=>'<a href="'.URL.'/?mdl=headStatement&hID='.$e['hID'].'&scId='.$e['scID'].'" class="btn btn-success" target="_blank">Statement</a>'
            ];
        }
    }
    $rData[]=[
        's' => '',
        'e' => ['t'=>'Total','b'=>1],
        'sl'=> ['t'=>$general->numberFormat($tSalary),'b'=>1],
        'a' => ['t'=>$general->numberFormat($total),'b'=>1],
        'd' => ['t'=>$general->numberFormat($tSalary-$total),'b'=>1],
        'st'=> ''
    ];*/

    $head=[
        ['title'=>'SL','key'=>'s','hw'=>5],
        ['title'=>'Month','key'=>'e'],
        ['title'=>'Basic Salary','key'=>'bs','al'=>'r'],
    ];
    
    foreach($allowance as $b){
        $head[]=['title'=>$b['title'],'key'=>'b'.$b['id'],'al'=>'r'];
    }
    $head[]=['title'=>'Total','key'=>'t','al'=>'r'];
    $head[]=['title'=>'Payment','key'=>'p','al'=>'r'];
    $head[]=['title'=>'Balance','key'=>'ba','al'=>'r'];
    $head[]=['title'=>'Payment Mode','key'=>'pm','al'=>'r'];

    $fileName='salary_payment_report_'.TIME.rand(0,999).'.txt';
    $report_data=array(
        'name'      => 'empSalaryReport',
        'title'     => 'Employee Salary Report',
        'info'      => $reportInfo,
        'fileName'  => $fileName,
        'head'=>$head,
        'data'=>$rData
    );
}
else{
    $empHeads=[];
    $total=0;
    $serial=1;
    $eIDs=[];
    if(!empty($expences)){
        foreach($expences as $ex){
            $eIDs[$ex['debit']]=$ex['debit'];
        }
        $employees=$db->selectAll($general->table(74),'where hID in('.implode(',',$eIDs).')','','array',$jArray);
        $general->arrayIndexChange($employees,'hID');
        foreach($expences as $ex){
            $e=$employees[$ex['debit']];
            if($eID>0){
                if($e['eID']!=$eID){continue;}
            }

            $rData[]=[
                's'=>$serial++,
                'd'=>$general->make_date($ex['veTime'],'time'),
                'e'=>@$e['eName'],
                'p'=>$ex['particular'],
                'a'=>$general->numberFormat($ex['amount']),
            ];
            $total+=$ex['amount'];
        }    
    }    
    $rData[]=[
        's' => '',
        'd' => ['t'=>'Total','b'=>1,'col'=>3],
        'e'=>['t'=>false],
        'p'=>['t'=>false],
        'a' => ['t'=>$general->numberFormat($total),'b'=>1]
    ];

    $fileName='empSalaryReport_'.TIME.rand(0,999).'.txt';
    $report_data=array(
        'name'      => 'empSalaryReport',
        'title'     => 'Employee Salary Report',
        'info'      => $reportInfo,
        'fileName'  => $fileName,
        'head'=>array(
            array('title'=>"SN"             ,'key'=>'s'),
            array('title'=>"Date"           ,'key'=>'d'),
            array('title'=>"Employee"       ,'key'=>'e'),
            array('title'=>"Particular"     ,'key'=>'p'),
            array('title'=>"Amount" ,'key'=>'a'     ,'al'=>'r')
        ),
        'data'=>$rData
    );
}
$jArray[__LINE__]=$report_data;
$gAr['report_data']= $report_data;
textFileWrite(json_encode($report_data),ROOT_DIR.'/print_file/'.$fileName);
$jArray['html']     = $general->fileToVariable(ROOT_DIR.'/ajax/report.phtml');
$jArray['status']=1;
?>