#!/usr/bin/perl -Tw # # cust_main.cgi: View a customer # # Usage: cust_main.cgi custnum # http://server.name/path/cust_main.cgi?custnum # # Note: Should be run setuid freeside as user nobody. # # the payment history section could use some work, see below # # ivan@voicenet.com 96-nov-29 -> 96-dec-11 # # added navigation bar (go to main menu ;) # ivan@voicenet.com 97-jan-30 # # changes to the way credits/payments are applied (the links are here). # ivan@voicenet.com 97-apr-21 # # added debugging code to diagnose CPU sucking problem. # ivan@voicenet.com 97-may-19 # # CPU sucking problem was in comment code? fixed? # ivan@voicenet.com 97-may-22 # # rewrote for new API # ivan@voicenet.com 97-jul-22 # # Changes to allow page to work at a relative position in server # Changed 'day' to 'daytime' because Pg6.3 reserves the day word # bmccane@maxbaud.net 98-apr-3 # # lose background, FS::CGI ivan@sisd.com 98-sep-2 use strict; use CGI::Base qw(:DEFAULT :CGI); # CGI module use CGI::Carp qw(fatalsToBrowser); use Date::Format; use FS::UID qw(cgisuidsetup); use FS::Record qw(qsearchs qsearch); use FS::CGI qw(header menubar); my($cgi) = new CGI::Base; $cgi->get; &cgisuidsetup($cgi); SendHeaders(); # one guess. print header("Customer View", menubar( 'Main Menu' => '../', )),< END #untaint custnum & get customer record $QUERY_STRING =~ /^(\d+)$/; my($custnum)=$1; my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum}); die "Customer not found!" unless $cust_main; my($hashref)=$cust_main->hashref; #custnum print "
Customer #$custnum
", qq!
Customer Information | !, qq!Comments | !, qq!Packages | !, qq!Payment History
!; #bill now linke print qq!
!, qq!Bill this customer now
!; #formatting print qq!
Customer Information!, qq!!, qq!
Edit this information
!; #agentnum my($agent)=qsearchs('agent',{ 'agentnum' => $cust_main->getfield('agentnum') } ); die "Agent not found!" unless $agent; print "
Agent #" , $agent->getfield('agentnum') , ": " , $agent->getfield('agent') , ""; #refnum my($referral)=qsearchs('part_referral',{'refnum' => $cust_main->refnum}); die "Referral not found!" unless $referral; print "
Referral #", $referral->refnum, ": ", $referral->referral, "<\B>"; #last, first print "

