[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[4759] trunk



Revision: 4759
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=4759&view=rev
Author:   einhverfr
Date:     2012-05-23 09:23:25 +0000 (Wed, 23 May 2012)
Log Message:
-----------
New Entity location classes being used, address table ported to dynatable

Modified Paths:
--------------
    trunk/LedgerSMB/DBObject/Entity/Location.pm
    trunk/LedgerSMB/DBObject_Moose.pm
    trunk/LedgerSMB/ScriptLib/Company.pm
    trunk/LedgerSMB.pm
    trunk/UI/Contact/divs/address.html
    trunk/UI/lib/dynatable.html
    trunk/sql/modules/Company.sql
    trunk/sql/modules/Location.sql

Modified: trunk/LedgerSMB/DBObject/Entity/Location.pm
===================================================================
--- trunk/LedgerSMB/DBObject/Entity/Location.pm	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/LedgerSMB/DBObject/Entity/Location.pm	2012-05-23 09:23:25 UTC (rev 4759)
@@ -59,16 +59,16 @@
 
 has 'entity_id' => (is => 'ro', isa => 'Maybe[Int]');
 
-=item eca_id
+=item credit_id
 
 Internal id of lined entity credit account.  Is undef if linked to entity
 instead.
 
 =cut
 
-has 'eca_id' => (is => 'ro', isa => 'Maybe[Int]');
+has 'credit_id' => (is => 'ro', isa => 'Maybe[Int]');
 
-=item class_id 
+=item location_class
 
 Internal id of location class.
 
@@ -84,7 +84,7 @@
 
 =cut
 
-has 'class_id' => (is => 'ro', isa => 'Maybe[Int]');
+has 'location_class' => (is => 'ro', isa => 'Maybe[Int]');
 
 =item class_name
 
@@ -93,9 +93,9 @@
 
 =cut
 
-our %classes = ( 1 => $App_State::Locate->text('Billing'),
-                 2 => $App_State::Locate->text('Sales'),
-                 3 => $App_State::Locate->text('Shipping'),
+our %classes = ( 1 => $LedgerSMB::App_State::Locale->text('Billing'),
+                 2 => $LedgerSMB::App_State::Locale->text('Sales'),
+                 3 => $LedgerSMB::App_State::Locale->text('Shipping'),
 );
 
 has 'class_name' => (is => 'rw', isa => 'Maybe[Str]');
@@ -172,13 +172,82 @@
 
 =item get($args hashref)
 
+Retrieves locations and returns them.  Args include:
 
+=over 
+
+=item entity_id (required)
+
+=item credit_id (optional)
+
+=item only_class int (optional)
+
+=item
+
+This function returns all locations attached to the entity_id and, if the credit_id is provided, all locations attached to the credit_id as well.  The two 
+are appended together with the ones at the entity level coming first.
+
+If only_class is set, all results will be discarded that are not a specific 
+class (useful for retrieving billing info only).
+
+=cut
+
+sub get_active {
+    my ($self, $request, $args) = @_;
+    my @results;
+    for my $ref ($self->call_procedure(procname => 'entity__list_locations',
+                                           args => [$args->{entity_id}]))
+    {
+       next if ($args->{only_class}) 
+               and ($args->{only_class} != $ref->{location_class});
+        LedgerSMB::DBObject_Moose::prepare_dbhash($request, $ref);
+        push @results, $self->new(%$ref);
+    }
+    return @results unless $args->{credit_id};
+
+    for my $ref ($self->call_procedure(procname => 'eca__list_locations',
+                                           args => [$args->{credit_id}]))
+    {
+       next if ($args->{only_class}) 
+               and ($args->{only_class} != $ref->{location_class});
+        $ref->{credit_id} = $args->{credit_id};
+        LedgerSMB::DBObject_Moose::prepare_dbhash($request, $ref);
+        push @results, $self->new(%$ref);
+    }
+
+    return @results;
+
+}
+
 =item save()
 
+Saves the current location to the database
+
+=cut
+
+sub save {
+    my ($self) = @_;
+    my $procname;
+
+    if ($self->credit_id){
+        $procname = 'eca__location_save';
+    } else {
+        $procname = 'entity__location_save';
+    }
+    $self->exec_method({funcname => $procname});
+}
+
 =item deactivate()
 
-=item list($args hashref)
+Deactivates the current location
 
+=cut
+
+sub deactivate {
+    my ($self) = @_;
+    $self->exec_method({funcname => 'location__deactivate'});
+}
+
 =back
 
 =head1 COPYRIGHT

Modified: trunk/LedgerSMB/DBObject_Moose.pm
===================================================================
--- trunk/LedgerSMB/DBObject_Moose.pm	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/LedgerSMB/DBObject_Moose.pm	2012-05-23 09:23:25 UTC (rev 4759)
@@ -44,6 +44,7 @@
 
 package LedgerSMB::DBObject_Moose;
 use LedgerSMB::DBObject;
+use LedgerSMB;
 use Moose;
 use Scalar::Util;
 use Log::Log4perl;
@@ -103,7 +104,7 @@
 
 sub call_procedure {
     my $self   = shift @_; 
-    return LedgerSMB::DBObject::call_procedure(@_);
+    return LedgerSMB->call_procedure(@_);
 }
 
 # Keeping this here due to common requirements

Modified: trunk/LedgerSMB/ScriptLib/Company.pm
===================================================================
--- trunk/LedgerSMB/ScriptLib/Company.pm	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/LedgerSMB/ScriptLib/Company.pm	2012-05-23 09:23:25 UTC (rev 4759)
@@ -3,6 +3,7 @@
 use LedgerSMB::DBObject::Customer;
 use LedgerSMB::DBObject::Entity::Company;
 use LedgerSMB::DBObject::Entity::Credit_Account;
+use LedgerSMB::DBObject::Entity::Location;
 use LedgerSMB::DBObject::Vendor;
 use Log::Log4perl;
 
@@ -244,15 +245,13 @@
 
 sub add_location {
     my ($request) = @_;
-    my $company = new_company($request);
-    set_entity_class($company);
-    $company->save_location();
-    $company->get();
+    my $location = LedgerSMB::DBObject::Entity::Location->new(%$request);
+    my $company = LedgerSMB::DBObject::Entity::Company->new(%$request);
 
-    
-    $company->get_metadata();
+    $location->save();
+    $company = $company->get($request->{entity_id});
 
-    _render_main_screen($company);
+    _render_main_screen($request, $company);
 	
 }
 
@@ -759,6 +758,17 @@
         $company = new_company($request);
         $company->get_metadata();
     }
+    for my $ref (@{$request->{credit_list}}){
+        $company->{credit_act} = $ref; 
+    }
+    my $err =  $company->{credit_act}->{id};
+    @{$company->{locations}} = 
+          LedgerSMB::DBObject::Entity::Location->get_active(
+                       $request,
+                       {entity_id => $company->{entity_id},
+                        credit_id => $company->{credit_act}->{id}}
+          );
+                         
 
     $company->{creditlimit} = $request->format_amount({amount => $company->{creditlimit}}) unless !defined $company->{creditlimit}; 
     $company->{discount} = "$company->{discount}" unless !defined $company->{discount}; 

Modified: trunk/LedgerSMB.pm
===================================================================
--- trunk/LedgerSMB.pm	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/LedgerSMB.pm	2012-05-23 09:23:25 UTC (rev 4759)
@@ -951,30 +951,30 @@
 sub dberror{
    my $self = shift @_;
    my $state_error = {};
-   if ($self->{_locale}){
-       $state_error = {
-            '42883' => $self->{_locale}->text('Internal Database Error'),
-            '42501' => $self->{_locale}->text('Access Denied'),
-            '42401' => $self->{_locale}->text('Access Denied'),
-            '22008' => $self->{_locale}->text('Invalid date/time entered'),
-            '22012' => $self->{_locale}->text('Division by 0 error'),
-            '22004' => $self->{_locale}->text('Required input not provided'),
-            '23502' => $self->{_locale}->text('Required input not provided'),
-            '23505' => $self->{_locale}->text('Conflict with Existing Data.  Perhaps you already entered this?'),
-            'P0001' => $self->{_locale}->text('Error from Function:') . "\n" .
-                    $self->{dbh}->errstr,
-       };
-   }
-   $logger->error("Logging SQL State ".$self->{dbh}->state.", error ".
-           $self->{dbh}->err . ", string " .$self->{dbh}->errstr);
-   if (defined $state_error->{$self->{dbh}->state}){
-       $self->error($state_error->{$self->{dbh}->state}
+   my $locale = $LedgerSMB::App_State::Locale;
+   my $dbh = $LedgerSMB::App_State::DBH;
+   $state_error = {
+            '42883' => $locale->text('Internal Database Error'),
+            '42501' => $locale->text('Access Denied'),
+            '42401' => $locale->text('Access Denied'),
+            '22008' => $locale->text('Invalid date/time entered'),
+            '22012' => $locale->text('Division by 0 error'),
+            '22004' => $locale->text('Required input not provided'),
+            '23502' => $locale->text('Required input not provided'),
+            '23505' => $locale->text('Conflict with Existing Data.  Perhaps you already entered this?'),
+            'P0001' => $locale->text('Error from Function:') . "\n" .
+                    $dbh->errstr,
+   };
+   $logger->error("Logging SQL State ".$dbh->state.", error ".
+           $dbh->err . ", string " .$dbh->errstr);
+   if (defined $state_error->{$dbh->state}){
+       die $state_error->{$dbh->state}
            . "\n" . 
-          $self->{_locale}->text('More information has been reported in the error logs'));
-       $self->{dbh}->rollback;
+          $locale->text('More information has been reported in the error logs');
+       $dbh->rollback;
        exit;
    }
-   $self->error($self->{dbh}->state . ":" . $self->{dbh}->errstr);
+   die $dbh->state . ":" . $dbh->errstr;
 }
 
 sub redo_rows {

Modified: trunk/UI/Contact/divs/address.html
===================================================================
--- trunk/UI/Contact/divs/address.html	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/UI/Contact/divs/address.html	2012-05-23 09:23:25 UTC (rev 4759)
@@ -26,56 +26,53 @@
 	<?lsmb PROCESS input element_data = {
 		type="hidden" 
 		name="credit_id" 
-		value=credit_id
+		value=credit_act.id
 	} ?>
 	<?lsmb PROCESS input element_data = {
 		type="hidden" 
 		name="location_id" 
 		value=location_id
-	} ?>
-    <table width="100%">
-    <tr class="listheading">
-        <th class="type"><?lsmb text('Type') ?></th>
-        <th class="line_one"><?lsmb text('Address1') ?></th>
-        <th class="city"><?lsmb text('City') ?></th>
-        <th class="state"><?lsmb text('State/Province') ?></th>
-        <th class="mail_code"><?lsmb text('ZIP/Post Code') ?></th>
-        <th class="country"><?lsmb text('Country') ?></th>
-	<th class="actions"><?lsmb text('Actions') ?></th>
-    </tr>
-	<?lsmb FOREACH loc = locations ?>
-    <?lsmb IF location_id == loc.id;
-                 FOREACH key = loc.keys();
-                      $key = loc.$key;
-                 END;
-           END;
-    ?>
-    <tr <?lsmb IF location_id == loc.id ?> class="active" <?lsmb END ?>>
-	<td class="type"><?lsmb loc.class ?></td>
-	<td class="line_one"><?lsmb loc.line_one ?></td>
-	<td class="city"><?lsmb loc.city ?></td>
-	<td class="state"><?lsmb loc.state ?></td>
-	<td class="mail_code"><?lsmb loc.mail_code ?></td>
-	<td class="country"><?lsmb loc.country ?></td>
-	<td class="actions">
-		<!--  TODO:  Automate links with AJAX -->
-		<a href="<?lsmb script ?>?action=edit&entity_id=<?lsmb entity_id 
-			?>&location_id=<?lsmb loc.id ?>&credit_id=<?lsmb 
-			credit_id ?>&country_code=<?lsmb loc.country_id
-                        ?>&target_div=location_div&location_class=<?lsmb
-                        loc.class_id ?>&person_id=<?lsmb tt_url(person_id) 
-                        ?>">[edit]</a> 
-		<a href="<?lsmb script ?>?action=delete_location&entity_id=<?lsmb
-			entity_id ?>&location_id=<?lsmb loc.id 
-			?>&credit_id=<?lsmb credit_id 
-                        ?>&form_id=<?lsmb form_id
-                        ?>&location_class=<?lsmb loc.class_id 
-                        ?>&target_div=location_div&person_id=<?lsmb person_id
-                        ?>">[delete]</a>
-	</td>
-    </tr>
-	<?lsmb END ?>
-    </table>
+	}; ?><?lsmb
+    FOREACH LOC IN locations;
+        IF LOC.credit_id;
+           LOC.is_for_credit = 'X';
+        END;
+        IF LOC.location_class == 1;
+           LOC.type = text('Billing');
+        ELSIF LOC.location_class == 2;
+           LOC.type = text('Sales');
+        ELSIF LOC.location_class == 3;
+           LOC.type = text('Shipping');
+        END; 
+        LOC.edit = '[' _ text('Edit') _ ']';
+        LOC.delete= '[' _ text('Delete') _ ']';
+        LOC.edit_href_suffix= script _ '?action=edit&entity_id=' _ entity_id _
+    			'&location_id=' _ loc.id _ '&credit_id=' _
+    			credit_act.id _ '&country_code=' _ loc.country_id _
+                       '&target_div=location_div&location_class=' _
+                        loc.location_class;
+        LOC.delete_href_suffix= script _ '?action=delete&entity_id=' _ entity_id _
+    			'&location_id=' _ loc.id _ '&credit_id=' _
+    			credit_act.id _ '&country_code=' _ loc.country_id _
+                       '&target_div=location_div&location_class=' _
+                        loc.location_class;
+    END; ?><?lsmb
+    PROCESS dynatable 
+               tbody = {rows = locations}
+          attributes = {id = 'location_table'
+                       width = '100%'}
+             columns = [
+       {col_id = 'is_for_credit', type = 'text', name = text('This Account') } #'
+       {col_id = 'type', type = 'text', name=text('Type') }
+       {col_id = 'line_one', type='text', name=text('Address1') }
+       {col_id = 'city', type='text', name=text('City') }
+       {col_id = 'state', type='text', name=text('State/Province') }
+       {col_id = 'mail_code', type='text', name=text('Zip/Post Code') } #'
+       {col_id = 'country', type='text', name=text('Country') }
+       {col_id = 'edit', type='href', name=' ', href_base='' }
+       {col_id = 'delete', type='href', name=' ', href_base='' } 
+    ];
+       ?> 
 	<div> 
                 <?lsmb INCLUDE select element_data = {
                        name           = "location_class"
@@ -144,13 +141,13 @@
 		} #' ?>
 	</div>
 	<div> 
-		<?lsmb IF !country_code; country_code = default_country; END -?>
+		<?lsmb IF !country_id; country_id = default_country; END -?>
 		<?lsmb INCLUDE select element_data = {
 			text_attr = "name"
 			value_attr = "id"
-			default_values = [country_code]
+			default_values = [country_id]
 			options = country_list
-			name = "country_code"
+			name = "country_id"
 			label = text('Country') 
 		} ?>
 	</div>

Modified: trunk/UI/lib/dynatable.html
===================================================================
--- trunk/UI/lib/dynatable.html	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/UI/lib/dynatable.html	2012-05-23 09:23:25 UTC (rev 4759)
@@ -1,5 +1,6 @@
 <?lsmb BLOCK dynatable ?>
-<table id="<?lsmb attributes.id ?>" class="dynatable <?lsmb attributes.class ?>">
+<table id="<?lsmb attributes.id ?>" class="dynatable <?lsmb attributes.class ?>"
+width="attributes.width">
 <?lsmb- IF !hide_header -?>
 <thead>
    <tr>

Modified: trunk/sql/modules/Company.sql
===================================================================
--- trunk/sql/modules/Company.sql	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/sql/modules/Company.sql	2012-05-23 09:23:25 UTC (rev 4759)
@@ -530,7 +530,7 @@
 BEGIN
 	
 	FOR out_row IN 
-		SELECT  c.id, e.id, ec.entity_class, ec.discount, 
+		SELECT  ec.id, e.id, ec.entity_class, ec.discount, 
                         ec.discount_terms,
 			ec.taxincluded, ec.creditlimit, ec.terms, 
 			ec.meta_number, ec.description, ec.business_id, 
@@ -540,9 +540,8 @@
                         ec.discount_account_id,
 			ec.threshold, e.control_code, ec.id, ec.pay_to_name,
                         ec.taxform_id
-		FROM company c
-		JOIN entity e ON (c.entity_id = e.id)
-		JOIN entity_credit_account ec ON (c.entity_id = ec.entity_id)
+		FROM entity e 
+		JOIN entity_credit_account ec ON (e.id = ec.entity_id)
 		WHERE e.id = in_entity_id
 			AND ec.entity_class = 
 				CASE WHEN in_entity_class = 3 THEN 2
@@ -887,7 +886,7 @@
 ) IS
 $$ Saves an entity credit account.  Returns the id of the record saved.  $$;
 
-CREATE OR REPLACE FUNCTION company__list_locations(in_entity_id int)
+CREATE OR REPLACE FUNCTION entity__list_locations(in_entity_id int)
 RETURNS SETOF location_result AS
 $$
 DECLARE out_row RECORD;
@@ -907,7 +906,7 @@
 END;
 $$ LANGUAGE PLPGSQL;
 
-COMMENT ON FUNCTION company__list_locations(in_entity_id int) IS
+COMMENT ON FUNCTION entity__list_locations(in_entity_id int) IS
 $$ Lists all locations for an entity.$$;
 
 DROP TYPE IF EXISTS contact_list CASCADE;
@@ -1131,25 +1130,25 @@
     
 $$ language 'sql';
 
-CREATE OR REPLACE FUNCTION company__location_save (
+CREATE OR REPLACE FUNCTION entity__location_save (
     in_entity_id int, in_location_id int,
     in_location_class int, in_line_one text, in_line_two text, 
-    in_city TEXT, in_state TEXT, in_mail_code text, in_country_code int,
+    in_city TEXT, in_state TEXT, in_mail_code text, in_country_id int,
     in_created date
 ) returns int AS $$
     BEGIN
     return _entity_location_save(
         in_entity_id, in_location_id,
         in_location_class, in_line_one, in_line_two, 
-        '', in_city , in_state, in_mail_code, in_country_code);
+        '', in_city , in_state, in_mail_code, in_country_id);
     END;
 
 $$ language 'plpgsql';
 
-COMMENT ON FUNCTION company__location_save (
+COMMENT ON FUNCTION entity__location_save (
     in_entity_id int, in_location_id int,
     in_location_class int, in_line_one text, in_line_two text,
-    in_city TEXT, in_state TEXT, in_mail_code text, in_country_code int,
+    in_city TEXT, in_state TEXT, in_mail_code text, in_country_id int,
     in_created date
 ) IS
 $$ Saves a location to a company.  Returns the location id.$$;
@@ -1158,7 +1157,7 @@
     in_entity_id int, in_location_id int,
     in_location_class int, in_line_one text, in_line_two text, 
     in_line_three text, in_city TEXT, in_state TEXT, in_mail_code text, 
-    in_country_code int
+    in_country_id int
 ) returns int AS $$
 
     DECLARE
@@ -1170,16 +1169,16 @@
 	FROM company WHERE entity_id = in_entity_id;
 
 	DELETE FROM entity_to_location
-	WHERE company_id = in_entity_id
+	WHERE entity_id = in_entity_id
 		AND location_class = in_location_class
 		AND location_id = in_location_id;
 
 	SELECT location_save(NULL, in_line_one, in_line_two, in_line_three, in_city,
-		in_state, in_mail_code, in_country_code) 
+		in_state, in_mail_code, in_country_id) 
 	INTO l_id;
 
 	INSERT INTO entity_to_location
