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

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



Revision: 2342
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=2342&view=rev
Author:   aurynn_cmd
Date:     2008-09-25 23:08:34 +0000 (Thu, 25 Sep 2008)

Log Message:
-----------
Saving locations and contacts to a user now works as expected.

Modified Paths:
--------------
    trunk/LedgerSMB/DBObject/Admin.pm
    trunk/LedgerSMB/DBObject/Location.pm
    trunk/LedgerSMB/DBObject/User.pm
    trunk/UI/Admin/edit_user.html
    trunk/scripts/admin.pl
    trunk/sql/Pg-database.sql
    trunk/sql/modules/Location.sql
    trunk/sql/modules/Person.sql

Modified: trunk/LedgerSMB/DBObject/Admin.pm
===================================================================
--- trunk/LedgerSMB/DBObject/Admin.pm	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/LedgerSMB/DBObject/Admin.pm	2008-09-25 23:08:34 UTC (rev 2342)
@@ -267,7 +267,7 @@
 sub get_contact_classes {
     
     my $self = shift @_;
-    my $sth = $self->{dbh}->prepare("select id, class as classname from contact_class");
+    my $sth = $self->{dbh}->prepare("select id, class as name from contact_class");
     my $code = $sth->execute();
     return $sth->fetchall_arrayref({});
 }

Modified: trunk/LedgerSMB/DBObject/Location.pm
===================================================================
--- trunk/LedgerSMB/DBObject/Location.pm	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/LedgerSMB/DBObject/Location.pm	2008-09-25 23:08:34 UTC (rev 2342)
@@ -17,6 +17,7 @@
     my ($ret) = $self->exec_method(funcname=>$type."__save_location", args=>[
         $self->{user_id}, # entity_id           
         $self->{location_id}, # location_id
+        3, # location_class, currently being set to "shipping"
         $self->{address1},
         $self->{address2},
         $self->{address3}, # address info
@@ -25,7 +26,8 @@
         $self->{zipcode},
         $self->{country} # obviously, country.
     ]);
-    $self->{id} = $ret->[0];
+    $self->{id} = $ret->{$type."__save_location"};
+    $self->{dbh}->commit();
     return $self->{id};
 }
 
@@ -59,7 +61,7 @@
     
     my ($ret) = $self->exec_method(funcname=>"location__get", args=>[$id]);
     
-    return $ret->[0];
+    return $ret->{location__get};
 }
 
 sub get_all {

Modified: trunk/LedgerSMB/DBObject/User.pm
===================================================================
--- trunk/LedgerSMB/DBObject/User.pm	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/LedgerSMB/DBObject/User.pm	2008-09-25 23:08:34 UTC (rev 2342)
@@ -90,6 +90,7 @@
     }
     $self->{roles} = ..hidden..;
     
+    $self->{entity_id} = $self->{entity}->{id};
     print STDERR "Got all user information";
     
     #$user->{user} = $u->get($id);
@@ -145,16 +146,56 @@
     
     my $self = shift @_;
     my $id = shift @_;
+    my $class = shift @_;
     my $contact = shift @_;
+    my @ret;
     
-    my @ret = $self->exec_method(funcname=>"person__save_contact", 
-        args=>[
-            $self->{entity_id},
-            $self->{contacts}->[$id]->{contact_class},
-            $self->{contacts}->[$id]->{contact},
-            $contact
-        ]
-    );
+    print STDERR Dumper($self->{entity}->{id});
+    if ($id) {
+        print STDERR "Found ID..";
+        @ret = $self->exec_method(funcname=>"person__save_contact", 
+            args=>[
+                $self->{entity}->{id},
+                $self->{contacts}->[$id]->{contact_class},
+                $self->{contacts}->[$id]->{contact},
+                $contact
+            ]
+        );
+    } 
+    else{
+        print STDERR "Did not find an ID, attempting to save a new contact..\n";
+        print STDERR ($class."\n");
+        print STDERR ($contact."\n");
+        print STDERR ($self->{entity_id}."\n");
+        @ret = $self->exec_method(funcname=>"person__save_contact",
+            args=>[
+                $self->{entity_id},
+                $class,
+                undef,
+                $contact
+            ]
+        );
+    }
+    print STDERR Dumper(..hidden..);
+    if ($ret[0]->{person__save_contact} == 1){
+        $self->{dbh}->commit();
+    }
+    else{
+        $self->error("Couldn't save contact...");
+    }
+    return 1;
 }
 
