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

SF.net SVN: ledger-smb:[5773] trunk/LedgerSMB/Form.pm



Revision: 5773
          http://sourceforge.net/p/ledger-smb/code/5773
Author:   einhverfr
Date:     2013-05-12 08:17:43 +0000 (Sun, 12 May 2013)
Log Message:
-----------
Adding back in the save_recurring logic because we are unlikely to replace this before GA

Modified Paths:
--------------
    trunk/LedgerSMB/Form.pm

Modified: trunk/LedgerSMB/Form.pm
===================================================================
--- trunk/LedgerSMB/Form.pm	2013-05-11 19:45:50 UTC (rev 5772)
+++ trunk/LedgerSMB/Form.pm	2013-05-12 08:17:43 UTC (rev 5773)
@@ -2991,7 +2991,228 @@
     }
 }
 
+=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 (?, null, ?, ?, ?, ?, ?, ?, ?)|;
+
+        $sth = $dbh->prepare($query);
+        $sth->execute(
+            $self->{id}, $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

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