<?php
    $dRange=$_POST['dRange'];
    $reportInfo=[];
    $reportInfo[]='Date : '.$dRange;
    $general->getFromToFromString($dRange,$from,$to);
    $jArray[fl()] = $general->make_date($from,'time');
    $jArray[fl()] = $general->make_date($to,'time');
    $total_in=0;                         
    $total_out=0;
    $user_ledger=$acc->get_user_head($userData,$jArray);
    $opening_balance = $acc->headBalance($user_ledger,$from-1);
    
    //$opening_balance = $acc->user_balance(USER_ID,$from);
    //$tData=$acc->cashFlowReport($from,$to,0,USER_ID);
    $statement=$acc->head_statement($user_ledger,$from,$to);
    $jArray[fl()]=$statement;
    $total=1;
    $data=[
        's' => '',
        'h' =>'Opening Balance',
        'dt'=> '',
        'p' => '',
        'i' => $general->numberFormat($opening_balance),
        'o' => '',
        'b' => $general->numberFormat($opening_balance),
    ];
    $rData[]=$data;
    $total_in+=$opening_balance;
    $balance=$opening_balance;
    if(!empty($statement)){
        foreach($statement as $tr){
            $total_in+=$tr['in'];
            $balance+=$tr['in'];
            $total_out+=$tr['out'];
            $balance-=$tr['out'];
            $data=[
                's' => $total++,
                'h' => $tr['head_title'],
                'dt'=> date('d-m-Y H:i',$tr['time']),
                'p' => $tr['note'],
                'i' => $general->numberFormat($tr['in']),
                'o' => $general->numberFormat($tr['out']),
                'b' => $general->numberFormat($balance),
            ];
            $rData[]=$data;
        }
    }
    $rData[]=array(
        's' => '',
        'h'=> array('t'=>'Total','b'=>1),
        'dt'=> array('t'=>''),
        'p' => array('t'=>''),
        'i' => ['t'=>$general->numberFormat($total_in),'b'=>1],
        'o' => ['t'=>$general->numberFormat($total_out),'b'=>1],
        'b' => ['t'=>$general->numberFormat($balance),'b'=>1],
    );
    $fileName='my_transaction_'.TIME.rand(0,999).'.txt';
    $headData=array(
        array('title'=>"#"              ,'key'=>'s' ,'w'=>5 ,'hw'=>4),
        array('title'=>"Head"           ,'key'=>'h' ),
        array('title'=>"Date time"      ,'key'=>'dt'),
        array('title'=>"Description"    ,'key'=>'p' ),
        array('title'=>"In"             ,'key'=>'i' ,'al'=>'r'),
        array('title'=>"Out"            ,'key'=>'o' ,'al'=>'r'),
        array('title'=>"Balance"        ,'key'=>'b' ,'al'=>'r'),
    );
    $reportData=array(
        'name'      => 'my_transaction_'.date('d_m_Y',$from).'_'.date('d_m_Y',$to),
        'title'     => 'my transaction',
        'info'      => $reportInfo,
        'head'      => $headData,
        'data'      => $rData
    );
    $gAr['report_data']= $reportData;

    textFileWrite(json_encode($reportData),ROOT_DIR.'/print_file/'.$fileName);
    $jArray['html']     = $general->fileToVariable(ROOT_DIR.'/ajax/report.phtml');

    $jArray['status']=1;