+sub delete_contact {
+    
+    my $self = shift @_;
+    my $id = shift @_;
+    
+    # Okay
+    # ID doesn't actually conform to any database entry
+    # We're basically cheating outrageously here.
+    
+    
+}
+
 1;

Modified: trunk/UI/Admin/edit_user.html
===================================================================
--- trunk/UI/Admin/edit_user.html	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/UI/Admin/edit_user.html	2008-09-25 23:08:34 UTC (rev 2342)
@@ -62,7 +62,7 @@
             <input type="hidden" name="location_id" value="<?lsmb location.id?>"/>
             <?lsmb END?>
             <input type="hidden" name="action" value="save_location"/>
-            <input type="hidden" name="user_id" value="<?lsmb user.user.entity_id?>"/>
+            <input type="hidden" name="user_id" value="<?lsmb user.user.id?>"/>
             <table>
 
                 <tr>
@@ -90,8 +90,7 @@
                         Country
                     </td>
                 </tr>
-            
-            
+                
                 <?lsmb FOR location IN user.locations ?>
                 
                     <tr>
@@ -200,7 +199,7 @@
         				options = contact_classes
         				default_values = [contact.contact_class_id]
         				value_attr = "id"
-        				text_attr = "classname"
+        				text_attr = "name"
         			} ?>
                 </td>
                 <td>
@@ -219,33 +218,35 @@
             </tr>
             </form>
         </table>
+        <form name="groups" method="POST" action="admin.pl">
+            <input type="hidden" name="action" value="save_groups"/>
+            <table>
+                <!-- Groups section -->
+                <tr>
+                <?lsmb FOREACH role IN roles ?>
+                <?lsmb IF loop.count % 2 == 0?>
+                </tr>
+                <tr>
+                <?lsmb END?>
+                    <td>
+                        <input type="checkbox" name="<?lsmb loop.index?>" value="1" 
+                            <?lsmb FOREACH rolname IN user.roles ?>
+                                <?lsmb IF role == rolname?>
+                                    checked
+                                <?lsmb END?>
+                            <?lsmb END?> />
+                        <?lsmb role?>
+                    </td>
+                <?lsmb END?>
+                </tr>
+            </table>
         
-        <table>
-            <!-- Groups section -->
-            <tr>
-            <?lsmb FOREACH role IN roles ?>
-            <?lsmb IF loop.count % 2 == 0?>
-            </tr>
-            <tr>
-            <?lsmb END?>
-                <td>
-                    <input type="checkbox" name="<?lsmb loop.index?>" value="1" 
-                        <?lsmb FOREACH rolname IN user.roles ?>
-                            <?lsmb IF role == rolname?>
-                                checked
-                            <?lsmb END?>
-                        <?lsmb END?> />
-                    <?lsmb role?>
-                </td>
-            <?lsmb END?>
-            </tr>
-        </table>
-        
-        <table>
-            <tr>
-                <td><button value="new_user">Submit</button></td>
-                <td><button name="method" value="cancel">Cancel</td>
-            </tr>
-        </table>
-    <?lsmb END?>
+            <table>
+                <tr>
+                    <td><button value="groups">Save Groups</button></td>
+                    <td><button name="method" value="cancel">Cancel</td>
+                </tr>
+            </table>
+        </form>   
+<?lsmb END?>
 </div>

Modified: trunk/scripts/admin.pl
===================================================================
--- trunk/scripts/admin.pl	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/scripts/admin.pl	2008-09-25 23:08:34 UTC (rev 2342)
@@ -9,6 +9,56 @@
 use LedgerSMB::DBObject::Location;
 use Data::Dumper;
 
