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

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



Revision: 5086
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=5086&view=rev
Author:   einhverfr
Date:     2012-08-03 06:50:39 +0000 (Fri, 03 Aug 2012)
Log Message:
-----------
Merging template transactions add-on into 1.4

Modified Paths:
--------------
    trunk/bin/aa.pl
    trunk/bin/gl.pl
    trunk/sql/Pg-database.sql
    trunk/sql/modules/LOADORDER
    trunk/sql/modules/Roles.sql

Added Paths:
-----------
    trunk/LedgerSMB/DBObject/TransTemplate.pm
    trunk/sql/modules/Transaction_Templates.sql

Added: trunk/LedgerSMB/DBObject/TransTemplate.pm
===================================================================
--- trunk/LedgerSMB/DBObject/TransTemplate.pm	                        (rev 0)
+++ trunk/LedgerSMB/DBObject/TransTemplate.pm	2012-08-03 06:50:39 UTC (rev 5086)
@@ -0,0 +1,74 @@
+package LedgerSMB::DBObject::TransTemplate;
+use base qw(LedgerSMB::DBObject);
+use strict;
+
+sub save {
+   my $self = shift @_;
+   $self->{is_template} = '1';
+   $self->{approved} = 0;
+   $self->{source} = $self->{invnumber} if $self->{invnumber};
+   my ($ref) = $self->exec_method(funcname => 'journal__add');
+   $self->merge($ref);
+   $self->{journal_id} = $self->{id};
+   $self->debug({file => '/tmp/temptrans'});
+   for my $line (@{$self->{journal_lines}}){
+       my $l = bless $line, 'LedgerSMB::DBObject';
+       $l->{_locale} = $self->{_locale};
+       $l->{dbh} = $self->{dbh};
+       $l->{journal_id} = $self->{id};
+       my ($ref) = $l->exec_method(funcname => 'account__get_from_accno');
+       $l->{account_id} = $ref->{id};
+       print STDERR "$l->{accno}\n";
+       if (!$ref->{id}){
+           $self->error($self->{_locale}->text('No Account id for [_1]', $l->{accno}));
+       }
+       $l->exec_method(funcname=> 'journal__add_line');
+   } 
+   if ($self->{is_invoice}){
+       $self->exec_method(funcname => 'journal__make_invoice');
+   }
+   $self->{dbh}->commit;
+}
+
+sub search {
+   my $self = shift @_;
+   $self->{approved} = 'false';
+   $self->{is_template} = 'true';
+   @{$self->{search_results}} = $self->exec_method(
+            funcname => 'journal__search'
+   );
+}
+
+sub retrieve {
+   my $self = shift @_;
+   my @vals = $self->exec_method(funcname => 'journal__retrieve');
+   $self->merge(shift @vals);
+   @{$self->{line_items}} = $self->exec_method(funcname => 'journal__retrieve_lines');
+   ($self->{inv_data}) = $self->exec_method(funcname => 'journal__retrieve_invoice');
+}
+
+sub get {
+    my ($self) = @_;
+    my ($ref) = $self->exec_method(funcname => 'journal__get_entry');
+    $self->merge($ref);
+    @{$self->{line_items}} =  $self->exec_method(funcname => 'journal__lines');
+    ($self->{invoice_data}) =
+                 $self->exec_method(funcname => 'journal__get_invoice');
+    if ($self->{invoice_data}->{credit_id}){
+        ($self->{credit_data}) = $self->call_procedure(
+               procname => 'entity_credit__get',
+               args     => [$self->{invoice_data}->{credit_id}]
+        );
+    }
+}
+
+sub get_account_info {
+    my ($self, $acct_id) = @_;
+    my ($ref) = $self->call_procedure(
+               procname => 'account_get', 
+               args     => [$acct_id]
+    );
+    return $ref;
+}
+
+1;

Modified: trunk/bin/aa.pl
===================================================================
--- trunk/bin/aa.pl	2012-08-03 05:17:09 UTC (rev 5085)
+++ trunk/bin/aa.pl	2012-08-03 06:50:39 UTC (rev 5086)
@@ -108,7 +108,6 @@
 }
 
 sub add {
-
     $form->{title} = "Add";
 	
     $form->{callback} =
@@ -1008,6 +1007,8 @@
 
             'save_info' => 
               { ndx => 9, key => 'I', value => $locale->text('Save Info') },
+            'save_temp' =>
+              { ndx => 10, key => 'T', value => $locale->text('Save Template')},
             'new_screen' => # Create a blank ar/ap invoice.
              { ndx => 10, key=> 'N', value => $locale->text('New') }
         );
@@ -1135,6 +1136,41 @@
 |;
 }
 
