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