<?php

function display_item_transactions_inquiry_selecting_parameters() {
    global $Ajax;
    
    if (! isset($_POST['stock_id'])) {
        $_POST['stock_id'] = get_global_stock_item();
    }

    $Ajax->activate('sm_filter');
    div_start('sm_filter');

        start_table(TABLESTYLE_NOBORDER);
            start_row();
                stock_items_list_cells(_("Item:"), 'stock_id', null, 'Select an item', true);
                if (strlen($_POST['stock_id']) > 0) {
                    set_global_stock_item($_POST['stock_id']);
                }
                text_cells("Item Description (%%)", 'filtering_item_description');
                project_list_cells(_("Project:"), 'project_id', null, _('Select a project'), true);   
            end_row();
        end_table();

        start_table(TABLESTYLE_NOBORDER);
            start_row();
                locations_list_cells(_("From Location:"), 'StockLocation', null, true, true);

                date_cells(_("From:"), 'AfterDate', '', null, -30);
                date_cells(_("To:"), 'BeforeDate', '', null, 0);
                
                $tran_types_array = build_item_transactions_tran_types_array();
                custom_values_list_cells('Trans Type:', 'type', $tran_types_array, null, 'All transaction types', true);
            end_row();
        end_table();

        start_table(TABLESTYLE_NOBORDER);
            start_row();
                $movement_types_array = build_item_transactions_inquiry_movement_types_array();
                custom_values_list_cells('Movement Type:', 'movement_type_id', $movement_types_array, null, 'All movement types', true);
                
                $linked_fields_array = build_item_transactions_inquiry_linked_fields_array();
                custom_values_list_cells('Linked Field:', 'linked_field', $linked_fields_array, null, false, true);
                
                if ($_POST['linked_field'] > 0) {
                    switch ($_POST['linked_field']) {
                        case IMLF_CUSTOMER:
                            customer_list_cells(_("Customer:"), 'linked_field_id', null, '', true);
                            break;
                        case IMLF_SUPPLIER:
                            supplier_list_cells(_("Supplier:"), 'linked_field_id', null, '', true);
                            break;
                        case IMLF_EMPLOYEE:
                            employees_simple_list_cells("Employee", 'linked_field_id', null, '', true);
                            break;
                        case IMLF_VEHICLE:
                            vehicles_list_cells('Vehicle:', 'linked_field_id', null, '', true);
                            break;
                        case IMLF_ASSET:	
                            label_cells(_("Asset:"), asset_list('linked_field_id', null, ''));	
                            break;
                    }
                } else {
                    $_POST['linked_field_id'] = -1;
                }
                
                submit_cells('SearchItemTrans', _("Search"),'', _('Refresh Inquiry'), 'default');
            end_row();
        end_table(2);

    div_end();
}

function build_item_transactions_tran_types_array() {
    global $systypes_array;
    
    $tran_types_array = array();
    
        $tran_types_array[ST_LOCTRANSFER] = $systypes_array[ST_LOCTRANSFER];
        $tran_types_array[ST_SUPPRECEIVE] = $systypes_array[ST_SUPPRECEIVE];
        $tran_types_array[ST_STOCK_COUNT] = $systypes_array[ST_STOCK_COUNT];
        $tran_types_array[ST_MATERIAL_ISSUE] = $systypes_array[ST_MATERIAL_ISSUE];
        $tran_types_array[ST_MATERIAL_RETURN] = $systypes_array[ST_MATERIAL_RETURN];
        $tran_types_array[ST_INVENTORY_RECEIPT] = $systypes_array[ST_INVENTORY_RECEIPT];
        $tran_types_array[ST_INVENTORY_ISSUE] = $systypes_array[ST_INVENTORY_ISSUE];
        $tran_types_array[ST_PR_ISSUE] = $systypes_array[ST_PR_ISSUE];
        $tran_types_array[ST_PR_RETURN] = $systypes_array[ST_PR_RETURN];
        $tran_types_array[ST_STOCK_RESERVATION] = $systypes_array[ST_STOCK_RESERVATION];
        $tran_types_array[ST_PETTY_CASH_ITEM_RECEIPT] = $systypes_array[ST_PETTY_CASH_ITEM_RECEIPT];
        $tran_types_array[ST_MATERIAL_TRANSFER_RECEIPT] = $systypes_array[ST_MATERIAL_TRANSFER_RECEIPT];
        $tran_types_array[ST_DAILY_ITEM_ISSUE] = $systypes_array[ST_DAILY_ITEM_ISSUE];
        $tran_types_array[ST_DAILY_ITEM_RETURN] = $systypes_array[ST_DAILY_ITEM_RETURN];
        $tran_types_array[ST_RAW_MATERIAL_ISSUE] = $systypes_array[ST_RAW_MATERIAL_ISSUE];
        $tran_types_array[ST_RAW_MATERIAL_RETURN] = $systypes_array[ST_RAW_MATERIAL_RETURN];
        $tran_types_array[ST_SITE_MATERIAL_RETURN] = $systypes_array[ST_SITE_MATERIAL_RETURN];
    
        asort($tran_types_array);
        
        return $tran_types_array;
}

function build_item_transactions_inquiry_movement_types_array($with_type=true) {
    global $systypes_array;
    
    $movement_types_array = array();
    
        $result = get_all_movement_types(true);
            while ($row = db_fetch($result)) {
                if ($with_type === true) {
                    $movement_types_array[$row['id']] = $systypes_array[$row['type']].' - '.$row['name'];
                } else {
                    $movement_types_array[$row['id']] = $row['name'];
                }
            }
    
        asort($movement_types_array);
        
        return $movement_types_array;
}