+sub save_temp {
+    use LedgerSMB;
+    use LedgerSMB::DBObject::TransTemplate;
+    my $lsmb = LedgerSMB->new();
+    $lsmb->merge($form);
+    $lsmb->{is_invoice} = 1;
+    my ($department_name, $department_id) = split/--/, $form->{department};
+     if (!$lsmb->{language_code}){
+        delete $lsmb->{language_code};
+    }
+    $lsmb->{credit_id} = $form->{"$form->{vc}_id"};
+    $lsmb->{department_id} = $department_id;
+    if ($form->{arap} eq 'ar'){
+        $lsmb->{entity_class} = 2;
+    } else {
+        $lsmb->{entity_class} = 1;
+    }
+    $lsmb->{transaction_date} = $form->{transdate}; 
+    for my $iter (0 .. $form->{rowcount}){
+        if ($form->{"AP_amount_$iter"} and 
+                  ($form->{"amount_$iter"} != 0)){
+             my ($acc_id, $acc_name) = split /--/, $form->{"AP_amount_$iter"};
+             my $amount = $form->{"amount_$iter"};
+             push @{$lsmb->{journal_lines}}, 
+                  {accno => $acc_id,
+                   amount => $amount, 
+                   cleared => false,
+                  };
+        }
+    }
+    $template = LedgerSMB::DBObject::TransTemplate->new(base => $lsmb);
+    $template->save;
+    $form->redirect( $locale->text('Template Saved!') );
+}
+
 sub edit_and_approve {
     use LedgerSMB::DBObject::Draft;
     use LedgerSMB;

Modified: trunk/bin/gl.pl
===================================================================
--- trunk/bin/gl.pl	2012-08-03 05:17:09 UTC (rev 5085)
+++ trunk/bin/gl.pl	2012-08-03 06:50:39 UTC (rev 5086)
@@ -267,6 +267,10 @@
 		  'update' =>
 		    { ndx => 1, key => 'U', value => $locale->text('Update') },
 		  'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
+                  'save_temp' =>
+                    { ndx   => 9, 
+                      key   => 'T', 
+                      value => $locale->text('Save Template') },
 		  'post_as_new' =>
 		    { ndx => 6, key => 'N', value => $locale->text('Post as new') },
 		  'schedule' =>
@@ -280,19 +284,17 @@
 		  $button{post}->{value} = $locale->text('Save'); 
 	      }
 	      %a = ();
-              if ($form->{id}){
-                 $a{'new'} = 1;
-                 
-              } else {
-                 $a{'update'} = 1;
-              }
+              $a{'save_temp'} = 1;
+
 	      if ( $form->{id}) {
+                  $a{'new'} = 1;
 
 		  for ( 'post_as_new', 'schedule' ) { $a{$_} = 1 }
 
 		  for ( 'post', 'delete' ) { $a{$_} = 1 }
 	      }
 	      elsif (!$form->{id}){
+                 $a{'update'} = 1;
 		  if ( $transdate > $closedto ) {
 		      for ( "post", "schedule" ) { $a{$_} = 1 }
 		  }
@@ -361,7 +363,37 @@
 }
  
 
+sub save_temp {
+    use LedgerSMB;
+    use LedgerSMB::DBObject::TransTemplate;
+    my $lsmb = LedgerSMB->new();
+    my ($department_name, $department_id) = split/--/, $form->{department};
+    $lsmb->{department_id} = $department_id;
+    $lsmb->{source} = $form->{reference};
+    $lsmb->{description} = $form->{description};
+    $lsmb->{department_id} = $department_id;
+    $lsmb->{transaction_date} = $form->{transdate};
+    $lsmb->{type} = 'gl';
+    $lsmb->{journal_lines} = [];
+    for my $iter (0 .. $form->{rowcount}){
+        if ($form->{"accno_$iter"} and 
+                  (($form->{"credit_$iter"} != 0) or ($form->{"debit_$iter"} != 0))){
+             my ($acc_id, $acc_name) = split /--/, $form->{"accno_$iter"};
+             my $amount = $form->{"credit_$iter"} || ( $form->{"debit_$iter"} 
+                                                     * -1 );
+             push @{$lsmb->{journal_lines}}, 
+                  {accno => $acc_id,
+                   amount => $amount, 
+                   cleared => false,
+                  };
+        }
+    }
+    $template = LedgerSMB::DBObject::TransTemplate->new(base => $lsmb);
+    $template->save;
+    $form->redirect( $locale->text('Template Saved!') );
+}
 
+
 sub display_row
 {
  

Modified: trunk/sql/Pg-database.sql
===================================================================
--- trunk/sql/Pg-database.sql	2012-08-03 05:17:09 UTC (rev 5085)
+++ trunk/sql/Pg-database.sql	2012-08-03 06:50:39 UTC (rev 5086)
@@ -1231,6 +1231,13 @@
     primary key (id)
 );
 
