Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / quotation.pm
index f2a9620..f820510 100644 (file)
@@ -260,15 +260,32 @@ sub _items_sections {
   my %opt = @_;
   my $escape = $opt{escape}; # the only one we care about
 
-  my %subtotals; # package frequency => subtotal
+  my %subtotals = (); # package frequency => subtotal
+  my $disable_total = 0;
   foreach my $pkg ($self->quotation_pkg) {
-    my $recur_freq = $pkg->part_pkg->freq;
+
+    my $part_pkg = $pkg->part_pkg;
+
+    my $recur_freq = $part_pkg->freq;
     ($subtotals{0} ||= 0) += $pkg->setup + $pkg->setup_tax;
     ($subtotals{$recur_freq} ||= 0) += $pkg->recur + $pkg->recur_tax;
+
+    #this is a shitty hack based on what's in part_pkg/ at the moment
+    # but its good enough for the 99% common case of preventing totals from
+    # displaying for prorate packages
+    $disable_total = 1
+      if $part_pkg->plan =~ /^(prorate|torrus|agent$)/
+      || $part_pkg->option('recur_method') eq 'prorate'
+      || ( $part_pkg->option('sync_bill_date')
+             && $self->custnum
+             && $self->cust_main->billing_pkgs #num_billing_pkgs when we have it
+         );
+
   }
   my @pkg_freq_order = keys %{ FS::Misc->pkg_freqs };
 
   my @sections;
+  my $no_recurring = 0;
   foreach my $freq (keys %subtotals) {
 
     next if $subtotals{$freq} == 0;
@@ -279,6 +296,7 @@ sub _items_sections {
     if ( $freq eq '0' ) {
       if ( scalar(keys(%subtotals)) == 1 ) {
         # there are no recurring packages
+        $no_recurring = 1;
         $desc = $self->mt('Charges');
       } else {
         $desc = $self->mt('Setup Charges');
@@ -295,6 +313,18 @@ sub _items_sections {
       'subtotal'    => sprintf('%.2f',$subtotals{$freq}),
     };
   }
+
+  unless ( $disable_total || $no_recurring ) {
+    my $total = 0;
+    $total += $_ for values %subtotals;
+    push @sections, {
+      'description' => 'First payment',
+      'sort_weight' => 0,
+      'category'   => 'Total category', #required but what's it used for?
+      'subtotal'    => sprintf('%.2f',$total)
+    };
+  }
+
   return \@sections, [];
 }
 
@@ -571,6 +601,7 @@ sub estimate {
 
   ###### BEGIN TRANSACTION ######
   local $@;
+  local $SIG{__DIE__};
   eval {
     my $temp_dbh = myconnect();
     local $FS::UID::dbh = $temp_dbh;
@@ -695,22 +726,24 @@ sub estimate {
       # discounts
       if ( $cust_bill_pkg->get('discounts') ) {
         my $discount = $cust_bill_pkg->get('discounts')->[0];
-        # discount records are generated as (setup, recur).
-        # well, not always, sometimes it's just (recur), but fixing this
-        # is horribly invasive.
-        my $qpd = $quotation_pkg_discount{$quotationpkgnum}
-              ||= qsearchs('quotation_pkg_discount', {
-                  'quotationpkgnum' => $quotationpkgnum
-                  });
-
-        if (!$qpd) { #can't happen
-          warn "$me simulated bill returned a discount but no discount is in effect.\n";
-        }
-        if ($discount and $qpd) {
-          if ( $i == 0 ) {
-            $qpd->set('setup_amount', $discount->amount);
-          } else {
-            $qpd->set('recur_amount', $discount->amount);
+        if ( $discount ) {
+          # discount records are generated as (setup, recur).
+          # well, not always, sometimes it's just (recur), but fixing this
+          # is horribly invasive.
+          my $qpd = $quotation_pkg_discount{$quotationpkgnum}
+                ||= qsearchs('quotation_pkg_discount', {
+                    'quotationpkgnum' => $quotationpkgnum
+                    });
+
+          if (!$qpd) { #can't happen
+            warn "$me simulated bill returned a discount but no discount is in effect.\n";
+          }
+          if ($discount and $qpd) {
+            if ( $i == 0 ) {
+              $qpd->set('setup_amount', $discount->amount);
+            } else {
+              $qpd->set('recur_amount', $discount->amount);
+            }
           }
         }
       } # end of discount stuff