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

include($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 . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/approve_i_owe_you_enties_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_payments_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_expenses_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_inquiry_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_expenses_entry_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 . "/petty_cash/includes/db/i_owe_you_advances_inquiry_db.inc");

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

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

page(_($help_context = "View Petty Cash Expense"), true, false, "", $js);

function display_pce_header($myrow)
{
    global $approval_statuses;
    
    $petty_cash_id = $myrow['id'];
    $batch_no = $myrow['batch_no'];
    
    display_heading("Petty Cash Expense #".$petty_cash_id);
    
    if($myrow['batch_no'] > 0){
        $header_text = 'Petty Cash Expense' . " #$petty_cash_id"." (Batch #".$myrow['batch_no'].")";
    }
    else{
        $header_text = 'Petty Cash Expense' . " #$petty_cash_id.";
    }
    
    br();
    
//    $myrow = view_i_owe_you_header($iou_id);

    start_table(TABLESTYLE2, "width=50%");

    start_row();

//    $iou_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU, $myrow['iou_id']);
//    label_cells(_("I Owe You No"), $iou_link, "class='tableheader2'");
//    end_row();
    label_cells(_("Reference"), $myrow['reference'], "class='tableheader2'");
    end_row();
    label_cells(_("Type"), $myrow['type_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Project"), $myrow['project_description'], "class='tableheader2'");
    end_row();

    
    $cmp_name = get_group_company_name($myrow['company_code']);
    label_cells(_("Company"), $cmp_name, "class='tableheader2'");
    end_row();
    
    $emp_name = get_group_company_employee_formatted_long_name($myrow['company_code'], $myrow['employee_code']);
    label_cells(_("Employee"), $emp_name, "class='tableheader2'");
    end_row();
    label_cells(_("Float"), $myrow['float_name'], "class='tableheader2'");
    end_row();
    
    label_cells(_("Date"), sql2date($myrow['tran_date']), "class='tableheader2'");
    end_row();
    label_cells(_("Amount"), number_format($myrow['amount'],2), "class='tableheader2'");
    end_row();
    
//    $advance_amount_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU_ADVANCES, $petty_cash_id, $myrow["advance_amount"]);
//    label_cells(_("Advances"), $advance_amount_link, "class='tableheader2'");
//    end_row();
    label_cells(_("Comment"), $myrow['comments'], "class='tableheader2'");
    end_row();
    
    $batch = get_i_owe_you_batch_header($batch_no);
    if($myrow['batch_complete'] == 1){
        $status = $approval_statuses[AS_COMPLETED];
    }
    else{
        $status = $approval_statuses[$batch['approval_status']];
    }
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $batch_no."&source_type_id=".ST_I_OWE_YOU_BATCH, "Approval Status");
    label_cells($s."(Batch)",$status, "class='tableheader2'");
//    label_cells(_("Approval Status"), $s, "class='tableheader2'");
    end_row();
    label_cells(_("Batch No"), get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $batch_no), "class='tableheader2'");
    end_row();
    
    end_table();
    
}



function view_pce_details($iou_id)
{
    display_heading2("I Owe You Details");
    
    $result = view_i_owe_you_details($iou_id);

    start_table(TABLESTYLE, "width=70%");

    $th = array(_("Budget"), _("Amount"), _("Comment"));
    table_header($th);

    $iou_total = 0;
    $k=0; //row colour counter

    while ($iou_line = db_fetch($result))
    {
        alt_table_row_color($k);

        //label_cells('',$myrow["id"]);
        label_cell($iou_line["task_description"]);
        amount_decimal_cell($iou_line["amount"]);
        label_cell($iou_line["comments"]);
        
        $iou_total += $iou_line["amount"];
    }

    start_row();
        label_cell("Total", "colspan=1 align=right");
        amount_cell($iou_total, true);
    end_row();
    
    end_table(1);
    div_end();
}



if (isset($_GET["trans_no"]))
{
	$trans_no = $_GET["trans_no"];
}

$myrow = view_petty_cash_expense_header($trans_no);

//view_pce_header($trans_no, $myrow);
display_pce_header($myrow);

br();

$total_paid = display_petty_cash_payments(ST_PETTY_CASH, $trans_no, false);

//br();

display_i_owe_you_batch_expences($myrow['batch_no'], $total_paid);
//view_pce_details($trans_no);

end_page(true, false, false, ST_BANKPAYMENT, $trans_no);
?>