+CREATE TABLE business_unit_jl (
+    entry_id int references journal_line(id),
+    bu_class int references business_unit_class(id),
+    bu_id int references business_unit(id) NOT NULL.
+    PRIMARY KEY(entry_id, bu_class)
+);
+
 COMMENT ON TABLE journal_line IS
 $$ Replaces acc_trans as the main account transaction line table.$$;
 

Modified: trunk/sql/modules/LOADORDER
===================================================================
--- trunk/sql/modules/LOADORDER	2012-08-03 05:17:09 UTC (rev 5085)
+++ trunk/sql/modules/LOADORDER	2012-08-03 06:50:39 UTC (rev 5086)
@@ -35,3 +35,4 @@
 Fixes.sql
 Inventory_Report.sql
 Payroll.sql
+Transaction_Templates.sql

Modified: trunk/sql/modules/Roles.sql
===================================================================
--- trunk/sql/modules/Roles.sql	2012-08-03 05:17:09 UTC (rev 5085)
+++ trunk/sql/modules/Roles.sql	2012-08-03 06:50:39 UTC (rev 5086)
@@ -242,14 +242,16 @@
 GRANT "lsmb_<?lsmb dbname ?>__exchangerate_edit"
    TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
 
-GRANT INSERT ON ar, invoice_note, business_unit_ac 
+GRANT INSERT ON ar, invoice_note, business_unit_ac, jounral_entry, journal_line,
+business_unit_jl
 TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
 
 GRANT SELECT ON oe TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
 
 GRANT ALL ON id TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
 GRANT INSERT ON acc_trans TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
-GRANT ALL ON acc_trans_entry_id_seq TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
+GRANT ALL ON acc_trans_entry_id_seq, journal_entry_id_seq, journal_line_id_seq 
+TO "lsmb_<?lsmb dbname ?>__ar_transaction_create";
 INSERT INTO menu_acl (node_id, acl_type, role_name)
 values (2, 'allow', 'lsmb_<?lsmb dbname ?>__ar_transaction_create');
 INSERT INTO menu_acl (node_id, acl_type, role_name)
@@ -425,12 +427,14 @@
    TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
 
 
-GRANT SELECT, INSERT ON ap, invoice_note 
+GRANT SELECT, INSERT ON ap, invoice_note, journal_entry, journal_line, 
+business_unit_jl
 TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
 GRANT ALL ON id TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
 GRANT INSERT ON acc_trans, business_unit_ac
  TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
-GRANT ALL ON acc_trans_entry_id_seq TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
+GRANT ALL ON acc_trans_entry_id_seq, journal_entry_id_seq, journal_line_id_seq
+TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
 
 GRANT SELECT ON oe TO "lsmb_<?lsmb dbname ?>__ap_transaction_create";
 
@@ -940,9 +944,11 @@
 
 GRANT SELECT, INSERT, UPDATe ON gl 
 TO "lsmb_<?lsmb dbname ?>__gl_transaction_create";
-GRANT INSERT ON acc_trans TO "lsmb_<?lsmb dbname ?>__gl_transaction_create";
+GRANT INSERT ON acc_trans, journal_entry, journal_line 
+TO "lsmb_<?lsmb dbname ?>__gl_transaction_create";
 GRANT ALL ON id TO "lsmb_<?lsmb dbname ?>__gl_transaction_create";
-GRANT ALL ON acc_trans_entry_id_seq TO "lsmb_<?lsmb dbname ?>__gl_transaction_create";
+GRANT ALL ON acc_trans_entry_id_seq, journal_entry_id_seq, journal_line_id_seq
+TO "lsmb_<?lsmb dbname ?>__gl_transaction_create";
 
 INSERT INTO menu_acl (node_id, acl_type, role_name)
 values (74, 'allow', 'lsmb_<?lsmb dbname ?>__gl_transaction_create');

