<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdme_Purchase_Payment($db,'');
include_once "includes/pagepermission.php";
//check permission - START
if(!($res_modm_prm)){
	header("Location:".admin_public_url."error.php");
}
//check permission - END

if(isset($_REQUEST['supplier_id']) && !empty($_REQUEST['supplier_id']) && isset($_REQUEST['company_id']) && !empty($_REQUEST['company_id']) ){
	$supplier_id = base64_decode($_REQUEST['supplier_id']);
	$company_id = base64_decode($_REQUEST['company_id']);
	//$supplier_id = $_REQUEST['supplier_id'];
	
	$str_ed = "select *, CASE WHEN sup_payment_type=2 THEN 'Non-GST Supplier' ELSE 'GST Supplier' END As disp_payment_type from m_supplier where 1=1 and sup_id = '".$supplier_id."' ";
	$sup_det = $db->get_a_line($str_ed);
	
	$company_det = $db->get_a_line("select * from m_company where company_id='".$company_id."' ");
		
	//get over all purchase amount details for the supplier
	$tot_purchase_amt =0;
	$tot_due_amt = 0;
	$tot_already_paid_amt = 0;
	
	if($sup_det['sup_payment_type'] =='1'){
		//gst supplier
		$get_tot_pur_amt = $db->get_a_line("SELECT sum(`final_pur_amt`) as tot_pur_amt FROM `t_purchase_entry_main` WHERE 1=1 and `sup_id`='".$supplier_id."' and `company_id`='".$company_id."' and IsActive=1 ");
	}
	else{
		//non-gst supplier
		$get_tot_pur_amt = $db->get_a_line("SELECT sum(`tot_pur_amount`) as tot_pur_amt FROM `t_purchase_entry_main` WHERE 1=1 and `sup_id`='".$supplier_id."' and `company_id`='".$company_id."' and IsActive=1 ");
	}
	
	if(isset($get_tot_pur_amt['tot_pur_amt']) && !empty($get_tot_pur_amt['tot_pur_amt']) && $get_tot_pur_amt['tot_pur_amt'] !=null){
		$tot_purchase_amt = $get_tot_pur_amt['tot_pur_amt'];		
	}
	
	//already paid amount
	$get_tot_paid_amt =$db->get_a_line(" SELECT sum(`amount_recv`) as tot_paid_amt FROM `t_suppliers_payments` WHERE 1=1 and `sup_id`='".$supplier_id."' and `company_id`='".$company_id."' and `IsActive`=1 ");
	
	if(isset($get_tot_paid_amt['tot_paid_amt']) && !empty($get_tot_paid_amt['tot_paid_amt']) && $get_tot_paid_amt['tot_paid_amt'] !=null){
		$tot_already_paid_amt = $get_tot_paid_amt['tot_paid_amt'];		
	}
	
	$tot_due_amt = (float)$tot_purchase_amt -  (float)$tot_already_paid_amt;
}
else{
	header("Location:".admin_public_url."error.php");
}
?> 


<div class="wrapper">
    <div class="container">
        <!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-12">   										
                <h4 class="page-title">Manage Purchase Payments</h4>
				<input type="hidden" name="disptblname" id="disptblname" value="<?php echo "purchase_payments"; ?>" />				
            </div>
        </div>
		<!-- Page-Title - END -->	
					
		<div class="row">
            <div class="col-sm-12">
                <div class="card-box">                         
                    
					<div class="row">
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Supplier Name</strong> <br> <p class="text-muted"><?php echo $sup_det['sup_name']; ?></p>
							</div>
						</div>
					
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Mobile</strong> <br> <p class="text-muted"><?php echo $sup_det['sup_mobile']; ?></p>
							</div>
						</div>
						
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Address</strong> <br> <p class="text-muted"><?php echo $sup_det['sup_address'].', '.$sup_det['sup_city'].', '.$sup_det['sup_state'].', '.$sup_det['sup_country']; ?></p>
							</div>
						</div>
						
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Payment Type</strong> <br> <p class="text-muted"><?php echo $sup_det['disp_payment_type']; ?></p>
							</div>
						</div>						
					</div>
					<hr/>
					<div class="row">
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Total Purchase Amount</strong> <br> <p class="text-muted"><?php echo $tot_purchase_amt; ?></p>
							</div>
						</div>
					
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Total Already Paid Amount</strong> <br> <p class="text-muted"><?php echo $tot_already_paid_amt; ?></p>
							</div>
						</div>
						
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Outstanding Amount</strong> <br> <p class="text-muted"><?php echo $tot_due_amt; ?></p>
							</div>
						</div>
								
						<div class="col-md-3">
							<div class="about-info-p">
								<strong>Payment For</strong> <br> <p class="text-muted"><?php echo $company_det['company_name']; ?></p>
							</div>
						</div>		

								
					</div>
					
                </div>
            </div>
        </div> <!-- end row -->		
	
	
