<?php
/**********************************************************************
***********************************************************************/
function add_wo_process_manufactured_qty($process_id, $process_qty, $work_start_time, $work_end_time, 
                                            $comment, $supplier_id, $supp_rate)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."wo_process_manufacture_qty( 
                    process_id, quantity, 
                    start_date_time,end_date_time,supplier_id,
                    supp_rate, comment,
                    created_by, created_on) 
            VALUES (".db_escape($process_id).", ".db_escape($process_qty) . ","
                    .db_escape($work_start_time) . ", ".db_escape($work_end_time) . ",".db_escape($supplier_id) . ","
                    .db_escape($supp_rate) . ",".db_escape($comment) . ","
                    .db_escape($created_by) . ", ".db_escape($created_on).")";
    
	db_query($sql,"Could not add work order manufacture quantity");
    
}
function add_wo_process_booking_labour($process_id, $work_type, $employee_code, $employee_name,
                                $comment, $work_start_time, $work_end_time,
                                $is_return, $company_code, $rate_category_id,
                                $rate,$supplier_id)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
        
        
	$sql = "INSERT INTO ".TB_PREF."wo_process_manufacture_labour( 
                    process_id, work_type, employee_code, employee_name,
                    start_date_time, end_date_time, is_return,
                    company_code, rate_category_id, rate,supplier_id,
                    comment, created_by, created_on) 
            VALUES (".db_escape($process_id).", ".db_escape($work_type).", ".db_escape($employee_code).", ".db_escape($employee_name).", "
                    .db_escape($work_start_time) . ", ".db_escape($work_end_time) . ",".db_escape($is_return) .","
                    .db_escape($company_code). ",".db_escape($rate_category_id). ",".db_escape($rate).",".db_escape($supplier_id).",".db_escape($comment).","
                    .db_escape($created_by) . ", ".db_escape($created_on).")";
	db_query($sql,"Could not add work order manufacture labour");
    
}
function get_employee_labour_booking_details($employee_code, $start_date_time, $end_date_time)
{
    $sql=" SELECT start_date_time,end_date_time 
           FROM ".TB_PREF."wo_process_manufacture_labour
           WHERE employee_code=".db_escape($employee_code)." 
                AND  end_date_time > ". db_escape($start_date_time)." 
                AND start_date_time < ". db_escape($end_date_time)." 
                AND is_return = 0";

    
    $result=db_query($sql,"Could not get_employee_labour_booking_details");   
    return db_fetch($result);
    
}

function get_process_qty_booking_details($process_id, $start_date_time, $end_date_time)
{
    $sql=" SELECT start_date_time,end_date_time 
           FROM ".TB_PREF."wo_process_manufacture_qty
           WHERE process_id=".db_escape($process_id)." 
                AND end_date_time > ". db_escape($start_date_time)." 
                AND start_date_time < ". db_escape($end_date_time)." 
                AND quantity > 0";

    
    $result=db_query($sql,"Could not get_process_qty_booking_details");   
    return db_fetch($result);
    
}

function get_sql_for_order_booking_inquiries_qty($w_no,$wo='',$workcentre,$from_date,$to_date)
{
    $sql = "SELECT
		wo.id work_order_id,
                wc.name wc_name,		
               	CONCAT(wp.sequence,' - ',wp.description) process,		
                wp.item_description,
                wpm.quantity item_quantity,
                wpm.start_date_time,
                wpm.end_date_time
		FROM    ".TB_PREF."wo_process_manufacture_qty as wpm,"                                               	
			.TB_PREF."workcentres as wc,"
                        .TB_PREF."workorders as wo,"
                        .TB_PREF."wo_processes as wp
		WHERE wpm.process_id=wp.id AND wp.workorder_id=wo.id AND wp.workcentre_id=wc.id  ";

	if ($wo !='')
	{
		$sql .= " AND wo.id=". db_escape($wo);
	}

	if ($workcentre != 0 )
	{
		$sql .= " AND wc.id=".db_escape($workcentre);
	}
        if ($from_date != null )
	{
		$sql .= " AND wpm.start_date_time >=". db_escape($from_date." ".'00:00:00') ;
	}
        if ($to_date != null )
	{
		$sql .= " AND wpm.start_date_time <=". db_escape($to_date." ".'23:59:00');
	}
        if ($w_no == '1')
	{
		$sql .= " AND wo.id > 0 ";
	}
        if ($w_no == '2')
	{
		$sql .= " AND wo.id < 0 ";
	}

	return $sql;
    
    
}

function get_sql_for_order_booking_inquiries_labor($w_no,$wo='',$employee_code,$workcentre,$work_type,$from_date,$to_date)
{
    $sql = "SELECT
		wo.id work_order_id,
                wc.name wc_name,		
               	CONCAT(wp.sequence,' - ',wp.description) process,
		wpm.work_type work_type,
                em.employee_code,
                em.initials,
                em.last_name,
                em.first_name,
                wpm.start_date_time,
                wpm.end_date_time
		FROM    ".TB_PREF."wo_process_manufacture_labour as wpm,"                                            	
			.TB_PREF."workcentres as wc,"
                        .TB_PREF."workorders as wo,"
			.TB_PREF."wo_processes as wp,"
                        .TB_PREF."employee_master as em 
		WHERE wpm.process_id=wp.id AND wp.workorder_id=wo.id AND wp.workcentre_id=wc.id AND wpm.employee_code=em.employee_code ";

	if ($wo !='')
	{
		$sql .= " AND wo.id=". db_escape($wo);
	}
        if ($employee_code !='')
	{
		$sql .= " AND em.employee_code=". db_escape($employee_code);
	}

	if ($workcentre != 0 )
	{
		$sql .= " AND wc.id=".db_escape($workcentre);
	}
        if ($work_type != 0 )
	{
		$sql .= " AND wpm.work_type=".db_escape($work_type);
	}
        if ($from_date != null )
	{
		$sql .= " AND wpm.start_date_time >=". db_escape($from_date." ".'00:00:00') ;
	}
        if ($to_date != null )
	{
		$sql .= " AND wpm.start_date_time <=". db_escape($to_date." ".'23:59:00');
	}
        if ($w_no == '1')
	{
		$sql .= " AND wo.id > 0 ";
	}
        if ($w_no == '2')
	{
		$sql .= " AND wo.id < 0 ";
	}

	return $sql;
    
    
}

function get_employee_current_rate($date,$employee_code)
{
    $sql="  SELECT ed.rate_category_id, er.rate 
            FROM ".TB_PREF."employee_departments ed 
                    LEFT JOIN ".TB_PREF."employee_rates er ON 
                            er.id = ed.rate_category_id
            WHERE  ed.start_date <=".db_escape(date2sql($date))." AND ed.employee_code=".db_escape($employee_code)."  
            ORDER BY ed.start_date DESC LIMIT 1";
    
    $result=db_query($sql,"Could not get employee labour rate details");   
    return db_fetch($result);
}
?>