<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_ASSET_MOVEMENT_INQ';
$path_to_root = "../..";

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/assets/includes/db/asset_movements_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
page(_($help_context = "Asset Movements Inquiry"), false, false, "", $js);

if (isset($_GET['Approves']))
{
    $_SESSION['current_user_for_mr'] = $_SESSION['wa_current_user']->user;
}


function display_asset_movements_inquiry($asset_id, $from_date, $to_date,
                                            $project_id, $location_code, $incl_tool_issues)                                           
{
    if(!($asset_id > 0)){
        display_note("Please selct an asset.");
        return;
    }
    
    $result = get_asset_movements($asset_id, $from_date, $to_date,
                                  $project_id, $location_code, $incl_tool_issues);  
                            
    global $systypes_array, $Ajax;
    
    div_start('asset_movement_inquiry');
    $Ajax->activate('asset_movement_inquiry');
    
    start_table(TABLESTYLE, "width=95%");
    $th = array(_('ID'), _('Type'), _('Tran #'),
                _('Reference'), _('Date'),
                _('Project'), _("Location"), _("Qty"),_("Balance"));              

    table_header($th);

    $balance = get_asset_balance($asset_id, $from_date, $project_id, $location_code);
    start_row("class='inquirybg'");
    label_cell("<b>"._("Total quantity before") . " " . $from_date."</b>", "align=center colspan = 8");
    qty_cell($balance, false, 0);
    end_row();
    
    $moves_total = 0;
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);
        
        label_cell($myrow['id'], "align=right");
        label_cell($systypes_array[$myrow['trans_type']]);
              
        label_cell(get_asset_trans_view_str($myrow['trans_type'], $myrow['trans_no']), "align=center");
        label_cell($myrow['reference']);
        label_cell(sql2date($myrow['tran_date']), "align=center");
             
//        label_cell($myrow['asset_code']."-".$myrow['asset_description']."-".$myrow['asset_id']); 
        
//        $p_details = get_project($project_id);
//        $description = $p_details['description'];
//        $department_name = $p_details['department_name'];
        label_cell($myrow['project_description']);
        label_cell($myrow['asset_location_name']);
        
//        label_cell($myrow['task_description']);
        label_cell($myrow['qty'], "align=right");
        
        $moves_total += $myrow['qty'];
        $balance += $myrow['qty'];
        label_cell($balance, "align=right");
    }    
    
    start_row("class='inquirybg'");
    label_cell("<b>"._("Total quantity after") . " " . $to_date."</b>", "align=center colspan = 7");
    qty_cell($moves_total, false, 0);
    qty_cell($balance, false, 0);
    end_row();
    
    end_table();
    div_end();
}


if (!@$_GET['popup'])
	start_form();

start_table();
    start_row();    
        asset_list_cells("Asset:", 'asset_id', null, 'Select an asset', true, null);  
    //    asset_transaction_types_list_cells("Asset Tran Type", 'asset_tran_type_id', null, '', true);
    end_row();
    
end_table();

start_table();
    date_cells(_("From Date:"), 'from_date', '', null, -60, null, null, null, true);
    date_cells(_("To Date:"), 'to_date', '', null, 0, null, null, null, true);
    //end_table();
    //
    //start_table();

    yesno_list_cells("Include Tool Issues", 'incl_tool_issues');
    asset_locations_list_cells("Location:", 'location_code', null, '', true);
    project_list_cells("Project:", 'project_id', null, '', true);

    submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
end_table(1);


$Ajax->activate('asset_moves_tbl');

div_start('asset_moves_tbl');

display_asset_movements_inquiry($_POST['asset_id'], $_POST['from_date'], $_POST['to_date'], 
                                $_POST['project_id'], $_POST['location_code'], $_POST['incl_tool_issues']);


div_end();

//---------------------------------------------------------------------------------------------

submit_center('Update', _("Update"), true, '', null);

if (!@$_GET['popup'])
{
    end_form(2);
    end_page();
}



?>
