add taxclass and paid/credit columns to line item reports, clickthrough to credit...
authorIvan Kohler <ivan@freeside.biz>
Tue, 18 Dec 2012 00:09:09 +0000 (16:09 -0800)
committerIvan Kohler <ivan@freeside.biz>
Tue, 18 Dec 2012 00:09:09 +0000 (16:09 -0800)
httemplate/search/cust_bill_pkg.cgi
httemplate/search/cust_credit_bill_pkg.html

index 0c5fbd2..34c89e5 100644 (file)
@@ -6,9 +6,12 @@
                  'count_addl'  => \@total_desc,
                  'header'      => [
                    emt('Description'),
+                   @post_desc_header,
                    @peritem_desc,
                    emt('Invoice'),
                    emt('Date'),
+                   emt('Paid'),
+                   emt('Credited'),
                    FS::UI::Web::cust_header(),
                  ],
                  'fields'      => [
                            ? $_[0]->get('pkg')      # possibly use override.pkg
                            : $_[0]->get('itemdesc') # but i think this correct
                        },
+                   @post_desc,
                    #strikethrough or "N/A ($amount)" or something these when
                    # they're not applicable to pkg_tax search
                    @peritem_sub,
                    'invnum',
                    sub { time2str('%b %d %Y', shift->_date ) },
+                   sub { sprintf($money_char.'%.2f', shift->get('pay_amount')) },
+                   sub { sprintf($money_char.'%.2f', shift->get('credit_amount')) },
                    \&FS::UI::Web::cust_fields,
                  ],
                  'sort_fields' => [
                    '',
+                   @post_desc_null,
                    @peritem,
                    'invnum',
                    '_date',
+                   #'pay_amount',
+                   #'credit_amount',
                  ],
                  'links'       => [
                    #'',
                    '',
+                   @post_desc_null,
                    @peritem_null,
                    $ilink,
                    $ilink,
+                   $pay_link,
+                   $credit_link,
                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
                          FS::UI::Web::cust_header()
                    ),
                  ],
                  #'align' => 'rlrrrc'.FS::UI::Web::cust_aligns(),
                  'align' => 'l'.
+                            $post_desc_align.
                             $peritem_align.
-                            'rc'.
+                            'rcrr'.
                             FS::UI::Web::cust_aligns(),
                  'color' => [ 
                               #'',
                               '',
+                              @post_desc_null,
                               @peritem_null,
                               '',
                               '',
+                              '',
+                              '',
                               FS::UI::Web::cust_colors(),
                             ],
                  'style' => [ 
                               #'',
                               '',
+                              @post_desc_null,
                               @peritem_null,
                               '',
                               '',
+                              '',
+                              '',
                               FS::UI::Web::cust_styles(),
                             ],
 &>
 <%doc>
 
-Output parameters:
+Output control parameters:
 - distribute: Boolean.  If true, recurring fees will be "prorated" for the 
   portion of the package date range (sdate-edate) that falls within the date
   range of the report.  Line items will be limited to those for which this 
   portion is > 0.  This disables filtering on invoice date.
 
-- use_usage: Separate usage (cust_bill_pkg_detail records) from
+- usage: Separate usage (cust_bill_pkg_detail records) from
   recurring charges.  If set to "usage", will show usage instead of 
   recurring charges.  If set to "recurring", will deduct usage and only
   show the flat rate charge.  If not passed, the "recurring charge" column
@@ -155,13 +174,24 @@ my $money_char = $conf->config('money_char') || '$';
 my @select = ( 'cust_bill_pkg.*', 'cust_bill._date' );
 my @total = ( 'COUNT(*)', 'SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)');
 my @total_desc = ( '%d line items', $money_char.'%.2f total' ); # sprintf strings
+
 my @peritem = ( 'setup', 'recur' );
 my @peritem_desc = ( 'Setup charge', 'Recurring charge' );
-my ($join_cust, $join_pkg ) = ('', '');
-my $use_usage;
+
+my @post_desc_header = ();
+my @post_desc = ();
+my @post_desc_null = ();
+my $post_desc_align = '';
+if ( $conf->exists('enable_taxclasses') ) {
+  push @post_desc_header, 'Tax class';
+  push @post_desc, 'taxclass';
+  push @post_desc_null, '';
+  $post_desc_align .= 'l';
+  push @select, 'part_pkg.taxclass'; # or should this use override?
+}
 
 # valid in both the tax and non-tax cases
-$join_cust = 
+my $join_cust = 
   " LEFT JOIN cust_bill USING (invnum)
     LEFT JOIN cust_main USING (custnum)
   ";
@@ -205,8 +235,8 @@ if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.custnum = $1";
 }
 
-# then we want the package and its definition
-$join_pkg = 
+# we want the package and its definition if available
+my $join_pkg = 
 ' LEFT JOIN cust_pkg      USING (pkgnum) 
   LEFT JOIN part_pkg      USING (pkgpart)';
 
@@ -379,8 +409,7 @@ if ( $cgi->param('nottax') ) {
   }
 
   # recur/usage separation
