<?php


function display_i_owe_you_expense_details_inquiry_selecting_parameters() {
    global $expense_categories;
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("IOU No:"), 'iou_id', '',null, '', true);
            ref_cells(text_bold("Pet.Cash Expe. No:"), 'petty_expn_id', '',null, '', true);
            ref_cells(text_bold("Batch No:"), 'batch_no', '',null, '', true);
            ref_cells(("Description (% %):"), 'description', '',null, '', true);
        end_row();
    end_table();
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();  
            project_list_cells(_("Project:"), 'project_id', null, _('All projects'));
            group_companies_list_cells("Company:", 'company_code', null, '', true);
            group_company_employees_simple_list_cells("Employee:", 'employee_code', $_POST['company_code'], null, '', false);
            date_cells(_("From:"), 'from_date', '', null, -14);
            date_cells(_("To:"), 'to_date', '', null, +7);
        end_row();
    end_table();
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();    
            $available_expense_categories = $expense_categories; 
            unset($available_expense_categories[EC_ASSET_MAINTENANCE]); 
            custom_values_list_cells("Expense Category:", "expense_category_id", $available_expense_categories, null, "", true);
    
            switch ($_POST['expense_category_id']) {
                case EC_PETTY_CASH_EXPENSE:
                    hidden('expense_item_id', 0);
                    break;
                case EC_STOCK_ITEM_RECEIPT:
                    stock_items_list_cells(_("Item:"), 'expense_item_id', null, true, false);
                    break;
                case EC_VEHICLE_MAINTENANCE:
                    vehicles_list_cells("Vehicle", 'expense_item_id', null, '');
                    break;
                case EC_UTILITY_ITEM:
                    value_list_cells("Utility Item", 'expense_item_id', "UTILITY_ITEMS", null, '');
                    break;
                case EC_ASSET_MAINTENANCE:
                    label_cell('Issuing Tool'); 
                    echo "<td>";
                        echo asset_list('expense_item_id', null, false, true);
                    echo "</td>";   
                    break;

                default:
                    hidden('expense_item_id', 0);
                    $_POST['expense_item_id'] = 0;
                    break;
            }

            value_list_cells("Expense Type:", "expense_type_id", "PETTY_CASH_EXPENSE_TYPES", null, '');

            submit_cells('SearchOrders', _("Search"), '', _('Search'), 'process');
        end_row();
    end_table(2);
}

function display_i_owe_you_expense_details_inquiry($iou_id=0, $petty_expn_id=0, $batch_no=0, $description='', $project_id=0, $company_code='', 
                                                   $employee_code='', $from_date_, $to_date_, $expense_category_id=0, $expense_item_id=0, 
                                                   $expense_type_id=0) {
    global $expense_categories, $systypes_array, $Ajax;
    
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);
    
    $Ajax->activate('iou_pet_details');
    div_start('iou_pet_details');
    
        start_table(TABLESTYLE2, "width=99%");

            $th = array(_("Id"), _("Batch #"), _("Type"), /*_("Trans #"),*/ _("Project"), _("Employee"), _("Expense Category"), 
                        _("Expense Item"), _("Expense Type"), _("Budget"), _("Date"), _("Invoice No"), _("Description"), _("Amount"));
            table_header($th);

                if ($iou_id > 0 && $petty_expn_id > 0) {
                        end_table();
                    display_note('Cannot select an IOU number and a Petty Cash Expenses numbers at once.', 2, 3, "style=color:red");
                    div_end();
                    return;
                }
        
            $j = 1;
            $total = $k =0;
            $result = get_i_owe_you_expense_details_inquiry($iou_id, $petty_expn_id, $batch_no, $project_id, $description, $company_code, $employee_code, 
                                                            $from_date, $to_date, $expense_category_id, $expense_item_id, $expense_type_id);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k); 
                        
                        label_cell($row["id"], 'align=center');                        
                        $iou_batch_link = get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $row["iou_batch_no"]);
                        label_cell($iou_batch_link, 'align=center');
                        
                        label_cell($systypes_array[$row["type"]]);
//                        $trans_link = get_petty_cash_trans_view_str($row["type"], $row["iou_pet_header_id"]);
//                        label_cell($trans_link, 'align=center');
//                        label_cell($row["iou_pet_header_id"], 'align=center');
                        label_cell($row["project_name"]);
                        $employee_name = get_group_company_employee_formatted_long_name($row["company_code"], $row["employee_code"]);
                        label_cell($employee_name);
                        
                        if ($row["expense_category_id"] > 0) {
                            label_cell($expense_categories[$row["expense_category_id"]]);
                        } else {
                            label_cell('');
                        }

                        
                        $expense_item_name = '';
                        switch ($row['expense_category_id']) {
                            case EC_PETTY_CASH_EXPENSE:
                                $expense_item_name = '';
                                break;
                            case EC_STOCK_ITEM_RECEIPT:
                                $expense_item_name = '';
                                if ($row["stock_id"] > 0) {
                                    $expense_item_name = get_stock_item_formatted_name($row["stock_id"]);
                                }
                                break;
                            case EC_VEHICLE_MAINTENANCE:
                                $expense_item_name = get_vehicle_formatted_name($row["expense_item_id"]);
                                break;
                            case EC_UTILITY_ITEM:
                                $utility_item = get_listvalue_by_id($row["expense_item_id"]);
                                $expense_item_name = $utility_item['value_name'];
                                break;
                            case EC_ASSET_MAINTENANCE:
                                $expense_item_name = get_asset_formatted_name($row["expense_item_id"]);
                                break;

                            default:
                                break;
                        }
                        
                        label_cell($expense_item_name);                     

                        label_cell($row["expense_type_name"]);

                        label_cell($row["task_description"]);
                        label_cell(sql2date($row["expence_date"]));
                        label_cell($row["bill_no"]);
                        label_cell($row["expence_description"]);
                        amount_decimal_cell($row["approved_amount"]);  

                    end_row();

                    $j++;

                    if( $j == 12) {
                        $j = 1;
                        table_header($th);
                    }
                        
                    $total += $row["approved_amount"];
                }
    
                start_row();
                    label_cell(text_bold("Total:"), "align=right colspan=12");
                    amount_cell($total,true);
                end_row();

        end_table(3);
        
    div_end();
    
    return $total;
}

?>