diff options
-rw-r--r-- | LedgerSMB/Location.pm | 58 | ||||
-rw-r--r-- | sql/upgrade/1.2-1.3.sql | 56 |
2 files changed, 114 insertions, 0 deletions
diff --git a/LedgerSMB/Location.pm b/LedgerSMB/Location.pm new file mode 100644 index 00000000..c58dbab0 --- /dev/null +++ b/LedgerSMB/Location.pm @@ -0,0 +1,58 @@ +=head1 NAME + +LedgerSMB::Location - LedgerSMB class for managing Business Locations + +=head1 SYOPSIS + +This module creates object instances based on LedgerSMB's in-database ORM. + +=head1 METHODS + +The following method is static: +=item new ($LedgerSMB object); + +The following methods are passed through to stored procedures: +=item save +=item get +=item search +=item list_all +=item delete (via Autoload) + +The above list may grow over time, and may depend on other installed modules. + +=head1 Copyright (C) 2007, The LedgerSMB core team. +This file is licensed under the Gnu General Public License version 2, or at your +option any later version. A copy of the license should have been included with +your software. + +=back + +=cut + +package LedgerSMB::Location; +use LedgerSMB; +use LedgerSMB::DBObject; +@ISA = (LedgerSMB::DBObject); + +sub AUTOLOAD { + my $procname = "location_$LedgerSMB::Location::Autoload"; + $self->exec_method($procname); +} + +sub save { + $ref = shift @{$self->exec_method("location_save")}; + $self->merge($ref, 'id'); +} + +sub get { + $ref = shift @{$self->exec_method('location_get')}; + $self->merge($ref, keys $ref); +} + +sub search { + $self->{search_results} = $self->exec_method('location_search'); +} + +sub list_all { + $self->{search_results} = $self->exec_method('location_list_all'); +} diff --git a/sql/upgrade/1.2-1.3.sql b/sql/upgrade/1.2-1.3.sql index 563d8b55..77fd43b3 100644 --- a/sql/upgrade/1.2-1.3.sql +++ b/sql/upgrade/1.2-1.3.sql @@ -181,4 +181,60 @@ BEGIN END; $$ language plpgsql; +CREATE OR REPLACE FUNCTION location_get (in_id integer) returns locations AS +$$ +DECLARE + location locations%ROWTYPE; +BEGIN + SELECT * INTO location FROM locations WHERE id = in_id; + RETURN location; +END; +$$ language plpgsql; + +CREATE OR REPLACE FUNCTION location_search +(in_companyname varchar, in_address1 varchar, in_address2 varchar, + in_city varchar, in_state varchar, in_zipcode varchar, + in_country varchar) +RETURNS SETOF locations +AS +$$ +DECLARE + location locations%ROWTYPE; +BEGIN + FOR location IN + SELECT * FROM locations + WHERE companyname ilike '%' || in_companyname || '%' + AND address1 ilike '%' || in_address1 || '%' + AND address2 ilike '%' || in_address2 || '%' + AND in_city ilike '%' || in_city || '%' + AND in_state ilike '%' || in_state || '%' + AND in_zipcode ilike '%' || in_zipcode || '%' + AND in_country ilike '%' || in_country || '%' + LOOP + RETURN NEXT location; + END LOOP; +END; +$$ LANGUAGE PLPGSQL; + +CREATE OR REPLACE FUNCTION location_list_all () RETURNS SETOF locations AS +$$ +DECLARE + location locations%ROWTYPE; +BEGIN + FOR location IN + SELECT * FROM locations + ORDER BY company_name, city, state, country + LOOP + RETURN NEXT location; + END LOOP; +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION location_delete (in_id integer) RETURNS VOID AS +$$ +BEGIN + DELETE FROM locations WHERE id = in_id; +END; +$$ language plpgsql; + COMMIT; |