<?php


function check_alternative_units($stock_id, $include_base_unit = false){

    $result = get_alternate_units($stock_id, $include_base_unit);
    
    $myrow = db_fetch_row($result);
	return ($myrow[0] > 0);
    
}



function validate_db_has_currency_rates($currency, $date_)

{

	$date = date2sql($date_);

	

	if (is_company_currency($currency)){

		return array("success" => true, "message" => "");

    }

    

	$ret = check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."exchange_rates WHERE curr_code = '$currency' && date_ <= '$date'");

	if ($ret == 0){

		$message = sprintf(_("Cannot retrieve exchange rate for currency %s as of %s. Please add exchange rate manually on Exchange Rates page."), $currency, $date_);

        return array("success" => false, "message" => $message);

    }

	

    return array("success" => true, "message" => "");				 

}



function db_has_movement_types()

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."movement_types");

}



function check_db_has_movement_types($msg)

{

	global $path_to_root;

    if (!db_has_movement_types()) 

    {

    	display_error($msg, true);

    	end_page();

    	exit;	

    }	

}



function check_num2($value, $min=null, $max=null, $dflt=0) {

	if($value === null){

        return 0;

    }

    if($value === ""){

        return 0;

    }

    $num = input_num2($value, $dflt);

    if ($num === false || $num === null) 

	  return 0;

    if (isset($min) && ($num<$min)) 

	  return 0;

    if (isset($max) && ($num>$max)) 

	  return 0;

    return 1;

}

function db_has_loyalty_members()

{

	return check_empty_result("SELECT COUNT(member_no) FROM ".TB_PREF."member_master");

}




function check_db_has_loyalty_cards($msg)

{

	global $path_to_root;

    if (!db_has_loyalty_cards()) 

    {

    	display_error($msg, true);

    	end_page();

    	exit;	

    }	

}



function db_has_loyalty_cards()

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."loyalty_cards_master");

}




function db_has_projects()

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."projects_master LIMIT 1");

}



function check_db_has_projects($msg)

{

	global $path_to_root;

    if (!db_has_projects()) 

    {

    	display_error($msg, true);

    	end_page();

    	exit;	

    }	

}



function db_has_approval_groups()

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."approval_group LIMIT 1");

}



function db_has_project_tasks()

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."project_tasks LIMIT 1");

}



function check_db_has_project_tasks($msg)

{

	global $path_to_root;

    if (!db_has_project_tasks()) 

    {

    	display_error($msg, true);

    	end_page();

    	exit;	

    }

}





function db_has_approval_paths()

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."approval_path LIMIT 1");

}



function check_po_has_advance_payments($po_no)

{

	return check_empty_result("SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE po_no = ". db_escape($po_no));

}