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

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



Revision: 5664
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=5664&view=rev
Author:   einhverfr
Date:     2013-02-15 07:03:05 +0000 (Fri, 15 Feb 2013)
Log Message:
-----------
Moving recurring save to template transactions framework
fixing a couple accidental references to new journal entry code

Modified Paths:
--------------
    trunk/LedgerSMB/AA.pm
    trunk/LedgerSMB/DBObject/TransTemplate.pm
    trunk/LedgerSMB/Form.pm
    trunk/LedgerSMB/IS.pm
    trunk/LedgerSMB/OE.pm
    trunk/sql/Pg-database.sql
    trunk/sql/modules/Fixes.sql
    trunk/sql/modules/Transaction_Templates.sql

Modified: trunk/LedgerSMB/AA.pm
===================================================================
--- trunk/LedgerSMB/AA.pm	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/LedgerSMB/AA.pm	2013-02-15 07:03:05 UTC (rev 5664)
@@ -728,7 +728,6 @@
 
     #$form->audittrail( $dbh, "", \%audittrail );
 
-    $form->save_recurring( $dbh, $myconfig );
 
     my $rc = $dbh->commit;
 

Modified: trunk/LedgerSMB/DBObject/TransTemplate.pm
===================================================================
--- trunk/LedgerSMB/DBObject/TransTemplate.pm	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/LedgerSMB/DBObject/TransTemplate.pm	2013-02-15 07:03:05 UTC (rev 5664)
@@ -27,6 +27,10 @@
    if ($self->{is_invoice}){
        $self->exec_method(funcname => 'journal__make_invoice');
    }
+   if ($self->{recurringreference}){
+       $self->exec_method(funcname => 'journal__save_recurring');
+       $self->exec_method(funcname => 'journal__save_recurring_print');
+   }
    $self->{dbh}->commit;
 }
 

Modified: trunk/LedgerSMB/Form.pm
===================================================================
--- trunk/LedgerSMB/Form.pm	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/LedgerSMB/Form.pm	2013-02-15 07:03:05 UTC (rev 5664)
@@ -2991,227 +2991,7 @@
     }
 }
 
-=item $form->save_recurring($dbh2, $myconfig);
 
