self-service can select domain on provision (#2801)
[freeside.git] / FS / FS / Setup.pm
1 package FS::Setup;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK );
5 use Exporter;
6 #use Tie::DxHash;
7 use Tie::IxHash;
8 use FS::UID qw( dbh driver_name );
9 use FS::Record;
10
11 use FS::svc_domain;
12 $FS::svc_domain::whois_hack = 1;
13 $FS::svc_domain::whois_hack = 1;
14
15 @ISA = qw( Exporter );
16 @EXPORT_OK = qw( create_initial_data );
17
18 =head1 NAME
19
20 FS::Setup - Database setup
21
22 =head1 SYNOPSIS
23
24   use FS::Setup;
25
26 =head1 DESCRIPTION
27
28 Currently this module simply provides a place to store common subroutines for
29 database setup.
30
31 =head1 SUBROUTINES
32
33 =over 4
34
35 =item
36
37 =cut
38
39 sub create_initial_data {
40   my %opt = @_;
41
42   my $oldAutoCommit = $FS::UID::AutoCommit;
43   local $FS::UID::AutoCommit = 0;
44   $FS::UID::AutoCommit = 0;
45
46   populate_locales();
47
48   populate_duplock();
49
50   #initial_data data
51   populate_initial_data(%opt);
52
53   populate_access();
54
55   populate_msgcat();
56   
57   if ( $oldAutoCommit ) {
58     dbh->commit or die dbh->errstr;
59   }
60
61 }
62
63 sub populate_locales {
64
65   use Locale::Country;
66   use FS::cust_main_county;
67
68   #cust_main_county
69   foreach my $country ( sort map uc($_), all_country_codes ) {
70     _add_country($country);
71   }
72
73 }
74
75 sub populate_addl_locales {
76
77   my %addl = (
78     'US' => {
79       'FM' => 'Federated States of Micronesia',
80       'MH' => 'Marshall Islands',
81       'PW' => 'Palau',
82       'AA' => "Armed Forces Americas (except Canada)",
83       'AE' => "Armed Forces Europe / Canada / Middle East / Africa",
84       'AP' => "Armed Forces Pacific",
85     },
86   );
87
88   foreach my $country ( keys %addl ) {
89     foreach my $state ( keys %{ $addl{$country} } ) {
90       # $longname = $addl{$country}{$state};
91       _add_locale( 'country'=>$country, 'state'=>$state);
92     }
93   }
94
95 }
96
97 sub _add_country {
98
99   use Locale::SubCountry;
100
101   my( $country ) = shift;
102
103   my $subcountry = eval { new Locale::SubCountry($country) };
104   my @states = $subcountry ? $subcountry->all_codes : undef;
105   
106   if ( !scalar(@states) || ( scalar(@states)==1 && !defined($states[0]) ) ) {
107
108     _add_locale( 'country'=>$country );
109   
110   } else {
111   
112     if ( $states[0] =~ /^(\d+|\w)$/ ) {
113       @states = map $subcountry->full_name($_), @states
114     }
115   
116     foreach my $state ( @states ) {
117       _add_locale( 'country'=>$country, 'state'=>$state);
118     }
119     
120   }
121
122 }
123
124 sub _add_locale {
125   my $cust_main_county = new FS::cust_main_county( { 'tax'=>0, @_ });  
126   my $error = $cust_main_county->insert;
127   die $error if $error;
128 }
129
130 sub populate_duplock {
131
132   return unless driver_name =~ /^mysql/i;
133
134   my $sth = dbh->prepare(
135     "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
136   ) or die dbh->errstr;
137
138   $sth->execute or die $sth->errstr;
139
140 }
141
142 sub populate_initial_data {
143   my %opt = @_;
144
145   my $data = initial_data(%opt);
146
147   foreach my $table ( keys %$data ) {
148
149     my $class = "FS::$table";
150     eval "use $class;";
151     die $@ if $@;
152
153     $class->_populate_initial_data(%opt)
154       if $class->can('_populate_inital_data');
155
156     my @records = @{ $data->{$table} };
157
158     foreach my $record ( @records ) {
159       my $args = delete($record->{'_insert_args'}) || [];
160       my $object = $class->new( $record );
161       my $error = $object->insert( @$args );
162       die "error inserting record into $table: $error\n"
163         if $error;
164     }
165
166   }
167
168 }
169
170 sub initial_data {
171   my %opt = @_;
172
173   #tie my %hash, 'Tie::DxHash', 
174   tie my %hash, 'Tie::IxHash', 
175
176     #superuser group
177     'access_group' => [
178       { 'groupname' => 'Superuser' },
179     ],
180
181     #reason types
182     'reason_type' => [],
183
184 #XXX need default new-style billing events
185 #    #billing events
186 #    'part_bill_event' => [
187 #      { 'payby'     => 'CARD',
188 #        'event'     => 'Batch card',
189 #        'seconds'   => 0,
190 #        'eventcode' => '$cust_bill->batch_card(%options);',
191 #        'weight'    => 40,
192 #        'plan'      => 'batch-card',
193 #      },
194 #      { 'payby'     => 'BILL',
195 #        'event'     => 'Send invoice',
196 #        'seconds'   => 0,
197 #        'eventcode' => '$cust_bill->send();',
198 #        'weight'    => 50,
199 #        'plan'      => 'send',
200 #      },
201 #      { 'payby'     => 'DCRD',
202 #        'event'     => 'Send invoice',
203 #        'seconds'   => 0,
204 #        'eventcode' => '$cust_bill->send();',
205 #        'weight'    => 50,
206 #        'plan'      => 'send',
207 #      },
208 #      { 'payby'     => 'DCHK',
209 #        'event'     => 'Send invoice',
210 #        'seconds'   => 0,
211 #        'eventcode' => '$cust_bill->send();',
212 #        'weight'    => 50,
213 #        'plan'      => 'send',
214 #      },
215 #      { 'payby'     => 'DCLN',
216 #        'event'     => 'Suspend',
217 #        'seconds'   => 0,
218 #        'eventcode' => '$cust_bill->suspend();',
219 #        'weight'    => 40,
220 #        'plan'      => 'suspend',
221 #      },
222 #      #{ 'payby'     => 'DCLN',
223 #      #  'event'     => 'Retriable',
224 #      #  'seconds'   => 0,
225 #      #  'eventcode' => '$cust_bill_event->retriable();',
226 #      #  'weight'    => 60,
227 #      #  'plan'      => 'retriable',
228 #      #},
229 #    ],
230     
231     #you must create a service definition. An example of a service definition
232     #would be a dial-up account or a domain. First, it is necessary to create a
233     #domain definition. Click on View/Edit service definitions and Add a new
234     #service definition with Table svc_domain (and no modifiers).
235     'part_svc' => [
236       { 'svc'   => 'Domain',
237         'svcdb' => 'svc_domain',
238       }
239     ],
240
241     #Now that you have created your first service, you must create a package
242     #including this service which you can sell to customers. Zero, one, or many
243     #services are bundled into a package. Click on View/Edit package
244     #definitions and Add a new package definition which includes quantity 1 of
245     #the svc_domain service you created above.
246     'part_pkg' => [
247       { 'pkg'     => 'System Domain',
248         'comment' => '(NOT FOR CUSTOMERS)',
249         'freq'    => '0',
250         'plan'    => 'flat',
251         '_insert_args' => [
252           'pkg_svc'     => { 1 => 1 }, # XXX
253           'primary_svc' => 1, #XXX
254           'options'     => {
255             'setup_fee' => '0',
256             'recur_fee' => '0',
257           },
258         ],
259       },
260     ],
261
262     #After you create your first package, then you must define who is able to
263     #sell that package by creating an agent type. An example of an agent type
264     #would be an internal sales representitive which sells regular and
265     #promotional packages, as opposed to an external sales representitive
266     #which would only sell regular packages of services. Click on View/Edit
267     #agent types and Add a new agent type.
268     'agent_type' => [
269       { 'atype' => 'internal' },
270     ],
271
272     #Allow this agent type to sell the package you created above.
273     'type_pkgs' => [
274       { 'typenum' => 1, #XXX
275         'pkgpart' => 1, #XXX
276       },
277     ],
278
279     #After creating a new agent type, you must create an agent. Click on
280     #View/Edit agents and Add a new agent.
281     'agent' => [
282       { 'agent'   => 'Internal',
283         'typenum' => 1, # XXX
284       },
285     ],
286
287     #Set up at least one Advertising source. Advertising sources will help you
288     #keep track of how effective your advertising is, tracking where customers
289     #heard of your service offerings. You must create at least one advertising
290     #source. If you do not wish to use the referral functionality, simply
291     #create a single advertising source only. Click on View/Edit advertising
292     #sources and Add a new advertising source.
293     'part_referral' => [
294       { 'referral' => 'Internal', },
295     ],
296     
297     #Click on New Customer and create a new customer for your system accounts
298     #with billing type Complimentary. Leave the First package dropdown set to
299     #(none).
300     'cust_main' => [
301       { 'agentnum'  => 1, #XXX
302         'refnum'    => 1, #XXX
303         'first'     => 'System',
304         'last'      => 'Accounts',
305         'address1'  => '1234 System Lane',
306         'city'      => 'Systemtown',
307         'state'     => 'CA',
308         'zip'       => '54321',
309         'country'   => 'US',
310         'payby'     => 'COMP',
311         'payinfo'   => 'system', #or something
312         'paydate'   => '1/2037',
313       },
314     ],
315
316     #From the Customer View screen of the newly created customer, order the
317     #package you defined above.
318     'cust_pkg' => [
319       { 'custnum' => 1, #XXX
320         'pkgpart' => 1, #XXX
321       },
322     ],
323
324     #From the Package View screen of the newly created package, choose
325     #(Provision) to add the customer's service for this new package.
326     #Add your own domain.
327     'svc_domain' => [
328       { 'domain'  => $opt{'domain'},
329         'pkgnum'  => 1, #XXX
330         'svcpart' => 1, #XXX
331         'action'  => 'N', #pseudo-field
332       },
333     ],
334
335     #Go back to View/Edit service definitions on the main menu, and Add a new
336     #service definition with Table svc_acct. Select your domain in the domsvc
337     #Modifier. Set Fixed to define a service locked-in to this domain, or
338     #Default to define a service which may select from among this domain and
339     #the customer's domains.
340
341     #not yet....
342
343   #)
344   ;
345
346   \%hash;
347
348 }
349
350 sub populate_access {
351
352   use FS::AccessRight;
353   use FS::access_right;
354
355   foreach my $rightname ( FS::AccessRight->rights ) {
356     my $access_right = new FS::access_right {
357       'righttype'   => 'FS::access_group',
358       'rightobjnum' => 1, #$supergroup->groupnum,
359       'rightname'   => $rightname,
360     };
361     my $ar_error = $access_right->insert;
362     die $ar_error if $ar_error;
363   }
364
365   #foreach my $agent ( qsearch('agent', {} ) ) {
366     my $access_groupagent = new FS::access_groupagent {
367       'groupnum' => 1, #$supergroup->groupnum,
368       'agentnum' => 1, #$agent->agentnum,
369     };
370     my $aga_error = $access_groupagent->insert;
371     die $aga_error if $aga_error;
372   #}
373
374 }
375
376 sub populate_msgcat {
377
378   use FS::Record qw(qsearch);
379   use FS::msgcat;
380
381   foreach my $del_msgcat ( qsearch('msgcat', {}) ) {
382     my $error = $del_msgcat->delete;
383     die $error if $error;
384   }
385
386   my %messages = msgcat_messages();
387
388   foreach my $msgcode ( keys %messages ) {
389     foreach my $locale ( keys %{$messages{$msgcode}} ) {
390       my $msgcat = new FS::msgcat( {
391         'msgcode' => $msgcode,
392         'locale'  => $locale,
393         'msg'     => $messages{$msgcode}{$locale},
394       });
395       my $error = $msgcat->insert;
396       die $error if $error;
397     }
398   }
399
400 }
401
402 sub msgcat_messages {
403
404   #  'msgcode' => {
405   #    'en_US' => 'Message',
406   #  },
407
408   (
409
410     'passwords_dont_match' => {
411       'en_US' => "Passwords don't match",
412     },
413
414     'invalid_card' => {
415       'en_US' => 'Invalid credit card number',
416     },
417
418     'unknown_card_type' => {
419       'en_US' => 'Unknown card type',
420     },
421
422     'not_a' => {
423       'en_US' => 'Not a ',
424     },
425
426     'empty_password' => {
427       'en_US' => 'Empty password',
428     },
429
430     'no_access_number_selected' => {
431       'en_US' => 'No access number selected',
432     },
433
434     'illegal_text' => {
435       'en_US' => 'Illegal (text)',
436       #'en_US' => 'Only letters, numbers, spaces, and the following punctuation symbols are permitted: ! @ # $ % & ( ) - + ; : \' " , . ? / in field',
437     },
438
439     'illegal_or_empty_text' => {
440       'en_US' => 'Illegal or empty (text)',
441       #'en_US' => 'Only letters, numbers, spaces, and the following punctuation symbols are permitted: ! @ # $ % & ( ) - + ; : \' " , . ? / in required field',
442     },
443
444     'illegal_username' => {
445       'en_US' => 'Illegal username',
446     },
447
448     'illegal_password' => {
449       'en_US' => 'Illegal password (',
450     },
451
452     'illegal_password_characters' => {
453       'en_US' => ' characters)',
454     },
455
456     'username_in_use' => {
457       'en_US' => 'Username in use',
458     },
459
460     'illegal_email_invoice_address' => {
461       'en_US' => 'Illegal email invoice address',
462     },
463
464     'illegal_name' => {
465       'en_US' => 'Illegal (name)',
466       #'en_US' => 'Only letters, numbers, spaces and the following punctuation symbols are permitted: , . - \' in field',
467     },
468
469     'illegal_phone' => {
470       'en_US' => 'Illegal (phone)',
471       #'en_US' => '',
472     },
473
474     'illegal_zip' => {
475       'en_US' => 'Illegal (zip)',
476       #'en_US' => '',
477     },
478
479     'expired_card' => {
480       'en_US' => 'Expired card',
481     },
482
483     'daytime' => {
484       'en_US' => 'Day Phone',
485     },
486
487     'night' => {
488       'en_US' => 'Night Phone',
489     },
490
491     'svc_external-id' => {
492       'en_US' => 'External ID',
493     },
494
495     'svc_external-title' => {
496       'en_US' => 'Title',
497     },
498
499     'stateid' => {
500       'en_US' => 'Driver\'s License',
501     },
502
503     'stateid_state' => {
504       'en_US' => 'Driver\'s License State',
505     },
506
507     'invalid_domain' => {
508       'en_US' => 'Invalid domain',
509     },
510
511   );
512 }
513
514 =back
515
516 =head1 BUGS
517
518 Sure.
519
520 =head1 SEE ALSO
521
522 =cut
523
524 1;
525