<form id="jvalidate_purchase_payment_updt" role="form" class="form-horizontal" action="#" method="post">	
<input type="hidden" name="disp_sup_id" id="disp_sup_id" value="<?php echo $supplier_id; ?>"  />
<input type="hidden" name="disp_sup_name" id="disp_sup_name" value="<?php echo $sup_det['sup_name']; ?>"  />
<input type="hidden" name="hdn_tot_due_amt" id="hdn_tot_due_amt" value="<?php echo $tot_due_amt; ?>"  />
<input type="hidden" name="disp_company_id" id="disp_company_id" value="<?php echo $company_id; ?>"  />

					
<div class="row">
	<div class="col-sm-12">	
		<div class="card-box">    
			<div class="row">	
				<div class="col-lg-12">
					<label>Total Amount Due :  </label>	
					<span style="font-size:15px;" class="label label-success"><?php echo "Rs. ".$tot_due_amt; ?></span>						
				</div>
				<div class="col-lg-12">
					<br/>
				</div> 
				
				<div class="col-lg-12">
					<div class="panel panel-border panel-info">
						<div class="panel-heading">
							<h3 class="panel-title">Payment Details</h3>
                        </div>
                        <div class="panel-body">
							<div class="col-lg-12">																	
								<label>Payment Mode : </label>
								<?php 
								$rslt_payment_mode = $db->get_rsltset("select * from status_payment_type where 1=1 and IsActive=1 and payment_type_id IN(1,2,3)");
								foreach($rslt_payment_mode as $rslt_S){
								?>	
									<div class="radio-info radio-inline">
										<input type="radio" id="rbtn_payment_mode_<?php echo $rslt_S['payment_type_id']; ?>" value="<?php echo $rslt_S['payment_type_id']; ?>" name="rbtn_payment_mode">
										<label for="rbtn_payment_mode_<?php echo $rslt_S['payment_type_id']; ?>"> <?php echo $rslt_S['payment_type']; ?> </label>
									</div>
								<?php	
								}
								?>									
								<br/>
							</div>
							<div class="col-lg-12">
								<label>&nbsp;</label>
							</div>
							
							<div class="col-lg-12 dv_fields" id="dv_common_fields">
								<div class="form-group row">
									<div class="col-lg-3">
										<label >Amount Paid </label>
										<input id="txt_given_amount" name="txt_given_amount" class="form-control jsrequired" type="text" onkeypress="return isNumber(event);" />
									</div>
									<div class="col-lg-3">
										<label >Remarks </label>
										<input id="txt_remarks" name="txt_remarks" class="form-control" type="text" />
									</div>	
								</div>
							</div>
																					
							<!-- Cash -- start -->
							<div class="col-lg-12 dv_fields" id="dv_rbtn_payment_mode_1">
								<div class="form-group row">
									<div class="col-lg-3">
										<label >Paid on </label>
										<input id="txt_paid_on" name="txt_paid_on" class="form-control cmn_st_date jsrequired" type="text" readonly="" />
									</div>							
								</div>
							</div>
							<!-- Cash -- end -->
							
							<!-- Cheque -- start -->
							<div class="col-lg-12 dv_fields" id="dv_rbtn_payment_mode_2">
								
								<div class="form-group row">
								
									<div class="col-lg-3">
										<label>Bank </label>
										<select onchange="get_acc_no_by_bank(this.value)" id="sel_bank" name="sel_bank" class="form-control select2 jsrequired">
											<option value="">Select Bank</option>
											<?php 
											$rslt_bank_list = $db->get_rsltset("select * from m_banks where 1=1 and IsActive=1 and company_id='".$company_id."' ");
											foreach($rslt_bank_list as $rslt_S){
											?>
											<option value="<?php echo $rslt_S['bank_id']; ?>"><?php echo $rslt_S['bank_name']; ?></option>
											<?php	
											}
											?>
										</select>
									</div>	
									
									<div class="col-lg-3">
										<label >Account No </label>
										<input readonly id="txt_acc_no" name="txt_acc_no" class="form-control jsrequired" type="text" />
									</div>	
									
									<div class="col-lg-3 rmv_frm_netbanking">
										<label >Cheque No </label>
										<input id="txt_cheque_no" name="txt_cheque_no" class="form-control jsrequired" type="text" />
									</div>	

									<div class="col-lg-2 rmv_frm_netbanking">
										<label >Cleared Date : </label>
										<input readonly id="txt_cleared_date" name="txt_cleared_date" class="form-control jsrequired cmn_st_date "  type="text" />
									</div>	
								</div>
								
								
								<div class="form-group row">																		
									<div class="col-lg-3">
										<label >Interest Amount</label>
										<input id="txt_cheque_interest" name="txt_cheque_interest" class="form-control" type="text" onkeypress="return isNumber(event);" />
									</div>	
									
									<div class="col-lg-3">
										<label >Total Amount </label>
										<input id="txt_cheque_amount" name="txt_cheque_amount" class="form-control jsrequired" type="text" onkeypress="return isNumber(event);" />
									</div>
									
									<div class="col-lg-3">
										<label >Issued / Tranfer on</label>
										<input id="txt_cheque_issued_on" name="txt_cheque_issued_on" class="form-control cmn_st_date jsrequired" type="text" readonly="" />
									</div>
																	
								</div>
								
							</div>
							<!-- Cheque -- end -->
															
                        </div>
						
						
						<div class="panel-footer">					
							<button onclick="save_payment_details();" class="btn btn-purple waves-effect waves-light pull-right" type="button">Update Payment Details</button>
							<div class="clearfix"></div>
						</div>
						
						
                    </div>
				</div>					
				
			</div>
		</div>
	</div>
