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