<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_MANUFTRANSVIEW';
$path_to_root = "../..";
include_once($path_to_root . "/includes/session.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
page(_($help_context = "View Work Order Issue"), true, false, "", $js);

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 . "/manufacturing/includes/manufacturing_db.inc");
include_once($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");

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

if ($_GET['trans_no'] != "")
{
	$wo_issue_no = $_GET['trans_no'];
}
function get_work_order_issue_data($issue_no)
{
	$sql = "SELECT woi.issue_no,woi.reference,woi.workorder_id,sm.stock_id,sm.description,l.location_name,wc.name work_centre,woi.issue_date
                
		FROM ".TB_PREF."wo_issues woi
                LEFT JOIN ".TB_PREF."workorders wo ON
                    woi.workorder_id = wo.id
                LEFT JOIN ".TB_PREF."stock_master sm ON 
                    sm.stock_id = wo.stock_id
		LEFT JOIN ".TB_PREF."locations l ON
                    woi.loc_code = l.loc_code
                LEFT JOIN ".TB_PREF."workcentres wc ON
                    wc.id = woi.workcentre_id                    
		WHERE woi.issue_no=".db_escape($issue_no);
    $result = db_query($sql, "A work order issue could not be retrieved");

    return db_fetch($result);
}


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

function display_wo_issue($issue_no)
{
    $myrow = get_work_order_issue_data($issue_no);

	br(1);
    start_table(TABLESTYLE);
    $th = array(_("Issue #"), _("Reference"), _("For Work Order #"),
    	_("Item"), _("From Location"), _("To Work Centre"), _("Date of Issue"));
    table_header($th);

	start_row();
	label_cell($myrow["issue_no"]);
	label_cell($myrow["reference"]);
	label_cell(get_trans_view_str(ST_WORKORDER,$myrow["workorder_id"]));
	label_cell($myrow["stock_id"] . " - " . $myrow["description"]);
	label_cell($myrow["location_name"]);
	label_cell($myrow["work_centre"]);
	label_cell(sql2date($myrow["issue_date"]));
	end_row();

    comments_display_row(28, $issue_no);

	end_table(1);

	is_voided_display(28, $issue_no, _("This issue has been voided."));
}

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

function display_wo_issue_details($issue_no)
{
    $result = get_work_order_issue_details($issue_no);

    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no items for this issue."));
    }
    else
    {
        start_table(TABLESTYLE);
        $th = array(_("Component"), _("Quantity"), _("Units"));

        table_header($th);

        $j = 1;
        $k = 0; //row colour counter

        $total_cost = 0;

        while ($myrow = db_fetch($result))
        {

			alt_table_row_color($k);

        	label_cell($myrow["stock_id"]  . " - " . $myrow["description"]);
            qty_cell($myrow["qty_issued"], false, get_qty_dec($myrow["stock_id"]));
			label_cell($myrow["units"]);
			end_row();;

        	$j++;
        	If ($j == 12)
        	{
        		$j = 1;
        		table_header($th);
        	}//end of page full new headings if
		}//end of while

		end_table();
    }
}

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

display_heading($systypes_array[ST_MANUISSUE] . " # " . $wo_issue_no);

display_wo_issue($wo_issue_no);

display_heading2(_("Items for this Issue"));

display_wo_issue_details($wo_issue_no);

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

echo "<br>";

end_page(true, false, false, ST_MANUISSUE, $wo_issue_no);

?>