+sub __edit_page {
+    
+    
+    my ($request, $otd) = @_;
+    
+    # otd stands for Other Template Data.
+    
+    my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
+    my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'list', merge=>['user_id','company']);
+    $user_obj->{company} = $request->{company};
+    $user_obj->get($request->{user_id});
+
+    my @all_roles = $admin->get_roles();
+    
+    my $template = LedgerSMB::Template->new( 
+        user => $user, 
+        template => 'Admin/edit_user', 
+        language => $user->{language}, 
+        format => 'HTML', 
+        path=>'UI'
+    );
+    my $location = LedgerSMB::DBObject::Location->new(base=>$request);
+    my $template_data = 
+            {
+                user=>$user_obj, 
+                roles=>@all_roles,
+                countries=>$admin->get_countries(),
+                user_roles=>$user_obj->{roles},
+                salutations=>$admin->get_salutations(),
+                contact_classes=>$admin->get_contact_classes(),
+                locations=>$location->get_all($user_obj->{entity_id},"person"),
+            };
+    open (FOO,">/tmp/dump.txt");
+    print STDERR Dumper($template_data->{contact_classes});
+    print FOO Dumper($template_data);
+    
+    for my $key (keys(%{$otd})) {
+        
+        $template_data->{$key} = $otd->{$key};
+    }
+    my $template = LedgerSMB::Template->new( 
+        user => $user, 
+        template => 'Admin/edit_user', 
+        language => $user->{language}, 
+        format => 'HTML', 
+        path=>'UI'
+    );
+    $template->render($template_data);
+}
+
 sub new_user {
     
     # uses the same page as create_user, only pre-populated.
@@ -62,7 +112,7 @@
     # uses the same page as create_user, only pre-populated.
     my ($request) = @_;
     my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
-    my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id');
+    my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'list', merge=>['user_id','company']);
     $user_obj->{company} = $request->{company};
     $user_obj->get($request->{user_id});
 
@@ -86,13 +136,14 @@
                 contact_classes=>$admin->get_contact_classes(),
                 locations=>$location->get_all($user_obj->{entity_id},"person"),
             };
+    open (FOO,">/tmp/dump.txt");
     print STDERR Dumper($template_data->{contact_classes});
-    print STDERR Dumper($template_data->{user_roles});
+    print FOO Dumper($template_data);
     if ($request->type() eq 'POST') {
         
         $admin->save_user();
         $admin->save_roles();
-        $template->render($test_data);
+        $template->render($template_data);
     }
     else {
 #        print STDERR Dumper($user);
@@ -266,32 +317,9 @@
         # ->{contacts} is an arrayref to the list of contacts this user has
         # $request->{contact_id} is a reference to this structure.
         
-        $user_obj->save_contact($c_id);
+        $user_obj->save_contact($c_id, $request->{contact_class}, $request->{contact});
         
-        my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
-        
-        $user_obj->get($request->{user_id});
-
-        my @all_roles = $admin->get_roles();
-
-        my $template = LedgerSMB::Template->new( 
-            user => $user, 
-            template => 'Admin/edit_user', 
-            language => $user->{language}, 
-            format => 'HTML', 
-            path=>'UI'
-        );
-        my $template_data = 
-                {
-                    user=>$user_obj, 
-                    roles=>@all_roles,
-                    countries=>$admin->get_countries(),
-                    user_roles=>$user_obj->{roles},
-                    salutations=>$admin->get_salutations(),
-                    contact_classes=>$admin->get_contact_classes(),
-                    locations=>$location->get_all($user_obj->{entity_id},"person"),
-                };
-        $template->render($template_data);
+        __edit_page($request,{});
     }
 }
 
@@ -316,7 +344,8 @@
         $user->delete_contact($c_id);
         # Boom. Done.
         # Now, just call the main edit user page.
-        edit_user($request);
+        
+        __edit_page($request,undef,);
     }
 }
 
