<?php 
include 'session.php';
extract($_REQUEST);
$act=$action;

//print_r($_REQUEST);
//die();

switch($act)
{	
	case 'due_invoice_list_for_payment':
		$rslt_html='';	
		if(isset($invoice_ids) && !empty($invoice_ids)){
			$temp_given_amt = (int)$given_amount;
			
			$rslt_data = $db->get_rsltset(" select t1.* from t_purchase_payment t1  where 1=1  and t1.p_invoice_no IN(".$invoice_ids.") order by t1.p_payment_id asc");
			
			if(!empty($rslt_data)){
				foreach($rslt_data as $rslt_data_S){
					$invoice_no = $rslt_data_S['p_invoice_no'];
					$act_amt_due =(int)$rslt_data_S['due_amt'];
					
					$paying_amt=0;
					$sel='';
					
					//paying amount calculation	
					if($temp_given_amt > 0){
						if($temp_given_amt >= $act_amt_due){
							$paying_amt = $act_amt_due;						
							$temp_given_amt = $temp_given_amt - $act_amt_due;
							$sel='checked';
						}
						else{
							$paying_amt = $temp_given_amt;
							$temp_given_amt=0;
							$sel='checked';
						}
					}	

					
					$rslt_html .=' <div class="form-group row">
											<div class="col-lg-2">
												<input '.$sel.' type="checkbox" class="serv_chk_bx" name="chkbx_inv_ids[]" id="chkbx_inv_'.$rslt_data_S['p_invoice_no'].'" value="'.$rslt_data_S['p_invoice_no'].'" />
												<label>'.$rslt_data_S['p_invoice_no'].'</label>							
											</div>	
											<div class="col-lg-2">
												<input id="txt_due_amt_'.$rslt_data_S['p_invoice_no'].'" class="form-control" name="txt_due_amt[]" readonly  type="text" value="'.$act_amt_due.'" />						
											</div>	
											<div class="col-lg-2">
												<input id="txt_paying_amt_'.$rslt_data_S['p_invoice_no'].'" class="form-control" name="txt_paying_amt[]" onkeypress="return isNumber(event);" type="text" value='.$paying_amt.' />							
											</div>
											<div class="col-lg-4">
												<select name="sel_balance_opt[]" id="sel_balance_opt_'.$rslt_data_S['p_invoice_no'].'" class="form-control select2">
													<option value="0">Select</option>
													<option value="1">Discount</option>
													<option value="2">Keep as Due</option>
												</select>
											</div>
										</div>	 ';
					
					
				}

				//balance if there 
				if($temp_given_amt > 0 ){
					$rslt_html .=' <div class="form-group row"> 
						<div class="col-lg-4">
							Credit Note for Balance Payment
						</div>
						<div class="col-lg-2">
							<input id="txt_credit_amt" class="form-control" name="txt_credit_amt" type="text" value="'.$temp_given_amt.'" />	
						</div>
						<div class="col-lg-4">
							<input id="txt_credit_comments" class="form-control" name="txt_credit_comments" type="text" value="Excess Payment Rs. '.$temp_given_amt.'" />	
						</div>
					</div>';
				}
				
			}
		}
		
		echo json_encode(array("rslt"=>$rslt_html));
	break;	


	case 'due_sales_bill_list_for_payment':
		$rslt_html='';	
		if(isset($invoice_ids) && !empty($invoice_ids)){
			$temp_given_amt = (int)$given_amount;
			
			$rslt_data = $db->get_rsltset(" select t1.* from t_sales_payment t1  where 1=1  and t1.s_bill_no IN(".$invoice_ids.") order by t1.s_payment_id asc ");
			
			if(!empty($rslt_data)){
				foreach($rslt_data as $rslt_data_S){
					$invoice_no = $rslt_data_S['s_bill_no'];
					$act_amt_due =(int)$rslt_data_S['due_amt'];
					
					$paying_amt=0;
					$sel='';
					
					//paying amount calculation	
					if($temp_given_amt > 0){
						if($temp_given_amt >= $act_amt_due){
							$paying_amt = $act_amt_due;						
							$temp_given_amt = $temp_given_amt - $act_amt_due;
							$sel='checked';
						}
						else{
							$paying_amt = $temp_given_amt;
							$temp_given_amt=0;
							$sel='checked';
						}
					}	

					
					$rslt_html .=' <div class="form-group row">
											<div class="col-lg-2">
												<input '.$sel.' type="checkbox" class="serv_chk_bx" name="chkbx_inv_ids[]" id="chkbx_inv_'.$rslt_data_S['s_bill_no'].'" value="'.$rslt_data_S['s_bill_no'].'" />
												<label>'.$rslt_data_S['s_bill_no'].'</label>							
											</div>	
											<div class="col-lg-2">
												<input id="txt_due_amt_'.$rslt_data_S['s_bill_no'].'" class="form-control" name="txt_due_amt[]" readonly  type="text" value="'.$act_amt_due.'" />						
											</div>	
											<div class="col-lg-2">
												<input id="txt_paying_amt_'.$rslt_data_S['s_bill_no'].'" class="form-control" name="txt_paying_amt[]" onkeypress="return isNumber(event);" type="text" value='.$paying_amt.' />							
											</div>
											<div class="col-lg-4">
												<select name="sel_balance_opt[]" id="sel_balance_opt_'.$rslt_data_S['s_bill_no'].'" class="form-control select2">
													<option value="0">Select</option>
													<option value="1">Discount</option>
													<option value="2">Keep as Due</option>
												</select>
											</div>
										</div>	 ';
					
					
				}

				//balance if there 
				if($temp_given_amt > 0 ){
					$rslt_html .=' <div class="form-group row"> 
						<div class="col-lg-4">
							Credit Note for Balance Payment
						</div>
						<div class="col-lg-2">
							<input id="txt_credit_amt" class="form-control" name="txt_credit_amt" type="text" value="'.$temp_given_amt.'" />	
						</div>
						<div class="col-lg-4">
							<input id="txt_credit_comments" class="form-control" name="txt_credit_comments" type="text" value="Excess Payment Rs. '.$temp_given_amt.'" />	
						</div>
					</div>';
				}
				
			}
		}
		
		echo json_encode(array("rslt"=>$rslt_html));
	break;

	
	case 'get_account_no_by_bank_id':
		$rslt_html='';
		if(isset($bank_id) && !empty($bank_id)){
			$get_info = $db->get_a_line("select * from m_banks where bank_id='".$bank_id."' ");
			
			$rslt_html=$get_info['account_no'];
		}		
		
		echo json_encode(array("rslt"=>$rslt_html));
	break;
	
	
}



?>