</div>	
</form>		
			
		

<?php include "includes/footer.php";  ?> 
	</div> <!-- container -->
</div> <!-- End wrapper -->

<?php include "includes/footer_script.php";  ?> 

<!-- Date Picker related --> 
<link type="text/css" href="assets/plugins/bootstrap-datepicker/jquery-ui.min.css" rel="stylesheet" /> 
<script src="assets/plugins/bootstrap-datepicker/jquery-ui.min.js" type="text/javascript"></script>	 


<script>
$(function () {  

    $(".dv_fields").hide();
	
	//check_due_amount();
	
	$('input').on('ifClicked', function (event) {
		var rbtn_id = $(this).attr('id');	       
		if(rbtn_id == "rbtn_payment_mode_1" || rbtn_id=="rbtn_payment_mode_2" || rbtn_id=="rbtn_payment_mode_3" || rbtn_id=="rbtn_payment_mode_4" || rbtn_id=="rbtn_payment_mode_8" ){
			
			$(".dv_fields").hide();					
			
			$("#dv_common_fields").show();
			
			if(rbtn_id == "rbtn_payment_mode_3"){
				$("#dv_rbtn_payment_mode_2").show();
				$(".rmv_frm_netbanking").hide();
			}
			else if(rbtn_id == "rbtn_payment_mode_2"){
				$("#dv_"+rbtn_id).show();
				$(".rmv_frm_netbanking").show();
			}
			else{
				$("#dv_"+rbtn_id).show();
			}
									
			//$("#dv_cleared_bank_details").show();
		}
	});	
	
	$('.cmn_st_date').datepicker({					
		dateFormat: "dd-mm-yy",				
        changeMonth: true,
        changeYear: true,
        yearRange: "-100:+20",
		autoclose: true,
		todayHighlight:true		
	});	
			
    //deposit date and cleared date custom funstion - start
	$("#txt_cheque_issued_on").on('change', function(ev){
		$("#txt_cleared_date").val("");
	});		
		
	$("#txt_cleared_date").on('change', function(ev){		
		var depositdate = $("#txt_cheque_issued_on").val();
		var clear_date = $(this).val(); 
		
		if(depositdate !='' && clear_date !=''){
			var temp_depo_dt = depositdate.split("-");
			arr_depo_dt=[];
			arr_depo_dt = arr_depo_dt.concat(temp_depo_dt);
			var chk_depo_dt = arr_depo_dt[1]+'/'+arr_depo_dt[0]+'/'+arr_depo_dt[2];
			
			var temp_clear_dt = clear_date.split("-");
			arr_clear_dt=[];
			arr_clear_dt = arr_clear_dt.concat(temp_clear_dt);
			var chk_clear_dt = arr_clear_dt[1]+'/'+arr_clear_dt[0]+'/'+arr_clear_dt[2];
									
			if(chk_clear_dt >= chk_depo_dt){
				
			}
			else{
				swal("Failure!", "Clearing date must be same or greater than the Cheque issue date", "warning");
				$("#txt_cleared_date").val("");
			}
			
		}
	});	
	//deposit date and cleared date custom funstion - end	
	
		
 //get_service_details_with_options(1);		

});

