#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::Region;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use Common::Assert;
use base 'Common::DB::Item';

use constant kDB => Common::DB::Item::kClientDB();
use constant kTable => 'region';

use constant kGlobal => 0;


sub Search
{
    my ($class, $searchTerm) = @_;

    assert($searchTerm);

	my $sql = "SELECT * FROM ".kTable
		. " WHERE (" . $class->SearchSyntax(field => "name", value => $searchTerm) . ")"
		. " ORDER BY name";

    return $class->SUPER::GetAll($sql);
}

sub SearchEditable
{
    my ($class, $searchTerm) = @_;

    assert($searchTerm);

	my $sql = "SELECT * FROM ".kTable
		. " WHERE (" . $class->SearchSyntax(field => "name", value => $searchTerm) . ")"
        . " AND region_id > 0"
		. " ORDER BY name";

    return $class->SUPER::GetAll($sql);
}


sub GetAll
{
    my ($class) = @_;

	my $sql = "SELECT * FROM region ORDER BY name";
    return $class->SUPER::GetAll($sql);
}



sub GetAllEditable
{
    my ($class) = @_;
	my $dbo = Common::RSApp::GetClientDB();

	my $sql = "SELECT * FROM region WHERE region_id > 0 ORDER BY name";
    return $class->SUPER::GetAll($sql);
}


sub SetAsDefaultMechanicalLicenseRegion
{
    my ($class, $regionID) = @_;

    # First, set all the other regions flag value to '0'.
    # Then set the flag to true for the region we were told to.
    #
    my $dbo = Common::RSApp::GetClientDB();
    $dbo->DoCmd('UPDATE '. kTable . ' SET is_default_mechanical_license_region=0');

    $dbo->DoCmd('UPDATE '. kTable . " SET is_default_mechanical_license_region=1 WHERE region_id=$regionID");
}



1;