Added: trunk/sql/modules/Transaction_Templates.sql
===================================================================
--- trunk/sql/modules/Transaction_Templates.sql	                        (rev 0)
+++ trunk/sql/modules/Transaction_Templates.sql	2012-08-03 06:50:39 UTC (rev 5086)
@@ -0,0 +1,137 @@
+-- Many of these will have to be rewritten to work with 1.4
+
+CREATE OR REPLACE FUNCTION journal__add(
+in_source text,
+in_description text,
+in_entry_type int,
+in_transaction_date date,
+in_approved bool,
+in_is_template bool
+) RETURNS journal AS 
+$$
+DECLARE retval journal;
+BEGIN
+	INSERT INTO journal (source, description, entry_type, transaction_date,
+			approved, is_template)
+	VALUES (in_source, in_description, in_entry_type, in_transaction_date,
+			coalesce(in_approved, false), 
+			coalesce(in_is_template, false));
+
+	SELECT * INTO retval FROM journal WHERE id = currval('journal_id_seq');
+	RETURN retval;
+END;
+$$ language plpgsql; 
+
+CREATE OR REPLACE FUNCTION journal__add_line(
+in_account_id int, in_journal_id int, in_amount numeric, 
+in_cleared bool, in_memo text, in_business_units int[],
+) RETURNS journal_line AS $$
+DECLARE retval journal_line;
+BEGIN
+	INSERT INTO journal_line(account_id, journal_id, amount, cleared, memo)
+	VALUES (in_account_id, in_journal_id, in_amount, 
+		coalesce(in_cleared, false), in_memo);
+
+        INSERT INTO business_unit_jl(entry_id, bu_class, bu_id)
+        SELECT currval('journal_line_line_id_seq'), business_unit_class, bu
+          FROM business_unit
+         WHERE id = any(in_business_units);
+
+	SELECT * INTO retval FROM journal_line where line_id = currval('journal_line_line_id_seq');
+	return retval;
+END;
+$$ LANGUAGE PLPGSQL;
+
+CREATE OR REPLACE FUNCTION journal__validate_entry(in_id int) RETURNS bool AS
+$$
+	SELECT sum(amount) = 0 FROM journal_line WHERE journal_id = $1;
+$$ language sql;
+
+CREATE OR REPLACE FUNCTION journal__make_invoice(
+in_order_id int,  in_journal_id int, in_on_hold bool, in_reverse bool,
+in_credit_id int, in_language_code varchar
+) returns eca_invoice AS $$
+DECLARE retval eca_invoice;
+BEGIN	
+	INSERT INTO eca_invoice (order_id, journal_id, on_hold, reverse,
+		credit_id, language_code)
+	VALUES (in_order_id, in_journal_id, coalesce(in_on_hold, false), 
+		in_reverse, in_credit_id, in_language_code);
+
+	SELECT * INTO retval FROM eca_invoice WHERE journal_id = in_journal_id;
+
+	RETURN retval;
+END;
+$$ language plpgsql;
+
+CREATE TYPE journal_search_result AS (
+id bigint,
+source text,
+description text,
+entry_type int,
+transaction_date date,
+approved bool,
+is_template bool,
+meta_number text,
+entity_name text,
+entity_class text
+);
+
+CREATE OR REPLACE FUNCTION journal__search(
+in_source text,
+in_description text,
+in_entry_type int,
+in_transaction_date date,
+in_approved bool,
+in_department_id int, 
+in_is_template bool,
+in_meta_number text,
+in_entity_class int	
+) RETURNS SETOF journal_search_result AS $$
+DECLARE retval journal_search_result;
+BEGIN
+	FOR retval IN 
+		SELECT j.id, j.source, j.description, j.entry_type, 
+			j.transaction_date, j.approved, 
+			j.is_template, eca.meta_number, 
+			e.name, ec.class
+		FROM journal j
+		LEFT JOIN eca_invoice i ON (i.journal_id = j.id)
+		LEFT JOIN entity_credit_account eca ON (eca.id = credit_id)
+		LEFT JOIN entity e ON (eca.entity_id = e.id)
+		LEFT JOIN entity_class ec ON (eca.entity_class = ec.id)
+		WHERE (in_source IS NULL OR in_source = j.source) AND
+			(in_description IS NULL 
+				or in_description = j.description) AND
+			(in_entry_type is null or in_entry_type = j.entry_type)
+			and (in_transaction_date is null 
+				or in_transaction_date = j.transaction_date) and
+			j.approved = coalesce(in_approved, true) and
+			j.is_template = coalesce(in_is_template, false) and
+			(in_department_id is null 
+				or j.department_id = in_department_id) and
+			(in_meta_number is null 
+				or eca.meta_number = in_meta_number) and
+			(in_entity_class is null
+				or eca.entity_class = in_entity_class)
+	LOOP
+		RETURN NEXT retval;
+	END LOOP;
+END;
+$$ language plpgsql;
+
+CREATE OR REPLACE FUNCTION journal__get_invoice(in_id int) RETURNS eca_invoice AS
+$$
+SELECT * FROM eca_invoice where journal_id = $1;
+$$ language sql;
+
+CREATE OR REPLACE FUNCTION journal__get_entry(in_id int) RETURNS journal AS
+$$
+SELECT * FROM journal where id = $1;
+$$ language sql;
+
+CREATE OR REPLACE FUNCTION journal__lines(in_id int) RETURNS SETOF journal_line AS
+$$
+select * from journal_line where journal_id = $1;
+$$ language sql;
+-- orders with inventory not supported yet.

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