<?php
/**********************************************************************
***********************************************************************/
function add_sales_order_approvers($user_id, $value_cost, $percentage_cost)
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."sales_order_approvers (
                        user_id, value_cost, 
                        percentage_cost,
                        created_by,created_on) 
                VALUES (".db_escape($user_id).", ".db_escape($value_cost).", "
                        .db_escape($percentage_cost).", "
                        .db_escape($id_user).", ".db_escape($datetime).")";

        db_query($sql,"Sales order approver could not be added");
        
        return db_insert_id();
}

function update_sales_order_approvers($id, $user_id, $value_cost, $percentage_cost)
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
                
        $sql = "UPDATE ".TB_PREF."sales_order_approvers 
                SET updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime).",
                    user_id=".db_escape($user_id).",
                    value_cost=".db_escape($value_cost).",
                    percentage_cost=".db_escape($percentage_cost)." 
                WHERE id = ".db_escape($id)." " ;

        db_query($sql,"Sales order approver could not be updated");
        
        return db_num_affected_rows();
}
//
function delete_sales_order_approvers($id)
{
	$sql="DELETE FROM ".TB_PREF."sales_order_approvers WHERE id= ".db_escape($id);
	db_query($sql,"Sales order approver could not be deleted");			
}
//
function get_sales_order_approvers()
{
        $sql = "SELECT soa.*,
                        u.real_name
            FROM ".TB_PREF."sales_order_approvers soa,
                    ".TB_PREF."users u
            WHERE 
                    soa.user_id = u.id
                ";
	
	return db_query($sql,"Sales order approver could not be retreived");
}
//
function get_sales_order_approver($id)
{
	$sql = "SELECT * FROM ".TB_PREF."sales_order_approvers WHERE id=".db_escape($id);
	
	$result = db_query($sql,"Sales order approver detail could not be retreived");
	
	return db_fetch($result);
}

function approve($value_loss, $percentage_loss, $current_user)
{
    $sql = "SELECT * FROM ".TB_PREF."sales_order_approvers 
        WHERE user_id=".db_escape($current_user)." AND
            value_cost = ".db_escape($value_loss)." AND
                percentage_cost = ".db_escape($percentage_loss)."
            ";
	
    $result = db_query($sql,"Sales order approver detail could not be retreived");
    return db_fetch($result);
}

function get_sql_for_so_view($selected_customer, $location = "", $so_no = null,  $reference = "", 
            $stock_item = null, $current_user)
{
        $sql = "SELECT 
			sorder.order_no,
			sorder.reference,
			debtor.name,
                        branch.br_name,
                        sorder.ord_date,
			sorder.delivery_date,
			sorder.deliver_to,
			Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost AS OrderValue,
			sorder.type,
			debtor.curr_code,
			Sum(line.qty_sent) AS TotDelivered,
			Sum(line.quantity) AS TotQuantity,
                         sorder.approval_status
		FROM ".TB_PREF."sales_orders as sorder, "
			.TB_PREF."sales_order_details as line, "
			.TB_PREF."debtors_master as debtor, "
			.TB_PREF."cust_branch as branch
			WHERE 
                            sorder.approval_status = 20
                        AND sorder.order_no = line.order_no
			AND sorder.trans_type = line.trans_type
			AND sorder.debtor_no = debtor.debtor_no
			AND sorder.branch_code = branch.branch_code
			AND debtor.debtor_no = branch.debtor_no";

	if (isset($so_no) && $so_no != "")
	{
		// search orders with number like 
		$number_like = "%".$so_no;
		$sql .= " AND sorder.order_no LIKE ".db_escape($number_like);
//				." GROUP BY sorder.order_no";
	}
	elseif ($reference != "")
	{
		// search orders with reference like 
		$number_like = "%".$reference."%";
		$sql .= " AND sorder.reference LIKE ".db_escape($number_like);
//				." GROUP BY sorder.order_no";
	}

	
		if ($selected_customer != -1)
			$sql .= " AND sorder.debtor_no=".db_escape($selected_customer);

		if (isset($stock_item))
			$sql .= " AND line.stk_code=".db_escape($stock_item);

		if ($location)
			$sql .= " AND sorder.from_stk_loc = ".db_escape($location);
		

		$sql .= " GROUP BY sorder.order_no,
					sorder.debtor_no,
					sorder.branch_code,
					sorder.customer_ref,
					sorder.ord_date,
					sorder.deliver_to";
	return $sql;

}

?>