bootstrapping issues, ugh
[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     warn "popuilating $table\n";
150
151     my $class = "FS::$table";
152     eval "use $class;";
153     die $@ if $@;
154
155     $class->_populate_initial_data(%opt)
156       if $class->can('_populate_inital_data');
157
158     my @records = @{ $data->{$table} };
159
160     foreach my $record ( @records ) {
161       my $args = delete($record->{'_insert_args'}) || [];
162       my $object = $class->new( $record );
163       my $error = $object->insert( @$args );
164       die "error inserting record into $table: $error\n"
165         if $error;
166     }
167
168   }
169
170 }
171
172 sub initial_data {
173   my %opt = @_;
174
175   #tie my %hash, 'Tie::DxHash', 
176   tie my %hash, 'Tie::IxHash', 
177
178     #superuser group
179     'access_group' => [
180       { 'groupname' => 'Superuser' },
181     ],
182
183     #reason types
184     'reason_type' => [],
185
186 #XXX need default new-style billing events
187 #    #billing events
188 #    'part_bill_event' => [
189 #      { 'payby'     => 'CARD',
190 #        'event'     => 'Batch card',
191 #        'seconds'   => 0,
192 #        'eventcode' => '$cust_bill->batch_card(%options);',
193 #        'weight'    => 40,
194 #        'plan'      => 'batch-card',
195 #      },
196 #      { 'payby'     => 'BILL',
197 #        'event'     => 'Send invoice',
198 #        'seconds'   => 0,
199 #        'eventcode' => '$cust_bill->send();',
200 #        'weight'    => 50,
201 #        'plan'      => 'send',
202 #      },
203 #      { 'payby'     => 'DCRD',
204 #        'event'     => 'Send invoice',
205 #        'seconds'   => 0,
206 #        'eventcode' => '$cust_bill->send();',
207 #        'weight'    => 50,
208 #        'plan'      => 'send',
209 #      },
210 #      { 'payby'     => 'DCHK',
211 #        'event'     => 'Send invoice',
212 #        'seconds'   => 0,
213 #        'eventcode' => '$cust_bill->send();',
214 #        'weight'    => 50,
215 #        'plan'      => 'send',
216 #      },
217 #      { 'payby'     => 'DCLN',
218 #        'event'     => 'Suspend',
219 #        'seconds'   => 0,
220 #        'eventcode' => '$cust_bill->suspend();',
221 #        'weight'    => 40,
222 #        'plan'      => 'suspend',
223 #      },
224 #      #{ 'payby'     => 'DCLN',
225 #      #  'event'     => 'Retriable',
226 #      #  'seconds'   => 0,
227 #      #  'eventcode' => '$cust_bill_event->retriable();',
228 #      #  'weight'    => 60,
229 #      #  'plan'      => 'retriable',
230 #      #},
231 #    ],
232     
233     #you must create a service definition. An example of a service definition
234     #would be a dial-up account or a domain. First, it is necessary to create a
235     #domain definition. Click on View/Edit service definitions and Add a new
236     #service definition with Table svc_domain (and no modifiers).
237     'part_svc' => [
238       { 'svc'   => 'Domain',
239         'svcdb' => 'svc_domain',
240       }
241     ],
242
243     #Now that you have created your first service, you must create a package
244     #including this service which you can sell to customers. Zero, one, or many
245     #services are bundled into a package. Click on View/Edit package
246     #definitions and Add a new package definition which includes quantity 1 of
247     #the svc_domain service you created above.
248     'part_pkg' => [
249       { 'pkg'     => 'System Domain',
250         'comment' => '(NOT FOR CUSTOMERS)',
251         'freq'    => '0',
252         'plan'    => 'flat',
253         '_insert_args' => [
254           'pkg_svc'     => { 1 => 1 }, # XXX
255           'primary_svc' => 1, #XXX
256           'options'     => {
257             'setup_fee' => '0',
258             'recur_fee' => '0',
259           },
260         ],
261       },
262     ],
263
264     #After you create your first package, then you must define who is able to
265     #sell that package by creating an agent type. An example of an agent type
266     #would be an internal sales representitive which sells regular and
267     #promotional packages, as opposed to an external sales representitive
268     #which would only sell regular packages of services. Click on View/Edit
269     #agent types and Add a new agent type.
270     'agent_type' => [
271       { 'atype' => 'Internal' },
272     ],
273
274     #Allow this agent type to sell the package you created above.
275     'type_pkgs' => [
276       { 'typenum' => 1, #XXX
277         'pkgpart' => 1, #XXX
278       },
279     ],
280
281     #After creating a new agent type, you must create an agent. Click on
282     #View/Edit agents and Add a new agent.
283     'agent' => [
284       { 'agent'   => 'Internal',
285         'typenum' => 1, # XXX
286       },
287     ],
288
289     #Set up at least one Advertising source. Advertising sources will help you
290     #keep track of how effective your advertising is, tracking where customers
291     #heard of your service offerings. You must create at least one advertising
292     #source. If you do not wish to use the referral functionality, simply
293     #create a single advertising source only. Click on View/Edit advertising
294     #sources and Add a new advertising source.
295     'part_referral' => [
296       { 'referral' => 'Internal', },
297     ],
298     
299     #Click on New Customer and create a new customer for your system accounts
300     #with billing type Complimentary. Leave the First package dropdown set to
301     #(none).
302     'cust_main' => [
303       { 'agentnum'  => 1, #XXX
304         'refnum'    => 1, #XXX
305         'first'     => 'System',
306         'last'      => 'Accounts',
307         'address1'  => '1234 System Lane',
308         'city'      => 'Systemtown',
309         'state'     => 'CA',
310         'zip'       => '54321',
311         'country'   => 'US',
312         'payby'     => 'COMP',
313         'payinfo'   => 'system', #or something
314         'paydate'   => '1/2037',
315       },
316     ],
317
318     #From the Customer View screen of the newly created customer, order the
319     #package you defined above.
320     'cust_pkg' => [
321       { 'custnum' => 1, #XXX
322         'pkgpart' => 1, #XXX
323       },
324     ],
325
326     #From the Package View screen of the newly created package, choose
327     #(Provision) to add the customer's service for this new package.
328     #Add your own domain.
329     'svc_domain' => [
330       { 'domain'  => $opt{'domain'},
331         'pkgnum'  => 1, #XXX
332         'svcpart' => 1, #XXX
333         'action'  => 'N', #pseudo-field
334       },
335     ],
336
337     #Go back to View/Edit service definitions on the main menu, and Add a new
338     #service definition with Table svc_acct. Select your domain in the domsvc
339     #Modifier. Set Fixed to define a service locked-in to this domain, or
340     #Default to define a service which may select from among this domain and
341     #the customer's domains.
342
343     #not yet....
344
345   #)
346
347     #usage classes
348     'usage_class' => [],
349
350   ;
351
352   \%hash;
353
354 }
355
356 sub populate_access {
357
358   use FS::AccessRight;
359   use FS::access_right;
360
361   foreach my $rightname ( FS::AccessRight->rights ) {
362     my $access_right = new FS::access_right {
363       'righttype'   => 'FS::access_group',
364       'rightobjnum' => 1, #$supergroup->groupnum,
365       'rightname'   => $rightname,
366     };
367     my $ar_error = $access_right->insert;
368     die $ar_error if $ar_error;
369   }
370
371   #foreach my $agent ( qsearch('agent', {} ) ) {
372     my $access_groupagent = new FS::access_groupagent {
373       'groupnum' => 1, #$supergroup->groupnum,
374       'agentnum' => 1, #$agent->agentnum,
375     };
376     my $aga_error = $access_groupagent->insert;
377     die $aga_error if $aga_error;
378   #}
379
380 }
381
382 sub populate_msgcat {
383
384   use FS::Record qw(qsearch);
385   use FS::msgcat;
386
387   foreach my $del_msgcat ( qsearch('msgcat', {}) ) {
388     my $error = $del_msgcat->delete;
389     die $error if $error;
390   }
391
392   my %messages = msgcat_messages();
393
394   foreach my $msgcode ( keys %messages ) {
395     foreach my $locale ( keys %{$messages{$msgcode}} ) {
396       my $msgcat = new FS::msgcat( {
397         'msgcode' => $msgcode,
398         'locale'  => $locale,
399         'msg'     => $messages{$msgcode}{$locale},
400       });
401       my $error = $msgcat->insert;
402       die $error if $error;
403     }
404   }
405
406 }
407
408 sub msgcat_messages {
409
410   #  'msgcode' => {
411   #    'en_US' => 'Message',
412   #  },
413
414   (
415
416     'passwords_dont_match' => {
417       'en_US' => "Passwords don't match",
418     },
419
420     'invalid_card' => {
421       'en_US' => 'Invalid credit card number',
422     },
423
424     'unknown_card_type' => {
425       'en_US' => 'Unknown card type',
426     },
427
428     'not_a' => {
429       'en_US' => 'Not a ',
430     },
431
432     'empty_password' => {
433       'en_US' => 'Empty password',
434     },
435
436     'no_access_number_selected' => {
437       'en_US' => 'No access number selected',
438     },
439
440     'illegal_text' => {
441       'en_US' => 'Illegal (text)',
442       #'en_US' => 'Only letters, numbers, spaces, and the following punctuation symbols are permitted: ! @ # $ % & ( ) - + ; : \' " , . ? / in field',
443     },
444
445     'illegal_or_empty_text' => {
446       'en_US' => 'Illegal or empty (text)',
447       #'en_US' => 'Only letters, numbers, spaces, and the following punctuation symbols are permitted: ! @ # $ % & ( ) - + ; : \' " , . ? / in required field',
448     },
449
450     'illegal_username' => {
451       'en_US' => 'Illegal username',
452     },
453
454     'illegal_password' => {
455       'en_US' => 'Illegal password (',
456     },
457
458     'illegal_password_characters' => {
459       'en_US' => ' characters)',
460     },
461
462     'username_in_use' => {
463       'en_US' => 'Username in use',
464     },
465
466     'phonenum_in_use' => {
467       'en_US' => 'Phone number in use',
468     },
469
470     'illegal_email_invoice_address' => {
471       'en_US' => 'Illegal email invoice address',
472     },
473
474     'illegal_name' => {
475       'en_US' => 'Illegal (name)',
476       #'en_US' => 'Only letters, numbers, spaces and the following punctuation symbols are permitted: , . - \' in field',
477     },
478
479     'illegal_phone' => {
480       'en_US' => 'Illegal (phone)',
481       #'en_US' => '',
482     },
483
484     'illegal_zip' => {
485       'en_US' => 'Illegal (zip)',
486       #'en_US' => '',
487     },
488
489     'expired_card' => {
490       'en_US' => 'Expired card',
491     },
492
493     'daytime' => {
494       'en_US' => 'Day Phone',
495     },
496
497     'night' => {
498       'en_US' => 'Night Phone',
499     },
500
501     'svc_external-id' => {
502       'en_US' => 'External ID',
503     },
504
505     'svc_external-title' => {
506       'en_US' => 'Title',
507     },
508
509     'stateid' => {
510       'en_US' => 'Driver\'s License',
511     },
512
513     'stateid_state' => {
514       'en_US' => 'Driver\'s License State',
515     },
516
517     'invalid_domain' => {
518       'en_US' => 'Invalid domain',
519     },
520
521   );
522 }
523
524 =back
525
526 =head1 BUGS
527
528 Sure.
529
530 =head1 SEE ALSO
531
532 =cut
533
534 1;
535