<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
function add_item_price1($stock_id, $sales_type_id, $curr_abrev, $price,$lower_qty = 0,$upper_qty = 0)
{
	$sql = "INSERT INTO ".TB_PREF."prices (stock_id, sales_type_id, curr_abrev, price,lower_qty,upper_qty) 
		VALUES (".db_escape($stock_id).", ".db_escape($sales_type_id)
		.", ".db_escape($curr_abrev).", ".db_escape($price).", ".db_escape($lower_qty).", ".db_escape($upper_qty).")";

	db_query($sql,"an item price could not be added");
}

function update_item_price1($price_id, $sales_type_id, $curr_abrev, $price,$lower_qty = 0,$upper_qty = 0)
{
	$sql = "UPDATE ".TB_PREF."prices SET sales_type_id=".db_escape($sales_type_id).", 
		curr_abrev=".db_escape($curr_abrev).", 
		price=".db_escape($price).",lower_qty=".db_escape($lower_qty).",upper_qty=".db_escape($upper_qty)." WHERE id=".db_escape($price_id);

	db_query($sql,"an item price could not be updated");
}


function get_qty_price($stock_id,$currency,$qty)
{
	$sql = "SELECT * FROM ".TB_PREF."prices WHERE stock_id=".db_escape($stock_id)." and "
                . " curr_abrev=".db_escape($currency)." and ".db_escape($qty)." BETWEEN lower_qty and upper_qty ";

	$result = db_query($sql,"price could not be retreived");

	return db_fetch($result);
}


function check_price_range($selected_id,$stock_id,$qty)
{
    $where = "";
    if(!empty($selected_id) && $selected_id != -1){
        $where .= " and id!=".db_escape($selected_id);
    }
	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."prices WHERE ".  db_escape($qty)." BETWEEN lower_qty and upper_qty "
                . " and stock_id=".db_escape($stock_id)." ".$where);
}