@@ -328,9 +357,9 @@
     if ($request->type eq "POST") {
         
         my $u_id = $request->{user_id}; # this is an entity_id
-        
+        my $user_obj = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id');
         my $location = LedgerSMB::DBObject::Location->new(base=>$request, copy=>'all');
-        
+        $user_obj->get($request->{user_id});
         # So there's a pile of stuff we need.
         # lineone
         # linetwo
@@ -339,14 +368,16 @@
         # state
         # zipcode
         # country
+        # u_id isn't an entity_it, though.
         print STDERR "Attempting to save location...\n";
+        $location->{user_id} = $user_obj->{user}->{entity_id};
+        print STDERR $location->{user_id}."\n";
         my $id = $location->save("person");
         # Done and done.
         
         my $admin = LedgerSMB::DBObject::Admin->new(base=>$request, copy=>'user_id');
-        my $user = LedgerSMB::DBObject::User->new(base=>$request, copy=>'user_id');
         
-        $user->get($request->{user_id});
+        
 
         my @all_roles = $admin->get_roles();
 
@@ -359,9 +390,9 @@
         );
         $template->render(
             {
-                user=>$user, 
+                user=>$user_obj, 
                 roles=>@all_roles,
-                user_roles=>$user->{roles},
+                user_roles=>$user_obj->{roles},
                 salutations=>$admin->get_salutations(),
                 locations=>$location->get_all($u_id,"person"),
                 location=>$location->get($id),

Modified: trunk/sql/Pg-database.sql
===================================================================
--- trunk/sql/Pg-database.sql	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/sql/Pg-database.sql	2008-09-25 23:08:34 UTC (rev 2342)
@@ -156,7 +156,7 @@
   state text check(state ~ '[[:alnum:]_]'),
   country_id integer not null REFERENCES country(id),
   mail_code text not null check (mail_code ~ '[[:alnum:]_]'),
-  created date not null,
+  created date not null default now(),
   inactive_date timestamp default null,
   active boolean not null default TRUE
 );

Modified: trunk/sql/modules/Location.sql
===================================================================
--- trunk/sql/modules/Location.sql	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/sql/modules/Location.sql	2008-09-25 23:08:34 UTC (rev 2342)
@@ -46,8 +46,8 @@
 	        line_three,
 	        city,
 	        state,
-	        zipcode,
-	        country)
+	        mail_code,
+	        country_id)
 	    VALUES (
 	        location_id,
 	        in_address1,
@@ -64,7 +64,7 @@
 	    SELECT * INTO location_row WHERE id = in_location_id;
 	    IF NOT FOUND THEN
 	        -- Tricky users are lying to us.
-	        RAISE EXCEPTION "location_save called with nonexistant location ID %", in_location_id;
+	        RAISE EXCEPTION 'location_save called with nonexistant location ID %', in_location_id;
 	    ELSE
 	        -- Okay, we're good.
 	        
@@ -74,8 +74,8 @@
 	            line_three = in_address3,
 	            city = in_city, 
 	            state = in_state,
-	            zipcode = in_zipcode,
-	            country = in_country
+	            mail_code = in_zipcode,
+	            country_id = in_country
 	        WHERE id = in_location_id;
 	        return in_location_id;
 	    END IF;
@@ -156,8 +156,32 @@
 	line_three text,
 	city text,
 	state text,
-        mail_code text,
+    mail_code text,
 	country text,
-	class text
 );
 
+CREATE OR REPLACE FUNCTION location__get(in_id int) returns location_result AS $$
+
+declare
+    l_row location_result;
+begin
+    FOR l_row IN 
+        SELECT 
+            l.id,                   
+            l.line_one,             
+            l.line_two,             
+            l.line_three,           
+            l.city,                 
+            l.state,                
+            l.mail_code,            
+            c.name as country,      
+            NULL
+        FROM location l
+        JOIN country c on l.country_id = c.id
+        WHERE l.id = in_id
+    LOOP
+    
+        return l_row;
+    END LOOP;
+END;
+$$ language plpgsql ;
\ No newline at end of file

Modified: trunk/sql/modules/Person.sql
===================================================================
--- trunk/sql/modules/Person.sql	2008-09-25 15:10:32 UTC (rev 2341)
+++ trunk/sql/modules/Person.sql	2008-09-25 23:08:34 UTC (rev 2342)
@@ -129,11 +129,12 @@
 BEGIN
     
     SELECT cc.* into v_orig 
-    FROM contact_class cc, person p
+    FROM person_to_contact cc, person p
     WHERE p.entity_id = in_entity_id 
-    and contact_class = in_contact_class
-    AND contact = in_contact_orig
+    and cc.contact_class_id = in_contact_class
+    AND cc.contact = in_contact_orig
     AND cc.person_id = p.id;
+    
     IF NOT FOUND THEN
     
         -- create
@@ -163,6 +164,7 @@
 create or replace function person__save_location(
     in_entity_id int, 
     in_location_id int,
+    in_location_class int,
     in_line_one text, 
     in_line_two text, 
     in_line_three text,
@@ -197,8 +199,8 @@
     		in_country_code);
     	
         INSERT INTO person_to_location 
-    		(person_id, location_id)
-    	VALUES  (t_person_id, l_id);
+    		(person_id, location_id, location_class)
+    	VALUES  (t_person_id, l_id, in_location_class);
     ELSE
         l_id := location_save(
             in_location_id, 


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