<?php
/**********************************************************************
Copyright (C) Evolve Techniologies
Author : Danushka Widanage
Email : dwidanage@gmail.com
Company : Evolve Technologies(pvt)Ltd
***********************************************************************/

$path_to_root = "../..";

include($path_to_root . "/includes/db_pager.inc");
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");

$page_security = 'SA_SALESTRANSVIEW';

set_page_security( @$_POST['order_view_mode'],
	array(	'OutstandingOnly' => 'SA_SALESDELIVERY',
			'InvoiceTemplates' => 'SA_SALESINVOICE'),
	array(	'OutstandingOnly' => 'SA_SALESDELIVERY',
			'InvoiceTemplates' => 'SA_SALESINVOICE')
);

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 600);
if (user_use_date_picker())
	$js .= get_js_date_picker();

if (get_post('type'))
	$trans_type = $_POST['type'];
else
	$trans_type = ST_SALESINVOICE;

if ($trans_type == ST_SALESINVOICE)
{
	if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true)){
		
		$_POST['order_view_mode'] = 'OutstandingOnly';
		$_SESSION['page_title'] = _($help_context = "Invoice Register");
	
	}elseif(isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true)){
		
		$_POST['order_view_mode'] = 'InvoiceTemplates';
		$_SESSION['page_title'] = _($help_context = "Search Template for Invoicing");
		
	}elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true)){
		
		$_POST['order_view_mode'] = 'DeliveryTemplates';
		$_SESSION['page_title'] = _($help_context = "Select Template for Delivery");
		
	}elseif (!isset($_POST['order_view_mode']))	{
		
		$_POST['order_view_mode'] = false;
		$_SESSION['page_title'] = _($help_context = "Search All Sales Orders");
	}
}

page($_SESSION['page_title'], false, false, "", $js);

//---------------------------------danu add new code 26.09.2012-------------------------------------------
if (isset($_POST['print_inv']))
{
	header("Location:../../report/view_invoice.php?trans_no=".$_POST['OrderNumber']."&trans_type=10&ex_Rate=".$_POST['ex_Rate']."");
}

//----------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
//	Query format functions
//
function check_overdue($row)
{
	global $trans_type;
	if ($trans_type == ST_SALESQUOTE)
		return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
	else
		return ($row['type'] == 0
			&& date1_greater_date2(Today(), sql2date($row['ord_date']))
			&& ($row['TotDelivered'] < $row['TotQuantity']));
}

function view_link($dummy, $order_no)
{
	global $trans_type;
	return  get_customer_trans_view_str1(10, $order_no);
}

//-----------danu add new function for print invoice----15.05.2012-----------
function print_invoice1($dummy, $order_no)
{
	global $trans_type;
	return  get_customer_trans_view_str1(10, $order_no, _("Print"), ICON_PRINT);
}

///------------------new function dwidanage   23.04.2014------------------------------
function print_invoice($dummy, $order_no)
{
	global $trans_type;
	return print_invoice_register($order_no, $trans_type);
	//return  '<a href="../../reporting/prn_redirect.php?PARAM_0='.$order_no.'-'.$dummy.'&PARAM_1='.$order_no.'-'.$dummy.'&PARAM_2=&PARAM_3=0
			//&PARAM_4=&PARAM_5=&PARAM_6='.$dummy.'&REP_ID=107" target="_blank">'.ICON_PRINT.'</a>';
}

//------------------------------------------



start_form();

start_table(TABLESTYLE_NOBORDER);
start_row();
ref_cells(_("Invoice Number :"), 'OrderNumber', '',null, '', true);
$show_dates ='';
$selected_customer ='';
if($show_dates) {
	end_row();
	end_table();

	start_table(TABLESTYLE_NOBORDER);
	start_row();
}
//customer_list_cells(_("Select a customer: "), 'customer_id', null, true);
customer_list_cells(_("Select a customer: "), 'customer_id', null,  true, true);

if (list_updated('customer_id'))//-----@widanage add new code for auto filter with selecting----22.04.2013---------
	$Ajax->activate('_orders_tbl_span');

locations_list_cells(_("Deliver from Location:"), 'location', null, true, true);//-----@widanage add new code for auto filter with selecting location----22.04.2013---------
if (list_updated('location'))
	$Ajax->activate('_orders_tbl_span');

//-----@widanage add new code for selected date range ----22.04.2013---------
date_cells(_("from:"), 'from_date', '', null, -30);
date_cells(_("to:"), 'to_date', '', null, 1);

submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
hidden('type', $trans_type);

end_row();
end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_sales_invoices_view($selected_customer, $trans_type, $_POST['OrderNumber'], $_POST['customer_id'], $_POST['location'], $_POST['from_date'], $_POST['to_date']);

if ($trans_type == ST_SALESINVOICE)
	$cols = array(
		_("Ref"),_("Invoice #") => array('align'=>'center'),
		
		_("Customer"),
		_("Order Date") => 'date',
		_("Order Total") => array('type'=>'amount', 'ord'=>''),
		_("Print") => array('align'=>'center', 'fun' =>'print_invoice')
	);

$table =& new_db_pager('orders_tbl', $sql, $cols);

$table->width = "90%";

display_db_pager($table);

end_form();
end_page();
?>