Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / view / cust_main / packages / package.html
index 3a362b6..e47d891 100644 (file)
@@ -1,54 +1,91 @@
-<TD CLASS="inv package" BGCOLOR="<% $bgcolor %>" VALIGN="top"
-  STYLE="border-left-width: <% $supplemental * 30 %>px">
+<TD CLASS="inv package" BGCOLOR="<% $bgcolor %>" VALIGN="top" <%$style%>>
   <TABLE CLASS="inv package"> 
+
+
     <TR>
       <TD COLSPAN=2>
+        <% $opt{before_pkg_callback}
+             ? &{ $opt{before_pkg_callback} }( $cust_pkg )
+             : ''
+        %>
         <A NAME="cust_pkg<% $cust_pkg->pkgnum %>"
            ID  ="cust_pkg<% $cust_pkg->pkgnum %>"
-        ><% $curuser->option('show_pkgnum') ? $cust_pkg->pkgnum.': ' : '' %><B><% $part_pkg->pkg |h %></B></A>
-        - 
-        <% $part_pkg->custom_comment |h %>
+        ><% $opt{show_pkgnum} ? $cust_pkg->pkgnum.': ' : '' %><B><% $part_pkg->pkg |h %></B></A>
+%
+%       my $custom_comment;
+%       if ( 0 && FS::part_pkg::flat->can('price_info') eq $part_pkg->can('price_info') ) {
+%         #perf hack / disabled in 4.x (feature pricing / cust_pkg_usageprice incompatible, well, harder)
+%
+%         $part_pkg->{"_$_"} = $part_pkg->option($_) || 0
+%           foreach grep !defined($part_pkg->{"_$_"} ), qw(setup_fee recur_fee);
+%           
+%         my $str = '';
+%         $str = $opt{money_char} . $part_pkg->{_setup_fee}.
+%                ($part_pkg->{_recur_fee} ? ' setup' : ' one-time')
+%           if $part_pkg->{_setup_fee};
+%         $str .= ', ' if $part_pkg->{_setup_fee} && $part_pkg->{_recur_fee};
+%         $str .= $opt{money_char}. $part_pkg->{_recur_fee}.
+%                 '/'. $part_pkg->freq_pretty
+%           if $part_pkg->{_recur_fee};
+%         $str;
+%
+%         $custom_comment = 
+%           ( $part_pkg->custom ? '(CUSTOM) ' : '' ).
+%           $part_pkg->comment.
+%           ( ($part_pkg->custom || $part_pkg->comment) ? ' - ' : '' ).
+%           ($str || 'No charge');
+%
+%       } else {
+%         $custom_comment = $part_pkg->custom_comment('cust_pkg'=>$cust_pkg);
+%       }
+        <% $custom_comment ? ' - ' : '' %>
+        <% $custom_comment |h %>
       </TD>
     </TR>
 
-% if ( $cust_pkg->quantity > 1 ) {
-    <TR>
-      <TD COLSPAN=2>
-        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<% mt('Quantity:') |h %> 
-        <B><% $cust_pkg->quantity %></B>
-      </TD>
-    </TR>
-% }
 
     <TR>
       <TD COLSPAN=2>
         <FONT SIZE=-1>
 
