This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / fs_selfservice / FS-SelfService / SelfService.pm
1 package FS::SelfService;
2
3 use strict;
4 use vars qw( $VERSION @ISA @EXPORT_OK $DEBUG
5              $skip_uid_check $dir $socket %autoload $tag );
6 use Exporter;
7 use Socket;
8 use FileHandle;
9 #use IO::Handle;
10 use IO::Select;
11 use Storable 2.09 qw(nstore_fd fd_retrieve);
12
13 $VERSION = '0.03';
14
15 @ISA = qw( Exporter );
16
17 $DEBUG = 0;
18
19 $dir = "/usr/local/freeside";
20 $socket =  "$dir/selfservice_socket";
21 $socket .= '.'.$tag if defined $tag && length($tag);
22
23 #maybe should ask ClientAPI for this list
24 %autoload = (
25   'passwd'                    => 'passwd/passwd',
26   'chfn'                      => 'passwd/passwd',
27   'chsh'                      => 'passwd/passwd',
28   'login_info'                => 'MyAccount/login_info',
29   'login'                     => 'MyAccount/login',
30   'logout'                    => 'MyAccount/logout',
31   'customer_info'             => 'MyAccount/customer_info',
32   'edit_info'                 => 'MyAccount/edit_info',     #add to ss cgi!
33   'invoice'                   => 'MyAccount/invoice',
34   'invoice_logo'              => 'MyAccount/invoice_logo',
35   'list_invoices'             => 'MyAccount/list_invoices', #?
36   'cancel'                    => 'MyAccount/cancel',        #add to ss cgi!
37   'payment_info'              => 'MyAccount/payment_info',
38   'payment_info_renew_info'   => 'MyAccount/payment_info_renew_info',
39   'process_payment'           => 'MyAccount/process_payment',
40   'process_payment_order_pkg' => 'MyAccount/process_payment_order_pkg',
41   'process_payment_change_pkg' => 'MyAccount/process_payment_change_pkg',
42   'process_payment_order_renew' => 'MyAccount/process_payment_order_renew',
43   'process_prepay'            => 'MyAccount/process_prepay',
44   'realtime_collect'          => 'MyAccount/realtime_collect',
45   'list_pkgs'                 => 'MyAccount/list_pkgs',     #add to ss (added?)
46   'list_svcs'                 => 'MyAccount/list_svcs',     #add to ss (added?)
47   'list_svc_usage'            => 'MyAccount/list_svc_usage',   
48   'port_graph'                => 'MyAccount/port_graph',   
49   'list_cdr_usage'            => 'MyAccount/list_cdr_usage',   
50   'list_support_usage'        => 'MyAccount/list_support_usage',   
51   'order_pkg'                 => 'MyAccount/order_pkg',     #add to ss cgi!
52   'change_pkg'                => 'MyAccount/change_pkg', 
53   'order_recharge'            => 'MyAccount/order_recharge',
54   'renew_info'                => 'MyAccount/renew_info',
55   'order_renew'               => 'MyAccount/order_renew',
56   'cancel_pkg'                => 'MyAccount/cancel_pkg',    #add to ss cgi!
57   'suspend_pkg'               => 'MyAccount/suspend_pkg',   #add to ss cgi!
58   'charge'                    => 'MyAccount/charge',        #?
59   'part_svc_info'             => 'MyAccount/part_svc_info',
60   'provision_acct'            => 'MyAccount/provision_acct',
61   'provision_phone'           => 'MyAccount/provision_phone',
62   'provision_external'        => 'MyAccount/provision_external',
63   'unprovision_svc'           => 'MyAccount/unprovision_svc',
64   'myaccount_passwd'          => 'MyAccount/myaccount_passwd',
65   'create_ticket'             => 'MyAccount/create_ticket',
66   'get_ticket'                => 'MyAccount/get_ticket',
67   'did_report'                => 'MyAccount/did_report',
68   'signup_info'               => 'Signup/signup_info',
69   'skin_info'                 => 'MyAccount/skin_info',
70   'access_info'               => 'MyAccount/access_info',
71   'domain_select_hash'        => 'Signup/domain_select_hash',  # expose?
72   'new_customer'              => 'Signup/new_customer',
73   'capture_payment'           => 'Signup/capture_payment',
74   'agent_login'               => 'Agent/agent_login',
75   'agent_logout'              => 'Agent/agent_logout',
76   'agent_info'                => 'Agent/agent_info',
77   'agent_list_customers'      => 'Agent/agent_list_customers',
78   'check_username'            => 'Agent/check_username',
79   'suspend_username'          => 'Agent/suspend_username',
80   'unsuspend_username'        => 'Agent/unsuspend_username',
81   'mason_comp'                => 'MasonComponent/mason_comp',
82   'call_time'                 => 'PrepaidPhone/call_time',
83   'call_time_nanpa'           => 'PrepaidPhone/call_time_nanpa',
84   'phonenum_balance'          => 'PrepaidPhone/phonenum_balance',
85   #izoom
86   #'bulk_processrow'           => 'Bulk/processrow',
87   #conflicts w/Agent one# 'check_username'            => 'Bulk/check_username',
88   #sg
89   'ping'                      => 'SGNG/ping',
90   'decompify_pkgs'            => 'SGNG/decompify_pkgs',
91   'previous_payment_info'     => 'SGNG/previous_payment_info',
92   'previous_payment_info_renew_info'
93                               => 'SGNG/previous_payment_info_renew_info',
94   'previous_process_payment'  => 'SGNG/previous_process_payment',
95   'previous_process_payment_order_pkg'
96                               => 'SGNG/previous_process_payment_order_pkg',
97   'previous_process_payment_change_pkg'
98                               => 'SGNG/previous_process_payment_change_pkg',
99   'previous_process_payment_order_renew'
100                               => 'SGNG/previous_process_payment_order_renew',
101 );
102 @EXPORT_OK = (
103   keys(%autoload),
104   qw( regionselector regionselector_hashref location_form
105       expselect popselector domainselector didselector
106     )
107 );
108
109 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
110 $ENV{'SHELL'} = '/bin/sh';
111 $ENV{'IFS'} = " \t\n";
112 $ENV{'CDPATH'} = '';
113 $ENV{'ENV'} = '';
114 $ENV{'BASH_ENV'} = '';
115
116 #you can add BEGIN { $FS::SelfService::skip_uid_check = 1; } 
117 #if you grant appropriate permissions to whatever user
118 my $freeside_uid = scalar(getpwnam('freeside'));
119 die "not running as the freeside user\n"
120   if $> != $freeside_uid && ! $skip_uid_check;
121
122 -e $dir or die "FATAL: $dir doesn't exist!";
123 -d $dir or die "FATAL: $dir isn't a directory!";
124 -r $dir or die "FATAL: Can't read $dir as freeside user!";
125 -x $dir or die "FATAL: $dir not searchable (executable) as freeside user!";
126
127 foreach my $autoload ( keys %autoload ) {
128
129   my $eval =
130   "sub $autoload { ". '
131                    my $param;
132                    if ( ref($_[0]) ) {
133                      $param = shift;
134                    } else {
135                      #warn scalar(@_). ": ". join(" / ", @_);
136                      $param = { @_ };
137                    }
138
139                    $param->{_packet} = \''. $autoload{$autoload}. '\';
140
141                    simple_packet($param);
142                  }';
143
144   eval $eval;
145   die $@ if $@;
146
147 }
148
149 sub simple_packet {
150   my $packet = shift;
151   warn "sending ". $packet->{_packet}. " to server"
152     if $DEBUG;
153   socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
154   connect(SOCK, sockaddr_un($socket)) or die "connect to $socket: $!";
155   nstore_fd($packet, \*SOCK) or die "can't send packet: $!";
156   SOCK->flush;
157
158   #shoudl trap: Magic number checking on storable file failed at blib/lib/Storable.pm (autosplit into blib/lib/auto/Storable/fd_retrieve.al) line 337, at /usr/local/share/perl/5.6.1/FS/SelfService.pm line 71
159
160   #block until there is a message on socket
161 #  my $w = new IO::Select;
162 #  $w->add(\*SOCK);
163 #  my @wait = $w->can_read;
164
165   warn "reading message from server"
166     if $DEBUG;
167
168   my $return = fd_retrieve(\*SOCK) or die "error reading result: $!";
169   die $return->{'_error'} if defined $return->{_error} && $return->{_error};
170
171   warn "returning message to client"
172     if $DEBUG;
173
174   $return;
175 }
176
177 =head1 NAME
178
179 FS::SelfService - Freeside self-service API
180
181 =head1 SYNOPSIS
182
183   # password and shell account changes
184   use FS::SelfService qw(passwd chfn chsh);
185
186   # "my account" functionality
187   use FS::SelfService qw( login customer_info invoice cancel payment_info process_payment );
188
189   my $rv = login( { 'username' => $username,
190                     'domain'   => $domain,
191                     'password' => $password,
192                   }
193                 );
194
195   if ( $rv->{'error'} ) {
196     #handle login error...
197   } else {
198     #successful login
199     my $session_id = $rv->{'session_id'};
200   }
201
202   my $customer_info = customer_info( { 'session_id' => $session_id } );
203
204   #payment_info and process_payment are available in 1.5+ only
205   my $payment_info = payment_info( { 'session_id' => $session_id } );
206
207   #!!! process_payment example
208
209   #!!! list_pkgs example
210
211   #!!! order_pkg example
212
213   #!!! cancel_pkg example
214
215   # signup functionality
216   use FS::SelfService qw( signup_info new_customer );
217
218   my $signup_info = signup_info;
219
220   $rv = new_customer( {
221                         'first'            => $first,
222                         'last'             => $last,
223                         'company'          => $company,
224                         'address1'         => $address1,
225                         'address2'         => $address2,
226                         'city'             => $city,
227                         'state'            => $state,
228                         'zip'              => $zip,
229                         'country'          => $country,
230                         'daytime'          => $daytime,
231                         'night'            => $night,
232                         'fax'              => $fax,
233                         'payby'            => $payby,
234                         'payinfo'          => $payinfo,
235                         'paycvv'           => $paycvv,
236                         'paystart_month'   => $paystart_month
237                         'paystart_year'    => $paystart_year,
238                         'payissue'         => $payissue,
239                         'payip'            => $payip
240                         'paydate'          => $paydate,
241                         'payname'          => $payname,
242                         'invoicing_list'   => $invoicing_list,
243                         'referral_custnum' => $referral_custnum,
244                         'agentnum'         => $agentnum,
245                         'pkgpart'          => $pkgpart,
246
247                         'username'         => $username,
248                         '_password'        => $password,
249                         'popnum'           => $popnum,
250                         #OR
251                         'countrycode'      => 1,
252                         'phonenum'         => $phonenum,
253                         'pin'              => $pin,
254                       }
255                     );
256   
257   my $error = $rv->{'error'};
258   if ( $error eq '_decline' ) {
259     print_decline();
260   } elsif ( $error ) {
261     reprint_signup();
262   } else {
263     print_success();
264   }
265
266 =head1 DESCRIPTION
267
268 Use this API to implement your own client "self-service" module.
269
270 If you just want to customize the look of the existing "self-service" module,
271 see XXXX instead.
272
273 =head1 PASSWORD, GECOS, SHELL CHANGING FUNCTIONS
274
275 =over 4
276
277 =item passwd
278
279 =item chfn
280
281 =item chsh
282
283 =back
284
285 =head1 "MY ACCOUNT" FUNCTIONS
286
287 =over 4
288
289 =item login HASHREF
290
291 Creates a user session.  Takes a hash reference as parameter with the
292 following keys:
293
294 =over 4
295
296 =item username
297
298 Username
299
300 =item domain
301
302 Domain
303
304 =item password
305
306 Password
307
308 =back
309
310 Returns a hash reference with the following keys:
311
312 =over 4
313
314 =item error
315
316 Empty on success, or an error message on errors.
317
318 =item session_id
319
320 Session identifier for successful logins
321
322 =back
323
324 =item customer_info HASHREF
325
326 Returns general customer information.
327
328 Takes a hash reference as parameter with a single key: B<session_id>
329
330 Returns a hash reference with the following keys:
331
332 =over 4
333
334 =item name
335
336 Customer name
337
338 =item balance
339
340 Balance owed
341
342 =item open
343
344 Array reference of hash references of open inoices.  Each hash reference has
345 the following keys: invnum, date, owed
346
347 =item small_custview
348
349 An HTML fragment containing shipping and billing addresses.
350
351 =item The following fields are also returned
352
353 first last company address1 address2 city county state zip country daytime night fax ship_first ship_last ship_company ship_address1 ship_address2 ship_city ship_state ship_zip ship_country ship_daytime ship_night ship_fax payby payinfo payname month year invoicing_list postal_invoicing
354
355 =back
356
357 =item edit_info HASHREF
358
359 Takes a hash reference as parameter with any of the following keys:
360
361 first last company address1 address2 city county state zip country daytime night fax ship_first ship_last ship_company ship_address1 ship_address2 ship_city ship_state ship_zip ship_country ship_daytime ship_night ship_fax payby payinfo paycvv payname month year invoicing_list postal_invoicing
362
363 If a field exists, the customer record is updated with the new value of that
364 field.  If a field does not exist, that field is not changed on the customer
365 record.
366
367 Returns a hash reference with a single key, B<error>, empty on success, or an
368 error message on errors
369
370 =item invoice HASHREF
371
372 Returns an invoice.  Takes a hash reference as parameter with two keys:
373 session_id and invnum
374
375 Returns a hash reference with the following keys:
376
377 =over 4
378
379 =item error
380
381 Empty on success, or an error message on errors
382
383 =item invnum
384
385 Invoice number
386
387 =item invoice_text
388
389 Invoice text
390
391 =back
392
393 =item list_invoices HASHREF
394
395 Returns a list of all customer invoices.  Takes a hash references with a single
396 key, session_id.
397
398 Returns a hash reference with the following keys:
399
400 =over 4
401
402 =item error
403
404 Empty on success, or an error message on errors
405
406 =item invoices
407
408 Reference to array of hash references with the following keys:
409
410 =over 4
411
412 =item invnum
413
414 Invoice ID
415
416 =item _date
417
418 Invoice date, in UNIX epoch time
419
420 =back
421
422 =back
423
424 =item cancel HASHREF
425
426 Cancels this customer.
427
428 Takes a hash reference as parameter with a single key: B<session_id>
429
430 Returns a hash reference with a single key, B<error>, which is empty on
431 success or an error message on errors.
432
433 =item payment_info HASHREF
434
435 Returns information that may be useful in displaying a payment page.
436
437 Takes a hash reference as parameter with a single key: B<session_id>.
438
439 Returns a hash reference with the following keys:
440
441 =over 4
442
443 =item error
444
445 Empty on success, or an error message on errors
446
447 =item balance
448
449 Balance owed
450
451 =item payname
452
453 Exact name on credit card (CARD/DCRD)
454
455 =item address1
456
457 Address line one
458
459 =item address2
460
461 Address line two
462
463 =item city
464
465 City
466
467 =item state
468
469 State
470
471 =item zip
472
473 Zip or postal code
474
475 =item payby
476
477 Customer's current default payment type.
478
479 =item card_type
480
481 For CARD/DCRD payment types, the card type (Visa card, MasterCard, Discover card, American Express card, etc.)
482
483 =item payinfo
484
485 For CARD/DCRD payment types, the card number
486
487 =item month
488
489 For CARD/DCRD payment types, expiration month
490
491 =item year
492
493 For CARD/DCRD payment types, expiration year
494
495 =item cust_main_county
496
497 County/state/country data - array reference of hash references, each of which has the fields of a cust_main_county record (see L<FS::cust_main_county>).  Note these are not FS::cust_main_county objects, but hash references of columns and values.
498
499 =item states
500
501 Array reference of all states in the current default country.
502
503 =item card_types
504
505 Hash reference of card types; keys are card types, values are the exact strings
506 passed to the process_payment function
507
508 =cut
509
510 #this doesn't actually work yet
511 #
512 #=item paybatch
513 #
514 #Unique transaction identifier (prevents multiple charges), passed to the
515 #process_payment function
516
517 =back
518
519 =item process_payment HASHREF
520
521 Processes a payment and possible change of address or payment type.  Takes a
522 hash reference as parameter with the following keys:
523
524 =over 4
525
526 =item session_id
527
528 Session identifier
529
530 =item amount
531
532 Amount
533
534 =item save
535
536 If true, address and card information entered will be saved for subsequent
537 transactions.
538
539 =item auto
540
541 If true, future credit card payments will be done automatically (sets payby to
542 CARD).  If false, future credit card payments will be done on-demand (sets
543 payby to DCRD).  This option only has meaning if B<save> is set true.  
544
545 =item payname
546
547 Name on card
548
549 =item address1
550
551 Address line one
552
553 =item address2
554
555 Address line two
556
557 =item city
558
559 City
560
561 =item state
562
563 State
564
565 =item zip
566
567 Zip or postal code
568
569 =item country
570
571 Two-letter country code
572
573 =item payinfo
574
575 Card number
576
577 =item month
578
579 Card expiration month
580
581 =item year
582
583 Card expiration year
584
585 =cut
586
587 #this doesn't actually work yet
588 #
589 #=item paybatch
590 #
591 #Unique transaction identifier, returned from the payment_info function.
592 #Prevents multiple charges.
593
594 =back
595
596 Returns a hash reference with a single key, B<error>, empty on success, or an
597 error message on errors.
598
599 =item process_payment_order_pkg
600
601 Combines the B<process_payment> and B<order_pkg> functions in one step.  If the
602 payment processes sucessfully, the package is ordered.  Takes a hash reference
603 as parameter with the keys of both methods.
604
605 Returns a hash reference with a single key, B<error>, empty on success, or an
606 error message on errors.
607
608 =item process_payment_change_pkg
609
610 Combines the B<process_payment> and B<change_pkg> functions in one step.  If the
611 payment processes sucessfully, the package is ordered.  Takes a hash reference
612 as parameter with the keys of both methods.
613
614 Returns a hash reference with a single key, B<error>, empty on success, or an
615 error message on errors.
616
617
618 =item process_payment_order_renew
619
620 Combines the B<process_payment> and B<order_renew> functions in one step.  If
621 the payment processes sucessfully, the renewal is processed.  Takes a hash
622 reference as parameter with the keys of both methods.
623
624 Returns a hash reference with a single key, B<error>, empty on success, or an
625 error message on errors.
626
627 =item list_pkgs
628
629 Returns package information for this customer.  For more detail on services,
630 see L</list_svcs>.
631
632 Takes a hash reference as parameter with a single key: B<session_id>
633
634 Returns a hash reference containing customer package information.  The hash reference contains the following keys:
635
636 =over 4
637
638 =item custnum
639
640 Customer number
641
642 =item error
643
644 Empty on success, or an error message on errors.
645
646 =item cust_pkg HASHREF
647
648 Array reference of hash references, each of which has the fields of a cust_pkg
649 record (see L<FS::cust_pkg>) as well as the fields below.  Note these are not
650 the internal FS:: objects, but hash references of columns and values.
651
652 =over 4
653
654 =item part_pkg fields
655
656 All fields of part_pkg for this specific cust_pkg (be careful with this
657 information - it may reveal more about your available packages than you would
658 like users to know in aggregate) 
659
660 =cut
661
662 #XXX pare part_pkg fields down to a more secure subset
663
664 =item part_svc
665
666 An array of hash references indicating information on unprovisioned services
667 available for provisioning for this specific cust_pkg.  Each has the following
668 keys:
669
670 =over 4
671
672 =item part_svc fields
673
674 All fields of part_svc (be careful with this information - it may reveal more
675 about your available packages than you would like users to know in aggregate) 
676
677 =cut
678
679 #XXX pare part_svc fields down to a more secure subset
680
681 =back
682
683 =item cust_svc
684
685 An array of hash references indicating information on the customer services
686 already provisioned for this specific cust_pkg.  Each has the following keys:
687
688 =over 4
689
690 =item label
691
692 Array reference with three elements: The first element is the name of this service.  The second element is a meaningful user-specific identifier for the service (i.e. username, domain or mail alias).  The last element is the table name of this service.
693
694 =back
695
696 =item svcnum
697
698 Primary key for this service
699
700 =item svcpart
701
702 Service definition (see L<FS::part_svc>)
703
704 =item pkgnum
705
706 Customer package (see L<FS::cust_pkg>)
707
708 =item overlimit
709
710 Blank if the service is not over limit, or the date the service exceeded its usage limit (as a UNIX timestamp).
711
712 =back
713
714 =back
715
716 =item list_svcs
717
718 Returns service information for this customer.
719
720 Takes a hash reference as parameter with a single key: B<session_id>
721
722 Returns a hash reference containing customer package information.  The hash reference contains the following keys:
723
724 =over 4
725
726 =item custnum
727
728 Customer number
729
730 =item svcs
731
732 An array of hash references indicating information on all of this customer's
733 services.  Each has the following keys:
734
735 =over 4
736
737 =item svcnum
738
739 Primary key for this service
740
741 =item label
742
743 Name of this service
744
745 =item value
746
747 Meaningful user-specific identifier for the service (i.e. username, domain, or
748 mail alias).
749
750 =back
751
752 Account (svc_acct) services also have the following keys:
753
754 =over 4
755
756 =item username
757
758 Username
759
760 =item email
761
762 username@domain
763
764 =item seconds
765
766 Seconds remaining
767
768 =item upbytes
769
770 Upload bytes remaining
771
772 =item downbytes
773
774 Download bytes remaining
775
776 =item totalbytes
777
778 Total bytes remaining
779
780 =item recharge_amount
781
782 Cost of a recharge
783
784 =item recharge_seconds
785
786 Number of seconds gained by recharge
787
788 =item recharge_upbytes
789
790 Number of upload bytes gained by recharge
791
792 =item recharge_downbytes
793
794 Number of download bytes gained by recharge
795
796 =item recharge_totalbytes
797
798 Number of total bytes gained by recharge
799
800 =back
801
802 =back
803
804 =item order_pkg
805
806 Orders a package for this customer.
807
808 Takes a hash reference as parameter with the following keys:
809
810 =over 4
811
812 =item session_id
813
814 Session identifier
815
816 =item pkgpart
817
818 Package to order (see L<FS::part_pkg>).
819
820 =item svcpart
821
822 Service to order (see L<FS::part_svc>).
823
824 Normally optional; required only to provision a non-svc_acct service, or if the
825 package definition does not contain one svc_acct service definition with
826 quantity 1 (it may contain others with quantity >1).  A svcpart of "none" can
827 also be specified to indicate that no initial service should be provisioned.
828
829 =back
830
831 Fields used when provisioning an svc_acct service:
832
833 =over 4
834
835 =item username
836
837 Username
838
839 =item _password
840
841 Password
842
843 =item sec_phrase
844
845 Optional security phrase
846
847 =item popnum
848
849 Optional Access number number
850
851 =back
852
853 Fields used when provisioning an svc_domain service:
854
855 =over 4
856
857 =item domain
858
859 Domain
860
861 =back
862
863 Fields used when provisioning an svc_phone service:
864
865 =over 4
866
867 =item phonenum
868
869 Phone number
870
871 =item pin
872
873 Voicemail PIN
874
875 =item sip_password
876
877 SIP password
878
879 =back
880
881 Fields used when provisioning an svc_external service:
882
883 =over 4
884
885 =item id
886
887 External numeric ID.
888
889 =item title
890
891 External text title.
892
893 =back
894
895 Returns a hash reference with a single key, B<error>, empty on success, or an
896 error message on errors.  The special error '_decline' is returned for
897 declined transactions.
898
899 =item change_pkg
900
901 Changes a package for this customer.
902
903 Takes a hash reference as parameter with the following keys:
904
905 =over 4
906
907 =item session_id
908
909 Session identifier
910
911 =item pkgnum
912
913 Existing customer package.
914
915 =item pkgpart
916
917 New package to order (see L<FS::part_pkg>).
918
919 =back
920
921 Returns a hash reference with a single key, B<error>, empty on success, or an
922 error message on errors.  
923
924 =item renew_info
925
926 Provides useful info for early renewals.
927
928 Takes a hash reference as parameter with the following keys:
929
930 =over 4
931
932 =item session_id
933
934 Session identifier
935
936 =back
937
938 Returns a hash reference.  On errors, it contains a single key, B<error>, with
939 the error message.  Otherwise, contains a single key, B<dates>, pointing to
940 an array refernce of hash references.  Each hash reference contains the
941 following keys:
942
943 =over 4
944
945 =item bill_date
946
947 (Future) Bill date.  Indicates a future date for which billing could be run.
948 Specified as a integer UNIX timestamp.  Pass this value to the B<order_renew>
949 function.
950
951 =item bill_date_pretty
952
953 (Future) Bill date as a human-readable string.  (Convenience for display;
954 subject to change, so best not to parse for the date.)
955
956 =item amount
957
958 Base amount which will be charged if renewed early as of this date.
959
960 =item renew_date
961
962 Renewal date; i.e. even-futher future date at which the customer will be paid
963 through if the early renewal is completed with the given B<bill-date>.
964 Specified as a integer UNIX timestamp.
965
966 =item renew_date_pretty
967
968 Renewal date as a human-readable string.  (Convenience for display;
969 subject to change, so best not to parse for the date.)
970
971 =item pkgnum
972
973 Package that will be renewed.
974
975 =item expire_date
976
977 Expiration date of the package that will be renewed.
978
979 =item expire_date_pretty
980
981 Expiration date of the package that will be renewed, as a human-readable
982 string.  (Convenience for display; subject to change, so best not to parse for
983 the date.)
984
985 =back
986
987 =item order_renew
988
989 Renews this customer early; i.e. runs billing for this customer in advance.
990
991 Takes a hash reference as parameter with the following keys:
992
993 =over 4
994
995 =item session_id
996
997 Session identifier
998
999 =item date
1000
1001 Integer date as returned by the B<renew_info> function, indicating the advance
1002 date for which to run billing.
1003
1004 =back
1005
1006 Returns a hash reference with a single key, B<error>, empty on success, or an
1007 error message on errors.
1008
1009 =item cancel_pkg
1010
1011 Cancels a package for this customer.
1012
1013 Takes a hash reference as parameter with the following keys:
1014
1015 =over 4
1016
1017 =item session_id
1018
1019 Session identifier
1020
1021 =item pkgpart
1022
1023 pkgpart of package to cancel
1024
1025 =back
1026
1027 Returns a hash reference with a single key, B<error>, empty on success, or an
1028 error message on errors.
1029
1030 =back
1031
1032 =head1 SIGNUP FUNCTIONS
1033
1034 =over 4
1035
1036 =item signup_info HASHREF
1037
1038 Takes a hash reference as parameter with the following keys:
1039
1040 =over 4
1041
1042 =item session_id - Optional agent/reseller interface session
1043
1044 =back
1045
1046 Returns a hash reference containing information that may be useful in
1047 displaying a signup page.  The hash reference contains the following keys:
1048
1049 =over 4
1050
1051 =item cust_main_county
1052
1053 County/state/country data - array reference of hash references, each of which has the fields of a cust_main_county record (see L<FS::cust_main_county>).  Note these are not FS::cust_main_county objects, but hash references of columns and values.
1054
1055 =item part_pkg
1056
1057 Available packages - array reference of hash references, each of which has the fields of a part_pkg record (see L<FS::part_pkg>).  Each hash reference also has an additional 'payby' field containing an array reference of acceptable payment types specific to this package (see below and L<FS::part_pkg/payby>).  Note these are not FS::part_pkg objects, but hash references of columns and values.  Requires the 'signup_server-default_agentnum' configuration value to be set, or
1058 an agentnum specified explicitly via reseller interface session_id in the
1059 options.
1060
1061 =item agent
1062
1063 Array reference of hash references, each of which has the fields of an agent record (see L<FS::agent>).  Note these are not FS::agent objects, but hash references of columns and values.
1064
1065 =item agentnum2part_pkg
1066
1067 Hash reference; keys are agentnums, values are array references of available packages for that agent, in the same format as the part_pkg arrayref above.
1068
1069 =item svc_acct_pop
1070
1071 Access numbers - array reference of hash references, each of which has the fields of an svc_acct_pop record (see L<FS::svc_acct_pop>).  Note these are not FS::svc_acct_pop objects, but hash references of columns and values.
1072
1073 =item security_phrase
1074
1075 True if the "security_phrase" feature is enabled
1076
1077 =item payby
1078
1079 Array reference of acceptable payment types for signup
1080
1081 =over 4
1082
1083 =item CARD
1084
1085 credit card - automatic
1086
1087 =item DCRD
1088
1089 credit card - on-demand - version 1.5+ only
1090
1091 =item CHEK
1092
1093 electronic check - automatic
1094
1095 =item DCHK
1096
1097 electronic check - on-demand - version 1.5+ only
1098
1099 =item LECB
1100
1101 Phone bill billing
1102
1103 =item BILL
1104
1105 billing, not recommended for signups
1106
1107 =item COMP
1108
1109 free, definitely not recommended for signups
1110
1111 =item PREPAY
1112
1113 special billing type: applies a credit (see FS::prepay_credit) and sets billing type to BILL
1114
1115 =back
1116
1117 =item cvv_enabled
1118
1119 True if CVV features are available (1.5+ or 1.4.2 with CVV schema patch)
1120
1121 =item msgcat
1122
1123 Hash reference of message catalog values, to support error message customization.  Currently available keys are: passwords_dont_match, invalid_card, unknown_card_type, and not_a (as in "Not a Discover card").  Values are configured in the web interface under "View/Edit message catalog".
1124
1125 =item statedefault
1126
1127 Default state
1128
1129 =item countrydefault
1130
1131 Default country
1132
1133 =back
1134
1135 =item new_customer HASHREF
1136
1137 Creates a new customer.  Takes a hash reference as parameter with the
1138 following keys:
1139
1140 =over 4
1141
1142 =item first
1143
1144 first name (required)
1145
1146 =item last
1147
1148 last name (required)
1149
1150 =item ss
1151
1152 (not typically collected; mostly used for ACH transactions)
1153
1154 =item company
1155
1156 Company name
1157
1158 =item address1 (required)
1159
1160 Address line one
1161
1162 =item address2
1163
1164 Address line two
1165
1166 =item city (required)
1167
1168 City
1169
1170 =item county
1171
1172 County
1173
1174 =item state (required)
1175
1176 State
1177
1178 =item zip (required)
1179
1180 Zip or postal code
1181
1182 =item daytime
1183
1184 Daytime phone number
1185
1186 =item night
1187
1188 Evening phone number
1189
1190 =item fax
1191
1192 Fax number
1193
1194 =item payby
1195
1196 CARD, DCRD, CHEK, DCHK, LECB, BILL, COMP or PREPAY (see L</signup_info> (required)
1197
1198 =item payinfo
1199
1200 Card number for CARD/DCRD, account_number@aba_number for CHEK/DCHK, prepaid "pin" for PREPAY, purchase order number for BILL
1201
1202 =item paycvv
1203
1204 Credit card CVV2 number (1.5+ or 1.4.2 with CVV schema patch)
1205
1206 =item paydate
1207
1208 Expiration date for CARD/DCRD
1209
1210 =item payname
1211
1212 Exact name on credit card for CARD/DCRD, bank name for CHEK/DCHK
1213
1214 =item invoicing_list
1215
1216 comma-separated list of email addresses for email invoices.  The special value 'POST' is used to designate postal invoicing (it may be specified alone or in addition to email addresses),
1217
1218 =item referral_custnum
1219
1220 referring customer number
1221
1222 =item agentnum
1223
1224 Agent number
1225
1226 =item pkgpart
1227
1228 pkgpart of initial package
1229
1230 =item username
1231
1232 Username
1233
1234 =item _password
1235
1236 Password
1237
1238 =item sec_phrase
1239
1240 Security phrase
1241
1242 =item popnum
1243
1244 Access number (index, not the literal number)
1245
1246 =item countrycode
1247
1248 Country code (to be provisioned as a service)
1249
1250 =item phonenum
1251
1252 Phone number (to be provisioned as a service)
1253
1254 =item pin
1255
1256 Voicemail PIN
1257
1258 =back
1259
1260 Returns a hash reference with the following keys:
1261
1262 =over 4
1263
1264 =item error
1265
1266 Empty on success, or an error message on errors.  The special error '_decline' is returned for declined transactions; other error messages should be suitable for display to the user (and are customizable in under Configuration | View/Edit message catalog)
1267
1268 =back
1269
1270 =item regionselector HASHREF | LIST
1271
1272 Takes as input a hashref or list of key/value pairs with the following keys:
1273
1274 =over 4
1275
1276 =item selected_county
1277
1278 Currently selected county
1279
1280 =item selected_state
1281
1282 Currently selected state
1283
1284 =item selected_country
1285
1286 Currently selected country
1287
1288 =item prefix
1289
1290 Specify a unique prefix string  if you intend to use the HTML output multiple time son one page.
1291
1292 =item onchange
1293
1294 Specify a javascript subroutine to call on changes
1295
1296 =item default_state
1297
1298 Default state
1299
1300 =item default_country
1301
1302 Default country
1303
1304 =item locales
1305
1306 An arrayref of hash references specifying regions.  Normally you can just pass the value of the I<cust_main_county> field returned by B<signup_info>.
1307
1308 =back
1309
1310 Returns a list consisting of three HTML fragments for county selection,
1311 state selection and country selection, respectively.
1312
1313 =cut
1314
1315 #false laziness w/FS::cust_main_county (this is currently the "newest" version)
1316 sub regionselector {
1317   my $param;
1318   if ( ref($_[0]) ) {
1319     $param = shift;
1320   } else {
1321     $param = { @_ };
1322   }
1323   $param->{'selected_country'} ||= $param->{'default_country'};
1324   $param->{'selected_state'} ||= $param->{'default_state'};
1325
1326   my $prefix = exists($param->{'prefix'}) ? $param->{'prefix'} : '';
1327
1328   my $countyflag = 0;
1329
1330   my %cust_main_county;
1331
1332 #  unless ( @cust_main_county ) { #cache 
1333     #@cust_main_county = qsearch('cust_main_county', {} );
1334     #foreach my $c ( @cust_main_county ) {
1335     foreach my $c ( @{ $param->{'locales'} } ) {
1336       #$countyflag=1 if $c->county;
1337       $countyflag=1 if $c->{county};
1338       #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
1339       #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
1340       $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
1341     }
1342 #  }
1343   $countyflag=1 if $param->{selected_county};
1344
1345   my $script_html = <<END;
1346     <SCRIPT>
1347     function opt(what,value,text) {
1348       var optionName = new Option(text, value, false, false);
1349       var length = what.length;
1350       what.options[length] = optionName;
1351     }
1352     function ${prefix}country_changed(what) {
1353       country = what.options[what.selectedIndex].text;
1354       for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
1355           what.form.${prefix}state.options[i] = null;
1356 END
1357       #what.form.${prefix}state.options[0] = new Option('', '', false, true);
1358
1359   foreach my $country ( sort keys %cust_main_county ) {
1360     $script_html .= "\nif ( country == \"$country\" ) {\n";
1361     foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
1362       my $text = $state || '(n/a)';
1363       $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
1364     }
1365     $script_html .= "}\n";
1366   }
1367
1368   $script_html .= <<END;
1369     }
1370     function ${prefix}state_changed(what) {
1371 END
1372
1373   if ( $countyflag ) {
1374     $script_html .= <<END;
1375       state = what.options[what.selectedIndex].text;
1376       country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
1377       for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
1378           what.form.${prefix}county.options[i] = null;
1379 END
1380
1381     foreach my $country ( sort keys %cust_main_county ) {
1382       $script_html .= "\nif ( country == \"$country\" ) {\n";
1383       foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
1384         $script_html .= "\nif ( state == \"$state\" ) {\n";
1385           #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
1386           foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
1387             my $text = $county || '(n/a)';
1388             $script_html .=
1389               qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
1390           }
1391         $script_html .= "}\n";
1392       }
1393       $script_html .= "}\n";
1394     }
1395   }
1396
1397   $script_html .= <<END;
1398     }
1399     </SCRIPT>
1400 END
1401
1402   my $county_html = $script_html;
1403   if ( $countyflag ) {
1404     $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$param->{'onchange'}">!;
1405     foreach my $county ( 
1406       sort keys %{ $cust_main_county{$param->{'selected_country'}}{$param->{'selected_state'}} }
1407     ) {
1408       my $text = $county || '(n/a)';
1409       $county_html .= qq!<OPTION VALUE="$county"!.
1410                       ($county eq $param->{'selected_county'} ? 
1411                         ' SELECTED>' : 
1412                         '>'
1413                       ).
1414                       $text.
1415                       '</OPTION>';
1416     }
1417     $county_html .= '</SELECT>';
1418   } else {
1419     $county_html .=
1420       qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$param->{'selected_county'}">!;
1421   }
1422
1423   my $state_html = qq!<SELECT NAME="${prefix}state" !.
1424                    qq!onChange="${prefix}state_changed(this); $param->{'onchange'}">!;
1425   foreach my $state ( sort keys %{ $cust_main_county{$param->{'selected_country'}} } ) {
1426     my $text = $state || '(n/a)';
1427     my $selected = $state eq $param->{'selected_state'} ? 'SELECTED' : '';
1428     $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
1429   }
1430   $state_html .= '</SELECT>';
1431
1432   my $country_html = '';
1433   if ( scalar( keys %cust_main_county ) > 1 )  {
1434
1435     $country_html = qq(<SELECT NAME="${prefix}country" ).
1436                     qq(onChange="${prefix}country_changed(this); ).
1437                                  $param->{'onchange'}.
1438                                '"'.
1439                       '>';
1440     my $countrydefault = $param->{default_country} || 'US';
1441     foreach my $country (
1442       sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
1443         keys %cust_main_county
1444     ) {
1445       my $selected = $country eq $param->{'selected_country'}
1446                        ? ' SELECTED'
1447                        : '';
1448       $country_html .= "\n<OPTION$selected>$country</OPTION>"
1449     }
1450     $country_html .= '</SELECT>';
1451   } else {
1452
1453     $country_html = qq(<INPUT TYPE="hidden" NAME="${prefix}country" ).
1454                             ' VALUE="'. (keys %cust_main_county )[0]. '">';
1455
1456   }
1457
1458   ($county_html, $state_html, $country_html);
1459
1460 }
1461
1462 sub regionselector_hashref {
1463   my ($county_html, $state_html, $country_html) = regionselector(@_);
1464   {
1465     'county_html'  => $county_html,
1466     'state_html'   => $state_html,
1467     'country_html' => $country_html,
1468   };
1469 }
1470
1471 =item location_form HASHREF | LIST
1472
1473 Takes as input a hashref or list of key/value pairs with the following keys:
1474
1475 =over 4
1476
1477 =item session_id
1478
1479 Current customer session_id
1480
1481 =item no_asterisks
1482
1483 Omit red asterisks from required fields.
1484
1485 =item address1_label
1486
1487 Label for first address line.
1488
1489 =back
1490
1491 Returns an HTML fragment for a location form (address, city, state, zip,
1492 country)
1493
1494 =cut
1495
1496 sub location_form {
1497   my $param;
1498   if ( ref($_[0]) ) {
1499     $param = shift;
1500   } else {
1501     $param = { @_ };
1502   }
1503
1504   my $session_id = delete $param->{'session_id'};
1505
1506   my $rv = mason_comp( 'session_id' => $session_id,
1507                        'comp'       => '/elements/location.html',
1508                        'args'       => [ %$param ],
1509                      );
1510
1511   #hmm.
1512   $rv->{'error'} || $rv->{'output'};
1513
1514 }
1515
1516
1517 #=item expselect HASHREF | LIST
1518 #
1519 #Takes as input a hashref or list of key/value pairs with the following keys:
1520 #
1521 #=over 4
1522 #
1523 #=item prefix - Specify a unique prefix string  if you intend to use the HTML output multiple time son one page.
1524 #
1525 #=item date - current date, in yyyy-mm-dd or m-d-yyyy format
1526 #
1527 #=back
1528
1529 =item expselect PREFIX [ DATE ]
1530
1531 Takes as input a unique prefix string and the current expiration date, in
1532 yyyy-mm-dd or m-d-yyyy format
1533
1534 Returns an HTML fragments for expiration date selection.
1535
1536 =cut
1537
1538 sub expselect {
1539   #my $param;
1540   #if ( ref($_[0]) ) {
1541   #  $param = shift;
1542   #} else {
1543   #  $param = { @_ };
1544   #my $prefix = $param->{'prefix'};
1545   #my $prefix = exists($param->{'prefix'}) ? $param->{'prefix'} : '';
1546   #my $date =   exists($param->{'date'})   ? $param->{'date'}   : '';
1547   my $prefix = shift;
1548   my $date = scalar(@_) ? shift : '';
1549
1550   my( $m, $y ) = ( 0, 0 );
1551   if ( $date  =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
1552     ( $m, $y ) = ( $2, $1 );
1553   } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
1554     ( $m, $y ) = ( $1, $3 );
1555   }
1556   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
1557   for ( 1 .. 12 ) {
1558     $return .= qq!<OPTION VALUE="$_"!;
1559     $return .= " SELECTED" if $_ == $m;
1560     $return .= ">$_";
1561   }
1562   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
1563   my @t = localtime;
1564   my $thisYear = $t[5] + 1900;
1565   for ( ($thisYear > $y && $y > 0 ? $y : $thisYear) .. ($thisYear+10) ) {
1566     $return .= qq!<OPTION VALUE="$_"!;
1567     $return .= " SELECTED" if $_ == $y;
1568     $return .= ">$_";
1569   }
1570   $return .= "</SELECT>";
1571
1572   $return;
1573 }
1574
1575 =item popselector HASHREF | LIST
1576
1577 Takes as input a hashref or list of key/value pairs with the following keys:
1578
1579 =over 4
1580
1581 =item popnum
1582
1583 Access number number
1584
1585 =item pops
1586
1587 An arrayref of hash references specifying access numbers.  Normally you can just pass the value of the I<svc_acct_pop> field returned by B<signup_info>.
1588
1589 =back
1590
1591 Returns an HTML fragment for access number selection.
1592
1593 =cut
1594
1595 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
1596 sub popselector {
1597   my $param;
1598   if ( ref($_[0]) ) {
1599     $param = shift;
1600   } else {
1601     $param = { @_ };
1602   }
1603   my $popnum = $param->{'popnum'};
1604   my $pops = $param->{'pops'};
1605
1606   return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
1607   return $pops->[0]{city}. ', '. $pops->[0]{state}.
1608          ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}. '-'. $pops->[0]{loc}.
1609          '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
1610     if scalar(@$pops) == 1;
1611
1612   my %pop = ();
1613   my %popnum2pop = ();
1614   foreach (@$pops) {
1615     push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
1616     $popnum2pop{$_->{popnum}} = $_;
1617   }
1618
1619   my $text = <<END;
1620     <SCRIPT>
1621     function opt(what,href,text) {
1622       var optionName = new Option(text, href, false, false)
1623       var length = what.length;
1624       what.options[length] = optionName;
1625     }
1626 END
1627
1628   my $init_popstate = $param->{'init_popstate'};
1629   if ( $init_popstate ) {
1630     $text .= '<INPUT TYPE="hidden" NAME="init_popstate" VALUE="'.
1631              $init_popstate. '">';
1632   } else {
1633     $text .= <<END;
1634       function acstate_changed(what) {
1635         state = what.options[what.selectedIndex].text;
1636         what.form.popac.options.length = 0
1637         what.form.popac.options[0] = new Option("Area code", "-1", false, true);
1638 END
1639   } 
1640
1641   my @states = $init_popstate ? ( $init_popstate ) : keys %pop;
1642   foreach my $state ( sort { $a cmp $b } @states ) {
1643     $text .= "\nif ( state == \"$state\" ) {\n" unless $init_popstate;
1644
1645     foreach my $ac ( sort { $a cmp $b } keys %{ $pop{$state} }) {
1646       $text .= "opt(what.form.popac, \"$ac\", \"$ac\");\n";
1647       if ($ac eq $param->{'popac'}) {
1648         $text .= "what.form.popac.options[what.form.popac.length-1].selected = true;\n";
1649       }
1650     }
1651     $text .= "}\n" unless $init_popstate;
1652   }
1653   $text .= "popac_changed(what.form.popac)}\n";
1654
1655   $text .= <<END;
1656   function popac_changed(what) {
1657     ac = what.options[what.selectedIndex].text;
1658     what.form.popnum.options.length = 0;
1659     what.form.popnum.options[0] = new Option("City", "-1", false, true);
1660
1661 END
1662
1663   foreach my $state ( @states ) {
1664     foreach my $popac ( keys %{ $pop{$state} } ) {
1665       $text .= "\nif ( ac == \"$popac\" ) {\n";
1666
1667       foreach my $pop ( @{$pop{$state}->{$popac}}) {
1668         my $o_popnum = $pop->{popnum};
1669         my $poptext =  $pop->{city}. ', '. $pop->{state}.
1670                        ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
1671
1672         $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n";
1673         if ($popnum == $o_popnum) {
1674           $text .= "what.form.popnum.options[what.form.popnum.length-1].selected = true;\n";
1675         }
1676       }
1677       $text .= "}\n";
1678     }
1679   }
1680
1681
1682   $text .= "}\n</SCRIPT>\n";
1683
1684   $param->{'acstate'} = '' unless defined($param->{'acstate'});
1685
1686   $text .=
1687     qq!<TABLE CELLPADDING="0"><TR><TD><SELECT NAME="acstate"! .
1688     qq!SIZE=1 onChange="acstate_changed(this)"><OPTION VALUE=-1>State!;
1689   $text .= "<OPTION" . ($_ eq $param->{'acstate'} ? " SELECTED" : "") .
1690            ">$_" foreach sort { $a cmp $b } @states;
1691   $text .= '</SELECT>'; #callback? return 3 html pieces?  #'</TD>';
1692
1693   $text .=
1694     qq!<SELECT NAME="popac" SIZE=1 onChange="popac_changed(this)">!.
1695     qq!<OPTION>Area code</SELECT></TR><TR VALIGN="top">!;
1696
1697   $text .= qq!<TR><TD><SELECT NAME="popnum" SIZE=1 STYLE="width: 20em"><OPTION>City!;
1698
1699
1700   #comment this block to disable initial list polulation
1701   my @initial_select = ();
1702   if ( scalar( @$pops ) > 100 ) {
1703     push @initial_select, $popnum2pop{$popnum} if $popnum2pop{$popnum};
1704   } else {
1705     @initial_select = @$pops;
1706   }
1707   foreach my $pop ( sort { $a->{state} cmp $b->{state} } @initial_select ) {
1708     $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
1709              ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
1710              $pop->{city}. ', '. $pop->{state}.
1711                ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
1712   }
1713
1714   $text .= qq!</SELECT></TD></TR></TABLE>!;
1715
1716   $text;
1717
1718 }
1719
1720 =item domainselector HASHREF | LIST
1721
1722 Takes as input a hashref or list of key/value pairs with the following keys:
1723
1724 =over 4
1725
1726 =item pkgnum
1727
1728 Package number
1729
1730 =item domsvc
1731
1732 Service number of the selected item.
1733
1734 =back
1735
1736 Returns an HTML fragment for domain selection.
1737
1738 =cut
1739
1740 sub domainselector {
1741   my $param;
1742   if ( ref($_[0]) ) {
1743     $param = shift;
1744   } else {
1745     $param = { @_ };
1746   }
1747   my $domsvc= $param->{'domsvc'};
1748   my $rv = 
1749       domain_select_hash(map {$_ => $param->{$_}} qw(pkgnum svcpart pkgpart) );
1750   my $domains = $rv->{'domains'};
1751   $domsvc = $rv->{'domsvc'} unless $domsvc;
1752
1753   return '<INPUT TYPE="hidden" NAME="domsvc" VALUE="">'
1754     unless scalar(keys %$domains);
1755
1756   if (scalar(keys %$domains) == 1) {
1757     my $key;
1758     foreach(keys %$domains) {
1759       $key = $_;
1760     }
1761     return '<TR><TD ALIGN="right">Domain</TD><TD>'. $domains->{$key}.
1762            '<INPUT TYPE="hidden" NAME="domsvc" VALUE="'. $key. '"></TD></TR>'
1763   }
1764
1765   my $text .= qq!<TR><TD ALIGN="right">Domain</TD><TD><SELECT NAME="domsvc" SIZE=1 STYLE="width: 20em"><OPTION>(Choose Domain)!;
1766
1767
1768   foreach my $domain ( sort { $domains->{$a} cmp $domains->{$b} } keys %$domains ) {
1769     $text .= qq!<OPTION VALUE="!. $domain. '"'.
1770              ( ( $domsvc && $domain == $domsvc ) ? ' SELECTED' : '' ). ">".
1771              $domains->{$domain};
1772   }
1773
1774   $text .= qq!</SELECT></TD></TR>!;
1775
1776   $text;
1777
1778 }
1779
1780 =item didselector HASHREF | LIST
1781
1782 Takes as input a hashref or list of key/value pairs with the following keys:
1783
1784 =over 4
1785
1786 =item field
1787
1788 Field name for the returned HTML fragment.
1789
1790 =item svcpart
1791
1792 Service definition (see L<FS::part_svc>)
1793
1794 =back
1795
1796 Returns an HTML fragment for DID selection.
1797
1798 =cut
1799
1800 sub didselector {
1801   my $param;
1802   if ( ref($_[0]) ) {
1803     $param = shift;
1804   } else {
1805     $param = { @_ };
1806   }
1807
1808   my $rv = mason_comp( 'comp'=>'/elements/select-did.html',
1809                        'args'=>[ %$param ],
1810                      );
1811
1812   #hmm.
1813   $rv->{'error'} || $rv->{'output'};
1814
1815 }
1816
1817 =back
1818
1819 =head1 RESELLER FUNCTIONS
1820
1821 Note: Resellers can also use the B<signup_info> and B<new_customer> functions
1822 with their active session, and the B<customer_info> and B<order_pkg> functions
1823 with their active session and an additional I<custnum> parameter.
1824
1825 For the most part, development of the reseller web interface has been
1826 superceded by agent-virtualized access to the backend.
1827
1828 =over 4
1829
1830 =item agent_login
1831
1832 Agent login
1833
1834 =item agent_info
1835
1836 Agent info
1837
1838 =item agent_list_customers
1839
1840 List agent's customers.
1841
1842 =back
1843
1844 =head1 BUGS
1845
1846 =head1 SEE ALSO
1847
1848 L<freeside-selfservice-clientd>, L<freeside-selfservice-server>
1849
1850 =cut
1851
1852 1;
1853