<?php 
// ----------------------------------------------------------------
// Creator: Nagaraj
// email:   nagamsc20@gmail.com
// Title:   Defer date update of collection payment
// ----------------------------------------------------------------
$page_security = 'SA_OPEN';
$path_to_root="../..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/modules/CollectionPayment/includes/collectionpayment_db.inc");

include_once($path_to_root . "/sales/includes/sales_ui.inc");

include_once($path_to_root . "/sales/includes/sales_db.inc");


    $js = get_js_date_picker();
page(_($help_context = "Invoice Defer Date"), false, false, "", $js);

if (isset($_GET["trans_no"]))
{
	$trans_id = $_GET["trans_no"];
}
elseif (isset($_POST["trans_no"]))
{
	$trans_id = $_POST["trans_no"];
}

// 3 different queries to get the information - what a JOKE !!!!

$myrow  = get_customer_trans($trans_id, ST_SALESINVOICE);
$paym   = get_payment_terms($myrow['payment_terms']);
$branch = get_branch($myrow["branch_code"]);
$sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);



	if(isset($_GET['Updated'])) {	
		display_notification(" The Selected Invoice defer date is updated"); 
	} 

	
	if (isset($_POST['submit'])) {
		
		if (!is_date($_POST['defer_date'])) {
			display_error(_("The entered defer date is invalid."));
			set_focus('defer_date');
		  	return false;
		}

		if (date1_greater_date2(sql2date($myrow['due_date']), $_POST['defer_date'])) {
			 display_error(_("The requested defer date is before the date of the invoice due date."));
			set_focus('defer_date');
			return false;
		}
	

		 if(isset($_POST['trans_no'])) {
		 	$trans_no = $myrow['trans_no'];
		 	$empl_id = update_defer($myrow['trans_no'],$_POST['defer_date']);

		 	//meta_forward($_SERVER['PHP_SELF'], "Updated=yes");
		 	meta_forward($_SERVER['PHP_SELF'], "Updated=yes&trans_no=$trans_no&Type=".ST_SALESINVOICE);
		 } 
	} 

	
	
	start_form();
	
		div_start('details');

		start_table(TABLESTYLE2);

			table_section_title(_("Invoice Details"));
				hidden('trans_no', $myrow['trans_no']);
			
			label_row(_("Customer Name:"), $myrow["DebtorName"] . "<br>" . nl2br($myrow["address"]), "nowrap");
				
			label_row(_("Reference:"), $myrow["reference"]);				
			label_row(_("Invoice Date:"), sql2date($myrow["tran_date"]));						
			$display_total = price_format($myrow["ov_freight"]+$myrow["ov_gst"]+$myrow["ov_amount"]+$myrow["ov_freight_tax"]);
				label_row(_("TOTAL INVOICE"), $display_total );

			label_row(_("Due Date:"), sql2date($myrow["due_date"]));							
			date_row(_("Post Defer Date") . ":", 'defer_date');
	
		end_table(1);

		submit_center('submit', _("Update"), true, '', 'default');
		
		div_end(); 
	end_form();



end_page();
?>