", $hashref->{'last'}, ", ", $hashref->{first}, ""; #ss print " (SS# ", $hashref->{ss}, ")" if $hashref->{ss}; #company print "
", $hashref->{company}, "" if $hashref->{company}; #address1 print "
", $hashref->{address1}, ""; #address2 print "
", $hashref->{address2}, "" if $hashref->{address2}; #city print "
", $hashref->{city}, ""; #county print " (", $hashref->{county}, " county)" if $hashref->{county}; #state print ",", $hashref->{state}, ""; #zip print " ", $hashref->{zip}, ""; #country print "
", $hashref->{country}, "" unless $hashref->{country} eq "US"; #daytime print "

", $hashref->{daytime}, "" if $hashref->{daytime}; print " (Day)" if $hashref->{daytime} && $hashref->{night}; #night print "
", $hashref->{night}, "" if $hashref->{night}; print " (Night)" if $hashref->{daytime} && $hashref->{night}; #fax print "
", $hashref->{fax}, " (Fax)" if $hashref->{fax}; #payby/payinfo/paydate/payname if ($hashref->{payby} eq "CARD") { print "

Card #", $hashref->{payinfo}, " Exp. ", $hashref->{paydate}, ""; print " (", $hashref->{payname}, ")" if $hashref->{payname}; } elsif ($hashref->{payby} eq "BILL") { print "

Bill"; print " on P.O. #", $hashref->{payinfo}, "" if $hashref->{payinfo}; print " until ", $hashref->{paydate}, "" if $hashref->{paydate}; print " to ", $hashref->{payname}, " at above address" if $hashref->{payname}; } elsif ($hashref->{payby} eq "COMP") { print "

Access complimentary"; print " courtesy of ", $hashref->{payinfo}, "" if $hashref->{payinfo}; print " until ", $hashref->{paydate}, "" if $hashref->{paydate}; } else { print "Unknown payment type ", $hashref->{payby}, "!"; } #tax print "
(Tax exempt)" if $hashref->{tax}; #otaker print "

Order taken by ", $hashref->{otaker}, ""; #formatting print qq!


Packages!, qq!
Click on package number to view/edit package.!, qq!
Add/Edit packages!, qq!

!; #display packages #formatting print qq!
\n!, qq!\n!, qq!!, qq!!, qq!\n!; #get package info my(@packages)=qsearch('cust_pkg',{'custnum'=>$custnum}); my($package); foreach $package (@packages) { my($pref)=$package->hashref; my($part_pkg)=qsearchs('part_pkg',{ 'pkgpart' => $pref->{pkgpart} } ); print qq!!, "", "", "", "", "", "", ""; } #formatting print "
#Package!, qq!Dates
Setup!, qq!Next bill!, qq!Susp.Expire!, qq!Cancel
!, $pref->{pkgnum}, qq!", $part_pkg->getfield('pkg'), " - ", $part_pkg->getfield('comment'), "", $pref->{setup} ? time2str("%D",$pref->{setup} ) : "" , "", $pref->{bill} ? time2str("%D",$pref->{bill} ) : "" , "", $pref->{susp} ? time2str("%D",$pref->{susp} ) : "" , "", $pref->{expire} ? time2str("%D",$pref->{expire} ) : "" , "", $pref->{cancel} ? time2str("%D",$pref->{cancel} ) : "" , "
"; #formatting print qq!

Payment History!, qq!
!, qq!Click on invoice to view invoice/enter payment.
!, qq!!, qq!Post Credit / Refund

!; #get payment history # # major problem: this whole thing is way too sloppy. # minor problem: the description lines need better formatting. my(@history); my(@bills)=qsearch('cust_bill',{'custnum'=>$custnum}); my($bill); foreach $bill (@bills) { my($bref)=$bill->hashref; push @history, $bref->{_date} . qq!\tInvoice #! . $bref->{invnum} . qq! (Balance \$! . $bref->{owed} . qq!)\t! . $bref->{charged} . qq!\t\t\t!; my(@payments)=qsearch('cust_pay',{'invnum'=> $bref->{invnum} } ); my($payment); foreach $payment (@payments) { # my($pref)=$payment->hashref; my($date,$invnum,$payby,$payinfo,$paid)=($payment->getfield('_date'), $payment->getfield('invnum'), $payment->getfield('payby'), $payment->getfield('payinfo'), $payment->getfield('paid'), ); push @history, "$date\tPayment, Invoice #$invnum ($payby $payinfo)\t\t$paid\t\t"; } } my(@credits)=qsearch('cust_credit',{'custnum'=>$custnum}); my($credit); foreach $credit (@credits) { my($cref)=$credit->hashref; push @history, $cref->{_date} . "\tCredit #" . $cref->{crednum} . ", (Balance \$" . $cref->{credited} . ") by " . $cref->{otaker} . " - " . $cref->{reason} . "\t\t\t" . $cref->{amount} . "\t"; my(@refunds)=qsearch('cust_refund',{'crednum'=> $cref->{crednum} } ); my($refund); foreach $refund (@refunds) { my($rref)=$refund->hashref; push @history, $rref->{_date} . "\tRefund, Credit #" . $rref->{crednum} . " (" . $rref->{payby} . " " . $rref->{payinfo} . ") by " . $rref->{otaker} . " - ". $rref->{reason} . "\t\t\t\t" . $rref->{refund}; } } #formatting print < END #display payment history my($balance)=0; my($item); foreach $item (sort keyfield_numerically @history) { my($date,$desc,$charge,$payment,$credit,$refund)=split(/\t/,$item); $charge ||= 0; $payment ||= 0; $credit ||= 0; $refund ||= 0; $balance += $charge - $payment; $balance -= $credit - $refund; print "", "", "", "", "", "", "", "\n"; } #formatting print "
Date Description Charge Payment In-house
Credit
Refund Balance
",time2str("%D",$date),"$desc", ( $charge ? "\$".sprintf("%.2f",$charge) : '' ), "", ( $payment ? "- \$".sprintf("%.2f",$payment) : '' ), "", ( $credit ? "- \$".sprintf("%.2f",$credit) : '' ), "", ( $refund ? "\$".sprintf("%.2f",$refund) : '' ), "\$" . sprintf("%.2f",$balance), "
"; #end #formatting print < END #subroutiens sub keyfield_numerically { (split(/\t/,$a))[0] <=> (split(/\t/,$b))[0] ; }