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