function build_item_transactions_inquiry_linked_fields_array() {
    global $inventory_movement_type_linked_fields;
    
    $linked_fields_array = array();
    
        $linked_fields_array[-1] = 'All linked fields';
        
        foreach ($inventory_movement_type_linked_fields as $key => $value) {
            $linked_fields_array[$key] = $value;
        }

        return $linked_fields_array;
}

function display_item_transactions_inquiry_details($stock_id='', $StockLocation='', $BeforeDate='', $AfterDate='', $project_id=0, $type=0, $movement_type_id=0, 
                                                   $linked_field=0, $linked_field_id=0, $filtering_item_description='') {
    global $systypes_array;
    
    $display_location = true;

    $item = get_item($stock_id);
        if (is_array($item)) {
            $item_unit = get_item_unit($item['units']);

            start_table(TABLESTYLE2);
                echo '<h3>Cost unit (Base Unit) of this item is "'.text_bold($item_unit['name']).'"</h3>';
            end_table(1);
        }
        
        $mv_tp_array = build_item_transactions_inquiry_movement_types_array(false);
        $mv_tp_array[-1] = 'No movement type';
        
        $linked_fields_array = build_item_transactions_inquiry_linked_fields_array();

        $result = get_item_transactions_inquiry($stock_id, $StockLocation, $BeforeDate, $AfterDate, $project_id, $type, $movement_type_id, 
                                                $linked_field, $linked_field_id, $filtering_item_description);
            start_table(TABLESTYLE, "width=99%");
                $th = array(_("ID"), _("Type"), _("#"));
                
                if ($display_location) array_push($th, _("Location"));
                    array_push($th, _("Date"), _("Project"), _("Item"), 
                                _("Movement Type"), _("Linked Field"), _("Linked Name"),
                                _("Tran Unit"), _("Tran Qty"), _("Base Unit"), _("Base Unit Qty"), 
                                _("Unit Cost"), _("Amount"));

                table_header($th);

                $dec = get_qty_dec($stock_id);
                $j = 1;

                $total_val = $total_qty = $total_in = $total_out = $k = 0;

                while($row = db_fetch($result)) {
                    alt_table_row_color($k);
                        $trandate = sql2date($row["tran_date"]);
                        $type = $row["type"];
                        $type_name = $systypes_array[$type];
                        
                        if ($row["qty"] > 0) {
                            $quantity_formatted = number_format2($row["qty"], $dec);
                            $total_in += $row["qty"];
                        } else {
                            $quantity_formatted = number_format2($row["qty"], $dec);
                            $total_out += $row["qty"];
                        }
//                        $after_qty += $row["qty"];

                        label_cell($row["trans_id"], "align=center");
                        label_cell($type_name);
                        label_cell(get_trans_view_str($type, $row["trans_no"]));
                        if($display_location) {
                            label_cell($row['location_name']);
                        }
                        label_cell($trandate);
                        label_cell($row["project_name"]);
                        
//                        if(strlen($row["item_trans_description"]) > 0){
//                            $item_trans_description = $row["item_trans_description"];
//                        }else{
//                            $item_trans_description = $row["item_description"];
//                        }
                            
                        $item_trans_description = $row["item_description"];
                        label_cell($row["stock_id"].' - '.$item_trans_description);
                        
                        
                        
                        if ($type == ST_RAW_MATERIAL_ISSUE || $type == ST_RAW_MATERIAL_RETURN) {
                            label_cell('');
                            label_cell($row['linked_field_type']);
                            $employee = get_group_company_employee_formatted_long_name($row['movement_type'], $row['linked_field_id']);
                                label_cell($employee);
                        } else {
                            $movement_type_name = $linked_field_type_name = $linked_field_value = '';
                            if($row['movement_type'] > 0){
                                $movement_type_name = $mv_tp_array[$row['movement_type']];
                                $linked_field_type_name = $linked_fields_array[$row['linked_field_type']];
                                $lf_v = get_movement_type_linked_field_values($row['linked_field_type'], $row['linked_field_id']);
                                $linked_field_value = $lf_v['value'];
                            }
                            label_cell($movement_type_name);
                            label_cell($linked_field_type_name);
                            label_cell($linked_field_value);
                        }
                        
                        label_cell($row["tran_unit_name"], 'align=center');
                        label_cell(number_format($row["tran_qty"],2), 'align=right');
                        label_cell($row["base_unit_name"], 'align=center');
                        label_cell($quantity_formatted, "nowrap align=right");
                        
                        label_cell(number_format($row["unit_cost"],2), 'align=right');
                        $line_cost = $row["qty"] * $row["unit_cost"];
                        $total_qty += $row["qty"];
                        label_cell(number_format($line_cost,2), 'align=right');
                        
//                        qty_cell($after_qty, false, $dec);
                    end_row();

                        $j++;
                        If($j == 12){
                            $j = 1;
                            table_header($th);
                        }
                        
                    $total_val += $line_cost;
                }
                
                start_row();
                    label_cell(text_bold("Totals:"), "align=right colspan=13");
                    amount_cell($total_qty,true);
                    label_cell('');
                    amount_cell($total_val,true);
                end_row();

            end_table(1);
            
        return $total_val;
}

?>