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