<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_SUBCONTRACTOR_PURCHASE_ORDER_INQ';
if (!@$_GET['popup'])
	$path_to_root = "../..";
else	
	$path_to_root = "..";
include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_purchase_orders_inquiry_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(1000, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

page($help_context = "Subcontractor Purchase Orders Inquiry", false, false, "", $js);
//---------------------------------------------------------------------------------------------
simple_page_mode(true);

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
if (isset($_GET['from_date']))
{
	$_POST['from_date'] = $_GET['from_date'];
}
if (isset($_GET['to_date']))
{
	$_POST['to_date'] = $_GET['to_date'];
}
function id($row)
{    
   // return $row['id'];
    return get_project_trans_view_str(ST_SUBCONTRACTOR_PURCH_ORDER, $row['id'], null);
}

function view_link($row)
{
   // return $row['order_no'];
    return get_project_trans_view_str(ST_PROJECT_INVOICE, $row['order_no']);
}


function project_description($row)
{       
    return $row['project_description'];
}


function supplier_name($row)
{
    return $row['supp_name'];

}


function order_date($row)
{
    return sql2date($row['date']);   
}

function reference($row)
{
    return $row['reference'];  
}

function rate_include_taxes($row)
{
    if($row['rate_include_taxes'] == 1){
        $rate_include_taxes = "Yes";
    }else{
        $rate_include_taxes = "No";
    }
    
    return $rate_include_taxes;
}

function tax_group_name($row)
{
    return $row['tax_group_name'];  
}

function agreement_reference($row)
{
    return $row['agreement_reference'];  
}

function comment($row)
{
    return $row['comment'];  
}


function edit_link($row) 
{
    if($row['approval_status'] == AS_NEW || $row['approval_status'] == AS_WITH_THE_APPROVER || 
            $row['approval_status'] == AS_AWAITING_APPROVAL || $row['approval_status'] == AS_BACK_TO_ORIGINATOR){
        
        $modify = "SCPO_No";
        return pager_link(_("Edit"), "/projects/subcontractor_purchase_orders.php?$modify=". $row['id'], ICON_EDIT);
    }
    else{
        return null;
    }
      
}

function invoice_link($row) 
{
    if($row['approval_status'] == AS_APPROVED){
        
        $modify = "SCPO_No";
        return pager_link(_("Invoice"), "/projects/generate_subcontractor_invoice.php?$modify=". $row['id'], ICON_DOC);
    }
    else{
        return null;
    }
      
}

function _originator($row)
{
    return $row['real_name'];
}

function _approval_status($row)
{
    global $approval_statuses;
    return $approval_statuses[$row['approval_status']];    
}

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

if ($Mode == 'Delete')
{
        $recs = delete_i_owe_u($selected_id, $_POST['employee_code']);
        
        if($recs == 1){
                display_notification(_("Salary advance has been deleted."));
        }
        else{
                display_error(_("Salary advance could not be deleted."));
        }

	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

if (isset($_POST['_selected_id_update']) )
{
	$selected_id = $_POST['selected_id'];
	//$Ajax->activate('_page_body');
}

//if (list_updated('employee_code')) 
	$Ajax->activate('_page_body');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();
start_table();

    start_row();
        ref_cells("ID:", 'selected_id');
        ref_cells("Reference:", 'reference');
    end_row();
 
    start_row();
        project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);
        supplier_list_cells("Supplier:", 'supplier_id', null, 'Select a Supplier', true);
        tax_groups_list_cells("Tax Group:", 'tax_group_id', null, 'Select a Tax Group', true);    
    end_row();
    
    start_row();
        date_cells(_("From Date:"), 'from_date', '', null, -30);
        date_cells(_("To Date:"), 'to_date', '', null, +30);
   
    
        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');

    end_row();
end_table(1);

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

$selected_id = "";
if(isset($_POST['selected_id'])){
  $selected_id = $_POST['selected_id'];
}

$reference = "";
if(isset($_POST['reference'])){
  $reference = $_POST['reference'];
}

$project_id = "";
if(isset($_POST['project_id'])){
  $project_id = $_POST['project_id'];
}

$supplier_id = "";
if(isset($_POST['supplier_id'])){
  $supplier_id = $_POST['supplier_id'];
}

$tax_group_id = "";
if(isset($_POST['tax_group_id'])){
  $tax_group_id = $_POST['tax_group_id'];
}

$from_date = "";
if(isset($_POST['from_date'])){
  $from_date = $_POST['from_date'];
}

$to_date = "";
if(isset($_POST['to_date'])){
  $to_date = $_POST['to_date'];
}


    $sql = get_sql_for_subcontractor_purchase_orders_inquiry($selected_id, $reference, $project_id, $supplier_id, 
                                                               $tax_group_id, $from_date, $to_date);    
    
    $cols = array(
                _("ID")=>array('fun'=>'id'),
                _("Reference")=>array('fun'=>'reference'),
                _("Project")=>array('fun'=>'project_description'),
                _("Supplier")=>array('fun'=>'supplier_name'),
                _("Date")=>array('fun'=>'order_date','align'=>'center'),        
                _("Comment")=>array('fun'=>'comment'),
                _("Originator")=>array('fun'=>'_originator'), 
                _("Approval Status")=>array('fun'=>'_approval_status', 'align' => 'center')            
            );
        array_append($cols, array(
            array('insert'=>true, 'fun'=>'edit_link'),
            array('insert'=>true, 'fun'=>'invoice_link')));

//------------------------------------------------------------------------------------------------      
//if (get_post('StockLocation') != $all_items) {
//	$cols[_("Location")] = 'skip';
//}
//---------------------------------------------------------------------------------------------------

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

$table->width = "80%";

display_db_pager($table);

br();

if (!@$_GET['popup'])
{
	end_form();
	end_page();
}	

?>
