<?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)){
        $rData=[];
        $balance=$acc->employeeCommissionBalance($eID,0,$from);
        $serial=1;
        $rData[]=[
            's'=>'',
            'h'=>'',
            'd'=>'',
            'p'=>['t'=>'Opening','b'=>1],
            'r'=>'',
            'u'=>'',
            'i'=>'',
            'o'=>'',
            'b'=>$general->numberFormat($balance),
        ];


        $statment=$db->selectAll($general->table(24),'where eID='.$eID.' and scoDate between '.$from.' and '.$to.' order by scoDate asc');

        $tIn=0;
        $tOut=0;
        if(!empty($statment)){
            $sIDs=[];
            foreach($statment as $t){
                if($t['sID']>0){
                    $sIDs[$t['sID']]=$t['sID'];
                }
            }
            $sale=$db->selectAll($general->table(14),'where sID in('.implode(',',$sIDs).')','sID,rtID','array',$jArray);
            $general->arrayIndexChange($sale,'sID');
            $routs=$db->selectAll($general->table(5),'','rtID,rtTitle');
            $general->arrayIndexChange($routs,'rtID');
            $users=$db->allUsers();
            foreach($statment as $s){
                $balance+=$s['scoOut'];
                $balance-=$s['scoCollect'];
                $tIn+=$s['scoOut'];
                $tOut+=$s['scoCollect'];
                $rData[]=[
                    's'=>$serial++,
                    'd'=>$general->make_date($s['scoDate']).' '.$s['scodID'],
                    'p'=>$s['scoRemarks'],
                    'r'=>@$routs[$sale[$s['sID']]['rtID']]['rtTitle'],
                    'u'=>@$users[$s['createdBy']]['username'].' '.$s['createdBy'],
                    'i'=>$general->numberFormat($s['scoOut']),
                    'o'=>$general->numberFormat($s['scoCollect']),
                    'b'=>$general->numberFormat($balance),
                ];
            }
        }

        $rData[]=[
            's'=>'',
            'd'=>['t'=>'Total','b'=>1,'col'=>4],
            'p'=>['t'=>false],
            'r'=>['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],
        ];

        $fileName='purRep_'.TIME.rand(0,999).'.txt';
        $report_data=array(
            'name'      => 'HeadStatement'.date('d_m_Y',$from).'_'.date('d_m_Y',$to),
            'title'     => 'Head Statment',
            //'info'      => $reportInfo,
            'fileName'  => $fileName,
            'head'=>array(
                array('title'=>"#"          ,'key'=>'s'),
                array('title'=>"Date Time"  ,'key'=>'d'),
                array('title'=>"Particular" ,'key'=>'p'),
                array('title'=>"Route"      ,'key'=>'r'),
                array('title'=>"User"       ,'key'=>'u'),
                array('title'=>"Due"        ,'key'=>'i'    ,'al'=>'r'),
                array('title'=>"Collect"    ,'key'=>'o'     ,'al'=>'r'),
                array('title'=>"Balance"    ,'key'=>'b'    ,'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;   
    }
    $jArray['m']=show_msg('y');
    $general->jsonHeader($jArray);
?>