fix unapplied payment report, RT#73048, fallout from #25944
[freeside.git] / httemplate / search / cust_bill_pkg_discount.html
index f598341..c634828 100644 (file)
@@ -7,6 +7,7 @@
                  'header'      => [
                    #'#',
                    'Discount',
+                   'Class',
                    'Amount',
                    'Months',
                    'Package',
                  '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;
-                       },
+                   $months_sub,
                    'pkg',#sub { $_[0]->cust_bill_pkg->cust_pkg->part_pkg->pkg },
                    'invnum',
                    sub { time2str('%b %d %Y', shift->_date ) },
@@ -28,6 +28,7 @@
                  ],
                  'sort_fields' => [
                    '',
+                   '',
                    'amount',
                    'months',
                    'pkg',
@@ -40,6 +41,7 @@
                    '',
                    '',
                    '',
+                   '',
                    $ilink,
                    $ilink,
                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
@@ -47,7 +49,7 @@
                    ),
                  ],
                  #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
-                 'align' => 'lrrlrr'.FS::UI::Web::cust_aligns(),
+                 'align' => 'lcrrlrr'.FS::UI::Web::cust_aligns(),
                  'color' => [ 
                               #'',
                               '',
@@ -56,6 +58,7 @@
                               '',
                               '',
                               '',
+                              '',
                               FS::UI::Web::cust_colors(),
                             ],
                  'style' => [ 
@@ -66,6 +69,7 @@
                               '',
                               '',
                               '',
+                              '',
                               FS::UI::Web::cust_styles(),
                             ],
            
@@ -98,7 +102,51 @@ if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
   push @where, "cust_pkg_discount.usernum = $1";
 }
 
-# #classnum
+# (discount) classnum
+my $join_discount = '';
+#false laziness w/cust_pkg_discount.html and cust_pkg.pm::search
+if ( grep { $_ eq 'discount_classnum' } $cgi->param ) {
+
+#  my @classnum = ();
+#  if ( ref($params->{'discount_classnum'}) ) {
+#
+#    if ( ref($params->{'discount_classnum'}) eq 'HASH' ) {
+#      @classnum = grep $params->{'discount_classnum'}{$_}, keys %{ $params->{'discount_classnum'} };
+#    } elsif ( ref($params->{'discount_classnum'}) eq 'ARRAY' ) {
+#      @classnum = @{ $params->{'discount_classnum'} };
+#    } else {
+#      die 'unhandled discount_classnum ref '. $params->{'discount_classnum'};
+#    }
+#
+#
+#  } elsif ( $params->{'discount_classnum'} =~ /^(\d*)$/ && $1 ne '0' ) {
+#    @classnum = ( $1 );
+#  }
+#
+#  if ( @classnum ) {
+
+   if ( $cgi->param('discount_classnum') =~ /^(\d*)$/ && $1 ne '0' ) {
+    my @classnum = ( $1 );
+
+    $join_discount = 'LEFT JOIN discount USING (discountnum)';
+
+    my @c_where = ();
+    my @nums = grep $_, @classnum;
+    push @c_where, 'discount.classnum IN ('. join(',',@nums). ')' if @nums;
+    my $null = scalar( grep { $_ eq '' } @classnum );
+    push @c_where, 'discount.classnum IS NULL' if $null;
+
+    if ( scalar(@c_where) == 1 ) {
+      push @where, @c_where;
+    } elsif ( @c_where ) {
+      push @where, ' ( '. join(' OR ', @c_where). ' ) ';
+    }
+
+  }
+
+}
+
+# #(package) classnum
 # # not specified: all classes
 # # 0: empty class
 # # N: classnum
@@ -121,7 +169,7 @@ if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
 #   }
 # }
 
-my $count_query = "SELECT COUNT(*), SUM(amount)";
+my $count_query = "SELECT COUNT(*), SUM(cust_bill_pkg_discount.amount)";
 
 my $join_cust_pkg_discount =
   'LEFT JOIN cust_pkg_discount USING (pkgdiscountnum)';
@@ -137,11 +185,11 @@ my $join_pkg =
   #LEFT JOIN part_pkg AS override
   #  ON pkgpart_override = override.pkgpart ';
 
+my $join = "$join_cust_pkg_discount $join_discount $join_pkg $join_cust";
+
 my $where = ' WHERE '. join(' AND ', @where);
 
-$count_query .=
-  " FROM cust_bill_pkg_discount $join_cust_pkg_discount $join_pkg $join_cust ".
-  $where;
+$count_query .= " FROM cust_bill_pkg_discount $join $where";
 
 my @select = (
                'cust_bill_pkg_discount.*',
@@ -155,7 +203,7 @@ push @select, 'cust_main.custnum',
 
 my $query = {
   'table'     => 'cust_bill_pkg_discount',
-  'addl_from' => "$join_cust_pkg_discount $join_pkg $join_cust",
+  'addl_from' => $join,
   'hashref'   => {},
   'select'    => join(', ', @select ),
   'extra_sql' => $where,
@@ -168,4 +216,11 @@ my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
 my $conf = new FS::Conf;
 my $money_char = $conf->config('money_char') || '$';
 
+my $months_sub = sub {
+  my $cust_bill_pkg_discount = shift;
+  return 'Setup'
+    if $cust_bill_pkg_discount->cust_pkg_discount->setuprecur eq 'setup';
+  sprintf('%.2f', $cust_bill_pkg_discount->months);
+};
+
 </%init>