Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / view / cust_main / packages / package.html
1 <TD CLASS="inv package" BGCOLOR="<% $bgcolor %>" VALIGN="top">
2   <% join('', @marker ) %>
3   <TABLE CLASS="inv package"> 
4     <TR>
5       <TD COLSPAN=2>
6         <% $opt{before_pkg_callback}
7              ? &{ $opt{before_pkg_callback} }( $cust_pkg )
8              : ''
9         %>
10         <A NAME="cust_pkg<% $cust_pkg->pkgnum %>"
11            ID  ="cust_pkg<% $cust_pkg->pkgnum %>"
12         ><% $opt{show_pkgnum} ? $cust_pkg->pkgnum.': ' : '' %><B><% $part_pkg->pkg |h %></B></A>
13 %
14 %       my $custom_comment;
15 %       if ( 0 && FS::part_pkg::flat->can('price_info') eq $part_pkg->can('price_info') ) {
16 %         #perf hack / disabled in 4.x (feature pricing / cust_pkg_usageprice incompatible, well, harder)
17 %
18 %         $part_pkg->{"_$_"} = $part_pkg->option($_) || 0
19 %           foreach grep !defined($part_pkg->{"_$_"} ), qw(setup_fee recur_fee);
20 %           
21 %         my $str = '';
22 %         $str = $opt{money_char} . $part_pkg->{_setup_fee}.
23 %                ($part_pkg->{_recur_fee} ? ' setup' : ' one-time')
24 %           if $part_pkg->{_setup_fee};
25 %         $str .= ', ' if $part_pkg->{_setup_fee} && $part_pkg->{_recur_fee};
26 %         $str .= $opt{money_char}. $part_pkg->{_recur_fee}.
27 %                 '/'. $part_pkg->freq_pretty
28 %           if $part_pkg->{_recur_fee};
29 %         $str;
30 %
31 %         $custom_comment = 
32 %           ( $part_pkg->custom ? '(CUSTOM) ' : '' ).
33 %           $part_pkg->comment.
34 %           ( ($part_pkg->custom || $part_pkg->comment) ? ' - ' : '' ).
35 %           ($str || 'No charge');
36 %
37 %       } else {
38 %         $custom_comment = $part_pkg->custom_comment('cust_pkg'=>$cust_pkg);
39 %       }
40         <% $custom_comment ? ' - ' : '' %>
41         <% $custom_comment |h %>
42       </TD>
43     </TR>
44
45
46     <TR>
47       <TD COLSPAN=2>
48         <FONT SIZE=-1>
49
50 %         if ( $part_pkg->freq eq '0' and !$opt{no_links} ) {
51 %           # One-time charge.  Nothing you can do with this, unless:
52 %           if ( $curuser->access_right('Modify one-time charge') ) {
53                 (&nbsp;<%onetime_change_link($cust_pkg)%>&nbsp;)
54 %           }
55 %           # also, you can discount it
56 %           if ( $can_discount_pkg ) {
57               (&nbsp;<%pkg_discount_link($cust_pkg)%>&nbsp;)
58 %           }
59           <BR>
60 %
61 %         } elsif ( !$cust_pkg->get('cancel') and !$opt{no_links} ) {
62 %
63 %           if ( $change_from ) {
64 %             # This is the target package for a future change.
65 %             # Nothing you can do with it besides modify/cancel the 
66 %             # future change, and that's on the current package.
67 %           } elsif ( $supplemental ) {
68 %             # Supplemental packages can't be changed independently.
69 %             # Show only "Add comments" and "Add invoice details".
70 %           } else {
71 %             # the usual case: links to change package definition,
72 %             # discount, and customization
73 %             my $br = 0;
74 %             if ( $curuser->access_right('Change customer package') ) {
75 %               $br=1;
76                 (&nbsp;<%pkg_change_link($cust_pkg)%>&nbsp;)
77 %             } 
78 %
79 %             if ( $can_discount_pkg ) {
80 %               $br=1;
81                 (&nbsp;<%pkg_discount_link($cust_pkg)%>&nbsp;)
82 %             }
83 %
84 %             if ( $curuser->access_right('Customize customer package') ) {
85 %               $br=1;
86                 (&nbsp;<%pkg_customize_link($cust_pkg,$part_pkg)%>&nbsp;)
87 %             } 
88 %
89               <% $br ? '<BR>' : '' %>
90 %           } 
91
92 %           if (    (    $curuser->access_right('Billing event reports')
93 %                     || $curuser->access_right('View customer billing events')
94 %                   )
95 %                && $cust_pkg->exists_cust_event
96 %              ) {
97               (&nbsp;<%pkg_event_link($cust_pkg)%>&nbsp;)
98 %           }
99 %         } # a canceled recurring package, or else no_links is in effect
100
101         </FONT>
102       </TD>
103     </TR>
104
105
106     <TR>
107       <TD COLSPAN=2>
108
109 %       my $change_quan_label = 'Change quantity';
110 %       if ( $cust_pkg->quantity > 1 ) {
111 %         $change_quan_label = 'change';
112           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<% mt('Quantity:') |h %> 
113           <B><% $cust_pkg->quantity %></B>
114 %       }
115
116 %       if ( $curuser->access_right('Change customer package')
117 %              && ! $cust_pkg->get('cancel')
118 %              && ! $supplemental
119 %              && $part_pkg->freq ne '0'
120 %              && ! $opt{no_links}
121 %              && $opt{'invoice-unitprice'}
122 %          )
123 %       {
124           <FONT SIZE="-1">
125             (&nbsp;<% pkg_change_quantity_link($cust_pkg, $change_quan_label) %>&nbsp;)
126           </FONT>
127 %       }
128
129       </TD>
130     </TR>
131
132
133     <TR>
134       <TD COLSPAN=2>
135
136 %       my $change_sales_label = 'Change sales person';
137 %       if ( $cust_pkg->salesnum ) {
138 %         $change_sales_label = 'change';
139           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<% mt('Sales Person:') |h %> 
140           <B><% $cust_pkg->salesperson |h %></B>
141 %       }
142
143 %       if ( $curuser->access_right('Change customer package')
144 %              && ! $cust_pkg->get('cancel')
145 %              && ! $supplemental
146 %              #&& $part_pkg->freq ne '0'
147 %              && ! $opt{no_links}
148 %          )
149 %       {
150         <FONT SIZE="-1">
151           (&nbsp;<% pkg_change_salesnum_link($cust_pkg, $change_sales_label) %>&nbsp;)
152         </FONT>
153 %     }
154
155       </TD>
156     </TR>
157
158
159 %   my $editi = $curuser->access_right('Edit customer package invoice details');
160 %   my $editc = $curuser->access_right('Edit customer package comments');
161 %   my @cust_pkg_detail = $cust_pkg->cust_pkg_detail;
162 %   my @invoice_detail = grep { $_->detailtype eq 'I' } @cust_pkg_detail;
163 %   my @comments       = grep { $_->detailtype eq 'C' } @cust_pkg_detail;
164 %
165 %   if ( scalar(@invoice_detail) || scalar(@comments) || $editi || $editc ) {
166 %
167 %     my $editlink = $p. 'edit/cust_pkg_detail.html?pkgnum='. $cust_pkg->pkgnum.
168 %                    ';detailtype=';
169
170       <TR>
171
172 %       if ( @invoice_detail ) {
173           <TD VALIGN="top">
174             <& /elements/table-grid.html &>
175               <TR>
176                 <TH BGCOLOR="#dddddd" STYLE="border-bottom: dashed 1px black; padding-bottom: 1px">
177                   <FONT SIZE="-1">
178                     <% mt('Invoice details') |h %> 
179 %                   if ( $editi && ! $cust_pkg->get('cancel') && ! $opt{no_links} ) {
180                       (<& /elements/popup_link.html, { 
181                                     'action'      => $editlink. 'I',
182                                     'label'       => emt('edit'),
183                                     'actionlabel' => emt('Edit invoice details'),
184                                     'color'       => '#333399',
185                                     'width'       => 763,
186                                  }
187                        &>)
188 %                   }
189                   </FONT>
190                 </TH>
191               </TR>
192 %             foreach my $cust_pkg_detail ( @invoice_detail ) {
193                 <TR>
194                   <TD><FONT SIZE="-1">&nbsp;-&nbsp;<% $cust_pkg_detail->detail |h %></FONT></TD>
195                 </TR>
196 %             }
197             </TABLE>
198           </TD>
199 %       } else {
200           <TD>
201 %           if ( $editi && ! $cust_pkg->get('cancel') && ! $opt{no_links} ) {
202               <FONT SIZE="-1">
203                 (&nbsp;<% include('/elements/popup_link.html', { 
204                                'action'      => $editlink. 'I',
205                                'label'       => emt('Add invoice details'),
206                                'actionlabel' => emt('Add invoice details'),
207                                'color'       => '#333399',
208                                'width'       => 763,
209                             })
210                   %>&nbsp;)
211               </FONT>
212 %           }
213           </TD>
214 %       }
215
216 %       if ( @comments ) { 
217           <TD VALIGN="top">
218             <& /elements/table-grid.html &>
219               <TR>
220                 <TH BGCOLOR="#dddddd" STYLE="border-bottom: dashed 1px black; padding-bottom: 1px">
221                   <FONT SIZE="-1">
222                     <% mt('Comments') |h %> 
223 %                   if ( $editc && ! $opt{no_links} ) {
224                       (<& /elements/popup_link.html, { 
225                                     'action'      => $editlink. 'C',
226                                     'label'       => emt('edit'),
227                                     'actionlabel' => emt('Edit comments'),
228                                     'color'       => '#333399',
229                                     'width'       => 763,
230                                  }
231                        &>)
232 %                   }
233                   </FONT>
234                 </TH>
235               </TR>
236 %             foreach my $cust_pkg_detail ( @comments ) {
237                 <TR>
238                   <TD><FONT SIZE="-1">&nbsp;-&nbsp;<% $cust_pkg_detail->detail |h %></FONT></TD>
239                 </TR>
240 %             }
241             </TABLE>
242           </TD>
243 %       } else {
244           <TD>
245 %           if ( $editc && ! $opt{no_links} ) {
246               <FONT SIZE="-1">
247                 (&nbsp;<& /elements/popup_link.html, { 
248                                'action'      => $editlink. 'C',
249                                'label'       => emt('Add comments'),
250                                'actionlabel' => emt('Add comments'),
251                                'color'       => '#333399',
252                                'width'       => 763,
253                             }
254                   &>&nbsp;)
255               </FONT>
256 %           }
257           </TD>
258 %       }
259
260       </TR>
261
262
263 %   }
264   </TABLE>
265
266 % if ( @cust_pkg_usage ) {
267   <TABLE CLASS="usage inv">
268     <TR><TH COLSPAN=4><% mt('Included usage') %></TH></TR>
269 %   foreach my $usage (@cust_pkg_usage) {
270 %     my $part = $usage->part_pkg_usage;
271 %     my $ratio = 255 * ($usage->minutes / $part->minutes);
272 %     $ratio = 255 if $ratio > 255; # because rollover
273 %     my $color = sprintf('STYLE="font-weight: bold; color: #%02x%02x00"', 255 - $ratio, $ratio);
274 %     my $trstyle = '';
275 %     $trstyle = ' CLASS="shared"' if $part->shared;
276     <TR<%$trstyle%>>
277       <TD ALIGN="right"><% $part->description %>: </TD>
278       <TD <%$color%> ALIGN="right"><% sprintf('%.1f', $usage->minutes) %></TD>
279       <TD <%$color%>> / </TD>
280       <TD <%$color%>><% $part->minutes %></TD>
281 %     if ( $part->shared ) {
282       <TD><I>(shared)</I></TD>
283 %     }
284     </TR>
285 %   }
286   </TABLE>
287 % }
288
289   <% join('', map '</DIV>', @marker ) %>
290 </TD>
291
292 <%init>
293
294 my %opt = @_;
295
296 my $bgcolor  = $opt{'bgcolor'};
297 my $cust_pkg = $opt{'cust_pkg'};
298 my $part_pkg = $opt{'part_pkg'};
299
300 my $curuser = $FS::CurrentUser::CurrentUser;
301
302 my $countrydefault = $opt{'countrydefault'} || 'US';
303 my $statedefault   = $opt{'statedefault'}
304                      || ($countrydefault eq 'US' ? 'CA' : '');
305
306 # put a marker on the left edge of this column
307 # if this package is somehow special
308 my $supplemental = $opt{'supplemental'} || 0;
309 my $change_from = $opt{'change_from'} || 0;
310 my @marker;
311 if ( $supplemental ) {
312   push @marker, '<DIV CLASS="package-marker-supplemental">';
313 }
314 if ( $change_from ) {
315   push @marker, '<DIV CLASS="package-marker-change_from">';
316 }
317
318 $cust_pkg->pkgnum =~ /^(\d+)$/;
319 my $pkgnum = $1;
320 my @cust_pkg_usage = qsearch({
321   'select'    => 'cust_pkg_usage.*',
322   'table'     => 'cust_pkg_usage',
323   'addl_from' => ' JOIN part_pkg_usage USING (pkgusagepart)',
324   'extra_sql' => " WHERE pkgnum = $1",
325   'order_by'  => ' ORDER BY priority ASC, description ASC',
326 });
327
328 #subroutines
329
330 #false laziness w/status.html
331 sub pkg_link {
332   my($action, $label, $cust_pkg) = @_;
333   return '' unless $cust_pkg;
334   qq!<a href="$p$action.cgi?!. $cust_pkg->pkgnum. qq!">$label</a>!;
335 }
336
337 sub pkg_change_link {
338   my $cust_pkg = shift;
339   my $locationnum = $cust_pkg->locationnum;
340   include( '/elements/popup_link-cust_pkg.html',
341     'action'      => $p. "misc/change_pkg.cgi?locationnum=$locationnum",
342     'label'       => emt('Change package'),
343     'actionlabel' => emt('Change'),
344     'cust_pkg'    => $cust_pkg,
345     'width'       => 960,
346     'height'      => 538,
347   );
348 }
349
350 sub onetime_change_link {
351   my $cust_pkg = shift;
352   my $pkgnum = $cust_pkg->pkgnum;
353   include( '/elements/popup_link-cust_pkg.html',
354     'action'      => $p. "edit/quick-charge.html?change_pkgnum=$pkgnum",
355     'label'       => emt('Modify one-time charge'),
356     'actionlabel' => emt('Modify'),
357     'cust_pkg'    => $cust_pkg,
358     'width'       => 690,
359     'height'      => 440,
360   );
361 }
362
363 sub pkg_change_location_link {
364   my $cust_pkg = shift;
365   my $pkgpart = $cust_pkg->pkgpart;
366   include( '/elements/popup_link-cust_pkg.html',
367     'action'      => $p. "misc/change_pkg.cgi?locationnum=-1;pkgpart=$pkgpart;".
368                      "address1=;address2=;city=;county=;state=$statedefault;".
369                      "zip=;country=$countrydefault",
370     'label'       => emt('Change location'),
371     'actionlabel' => emt('Change'),
372     'cust_pkg'    => $cust_pkg,
373     'width'       => 960,
374     'height'      => 530,
375   );
376 }
377
378 sub pkg_change_quantity_link {
379   my( $cust_pkg, $label ) = @_;
380   include( '/elements/popup_link-cust_pkg.html',
381     'action'      => $p. 'edit/cust_pkg_quantity.html?',
382     'label'       => emt($label),
383     'actionlabel' => emt('Change'),
384     'cust_pkg'    => $cust_pkg,
385     'width'       => 390,
386     'height'      => 220,
387   );
388 }
389
390 sub pkg_change_salesnum_link {
391   my( $cust_pkg, $label ) = @_;
392   include( '/elements/popup_link-cust_pkg.html',
393     'action'      => $p. 'edit/cust_pkg_salesnum.html?',
394     'label'       => emt($label),
395     'actionlabel' => emt('Change'),
396     'cust_pkg'    => $cust_pkg,
397     'width'       => 390,
398     'height'      => 220,
399   );
400 }
401
402 sub pkg_discount_link {
403   my $cust_pkg = shift or return '';
404   include( '/elements/popup_link-cust_pkg.html',
405     'action'      => $p.'edit/cust_pkg_discount.html',
406     'label'       => emt('Discount'),
407     'actionlabel' => emt('Discount'),
408     'cust_pkg'    => $cust_pkg,
409     'width'       => 616,
410   );
411 }
412
413 sub pkg_customize_link {
414   my $cust_pkg = shift or return '';
415   my $part_pkg = shift;
416   my $custnum = $cust_pkg->custnum;
417   qq!<A HREF="${p}edit/part_pkg.cgi?!.
418     "clone=". $part_pkg->pkgpart. ';'.
419     "pkgnum=". $cust_pkg->pkgnum.
420     qq!">!.emt('Customize').'</A>';
421 }
422
423 sub pkg_event_link {
424   my($cust_pkg) = @_;
425   qq!<a href="${p}search/cust_event.html?pkgnum=!. $cust_pkg->pkgnum. qq!">!.
426   emt('View package events').
427   '</a>';
428 }
429
430 # figure out if this user will be able to edit either the setup or recurring
431 # discounts for this package
432 my $can_discount_pkg = (
433   $part_pkg->can_discount
434   and
435   ( ( $curuser->access_right(['Discount customer package', 'Waive setup fee'])
436       and $cust_pkg->base_setup > 0
437       and !$cust_pkg->setup
438     )
439    or
440     ( $curuser->access_right('Discount customer package')
441       and $cust_pkg->base_recur > 0
442       and $cust_pkg->freq ne '0'
443     )
444   )
445 );
446
447 </%init>