<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_TRANSPORT_TRANSVIEW';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/transport/includes/db/supplier_transport_invoices_inquiry_db.inc");
include_once($path_to_root . "/transport/includes/db/supplier_transport_invoices_db.inc");    


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Supplier Transport Invoice"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------
$sti_id = 0;
if ($_GET['trans_no'] != "")
{
	$sti_id = $_GET['trans_no'];
}



function display_transport_invoice_header($sti_id, &$other_charges_text, &$other_charges)
{
    global $approval_statuses;
    
    echo "<br>";
    start_table(TABLESTYLE2, "width=60%");

    $myrow = view_supplier_transport_invoice_header($sti_id);

    start_row();

    label_cells(_("Supplier Reference"), $myrow['supp_inv_ref'], "class='tableheader2'");
    end_row();
    label_cells(_("Supplier"), $myrow['supp_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Date"), $myrow['tran_date'], "class='tableheader2'");
    end_row();
    label_cells(_("Payment Terms"), $myrow['terms'], "class='tableheader2'");
    end_row();
    //label_cells(_($myrow['other_charges_text']),number_format($myrow['other_charges'],2), "class='tableheader2'");
    //end_row();
    label_cells(_("Project"), $myrow['project_description'], "class='tableheader2'");
    end_row();
    label_cells(_("Budget"), $myrow['task_description'], "class='tableheader2'");
    end_row();
    label_cells(_("Comments"), $myrow['comments'], "class='tableheader2'");
    end_row();

    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $sti_id."&source_type_id=".ST_SUPPLIER_TRANSPORT_INVOICES, "Approval Status");
    label_cells($s,$approval_statuses[$myrow['approval_status']], "class='tableheader2'");
    end_row();
    
    if($myrow['payment_source_type'] > 0){
        global $supplier_invoice_payment_source_types;
        label_cells(_("Payment Source"), $supplier_invoice_payment_source_types[$myrow['payment_source_type']], "class='tableheader2'");
        end_row();

        switch ($myrow['payment_source_type']) {
            case ST_PETTY_CASH_DIRECT_PAYMENTS:
                label_cells(_("Payment Source No"), get_petty_cash_trans_view_str(ST_PETTY_CASH_DIRECT_PAYMENTS, $myrow['payment_source_no'] > 0 ? $myrow['payment_source_no'] : '') , "class='tableheader2'");
                break;
            
            case ST_SUPPINVOICE:
                label_cells(_("Payment Source No"), get_supplier_trans_view_str(ST_SUPPINVOICE, $myrow['payment_source_no'] > 0 ? $myrow['payment_source_no'] : '') , "class='tableheader2'");
                break;


            default:
                label_cells(_("Payment Source No"), '', "class='tableheader2'");
                break;
        }
        end_row();
    }
    else{
        label_cells(_("Payment Source"), '', "class='tableheader2'");
        end_row();
        label_cells(_("Payment Source No"), '', "class='tableheader2'");
        end_row();

    }
    
    end_table();
    
    $other_charges_text = $myrow['other_charges_text'];
    $other_charges = $myrow['other_charges'];
    
    return $myrow;
}

function display_transport_invoice_details($sti_id, $other_charges_text, $other_charges, $advances_total)
{
    display_heading2("Running Chart Entries");
    
    start_table(TABLESTYLE2, "width=95%");
    $th = array(("VRC #"),_("Project"),
                _("Vehicle"),_("Plate No"),
                _("Project Cost"),_("Start"),_("End"),
                _("Route"), _("Bill Reference"),
                _("Supplier Charge"));
    //inactive_control_column($th);
    table_header($th);

    $total = 0;
    $result = view_supplier_transport_invoice_details($sti_id);
    while ($myrow = db_fetch($result)) 
    {
        alt_table_row_color($k);	

    //	label_cell($myrow["id"]);       
        label_cell(get_transport_trans_view_str(ST_VEHICLE_RUNNING_CHART, $myrow['id']));
        label_cell($myrow["project_description"]);      
//        label_cell($myrow["package_description"]);       
        label_cell($myrow["vehicle_description"]);
        label_cell($myrow["plate_no"]);       
        label_cell($myrow["cost"]);
        label_cell(substr($myrow["start_date_time"],0,16));       
        label_cell(substr($myrow["end_date_time"],0,16));
        label_cell($myrow["route"]); 
        label_cell($myrow["bill_reference"]); 

        amount_decimal_cell($myrow["supplier_charge"]);  
        
        $total += $myrow["supplier_charge"];
        
//        label_cell(get_transport_trans_view_str(ST_VEHICLE_ALLOCATION, $myrow['vehicle_allocation_id'] > 0 ? $myrow['vehicle_allocation_id'] : ''));

        end_row();
    }

    if((strlen($other_charges_text) > 0) || $other_charges > 0)
    {
        start_row();
            label_cell($other_charges_text, "colspan=9 align=right");
            amount_decimal_cell($other_charges);  
        end_row();
    }
    $total += $other_charges;
    
    start_row();
        label_cell("Charges Total", "colspan=9 align=right");
        amount_decimal_cell($total);  
    end_row();
    
    start_row();    
        label_cell("Sub Total (Charges - Advances):", "colspan=9 align=right");
        amount_cell($total - $advances_total, true); 
    end_row();
    

    $sti = get_supplier_transport_invoice_record($sti_id);  
    $tax_included = $sti['tax_included'];

    $taxes = get_supplier_transport_invoice_tax_details($sti_id);        
    display_edit_tax_items($taxes, 9, $tax_included, 0, false);
    
    $taxes_total = 0;
    
    if($tax_included){
        $final_total = $total - $advances_total;
    }
    else{
        foreach ($taxes as $tax_line){
            $taxes_total += $tax_line['Value'];
        }
        
        $final_total = $total - $advances_total + $taxes_total;
    }
    
    start_row();    
        label_cell("Total:", "colspan=9 align=right");
        amount_cell($final_total, true); 
    end_row();
    
    end_table();
    
    return $final_total;
}


function display_transport_inv_vehicle_allocations($sti_id)
{
    global $Ajax;
    
    $result = view_transport_inv_vehicle_allocations($sti_id);
    if(!(db_num_rows($result) > 0)){
        display_note("No advances deducted in this invoice");
        return 0;
    }
    
    $Ajax->activate('vas');
    div_start('vas');

    

    display_heading2("Advances to Vehicle Allocations");
    
    start_table(TABLESTYLE, "width=95%");
    $th = array(_('VA'),"VR", _("Project"), 
                "Package" , _('From Date'),_('To Date'), 
                "Route", "Passengers",  "Comment", "Supplier", "Payment", "");

    table_header($th);
    $k = $total = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        $prefix = "va_".$myrow["id"]."_";
        
        alt_table_row_color($k);

        $a = get_trans_view_str(ST_VEHICLE_ALLOCATION, $myrow['id'],$myrow['id']);
        label_cell($a, "align=center");
        
        if($myrow["vehicle_request_id"] > 0){
            $r = get_trans_view_str(ST_VEHICLE_REQUEST, $myrow['vehicle_request_id'],$myrow['vehicle_request_id']);
            label_cell($r, "align=center");
//            label_cell($myrow["vehicle_request_id"], "align=center");
        }
        else{
            label_cell("");
        }
        
        label_cell($myrow["project_description"]);
//        label_cell($myrow["task_description"]);
        label_cell($myrow["package_description"]);
//        label_cell($myrow["cost"]);
        label_cell(substr($myrow["from_date_time"], 0, 16));
        label_cell(substr($myrow["to_date_time"], 0, 16));
        label_cell($myrow["route"]);
        label_cell($myrow["passengers"]);
        
        label_cell($myrow["comment"]);
        label_cell($myrow["supp_name"]);
        
        $dp_link = get_petty_cash_trans_view_str(ST_PETTY_CASH_DIRECT_PAYMENTS, $myrow["payment_source_no"], $myrow["paid_amount"]);
        label_cell($dp_link, "align=center");     
        
        
        $_POST[$prefix.'package_id'] = $myrow['package_id'];
//        vehicle_packages_list_cells(null, $prefix.'package_id');
        hidden($prefix.'package_id', $_POST[$prefix.'package_id']);

      
        end_row();
 
        $total += $myrow['paid_amount'];
        
        
//        $k--;
        
    }
    //inactive_control_row($th);
    start_row();    
        label_cell("Payments Total:", "colspan=10 align=right");
        amount_cell($total); 
    end_row();
    

    
    end_table(1);
    div_end();
    
    return $total;

}

display_heading("Transport Invoice: " . $sti_id);

$myrow = display_transport_invoice_header($sti_id, $other_charges_text, $other_charges);

br();

$advances_total = display_transport_inv_vehicle_allocations($sti_id);

br();

$final_total = display_transport_invoice_details($sti_id, $other_charges_text, $other_charges, $advances_total);

br(2);

display_allocations_to(PT_SUPPLIER, $myrow['supplier_id'], ST_SUPPLIER_TRANSPORT_INVOICES, $sti_id, 
		($final_total));

end_page(true, false, false, ST_SUPPLIER_TRANSPORT_INVOICES, $sti_id);

?>