-  $use_usage = $cgi->param('usage');
-  if ( $use_usage eq 'recurring' ) {
+  if ( $cgi->param('usage') eq 'recurring' ) {
 
     my $recur_no_usage = FS::cust_bill_pkg->charged_sql('', '', no_usage => 1);
     push @select, "($recur_no_usage) AS recur_no_usage";
@@ -388,7 +417,7 @@ if ( $cgi->param('nottax') ) {
     $total[1] = "SUM(cust_bill_pkg.setup + $recur_no_usage)";
     $total_desc[1] .= ' (excluding usage)';
 
-  } elsif ( $use_usage eq 'usage' ) {
+  } elsif ( $cgi->param('usage') eq 'usage' ) {
 
     my $usage = FS::cust_bill_pkg->usage_sql();
     push @select, "($usage) AS _usage";
@@ -491,6 +520,16 @@ if ( $cgi->param('nottax') ) {
 
 } # nottax / istax
 
+
+#total payments
+my $pay_sub = "SELECT SUM(cust_bill_pay_pkg.amount) AS pay_amount,
+    billpkgnum
+  FROM cust_bill_pay_pkg
+  GROUP BY billpkgnum";
+$join_pkg .= " LEFT JOIN ($pay_sub) AS item_pay USING (billpkgnum)";
+push @select, 'item_pay.pay_amount';
+
+
 # credit
 if ( $cgi->param('credit') ) {
 
@@ -549,7 +588,20 @@ if ( $cgi->param('credit') ) {
   push @peritem_desc, 'Credited', 'By', 'Reason';
   push @total,    'SUM(credit_amount)';
   push @total_desc, "$money_char%.2f credited";
-} # if credit
+
+} else {
+
+  #still want a credit total column
+
+  my $credit_sub = "SELECT SUM(cust_credit_bill_pkg.amount) AS credit_amount,
+      billpkgnum
+    FROM cust_credit_bill_pkg
+    GROUP BY billpkgnum";
+  $join_pkg .= " LEFT JOIN ($credit_sub) AS item_credit USING (billpkgnum)";
+
+  push @select,   'item_credit.credit_amount';
+
+}
 
 push @select, 'cust_main.custnum', FS::UI::Web::cust_sql_fields();
 
@@ -587,6 +639,9 @@ my $peritem_align = 'r' x scalar(@peritem);
 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
 
+my $pay_link    = ''; #[, 'billpkgnum', ];
+my $credit_link = [ "${p}search/cust_credit_bill_pkg.html?billpkgnum=", 'billpkgnum', ];
+
 warn "\n\nQUERY:\n".Dumper($query)."\n\nCOUNT_QUERY:\n$count_query\n\n"
   if $cgi->param('debug');
 </%init>
index 4612118..a57a5ae 100644 (file)
@@ -1,10 +1,10 @@
 <% include( 'elements/search.html',
-              'title'       => 'Tax credits', #well, actually application of
-              'name'        => 'tax credits', # credit to line item
-              'query'       => $query,
-              'count_query' => $count_query,
-                 'count_addl'  => [ $money_char. '%.2f total', ],
-                 'header'      => [
+              'title'         => 'Credit application detail', #to line item
+              'name_singular' => 'credit application',
+              'query'         => $query,
+              'count_query'   => $count_query,
+               'count_addl'   => [ $money_char. '%.2f total', ],
+               'header'       => [
                    #'#',
 
                    'Amount',
@@ -21,8 +21,8 @@
                    'Invoice',
                    'Date',
                    FS::UI::Web::cust_header(),
-                 ],
-                 'fields'      => [
+               ],
+               'fields'       => [
                    #'creditbillpkgnum',
                    sub { sprintf($money_char.'%.2f', shift->amount ) },
 
@@ -37,8 +37,8 @@
                    'invnum',
                    sub { time2str('%b %d %Y', shift->_date ) },
                    \&FS::UI::Web::cust_fields,
-                 ],
-                 'sort_fields' => [
+               ],
+               'sort_fields'  => [
                    'amount',
                    'cust_credit_date',
                    '', #'otaker',
@@ -47,8 +47,8 @@
                    'invnum',
                    '_date',
                    #cust fields
-                 ],
-                 'links'       => [
+               ],
+               'links' => [
                    '',
                    '',
                    '',
@@ -59,9 +59,9 @@
                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
                          FS::UI::Web::cust_header()
                    ),
-                 ],
-                 'align' => 'rrlllrr'.FS::UI::Web::cust_aligns(),
-                 'color' => [ 
+               ],
+               'align' => 'rrlllrr'.FS::UI::Web::cust_aligns(),
+               'color' => [ 
                               '',
                               '',
                               '',
@@ -71,7 +71,7 @@
                               '',
                               FS::UI::Web::cust_colors(),
                             ],
-                 'style' => [ 
+               'style' => [ 
                               '',
                               '',
                               '',
@@ -80,7 +80,7 @@
                               '',
                               '',
                               FS::UI::Web::cust_styles(),
-                            ],
+                          ],
            )
 %>
 <%init>
@@ -106,6 +106,10 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.agentnum = $1";
 }
 
+if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
+  push @where, "billpkgnum = $1";
+}
+
 #classnum
 # not specified: all classes
 # 0: empty class
@@ -346,7 +350,7 @@ if ( $cgi->param('cust_tax') ) {
   push @where, $cust_exempt;
 }
 
-my $count_query = "SELECT COUNT(DISTINCT billpkgnum),
+my $count_query = "SELECT COUNT(DISTINCT creditbillpkgnum),
                           SUM(cust_credit_bill_pkg.amount)";
 
 my $join_cust =
@@ -412,8 +416,8 @@ my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
                     LEFT JOIN cust_credit      USING ( crednum ) ';
 
 $count_query .= " FROM cust_credit_bill_pkg
-                         $join_pkg
                          $join_cust_bill_pkg
+                         $join_pkg
                          $join_credit
                          $join_cust
                        $where";
@@ -430,8 +434,8 @@ push @select, 'cust_main.custnum',
 
 my $query = {
   'table'     => 'cust_credit_bill_pkg',
-  'addl_from' => "$join_pkg
-                  $join_cust_bill_pkg
+  'addl_from' => "$join_cust_bill_pkg
+                  $join_pkg
                   $join_credit
                   $join_cust",
   'hashref'   => {},