2 use base qw( Exporter );
5 use vars qw( @EXPORT_OK );
8 use Crypt::OpenSSL::RSA;
9 use FS::UID qw( dbh driver_name );
13 $FS::svc_domain::whois_hack = 1;
14 $FS::svc_domain::whois_hack = 1;
18 use Locale::SubCountry 1.42;
19 use FS::cust_main_county;
24 use FS::access_groupagent;
27 use FS::Record qw(qsearch);
30 @EXPORT_OK = qw( create_initial_data enable_encryption enable_banned_pay_pad );
34 FS::Setup - Database setup
42 Currently this module simply provides a place to store common subroutines for
53 sub create_initial_data {
56 my $oldAutoCommit = $FS::UID::AutoCommit;
57 local $FS::UID::AutoCommit = 0;
58 $FS::UID::AutoCommit = 0;
65 populate_initial_data(%opt);
75 enable_banned_pay_pad();
77 if ( $oldAutoCommit ) {
78 dbh->commit or die dbh->errstr;
83 sub enable_encryption {
88 my $conf = new FS::Conf;
90 die "encryption key(s) already in place"
91 if $conf->exists('encryptionpublickey')
92 || $conf->exists('encryptionprivatekey');
95 my $rsa = Crypt::OpenSSL::RSA->generate_key($length);
97 $conf->set('encryption', 1);
98 $conf->set('encryptionmodule', 'Crypt::OpenSSL::RSA');
99 $conf->set('encryptionpublickey', $rsa->get_public_key_string );
100 $conf->set('encryptionprivatekey', $rsa->get_private_key_string );
104 sub enable_banned_pay_pad {
109 my $conf = new FS::Conf;
111 die "banned_pay-pad already in place"
112 if length( $conf->config('banned_pay-pad') );
114 #arbitrary but good enough... all we need is *some* per-site random padding
115 my @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '.', ',' );
117 $conf->set('banned_pay-pad',
118 join('', map($pw_set[ int(rand($#pw_set)) ], (0..15) ) )
123 sub populate_numbering {
124 eval "use FS::lata_Data;"; # this automatically populates the lata table, if unpopulated
125 eval "use FS::msa_Data;"; # this automatically populates the msa table, if unpopulated
128 sub populate_locales {
131 foreach my $country ( sort map uc($_), all_country_codes ) {
132 _add_country($country);
137 sub populate_addl_locales {
141 'FM' => 'Federated States of Micronesia',
142 'MH' => 'Marshall Islands',
144 'AA' => "Armed Forces Americas (except Canada)",
145 'AE' => "Armed Forces Europe / Canada / Middle East / Africa",
146 'AP' => "Armed Forces Pacific",
150 foreach my $country ( keys %addl ) {
151 foreach my $state ( keys %{ $addl{$country} } ) {
152 # $longname = $addl{$country}{$state};
153 _add_locale( 'country'=>$country, 'state'=>$state);
161 my( $country ) = shift;
163 my $subcountry = eval { new Locale::SubCountry($country) };
164 my @states = $subcountry ? $subcountry->all_codes : undef;
166 if ( !scalar(@states) || ( scalar(@states)==1 && !defined($states[0]) ) ) {
168 _add_locale( 'country'=>$country );
172 if ( $states[0] =~ /^(\d+|\w)$/ ) {
173 @states = map $subcountry->full_name($_), @states
176 foreach my $state ( @states ) {
177 _add_locale( 'country'=>$country, 'state'=>$state);
185 my $cust_main_county = new FS::cust_main_county( { 'tax'=>0, @_ });
186 my $error = $cust_main_county->insert;
187 die $error if $error;
190 sub populate_duplock {
192 return unless driver_name =~ /^mysql/i;
194 my $sth = dbh->prepare(
195 "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
196 ) or die dbh->errstr;
198 $sth->execute or die $sth->errstr;
202 sub populate_initial_data {
205 my $data = initial_data(%opt);
207 foreach my $table ( keys %$data ) {
209 #warn "popuilating $table\n";
211 my $class = "FS::$table";
215 $class->_populate_initial_data(%opt)
216 if $class->can('_populate_initial_data');
218 my @records = @{ $data->{$table} };
220 foreach my $record ( @records ) {
222 my $args = delete($record->{'_insert_args'}) || [];
223 my $object = $class->new( $record );
224 my $error = $object->insert( @$args );
225 die "error inserting record into $table: $error\n"
228 #my $pkey = $object->primary_key;
229 #my $pkeyvalue = $object->$pkey();
230 #warn " inserted $pkeyvalue\n";
241 my $cust_location = FS::cust_location->new({
242 'address1' => '1234 System Lane',
243 'city' => 'Systemtown',
249 #tie my %hash, 'Tie::DxHash',
250 tie my %hash, 'Tie::IxHash',
254 { 'username' => 'fs_bootstrap',
255 '_password' => 'changeme', #will trigger warning if you try to enable
257 'first' => 'Bootstrap',
264 { 'groupname' => 'Superuser' },
270 #XXX need default new-style billing events
272 # 'part_bill_event' => [
273 # { 'payby' => 'CARD',
274 # 'event' => 'Batch card',
276 # 'eventcode' => '$cust_bill->batch_card(%options);',
278 # 'plan' => 'batch-card',
280 # { 'payby' => 'BILL',
281 # 'event' => 'Send invoice',
283 # 'eventcode' => '$cust_bill->send();',
287 # { 'payby' => 'DCRD',
288 # 'event' => 'Send invoice',
290 # 'eventcode' => '$cust_bill->send();',
294 # { 'payby' => 'DCHK',
295 # 'event' => 'Send invoice',
297 # 'eventcode' => '$cust_bill->send();',
301 # { 'payby' => 'DCLN',
302 # 'event' => 'Suspend',
304 # 'eventcode' => '$cust_bill->suspend();',
306 # 'plan' => 'suspend',
308 # #{ 'payby' => 'DCLN',
309 # # 'event' => 'Retriable',
311 # # 'eventcode' => '$cust_bill_event->retriable();',
313 # # 'plan' => 'retriable',
317 #you must create a service definition. An example of a service definition
318 #would be a dial-up account or a domain. First, it is necessary to create a
319 #domain definition. Click on View/Edit service definitions and Add a new
320 #service definition with Table svc_domain (and no modifiers).
323 'svcdb' => 'svc_domain',
327 #Now that you have created your first service, you must create a package
328 #including this service which you can sell to customers. Zero, one, or many
329 #services are bundled into a package. Click on View/Edit package
330 #definitions and Add a new package definition which includes quantity 1 of
331 #the svc_domain service you created above.
333 { 'pkg' => 'System Domain',
334 'comment' => '(NOT FOR CUSTOMERS)',
338 'pkg_svc' => { 1 => 1 }, # XXX
339 'primary_svc' => 1, #XXX
348 #After you create your first package, then you must define who is able to
349 #sell that package by creating an agent type. An example of an agent type
350 #would be an internal sales representitive which sells regular and
351 #promotional packages, as opposed to an external sales representitive
352 #which would only sell regular packages of services. Click on View/Edit
353 #agent types and Add a new agent type.
355 { 'atype' => 'Internal' },
358 #Allow this agent type to sell the package you created above.
360 { 'typenum' => 1, #XXX
365 #After creating a new agent type, you must create an agent. Click on
366 #View/Edit agents and Add a new agent.
368 { 'agent' => 'Internal',
369 'typenum' => 1, # XXX
373 #Set up at least one Advertising source. Advertising sources will help you
374 #keep track of how effective your advertising is, tracking where customers
375 #heard of your service offerings. You must create at least one advertising
376 #source. If you do not wish to use the referral functionality, simply
377 #create a single advertising source only. Click on View/Edit advertising
378 #sources and Add a new advertising source.
380 { 'referral' => 'Internal', },
383 #Click on New Customer and create a new customer for your system accounts
384 #with billing type Complimentary. Leave the First package dropdown set to
387 { 'agentnum' => 1, #XXX
390 'last' => 'Accounts',
391 'complimentary' => 'Y',
392 'bill_location' => $cust_location,
393 'ship_location' => $cust_location,
397 #From the Customer View screen of the newly created customer, order the
398 #package you defined above.
400 { 'custnum' => 1, #XXX
405 #From the Package View screen of the newly created package, choose
406 #(Provision) to add the customer's service for this new package.
407 #Add your own domain.
409 { 'domain' => $opt{'domain'},
412 'action' => 'N', #pseudo-field
416 #Go back to View/Edit service definitions on the main menu, and Add a new
417 #service definition with Table svc_acct. Select your domain in the domsvc
418 #Modifier. Set Fixed to define a service locked-in to this domain, or
419 #Default to define a service which may select from among this domain and
420 #the customer's domains.
431 'msg_template' => [],
439 sub populate_access {
441 foreach my $rightname ( FS::AccessRight->default_superuser_rights ) {
442 my $access_right = new FS::access_right {
443 'righttype' => 'FS::access_group',
444 'rightobjnum' => 1, #$supergroup->groupnum,
445 'rightname' => $rightname,
447 my $ar_error = $access_right->insert;
448 die $ar_error if $ar_error;
451 #foreach my $agent ( qsearch('agent', {} ) ) {
452 my $access_groupagent = new FS::access_groupagent {
453 'groupnum' => 1, #$supergroup->groupnum,
454 'agentnum' => 1, #$agent->agentnum,
456 my $aga_error = $access_groupagent->insert;
457 die $aga_error if $aga_error;
462 sub populate_msgcat {
464 foreach my $del_msgcat ( qsearch('msgcat', {}) ) {
465 my $error = $del_msgcat->delete;
466 die $error if $error;
469 my %messages = FS::msgcat::_legacy_messages();
471 foreach my $msgcode ( keys %messages ) {
472 foreach my $locale ( keys %{$messages{$msgcode}} ) {
473 my $msgcat = new FS::msgcat( {
474 'msgcode' => $msgcode,
476 'msg' => $messages{$msgcode}{$locale},
478 my $error = $msgcat->insert;
479 die $error if $error;