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