a1a47ea74a6baa348af7412bdbd53a3a73c5b78c
[freeside.git] / httemplate / view / cust_main.cgi
1 <!-- mason kludge -->
2 <%
3
4 my $conf = new FS::Conf;
5
6 #false laziness with view/cust_pkg.cgi, but i'm trying to make that go away so
7 my %uiview = ();
8 my %uiadd = ();
9 foreach my $part_svc ( qsearch('part_svc',{}) ) {
10   $uiview{$part_svc->svcpart} = popurl(2). "view/". $part_svc->svcdb . ".cgi";
11   $uiadd{$part_svc->svcpart}= popurl(2). "edit/". $part_svc->svcdb . ".cgi";
12 }
13
14 print header("Customer View", menubar(
15   'Main Menu' => popurl(2)
16 ));
17
18 die "No customer specified (bad URL)!" unless $cgi->keywords;
19 my($query) = $cgi->keywords; # needs parens with my, ->keywords returns array
20 $query =~ /^(\d+)$/;
21 my $custnum = $1;
22 my $cust_main = qsearchs('cust_main',{'custnum'=>$custnum});
23 die "Customer not found!" unless $cust_main;
24
25 print qq!<A HREF="!, popurl(2), 
26       qq!edit/cust_main.cgi?$custnum">Edit this customer</A>!;
27 print qq! | <A HREF="!, popurl(2), 
28       qq!misc/delete-customer.cgi?$custnum"> Delete this customer</A>!
29   if $conf->exists('deletecustomers');
30
31 unless ( $conf->exists('disable_customer_referrals') ) {
32   print qq! | <A HREF="!, popurl(2),
33         qq!edit/cust_main.cgi?referral_custnum=$custnum">!,
34         qq!Refer a new customer</A>!;
35
36   print qq! | <A HREF="!, popurl(2),
37         qq!search/cust_main.cgi?referral_custnum=$custnum">!,
38         qq!View this customer's referrals</A>!;
39 }
40
41 print '<BR><BR>';
42
43 my $signupurl = $conf->config('signupurl');
44 if ( $signupurl ) {
45 print "This customer's signup URL: ".
46       "<a href=\"$signupurl?ref=$custnum\">$signupurl?ref=$custnum</a><BR><BR>";
47 }
48
49 print '<A NAME="cust_main"></A>';
50
51 print &itable(), '<TR>';
52
53 print '<TD VALIGN="top">';
54
55   print "Billing address", &ntable("#cccccc"), "<TR><TD>",
56         &ntable("#cccccc",2),
57     '<TR><TD ALIGN="right">Contact name</TD>',
58       '<TD COLSPAN=3 BGCOLOR="#ffffff">',
59       $cust_main->last, ', ', $cust_main->first,
60       '</TD>';
61 print '<TD ALIGN="right">SS#</TD><TD BGCOLOR="#ffffff">',
62       $cust_main->ss || '&nbsp', '</TD>'
63   if $conf->exists('show_ss');
64
65 print '</TR>',
66     '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
67       $cust_main->company,
68       '</TD></TR>',
69     '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
70       $cust_main->address1,
71       '</TD></TR>',
72   ;
73   print '<TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
74         $cust_main->address2, '</TD></TR>'
75     if $cust_main->address2;
76   print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
77           $cust_main->city,
78           '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
79           $cust_main->state,
80           '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
81           $cust_main->zip, '</TD></TR>',
82         '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
83           $cust_main->country,
84           '</TD></TR>',
85   ;
86   my $daytime_label = FS::Msgcat::_gettext('daytime') || 'Day Phone';
87   my $night_label = FS::Msgcat::_gettext('night') || 'Night Phone';
88   print '<TR><TD ALIGN="right">'. $daytime_label.
89           '</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
90           $cust_main->daytime || '&nbsp', '</TD></TR>',
91         '<TR><TD ALIGN="right">'. $night_label. 
92           '</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
93           $cust_main->night || '&nbsp', '</TD></TR>',
94         '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
95           $cust_main->fax || '&nbsp', '</TD></TR>',
96         '</TABLE>', "</TD></TR></TABLE>"
97   ;
98
99   if ( defined $cust_main->dbdef_table->column('ship_last') ) {
100
101     my $pre = $cust_main->ship_last ? 'ship_' : '';
102
103     print "<BR>Service address", &ntable("#cccccc"), "<TR><TD>",
104           &ntable("#cccccc",2),
105       '<TR><TD ALIGN="right">Contact name</TD>',
106         '<TD COLSPAN=5 BGCOLOR="#ffffff">',
107         $cust_main->get("${pre}last"), ', ', $cust_main->get("${pre}first"),
108         '</TD></TR>',
109       '<TR><TD ALIGN="right">Company</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
110         $cust_main->get("${pre}company"),
111         '</TD></TR>',
112       '<TR><TD ALIGN="right">Address</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
113         $cust_main->get("${pre}address1"),
114         '</TD></TR>',
115     ;
116     print '<TR><TD ALIGN="right">&nbsp;</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
117           $cust_main->get("${pre}address2"), '</TD></TR>'
118       if $cust_main->get("${pre}address2");
119     print '<TR><TD ALIGN="right">City</TD><TD BGCOLOR="#ffffff">',
120             $cust_main->get("${pre}city"),
121             '</TD><TD ALIGN="right">State</TD><TD BGCOLOR="#ffffff">',
122             $cust_main->get("${pre}state"),
123             '</TD><TD ALIGN="right">Zip</TD><TD BGCOLOR="#ffffff">',
124             $cust_main->get("${pre}zip"), '</TD></TR>',
125           '<TR><TD ALIGN="right">Country</TD><TD BGCOLOR="#ffffff">',
126             $cust_main->get("${pre}country"),
127             '</TD></TR>',
128     ;
129     print '<TR><TD ALIGN="right">'. $daytime_label. '</TD>',
130           '<TD COLSPAN=5 BGCOLOR="#ffffff">',
131             $cust_main->get("${pre}daytime") || '&nbsp', '</TD></TR>',
132           '<TR><TD ALIGN="right">'. $night_label. '</TD>'.
133           '<TD COLSPAN=5 BGCOLOR="#ffffff">',
134             $cust_main->get("${pre}night") || '&nbsp', '</TD></TR>',
135           '<TR><TD ALIGN="right">Fax</TD><TD COLSPAN=5 BGCOLOR="#ffffff">',
136             $cust_main->get("${pre}fax") || '&nbsp', '</TD></TR>',
137           '</TABLE>', "</TD></TR></TABLE>"
138     ;
139
140   }
141
142 print '</TD>';
143
144 print '<TD VALIGN="top">';
145
146   print &ntable("#cccccc"), "<TR><TD>", &ntable("#cccccc",2),
147         '<TR><TD ALIGN="right">Customer number</TD><TD BGCOLOR="#ffffff">',
148         $custnum, '</TD></TR>',
149   ;
150
151   my @agents = qsearch( 'agent', {} );
152   my $agent;
153   unless ( scalar(@agents) == 1 ) {
154     $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );
155     print '<TR><TD ALIGN="right">Agent</TD><TD BGCOLOR="#ffffff">',
156         $agent->agentnum, ": ", $agent->agent, '</TD></TR>';
157   } else {
158     $agent = $agents[0];
159   }
160   my @referrals = qsearch( 'part_referral', {} );
161   unless ( scalar(@referrals) == 1 ) {
162     my $referral = qsearchs('part_referral', {
163       'refnum' => $cust_main->refnum
164     } );
165     print '<TR><TD ALIGN="right">Advertising source</TD><TD BGCOLOR="#ffffff">',
166           $referral->refnum, ": ", $referral->referral, '</TD></TR>';
167   }
168   print '<TR><TD ALIGN="right">Order taker</TD><TD BGCOLOR="#ffffff">',
169     $cust_main->otaker, '</TD></TR>';
170
171   print '<TR><TD ALIGN="right">Referring Customer</TD><TD BGCOLOR="#ffffff">';
172   my $referring_cust_main = '';
173   if ( $cust_main->referral_custnum
174        && ( $referring_cust_main =
175             qsearchs('cust_main', { custnum => $cust_main->referral_custnum } )
176           )
177      ) {
178     print '<A HREF="'. popurl(1). 'cust_main.cgi?'.
179           $cust_main->referral_custnum. '">'.
180           $cust_main->referral_custnum. ': '.
181           ( $referring_cust_main->company
182               ? $referring_cust_main->company. ' ('.
183                   $referring_cust_main->last. ', '. $referring_cust_main->first.
184                   ')'
185               : $referring_cust_main->last. ', '. $referring_cust_main->first
186           ).
187           '</A>';
188   }
189   print '</TD></TR>';
190
191   print '</TABLE></TD></TR></TABLE>';
192
193 print '<BR>';
194
195 if ( $conf->config('payby-default') ne 'HIDE' ) {
196
197   my @invoicing_list = $cust_main->invoicing_list;
198   print "Billing information (",
199        qq!<A HREF="!, popurl(2), qq!misc/bill.cgi?$custnum">!, "Bill now</A>)",
200         &ntable("#cccccc"), "<TR><TD>", &ntable("#cccccc",2),
201         '<TR><TD ALIGN="right">Tax exempt</TD><TD BGCOLOR="#ffffff">',
202         $cust_main->tax ? 'yes' : 'no',
203         '</TD></TR>',
204         '<TR><TD ALIGN="right">Postal invoices</TD><TD BGCOLOR="#ffffff">',
205         ( grep { $_ eq 'POST' } @invoicing_list ) ? 'yes' : 'no',
206         '</TD></TR>',
207         '<TR><TD ALIGN="right">Email invoices</TD><TD BGCOLOR="#ffffff">',
208         join(', ', grep { $_ ne 'POST' } @invoicing_list ) || 'no',
209         '</TD></TR>',
210         '<TR><TD ALIGN="right">Billing type</TD><TD BGCOLOR="#ffffff">',
211   ;
212
213   if ( $cust_main->payby eq 'CARD' ) {
214     my $payinfo = $cust_main->payinfo;
215     $payinfo = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
216     print 'Credit card</TD></TR>',
217           '<TR><TD ALIGN="right">Card number</TD><TD BGCOLOR="#ffffff">',
218           $payinfo, '</TD></TR>',
219           '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
220           $cust_main->paydate, '</TD></TR>',
221           '<TR><TD ALIGN="right">Name on card</TD><TD BGCOLOR="#ffffff">',
222           $cust_main->payname, '</TD></TR>'
223     ;
224   } elsif ( $cust_main->payby eq 'CHEK' ) {
225     my( $account, $aba ) = split('@', $cust_main->payinfo );
226     print 'Electronic check</TD></TR>',
227           '<TR><TD ALIGN="right">Account number</TD><TD BGCOLOR="#ffffff">',
228           $account, '</TD></TR>',
229           '<TR><TD ALIGN="right">ABA/Routing code</TD><TD BGCOLOR="#ffffff">',
230           $aba, '</TD></TR>',
231           '<TR><TD ALIGN="right">Bank name</TD><TD BGCOLOR="#ffffff">',
232           $cust_main->payname, '</TD></TR>'
233     ;
234   } elsif ( $cust_main->payby eq 'BILL' ) {
235     print 'Billing</TD></TR>';
236     print '<TR><TD ALIGN="right">P.O. </TD><TD BGCOLOR="#ffffff">',
237           $cust_main->payinfo, '</TD></TR>',
238       if $cust_main->payinfo;
239     print '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
240           $cust_main->paydate, '</TD></TR>',
241           '<TR><TD ALIGN="right">Attention</TD><TD BGCOLOR="#ffffff">',
242           $cust_main->payname, '</TD></TR>',
243     ;
244   } elsif ( $cust_main->payby eq 'COMP' ) {
245     print 'Complimentary</TD></TR>',
246           '<TR><TD ALIGN="right">Authorized by</TD><TD BGCOLOR="#ffffff">',
247           $cust_main->payinfo, '</TD></TR>',
248           '<TR><TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">',
249           $cust_main->paydate, '</TD></TR>',
250     ;
251   }
252
253   print "</TABLE></TD></TR></TABLE>";
254
255 }
256
257 print '</TD></TR></TABLE>';
258
259 if ( defined $cust_main->dbdef_table->column('comments')
260      && $cust_main->comments )
261 {
262   print "<BR>Comments". &ntable("#cccccc"). "<TR><TD>".
263         &ntable("#cccccc",2).
264         '<TR><TD BGCOLOR="#ffffff"><PRE>'.
265         encode_entities($cust_main->comments).
266         '</PRE></TD></TR></TABLE></TABLE>';
267 }
268
269 print '</TD></TR></TABLE>';
270
271 print '<BR>'.
272   '<FORM ACTION="'.popurl(2).'edit/process/quick-cust_pkg.cgi" METHOD="POST">'.
273   qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!.
274   '<SELECT NAME="pkgpart"><OPTION> ';
275
276 foreach my $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
277   my $pkgpart = $type_pkgs->pkgpart;
278 #  my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } )
279 #    or do { warn "unknown type_pkgs.pkgpart $pkgpart"; next; };
280   my $part_pkg =
281     qsearchs('part_pkg', { 'pkgpart' => $pkgpart, 'disabled' => '' } )
282     or next;
283   print qq!<OPTION VALUE="$pkgpart">!. $part_pkg->pkg. ' - '.
284         $part_pkg->comment;
285 }
286
287 print '</SELECT><INPUT TYPE="submit" VALUE="Order Package"></FORM><BR>';
288
289 if ( $conf->config('payby-default') ne 'HIDE' ) {
290
291   print '<BR>'.
292     qq!<FORM ACTION="${p}edit/process/quick-charge.cgi" METHOD="POST">!.
293     qq!<INPUT TYPE="hidden" NAME="custnum" VALUE="$custnum">!.
294     qq!Description:<INPUT TYPE="text" NAME="pkg">!.
295     qq!&nbsp;Amount:<INPUT TYPE="text" NAME="amount" SIZE=6>!.
296     qq!&nbsp;!;
297   
298   #false laziness w/ edit/part_pkg.cgi
299   if ( $conf->exists('enable_taxclasses') ) {
300     print '<SELECT NAME="taxclass">';
301     my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
302       or die dbh->errstr;
303     $sth->execute or die $sth->errstr;
304     foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
305       print qq!<OPTION VALUE="$taxclass"!;
306       #print ' SELECTED' if $taxclass eq $hashref->{taxclass};
307       print qq!>$taxclass</OPTION>!;
308     }
309     print '</SELECT>';
310   } else {
311     print '<INPUT TYPE="hidden" NAME="taxclass" VALUE="">';
312   }
313   
314   print qq!<INPUT TYPE="submit" VALUE="One-time charge"></FORM><BR>!;
315
316 }
317
318 print <<END;
319 <SCRIPT>
320 function cust_pkg_areyousure(href) {
321     if (confirm("Permanently delete included services and cancel this package?") == true)
322         window.location.href = href;
323 }
324 function svc_areyousure(href) {
325     if (confirm("Permanently unprovision and delete this service?") == true)
326         window.location.href = href;
327 }
328 </SCRIPT>
329 END
330
331 print qq!<BR><A NAME="cust_pkg">Packages</A> !,
332 #      qq!<BR>Click on package number to view/edit package.!,
333       qq!( <A HREF="!, popurl(2), qq!edit/cust_pkg.cgi?$custnum">Order and cancel packages</A> (preserves services) )!,
334 ;
335
336 #display packages
337
338 #formatting
339 print qq!!, &table(), "\n",
340       qq!<TR><TH COLSPAN=2 ROWSPAN=2>Package</TH><TH COLSPAN=5>!,
341       qq!Dates</TH><TH COLSPAN=2 ROWSPAN=2>Services</TH></TR>\n!,
342       qq!<TR><TH><FONT SIZE=-1>Setup</FONT></TH><TH>!,
343       qq!<FONT SIZE=-1>Next bill</FONT>!,
344       qq!</TH><TH><FONT SIZE=-1>Susp.</FONT></TH><TH><FONT SIZE=-1>Expire!,
345       qq!</FONT></TH>!,
346       qq!<TH><FONT SIZE=-1>Cancel</FONT></TH>!,
347       qq!</TR>\n!;
348
349 #get package info
350 my @packages;
351 if ( $conf->exists('hidecancelledpackages') ) {
352   @packages = sort { $a->pkgnum <=> $b->pkgnum } ($cust_main->ncancelled_pkgs);
353 } else {
354   @packages = sort { $a->pkgnum <=> $b->pkgnum } ($cust_main->all_pkgs);
355 }
356
357 my $n1 = '<TR>';
358 foreach my $package (@packages) {
359   my $pkgnum = $package->pkgnum;
360   my $pkg = $package->part_pkg->pkg;
361   my $comment = $package->part_pkg->comment;
362   my $pkgview = popurl(2). "view/cust_pkg.cgi?$pkgnum";
363
364   #my @cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
365   #my $rowspan = scalar(@cust_svc) || 1;
366   my @cust_svc = ();
367   my $rowspan = 0;
368   my %pkg_svc = ();
369   unless ( $package->getfield('cancel') ) {
370     foreach my $pkg_svc (
371       grep { $_->quantity }
372         qsearch('pkg_svc',{'pkgpart'=> $package->pkgpart })
373     ) {
374       $rowspan += ( $pkg_svc{$pkg_svc->svcpart} = $pkg_svc->quantity );
375     }
376   } else {
377     #@cust_svc = qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } );
378     @cust_svc = ();
379     $rowspan = scalar(@cust_svc) || 1;
380   }
381   $rowspan ||= 1;
382
383   my $button_cgi = new CGI;
384   $button_cgi->param('clone', $package->part_pkg->pkgpart);
385   $button_cgi->param('pkgnum', $package->pkgnum);
386   my $button_url = popurl(2). "edit/part_pkg.cgi?". $button_cgi->query_string;
387
388   #print $n1, qq!<TD ROWSPAN=$rowspan><A HREF="$pkgview">$pkgnum</A></TD>!,
389   print $n1, qq!<TD ROWSPAN=$rowspan>$pkgnum</TD>!,
390         qq!<TD ROWSPAN=$rowspan><FONT SIZE=-1>!,
391         #qq!<A HREF="$pkgview">$pkg - $comment</A>!,
392         qq!$pkg - $comment (&nbsp;<a href="$pkgview">Details</a>&nbsp;)!;
393        # | !;
394
395   print qq! (&nbsp;<A HREF="${p}misc/change_pkg.cgi?$pkgnum">!.
396         'Change&nbsp;package</A>&nbsp;)';
397
398   #false laziness with view/cust_pkg.cgi, but i'm trying to make that go away so
399   unless ( $package->getfield('cancel') ) {
400     print ' (&nbsp;';
401     if ( $package->getfield('susp') ) {
402       print qq!<A HREF="${p}misc/unsusp_pkg.cgi?$pkgnum">Unsuspend</A>!;
403     } else {
404       print qq!<A HREF="${p}misc/susp_pkg.cgi?$pkgnum">Suspend</A>!;
405     }
406     print '&nbsp;|&nbsp;<A HREF="javascript:cust_pkg_areyousure(\''. popurl(2).
407           'misc/cancel_pkg.cgi?'. $pkgnum.  '\')">Cancel</A>';
408   
409     print '&nbsp;) ';
410
411     print ' (&nbsp;<A HREF="'. popurl(2). 'edit/REAL_cust_pkg.cgi?'. $pkgnum.
412           '">Edit&nbsp;dates</A>&nbsp;|&nbsp;';
413         
414     print qq!<A HREF="$button_url">Customize</A>&nbsp;)!;
415
416   }
417   print '</FONT></TD>';
418
419   for ( qw( setup bill susp expire cancel ) ) {
420     print "<TD ROWSPAN=$rowspan><FONT SIZE=-1>", ( $package->getfield($_)
421             ? time2str("%D</FONT><BR><FONT SIZE=-3>%l:%M:%S%P&nbsp;%z</FONT>",
422               $package->getfield($_) )
423             :  '&nbsp'
424           ), '</FONT></TD>',
425     ;
426   }
427
428   my $n2 = '';
429   #false laziness with view/cust_pkg.cgi, but i'm trying to make that go away so
430   #foreach my $cust_svc ( @cust_svc ) {
431   foreach my $svcpart ( sort { $a<=>$b } keys %pkg_svc ) {
432     my $svc = qsearchs('part_svc',{'svcpart'=>$svcpart})->getfield('svc');
433     $svc =~ s/ /&nbsp;/g;
434     my(@cust_svc)=qsearch('cust_svc',{'pkgnum'=>$pkgnum, 
435                                       'svcpart'=>$svcpart,
436                                     });
437     for my $enum ( 1 .. $pkg_svc{$svcpart} ) {
438       my $cust_svc;
439       if ( $cust_svc = shift @cust_svc ) {
440         my($label, $value, $svcdb) = $cust_svc->label;
441         my($svcnum) = $cust_svc->svcnum;
442         my($sview) = popurl(2). "view";
443         print $n2,qq!<TD><A HREF="$sview/$svcdb.cgi?$svcnum"><FONT SIZE=-1>$label</FONT></A></TD>!,
444               qq!<TD><FONT SIZE=-1><A HREF="$sview/$svcdb.cgi?$svcnum">$value</A><BR>(&nbsp;<A HREF="javascript:svc_areyousure('${p}misc/unprovision.cgi?$svcnum')">Unprovision</A>&nbsp;)</FONT></TD>!;
445       } else {
446         print $n2, qq!<TD COLSPAN=2><A HREF="$uiadd{$svcpart}?pkgnum$pkgnum-svcpart$svcpart"><b><font size="+1" color="#ff0000">!.
447               qq!Provision&nbsp;$svc</A></b></font>!;
448
449         print qq!<BR><A HREF="../misc/link.cgi?pkgnum$pkgnum-svcpart$svcpart">!.
450               qq!<b><font size="+1" color="#ff0000">Link&nbsp;to&nbsp;legacy&nbsp;$svc</A></b></font>!
451           if $conf->exists('legacy_link');
452
453         print '</TD>';
454       }
455       $n2="</TR><TR>";
456     }
457   }
458
459   $n1="</TR><TR>";
460 }  
461 print "</TR>";
462
463 #formatting
464 print "</TABLE>";
465
466 print <<END;
467 <SCRIPT>
468 function cust_pay_areyousure(href) {
469     if (confirm("Are you sure you want to delete this payment?")
470  == true)
471         window.location.href = href;
472 }
473 </SCRIPT>
474 END
475
476 if ( $conf->config('payby-default') ne 'HIDE' ) {
477   
478   #formatting
479   print qq!<BR><BR><A NAME="history">Payment History!.
480         qq!</A> ( !.
481         qq!<A HREF="!. popurl(2). qq!edit/cust_pay.cgi?custnum=$custnum">!.
482         qq!Post payment</A> | !.
483         qq!<A HREF="!. popurl(2). qq!edit/cust_credit.cgi?$custnum">!.
484         qq!Post credit</A> )!;
485   
486   #get payment history
487   #
488   # major problem: this whole thing is way too sloppy.
489   # minor problem: the description lines need better formatting.
490   
491   my @history = (); #needed for mod_perl :)
492   
493   my %target = ();
494   
495   my @bills = qsearch('cust_bill',{'custnum'=>$custnum});
496   foreach my $bill (@bills) {
497     my($bref)=$bill->hashref;
498     my $bpre = ( $bill->owed > 0 )
499                  ? '<b><font size="+1" color="#ff0000"> Open '
500                  : '';
501     my $bpost = ( $bill->owed > 0 ) ? '</font></b>' : '';
502     push @history,
503       $bref->{_date} . qq!\t<A HREF="!. popurl(2). qq!view/cust_bill.cgi?! .
504       $bref->{invnum} . qq!">${bpre}Invoice #! . $bref->{invnum} .
505       qq! (Balance \$! . $bill->owed . qq!)$bpost</A>\t! .
506       $bref->{charged} . qq!\t\t\t!;
507   
508     my(@cust_bill_pay)=qsearch('cust_bill_pay',{'invnum'=> $bref->{invnum} } );
509   #  my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } );
510   #  my($payment);
511     foreach my $cust_bill_pay (@cust_bill_pay) {
512       my $payment = $cust_bill_pay->cust_pay;
513       my($date,$invnum,$payby,$payinfo,$paid)=($payment->_date,
514                                                $cust_bill_pay->invnum,
515                                                $payment->payby,
516                                                $payment->payinfo,
517                                                $cust_bill_pay->amount,
518                         );
519       $payinfo = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4))
520         if $payby eq 'CARD';
521       my $target = "$payby$payinfo";
522       $payby =~ s/^BILL$/Check #/ if $payinfo;
523       $payby =~ s/^(CARD|COMP)$/$1 /;
524       my $delete = $payment->closed !~ /^Y/i && $conf->exists('deletepayments')
525                      ? qq! (<A HREF="javascript:cust_pay_areyousure('${p}misc/delete-cust_pay.cgi?!. $payment->paynum. qq!')">delete</A>)!
526                      : '';
527       push @history,
528         "$date\tPayment, Invoice #$invnum ($payby$payinfo)$delete\t\t$paid\t\t\t$target";
529     }
530   
531     my(@cust_credit_bill)=
532       qsearch('cust_credit_bill', { 'invnum'=> $bref->{invnum} } );
533     foreach my $cust_credit_bill (@cust_credit_bill) {
534       my $cust_credit = $cust_credit_bill->cust_credit;
535       my($date, $invnum, $crednum, $amount, $reason, $app_date ) = (
536         $cust_credit->_date,
537         $cust_credit_bill->invnum,
538         $cust_credit_bill->crednum,
539         $cust_credit_bill->amount,
540         $cust_credit->reason,
541         time2str("%D", $cust_credit_bill->_date),
542       );
543       push @history,
544         "$date\tCredit #$crednum: $reason<BR>".
545         "(applied to invoice #$invnum on $app_date)\t\t\t$amount\t";
546     }
547   }
548   
549   my @credits = grep { scalar(my @array = $_->cust_credit_refund) }
550              qsearch('cust_credit',{'custnum'=>$custnum});
551   foreach my $credit (@credits) {
552     my($cref)=$credit->hashref;
553     my(@cust_credit_refund)=
554       qsearch('cust_credit_refund', { 'crednum'=> $cref->{crednum} } );
555     foreach my $cust_credit_refund (@cust_credit_refund) {
556       my $cust_refund = $cust_credit_refund->cust_credit;
557       my($date, $crednum, $amount, $reason, $app_date ) = (
558         $credit->_date,
559         $credit->crednum,
560         $cust_credit_refund->amount,
561         $credit->reason,
562         time2str("%D", $cust_credit_refund->_date),
563       );
564       push @history,
565         "$date\tCredit #$crednum: $reason<BR>".
566         "(applied to refund on $app_date)\t\t\t$amount\t";
567     }
568   }
569   
570   @credits = grep { $_->credited  > 0 }
571              qsearch('cust_credit',{'custnum'=>$custnum});
572   foreach my $credit (@credits) {
573     my($cref)=$credit->hashref;
574     push @history,
575       $cref->{_date} . "\t" .
576       qq!<A HREF="! . popurl(2). qq!edit/cust_credit_bill.cgi?!. $cref->{crednum} . qq!">!.
577       '<b><font size="+1" color="#ff0000">Unapplied credit #' .
578       $cref->{crednum} . "</font></b></A>: ".
579       $cref->{reason} . "\t\t\t" . $credit->credited . "\t";
580   }
581   
582   my(@refunds)=qsearch('cust_refund',{'custnum'=> $custnum } );
583   foreach my $refund (@refunds) {
584     my($rref)=$refund->hashref;
585     my($refundnum) = (
586       $refund->refundnum,
587     );
588   
589     push @history,
590       $rref->{_date} . "\tRefund #$refundnum, (" .
591       $rref->{payby} . " " . $rref->{payinfo} . ") by " .
592       $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" .
593       $rref->{refund};
594   }
595   
596   my @unapplied_payments =
597     grep { $_->unapplied > 0 } qsearch('cust_pay', { 'custnum' => $custnum } );
598   foreach my $payment (@unapplied_payments) {
599     my $payby = $payment->payby;
600     my $payinfo = $payment->payinfo;
601     #false laziness w/above
602     $payinfo = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4))
603       if $payby eq 'CARD';
604     my $target = "$payby$payinfo";
605     $payby =~ s/^BILL$/Check #/ if $payinfo;
606     $payby =~ s/^(CARD|COMP)$/$1 /;
607     my $delete = $payment->closed !~ /^Y/i && $conf->exists('deletepayments')
608                    ? qq! (<A HREF="javascript:cust_pay_areyousure('${p}misc/delete-cust_pay.cgi?!. $payment->paynum. qq!')">delete</A>)!
609                    : '';
610     push @history,
611       $payment->_date. "\t".
612       '<b><font size="+1" color="#ff0000">Unapplied payment #' .
613       $payment->paynum . " ($payby$payinfo)</font></b> ".
614       '(<A HREF="'. popurl(2). 'edit/cust_bill_pay.cgi?'. $payment->paynum. '">'.
615       "apply</A>)$delete".
616       "\t\t" . $payment->unapplied . "\t\t\t$target";
617   }
618   
619           #formatting
620           print &table(), <<END;
621   <TR>
622     <TH>Date</TH>
623     <TH>Description</TH>
624     <TH><FONT SIZE=-1>Charge</FONT></TH>
625     <TH><FONT SIZE=-1>Payment</FONT></TH>
626     <TH><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
627     <TH><FONT SIZE=-1>Refund</FONT></TH>
628     <TH><FONT SIZE=-1>Balance</FONT></TH>
629   </TR>
630 END
631   
632   #display payment history
633   
634   my $balance = 0;
635   foreach my $item (sort keyfield_numerically @history) {
636     my($date,$desc,$charge,$payment,$credit,$refund,$target)=split(/\t/,$item);
637     $charge ||= 0;
638     $payment ||= 0;
639     $credit ||= 0;
640     $refund ||= 0;
641     $balance += $charge - $payment;
642     $balance -= $credit - $refund;
643     $balance = sprintf("%.2f", $balance);
644     $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
645     $target = '' unless defined $target;
646   
647     print "<TR><TD><FONT SIZE=-1>";
648     print qq!<A NAME="$target">! unless $target && $target{$target}++;
649     print time2str("%D",$date);
650     print '</A>' if $target && $target{$target} == 1;
651     print "</FONT></TD>",
652         "<TD><FONT SIZE=-1>$desc</FONT></TD>",
653         "<TD><FONT SIZE=-1>",
654           ( $charge ? "\$".sprintf("%.2f",$charge) : '' ),
655           "</FONT></TD>",
656         "<TD><FONT SIZE=-1>",
657           ( $payment ? "-&nbsp;\$".sprintf("%.2f",$payment) : '' ),
658           "</FONT></TD>",
659         "<TD><FONT SIZE=-1>",
660           ( $credit ? "-&nbsp;\$".sprintf("%.2f",$credit) : '' ),
661           "</FONT></TD>",
662         "<TD><FONT SIZE=-1>",
663           ( $refund ? "\$".sprintf("%.2f",$refund) : '' ),
664           "</FONT></TD>",
665         "<TD><FONT SIZE=-1>\$" . $balance,
666           "</FONT></TD>",
667           "\n";
668   }
669   
670   print "</TABLE>";
671
672 }
673
674 print '</BODY></HTML>';
675
676 #subroutiens
677 sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0]; }
678
679 %>