1 #this is so kludgy i'd be embarassed if it wasn't cybercash's fault
3 use vars qw($paymentserversecret $paymentserverport $paymentserverhost);
8 use vars qw(@ISA @EXPORT_OK $conf $lpr $processor $xaction $E_NoErr);
15 use Business::CreditCard;
16 use FS::UID qw(getotaker);
17 use FS::Record qw(fields hfields qsearchs qsearch);
20 use FS::cust_bill_pkg;
22 #use FS::cust_pay_batch;
24 @ISA = qw(FS::Record Exporter);
25 @EXPORT_OK = qw(hfields);
27 #ask FS::UID to run this stuff for us later
28 $FS::UID::callback{'FS::cust_main'} = sub {
30 $lpr = $conf->config('lpr');
32 if ( $conf->exists('cybercash3.2') ) {
34 #qw($MCKversion %Config InitConfig CCError CCDebug CCDebug2);
35 require CCMckDirectLib3_2;
37 require CCMckErrno3_2;
38 #qw(MCKGetErrorMessage $E_NoErr);
39 import CCMckErrno3_2 qw($E_NoErr);
42 ($merchant_conf,$xaction)= $conf->config('cybercash3.2');
43 my $status = &CCMckLib3_2::InitConfig($merchant_conf);
44 if ( $status != $E_NoErr ) {
45 warn "CCMckLib3_2::InitConfig error:\n";
46 foreach my $key (keys %CCMckLib3_2::Config) {
47 warn " $key => $CCMckLib3_2::Config{$key}\n"
49 my($errmsg) = &CCMckErrno3_2::MCKGetErrorMessage($status);
50 die "CCMckLib3_2::InitConfig fatal error: $errmsg\n";
52 $processor='cybercash3.2';
53 } elsif ( $conf->exists('cybercash2') ) {
56 ( $main::paymentserverhost,
57 $main::paymentserverport,
58 $main::paymentserversecret,
60 ) = $conf->config('cybercash2');
61 $processor='cybercash2';
67 FS::cust_main - Object methods for cust_main records
73 $record = create FS::cust_main \%hash;
74 $record = create FS::cust_main { 'column' => 'value' };
76 $error = $record->insert;
78 $error = $new_record->replace($old_record);
80 $error = $record->delete;
82 $error = $record->check;
84 @cust_pkg = $record->all_pkgs;
86 @cust_pkg = $record->ncancelled_pkgs;
88 $error = $record->bill;
89 $error = $record->bill %options;
90 $error = $record->bill 'time' => $time;
92 $error = $record->collect;
93 $error = $record->collect %options;
94 $error = $record->collect 'invoice_time' => $time,
95 'batch_card' => 'yes',
96 'report_badcard' => 'yes',
101 An FS::cust_main object represents a customer. FS::cust_main inherits from
102 FS::Record. The following fields are currently supported:
106 =item custnum - primary key (assigned automatically for new customers)
108 =item agentnum - agent (see L<FS::agent>)
110 =item refnum - referral (see L<FS::part_referral>)
116 =item ss - social security number (optional)
118 =item company - (optional)
122 =item address2 - (optional)
126 =item county - (optional, see L<FS::cust_main_county>)
128 =item state - (see L<FS::cust_main_county>)
132 =item country - (see L<FS::cust_main_county>)
134 =item daytime - phone (optional)
136 =item night - phone (optional)
138 =item payby - `CARD' (credit cards), `BILL' (billing), or `COMP' (free)
140 =item payinfo - card number, P.O.#, or comp issuer (4-8 lowercase alphanumerics; think username)
142 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
144 =item payname - name on card or billing name
146 =item tax - tax exempt, empty or `Y'
148 =item otaker - order taker (assigned automatically, see L<FS::UID>)
158 Creates a new customer. To add the customer to the database, see L<"insert">.
160 Note that this stores the hash reference, not a distinct copy of the hash it
161 points to. You can ask the object for a copy with the I<hash> method.
166 my($proto,$hashref)=@_;
168 #now in FS::Record::new
170 #foreach $field (fields('cust_main')) {
171 # $hashref->{$field}='' unless defined $hashref->{$field};
174 $proto->new('cust_main',$hashref);
179 Adds this customer to the database. If there is an error, returns the error,
180 otherwise returns false.
187 #no callbacks in check, only data checks
188 #local $SIG{HUP} = 'IGNORE';
189 #local $SIG{INT} = 'IGNORE';
190 #local $SIG{QUIT} = 'IGNORE';
191 #local $SIG{TERM} = 'IGNORE';
192 #local $SIG{TSTP} = 'IGNORE';
200 Currently unimplemented. Maybe cancel all of this customer's
203 I don't remove the customer record in the database because there would then
204 be no record the customer ever existed (which is bad, no?)
208 # Usage: $error = $record -> delete;
210 return "Can't (yet?) delete customers.";
216 =item replace OLD_RECORD
218 Replaces the OLD_RECORD with this one in the database. If there is an error,
219 returns the error, otherwise returns false.
225 return "(Old) Not a cust_main record!" unless $old->table eq "cust_main";
226 return "Can't change custnum!"
227 unless $old->getfield('custnum') eq $new->getfield('custnum');
234 Checks all fields to make sure this is a valid customer record. If there is
235 an error, returns the error, otherwise returns false. Called by the insert
243 return "Not a cust_main record!" unless $self->table eq "cust_main";
246 $self->ut_number('agentnum')
247 || $self->ut_number('refnum')
248 || $self->ut_textn('company')
249 || $self->ut_text('address1')
250 || $self->ut_textn('address2')
251 || $self->ut_text('city')
252 || $self->ut_textn('county')
253 || $self->ut_text('state')
254 || $self->ut_phonen('daytime')
255 || $self->ut_phonen('night')
256 || $self->ut_phonen('fax')
258 return $error if $error;
260 return "Unknown agent"
261 unless qsearchs('agent',{'agentnum'=>$self->agentnum});
263 return "Unknown referral"
264 unless qsearchs('part_referral',{'refnum'=>$self->refnum});
266 $self->getfield('last') =~ /^([\w \,\.\-\']+)$/ or return "Illegal last name";
267 $self->setfield('last',$1);
269 $self->first =~ /^([\w \,\.\-\']+)$/ or return "Illegal first name";
272 if ( $self->ss eq '' ) {
277 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
278 or return "Illegal social security number";
279 $self->ss("$1-$2-$3");
282 return "Unknown state/county/country"
283 unless qsearchs('cust_main_county',{
284 'state' => $self->state,
285 'county' => $self->county,
289 $self->zip =~ /^(\d{5}(-\d{4})?)$/ or return "Illegal zip";
293 $self->country =~ /^(US)$/ or return "Illegal country";
296 $self->payby =~ /^(CARD|BILL|COMP)$/ or return "Illegal payby";
299 if ( $self->payby eq 'CARD' ) {
301 my $payinfo = $self->payinfo;
303 $payinfo =~ /^(\d{13,16})$/
304 or return "Illegal credit card number";
306 $self->payinfo($payinfo);
307 validate($payinfo) or return "Illegal credit card number";
308 my $type = cardtype($payinfo);
309 return "Unknown credit card type"
310 unless ( $type =~ /^VISA/ ||
311 $type =~ /^MasterCard/ ||
312 $type =~ /^American Express/ ||
313 $type =~ /^Discover/ );
315 } elsif ( $self->payby eq 'BILL' ) {
317 $self->payinfo =~ /^([\w \-]*)$/ or return "Illegal P.O. number";
320 } elsif ( $self->payby eq 'COMP' ) {
322 $self->payinfo =~ /^(\w{2,8})$/ or return "Illegal comp account issuer";
327 if ( $self->paydate eq '' ) {
328 return "Expriation date required" unless $self->payby eq 'BILL';
331 $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/
332 or return "Illegal expiration date";
333 if ( length($2) == 4 ) {
334 $self->paydate("$2-$1-01");
335 } elsif ( $2 > 97 ) { #should pry change to check for "this year"
336 $self->paydate("19$2-$1-01");
338 $self->paydate("20$2-$1-01");
342 if ( $self->payname eq '' ) {
343 $self->payname( $self->first. " ". $self->getfield('last') );
345 $self->payname =~ /^([\w \,\.\-\']+)$/
346 or return "Illegal billing name";
350 $self->tax =~ /^(Y?)$/ or return "Illegal tax";
353 $self->otaker(getotaker);
360 Returns all packages (see L<FS::cust_pkg>) for this customer.
366 qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
369 =item ncancelled_pkgs
371 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
375 sub ncancelled_pkgs {
377 qsearch( 'cust_pkg', {
378 'custnum' => $self->custnum,
385 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
386 conjunction with the collect method.
388 The only currently available option is `time', which bills the customer as if
389 it were that time. It is specified as a UNIX timestamp; see
390 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
393 If there is an error, returns the error, otherwise returns false.
398 my($self,%options)=@_;
399 my($time) = $options{'time'} || $^T;
404 local $SIG{HUP} = 'IGNORE';
405 local $SIG{INT} = 'IGNORE';
406 local $SIG{QUIT} = 'IGNORE';
407 local $SIG{TERM} = 'IGNORE';
408 local $SIG{TSTP} = 'IGNORE';
410 # find the packages which are due for billing, find out how much they are
411 # & generate invoice database.
413 my($total_setup,$total_recur)=(0,0);
419 qsearch('cust_pkg',{'custnum'=> $self->getfield('custnum') } )
422 bless($cust_pkg,"FS::cust_pkg");
424 next if ( $cust_pkg->getfield('cancel') );
426 #? to avoid use of uninitialized value errors... ?
427 $cust_pkg->setfield('bill', '')
428 unless defined($cust_pkg->bill);
431 qsearchs('part_pkg',{'pkgpart'=> $cust_pkg->pkgpart } );
433 #so we don't modify cust_pkg record unnecessarily
434 my($cust_pkg_mod_flag)=0;
435 my(%hash)=$cust_pkg->hash;
436 my($old_cust_pkg)=create FS::cust_pkg(\%hash);
440 unless ( $cust_pkg->setup ) {
441 my($setup_prog)=$part_pkg->getfield('setup');
443 #$cpt->permit(); #what is necessary?
444 $cpt->share(qw($cust_pkg)); #can $cpt now use $cust_pkg methods?
445 $setup = $cpt->reval($setup_prog);
446 unless ( defined($setup) ) {
447 warn "Error reval-ing part_pkg->setup pkgpart ",
448 $part_pkg->pkgpart, ": $@";
450 $cust_pkg->setfield('setup',$time);
451 $cust_pkg_mod_flag=1;
458 if ( $part_pkg->getfield('freq') > 0 &&
459 ! $cust_pkg->getfield('susp') &&
460 ( $cust_pkg->getfield('bill') || 0 ) < $time
462 my($recur_prog)=$part_pkg->getfield('recur');
464 #$cpt->permit(); #what is necessary?
465 $cpt->share(qw($cust_pkg)); #can $cpt now use $cust_pkg methods?
466 $recur = $cpt->reval($recur_prog);
467 unless ( defined($recur) ) {
468 warn "Error reval-ing part_pkg->recur pkgpart ",
469 $part_pkg->pkgpart, ": $@";
471 #change this bit to use Date::Manip?
472 #$sdate=$cust_pkg->bill || time;
473 #$sdate=$cust_pkg->bill || $time;
474 $sdate=$cust_pkg->bill || $cust_pkg->setup || $time;
475 my($sec,$min,$hour,$mday,$mon,$year)=
476 (localtime($sdate) )[0,1,2,3,4,5];
477 $mon += $part_pkg->getfield('freq');
478 until ( $mon < 12 ) { $mon -= 12; $year++; }
479 $cust_pkg->setfield('bill',timelocal($sec,$min,$hour,$mday,$mon,$year));
480 $cust_pkg_mod_flag=1;
484 warn "setup is undefinded" unless defined($setup);
485 warn "recur is undefinded" unless defined($recur);
486 warn "cust_pkg bill is undefinded" unless defined($cust_pkg->bill);
488 if ($cust_pkg_mod_flag) {
489 $error=$cust_pkg->replace($old_cust_pkg);
491 warn "Error modifying pkgnum ", $cust_pkg->pkgnum, ": $error";
494 $setup=sprintf("%.2f",$setup);
495 $recur=sprintf("%.2f",$recur);
496 my($cust_bill_pkg)=create FS::cust_bill_pkg ({
497 'pkgnum' => $cust_pkg->pkgnum,
501 'edate' => $cust_pkg->bill,
503 push @cust_bill_pkg, $cust_bill_pkg;
504 $total_setup += $setup;
505 $total_recur += $recur;
511 my($charged)=sprintf("%.2f",$total_setup + $total_recur);
513 return '' if scalar(@cust_bill_pkg) == 0;
515 unless ( $self->getfield('tax') eq 'Y' ||
516 $self->getfield('tax') eq 'y' ||
517 $self->getfield('payby') eq 'COMP'
519 my($cust_main_county) = qsearchs('cust_main_county',{
520 'county' => $self->getfield('county'),
521 'state' => $self->getfield('state'),
523 my($tax) = sprintf("%.2f",
524 $charged * ( $cust_main_county->getfield('tax') / 100 )
526 $charged = sprintf("%.2f",$charged+$tax);
528 my($cust_bill_pkg)=create FS::cust_bill_pkg ({
535 push @cust_bill_pkg, $cust_bill_pkg;
538 my($cust_bill) = create FS::cust_bill ( {
539 'custnum' => $self->getfield('custnum'),
541 'charged' => $charged,
543 $error=$cust_bill->insert;
544 #shouldn't happen, but how else to handle this? (wrap me in eval, to catch
546 die "Error creating cust_bill record: $error!\n",
547 "Check updated but unbilled packages for customer", $self->custnum, "\n"
550 my($invnum)=$cust_bill->invnum;
552 foreach $cust_bill_pkg ( @cust_bill_pkg ) {
553 $cust_bill_pkg->setfield('invnum',$invnum);
554 $error=$cust_bill_pkg->insert;
555 #shouldn't happen, but how else tohandle this?
556 die "Error creating cust_bill_pkg record: $error!\n",
557 "Check incomplete invoice ", $invnum, "\n"
564 =item collect OPTIONS
566 (Attempt to) collect money for this customer's outstanding invoices (see
567 L<FS::cust_bill>). Usually used after the bill method.
569 Depending on the value of `payby', this may print an invoice (`BILL'), charge
570 a credit card (`CARD'), or just add any necessary (pseudo-)payment (`COMP').
572 If there is an error, returns the error, otherwise returns false.
574 Currently available options are:
576 invoice_time - Use this time when deciding when to print invoices and
577 late notices on those invoices. The default is now. It is specified as a UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse>
578 for conversion functions.
580 batch_card - Set this true to batch cards (see L<cust_pay_batch>). By
581 default, cards are processed immediately, which will generate an error if
582 CyberCash is not installed.
584 report_badcard - Set this true if you want bad card transactions to
585 return an error. By default, they don't.
590 my($self,%options)=@_;
591 my($invoice_time) = $options{'invoice_time'} || $^T;
593 my($total_owed) = $self->balance;
594 return '' unless $total_owed > 0; #redundant?????
597 local $SIG{HUP} = 'IGNORE';
598 local $SIG{INT} = 'IGNORE';
599 local $SIG{QUIT} = 'IGNORE';
600 local $SIG{TERM} = 'IGNORE';
601 local $SIG{TSTP} = 'IGNORE';
603 foreach my $cust_bill ( qsearch('cust_bill', {
604 'custnum' => $self->getfield('custnum'),
607 bless($cust_bill,"FS::cust_bill");
609 #this has to be before next's
610 my($amount) = sprintf("%.2f", $total_owed < $cust_bill->owed
614 $total_owed = sprintf("%.2f",$total_owed-$amount);
616 next unless $cust_bill->owed > 0;
618 next if qsearchs('cust_pay_batch',{'invnum'=> $cust_bill->invnum });
620 #warn "invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ", amount $amount, total_owed $total_owed)";
622 next unless $amount > 0;
624 if ( $self->getfield('payby') eq 'BILL' ) {
627 my($since)=$invoice_time - ( $cust_bill->_date || 0 );
628 #warn "$invoice_time ", $cust_bill->_date, " $since";
629 if ( $since >= 0 #don't print future invoices
630 && ( $cust_bill->printed * 2592000 ) <= $since
633 open(LPR,"|$lpr") or die "Can't open $lpr: $!";
634 print LPR $cust_bill->print_text; #( date )
636 or die $! ? "Error closing $lpr: $!"
637 : "Exit status $? from $lpr";
639 my(%hash)=$cust_bill->hash;
641 my($new_cust_bill)=create FS::cust_bill(\%hash);
642 my($error)=$new_cust_bill->replace($cust_bill);
644 warn "Error updating $cust_bill->printed: $error";
649 } elsif ( $self->getfield('payby') eq 'COMP' ) {
650 my($cust_pay) = create FS::cust_pay ( {
651 'invnum' => $cust_bill->getfield('invnum'),
655 'payinfo' => $self->getfield('payinfo'),
658 my($error)=$cust_pay->insert;
659 return 'Error COMPing invnum #' . $cust_bill->getfield('invnum') .
660 ':' . $error if $error;
661 } elsif ( $self->getfield('payby') eq 'CARD' ) {
663 if ( $options{'batch_card'} ne 'yes' ) {
665 return "Real time card processing not enabled!" unless $processor;
667 if ( $processor =~ /cybercash/ ) {
669 #fix exp. date for cybercash
670 $self->getfield('paydate') =~ /^(\d+)\/\d*(\d{2})$/;
673 my($paybatch)= $cust_bill->getfield('invnum') .
674 '-' . time2str("%y%m%d%H%M%S",time);
676 my($payname)= $self->getfield('payname') ||
677 $self->getfield('first') . ' ' .$self->getfield('last');
679 my($address)= $self->getfield('address1');
680 $address .= ", " . $self->getfield('address2')
681 if $self->getfield('address2');
683 my($country) = $self->getfield('country') eq 'US' ?
684 'USA' : $self->getfield('country');
686 my(@full_xaction)=($xaction,
687 'Order-ID' => $paybatch,
688 'Amount' => "usd $amount",
689 'Card-Number' => $self->getfield('payinfo'),
690 'Card-Name' => $payname,
691 'Card-Address' => $address,
692 'Card-City' => $self->getfield('city'),
693 'Card-State' => $self->getfield('state'),
694 'Card-Zip' => $self->getfield('zip'),
695 'Card-Country' => $country,
700 if ( $processor eq 'cybercash2' ) {
701 $^W=0; #CCLib isn't -w safe, ugh!
702 %result = &CCLib::sendmserver(@full_xaction);
704 } elsif ( $processor eq 'cybercash3.2' ) {
705 %result = &CCMckDirectLib3_2::SendCC2_1Server(@full_xaction);
707 return "Unkonwn real-time processor $processor\n";
710 #if ( $result{'MActionCode'} == 7 ) { #cybercash smps v.1.1.3
711 #if ( $result{'action-code'} == 7 ) { #cybercash smps v.2.1
712 if ( $result{'MStatus'} eq 'success' ) { #cybercash smps v.2 or 3
713 my($cust_pay) = create FS::cust_pay ( {
714 'invnum' => $cust_bill->getfield('invnum'),
718 'payinfo' => $self->getfield('payinfo'),
719 'paybatch' => "$processor:$paybatch",
721 my($error)=$cust_pay->insert;
722 return 'Error applying payment, invnum #' .
723 $cust_bill->getfield('invnum') . ':' . $error if $error;
724 } elsif ( $result{'Mstatus'} ne 'failure-bad-money'
725 || $options{'report_badcard'} ) {
726 return 'Cybercash error, invnum #' .
727 $cust_bill->getfield('invnum') . ':' . $result{'MErrMsg'};
733 return "Unkonwn real-time processor $processor\n";
738 # my($cust_pay_batch) = create FS::cust_pay_batch ( {
739 my($cust_pay_batch) = new FS::Record ('cust_pay_batch', {
740 'invnum' => $cust_bill->getfield('invnum'),
741 'custnum' => $self->getfield('custnum'),
742 'last' => $self->getfield('last'),
743 'first' => $self->getfield('first'),
744 'address1' => $self->getfield('address1'),
745 'address2' => $self->getfield('address2'),
746 'city' => $self->getfield('city'),
747 'state' => $self->getfield('state'),
748 'zip' => $self->getfield('zip'),
749 'country' => $self->getfield('country'),
751 'cardnum' => $self->getfield('payinfo'),
752 'exp' => $self->getfield('paydate'),
753 'payname' => $self->getfield('payname'),
756 # my($error)=$cust_pay_batch->insert;
757 my($error)=$cust_pay_batch->add;
758 return "Error adding to cust_pay_batch: $error" if $error;
763 return "Unknown payment type ".$self->getfield('payby');
773 Returns the total owed for this customer on all invoices
774 (see L<FS::cust_bill>).
782 foreach $cust_bill ( qsearch('cust_bill', {
783 'custnum' => $self->getfield('custnum'),
785 $total_bill += $cust_bill->getfield('owed');
787 sprintf("%.2f",$total_bill);
792 Returns the total credits (see L<FS::cust_credit>) for this customer.
798 my($total_credit) = 0;
800 foreach $cust_credit ( qsearch('cust_credit', {
801 'custnum' => $self->getfield('custnum'),
803 $total_credit += $cust_credit->getfield('credited');
805 sprintf("%.2f",$total_credit);
810 Returns the balance for this customer (total owed minus total credited).
816 sprintf("%.2f",$self->total_owed - $self->total_credited);
825 It doesn't properly override FS::Record yet.
827 hfields should be removed.
829 Bill and collect options should probably be passed as references instead of a
832 CyberCash v2 forces us to define some variables in package main.
836 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
837 L<FS::cust_pay_batch>, L<FS::agent>, L<FS::part_referral>,
838 L<FS::cust_main_county>, L<FS::UID>, schema.html from the base documentation.
842 ivan@voicenet.com 97-jul-28
844 Changed to standard Business::CreditCard
846 EXPORT_OK FS::Record's hfields
847 removed unique calls and locking (not needed here now)
848 wrapped the (now) optional fields in if statements in sub check (notyetdone!)
849 ivan@sisd.com 97-nov-12
851 updated paydate with SQL-type date info ivan@sisd.com 98-mar-5
853 Added export of datasrc from UID.pm for Pg6.3
854 changed 'day' to 'daytime' because Pg6.3 reserves the day word
855 bmccane@maxbaud.net 98-apr-3
857 in ->create, s/svc_acct/cust_main/, now it should actually eliminate the
858 warnings it was meant to ivan@sisd.com 98-jul-16
860 don't require a phone number and allow '/' in company names
861 ivan@sisd.com 98-jul-18
863 use ut_ and rewrite &check, &*_pkgs ivan@sisd.com 98-sep-5
865 pod, merge with FS::Bill (about time!), total_owed, total_credited and balance
866 methods, cleaned collect method, source modifications no longer necessary to
867 enable cybercash, cybercash v3 support, don't need to import
868 FS::UID::{datasrc,checkruid} ivan@sisd.com 98-sep-19-21
870 $Log: cust_main.pm,v $
871 Revision 1.3 1998-11-13 09:56:54 ivan
872 change configuration file layout to support multiple distinct databases (with
873 own set of config files, export, etc.)
875 Revision 1.2 1998/11/07 10:24:25 ivan
876 don't use depriciated FS::Bill and FS::Invoice, other miscellania