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



function update_scheduled_workorder_wo_no($scheduled_trans_no, $wo_no, $wo_reference)
{
        $swo = get_scheduled_workorder_header($scheduled_trans_no);
        
        $sql = "UPDATE ".TB_PREF."workorders  
                SET 
                    id =".db_escape($wo_no).",
                    wo_ref=".db_escape($wo_reference).",
                    row_state =". db_new_row_state($swo['row_state'])."  
                WHERE id=".db_escape($scheduled_trans_no)." 
                    AND row_state = ".db_escape($swo['row_state']);

        db_query($sql,"Could not update_scheduled_workorder_wo_no");
            
        $num_rows = db_num_affected_rows();
        if($num_rows == 1){
            return array("success" => true, "message" => "");
        }
        else{
            return array("success" => false, "message" => "Another user has updated workorder $scheduled_trans_no");
        }
            
}


function update_scheduled_workorder_process_wo_no($scheduled_trans_no, $trans_no)
{
    $sql = "UPDATE ".TB_PREF."wo_processes  
            SET workorder_id =".db_escape($trans_no)."  
            WHERE workorder_id=".db_escape($scheduled_trans_no);

        db_query($sql,"Could not update_scheduled_workorder_process_wo_no");
}

function update_scheduled_workorder_material_wo_no($scheduled_trans_no, $trans_no)
{
    $sql = "UPDATE ".TB_PREF."wo_material   
            SET workorder_id =".db_escape($trans_no)."  
            WHERE workorder_id=".db_escape($scheduled_trans_no);

        db_query($sql,"Could not update_scheduled_workorder_material_wo_no");
}


function update_production_schedule_detail_order_no($id, $trans_no, $row_state)
{
        $sql = "UPDATE ".TB_PREF."production_schedule_details  
                SET 
                    trans_no =".db_escape($trans_no).",
                    selected = 0,
                    row_state =". db_new_row_state($row_state)."  
                WHERE id=".db_escape($id)." 
                    AND row_state = ".db_escape($row_state);

        db_query($sql,"Could not update_production_schedule_detail_order_no");
            
        $num_rows = db_num_affected_rows();
        if($num_rows == 1){
            return array("success" => true, "message" => "");
        }
        else{
            return array("success" => false, "message" => "Another user has updated production schedule detail $id");
        }
            
}


function update_production_schedule_secondary_wo_no($id, $scheduled_wo_no, $wo_no)
{
        $sql = "UPDATE ".TB_PREF."production_schedule_details  
                SET source_id =".db_escape($wo_no)." 
                WHERE source_type =".db_escape(ST_WORKORDER)." 
                    AND trans_no = 0 
                    AND source_id = ".db_escape($scheduled_wo_no);

        db_query($sql,"Could not update_production_schedule_secondary_wo_no");
            
}


function get_all_production_schedule_unconfirmed_records()
{
    $sql = "SELECT prd.* 
            FROM ".TB_PREF."production_schedule_details prd                     
            WHERE trans_no = 0";  
	
	$result = db_query($sql,"Could not get_all_production_schedule_unconfirmed_records");	
        
    return $result;
    
}

function get_scheduled_workorder_header($swo_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."workorders                      
            WHERE id < -99 AND id = ".db_escape($swo_id);  
	
	$result = db_query($sql,"Could not get_scheduled_workorder_header");	
        
    return db_fetch($result);
    
}


function get_all_unconfirmed_workorders()
{
    $sql = "SELECT id  
            FROM ".TB_PREF."workorders                      
            WHERE id < -99";  
	
	$result = db_query($sql,"Could not get_all_unconfirmed_workorders");	
        
    return $result;
    
}

function get_production_schedule_details($ps_id, $order_type = 0)
{
    $sql = "SELECT prd.*, sm.description item_description, l.location_name, u.name tran_unit_name 
            FROM ".TB_PREF."production_schedule_details prd,
                    ".TB_PREF."stock_master sm,
                    ".TB_PREF."locations l,
                    ".TB_PREF."item_units u 
            WHERE prd.stock_id = sm.stock_id 
                    AND prd.loc_code = l.loc_code  
                    AND prd.tran_unit = u.abbr 
                    AND production_schedule_id = ".db_escape($ps_id);
    
    if($order_type > 0){
        $sql .= " AND prd.tran_type = ".db_escape($order_type);
    }
    
    $sql .= " ORDER BY prd.id ";  
	
	$result = db_query($sql,"Could not get_production_schedule_details");	
        
    return $result;
    
}


function get_production_schedule_details_for_confirmation($ps_id)
{
    $sql = "SELECT prd.*, sm.description item_description, l.location_name, u.name tran_unit_name 
            FROM ".TB_PREF."production_schedule_details prd,
                    ".TB_PREF."stock_master sm,
                    ".TB_PREF."locations l,
                    ".TB_PREF."item_units u 
            WHERE prd.stock_id = sm.stock_id 
                    AND prd.loc_code = l.loc_code  
                    AND prd.tran_unit = u.abbr 
                    AND production_schedule_id = ".db_escape($ps_id)." 
                    AND ((source_type = ".db_escape(ST_MANUF_REQUIREMENT)." AND trans_no = 0) OR 
                                    (source_type = ".db_escape(ST_WORKORDER)." AND source_id > 0 AND trans_no = 0))
            ORDER BY prd.id ";  
    
    
    $result = db_query($sql,"Could not get_production_schedule_details");	
        
    return $result;
    
}

function get_production_schedule_details_for_workorders($ps_id)
{
    $sql = "SELECT prd.* 
            FROM ".TB_PREF."production_schedule_details prd 
            WHERE prd.tran_type = ".db_escape(ST_WORKORDER)." 
                    AND prd.trans_no = 0 
                    AND prd.selected = 1 
                    AND production_schedule_id = ".db_escape($ps_id)." 
            ORDER BY prd.id ";  
	
	$result = db_query($sql,"Could not get_production_schedule_details_for_workorders");	
        
    return $result;
    
}

function get_production_schedule_detail_record($id)
{
    $sql = "SELECT prd.* 
            FROM ".TB_PREF."production_schedule_details prd                     
            WHERE id = ".db_escape($id);  
	
	return db_query($sql,"Could not get_production_schedule_detail_record");	
}

function get_production_schedule_details_summary_for_purch_reqs($ps_id)
{
    $sql = "SELECT distinct project_id, loc_code  
            FROM ".TB_PREF."production_schedule_details prd                     
            WHERE tran_type = ".ST_PURCH_REQ." 
                    AND selected = 1 
                    AND trans_no = 0 
                    AND production_schedule_id = ".db_escape($ps_id);  
	
	return db_query($sql,"Could not get_production_schedule_details_summary_for_purch_reqs");	
}

function get_production_schedule_details_for_purch_req($project_id, $loc_code)
{
    $sql = "SELECT prd.*,
                    mreq.id mreq_id,
                    mreq.order_no mreq_order_no,
                    mreq.row_state mreq_row_state 
            FROM ".TB_PREF."production_schedule_details prd 
                    LEFT JOIN ".TB_PREF."manufacturing_requirements mreq ON 
                            prd.source_type = ".ST_MANUF_REQUIREMENT." AND 
                            prd.source_id = mreq.id 
            WHERE prd.tran_type = ".ST_PURCH_REQ." 
                    AND prd.selected = 1 
                    AND prd.trans_no = 0 
                    AND prd.project_id = ".db_escape($project_id)."  
                    AND prd.loc_code = ".db_escape($loc_code);  
	
	return db_query($sql,"Could not get_production_schedule_details_for_purch_req");	
}

?>