continue sales person work: customer and package selection, commissions, reporting...
[freeside.git] / httemplate / search / cust_bill_pkg.cgi
index 11937b3..744286a 100644 (file)
@@ -10,6 +10,7 @@
                    emt('Description'),
                    @post_desc_header,
                    @peritem_desc,
+                   @currency_desc,
                    emt('Invoice'),
                    emt('Date'),
                    emt('Paid'),
@@ -32,6 +33,7 @@
                    #strikethrough or "N/A ($amount)" or something these when
                    # they're not applicable to pkg_tax search
                    @peritem_sub,
+                   @currency_sub,
                    'invnum',
                    sub { time2str('%b %d %Y', shift->_date ) },
                    sub { sprintf($money_char.'%.2f', shift->get('pay_amount')) },
@@ -44,6 +46,7 @@
                    '',
                    @post_desc_null,
                    @peritem,
+                   @currency,
                    'invnum',
                    '_date',
                    #'pay_amount',
@@ -55,6 +58,7 @@
                    '',
                    @post_desc_null,
                    @peritem_null,
+                   @currency_null,
                    $ilink,
                    $ilink,
                    $pay_link,
@@ -68,6 +72,7 @@
                             'rl'.
                             $post_desc_align.
                             $peritem_align.
+                            $currency_align.
                             'rcrr'.
                             FS::UI::Web::cust_aligns(),
                  'color' => [ 
@@ -76,6 +81,7 @@
                               '',
                               @post_desc_null,
                               @peritem_null,
+                              @currency_null,
                               '',
                               '',
                               '',
@@ -88,6 +94,7 @@
                               '',
                               @post_desc_null,
                               @peritem_null,
+                              @currency_null,
                               '',
                               '',
                               '',
@@ -124,6 +131,10 @@ Filtering parameters:
 
 - classnum: Filter on package class.
 
+- report_optionnum: Filter on package report class.  Can be a single report
+  class number, a comma-separated list, the word "multiple", or an empty 
+  string (for "no report class").
+
 - use_override: Apply "classnum" and "taxclass" filtering based on the 
   override (bundle) pkgpart, rather than always using the true pkgpart.
 
@@ -196,6 +207,23 @@ my @total_desc = ( $money_char.'%.2f total' ); # sprintf strings
 my @peritem = ( 'setup', 'recur' );
 my @peritem_desc = ( 'Setup charge', 'Recurring charge' );
 
+my @currency_desc = ();
+my @currency_sub = ();
+my @currency = ();
+if ( $conf->config('currencies') ) {
+  @currency_desc = ( 'Setup billed', 'Recurring billed' );
+  @currency_sub = (
+    map {
+      my $what = $_;
+      sub { my $currency = $_[0]->get($what.'_billed_currency') or return '';
+            $currency. ' '. currency_symbol($currency, SYM_HTML).
+              $_[0]->get($what.'_billed_amount');
+          };
+    } qw( setup recur )
+  );
+  @currency = ( 'setup_billed_amount', 'recur_billed_amount' ); #for sorting
+}
+
 my @pkgnum_header = ();
 my @pkgnum = ();
 my @pkgnum_null;
@@ -222,7 +250,7 @@ if ( $conf->exists('enable_taxclasses') ) {
 
 # valid in both the tax and non-tax cases
 my $join_cust = 
-  " LEFT JOIN cust_bill USING (invnum)".
+  " LEFT JOIN cust_bill ON (cust_bill_pkg.invnum = cust_bill.invnum)".
   # use cust_pkg.locationnum if it exists
   FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
 
@@ -255,18 +283,35 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.agentnum = $1";
 }
 
+# salesnum
+if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) {
+
+  my $salesnum = $1;
+
+  my $cmp_salesnum = $cgi->param('cust_main_sales')
+                       ? ' COALESCE( cust_pkg.salesnum, cust_main.salesnum )'
+                       : ' cust_pkg.salesnum ';
+
+  push @where, "$cmp_salesnum = $salesnum";
+
+  $cgi->param('classnum', 0) unless $cgi->param('classnum');
+}
+
 # refnum
 if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.refnum = $1";
 }
 
-# cust_classnum
-if ( $cgi->param('cust_classnum') ) {
-  my @classnums = grep /^\d+$/, $cgi->param('cust_classnum');
-  push @where, 'cust_main.classnum IN('.join(',',@classnums).')'
+# cust_classnum (false laziness w/ elements/cust_main_dayranges.html, elements/cust_pay_or_refund.html, prepaid_income.html, cust_bill_pay.html, cust_bill_pkg_referral.html, unearned_detail.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
+if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
+  my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
+  push @where, 'COALESCE( cust_main.classnum, 0) IN ( '.
+                   join(',', map { $_ || '0' } @classnums ).
+               ' )'
     if @classnums;
 }
 
+
 # custnum
 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
   push @where, "cust_main.custnum = $1";
@@ -304,6 +349,14 @@ if ( $cgi->param('nottax') ) {
     push @where, "COALESCE($part_pkg.classnum, 0) = $1";
   }
 
+  if ( $cgi->param('report_optionnum') =~ /^(\w+)$/ ) {
+    # code reuse FTW
+    my $num = $1;
+    push @where, 
+      FS::Report::Table->with_report_option( $1, $cgi->param('use_override') )
+    ;
+  }
+
   # taxclass
   if ( $cgi->param('taxclassNULL') ) {
     # a little different from 'taxclass' in that it applies to the
@@ -559,12 +612,11 @@ if ( $cgi->param('nottax') ) {
 
 
 #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';
+my $pay_sub = "SELECT SUM(cust_bill_pay_pkg.amount)
+                 FROM cust_bill_pay_pkg
+                   WHERE cust_bill_pkg.billpkgnum = cust_bill_pay_pkg.billpkgnum
+              ";
+push @select, "($pay_sub) AS pay_amount";
 
 
 # credit
@@ -630,13 +682,12 @@ if ( $cgi->param('credit') ) {
 
   #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';
+  my $credit_sub = "
+    SELECT SUM(cust_credit_bill_pkg.amount)
+      FROM cust_credit_bill_pkg
+        WHERE cust_bill_pkg.billpkgnum = cust_credit_bill_pkg.billpkgnum
+  ";
+  push @select, "($credit_sub) AS credit_amount";
 
 }
 
@@ -671,6 +722,10 @@ my @peritem_sub = map {
 my @peritem_null = map { '' } @peritem; # placeholders
 my $peritem_align = 'r' x scalar(@peritem);
 
+@currency_desc = map {emt($_)} @currency_desc;
+my @currency_null = map { '' } @currency; # placeholders
+my $currency_align = 'r' x scalar(@currency);
+
 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];