[freeside-commits] branch FREESIDE_4_BRANCH updated. 0c6517d34fb0624079949223210ea298eab7e601

Jonathan Prykop jonathan at 420.am
Mon Jul 20 17:36:40 PDT 2015


The branch, FREESIDE_4_BRANCH has been updated
       via  0c6517d34fb0624079949223210ea298eab7e601 (commit)
       via  078454a5494f6611eef48f8ad06c1a5a5c719e00 (commit)
      from  3c8b1c8f6953ed8b7af5cdfbb47d7a4d04b2f7b5 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 0c6517d34fb0624079949223210ea298eab7e601
Author: Jonathan Prykop <jonathan at freeside.biz>
Date:   Mon Jul 20 19:24:48 2015 -0500

    RT#37125: Include discounts in report: customer accounting summary [some fixes]

diff --git a/FS/FS/Report/Table.pm b/FS/FS/Report/Table.pm
index ffa1172..63e5318 100644
--- a/FS/FS/Report/Table.pm
+++ b/FS/FS/Report/Table.pm
@@ -229,7 +229,8 @@ sub receipts { #net payments
   my $sql = 'SELECT SUM(cust_bill_pay.amount) FROM cust_bill_pay';
   if ( $opt{'setuprecur'} ) {
     $sql = 'SELECT SUM('.
-            FS::cust_bill_pkg->paid_sql($speriod, $eperiod, %opt).
+            #in practice, but not appearance, paid_sql accepts end before start
+            FS::cust_bill_pkg->paid_sql($eperiod, $speriod, %opt).
            ') FROM cust_bill_pkg';
   }
 
@@ -275,6 +276,11 @@ sub discounted {
 
   my $sql = 'SELECT SUM(';
   if ($opt{'setuprecur'}) {
+    # (This isn't exact but it works in most cases.)
+    # When splitting into setup/recur values, 
+    # if the discount is allowed to apply to setup fees (discount.setup = 'Y')
+    # then split it between the "setup" and "recurring" rows in proportion to 
+    # the "unitsetup" and "unitrecur" fields of the line item. 
     $sql .= <<EOF;
 CASE
   WHEN discount.setup = 'Y' 
@@ -291,11 +297,15 @@ EOF
     }
     $sql .= ' / (COALESCE(cust_bill_pkg.unitsetup,0) + COALESCE(cust_bill_pkg.unitrecur,0)))';
     $sql .= " * cust_bill_pkg_discount.amount\n";
-    $sql .= <<EOF;
-  ELSE cust_bill_pkg_discount.amount
-END
-EOF
+    # Otherwise, show it all as "recurring"
+    if ($opt{'setuprecur'} eq 'setup') {
+      $sql .= "  ELSE 0\n";
+    } elsif ($opt{'setuprecur'} eq 'recur') {
+      $sql .= "  ELSE cust_bill_pkg_discount.amount\n";
+    }
+    $sql .= "END\n";
   } else {
+    # simple case, no setuprecur
     $sql .= "cust_bill_pkg_discount.amount\n";
   }
   $sql .= <<EOF;
diff --git a/httemplate/search/report_customer_accounting_summary.html b/httemplate/search/report_customer_accounting_summary.html
index bdcdbcc..cd8622e 100755
--- a/httemplate/search/report_customer_accounting_summary.html
+++ b/httemplate/search/report_customer_accounting_summary.html
@@ -38,9 +38,12 @@
     &>
 
     <& /elements/tr-checkbox.html,
-        'label' => 'Show Gross & Discounted',
-        'field' => 'grossdiscount',
-        'value' => 1,
+        'label'      => 'Show Gross & Discounted',
+        'field'      => 'grossdiscount',
+        'value'      => 1,
+        'curr_value' => defined($cgi->param('grossdiscount'))
+                        ? scalar($cgi->param('grossdiscount'))
+                        : 1, #default to on in v4
     &>
     
   </TABLE>

commit 078454a5494f6611eef48f8ad06c1a5a5c719e00
Author: Jonathan Prykop <jonathan at freeside.biz>
Date:   Wed Jul 15 22:07:27 2015 -0500

    RT#37125: Include discounts in report: customer accounting summary

diff --git a/FS/FS/Report/Table.pm b/FS/FS/Report/Table.pm
index 0a0d24a..ffa1172 100644
--- a/FS/FS/Report/Table.pm
+++ b/FS/FS/Report/Table.pm
@@ -272,17 +272,53 @@ sub netrefunds {
 
 sub discounted {
   my( $self, $speriod, $eperiod, $agentnum, %opt) = @_;
-  $self->scalar_sql('SELECT SUM(cust_bill_pkg_discount.amount)
-    FROM cust_bill_pkg_discount
-      JOIN cust_bill_pkg USING  ( billpkgnum )
-      JOIN cust_bill     USING  ( invnum )
-      JOIN cust_main     USING  ( custnum )
-    WHERE '. $self->in_time_period_and_agent( $speriod,
-                                              $eperiod,
-                                              $agentnum,
-                                              'cust_bill._date'
-                                            ).
-              $self->for_opts(%opt)
+
+  my $sql = 'SELECT SUM(';
+  if ($opt{'setuprecur'}) {
+    $sql .= <<EOF;
+CASE
+  WHEN discount.setup = 'Y' 
+    AND ((COALESCE(cust_bill_pkg.unitsetup,0) > 0) 
+          OR (COALESCE(cust_bill_pkg.unitrecur,0) > 0))
+  THEN
+EOF
+    if ($opt{'setuprecur'} eq 'setup') {
+      $sql .= '    (COALESCE(cust_bill_pkg.unitsetup,0)';
+    } elsif ($opt{'setuprecur'} eq 'recur') {
+      $sql .= '    (COALESCE(cust_bill_pkg.unitrecur,0)';
+    } else {
+      die 'Unrecognized value for setuprecur';
+    }
+    $sql .= ' / (COALESCE(cust_bill_pkg.unitsetup,0) + COALESCE(cust_bill_pkg.unitrecur,0)))';
+    $sql .= " * cust_bill_pkg_discount.amount\n";
+    $sql .= <<EOF;
+  ELSE cust_bill_pkg_discount.amount
+END
+EOF
+  } else {
+    $sql .= "cust_bill_pkg_discount.amount\n";
+  }
+  $sql .= <<EOF;
+) FROM cust_bill_pkg_discount
+  JOIN cust_bill_pkg     USING  ( billpkgnum )
+  JOIN cust_bill         USING  ( invnum )
+  JOIN cust_main         USING  ( custnum )
+EOF
+  if ($opt{'setuprecur'}) {
+    $sql .= <<EOF;
+  JOIN cust_pkg_discount USING ( pkgdiscountnum )
+  LEFT JOIN discount          USING ( discountnum )
+EOF
+  }
+  $self->scalar_sql(
+    $sql 
+    . 'WHERE '
+    . $self->in_time_period_and_agent( $speriod,
+                                       $eperiod,
+                                       $agentnum,
+                                       'cust_bill._date'
+                                      )
+    . $self->for_opts(%opt)
   );
 }
 
diff --git a/httemplate/search/customer_accounting_summary.html b/httemplate/search/customer_accounting_summary.html
index 0dab7ce..744b313 100644
--- a/httemplate/search/customer_accounting_summary.html
+++ b/httemplate/search/customer_accounting_summary.html
@@ -141,9 +141,20 @@ $title .= 'Customer Accounting Summary Report';
 
 my @items  = ('netsales', 'cashflow');
 my @params = ( [], [] );
-my $setuprecur = '';
-if ( $cgi->param('setuprecur') ) {
-  $setuprecur = 1;
+my $grossdiscount = $cgi->param('grossdiscount');
+my $setuprecur = $cgi->param('setuprecur');
+if ($setuprecur && $grossdiscount) {
+  #see blocks below for more details on each option
+  @items  = ('gross', 'discounted', 'receipts', 'gross', 'discounted', 'receipts');
+  @params = ( 
+    [ setuprecur => 'setup' ],
+    [ setuprecur => 'setup' ],
+    [ setuprecur => 'setup' ],
+    [ setuprecur => 'recur' ],
+    [ setuprecur => 'recur' ],
+    [ setuprecur => 'recur' ],
+  );
+} elsif ($setuprecur) {
   # instead of 'cashflow' (payments - refunds), use 'receipts'
   # (applied payments), because it's divisible into setup and recur.
   @items = ('netsales', 'receipts', 'netsales', 'receipts');
@@ -153,7 +164,14 @@ if ( $cgi->param('setuprecur') ) {
     [ setuprecur => 'recur' ],
     [ setuprecur => 'recur' ],
   );
+} elsif ($grossdiscount) {
+  # instead of 'netsales' (invoiced - netcredits)
+  # use 'gross' (invoiced + discounted) and 'discounted' (sum of discounts on invoices)
+  @items  = ('gross', 'discounted', 'cashflow');
+  @params = ( [], [], [] );
 }
+
+
 my @labels = ();
 my @cross_params = ();
 
@@ -208,7 +226,7 @@ $cells[0] = [
   { header => 1, rowspan => 2, colspan => ($setuprecur ? 4 : 3) },
   ($setuprecur ? '' : ()),
   map {
-    { header => 1, colspan => 2, value => time2str('%b %Y', $_) },
+    { header => 1, colspan => ($grossdiscount ? 3 : 2), value => time2str('%b %Y', $_) },
     ''
   } @{ $data->{speriod} }
 ];
@@ -218,8 +236,14 @@ $rows[1] = {};
 $cells[1] = [ '',
   ($setuprecur ? '' : ()),
   map { 
-  ( { header => 1, value => mt('Billed') },
-    { header => 1, value => mt('Paid') }
+  ( ($grossdiscount 
+      ? (
+         { header => 1, value => mt('Gross') },
+         { header => 1, value => mt('Discount') }
+      )
+      : { header => 1, value => mt('Billed') } 
+    ),
+    { header => 1, value => mt('Paid') },
   ) } (1..$ncols)
 ];
 
@@ -256,12 +280,12 @@ foreach my $cust_main (@cust_main) { # correspond to cross_params
           header => 1 };
     }
     for my $col (0..$ncols-1) { # the month
-      for my $subcol (0..1) { # the billed/paid axis
-        my $item = $subrow * 2 + $subcol;
+      for my $subcol (0..($grossdiscount ? 2 : 1)) { # the billed/paid or gross/discount/paid axis
+        my $item = $subrow * ($grossdiscount ? 3 : 2) + $subcol;
         my $value = $data->{data}[$item][$col][$row];
         $skip = 0 if abs($value) > 0.005;
         push @thisrow, { value => sprintf('%0.2f', $value), format => 'money' };
-        $total[( ($ncols * $subrow) + $col ) * 2 + $subcol] += $value;
+        $total[( ($ncols * $subrow) + $col ) * ($grossdiscount ? 3 : 2) + $subcol] += $value;
       } #subcol
     } #col
     push @cells, \@thisrow;
@@ -294,8 +318,8 @@ for my $subrow (0..($setuprecur ? 1 : 0)) {
       { value => $subrow ? mt('recurring') : mt('setup'),
         header => 1 };
   }
-  for my $col (0..($ncols * 2)-1) { # month and billed/paid axis
-    my $value = $total[($subrow * $ncols * 2) + $col];
+  for my $col (0..($ncols * ($grossdiscount ? 3 : 2))-1) { # month and billed/paid or gross/discount/paid axis
+    my $value = $total[($subrow * $ncols * ($grossdiscount ? 3 : 2)) + $col];
     push @thisrow, { value => sprintf('%0.2f', $value), format => 'money' };
   }
   push @cells, \@thisrow;
diff --git a/httemplate/search/report_customer_accounting_summary.html b/httemplate/search/report_customer_accounting_summary.html
index 8206f34..bdcdbcc 100755
--- a/httemplate/search/report_customer_accounting_summary.html
+++ b/httemplate/search/report_customer_accounting_summary.html
@@ -37,6 +37,11 @@
         'value' => 1,
     &>
 
+    <& /elements/tr-checkbox.html,
+        'label' => 'Show Gross & Discounted',
+        'field' => 'grossdiscount',
+        'value' => 1,
+    &>
     
   </TABLE>
 

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Report/Table.pm                              |   70 ++++++++++++++++----
 httemplate/search/customer_accounting_summary.html |   46 ++++++++++---
 .../search/report_customer_accounting_summary.html |    8 +++
 3 files changed, 101 insertions(+), 23 deletions(-)




More information about the freeside-commits mailing list