-Saves or deletes recurring transaction scheduling.  $form->{id} is used to
-determine the id used in the various recurring tables.  A recurring transaction
-schedule is deleted by having $form->{recurring} be false.  For adding or
-updating a schedule, $form->{recurring} is a comma separated field with partial
-subfield quoting of the form:
-
-  reference,startdate,repeat,unit,howmany,payment,print,email,message
-       text      date    int text     int     int  text  text    text
-
-=over
-
-=item reference
-
-A URI-encoded reference string for the recurrence set.
-
-=item startdate
-
-The index date for the recurrence.
-
-=item repeat
-
-The unitless repetition frequency.
-
-=item unit
-
-The interval unit used.  Can be 'days', 'weeks', 'months', or 'years',
-capitalisation and pluralisation ignored.
-
-=item howmany
-
-The number of recurrences for the transaction.
-
-=item payment
-
-Flag to indicate if a payment is included in the transaction.
-
-=item print
-
-A colon separated list of formname:format:printer triplets.
-
-=item email
-
-A colon separated list of formname:format pairs.
-
-=item message
-
-A URI-encoded message for the emails to be sent.
-
-=back
-
-Values for the nextdate and enddate columns of the recurring table are
-calculated using startdate, repeat, unit, howmany, and the current database
-date.  All other fields of the recurring, recurringemail, and recurringprint are
-obtained directly from $form->{recurring}.
-
-B<WARNING>: This function does not check the validity of most subfields of
-$form->{recurring}.
-
-$dbh2 is not used.
-
-=cut
-
-sub save_recurring {
-
-    my ( $self, $dbh2, $myconfig ) = @_;
-
-    my $dbh = $self->{dbh};
-
-    my $query;
-
-    $query = qq|DELETE FROM recurring
-				 WHERE id = ?|;
-
-    my $sth = $dbh->prepare($query) || $self->dberror($query);
-    $sth->execute( $self->{id} ) || $self->dberror($query);
-
-    $query = qq|DELETE FROM recurringemail
-				 WHERE id = ?|;
-
-    $sth = $dbh->prepare($query) || $self->dberror($query);
-    $sth->execute( $self->{id} ) || $self->dberror($query);
-
-    $query = qq|DELETE FROM recurringprint
-				 WHERE id = ?|;
-
-    $sth = $dbh->prepare($query) || $self->dberror($query);
-    $sth->execute( $self->{id} ) || $self->dberror($query);
-
-    if ( $self->{recurring} ) {
-
-        my %s = ();
-        (
-            $s{reference}, $s{startdate}, $s{repeat},
-            $s{unit},      $s{howmany},   $s{payment},
-            $s{print},     $s{email},     $s{message}
-        ) = split /,/, $self->{recurring};
-
-        if ($s{unit} !~ /^(day|week|month|year)s?$/i){
-            $dbh->rollback;
-            $self->error("Invalid recurrence unit");
-        }
-        if ($s{howmany} == 0){
-            $self->error("Cannot set to recur 0 times");
-        }
-
-        for (qw(reference message)) { $s{$_} = $self->unescape( $s{$_} ) }
-        for (qw(repeat howmany payment)) { $s{$_} *= 1 }
-
-        # calculate enddate
-        my $advance = $s{repeat} * ( $s{howmany} - 1 );
-
-        $query = qq|SELECT (?::date + interval '$advance $s{unit}')|;
-
-        my ($enddate) = $dbh->selectrow_array($query, undef, $s{startdate});
-
-        # calculate nextdate
-        $query = qq|
-			SELECT current_date - ?::date AS a,
-				?::date - current_date AS b|;
-
-        $sth = $dbh->prepare($query) || $self->dberror($query);
-        $sth->execute( $s{startdate}, $enddate ) || $self->dberror($query);
-        my ( $a, $b ) = $sth->fetchrow_array;
-
-        if ( $a + $b ) {
-            $advance =
-              int( ( $a / ( $a + $b ) ) * ( $s{howmany} - 1 ) + 1 ) *
-              $s{repeat};
-        }
-        else {
-            $advance = 0;
-        }
-
-        my $nextdate = $enddate;
-        if ( $advance > 0 ) {
-            if ( $advance < ( $s{repeat} * $s{howmany} ) ) {
-
-                $query = qq|SELECT (?::date + interval '$advance $s{unit}')|;
-
-                ($nextdate) = $dbh->selectrow_array($query, undef, $s{startdate});
-            }
-
-        }
-        else {
-            $nextdate = $s{startdate};
-        }
-
-        if ( $self->{recurringnextdate} ) {
-
-            $nextdate = $self->{recurringnextdate};
-
-            $query = qq|SELECT ?::date - ?::date|;
-
-            if ( $dbh->selectrow_array($query, undef, $enddate, $nextdate) < 0 ) {
-                undef $nextdate;
-            }
-        }
-
-        $self->{recurringpayment} *= 1;
-
-        $query = qq|
-			INSERT INTO recurring 
-				(id, reference, startdate, enddate, nextdate, 
-				repeat, unit, howmany, payment)
-			VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)|;
-
-        $sth = $dbh->prepare($query);
-        $sth->execute(
-            $self->{id}, $s{reference}, $s{startdate},
-            $enddate,    $nextdate,     $s{repeat},
-            $s{unit},    $s{howmany},   $s{payment}
-        );
-
-        my @p;
-        my $p;
-        my $i;
-        my $sth;
-
-        if ( $s{email} ) {
-
-            # formname:format
-            @p = split /:/, $s{email};
-
-            $query =
-              qq|INSERT INTO recurringemail (id, formname, format, message)
-						VALUES (?, ?, ?, ?)|;
-
-            $sth = $dbh->prepare($query) || $self->dberror($query);
-
-            for ( $i = 0 ; $i <= $#p ; $i += 2 ) {
-                $sth->execute( $self->{id}, $p[$i], $p[ $i + 1 ], $s{message} );
-            }
-
-            $sth->finish;
-        }
-
-        if ( $s{print} ) {
-
-            # formname:format:printer
-            @p = split /:/, $s{print};
-
-            $query =
-              qq|INSERT INTO recurringprint (id, formname, format, printer)
-						VALUES (?, ?, ?, ?)|;
-
-            $sth = $dbh->prepare($query) || $self->dberror($query);
-
-            for ( $i = 0 ; $i <= $#p ; $i += 3 ) {
-                $p = ( $p[ $i + 2 ] ) ? $p[ $i + 2 ] : "";
-                $sth->execute( $self->{id}, $p[$i], $p[ $i + 1 ], $p );
-            }
-
-            $sth->finish;
-        }
-    }
-    $dbh->commit;
-
-}
-
 =item $form->save_intnotes($myconfig, $vc);
 
 Sets the intnotes field of the entry in the table $vc that has the id

Modified: trunk/LedgerSMB/IS.pm
===================================================================
--- trunk/LedgerSMB/IS.pm	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/LedgerSMB/IS.pm	2013-02-15 07:03:05 UTC (rev 5664)
@@ -1676,8 +1676,6 @@
 
     $form->audittrail( $dbh, "", \%audittrail );
 
-    $form->save_recurring( $dbh, $myconfig );
-
     my $rc = $dbh->commit;
 
     $rc;

Modified: trunk/LedgerSMB/OE.pm
===================================================================
--- trunk/LedgerSMB/OE.pm	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/LedgerSMB/OE.pm	2013-02-15 07:03:05 UTC (rev 5664)
@@ -500,8 +500,6 @@
 
    # $form->audittrail( $dbh, "", \%audittrail );
 
-    $form->save_recurring( $dbh, $myconfig );
-
     my $rc = $dbh->commit;
 
     $rc;

