<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PETTYCASH_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 . "/petty_cash/includes/db/petty_cash_float_reimbursement_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_advances_inquiry_db.inc");
include_once($path_to_root . "/petty_cash/includes/ui/petty_cash_ui.inc");

include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");                


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "I Owe You Advances Batch"), true, false, "", $js);

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

global $approval_statuses;

function display_float_total_row($float_name, $float_total)
{
    label_cell("Total advances for $float_name:", "colspan=7 align=right");
    amount_cell($float_total, true); 
}

function display_i_owe_you_advances_details($ad_batch_no)
{

    div_start('price_table');
    
    start_table(TABLESTYLE2, "width=90%");

    $th = array("Id", "IOU #", _("Employee"), _("Type"), _("Project"), 
                _("Float"), _("Date"),
                _("IOU Amount"),_("Adv. Amount"), 
                _("Exp. Batch No"), "Reimb. Amount");
    table_header($th);
    
    $q = view_i_owe_you_advances_details($ad_batch_no);
    
    $total = $float_total = $previous_float_id = 0;
    $float_name = "";
    
    while ($iou = db_fetch($q))
    {     
        if($iou['float_id'] > 0 && $previous_float_id > 0 && $iou['float_id'] != $previous_float_id){
            display_float_total_row($float_name, $float_total);
            
            $float_total = 0;
        }
        
        $float_total += $iou["batch_advance_amount"];
        
        start_row();    
        $adv_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU_ADVANCES, $iou["iou_advance_detail_id"]);
        display_pety_cash_detail_row($adv_link,null);
        $iou_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU, $iou['id']);
        display_pety_cash_detail_row($iou_link,"align=center");          
        
        $emp_full_name = get_group_company_employee_formatted_long_name($iou['company_code'], $iou['employee_code']);
        display_pety_cash_detail_row($emp_full_name,null);
        display_pety_cash_detail_row($iou["type_name"],null);
        display_pety_cash_detail_row($iou["project_description"],null);
        display_pety_cash_detail_row($iou["float_name"],null);
        display_pety_cash_detail_row(sql2date($iou["tran_date"]),"align=center");
        amount_decimal_cell($iou["amount"]);
//        display_pety_cash_detail_row($iou["comments"],null);
//        amount_decimal_cell($iou["advance_amount"]);
        amount_decimal_cell($iou["batch_advance_amount"]);
        $total += $iou["batch_advance_amount"];
        $iou_batch_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $iou['batch_no']);
        display_pety_cash_detail_row($iou_batch_link,"align=center");
        
//        $link = get_petty_cash_trans_view_str(ST_PETTY_CASH_FLOAT_REIMBUR, $iou['reimbursed_amount']);
//        label_cell($link);
        $link = get_petty_cash_float_reimbursement_details_view_str(ST_I_OWE_YOU, $iou['id'], number_format($iou['reimbursed_amount'],2));
//        $link = get_petty_cash_float_reimbursement_details_view_str(ST_I_OWE_YOU_ADVANCES, $iou['iou_advance_detail_id'], number_format($iou['reimbursed_amount'],2));
        label_cell($link, "class='tableheader2' align=right");
//        display_pety_cash_detail_row($link,"align=right");

        end_row();
        
        $previous_float_id = $iou['float_id'];
        $float_name = $iou["float_name"];
    }

    display_float_total_row($float_name, $float_total);
        
    start_row();    
        label_cell("I Owe You advances batch total:", "colspan=7 align=right");
        amount_cell($total, true); 
    end_row();

    end_table(1);  
    
    div_end();   
}

function display_i_owe_you_advances_batch_header_details($id)
{
    start_table(TABLESTYLE2, "width=40%");

    $th = array("Batch #", _("Date"), _("Amount"));
    table_header($th);
    
    $iou = view_i_owe_you_advances_batch_header($id);
       
        start_row(); 
        label_cell($iou['batch_no'], "align=center");
        label_cell(sql2date($iou['tran_date']), "align=center");
        amount_decimal_cell($iou['amount']);
        end_row();
    
    end_table();
}


display_heading("I Owe You Advances Batch No: " . $id);
br();

display_i_owe_you_advances_batch_header_details($id);
br(2);
display_i_owe_you_advances_details($id);

end_page(true, false, false, ST_WORKORDER, $woid);

?>