-		(company_id, location_class, location_id)
+		(entity_id, location_class, location_id)
 	VALUES  (in_entity_id, in_location_class, l_id);
 
 	RETURN l_id;    
@@ -1201,7 +1200,7 @@
     in_credit_id int, in_location_id int,
     in_location_class int, in_line_one text, in_line_two text, 
     in_line_three text, in_city TEXT, in_state TEXT, in_mail_code text, 
-    in_country_code int, in_old_location_class int
+    in_country_id int, in_old_location_class int
 ) returns int AS $$
 
     DECLARE
@@ -1225,7 +1224,7 @@
                 in_city,
                 in_state, 
                 in_mail_code, 
-                in_country_code
+                in_country_id
             )
         	INTO l_id; 
         ELSE
@@ -1237,7 +1236,7 @@
                 in_city,
                 in_state, 
                 in_mail_code, 
-                in_country_code
+                in_country_id
             )
         	INTO l_id; 
             INSERT INTO eca_to_location 

Modified: trunk/sql/modules/Location.sql
===================================================================
--- trunk/sql/modules/Location.sql	2012-05-23 06:00:43 UTC (rev 4758)
+++ trunk/sql/modules/Location.sql	2012-05-23 09:23:25 UTC (rev 4759)
@@ -1,5 +1,7 @@
 -- VERSION 1.3.0
 
+BEGIN;
+
 CREATE OR REPLACE FUNCTION location_list_class()
 RETURNS SETOF location_class AS
 $$
@@ -180,6 +182,8 @@
 COMMENT ON FUNCTION location_delete (in_id integer)
 IS $$ DELETES the location specified by in_id.  Does not return a value.$$;
 
+DROP TYPE IF EXISTS location_result CASCADE;
+
 CREATE TYPE location_result AS (
         id int,
         line_one text,
@@ -190,7 +194,19 @@
         mail_code text,
         country_id int,
         country text,
-        class_id int,
+        location_class int,
         class text
 );
 
+CREATE OR REPLACE FUNCTION location__deactivate(in_id int)
+RETURNS location AS
+$$
+
+UPDATE location set active = false, inactive_date = now()
+ WHERE id = $1;
+
+SELECT * FROM location WHERE id = 1;
+
+$$ language sql;
+
+COMMIT;

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.