[freeside-commits] branch FREESIDE_3_BRANCH updated. 1a49f525882c638ef2df5b9732d2504f18c14da7

Mitch Jackson mitch at freeside.biz
Mon Apr 16 13:12:21 PDT 2018


The branch, FREESIDE_3_BRANCH has been updated
       via  1a49f525882c638ef2df5b9732d2504f18c14da7 (commit)
       via  f1dc2a40887b13ca52169ef2812795d9d686a1fd (commit)
       via  f73668e8c904fd91881c583a80b206851b7d5e83 (commit)
      from  052b108c68cc7630d6cd5c61a3f7dbc4c824abbb (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 1a49f525882c638ef2df5b9732d2504f18c14da7
Author: Mitch Jackson <mitch at freeside.biz>
Date:   Sun Apr 15 22:48:58 2018 -0500

    RT# 79353 Update discount graph - include waived setup fees

diff --git a/FS/FS/Report/Table.pm b/FS/FS/Report/Table.pm
index 0c4d9bfa6..cef7813af 100644
--- a/FS/FS/Report/Table.pm
+++ b/FS/FS/Report/Table.pm
@@ -745,6 +745,12 @@ sub cust_bill_pkg_detail {
   
 }
 
+=item cust_bill_pkg_discount: Discounts issued
+
+Arguments: agentnum, refnum, cust_classnum
+
+=cut
+
 sub cust_bill_pkg_discount {
   my $self = shift;
   my ($speriod, $eperiod, $agentnum, %opt) = @_;
@@ -770,6 +776,60 @@ sub cust_bill_pkg_discount {
   $self->scalar_sql($total_sql);
 }
 
+=item cust_bill_pkg_discount_or_waived: Discounts and waived fees issued
+
+Arguments: agentnum, refnum, cust_classnum
+
+=cut
+
+sub cust_bill_pkg_discount_or_waived {
+
+  my $self = shift;
+  my ($speriod, $eperiod, $agentnum, %opt) = @_;
+
+  $agentnum ||= $opt{'agentnum'};
+
+  my $total_sql = "
+    SELECT
+      COALESCE(
+          SUM(
+            COALESCE(
+              cust_bill_pkg_discount.amount,
+              CAST((  SELECT optionvalue
+                 FROM part_pkg_option
+                 WHERE
+                    part_pkg_option.pkgpart = cust_pkg.pkgpart
+                    AND optionname = 'setup_fee'
+              ) AS NUMERIC )
+            )
+          ),
+          0
+       )
+    FROM cust_bill_pkg
+    LEFT JOIN cust_bill_pkg_discount USING (billpkgnum)
+    LEFT JOIN cust_pkg ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum
+    LEFT JOIN part_pkg USING (pkgpart)
+    LEFT JOIN cust_bill USING ( invnum )
+    LEFT JOIN cust_main ON cust_pkg.custnum = cust_main.custnum
+    WHERE
+    (
+        cust_bill_pkg_discount.billpkgdiscountnum IS NOT NULL
+        OR (
+            cust_pkg.setup = cust_bill_pkg.sdate
+            AND cust_pkg.waive_setup = 'Y'
+        )
+    )
+    AND cust_bill_pkg.pkgpart_override IS NULL
+  " . join "\n",
+      map  { " AND ( $_ ) " }
+      grep { $_ }
+      $self->with_classnum($opt{'classnum'}, $opt{'use_override'}),
+      $self->with_report_option(%opt),
+      $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
+
+  $self->scalar_sql($total_sql);
+}
+
 sub cust_bill_pkg_taxes {
   my $self = shift;
   my ($speriod, $eperiod, $agentnum, %opt) = @_;
diff --git a/httemplate/graph/cust_bill_pkg_discount.html b/httemplate/graph/cust_bill_pkg_discount.html
index 5074aa35c..22327e05b 100644
--- a/httemplate/graph/cust_bill_pkg_discount.html
+++ b/httemplate/graph/cust_bill_pkg_discount.html
@@ -20,7 +20,10 @@
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
 
+my $include_waived_setup = $cgi->param('include_waived_setup') || 0;
+
 my $link = "${p}search/cust_bill_pkg_discount.html?";
+$link .= "include_waived_setup=Y&" if $include_waived_setup;
 my $bottom_link = $link;
 
 #XXX or virtual
@@ -35,7 +38,8 @@ my $title = $sel_agent ? $sel_agent->agent.' ' : '';
 $title .= 'Discount Overview';
 
 
-my $hue = 0;
+#my $hue = 0; # Start with illegible yellow-on-white
+my $hue = 255; # Start with red-on-white
 #my $hue_increment = 170;
 #my $hue_increment = 145;
 my $hue_increment = 125;
@@ -56,7 +60,10 @@ foreach my $agent ( $sel_agent || qsearch('agent', { 'disabled' => '' } ) ) {
 
   #foreach my $pkg_class ( @pkg_class ) {
 
-      push @items, 'cust_bill_pkg_discount';
+      push @items,
+        $include_waived_setup
+        ? 'cust_bill_pkg_discount_or_waived'
+        : 'cust_bill_pkg_discount';
 
       push @labels,
         ( $sel_agent ? '' : $agent->agent.' ' );
diff --git a/httemplate/graph/report_cust_bill_pkg_discount.html b/httemplate/graph/report_cust_bill_pkg_discount.html
index 36ad78252..094c652c7 100644
--- a/httemplate/graph/report_cust_bill_pkg_discount.html
+++ b/httemplate/graph/report_cust_bill_pkg_discount.html
@@ -19,6 +19,12 @@
        'disable_empty' => 0,
   &>
 
+  <& /elements/tr-checkbox.html,
+     label => 'Include waived setup fees:',
+     field => 'include_waived_setup',
+     value => 'Y',
+  &>
+
 % # anything about line items, discounts or packages really
 % # otaker?
 % # package class?
diff --git a/httemplate/search/cust_bill_pkg_discount.html b/httemplate/search/cust_bill_pkg_discount.html
index a669e6c33..691a11e60 100644
--- a/httemplate/search/cust_bill_pkg_discount.html
+++ b/httemplate/search/cust_bill_pkg_discount.html
@@ -224,7 +224,11 @@ if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
 }
 
 # Filter: Include waived setup fees
-if ( !$cgi->param('include_waived_setup') ) {
+if ( $cgi->param('include_waived_setup') ) {
+  # Filter a hidden fee attached to a package with a waived setup fee from
+  # causing the waived-fee for that package to be double-counted
+  push @where, 'cust_bill_pkg.pkgpart_override IS NULL';
+} else {
   push @where, "cust_bill_pkg_discount.pkgdiscountnum IS NOT NULL";
 }
 

commit f1dc2a40887b13ca52169ef2812795d9d686a1fd
Author: Mitch Jackson <mitch at freeside.biz>
Date:   Mon Apr 16 19:37:05 2018 +0000

    RT# 79353 Update discount report - include waived setup fees

diff --git a/httemplate/search/cust_bill_pkg_discount.html b/httemplate/search/cust_bill_pkg_discount.html
index d0f9bcf7a..a669e6c33 100644
--- a/httemplate/search/cust_bill_pkg_discount.html
+++ b/httemplate/search/cust_bill_pkg_discount.html
@@ -38,7 +38,7 @@ Parameters:
         if ( $_[0]->pkgdiscountnum ) {
             # Standard discount, not a waived setup fee
             my $discount = qsearchs('discount',{
-                pkgdiscountnum => $_[0]->pkgdiscountnum
+                discountnum => $_[0]->discountnum
             });
             return $discount->description;
         } else {
@@ -71,9 +71,7 @@ Parameters:
     sub {
       my $row = shift;
       if ($row->pkgdiscountnum) {
-        $row->setuprecur eq 'setup'
-          ? 'Setup'
-          : sprintf( '%.2f', $row->months ),
+        return sprintf( '%.2f', $row->months );
       } else {
         return 'Setup';
       }
@@ -144,7 +142,6 @@ my @select = (qw(
 
   cust_main.custnum
 
-  cust_pkg_discount.setuprecur
   cust_pkg_discount.discountnum
   cust_pkg.waive_setup),
 

commit f73668e8c904fd91881c583a80b206851b7d5e83
Author: Mitch Jackson <mitch at freeside.biz>
Date:   Thu Feb 22 14:07:05 2018 -0600

    RT# 79353 Update discount report - include waived setup fees

diff --git a/httemplate/search/cust_bill_pkg_discount.html b/httemplate/search/cust_bill_pkg_discount.html
index 6da5787a0..d0f9bcf7a 100644
--- a/httemplate/search/cust_bill_pkg_discount.html
+++ b/httemplate/search/cust_bill_pkg_discount.html
@@ -1,112 +1,237 @@
+<%doc>
+
+Display the Discount Detail Report:
+
+Parameters:
+
+ - discount_classnum
+ - usernum
+ - agentnum
+ - beginning
+ - ending
+ - include_waived_setup
+
+</%doc>
 <& elements/search.html,
-                 'title'       => 'Discounts',
-                 'name'        => 'discounts',
-                 'query'       => $query,
-                 'count_query' => $count_query,
-                 'count_addl'  => [ $money_char. '%.2f total', ],
-                 'header'      => [
-                   #'#',
-                   'Discount',
-                   'Class',
-                   'Amount',
-                   'Months',
-                   'Package',
-                   'Invoice',
-                   'Date',
-                   FS::UI::Web::cust_header(),
-                 ],
-                 'fields'      => [
-                   #'billpkgdiscountnum',
-                   sub { $_[0]->cust_pkg_discount->discount->description },
-                   sub { $_[0]->cust_pkg_discount->discount->classname },
-                   sub { sprintf($money_char.'%.2f', shift->amount ) },
-                   sub { my $m = shift->months;
-                         $m =~ /\./ ? sprintf('%.2f', $m) : $m;
-                       },
-                   'pkg',#sub { $_[0]->cust_bill_pkg->cust_pkg->part_pkg->pkg },
-                   'invnum',
-                   sub { time2str('%b %d %Y', shift->_date ) },
-                   \&FS::UI::Web::cust_fields,
-                 ],
-                 'sort_fields' => [
-                   '',
-                   '',
-                   'amount',
-                   'months',
-                   'pkg',
-                   'invnum',
-                   '_date',
-                 ],
-                 'links'       => [
-                   #'',
-                   '', #link to customer discount???
-                   '',
-                   '',
-                   '',
-                   '',
-                   $ilink,
-                   $ilink,
-                   ( map { $_ ne 'Cust. Status' ? $clink : '' }
-                         FS::UI::Web::cust_header()
-                   ),
-                 ],
-                 #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
-                 'align' => 'lcrrlrr'.FS::UI::Web::cust_aligns(),
-                 'color' => [ 
-                              #'',
-                              '',
-                              '',
-                              '',
-                              '',
-                              '',
-                              '',
-                              '',
-                              FS::UI::Web::cust_colors(),
-                            ],
-                 'style' => [ 
-                              #'',
-                              '',
-                              '',
-                              '',
-                              '',
-                              '',
-                              '',
-                              '',
-                              FS::UI::Web::cust_styles(),
-                            ],
-           
+  title       => 'Discounts',
+  name        => 'discounts',
+  query       => $query,
+  count_query => $count_query,
+  count_addl  => [ $money_char . '%.2f total' ],
+
+  header => [
+    qw(
+      Discount
+      Class
+      Amount
+      Months
+      Package
+      Invoice
+      Date
+    ),
+    FS::UI::Web::cust_header(),
+  ],
+
+  fields => [
+    # Discount
+    sub {
+        if ( $_[0]->pkgdiscountnum ) {
+            # Standard discount, not a waived setup fee
+            my $discount = qsearchs('discount',{
+                pkgdiscountnum => $_[0]->pkgdiscountnum
+            });
+            return $discount->description;
+        } else {
+            return 'Waive setup fee';
+        }
+    },
+
+    # Class
+    sub {
+        if ( $_[0]->discountnum ) {
+            # Standard discount, not a waived setup fee
+            my $discount = qsearchs('discount',{
+                discountnum => $_[0]->discountnum
+            });
+            return $discount->classname;
+        } else {
+            return 'n/a';
+        }
+    },
+
+    # Amount
+    sub {
+      sprintf(
+        $money_char.'%.2f',
+        $_[0]->amount ? $_[0]->amount : $_[0]->setup_fee
+      )
+    },
+
+    # Months
+    sub {
+      my $row = shift;
+      if ($row->pkgdiscountnum) {
+        $row->setuprecur eq 'setup'
+          ? 'Setup'
+          : sprintf( '%.2f', $row->months ),
+      } else {
+        return 'Setup';
+      }
+    },
+
+    # Package
+    'pkg',
+
+    # Invoice
+    'invnum',
+
+    # Date
+    sub { time2str('%b %d %Y', $_[0]->_date) },
+
+    # Customer
+    \&FS::UI::Web::cust_fields,
+  ],
+
+  sort_fields => [
+    '',       # Discount
+    '',       # Class
+    'amount', # Amount
+    'months', # Months
+    'pkg',    # Package
+    'invnum', # Invoice
+    '_date',  # Date
+  ],
+
+  links => [
+    '', # Discount
+    '', # Class
+    '', # Amount
+    '', # Months
+    '', # Package
+    [ "${p}view/cust_bill.cgi?", 'invnum' ], # Invoice
+    [ "${p}view/cust_bill.cgi?", 'invnum' ], # Date
+    (   # Customer
+      map {
+        $_ ne 'Cust. Status'
+          ? [ "${p}view/cust_main.cgi?", 'custnum' ]
+          : ''
+      } FS::UI::Web::cust_header()
+    ),
+  ],
+
+  align => 'lcrrlrr'.FS::UI::Web::cust_aligns(),
+  color => [ '','','','','','','',FS::UI::Web::cust_colors() ],
+  style => [ '','','','','','','',FS::UI::Web::cust_styles() ],
 &>
 <%init>
 
-#a little false laziness below w/cust_bill_pkg.cgi
-
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
 
 my $conf = new FS::Conf;
 
-#here is the agent virtualization
-my $agentnums_sql =
-  $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
+# Query building blocks
+my @select = (qw(
+  cust_bill_pkg_discount.billpkgdiscountnum
+  cust_bill_pkg_discount.pkgdiscountnum
+  cust_bill_pkg_discount.amount
+  cust_bill_pkg_discount.months
+
+  cust_bill.invnum
+  cust_bill._date
+
+  part_pkg.pkg
+
+  cust_main.custnum
+
+  cust_pkg_discount.setuprecur
+  cust_pkg_discount.discountnum
+  cust_pkg.waive_setup),
+
+  "
+  (  SELECT optionvalue
+     FROM part_pkg_option
+     WHERE
+        part_pkg_option.pkgpart = cust_pkg.pkgpart
+        AND optionname = 'setup_fee'
+  ) as setup_fee  ",
+
+  "
+  COALESCE(
+      CAST(cust_bill_pkg_discount.amount AS TEXT),
+      (  SELECT optionvalue
+         FROM part_pkg_option
+         WHERE
+            part_pkg_option.pkgpart = cust_pkg.pkgpart
+            AND optionname = 'setup_fee'
+      )
+  ) as discountorwaive
+  ",
+
+  FS::UI::Web::cust_sql_fields(),
+);
 
-my @where = ( $agentnums_sql );
+my $join = "
+LEFT JOIN cust_bill_pkg_discount
+  USING (billpkgnum)
 
+LEFT JOIN cust_pkg_discount
+  ON cust_bill_pkg_discount.pkgdiscountnum = cust_pkg_discount.pkgdiscountnum
+
+LEFT JOIN cust_pkg
+  ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum
+
+LEFT JOIN part_pkg
+  USING (pkgpart)
+
+LEFT JOIN cust_bill USING (invnum)
+
+LEFT JOIN cust_main
+  ON cust_bill.custnum = cust_main.custnum
+
+";
+
+# Each entry in @where will be joined with AND
+my @where = (
+
+  # Agent Virtualization
+  $FS::CurrentUser::CurrentUser->agentnums_sql(table => 'cust_main'),
+
+  # Select only rows where there is a discount or waived setup fee
+  "(
+    cust_bill_pkg_discount.billpkgdiscountnum IS NOT NULL
+    OR (
+      cust_pkg.setup = cust_bill_pkg.sdate
+      AND cust_pkg.waive_setup = 'Y'
+    )
+  )",
+
+);
+
+
+# Filter: Date Range
 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
-push @where, "_date >= $beginning",
-             "_date <= $ending";
+push @where, "_date >= $beginning", "_date <= $ending";
+
 
+# Filter: Agent
 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.agentnum = $1";
 }
 
-#usernum
+
+# Filter: User
 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
-  push @where, "cust_pkg_discount.usernum = $1";
+  push @where,
+    "( cust_pkg_discount.usernum = $1 OR cust_pkg_discount.usernum IS NULL )";
+}
+
+# Filter: Include waived setup fees
+if ( !$cgi->param('include_waived_setup') ) {
+  push @where, "cust_bill_pkg_discount.pkgdiscountnum IS NOT NULL";
 }
 
-# (discount) classnum
-my $join_discount = '';
-#false laziness w/cust_pkg_discount.html and cust_pkg.pm::search
+# Filter: Discount Class
 if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
 
 #  my @classnum = ();
@@ -130,7 +255,7 @@ if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
    if ( $cgi->param('discount_classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
     my @classnum = ( $1 );
 
-    $join_discount = 'LEFT JOIN discount USING (discountnum)';
+    $join .= ' LEFT JOIN discount USING (discountnum) ';
 
     my @c_where = ();
     my @nums = grep $_, @classnum;
@@ -171,40 +296,32 @@ if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
 #   }
 # }
 
-my $count_query = "SELECT COUNT(*), SUM(cust_bill_pkg_discount.amount)";
-
-my $join_cust_pkg_discount =
-  'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
-
-my $join_cust =
-  '         JOIN cust_bill USING ( invnum ) '.
-  FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
-
-my $join_pkg =
-  '    JOIN cust_bill_pkg USING ( billpkgnum )
-  LEFT JOIN cust_pkg ON ( cust_bill_pkg.pkgnum = cust_pkg.pkgnum )
-  LEFT JOIN part_pkg USING ( pkgpart ) ';
-  #LEFT JOIN part_pkg AS override
-  #  ON pkgpart_override = override.pkgpart ';
-
-my $join = "$join_cust_pkg_discount $join_discount $join_pkg $join_cust";
+my $count_query;
+if ( $cgi->param('include_waived_setup') ) {
+  $count_query = "
+    SELECT
+      COUNT(*),
+      SUM(
+        COALESCE(
+          cust_bill_pkg_discount.amount,
+          CAST((  SELECT optionvalue
+             FROM part_pkg_option
+             WHERE
+                part_pkg_option.pkgpart = cust_pkg.pkgpart
+                AND optionname = 'setup_fee'
+          ) AS NUMERIC )
+        )
+      ) ";
+} else {
+  $count_query = "SELECT COUNT(*), SUM(cust_bill_pkg_discount.amount)";
+}
 
 my $where = ' WHERE '. join(' AND ', @where);
 
-$count_query .= " FROM cust_bill_pkg_discount $join $where";
-
-my @select = (
-               'cust_bill_pkg_discount.*',
-               #'cust_bill_pkg.*',
-               'cust_bill.invnum',
-               'cust_bill._date',
-             );
-push @select, 'part_pkg.pkg';
-push @select, 'cust_main.custnum',
-              FS::UI::Web::cust_sql_fields();
+$count_query .= " FROM cust_bill_pkg $join $where";
 
 my $query = {
-  'table'     => 'cust_bill_pkg_discount',
+  'table'     => 'cust_bill_pkg',
   'addl_from' => $join,
   'hashref'   => {},
   'select'    => join(', ', @select ),
@@ -212,10 +329,6 @@ my $query = {
   'order_by'  => 'ORDER BY _date, billpkgdiscountnum',
 };
 
-my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
-my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
-
-my $conf = new FS::Conf;
 my $money_char = $conf->config('money_char') || '$';
 
 </%init>
diff --git a/httemplate/search/report_cust_bill_pkg_discount.html b/httemplate/search/report_cust_bill_pkg_discount.html
index 78c5e4f16..9c935b723 100644
--- a/httemplate/search/report_cust_bill_pkg_discount.html
+++ b/httemplate/search/report_cust_bill_pkg_discount.html
@@ -24,6 +24,12 @@
 
   <& /elements/tr-input-beginning_ending.html &>
 
+  <& /elements/tr-checkbox.html,
+        label => 'Include waived setup fees:',
+        field => 'include_waived_setup',
+        value => 'Y',
+  &>
+
 <!-- doesn't actually work yet, needs support in cust_bill_pkg_discount.html
   <& /elements/tr-input-lessthan_greaterthan.html,
        'label' => 'Amount',

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

Summary of changes:
 FS/FS/Report/Table.pm                              |  60 ++++
 httemplate/graph/cust_bill_pkg_discount.html       |  11 +-
 .../graph/report_cust_bill_pkg_discount.html       |   6 +
 httemplate/search/cust_bill_pkg_discount.html      | 360 ++++++++++++++-------
 .../search/report_cust_bill_pkg_discount.html      |   6 +
 5 files changed, 318 insertions(+), 125 deletions(-)




More information about the freeside-commits mailing list