-%         unless ( $cust_pkg->get('cancel') ) { 
+%         if ( $part_pkg->freq eq '0' and !$opt{no_links} ) {
+%           # One-time charge.  Nothing you can do with this, unless:
+%           if ( $curuser->access_right('Modify one-time charge') ) {
+                (&nbsp;<%onetime_change_link($cust_pkg)%>&nbsp;)
+%           }
+%           # also, you can discount it
+%           if ( $curuser->access_right('Discount customer package')
+%                && ! scalar($cust_pkg->cust_pkg_discount_active)
+%                && ! scalar($cust_pkg->part_pkg->part_pkg_discount)
+%              ) {
+              (&nbsp;<%pkg_discount_link($cust_pkg)%>&nbsp;)
+%           }
+          <BR>
 %
-%           if ( $supplemental ) {
-%             # then only show "Edit dates", "Add invoice details", and "Add
-%             # comments".
-%             if ( $curuser->access_right('Edit customer package dates') ) {
-                (&nbsp;<%pkg_dates_link($cust_pkg)%>&nbsp;)
-%             }
+%         } elsif ( !$cust_pkg->get('cancel') and !$opt{no_links} ) {
+%
+%           if ( $change_from ) {
+%             # This is the target package for a future change.
+%             # Nothing you can do with it besides modify/cancel the 
+%             # future change, and that's on the current package.
+%           } elsif ( $supplemental ) {
+%             # Supplemental packages can't be changed independently.
+%             # Show only "Add comments" and "Add invoice details".
 %           } else {
-%             # the usual case
+%             # the usual case: links to change package definition,
+%             # discount, and customization
 %             my $br = 0;
 %             if ( $curuser->access_right('Change customer package') ) {
 %               $br=1;
                 (&nbsp;<%pkg_change_link($cust_pkg)%>&nbsp;)
 %             } 
 %
-%             if ( $curuser->access_right('Edit customer package dates') ) {
-%               $br=1;
-                (&nbsp;<%pkg_dates_link($cust_pkg)%>&nbsp;)
-%             } 
-%
 %             if ( $curuser->access_right('Discount customer package')
 %                  && $part_pkg->can_discount
-%                  && ! scalar($cust_pkg->cust_pkg_discount_active)
-%                  && ! scalar($cust_pkg->part_pkg->part_pkg_discount)
+%                  && ! scalar( @{ $cust_pkg->{_cust_pkg_discount_active} } )
+%                  && (    ! $opt{'term_discounts'}
+%                       || ! scalar($cust_pkg->part_pkg->part_pkg_discount)
+%                     )
 %                )
 %             {
 %               $br=1;
               <% $br ? '<BR>' : '' %>
 %           } 
 
-%           if ( $cust_pkg->num_cust_event
-%                && (    $curuser->access_right('Billing event reports')
+%           if (    (    $curuser->access_right('Billing event reports')
 %                     || $curuser->access_right('View customer billing events')
 %                   )
+%                && $cust_pkg->exists_cust_event
 %              ) {
               (&nbsp;<%pkg_event_link($cust_pkg)%>&nbsp;)
 %           }
       </TD>
     </TR>
 
+
+    <TR>
+      <TD COLSPAN=2>
+
+%       my $change_quan_label = 'Change quantity';
+%       if ( $cust_pkg->quantity > 1 ) {
+%         $change_quan_label = 'change';
+          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<% mt('Quantity:') |h %> 
+          <B><% $cust_pkg->quantity %></B>
+%       }
+
+%       if ( $curuser->access_right('Change customer package')
+%              && ! $cust_pkg->get('cancel')
+%              && ! $supplemental
+%              && $part_pkg->freq ne '0'
+%              && ! $opt{no_links}
+%              && $opt{'invoice-unitprice'}
+%          )
+%       {
+          <FONT SIZE="-1">
+            (&nbsp;<% pkg_change_quantity_link($cust_pkg, $change_quan_label) %>&nbsp;)
+          </FONT>
+%       }
+
+      </TD>
+    </TR>
+
+
+    <TR>
+      <TD COLSPAN=2>
+
+%       my $change_sales_label = 'Change sales person';
+%       if ( $cust_pkg->salesnum ) {
+%         $change_sales_label = 'change';
+          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<% mt('Sales Person:') |h %> 
+          <B><% $cust_pkg->salesperson |h %></B>
+%       }
+
+%       if ( $curuser->access_right('Change customer package')
+%              && ! $cust_pkg->get('cancel')
+%              && ! $supplemental
+%              #&& $part_pkg->freq ne '0'
+%              && ! $opt{no_links}
+%          )
+%       {
+        <FONT SIZE="-1">
+          (&nbsp;<% pkg_change_salesnum_link($cust_pkg, $change_sales_label) %>&nbsp;)
+        </FONT>
+%     }
+
+      </TD>
+    </TR>
+
+
 %   my $editi = $curuser->access_right('Edit customer package invoice details');
 %   my $editc = $curuser->access_right('Edit customer package comments');
 %   my @cust_pkg_detail = $cust_pkg->cust_pkg_detail;
                 <TH BGCOLOR="#dddddd" STYLE="border-bottom: dashed 1px black; padding-bottom: 1px">
                   <FONT SIZE="-1">
                     <% mt('Invoice details') |h %> 
-%                   if ( $editi && ! $cust_pkg->get('cancel') ) {
+%                   if ( $editi && ! $cust_pkg->get('cancel') && ! $opt{no_links} ) {
                       (<& /elements/popup_link.html, { 
                                     'action'      => $editlink. 'I',
                                     'label'       => emt('edit'),
           </TD>
 %       } else {
           <TD>
-%           if ( $editi && ! $cust_pkg->get('cancel') ) {
+%           if ( $editi && ! $cust_pkg->get('cancel') && ! $opt{no_links} ) {
               <FONT SIZE="-1">
                 (&nbsp;<% include('/elements/popup_link.html', { 
                                'action'      => $editlink. 'I',
                 <TH BGCOLOR="#dddddd" STYLE="border-bottom: dashed 1px black; padding-bottom: 1px">
                   <FONT SIZE="-1">
                     <% mt('Comments') |h %> 
-%                   if ( $editc ) {
+%                   if ( $editc && ! $opt{no_links} ) {
                       (<& /elements/popup_link.html, { 
                                     'action'      => $editlink. 'C',
                                     'label'       => emt('edit'),
           </TD>
 %       } else {
           <TD>
-%           if ( $editc ) {
+%           if ( $editc && ! $opt{no_links} ) {
               <FONT SIZE="-1">
                 (&nbsp;<& /elements/popup_link.html, { 
                                'action'      => $editlink. 'C',
 %       }
 
       </TR>
-%     if ( $curuser->access_right('Change customer package') and 
-%           !$cust_pkg->get('cancel') and
-%           !$supplemental and
-%           !$opt{'show_location'}) {
-      <TR>
-        <TD><FONT SIZE="-1">
-          (&nbsp;<% pkg_change_location_link($cust_pkg) %>&nbsp;)
-        </FONT></TD>
-      </TR>
+
+
+%   }
+  </TABLE>
+
+% if ( @cust_pkg_usage ) {
+  <TABLE CLASS="usage inv">
+    <TR><TH COLSPAN=4><% mt('Included usage') %></TH></TR>
+%   foreach my $usage (@cust_pkg_usage) {
+%     my $part = $usage->part_pkg_usage;
+%     my $ratio = 255 * ($usage->minutes / $part->minutes);
+%     $ratio = 255 if $ratio > 255; # because rollover
+%     my $color = sprintf('STYLE="font-weight: bold; color: #%02x%02x00"', 255 - $ratio, $ratio);
+%     my $trstyle = '';
+%     $trstyle = ' CLASS="shared"' if $part->shared;
+    <TR<%$trstyle%>>
+      <TD ALIGN="right"><% $part->description %>: </TD>
+      <TD <%$color%> ALIGN="right"><% sprintf('%.1f', $usage->minutes) %></TD>
+      <TD <%$color%>> / </TD>
+      <TD <%$color%>><% $part->minutes %></TD>
+%     if ( $part->shared ) {
+      <TD><I>(shared)</I></TD>
 %     }
+    </TR>
 %   }
   </TABLE>
+% }
 
 </TD>
 
@@ -207,7 +313,32 @@ my $countrydefault = $opt{'countrydefault'} || 'US';
 my $statedefault   = $opt{'statedefault'}
                      || ($countrydefault eq 'US' ? 'CA' : '');
 
+# put a marker on the left edge of this column
+# if this package is somehow special
 my $supplemental = $opt{'supplemental'} || 0;
+my $change_from = $opt{'change_from'} || 0;
+my $style = '';
+if ( $supplemental or $change_from ) {
+  $style = 'border-left-width: '.($supplemental + $change_from)*30 . 'px; '.
+           'border-color: ';
+  if ( $supplemental ) {
+    $style .= '#bbbbff';
+  } elsif ( $change_from ) {
+    $style .= '#bbffbb';
+  }
+  $style = qq!STYLE="$style"!;
+}
+
+$cust_pkg->pkgnum =~ /^(\d+)$/;
+my $pkgnum = $1;
+my @cust_pkg_usage = qsearch({
+  'select'    => 'cust_pkg_usage.*',
+  'table'     => 'cust_pkg_usage',
+  'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)',
+  'extra_sql' => " WHERE pkgnum = $1",
+  'order_by'  => ' ORDER BY priority ASC, description ASC',
+});
+
 #subroutines
 
 #false laziness w/status.html
@@ -225,6 +356,21 @@ sub pkg_change_link {
     'label'       => emt('Change package'),
     'actionlabel' => emt('Change'),
     'cust_pkg'    => $cust_pkg,
+    'width'       => 960,
+    'height'      => 490,
+  );
+}
+
+sub onetime_change_link {
+  my $cust_pkg = shift;
+  my $pkgnum = $cust_pkg->pkgnum;
+  include( '/elements/popup_link-cust_pkg.html',
+    'action'      => $p. "edit/quick-charge.html?change_pkgnum=$pkgnum",
+    'label'       => emt('Modify one-time charge'),
+    'actionlabel' => emt('Modify'),
+    'cust_pkg'    => $cust_pkg,
+    'width'       => 690,
+    'height'      => 380,
   );
 }
 
@@ -238,10 +384,34 @@ sub pkg_change_location_link {
     'label'       => emt('Change location'),
     'actionlabel' => emt('Change'),
     'cust_pkg'    => $cust_pkg,
+    'width'       => 960,
+    'height'      => 490,
+  );
+}
+
+sub pkg_change_quantity_link {
+  my( $cust_pkg, $label ) = @_;
+  include( '/elements/popup_link-cust_pkg.html',
+    'action'      => $p. 'edit/cust_pkg_quantity.html?',
+    'label'       => emt($label),
+    'actionlabel' => emt('Change'),
+    'cust_pkg'    => $cust_pkg,
+    'width'       => 390,
+    'height'      => 220,
   );
 }
 
-sub pkg_dates_link { pkg_link('edit/REAL_cust_pkg', emt('Edit dates'), @_ ); }
+sub pkg_change_salesnum_link {
+  my( $cust_pkg, $label ) = @_;
+  include( '/elements/popup_link-cust_pkg.html',
+    'action'      => $p. 'edit/cust_pkg_salesnum.html?',
+    'label'       => emt($label),
+    'actionlabel' => emt('Change'),
+    'cust_pkg'    => $cust_pkg,
+    'width'       => 390,
+    'height'      => 220,
+  );
+}
 
 sub pkg_discount_link {
   my $cust_pkg = shift or return '';