<?php 

function add_charge($gl_code='', $name='', $is_percentage=0, $is_weight=0, 
	$predefined_rate=0, $is_negative=0)
{
	$sql = "INSERT INTO ".TB_PREF."charges (gl_code, name, is_percentage, is_weight, predefined_rate, is_negative) VALUES 
	(".db_escape($gl_code) . ", ".db_escape($name) . ", ".db_escape($is_percentage) . ", ".db_escape($is_weight) . ", ".db_escape($predefined_rate) . ", 
		".db_escape($is_negative) . ")";
   	db_query($sql,"The customer_charge could not be added");
}

function update_charge($selected_id, $gl_code='', $name='', $is_percentage=0, $is_weight=0, 
	$predefined_rate=0, $is_negative=0)
{
	$sql = "UPDATE ".TB_PREF."charges SET 
	gl_code=".db_escape($gl_code).", name=".db_escape($name).", is_percentage=".db_escape($is_percentage).", is_weight=".db_escape($is_weight).", 
	predefined_rate=".db_escape($predefined_rate).", is_negative=".db_escape($is_negative)." WHERE id = ".db_escape($selected_id);
   	db_query($sql,"The customer_charge could not be updated");
}

function delete_charge($selected_id)
{
	$sql="DELETE FROM ".TB_PREF."charges WHERE id=".db_escape($selected_id);
	db_query($sql,"could not delete customer_charge");
}

function get_charges($show_inactive)
{
	$sql = "SELECT * FROM ".TB_PREF."charges";
	if (!$show_inactive) $sql .= " WHERE !inactive";
	// $sql .= " ORDER BY customer_charge";
	return db_query($sql,"could not customer_charge");
}

function get_charge($selected_id)
{
	$sql = "SELECT * FROM ".TB_PREF."charges WHERE id=".db_escape($selected_id);

	$result = db_query($sql,"could not customer_charge");
	return db_fetch($result);
}

function get_charge_is_percentage($is_percentage, $is_weight='')
{
	$sql = "SELECT * FROM ".TB_PREF."charges WHERE is_percentage=".db_escape($is_percentage);
	if($is_weight != '')
		$sql .= " AND is_weight=".db_escape($is_weight);	
	// display_error($sql);
	$result = db_query($sql,"could not customer_charge");
	return db_fetch($result);
}

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

function charges_trnas($trans_no, $type)
{
	$sql = "SELECT * FROM " . TB_PREF . "charges_trans WHERE trans_no = " . db_escape($trans_no) . " AND type = " . db_escape($type);
	return db_query($sql,"could not customer_charge");
}

function charges_trans_edit($trans_no, $type, $charge_id)
{
	$sql = "SELECT * FROM " . TB_PREF . "charges_trans WHERE trans_no = " . db_escape($trans_no) . " AND type = " . db_escape($type). " AND charge_id = " . db_escape($charge_id);
	$result =  db_query($sql,"could not customer_charge");
	return db_fetch($result);
}

function delete_charges_trnas($trans_no, $type)
{
	$sql="DELETE FROM ".TB_PREF."charges_trans WHERE trans_no=".db_escape($trans_no). " AND type=".db_escape($type);
	db_query($sql,"could not delete charges");
}

// function getpurchase_charge_name($id)
// {
// 	$sql = "SELECT CONCAT(name,' - ',reference)  FROM ".TB_PREF."customer_charges WHERE id = ".db_escape($id);
// 	$result = db_query($sql, "could not purchase_charge");
// 	$row = db_fetch($result);
// 	return is_array($row) ? $row[0] : false;
// }

?>