function check_due_amount(){	
	var due_amt = $("#hdn_tot_due_amt").val();
	if(due_amt >0){		
		return true;
	}
	else{
		swal("Failure!", "Due amount is 0, so you can't do this operations, click ok to redirect dashboard", "warning");
		//window.history.back();
		return false;
	}
}


function save_payment_details(){
	if ($('#jvalidate_purchase_payment_updt').valid()) {	
				
		var chk_validate = check_validate();
		if(chk_validate == true){
			var m_data = new FormData();		
			var formdatas = $("#jvalidate_purchase_payment_updt").serializeArray();
			$.each( formdatas, function( key, value ) {
				 m_data.append( value.name, value.value);							 
			});
			m_data.append("action", "update_invoice_payment_details");
			
			$.ajax({
				url        : 'purchase_payment_actions.php',
				type	   : 'POST',
				dataType   : 'json',
				processData: false,
				contentType: false,
				data       : m_data,
				beforeSend : function(){ loading();	},
				success: function(response){ 
					if(response.rslt == "1"){
						swal("Success!", "Payment details update successfully", "success");	
					}
					else if(response.rslt == "-1"){
						swal("Failure!", response.errmsg, "warning");
					}
					else{
						swal("Failure!", "Some session data are missing please try again once or contact administrator", "warning");
					}
					unloading();
				}
			});
						
		}		
	}
}


function check_validate(){
	var due_amt = parseFloat($("#hdn_tot_due_amt").val());
	if(due_amt >0){
		var temp_given_amt =parseFloat($("#txt_given_amount").val());
		
		alert(due_amt);
		alert(temp_given_amt);
		
		
		
		if(temp_given_amt > due_amt){
			swal("Failure!", "Amount Paid is greater than Due Amount!!!", "warning");					
			return false;
		}
		else{
			return true;	
		}					
	}
	else{
		swal("Failure!", "Due amount is 0, so you can't do this operations", "warning");
		return false;
	}
	
}

function get_acc_no_by_bank(bank_id){
	if(bank_id){
		$.ajax({		
			url        : 'bind_html_actions.php',
			method     : 'POST',
			dataType   : 'json',
			data       : 'action=get_account_no_by_bank_id&bank_id='+bank_id,
			beforeSend: function() { loading(); },
			success: function(response){				
				$("#txt_acc_no").val(response.rslt);
				unloading();
			}		
		});	
	}
	else{
		$("#txt_acc_no").val('');
	}
}

</script>	

</body>
</html>
