<?php

    $dRange=$_POST['dRange'];
    $customer_id = intval($_POST['customer_id']);
    $base_id = intval($_POST['base_id']);
    $transaction_type = intval($_POST['transaction_type']);
    $reportInfo=[];
    $reportInfo[]='Date : '.$dRange;
    $general->getFromToFromString($dRange,$from,$to);
    $reference='';
    if($base_id>0){
        $reference=0;
        $customers = $db->selectAll('customer','where base_id='.$base_id,'id');
        $general->arrayIndexChange($customers);
        if(!empty($customers)){
            $reference = array_keys($customers);
        }
    }
    if($customer_id>0){
        $reference =$customer_id; 
    }

    $vType  = [V_T_CUSTOMER_YEARLY_DISCOUNT, V_T_PAY_TO_CUSTOMER,V_T_RECEIVE_FROM_CUSTOMER,V_T_RECOVERABLE_ENTRY,V_T_CUSTOMER_BAD_DEBT,V_T_CUSTOMER_COLLECTION_DISCOUNT];


    if($transaction_type>0){
        $vType = $transaction_type;
    }
    $jArray[fl()]=$reference;
    $jArray[fl()]=$vType;
    $tData = $acc->voucherDetails($vType,$reference,$from,$to,[],$jArray);
    $jArray[fl()] = $tData;

    $rData=[];
    $total_in=0;                         
    $total_out=0;
    $total=1;

    if(!empty($tData)){
        $head_ids=[];
        $user_ids=[];
        foreach($tData as $d){
            $head_ids[$d['debit']] = $d['debit'];
            $head_ids[$d['credit']] = $d['credit'];
            $user_ids[$d['createdBy']] = $d['createdBy'];
        }
        $heads = $db->selectAll('a_heads','where id in('.implode(',',$head_ids).')','id,title');
        $general->arrayIndexChange($heads);
        $users = $db->selectAll('users','where id in('.implode(',',$user_ids).')','id,name');
        $general->arrayIndexChange($users);
        foreach($tData as $tr){
            $in = 0;
            $out = 0;

            if($tr['type']==V_T_PAY_TO_CUSTOMER){
                $in = $tr['amount'];
                $head_id = $tr['debit'];
                $type='Pay to customer';
            }
            else if($tr['type']==V_T_RECEIVE_FROM_CUSTOMER){
                $out = $tr['amount'];
                $head_id = $tr['credit'];
                $type='Receive from customer';
            }
            else if($tr['type']==V_T_RECOVERABLE_ENTRY){
                $out = $tr['amount'];
                $head_id = $tr['credit'];
                $type='Recoverable collection';
            }
            else if($tr['type']==V_T_CUSTOMER_BAD_DEBT){
                $out = $tr['amount'];
                $head_id = $tr['credit'];
                $type='Bad-Debt';
            }else if($tr['type']==V_T_CUSTOMER_COLLECTION_DISCOUNT){
                $out = $tr['amount'];
                $head_id = $tr['credit'];
                $type='Collection discount';
            }else if($tr['type']==V_T_CUSTOMER_YEARLY_DISCOUNT){
                $out = $tr['amount'];
                $head_id = $tr['credit'];
                $type='Yearly discount';
            }
            $total_in+=$in;
            $total_out+=$out;
            $data=array(
                's' => $total++,
                'h' => $heads[$head_id]['title'],//.' - '.$tr['id'],
                't' => $type,//.' '.$tr['id'],
                'u' => $users[$tr['createdBy']]['name']??'',
                'dt'=> date('d-m-Y H:i',$tr['time']),
                'p' => $tr['note'],
                'i' => $general->numberFormat($out),
                'o' => $general->numberFormat($in),
                'r'=>'<button onclick="are_you_sure(1,\'Are you sure you want to delete the transaction?\','.$tr['id'].',customer_transaction_remove)" class="btn btn-danger">Delete</button>'
            );
            $rData[]=$data;
        }
    }

    //$tData=$acc->cashFlowReport($from,$to,0,0,0,$vType,'',jArray: $jArray);

    $rData[]=[
        's' => '',
        'h'=> array('t'=>'Total','b'=>1,'col'=>5),
        'u' => array('t'=>false),
        't' => array('t'=>false),
        'dt'=> array('t'=>false),
        'p' => array('t'=>false),
        'o' => ['t'=>$general->numberFormat($total_in),'b'=>1],
        'i' => ['t'=>$general->numberFormat($total_out),'b'=>1],
    ];
    $fileName='customerPaymentList_'.TIME.rand(0,999).'.txt';
    $headData=array(
        array('title'=>"#"              ,'key'=>'s' ,'w'=>5 ,'hw'=>4),
        array('title'=>"Customer"       ,'key'=>'h' ),
        array('title'=>"Typ"            ,'key'=>'t' ),
        array('title'=>"User"           ,'key'=>'u' ,'w'=>10,'hw'=>8),
        array('title'=>"Date time"      ,'key'=>'dt','w'=>20,'hw'=>12),
        array('title'=>"Description"    ,'key'=>'p' ,'w'=>20),
        array('title'=>"Receive"        ,'key'=>'i' ,'w'=>20,'hw'=>8    ,'al'=>'r'),
        array('title'=>"Pay"         	,'key'=>'o' ,'w'=>20,'hw'=>8    ,'al'=>'r'),
        array('title'=>"X"         	    ,'key'=>'r' ,'hw'=>8),
    );
    $reportData=array(
        'name'      => 'customer_payment_list_'.date('d_m_Y',$from).'_'.date('d_m_Y',$to),
        'title'     => 'customer payment list',
        'info'      => $reportInfo,
        'fileName'  => $fileName,
        '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;
