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

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



Revision: 1456
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=1456&view=rev
Author:   tetragon
Date:     2007-08-09 15:15:21 -0700 (Thu, 09 Aug 2007)

Log Message:
-----------
POD and more input validation in save_recurring

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

Modified: trunk/LedgerSMB/Form.pm
===================================================================
--- trunk/LedgerSMB/Form.pm	2007-08-09 20:08:36 UTC (rev 1455)
+++ trunk/LedgerSMB/Form.pm	2007-08-09 22:15:21 UTC (rev 1456)
@@ -172,6 +172,8 @@
 single encoding run.  When false, it escapes the string twice if it detects
 that it is running on a version of Apache 2.0 earlier than 2.0.44.
 
+Note that recurring transaction support depends on this function escaping ','.
+
 =cut
 
 sub escape {
@@ -185,6 +187,7 @@
     }
 
     utf8::encode($str);
+    # SC: Adding commas to the ignore list will break recurring transactions
     $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
     $str;
 
@@ -764,7 +767,7 @@
     return ( $amount * 1 );
 }
 
-=item rount_amount($amount, $places);
+=item $form->round_amount($amount, $places);
 
 Rounds the provided $amount to $places decimal places.
 
@@ -840,7 +843,7 @@
     @results;
 }
 
-=item $form->get_my_emp_num($myconfig, \%$form);
+=item $form->get_my_emp_num($myconfig);
 
 Function to get the employee number of the user $form->{login}.  $myconfig is
 only used to create %myconfig.  $form->{emp_num} is set to the retrieved value.
@@ -1122,6 +1125,15 @@
     }
 }
 
+=item $form->run_custom_queries($tablename, $query_type[, $linenum]);
+
+Runs queries against custom fields for the specified $query_type against
+$tablename.
+
+Valid values for $query_type are any casing of 'SELECT', 'INSERT', and 'UPDATE'.
+
+=cut
+
 sub run_custom_queries {
     my ( $self, $tablename, $query_type, $linenum ) = @_;
     my $dbh = $self->{dbh};
@@ -1465,6 +1477,17 @@
     $exchangerate;
 }
 
+=item $form->add_shipto($dbh, $id);
+
+Inserts a new address into the table shipto if the value of any of the shipto
+address components in $form differs to the regular attribute in $form.  The
+inserted value of trans_id is $id, the other fields correspond with the shipto
+address components of $form.
+
+$dbh is unused.
+
+=cut
+
 sub add_shipto {
     my ( $self, $dbh, $id ) = @_;
 
@@ -1504,6 +1527,15 @@
     }
 }
 
+=item $form->get_employee($dbh);
+
+Returns a list containing the name and id of the employee $form->{login}.  Any
+portion of $form->{login} including and past '@' are ignored.
+
+$dbh is unused.
+
+=cut
+
 sub get_employee {
     my ( $self, $dbh ) = @_;
 
@@ -1710,6 +1742,17 @@
     }
 }
 
+=item $form->all_employees($myconfig, $dbh2, $transdate, $sales);
+
+Sets $form->{all_employee} to be a reference to an array referencing hashes of
+employee information.  The hashes are of the form {'id' => id, 'name' => name}.
+If $transdate is set, the query is limited to employees who are active on that
+day.  If $sales is true, only employees with the sales flag set are added.
+
+$dbh2 is unused.
+
+=cut 
+
 sub all_employees {
 
     my ( $self, $myconfig, $dbh2, $transdate, $sales ) = @_;
@@ -1796,6 +1839,19 @@
     $sth->finish;
 }
 
+=item $form->all_departments($myconfig, $dbh2, $vc);
+
+Set $form->{all_department} to be a reference to a list of hashrefs describing
+departments of the form {'id' => id, 'description' => description}.  If $vc
+is 'customer', further limit the results to those whose role is 'P' (Profit
+Center).
+
+This procedure is internally followed by a call to $form->all_years($myconfig).
+
+$dbh2 is not used.
+
+=cut
+
 sub all_departments {
 
     my ( $self, $myconfig, $dbh2, $vc ) = @_;
@@ -1828,6 +1884,17 @@
     $self->all_years($myconfig);
 }
 
+=item $form->all_years($myconfig[, $dbh2]);
+
+Populates the hash $form->{all_month} with a mapping between a two-digit month
+number and the English month name.  Populates the list $form->{all_years} with
+the years between the year of the oldest transaction date in acc_trans and the
+newest, inclusive.
+
+$dbh2 is unused.
+
+=cut
+
 sub all_years {
 
     my ( $self, $myconfig, $dbh2 ) = @_;
@@ -2249,6 +2316,17 @@
     $sth->finish;
 }
 
+=item $form->update_status($myconfig);
+
+DELETEs all status rows which have a formname of $form->{formname} and a 
+trans_id of $form->{id}.  INSERTs a new row into status where trans_id is
+$form->{id}, formname is $form->{formname}, printed and emailed are true if
+their respective $form attributes match /$form->{formname}/,,and spoolfile is
+the file extracted from the string $form->{queued} or NULL if there is no entry
+for $form->{formname}.
+
+=cut
+
 sub update_status {
 
     my ( $self, $myconfig ) = @_;
@@ -2408,6 +2486,70 @@
     }
 }
 
+=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 seperated 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 seperated list of formname:format:printer triplets.
+
+=item email
+
+A colon seperated 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 ) = @_;
@@ -2443,6 +2585,10 @@
             $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");
         }
@@ -2454,11 +2600,11 @@
         my $advance = $s{repeat} * ( $s{howmany} - 1 );
         my %interval;
         $interval{'Pg'} =
-          "(date '$s{startdate}' + interval '$advance $s{unit}')";
+          "(?::date + interval '$advance $s{unit}')";
 
         $query = qq|SELECT $interval{$myconfig->{dbdriver}}|;
 
-        my ($enddate) = $dbh->selectrow_array($query);
+        my ($enddate) = $dbh->selectrow_array($query, undef, $s{startdate});
 
         # calculate nextdate
         $query = qq|
@@ -2482,9 +2628,9 @@
         if ( $advance > 0 ) {
             if ( $advance < ( $s{repeat} * $s{howmany} ) ) {
 
-                $query = qq|SELECT (date '$s{startdate}' + interval '$advance $s{unit}')|;
+                $query = qq|SELECT (?::date + interval '$advance $s{unit}')|;
 
-                ($nextdate) = $dbh->selectrow_array($query);
+                ($nextdate) = $dbh->selectrow_array($query, undef, $s{startdate});
             }
 
         }
@@ -2496,9 +2642,9 @@
 
             $nextdate = $self->{recurringnextdate};
 
-            $query = qq|SELECT '$enddate' - date '$nextdate'|;
+            $query = qq|SELECT ?::date - ?::date|;
 
-            if ( $dbh->selectrow_array($query) < 0 ) {
+            if ( $dbh->selectrow_array($query, undef, $enddate, $nextdate) < 0 ) {
                 undef $nextdate;
             }
         }
@@ -2564,6 +2710,15 @@
 
 }
 
+=item $form->save_intnotes($myconfig, $vc);
+
+Sets the intnotes field of the entry in the table $vc that has the id
+$form->{id} to the value of $form->{intnotes}.
+
+Does nothing if $form->{id} is not set.
+
+=cut
+
 sub save_intnotes {
 
     my ( $self, $myconfig, $vc ) = @_;
@@ -3092,3 +3247,4 @@
 
 
 
+


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