Modified: trunk/sql/Pg-database.sql
===================================================================
--- trunk/sql/Pg-database.sql	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/sql/Pg-database.sql	2013-02-15 07:03:05 UTC (rev 5664)
@@ -1161,7 +1161,7 @@
 cleared.$$;
 
 COMMENT ON COLUMN cr_report_line.scn IS
-$$ This is the check number.  Maps to journal_entry.reference $$;
+$$ This is the check number.  Maps to gl.reference $$;
 
 CREATE TABLE cr_coa_to_account (
     chart_id int not null references account(id),
@@ -2220,7 +2220,7 @@
 $$ This table sets the basic preferences for formats, languages, printers, and user-selected stylesheets.$$;
 
 CREATE TABLE recurring (
-  id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
+  id int references journal_entry(id),
   reference text,
   startdate date,
   nextdate date,
@@ -2244,7 +2244,7 @@
 
 --
 CREATE TABLE recurringemail (
-  id int,
+  id int references recurring(id), 
   formname text,
   format text,
   message text,
@@ -2255,7 +2255,7 @@
 $$Email  to be sent out when recurring transaction is posted.$$;
 --
 CREATE TABLE recurringprint (
-  id int,
+  id int references recurring(id),
   formname text,
   format text,
   printer text,
@@ -3760,7 +3760,7 @@
 CREATE TABLE asset_report (
 	id serial primary key,
 	report_date date,
-	gl_id bigint references journal_entry(id) unique,
+	gl_id bigint references gl(id) unique,
 	asset_class bigint references asset_class(id),
 	report_class int references asset_report_class(id),
 	entered_by bigint not null references entity(id),
@@ -4650,7 +4650,7 @@
 CREATE TABLE file_order_to_tx (
        PRIMARY KEY(file_id, source_class, dest_class, ref_key),
        foreign key (file_id) references file_order(id),
-       foreign key (ref_key) references journal_entry(id),
+       foreign key (ref_key) references gl(id),
        check (source_class = 2),
        check (dest_class = 1)
 ) INHERITS (file_secondary_attachment);

Modified: trunk/sql/modules/Fixes.sql
===================================================================
--- trunk/sql/modules/Fixes.sql	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/sql/modules/Fixes.sql	2013-02-15 07:03:05 UTC (rev 5664)
@@ -166,3 +166,13 @@
 SELECT SETVAL('contact_class_id_seq',19);
 
 COMMIT;
+
+BEGIN;
+
+ALTER TABLE asset_report DROP CONSTRAINT "asset_report_gl_id_fkey";
+ALTER TABLE asset_report ADD FOREIGN KEY gl_id REFERENCES gl(id);
+
+ALTER TABLE file_order_to_tx DROP CONSTRAINT "file_order_to_tx_ref_key_fkey";
+ALTER TABLE file_order_to_tx ADD FOREIGN KEY ref_key REFERENCES gl(id);
+
+COMMIT;

Modified: trunk/sql/modules/Transaction_Templates.sql
===================================================================
--- trunk/sql/modules/Transaction_Templates.sql	2013-02-14 15:33:13 UTC (rev 5663)
+++ trunk/sql/modules/Transaction_Templates.sql	2013-02-15 07:03:05 UTC (rev 5664)
@@ -78,7 +78,8 @@
 is_template bool,
 meta_number text,
 entity_name text,
-entity_class text
+entity_class text,
+nextdate date,
 );
 
 CREATE OR REPLACE FUNCTION journal__search(
@@ -90,7 +91,8 @@
 in_department_id int, 
 in_is_template bool,
 in_meta_number text,
-in_entity_class int	
+in_entity_class int,
+in_recurring bool
 ) RETURNS SETOF journal_search_result AS $$
 DECLARE retval journal_search_result;
 BEGIN
@@ -98,12 +100,16 @@
 		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
+			e.name, ec.class, 
+                        coalesce(
+                          r.startdate + r.recurring_interval,
+                          j.transaction_date);
 		FROM journal_entry 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)
+                LEFT JOIN recurring r ON j.id = r.id
 		WHERE (in_source IS NULL OR in_source = j.source) AND
 			(in_description IS NULL 
 				or in_description = j.description) AND
@@ -140,4 +146,23 @@
 $$ language sql;
 -- orders with inventory not supported yet.
 
+CREATE OR REPLACE FUNCTION journal__save_recurring
+(in_recurringreference text, in_recurringstartdate date, 
+in_recurring_interval interval, in_recurringhowmany int, in_id int) 
+RETURNS recurring LANGUAGE SQL AS
+$$
+delete from recurringprint where id = $5;
+delete from recurring where id = $5;
+insert into recurring (id, reference, startdate, interval, howmany)
+values ($5, $1, $2, $3, $4);
+$$;
+
+CREATE OR REPLACE FUNCTION journal__save_recurring_print
+(in_id int, in_formname text, in_printer text)
+RETURNS recurringprint LANGUAGE SQL AS
+$$
+insert into recurringprint (id, formname, format, printer)
+values ($1, $2, 'PDF', $3);
+$$;
+
 COMMIT;

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