<?php
    $dRange=$_POST['dRange'];
    $general->getFromToFromStringt($dRange,$from,$to);
    $eID= intval($_POST['eID']);
    $e=$smt->employeeInfoByID($eID);
    if(empty($e)){$error=fl();setMessage(63,'Employee');}

    if(!isset($error)){
        $colDelstatus=$db->permission(3);
        $rData=[];
        $balance=$acc->employeeDueBalance($eID,0,$from);
        $serial=1;
        $rData[]=[
            's'=>'',
            'h'=>'',
            'd'=>'',
            'p'=>['t'=>'Opening','b'=>1],
            'u'=>'',
            'i'=>'',
            'o'=>'',
            'b'=>$general->numberFormat($balance),
        ];


        $statment=$db->selectAll($general->table(20),'where sdRef='.$eID.' and sdDate between '.$from.' and '.$to.' order by sdDate asc');
        $tIn=0;
        $tOut=0;
        if(!empty($statment)){
            $sIDs=[];
            $supIDs=[];
            foreach($statment as $s){
                $sIDs[$s['sID']]=$s['sID'];
                $supIDs[$s['supID']]=$s['supID'];
            }
            $sale=$db->selectAll($general->table(14),'where sID in('.implode(',',$sIDs).')','sID,sDsr');
            $general->arrayIndexChange($sale,'sID');
            $suppliers=$db->selectAll($general->table(45),'where supID in('.implode(',',$supIDs).')');
            $general->arrayIndexChange($suppliers,'supID');
            $users=$db->allUsers();
            foreach($statment as $s){
                $dsr=$smt->employeeInfoByID($sale[$s['sID']]['sDsr']);
                $balance+=$s['sdAmount'];
                $balance-=$s['sdCollect'];
                $tIn+=$s['sdAmount'];
                $tOut+=$s['sdCollect'];
                $data=[
                    's'=>$serial++,
                    'd'=>$general->make_date($s['sdDate'],'time'),
                    'p'=>$s['sdRemarks'],
                    'dsr'=>$dsr['eName'],
                    'u'=>@$users[$s['createdBy']]['username'],
                    'su'=>@$suppliers[$s['supID']]['supName'],
                    'i'=>$general->numberFormat($s['sdAmount']),
                    'o'=>$general->numberFormat($s['sdCollect']),
                    'b'=>$general->numberFormat($balance),
                ];
                if($colDelstatus==true){
                    $data['dl']='';
                    if($s['sdCollect']>0){
                        if($s['sID']==0){
                            $data['dl']='<a href="javascript:void()" onclick="deleteDueCollect('.$s['sdID'].')">X</a>';
                        }
                    }
                }
                $rData[]=$data;
            }
        }

        $rData[]=[
            's'=>'',
            'd'=>['t'=>'Total','b'=>1,'col'=>4],
            'p'=>['t'=>false],
            'dsr'=>['t'=>false],
            'u'=>['t'=>false],
            'i'=>['t'=>$general->numberFormat($tIn),'b'=>1],
            'o'=>['t'=>$general->numberFormat($tOut),'b'=>1],
            'b'=>['t'=>$general->numberFormat($balance),'b'=>1],
        ];
        $headData=array(
            array('title'=>"#"          ,'key'=>'s'),
            array('title'=>"Date Time"  ,'key'=>'d'),
            array('title'=>"Particular" ,'key'=>'p'),
            array('title'=>"DSR"        ,'key'=>'dsr'),
            array('title'=>"User"       ,'key'=>'u'),
            array('title'=>"Suppliers"  ,'key'=>'su'),
            array('title'=>"Due"        ,'key'=>'i'    ,'al'=>'r'),
            array('title'=>"Collect"    ,'key'=>'o'     ,'al'=>'r'),
            array('title'=>"Balance"    ,'key'=>'b'    ,'al'=>'r'));
        if($colDelstatus==true){
            $headData[]=['title'=>'X','key'=>'dl'];
        }
        $fileName='purRep_'.TIME.rand(0,999).'.txt';
        $report_data=array(
            'name'      => 'HeadStatement'.date('d_m_Y',$from).'_'.date('d_m_Y',$to),
            'title'     => 'Head Statement',
            //'info'      => $reportInfo,
            'fileName'  => $fileName,
            'head'=>$headData,
            '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;   
    }
    $jArray['m']=show_msg('y');
    $general->jsonHeader($jArray);
?>