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