<?php 
error_reporting(-1);
include 'session.php';
require_once('common/getname_functions.php');
extract($_REQUEST);
$act=$action;

switch($act)
{
	
	case 'update_sales_payment_details':
	
	    
		if(!empty($disp_cus_id) && !empty($txt_given_amount)){
			
			$payment_cleared = 1; $disp_payment_cleared='Uncleared'; $disp_cleared_dt='N/A'; //payment status uncleared
			$cheque_cleared_dt = '0000-00-00';
			
			if($rbtn_payment_mode == "1"){
				//cash
				$recv_on_dt = date("Y-m-d", strtotime($txt_recv_date)); 
				
				$cus_payment_ins_qry = " insert into t_customers_payments(cus_id, company_id, payment_type_id, received_on_date, amount_recv,  remarks, financial_id, IsActive, UserId, createddate) values('".$disp_cus_id."', '".$disp_company_id."', '".$rbtn_payment_mode."', '".$recv_on_dt."', '".$txt_given_amount."', '".$txt_remarks."', '".$_SESSION["Financial_id"]."', '1', '".$_SESSION["UserId"]."', NOW()) ";	
				
				$sv_payment_desc='Payment Mode: '.get_payment_type_name($db,$rbtn_payment_mode).'| Amount Recv: '.$txt_given_amount.'| recv date: '.date("d-M-Y", strtotime($txt_recv_date));
				
			}
			else if($rbtn_payment_mode == "2"){
				//cheque
				$cheque_issue_dt = date("Y-m-d", strtotime($txt_cheque_issued_on)); 
				if(isset($txt_cleared_date) && !empty($txt_cleared_date)){
					$cheque_cleared_dt = date("Y-m-d", strtotime($txt_cleared_date)); 
					$payment_cleared=2;
					$disp_payment_cleared='Cleared';
					$disp_cleared_dt = date("d-M-Y", strtotime($txt_cleared_date)); 
				}
								
				$cus_payment_ins_qry = " insert into t_customers_payments(cus_id, company_id, payment_type_id, amount_recv, bank_id, acc_no, cleared_status, cheque_issue_date, cheque_no, cleared_date, comission_amount, total_amount, remarks, financial_id, IsActive, UserId, createddate) values('".$disp_cus_id."', '".$disp_company_id."', '".$rbtn_payment_mode."', '".$txt_given_amount."', '".$sel_bank."', '".$txt_acc_no."', '".$payment_cleared."', '".$cheque_issue_dt."', '".$txt_cheque_no."', '".$cheque_cleared_dt."', '".$txt_cheque_interest."', '".$txt_cheque_amount."', '".$txt_remarks."', '".$_SESSION["Financial_id"]."', '1', '".$_SESSION["UserId"]."', NOW()) ";
				
				$sv_payment_desc='Payment Mode: '.get_payment_type_name($db,$rbtn_payment_mode).'| Bank: '.get_bank_name($db,$sel_bank).'| Amount Recv: '.$txt_given_amount.'| Issued date: '.date("d-M-Y", strtotime($txt_cheque_issued_on)).'| Instrument No: '.$txt_cheque_no.'| Commission Amount: '.$txt_cheque_interest.'| Total Amount: '.$txt_cheque_amount;
			}
			else if($rbtn_payment_mode == "3"){
				//netbanking
				$transfer_dt = date("Y-m-d", strtotime($txt_cheque_issued_on)); 
				
				$cus_payment_ins_qry = " insert into t_customers_payments(cus_id, company_id, payment_type_id, amount_recv, bank_id, acc_no, cheque_issue_date, comission_amount, total_amount, remarks, financial_id, IsActive, UserId, createddate) values('".$disp_cus_id."', '".$disp_company_id."', '".$rbtn_payment_mode."', '".$txt_given_amount."', '".$sel_bank."', '".$txt_acc_no."', '".$transfer_dt."', '".$txt_cheque_interest."', '".$txt_cheque_amount."', '".$txt_remarks."', '".$_SESSION["Financial_id"]."', '1', '".$_SESSION["UserId"]."', NOW()) ";
				
				$sv_payment_desc='Payment Mode: '.get_payment_type_name($db,$rbtn_payment_mode).'| Bank: '.get_bank_name($db,$sel_bank).'| Amount Recv: '.$txt_given_amount.'| Transfer date: '.date("d-M-Y", strtotime($txt_cheque_issued_on)).'| Instrument No: '.$txt_cheque_no.'| Commission Amount: '.$txt_cheque_interest.'| Total Amount: '.$txt_cheque_amount;
			}
			
			//supplier payments main table save [t_customers_payments] - START	
			
									
			$cus_payment_insert = $db->insert($cus_payment_ins_qry);
			$payment_id_insert=$db->insert_id;
			
			//supplier payments main table save [t_customers_payments] - END	
			
			if(!empty($payment_id_insert)){
				
				//payment update invoice wise - START 
				$inv_cnt = count($h_sel_invoice_id);
				for($i=0; $i < $inv_cnt; $i++){
					$act_inv_due_amt = $txt_due_amt[$i];
					$act_inv_paying_amt = $txt_paying_amt[$i];				
					$bal_due_amt = $act_inv_due_amt - $act_inv_paying_amt;
					
					$discount_amt=0;
					if($bal_due_amt > 0){
						$inv_payment_status=3; //partial payment	
						if($h_sel_bal_opt[$i] == "1"){
							//discount
							$inv_payment_status=2; //payment received	
							$discount_amt= $bal_due_amt; //due amount update to discount
							$bal_due_amt = 0; //due amount for discount
						}
						else if($h_sel_bal_opt[$i] == "2"){
							//keep as a due
						}					
					}
					else{
						$bal_due_amt = 0; //no due amount
						$inv_payment_status=2; //payment received	
					}	

					//update t_sales_payment for due amount
					$db->insert("update t_sales_payment set due_amt='".$bal_due_amt."', payment_status_id='".$inv_payment_status."', tot_paid_amt = (tot_paid_amt + ".$act_inv_paying_amt."),  UserId='".$_SESSION["UserId"]."' where s_bill_no='".$h_sel_invoice_id[$i]."' ");
					
				}
				//payment update invoice wise - END 
				
				
				//supplier payments details table save for log purpose [t_customers_payments_det] - START	
				$sv_operation=1;				
				$sv_cleared_desc=' Payment status: '.$disp_payment_cleared.'| Cleared Date: '.$disp_cleared_dt;
				
				$cleared_msg=' (Uncleared)';
				if($payment_cleared == 2){
					$cleared_msg=' (Cleared on '.$disp_cleared_dt.')';
				}			
				$ge_comments_msg=' Payment Added PaymentID: '.$payment_id_insert.$cleared_msg.'| '.$sv_payment_desc;
				
				$db->insert(" insert into t_customers_payments_det(s_payment_id, cus_id, opertion_det, payment_desc, cleared_desc, general_comments, UserId) values(".$payment_id_insert.", ".$disp_cus_id.", '".$sv_operation."', '".$sv_payment_desc."', '".$sv_cleared_desc."', '".$ge_comments_msg."', '".$_SESSION["UserId"]."') ");
				
				//supplier payments details table save for log purpose [t_customers_payments_det] - END
				
				echo json_encode(array("rslt"=>"1")); //success
			}
			else{
				echo json_encode(array("rslt"=>"-2")); //payment insert failed
			}
			
		}
		else{
			echo json_encode(array("rslt"=>"-1")); //no data
		}
	break;
	
}


?>