<?php
/**********************************************************************
***********************************************************************/
function get_wo_requirements($woid)
{
	$sql = "SELECT ".TB_PREF."wo_requirements.*, ".TB_PREF."stock_master.description,
		".TB_PREF."stock_master.mb_flag, 
		".TB_PREF."locations.location_name, 
		".TB_PREF."workcentres.name AS WorkCentreDescription FROM 
		(".TB_PREF."wo_requirements, ".TB_PREF."locations, "
		.TB_PREF."workcentres) INNER JOIN ".TB_PREF."stock_master ON 
		".TB_PREF."wo_requirements.stock_id = ".TB_PREF."stock_master.stock_id 
		WHERE workorder_id=".db_escape($woid)."
		AND ".TB_PREF."locations.loc_code = ".TB_PREF."wo_requirements.loc_code
		AND ".TB_PREF."workcentres.id=workcentre";

	return db_query($sql, "The work order requirements could not be retrieved");    
}

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

function create_wo_requirements($woid, $stock_id)
{

    $p = get_wo_processes($woid);	
	while ($process = db_fetch($p)){
        $m = get_wo_process_material($process['id']);
        while ($item = db_fetch($m)) 
        {
            $units_issued = 0;
            $sql = "INSERT INTO ".TB_PREF."wo_requirements (
                            workorder_id, process_id, 
                            stock_id, workcentre, 
                            units_req, req_unit, 
                            req_unit_qty, req_unit_cf, 
                            std_cost, loc_code, 
                            units_issued)
                    VALUES (".db_escape($process['workorder_id']).", ".db_escape($process['id']).", "
                            .db_escape($item['stock_id']).", ".db_escape($process['workcentre_id']).", "
                            .db_escape($item['units_req']).", ".db_escape($item['req_unit']).", "
                            .db_escape($item['req_unit_qty']).", ".db_escape($item['req_unit_conversion_factor']).", "
                            .db_escape($item['std_cost']).", ".db_escape($item['loc_code']).", "
                            .db_escape($units_issued).")";

            db_query($sql, "The work order requirements could not be added");
        }
    }
}

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

function delete_wo_requirements($woid)
{
	$sql="DELETE FROM ".TB_PREF."wo_requirements WHERE workorder_id=".db_escape($woid);
	db_query($sql,"The work order requirements could not be deleted");	
}


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

function update_wo_requirement_issued($woid, $stock_id, $quantity)
{
	$sql = "UPDATE ".TB_PREF."wo_requirements SET units_issued = units_issued + ".db_escape($quantity)."
		WHERE workorder_id = ".db_escape($woid)." AND stock_id = ".db_escape($stock_id);
				
	db_query($sql, "The work requirements issued quantity couldn't be updated");
}

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

function void_wo_requirements($woid)
{
	$sql = "UPDATE ".TB_PREF."wo_requirements SET units_issued = 0 WHERE workorder_id = "
	.db_escape($woid);

	db_query($sql, "The work requirements issued quantity couldn't be voided");	
}

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

?>