<?php
ob_start();
session_start();
include "../config/config.php";
$today_date = date("Y-m-d");


error_reporting(E_ALL);











if($_POST['get_details']=="get_customer_details"){
     $customer_id = $_POST['customer_id'];
     
     $condition="";
     $conditions="";
     
     if($customer_id!=""){
    	$condition.=" and tbl_customer_scheme.customer_id=".$customer_id;
     }
     if($customer_id!=""){
    	$conditions.=" and ad.customer_id=".$customer_id;
    	$conditions.=" and (ad.expected_date>='".$today_date."' or ad.expected_date='".$today_date."')";
     }
   

$statementss = $pdo->prepare("SELECT tbl_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
    	tbl_schemes.type as scheme_type,
cu.name as created_user_name, uu.name as updated_user_name,
(SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts,
(SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_amounts,
(SELECT SUM(scheme_weight) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_weight
FROM `tbl_customer_scheme` 
LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
    	WHERE tbl_customer_scheme.id!=0 and tbl_customer_scheme.scheme_completed_date!=0 AND tbl_customer_scheme.sale_id=0 AND tbl_customer_scheme.soft_delete=0 ".$condition."");
$statementss->execute();
$results = $statementss->fetchAll(PDO::FETCH_ASSOC);
$totalrows = $statementss->rowCount(); 

if($totalrows>0){
    $scheme_amount = $results[0]['scheme_total_amounts'];
}else{
    $scheme_amount = 0;
}





$statementsss = $pdo->prepare("SELECT ad.*, c.customer_name, c.mobile_no,
p.hsn_code, pro.name as products_name, 
(SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=4 and p.advance_id=ad.id) as advance_paid_amount,
cu.name as created_user, uu.name as updated_user
FROM `tbl_advance_voucher` ad 
LEFT JOIN tbl_customers c ON c.id=ad.customer_id
LEFT JOIN tbl_product_variants p ON p.id=ad.product_variant_id
LEFT JOIN tbl_products_name pro ON pro.id=p.product_name
LEFT JOIN tbl_users cu ON cu.id=ad.created_by
LEFT JOIN tbl_users uu ON uu.id=ad.updated_by
    	WHERE ad.id!=0 AND ad.sale_id=0 ".$conditions."");
$statementsss->execute();
$resultss = $statementsss->fetchAll(PDO::FETCH_ASSOC);
$totalrowss = $statementsss->rowCount();    

if($totalrowss>0){
    $advance_amount = $resultss[0]['advance_paid_amount'];
}else{
    $advance_amount = 0;
}
    	
      if($results){
		    $response['scheme_amount'] = $scheme_amount;
		    $response['advance_amount'] = $advance_amount;
		    $response['error']=false; 
		    $response['message']="Customer Details Fetched Successfully";
		}else{
		    $response['error']=true;
		    $response['message']='Failed';
		}
		
		echo json_encode($response);    	
    	

 
}





















if($_POST['get_details']=="get_payment_scheme_details"){
     $customer_id = $_POST['customer_id'];
     
     $condition="";
     
     if($customer_id!=""){
    	$condition.=" and tbl_customer_scheme.customer_id=".$customer_id;
     }
   
        $statement = $pdo->prepare("SELECT tbl_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
                            	tbl_schemes.type as scheme_type,
                                cu.name as created_user_name, uu.name as updated_user_name,
                                (SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts,
                                (SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_amounts,
                                (SELECT SUM(scheme_weight) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_weight
                                FROM `tbl_customer_scheme` 
                                LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
                                LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
                                LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
                                LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
                            	WHERE tbl_customer_scheme.id!=0 and tbl_schemes.type=1 AND tbl_customer_scheme.sale_id=0 AND tbl_customer_scheme.soft_delete=0 ".$condition." and tbl_customer_scheme.scheme_completed_date!=0 ORDER BY tbl_customer_scheme.id DESC LIMIT 5");
    	$statement->execute();
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		



    $statementss = $pdo->prepare("SELECT tbl_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
        	                tbl_schemes.type as scheme_type,
                            cu.name as created_user_name, uu.name as updated_user_name,
                            (SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts,
                            (SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_amounts,
                            (SELECT SUM(scheme_weight) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_weight
                            FROM `tbl_customer_scheme` 
                            LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
                            LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
                            LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
                            LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
                            WHERE tbl_customer_scheme.id!=0 and tbl_customer_scheme.scheme_completed_date!=0 and tbl_schemes.type=1 AND tbl_customer_scheme.sale_id=0 AND tbl_customer_scheme.soft_delete=0 ".$condition."");
    $statementss->execute();
    $results = $statementss->fetchAll(PDO::FETCH_ASSOC);
    $totalrows = $statementss->rowCount();    	
    	
    	
    	

		$content="<style>
		            thead{
		                background: blue;
                        color: white;
		            }
		          </style>
		          
		          
		          
		<div class='row'>
		<center><a href='customers-scheme-list.php?cus_id=".$customer_id."&type=1' class='btn btn-warning btn-xs' target='_BLANK'> View Payment Scheme Details</a></center>";
		
		
		// Last Sales - Starts Here
        if($totalrow>0){
		    $content.='
		                <div class="col-md-12" style="margin-top: 12px;">
                              <table style="width: 100%;">
                                    <thead>
                                        <tr>
                                            <th style="text-align: center;">S.No</th>
                                            <th style="text-align: center;">&nbsp;</th>
                                            <th style="text-align: center;">Scheme ID</th>
                                            <th style="text-align: center;">Scheme Amount</th>
                                            <th style="text-align: center;">Paid Amount</th>
                                            <th style="text-align: center;">Complete Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>';
		    $i=1;
		    foreach($result as $rows){
		        
		        
		        if($rows['type']==1){
		            $type = "<span class='btn btn-info btn-xs'>Payments</span>";
		        }else if($rows['type']==2){
		            $type = "<span class='btn btn-primary btn-xs'>Weight</span>";
		        }
		        
		        if($rows['scheme_months']==$rows['payment_paid_month_counts']){
		            $status = "<span class='btn btn-success btn-xs'><i class='fa fa-check'></i> Completed</span>";
		        }else {
		            $status = "<span class='btn btn-danger btn-xs'><i class='fa fa-spinner'></i> Process</span>";
		        }
		        
		        
		        
		        $content.='<tr>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$i++.'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">
		                            <input type="checkbox" name="check_ids" value="'.$rows['scheme_total_amounts'].'" onclick="SchemeWeight('.$rows['scheme_total_weight'].', '.$rows['scheme_total_amounts'].', 1, 0); handlePaymentids(this, '.$rows['id'].')" data-paid_amount="'.$rows['scheme_total_amounts'].'" class="" style="width: 20px; height: 15px;">
		                        </td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">
		                            '.$rows['scheme_no'].'
		                        </td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;"><i class="fa fa-inr"></i> <b>'.number_format($rows['scheme_amount'],2).'</b></td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;"><i class="fa fa-inr"></i> <b>'.number_format($rows['scheme_total_amounts'],2).'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.date('d-m-Y', strtotime($rows['scheme_completed_date'])).'</td>
		                   </tr>';
		    }
		    
		    $content.='</tbody>
		               </table>
		               </div>';
		               
		       
		   
		}
        else{
		    $content.="<center><b style='color: red;'>No Scheme Found</b></center>";
		}
		// Last Sales - Endsss Here
		
    if($totalrows>0){
        $content.='<div class="col-md-12" style="padding-top: 0px;">
                   <center><h3><b>Completed Scheme Details</b></h3></center>
                   
                   
                   <div class="col-md-12" style="padding-top: 0px; text-align: center;">
                        <b>Payment Scheme Amount: <i class="fa fa-inr"></i> &nbsp;&nbsp;'.number_format($results[0]['scheme_total_amounts'],2).'</b>
		           </div>
                   
                  
                   
		           </div>';        
    }
		
		$content.="</div>";
		
		
		echo $content;
 
}






















if($_POST['get_details']=="get_weight_scheme_details"){
     $customer_id = $_POST['customer_id'];
     
     $condition="";
     
     if($customer_id!=""){
    	$condition.=" and tbl_customer_scheme.customer_id=".$customer_id;
     }
   
        $statement = $pdo->prepare("SELECT tbl_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
                	tbl_schemes.type as scheme_type,
                    cu.name as created_user_name, uu.name as updated_user_name,
                    (SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts,
                    (SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_amounts,
                    (SELECT SUM(scheme_weight) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_weight
                    FROM `tbl_customer_scheme` 
                    LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
                    LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
                    LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
                    LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
                	WHERE tbl_customer_scheme.id!=0 and tbl_schemes.type=2 and tbl_customer_scheme.scheme_completed_date!=0 AND tbl_customer_scheme.sale_id=0 ".$condition." ORDER BY tbl_customer_scheme.id DESC LIMIT 5");
    	$statement->execute();
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		



    $statementss = $pdo->prepare("SELECT tbl_customer_scheme.*,c.customer_name, c.mobile_no, tbl_schemes.name as scheme_name, 
                	tbl_schemes.type as scheme_type,
                    cu.name as created_user_name, uu.name as updated_user_name,
                    (SELECT COUNT(id) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as payment_paid_month_counts,
                    (SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_amounts,
                    (SELECT SUM(scheme_weight) FROM `tbl_payments` p WHERE p.accounts=3 and p.scheme_id=tbl_customer_scheme.id) as scheme_total_weight
                    FROM `tbl_customer_scheme` 
                    LEFT JOIN tbl_customers c ON c.id=tbl_customer_scheme.customer_id
                    LEFT JOIN tbl_schemes ON tbl_schemes.id=tbl_customer_scheme.scheme_id
                    LEFT JOIN tbl_users cu ON cu.id=tbl_customer_scheme.created_by
                    LEFT JOIN tbl_users uu ON uu.id=tbl_customer_scheme.updated_by
                	WHERE tbl_customer_scheme.id!=0 and tbl_customer_scheme.scheme_completed_date!=0 and tbl_schemes.type=2 AND tbl_customer_scheme.sale_id=0 ".$condition."");
    $statementss->execute();
    $results = $statementss->fetchAll(PDO::FETCH_ASSOC);
    $totalrows = $statementss->rowCount();    	
    	
    	
    	

		$content="<style>
		            thead{
		                background: blue;
                        color: white;
		            }
		          </style>
		<div class='row'>
		<center><a href='customers-scheme-list.php?cus_id=".$customer_id."&type=2' class='btn btn-warning btn-xs' target='_BLANK'>View Weight Scheme Details</a></center>";
		
		
		// Last Sales - Starts Here
        if($totalrow>0){
		    $content.='
		                <div class="col-md-12" style="margin-top: 12px;">
                              <table style="width: 100%;">
                                    <thead>
                                        <tr>
                                            <th style="text-align: center;">S.No</th>
                                            <th style="text-align: center;"></th>
                                            <th style="text-align: center;">Scheme ID</th>
                                            <th style="text-align: center;">Scheme Amount</th>
                                            <th style="text-align: center;">Paid Amount</th>
                                            <th style="text-align: center;">Weight</th>
                                            <th style="text-align: center;">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>';
		    $i=1;
		    foreach($result as $rows){
		        
		        
		        if($rows['type']==1){
		            $type = "<span class='btn btn-info btn-xs'>Payments</span>";
		        }else if($rows['type']==2){
		            $type = "<span class='btn btn-primary btn-xs'>Weight</span>";
		        }
		        
		        if($rows['scheme_months']==$rows['payment_paid_month_counts']){
		            $status = "<span class='btn btn-success btn-xs'><i class='fa fa-check'></i> Completed</span>";
		        }else {
		            $status = "<span class='btn btn-danger btn-xs'><i class='fa fa-spinner'></i> Process</span>";
		        }
		        
		        
		        
		        $content.='<tr>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$i++.'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">
		                            <input type="checkbox" name="check_ids" value="'.$rows['scheme_total_weight'].'" onclick="SchemeWeight('.$rows['scheme_total_weight'].', '.$rows['scheme_total_amounts'].', 1, 2);  handleWeightids(this, '.$rows['id'].')" class="paid_amounts" data-paid_amount="'.$rows['scheme_total_amounts'].'" style="width: 20px; height: 15px;">
		                        </td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$rows['scheme_no'].'</td>
		                        
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;"><i class="fa fa-inr"></i> <b>'.number_format($rows['scheme_amount'],2).'</b></td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;"><i class="fa fa-inr"></i> <b>'.number_format($rows['scheme_total_amounts'],2).'</b></td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$rows['scheme_total_weight'].'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$status.'</td>
		                   </tr>';
		    }
		    
		    $content.='</tbody>
		               </table>
		               </div>';
		               
		       
		   
		}
        else{
		    $content.="<center><b style='color: red;'>No Scheme Found</b></center>";
		}
		// Last Sales - Endsss Here
		
    if($totalrows>0){
        $content.='<div class="col-md-12" style="padding-top: 0px;">
                   <center><h3><b>Completed Scheme Details</b></h3></center>
                   
                   
                   <div class="col-md-6" style="padding-top: 0px; text-align: center;">
                        <b>Scheme Amount: <i class="fa fa-inr"></i> &nbsp;&nbsp;'.number_format($results[0]['scheme_total_amounts'],2).'</b>
		           </div>
                   
                   
                   <div class="col-md-6" style="padding-top: 0px; text-align: center;">
                        <b>Scheme Weight: '.$results[0]['scheme_total_weight'].'</b>
		           </div>
                   
		           </div>';        
    }
		
		$content.="</div>";
		
		
		echo $content;
 
}








if($_POST['get_details']=="get_customer_advance_details"){
     $customer_id = $_POST['customer_id'];
     
     $condition="";
     
     if($customer_id!=""){
    	$condition.=" and ad.customer_id=".$customer_id;
     }
     
     $condition.=" and (ad.expected_date>='".$today_date."' or ad.expected_date='".$today_date."')";
   
        $statement = $pdo->prepare("SELECT ad.*, c.customer_name, c.mobile_no,
                                    p.hsn_code, pro.name as products_name, 
                                    (SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=4 and p.advance_id=ad.id) as advance_paid_amount,
                                    cu.name as created_user, uu.name as updated_user
                                    FROM `tbl_advance_voucher` ad 
                                    LEFT JOIN tbl_customers c ON c.id=ad.customer_id
                                    LEFT JOIN tbl_product_variants p ON p.id=ad.product_variant_id
                                    LEFT JOIN tbl_products_name pro ON pro.id=p.product_name
                                    LEFT JOIN tbl_users cu ON cu.id=ad.created_by
                                    LEFT JOIN tbl_users uu ON uu.id=ad.updated_by
                                    WHERE ad.id!=0 AND ad.sale_id=0 ".$condition." ORDER BY ad.id DESC");
    	$statement->execute();
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalrow = $statement->rowCount();
		



        $statementss = $pdo->prepare("SELECT ad.*, c.customer_name, c.mobile_no,
                            p.hsn_code, pro.name as products_name, 
                            (SELECT SUM(amount) FROM `tbl_payments` p WHERE p.accounts=4 and p.advance_id=ad.id) as advance_paid_amount,
                            cu.name as created_user, uu.name as updated_user
                            FROM `tbl_advance_voucher` ad 
                            LEFT JOIN tbl_customers c ON c.id=ad.customer_id
                            LEFT JOIN tbl_product_variants p ON p.id=ad.product_variant_id
                            LEFT JOIN tbl_products_name pro ON pro.id=p.product_name
                            LEFT JOIN tbl_users cu ON cu.id=ad.created_by
                            LEFT JOIN tbl_users uu ON uu.id=ad.updated_by
                            WHERE ad.id!=0 AND ad.sale_id=0 ".$condition."");
        $statementss->execute();
        $results = $statementss->fetchAll(PDO::FETCH_ASSOC);
        $totalrows = $statementss->rowCount();    	
    	
    	
    	

		$content="<style>
		            thead{
		                background: blue;
                        color: white;
		            }
		          </style>
		<div class='row'>
		<center><a href='advance-voucher-list.php?cus_id=".$customer_id."' class='btn btn-warning btn-xs' target='_BLANK'>View Advance Details</a></center>";
		
		
		// Last Sales - Starts Here
        if($totalrow>0){
		    $content.='
		                <div class="col-md-12" style="margin-top: 12px;">
                              <table style="width: 100%;">
                                    <thead>
                                        <tr>
                                            <th style="text-align: center;">S.No</th>
                                            <th style="text-align: center;"></th>
                                            <th style="text-align: center;">Advance ID</th>
                                            <th style="text-align: center;">Amount</th>
                                            <th style="text-align: center;">Weight</th>
                                            <th style="text-align: center;">Expected Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>';
		    $i=1;
		    foreach($result as $rows){
		        

		        
		        
		        $content.='<tr>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$i++.'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">
                                	<input type="checkbox" name="check_ids" value="'.$rows['advance_weight'].'" onclick="Advanceweight('.$rows['advance_weight'].', '.$rows['advance_paid_amount'].', 1, 2); handleAdvanceids(this, '.$rows['id'].')" class="paid_amounts" data-paid_amount="'.$rows['advance_paid_amount'].'" style="width: 20px; height: 15px;">
                                </td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$rows['advance_no'].'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;"><i class="fa fa-inr"></i> <b>'.number_format($rows['advance_paid_amount'],2).'</b></td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.$rows['advance_weight'].'</td>
		                        <td style="text-align: center; padding: 2px 0px 0px 0px;">'.date('d-m-Y', strtotime($rows['expected_date'])).'</td>
		                   </tr>';
		    }
		    
		    $content.='</tbody>
		               </table>
		               </div>';
		               
		       
		   
		}
        else{
		    $content.="<center><b style='color: red;'>No Advance Found</b></center>";
		}
		// Last Sales - Endsss Here
		
    if($totalrows>0){
        $content.='<div class="col-md-12" style="padding-top: 0px;">
                   <center><h3><b>Completed Advance Details</b></h3></center>
                   
                   
                   <div class="col-md-12" style="padding-top: 0px; text-align: center;">
                        <b>Advance Amount: &nbsp;<i class="fa fa-inr"></i> &nbsp;'.number_format($results[0]['advance_paid_amount'],2).'</b>
		           </div>
		           
		           
		           </div>';        
    }
		
		$content.="</div>";
		
		
		echo $content;
 
}















?>