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