1 package FS::SelfService;
4 use vars qw( $VERSION @ISA @EXPORT_OK $DEBUG
5 $skip_uid_check $dir $socket %autoload $tag );
11 use Storable 2.09 qw(nstore_fd fd_retrieve);
15 @ISA = qw( Exporter );
19 $dir = "/usr/local/freeside";
20 $socket = "$dir/selfservice_socket";
21 $socket .= '.'.$tag if defined $tag && length($tag);
23 #maybe should ask ClientAPI for this list
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 'switch_acct' => 'MyAccount/switch_acct',
32 'customer_info' => 'MyAccount/customer_info',
33 'customer_info_short' => 'MyAccount/customer_info_short',
34 'edit_info' => 'MyAccount/edit_info', #add to ss cgi!
35 'invoice' => 'MyAccount/invoice',
36 'invoice_pdf' => 'MyAccount/invoice_pdf',
37 'legacy_invoice' => 'MyAccount/legacy_invoice',
38 'legacy_invoice_pdf' => 'MyAccount/legacy_invoice_pdf',
39 'invoice_logo' => 'MyAccount/invoice_logo',
40 'list_invoices' => 'MyAccount/list_invoices', #?
41 'cancel' => 'MyAccount/cancel', #add to ss cgi!
42 'payment_info' => 'MyAccount/payment_info',
43 'payment_info_renew_info' => 'MyAccount/payment_info_renew_info',
44 'process_payment' => 'MyAccount/process_payment',
45 'store_payment' => 'MyAccount/store_payment',
46 'process_stored_payment' => 'MyAccount/process_stored_payment',
47 'process_payment_order_pkg' => 'MyAccount/process_payment_order_pkg',
48 'process_payment_change_pkg' => 'MyAccount/process_payment_change_pkg',
49 'process_payment_order_renew' => 'MyAccount/process_payment_order_renew',
50 'process_prepay' => 'MyAccount/process_prepay',
51 'realtime_collect' => 'MyAccount/realtime_collect',
52 'list_pkgs' => 'MyAccount/list_pkgs', #add to ss (added?)
53 'list_svcs' => 'MyAccount/list_svcs', #add to ss (added?)
54 'list_svc_usage' => 'MyAccount/list_svc_usage',
55 'svc_status_html' => 'MyAccount/svc_status_html',
56 'svc_status_hash' => 'MyAccount/svc_status_hash',
57 'set_svc_status_hash' => 'MyAccount/set_svc_status_hash',
58 'acct_forward_info' => 'MyAccount/acct_forward_info',
59 'process_acct_forward' => 'MyAccount/process_acct_forward',
60 'list_dsl_devices' => 'MyAccount/list_dsl_devices',
61 'add_dsl_device' => 'MyAccount/add_dsl_device',
62 'delete_dsl_device' => 'MyAccount/delete_dsl_device',
63 'port_graph' => 'MyAccount/port_graph',
64 'list_cdr_usage' => 'MyAccount/list_cdr_usage',
65 'list_support_usage' => 'MyAccount/list_support_usage',
66 'order_pkg' => 'MyAccount/order_pkg', #add to ss cgi!
67 'change_pkg' => 'MyAccount/change_pkg',
68 'order_recharge' => 'MyAccount/order_recharge',
69 'renew_info' => 'MyAccount/renew_info',
70 'order_renew' => 'MyAccount/order_renew',
71 'cancel_pkg' => 'MyAccount/cancel_pkg', #add to ss cgi!
72 'suspend_pkg' => 'MyAccount/suspend_pkg', #add to ss cgi!
73 'charge' => 'MyAccount/charge', #?
74 'part_svc_info' => 'MyAccount/part_svc_info',
75 'provision_acct' => 'MyAccount/provision_acct',
76 'provision_phone' => 'MyAccount/provision_phone',
77 'provision_external' => 'MyAccount/provision_external',
78 'unprovision_svc' => 'MyAccount/unprovision_svc',
79 'myaccount_passwd' => 'MyAccount/myaccount_passwd',
80 'reset_passwd' => 'MyAccount/reset_passwd',
81 'check_reset_passwd' => 'MyAccount/check_reset_passwd',
82 'process_reset_passwd' => 'MyAccount/process_reset_passwd',
83 'create_ticket' => 'MyAccount/create_ticket',
84 'get_ticket' => 'MyAccount/get_ticket',
85 'adjust_ticket_priority' => 'MyAccount/adjust_ticket_priority',
86 'did_report' => 'MyAccount/did_report',
87 'signup_info' => 'Signup/signup_info',
88 'skin_info' => 'MyAccount/skin_info',
89 'access_info' => 'MyAccount/access_info',
90 'domain_select_hash' => 'Signup/domain_select_hash', # expose?
91 'new_customer' => 'Signup/new_customer',
92 'capture_payment' => 'Signup/capture_payment',
93 #N/A 'clear_signup_cache' => 'Signup/clear_cache',
94 'new_agent' => 'Agent/new_agent',
95 'agent_login' => 'Agent/agent_login',
96 'agent_logout' => 'Agent/agent_logout',
97 'agent_info' => 'Agent/agent_info',
98 'agent_list_customers' => 'Agent/agent_list_customers',
99 'check_username' => 'Agent/check_username',
100 'suspend_username' => 'Agent/suspend_username',
101 'unsuspend_username' => 'Agent/unsuspend_username',
102 'mason_comp' => 'MasonComponent/mason_comp',
103 'call_time' => 'PrepaidPhone/call_time',
104 'call_time_nanpa' => 'PrepaidPhone/call_time_nanpa',
105 'phonenum_balance' => 'PrepaidPhone/phonenum_balance',
107 #'bulk_processrow' => 'Bulk/processrow',
108 #conflicts w/Agent one# 'check_username' => 'Bulk/check_username',
110 'ping' => 'SGNG/ping',
111 'decompify_pkgs' => 'SGNG/decompify_pkgs',
112 'previous_payment_info' => 'SGNG/previous_payment_info',
113 'previous_payment_info_renew_info'
114 => 'SGNG/previous_payment_info_renew_info',
115 'previous_process_payment' => 'SGNG/previous_process_payment',
116 'previous_process_payment_order_pkg'
117 => 'SGNG/previous_process_payment_order_pkg',
118 'previous_process_payment_change_pkg'
119 => 'SGNG/previous_process_payment_change_pkg',
120 'previous_process_payment_order_renew'
121 => 'SGNG/previous_process_payment_order_renew',
125 qw( regionselector regionselector_hashref location_form
126 expselect popselector domainselector didselector
130 $ENV{'PATH'} ='/usr/bin:/usr/ucb:/bin';
131 $ENV{'SHELL'} = '/bin/sh';
132 $ENV{'IFS'} = " \t\n";
135 $ENV{'BASH_ENV'} = '';
137 #you can add BEGIN { $FS::SelfService::skip_uid_check = 1; }
138 #if you grant appropriate permissions to whatever user
139 my $freeside_uid = scalar(getpwnam('freeside'));
140 die "not running as the freeside user\n"
141 if $> != $freeside_uid && ! $skip_uid_check;
143 -e $dir or die "FATAL: $dir doesn't exist!";
144 -d $dir or die "FATAL: $dir isn't a directory!";
145 -r $dir or die "FATAL: Can't read $dir as freeside user!";
146 -x $dir or die "FATAL: $dir not searchable (executable) as freeside user!";
148 foreach my $autoload ( keys %autoload ) {
151 "sub $autoload { ". '
156 #warn scalar(@_). ": ". join(" / ", @_);
160 $param->{_packet} = \''. $autoload{$autoload}. '\';
162 simple_packet($param);
172 warn "sending ". $packet->{_packet}. " to server"
174 socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
175 connect(SOCK, sockaddr_un($socket)) or die "connect to $socket: $!";
176 nstore_fd($packet, \*SOCK) or die "can't send packet: $!";
179 #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
181 #block until there is a message on socket
182 # my $w = new IO::Select;
184 # my @wait = $w->can_read;
186 warn "reading message from server"
189 my $return = fd_retrieve(\*SOCK) or die "error reading result: $!";
190 die $return->{'_error'} if defined $return->{_error} && $return->{_error};
192 warn "returning message to client"
200 FS::SelfService - Freeside self-service API
204 # password and shell account changes
205 use FS::SelfService qw(passwd chfn chsh);
207 # "my account" functionality
208 use FS::SelfService qw( login customer_info invoice cancel payment_info process_payment );
210 my $rv = login( { 'username' => $username,
212 'password' => $password,
216 if ( $rv->{'error'} ) {
217 #handle login error...
220 my $session_id = $rv->{'session_id'};
223 my $customer_info = customer_info( { 'session_id' => $session_id } );
225 #payment_info and process_payment are available in 1.5+ only
226 my $payment_info = payment_info( { 'session_id' => $session_id } );
228 #!!! process_payment example
230 #!!! list_pkgs example
232 #!!! order_pkg example
234 #!!! cancel_pkg example
236 # signup functionality
237 use FS::SelfService qw( signup_info new_customer );
239 my $signup_info = signup_info;
241 $rv = new_customer( {
244 'company' => $company,
245 'address1' => $address1,
246 'address2' => $address2,
250 'country' => $country,
251 'daytime' => $daytime,
255 'payinfo' => $payinfo,
257 'paystart_month' => $paystart_month
258 'paystart_year' => $paystart_year,
259 'payissue' => $payissue,
261 'paydate' => $paydate,
262 'payname' => $payname,
263 'invoicing_list' => $invoicing_list,
264 'referral_custnum' => $referral_custnum,
265 'agentnum' => $agentnum,
266 'pkgpart' => $pkgpart,
268 'username' => $username,
269 '_password' => $password,
273 'phonenum' => $phonenum,
278 my $error = $rv->{'error'};
279 if ( $error eq '_decline' ) {
289 Use this API to implement your own client "self-service" module.
291 If you just want to customize the look of the existing "self-service" module,
294 =head1 PASSWORD, GECOS, SHELL CHANGING FUNCTIONS
306 =head1 "MY ACCOUNT" FUNCTIONS
312 Creates a user session. Takes a hash reference as parameter with the
331 Returns a hash reference with the following keys:
337 Empty on success, or an error message on errors.
341 Session identifier for successful logins
345 =item customer_info HASHREF
347 Returns general customer information.
349 Takes a hash reference as parameter with a single key: B<session_id>
351 Returns a hash reference with the following keys:
365 Array reference of hash references of open inoices. Each hash reference has
366 the following keys: invnum, date, owed
370 An HTML fragment containing shipping and billing addresses.
372 =item The following fields are also returned
374 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
378 =item edit_info HASHREF
380 Takes a hash reference as parameter with any of the following keys:
382 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
384 If a field exists, the customer record is updated with the new value of that
385 field. If a field does not exist, that field is not changed on the customer
388 Returns a hash reference with a single key, B<error>, empty on success, or an
389 error message on errors
391 =item invoice HASHREF
393 Returns an invoice. Takes a hash reference as parameter with two keys:
394 session_id and invnum
396 Returns a hash reference with the following keys:
402 Empty on success, or an error message on errors
414 =item list_invoices HASHREF
416 Returns a list of all customer invoices. Takes a hash references with a single
419 Returns a hash reference with the following keys:
425 Empty on success, or an error message on errors
429 Reference to array of hash references with the following keys:
439 Invoice date, in UNIX epoch time
447 Cancels this customer.
449 Takes a hash reference as parameter with a single key: B<session_id>
451 Returns a hash reference with a single key, B<error>, which is empty on
452 success or an error message on errors.
454 =item payment_info HASHREF
456 Returns information that may be useful in displaying a payment page.
458 Takes a hash reference as parameter with a single key: B<session_id>.
460 Returns a hash reference with the following keys:
466 Empty on success, or an error message on errors
474 Exact name on credit card (CARD/DCRD)
498 Customer's current default payment type.
502 For CARD/DCRD payment types, the card type (Visa card, MasterCard, Discover card, American Express card, etc.)
506 For CARD/DCRD payment types, the card number
510 For CARD/DCRD payment types, expiration month
514 For CARD/DCRD payment types, expiration year
516 =item cust_main_county
518 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.
522 Array reference of all states in the current default country.
526 Hash reference of card types; keys are card types, values are the exact strings
527 passed to the process_payment function
531 #this doesn't actually work yet
535 #Unique transaction identifier (prevents multiple charges), passed to the
536 #process_payment function
540 =item process_payment HASHREF
542 Processes a payment and possible change of address or payment type. Takes a
543 hash reference as parameter with the following keys:
557 If true, address and card information entered will be saved for subsequent
562 If true, future credit card payments will be done automatically (sets payby to
563 CARD). If false, future credit card payments will be done on-demand (sets
564 payby to DCRD). This option only has meaning if B<save> is set true.
592 Two-letter country code
600 Card expiration month
608 #this doesn't actually work yet
612 #Unique transaction identifier, returned from the payment_info function.
613 #Prevents multiple charges.
617 Returns a hash reference with a single key, B<error>, empty on success, or an
618 error message on errors.
620 =item process_payment_order_pkg
622 Combines the B<process_payment> and B<order_pkg> functions in one step. If the
623 payment processes sucessfully, the package is ordered. Takes a hash reference
624 as parameter with the keys of both methods.
626 Returns a hash reference with a single key, B<error>, empty on success, or an
627 error message on errors.
629 =item process_payment_change_pkg
631 Combines the B<process_payment> and B<change_pkg> functions in one step. If the
632 payment processes sucessfully, the package is ordered. Takes a hash reference
633 as parameter with the keys of both methods.
635 Returns a hash reference with a single key, B<error>, empty on success, or an
636 error message on errors.
639 =item process_payment_order_renew
641 Combines the B<process_payment> and B<order_renew> functions in one step. If
642 the payment processes sucessfully, the renewal is processed. Takes a hash
643 reference as parameter with the keys of both methods.
645 Returns a hash reference with a single key, B<error>, empty on success, or an
646 error message on errors.
650 Returns package information for this customer. For more detail on services,
653 Takes a hash reference as parameter with a single key: B<session_id>
655 Returns a hash reference containing customer package information. The hash reference contains the following keys:
665 Empty on success, or an error message on errors.
667 =item cust_pkg HASHREF
669 Array reference of hash references, each of which has the fields of a cust_pkg
670 record (see L<FS::cust_pkg>) as well as the fields below. Note these are not
671 the internal FS:: objects, but hash references of columns and values.
675 =item part_pkg fields
677 All fields of part_pkg for this specific cust_pkg (be careful with this
678 information - it may reveal more about your available packages than you would
679 like users to know in aggregate)
683 #XXX pare part_pkg fields down to a more secure subset
687 An array of hash references indicating information on unprovisioned services
688 available for provisioning for this specific cust_pkg. Each has the following
693 =item part_svc fields
695 All fields of part_svc (be careful with this information - it may reveal more
696 about your available packages than you would like users to know in aggregate)
700 #XXX pare part_svc fields down to a more secure subset
706 An array of hash references indicating information on the customer services
707 already provisioned for this specific cust_pkg. Each has the following keys:
713 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.
719 Primary key for this service
723 Service definition (see L<FS::part_svc>)
727 Customer package (see L<FS::cust_pkg>)
731 Blank if the service is not over limit, or the date the service exceeded its usage limit (as a UNIX timestamp).
739 Returns service information for this customer.
741 Takes a hash reference as parameter with a single key: B<session_id>
743 Returns a hash reference containing customer package information. The hash reference contains the following keys:
753 An array of hash references indicating information on all of this customer's
754 services. Each has the following keys:
760 Primary key for this service
768 Meaningful user-specific identifier for the service (i.e. username, domain, or
773 Account (svc_acct) services also have the following keys:
791 Upload bytes remaining
795 Download bytes remaining
799 Total bytes remaining
801 =item recharge_amount
805 =item recharge_seconds
807 Number of seconds gained by recharge
809 =item recharge_upbytes
811 Number of upload bytes gained by recharge
813 =item recharge_downbytes
815 Number of download bytes gained by recharge
817 =item recharge_totalbytes
819 Number of total bytes gained by recharge
827 Orders a package for this customer.
829 Takes a hash reference as parameter with the following keys:
839 Package to order (see L<FS::part_pkg>).
843 Service to order (see L<FS::part_svc>).
845 Normally optional; required only to provision a non-svc_acct service, or if the
846 package definition does not contain one svc_acct service definition with
847 quantity 1 (it may contain others with quantity >1). A svcpart of "none" can
848 also be specified to indicate that no initial service should be provisioned.
852 Fields used when provisioning an svc_acct service:
866 Optional security phrase
870 Optional Access number number
874 Fields used when provisioning an svc_domain service:
884 Fields used when provisioning an svc_phone service:
902 Fields used when provisioning an svc_external service:
916 Fields used when provisioning an svc_pbx service:
930 Returns a hash reference with a single key, B<error>, empty on success, or an
931 error message on errors. The special error '_decline' is returned for
932 declined transactions.
936 Changes a package for this customer.
938 Takes a hash reference as parameter with the following keys:
948 Existing customer package.
952 New package to order (see L<FS::part_pkg>).
956 Returns a hash reference with the following keys:
962 Empty on success, or an error message on errors.
966 On success, the new pkgnum
973 Provides useful info for early renewals.
975 Takes a hash reference as parameter with the following keys:
985 Returns a hash reference. On errors, it contains a single key, B<error>, with
986 the error message. Otherwise, contains a single key, B<dates>, pointing to
987 an array refernce of hash references. Each hash reference contains the
994 (Future) Bill date. Indicates a future date for which billing could be run.
995 Specified as a integer UNIX timestamp. Pass this value to the B<order_renew>
998 =item bill_date_pretty
1000 (Future) Bill date as a human-readable string. (Convenience for display;
1001 subject to change, so best not to parse for the date.)
1005 Base amount which will be charged if renewed early as of this date.
1009 Renewal date; i.e. even-futher future date at which the customer will be paid
1010 through if the early renewal is completed with the given B<bill-date>.
1011 Specified as a integer UNIX timestamp.
1013 =item renew_date_pretty
1015 Renewal date as a human-readable string. (Convenience for display;
1016 subject to change, so best not to parse for the date.)
1020 Package that will be renewed.
1024 Expiration date of the package that will be renewed.
1026 =item expire_date_pretty
1028 Expiration date of the package that will be renewed, as a human-readable
1029 string. (Convenience for display; subject to change, so best not to parse for
1036 Renews this customer early; i.e. runs billing for this customer in advance.
1038 Takes a hash reference as parameter with the following keys:
1048 Integer date as returned by the B<renew_info> function, indicating the advance
1049 date for which to run billing.
1053 Returns a hash reference with a single key, B<error>, empty on success, or an
1054 error message on errors.
1058 Cancels a package for this customer.
1060 Takes a hash reference as parameter with the following keys:
1070 pkgpart of package to cancel
1074 Returns a hash reference with a single key, B<error>, empty on success, or an
1075 error message on errors.
1079 =head1 SIGNUP FUNCTIONS
1083 =item signup_info HASHREF
1085 Takes a hash reference as parameter with the following keys:
1089 =item session_id - Optional agent/reseller interface session
1093 Returns a hash reference containing information that may be useful in
1094 displaying a signup page. The hash reference contains the following keys:
1098 =item cust_main_county
1100 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.
1104 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
1105 an agentnum specified explicitly via reseller interface session_id in the
1110 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.
1112 =item agentnum2part_pkg
1114 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.
1118 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.
1120 =item security_phrase
1122 True if the "security_phrase" feature is enabled
1126 Array reference of acceptable payment types for signup
1132 credit card - automatic
1136 credit card - on-demand - version 1.5+ only
1140 electronic check - automatic
1144 electronic check - on-demand - version 1.5+ only
1152 billing, not recommended for signups
1156 free, definitely not recommended for signups
1160 special billing type: applies a credit (see FS::prepay_credit) and sets billing type to BILL
1166 True if CVV features are available (1.5+ or 1.4.2 with CVV schema patch)
1170 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".
1176 =item countrydefault
1182 =item new_customer HASHREF
1184 Creates a new customer. Takes a hash reference as parameter with the
1191 first name (required)
1195 last name (required)
1199 (not typically collected; mostly used for ACH transactions)
1205 =item address1 (required)
1213 =item city (required)
1221 =item state (required)
1225 =item zip (required)
1231 Daytime phone number
1235 Evening phone number
1243 CARD, DCRD, CHEK, DCHK, LECB, BILL, COMP or PREPAY (see L</signup_info> (required)
1247 Card number for CARD/DCRD, account_number@aba_number for CHEK/DCHK, prepaid "pin" for PREPAY, purchase order number for BILL
1251 Credit card CVV2 number (1.5+ or 1.4.2 with CVV schema patch)
1255 Expiration date for CARD/DCRD
1259 Exact name on credit card for CARD/DCRD, bank name for CHEK/DCHK
1261 =item invoicing_list
1263 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),
1265 =item referral_custnum
1267 referring customer number
1275 pkgpart of initial package
1291 Access number (index, not the literal number)
1295 Country code (to be provisioned as a service)
1299 Phone number (to be provisioned as a service)
1307 Returns a hash reference with the following keys:
1313 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)
1317 =item regionselector HASHREF | LIST
1319 Takes as input a hashref or list of key/value pairs with the following keys:
1323 =item selected_county
1325 Currently selected county
1327 =item selected_state
1329 Currently selected state
1331 =item selected_country
1333 Currently selected country
1337 Specify a unique prefix string if you intend to use the HTML output multiple time son one page.
1341 Specify a javascript subroutine to call on changes
1347 =item default_country
1353 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>.
1357 Returns a list consisting of three HTML fragments for county selection,
1358 state selection and country selection, respectively.
1362 #false laziness w/FS::cust_main_county (this is currently the "newest" version)
1363 sub regionselector {
1370 $param->{'selected_country'} ||= $param->{'default_country'};
1371 $param->{'selected_state'} ||= $param->{'default_state'};
1373 my $prefix = exists($param->{'prefix'}) ? $param->{'prefix'} : '';
1377 my %cust_main_county;
1379 # unless ( @cust_main_county ) { #cache
1380 #@cust_main_county = qsearch('cust_main_county', {} );
1381 #foreach my $c ( @cust_main_county ) {
1382 foreach my $c ( @{ $param->{'locales'} } ) {
1383 #$countyflag=1 if $c->county;
1384 $countyflag=1 if $c->{county};
1385 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
1386 #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
1387 $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
1390 $countyflag=1 if $param->{selected_county};
1392 my $script_html = <<END;
1394 function opt(what,value,text) {
1395 var optionName = new Option(text, value, false, false);
1396 var length = what.length;
1397 what.options[length] = optionName;
1399 function ${prefix}country_changed(what) {
1400 country = what.options[what.selectedIndex].text;
1401 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
1402 what.form.${prefix}state.options[i] = null;
1404 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
1406 foreach my $country ( sort keys %cust_main_county ) {
1407 $script_html .= "\nif ( country == \"$country\" ) {\n";
1408 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
1409 my $text = $state || '(n/a)';
1410 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
1412 $script_html .= "}\n";
1415 $script_html .= <<END;
1417 function ${prefix}state_changed(what) {
1420 if ( $countyflag ) {
1421 $script_html .= <<END;
1422 state = what.options[what.selectedIndex].text;
1423 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
1424 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
1425 what.form.${prefix}county.options[i] = null;
1428 foreach my $country ( sort keys %cust_main_county ) {
1429 $script_html .= "\nif ( country == \"$country\" ) {\n";
1430 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
1431 $script_html .= "\nif ( state == \"$state\" ) {\n";
1432 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
1433 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
1434 my $text = $county || '(n/a)';
1436 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
1438 $script_html .= "}\n";
1440 $script_html .= "}\n";
1444 $script_html .= <<END;
1449 my $county_html = $script_html;
1450 if ( $countyflag ) {
1451 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$param->{'onchange'}">!;
1452 foreach my $county (
1453 sort keys %{ $cust_main_county{$param->{'selected_country'}}{$param->{'selected_state'}} }
1455 my $text = $county || '(n/a)';
1456 $county_html .= qq!<OPTION VALUE="$county"!.
1457 ($county eq $param->{'selected_county'} ?
1464 $county_html .= '</SELECT>';
1467 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$param->{'selected_county'}">!;
1470 my $state_html = qq!<SELECT NAME="${prefix}state" !.
1471 qq!onChange="${prefix}state_changed(this); $param->{'onchange'}">!;
1472 foreach my $state ( sort keys %{ $cust_main_county{$param->{'selected_country'}} } ) {
1473 my $text = $state || '(n/a)';
1474 my $selected = $state eq $param->{'selected_state'} ? 'SELECTED' : '';
1475 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
1477 $state_html .= '</SELECT>';
1479 my $country_html = '';
1480 if ( scalar( keys %cust_main_county ) > 1 ) {
1482 $country_html = qq(<SELECT NAME="${prefix}country" ).
1483 qq(onChange="${prefix}country_changed(this); ).
1484 $param->{'onchange'}.
1487 my $countrydefault = $param->{default_country} || 'US';
1488 foreach my $country (
1489 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
1490 keys %cust_main_county
1492 my $selected = $country eq $param->{'selected_country'}
1495 $country_html .= "\n<OPTION$selected>$country</OPTION>"
1497 $country_html .= '</SELECT>';
1500 $country_html = qq(<INPUT TYPE="hidden" NAME="${prefix}country" ).
1501 ' VALUE="'. (keys %cust_main_county )[0]. '">';
1505 ($county_html, $state_html, $country_html);
1509 sub regionselector_hashref {
1510 my ($county_html, $state_html, $country_html) = regionselector(@_);
1512 'county_html' => $county_html,
1513 'state_html' => $state_html,
1514 'country_html' => $country_html,
1518 =item location_form HASHREF | LIST
1520 Takes as input a hashref or list of key/value pairs with the following keys:
1526 Current customer session_id
1530 Omit red asterisks from required fields.
1532 =item address1_label
1534 Label for first address line.
1538 Returns an HTML fragment for a location form (address, city, state, zip,
1551 my $session_id = delete $param->{'session_id'};
1553 my $rv = mason_comp( 'session_id' => $session_id,
1554 'comp' => '/elements/location.html',
1555 'args' => [ %$param ],
1559 $rv->{'error'} || $rv->{'output'};
1564 #=item expselect HASHREF | LIST
1566 #Takes as input a hashref or list of key/value pairs with the following keys:
1570 #=item prefix - Specify a unique prefix string if you intend to use the HTML output multiple time son one page.
1572 #=item date - current date, in yyyy-mm-dd or m-d-yyyy format
1576 =item expselect PREFIX [ DATE ]
1578 Takes as input a unique prefix string and the current expiration date, in
1579 yyyy-mm-dd or m-d-yyyy format
1581 Returns an HTML fragments for expiration date selection.
1587 #if ( ref($_[0]) ) {
1591 #my $prefix = $param->{'prefix'};
1592 #my $prefix = exists($param->{'prefix'}) ? $param->{'prefix'} : '';
1593 #my $date = exists($param->{'date'}) ? $param->{'date'} : '';
1595 my $date = scalar(@_) ? shift : '';
1597 my( $m, $y ) = ( 0, 0 );
1598 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
1599 ( $m, $y ) = ( $2, $1 );
1600 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
1601 ( $m, $y ) = ( $1, $3 );
1603 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
1605 $return .= qq!<OPTION VALUE="$_"!;
1606 $return .= " SELECTED" if $_ == $m;
1609 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
1611 my $thisYear = $t[5] + 1900;
1612 for ( ($thisYear > $y && $y > 0 ? $y : $thisYear) .. ($thisYear+10) ) {
1613 $return .= qq!<OPTION VALUE="$_"!;
1614 $return .= " SELECTED" if $_ == $y;
1617 $return .= "</SELECT>";
1622 =item popselector HASHREF | LIST
1624 Takes as input a hashref or list of key/value pairs with the following keys:
1630 Access number number
1634 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>.
1638 Returns an HTML fragment for access number selection.
1642 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
1650 my $popnum = $param->{'popnum'};
1651 my $pops = $param->{'pops'};
1653 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
1654 return $pops->[0]{city}. ', '. $pops->[0]{state}.
1655 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}. '-'. $pops->[0]{loc}.
1656 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
1657 if scalar(@$pops) == 1;
1660 my %popnum2pop = ();
1662 push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
1663 $popnum2pop{$_->{popnum}} = $_;
1668 function opt(what,href,text) {
1669 var optionName = new Option(text, href, false, false)
1670 var length = what.length;
1671 what.options[length] = optionName;
1675 my $init_popstate = $param->{'init_popstate'};
1676 if ( $init_popstate ) {
1677 $text .= '<INPUT TYPE="hidden" NAME="init_popstate" VALUE="'.
1678 $init_popstate. '">';
1681 function acstate_changed(what) {
1682 state = what.options[what.selectedIndex].text;
1683 what.form.popac.options.length = 0
1684 what.form.popac.options[0] = new Option("Area code", "-1", false, true);
1688 my @states = $init_popstate ? ( $init_popstate ) : keys %pop;
1689 foreach my $state ( sort { $a cmp $b } @states ) {
1690 $text .= "\nif ( state == \"$state\" ) {\n" unless $init_popstate;
1692 foreach my $ac ( sort { $a cmp $b } keys %{ $pop{$state} }) {
1693 $text .= "opt(what.form.popac, \"$ac\", \"$ac\");\n";
1694 if ($ac eq $param->{'popac'}) {
1695 $text .= "what.form.popac.options[what.form.popac.length-1].selected = true;\n";
1698 $text .= "}\n" unless $init_popstate;
1700 $text .= "popac_changed(what.form.popac)}\n";
1703 function popac_changed(what) {
1704 ac = what.options[what.selectedIndex].text;
1705 what.form.popnum.options.length = 0;
1706 what.form.popnum.options[0] = new Option("City", "-1", false, true);
1710 foreach my $state ( @states ) {
1711 foreach my $popac ( keys %{ $pop{$state} } ) {
1712 $text .= "\nif ( ac == \"$popac\" ) {\n";
1714 foreach my $pop ( @{$pop{$state}->{$popac}}) {
1715 my $o_popnum = $pop->{popnum};
1716 my $poptext = $pop->{city}. ', '. $pop->{state}.
1717 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
1719 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n";
1720 if ($popnum == $o_popnum) {
1721 $text .= "what.form.popnum.options[what.form.popnum.length-1].selected = true;\n";
1729 $text .= "}\n</SCRIPT>\n";
1731 $param->{'acstate'} = '' unless defined($param->{'acstate'});
1734 qq!<TABLE CELLPADDING="0"><TR><TD><SELECT NAME="acstate"! .
1735 qq!SIZE=1 onChange="acstate_changed(this)"><OPTION VALUE=-1>State!;
1736 $text .= "<OPTION" . ($_ eq $param->{'acstate'} ? " SELECTED" : "") .
1737 ">$_" foreach sort { $a cmp $b } @states;
1738 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD>';
1741 qq!<SELECT NAME="popac" SIZE=1 onChange="popac_changed(this)">!.
1742 qq!<OPTION>Area code</SELECT></TR><TR VALIGN="top">!;
1744 $text .= qq!<TR><TD><SELECT NAME="popnum" SIZE=1 STYLE="width: 20em"><OPTION>City!;
1747 #comment this block to disable initial list polulation
1748 my @initial_select = ();
1749 if ( scalar( @$pops ) > 100 ) {
1750 push @initial_select, $popnum2pop{$popnum} if $popnum2pop{$popnum};
1752 @initial_select = @$pops;
1754 foreach my $pop ( sort { $a->{state} cmp $b->{state} } @initial_select ) {
1755 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
1756 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
1757 $pop->{city}. ', '. $pop->{state}.
1758 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
1761 $text .= qq!</SELECT></TD></TR></TABLE>!;
1767 =item domainselector HASHREF | LIST
1769 Takes as input a hashref or list of key/value pairs with the following keys:
1779 Service number of the selected item.
1783 Returns an HTML fragment for domain selection.
1787 sub domainselector {
1794 my $domsvc= $param->{'domsvc'};
1796 domain_select_hash(map {$_ => $param->{$_}} qw(pkgnum svcpart pkgpart) );
1797 my $domains = $rv->{'domains'};
1798 $domsvc = $rv->{'domsvc'} unless $domsvc;
1800 return '<INPUT TYPE="hidden" NAME="domsvc" VALUE="">'
1801 unless scalar(keys %$domains);
1803 if (scalar(keys %$domains) == 1) {
1805 foreach(keys %$domains) {
1808 return '<TR><TD ALIGN="right">Domain</TD><TD>'. $domains->{$key}.
1809 '<INPUT TYPE="hidden" NAME="domsvc" VALUE="'. $key. '"></TD></TR>'
1812 my $text .= qq!<TR><TD ALIGN="right">Domain</TD><TD><SELECT NAME="domsvc" SIZE=1 STYLE="width: 20em"><OPTION>(Choose Domain)!;
1815 foreach my $domain ( sort { $domains->{$a} cmp $domains->{$b} } keys %$domains ) {
1816 $text .= qq!<OPTION VALUE="!. $domain. '"'.
1817 ( ( $domsvc && $domain == $domsvc ) ? ' SELECTED' : '' ). ">".
1818 $domains->{$domain};
1821 $text .= qq!</SELECT></TD></TR>!;
1827 =item didselector HASHREF | LIST
1829 Takes as input a hashref or list of key/value pairs with the following keys:
1835 Field name for the returned HTML fragment.
1839 Service definition (see L<FS::part_svc>)
1843 Returns an HTML fragment for DID selection.
1855 my $rv = mason_comp( 'comp'=>'/elements/select-did.html',
1856 'args'=>[ %$param ],
1860 $rv->{'error'} || $rv->{'output'};
1866 =head1 RESELLER FUNCTIONS
1868 Note: Resellers can also use the B<signup_info> and B<new_customer> functions
1869 with their active session, and the B<customer_info> and B<order_pkg> functions
1870 with their active session and an additional I<custnum> parameter.
1872 For the most part, development of the reseller web interface has been
1873 superceded by agent-virtualized access to the backend.
1885 =item agent_list_customers
1887 List agent's customers.
1895 L<freeside-selfservice-clientd>, L<freeside-selfservice-server>