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

$page_security = 'SA_SUBCONTRACTOR_CREDIT_NOTE_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_credit_note_inquiry_db.inc");


$js = '';

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

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

page($help_context = "Approve Subcontractor Credit Notes", false, false, "", $js);

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

simple_page_mode(true);


function selected_id($row)
{
    return get_project_trans_view_str(ST_SUBCONTRACTOR_CREDIT_NOTE, $row['order_no']);  
}

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

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

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

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

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

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

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

function subcontractor_invoice($row)
{
    if($row['sc_invoice_id'] > 0){       
        return get_project_trans_view_str(ST_SUBCONTRACTOR_INVOICE, $row['sc_invoice_id']);
    }else{
        null;
    }
}


function edit_link($row) 
{
    if (@$_GET['popup'])
            return '';
   // global $trans_type;
    $modify = "SCCN_No";

     return pager_link(_("Edit"), "/projects/subcontractor_credit_note.php?$modify=".$row['order_no'], ICON_EDIT);
     
//    if($row['approval_status'] == 0){
//        return pager_link(_("Edit"), "/projects/project_credit_note.php?$modify=".$row['order_no'], ICON_EDIT);
//    }elseif($row['approval_status'] == 10){
//        return pager_link(_("Edit"), "/projects/project_credit_note.php?$modify=".$row['order_no'], ICON_EDIT);
//    }elseif($row['approval_status'] == 20){
//        return pager_link(_("Edit"), "/projects/project_credit_note.php?$modify=".$row['order_no'], ICON_EDIT);
//    }elseif($row['approval_status'] == 120){
//        return pager_link(_("Edit"), "/projects/project_credit_note.php?$modify=".$row['order_no'], ICON_EDIT);
//    }else{
//        return null;  
//    }
}

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


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');
//--------------------------------------------------------------------------------------------------

start_form();

//start_table();
//    start_row();
//        ref_cells("ID:", 'selected_id');
//        ref_cells("Reference:", 'reference');
//        project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);     
//    end_row();
//    
//    
//    start_row();
//        supplier_list_cells("Supplier:", 'supplier_id', null, 'Select a Supplier', true);
//        ref_cells("SubContractor Invoice #:", 'sc_invoice_id');
//    end_row();
//    
//    start_row();
//        date_cells(_("From:"), 'from_date', '', null, -30);
//        date_cells(_("To:"), 'to_date', '', null, +30);
//        
//        submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
//
//    end_row();        
//     
//
//    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'];
}

$sc_invoice_id = "";
if(isset($_POST['sc_invoice_id'])){
    $sc_invoice_id = $_POST['sc_invoice_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'];
}

$current_user_id = $_SESSION['wa_current_user']->user;
$sql = get_sql_for_approve_subcontractor_credit_note_inquiry($current_user_id);    
    
    $cols = array(_("CN No.")=> array('fun'=>'selected_id','align'=>'center'), 
                _("Reference")=>array('fun'=>'reference'),   
                _("Project")=> array('fun'=>'project_description'),
                _("Supplier")=> array('fun'=>'supplier_id'),
                _("Order Date")=>array('fun'=>'ord_date','align'=>'center'), 
                _("Supp. Ref.")=>array('fun'=>'sc_inv_ref'),
                _("Invoice No")=> array('fun'=>'subcontractor_invoice','align'=>'center'),
                _("Status")=>array('fun'=>'approval_status','align'=>'center'),
               
            );
    
        array_append($cols, array(         
            array('insert'=>true, 'fun'=>'edit_link')));

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

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

$table->width = "98%";

display_db_pager($table);

br();

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


?>
