<?php
$customer_code=$_POST['customer_code'];
if($customer_code==''){
    $error=fl();set_message('Please enter customer code');
}

if(!isset($error)){
    $customer=$db->get_row('customers','customer_code',$customer_code);
    if(!empty($customer)){
        $jArray['status']=1;
        $jArray['customer']=[
            'id'                =>$customer['customer_code'],
            'customer_name'     =>$customer['customer_name'],
            'customer_mobile'   =>$customer['customer_mobile'],
            'customer_address'  =>$customer['customer_address'],
        ];
        
        $jArray['subscription_info']=[
            'next_range'=>'',
            'subscribes'=>[]
        ];
        $start=strtotime('today');
        $last_end=0;
        $subscribes=$db->select_all('customer_subscribe',"where customer_id=$customer[customer_id] order by start_date desc limit 50");
        if(!empty($subscribes)){
            
            foreach($subscribes as $s){
                $data=[
                    'start_date'=>date('d-m-Y',$s['start_date']),
                    'end_date'=>date('d-m-Y',$s['end_date']),
                    'amount'=>(float)$s['amount'],
                    'created_on'=>date('d-m-Y h:i:s',$s['created_on'])
                ];
                if($last_end<$s['end_date']){
                    $last_end=$s['end_date'];
                }
                $jArray['subscription_info']['subscribes'][]=$data;
            }
            if($last_end>0){
                $last_end=strtotime(date('d-m-Y',$last_end));
                $start=strtotime('+1 day',$last_end);
            }
            $end=strtotime('+30 day',$start);
            $jArray['subscription_info']['next_range']=date('d M Y',$start).' to '.date('d M Y',$end);
        }
    }
    else{
        $error=fl();set_message('Invalid customer code');
    }

}
