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 );
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 if ( $oldAutoCommit ) {
76 dbh->commit or die dbh->errstr;
81 sub enable_encryption {
86 my $conf = new FS::Conf;
88 die "encryption key(s) already in place"
89 if $conf->exists('encryptionpublickey')
90 || $conf->exists('encryptionprivatekey');
93 my $rsa = Crypt::OpenSSL::RSA->generate_key($length);
95 $conf->set('encryption', 1);
96 $conf->set('encryptionmodule', 'Crypt::OpenSSL::RSA');
97 $conf->set('encryptionpublickey', $rsa->get_public_key_string );
98 $conf->set('encryptionprivatekey', $rsa->get_private_key_string );
102 sub populate_numbering {
103 eval "use FS::lata_Data;"; # this automatically populates the lata table, if unpopulated
104 eval "use FS::msa_Data;"; # this automatically populates the msa table, if unpopulated
107 sub populate_locales {
110 foreach my $country ( sort map uc($_), all_country_codes ) {
111 _add_country($country);
116 sub populate_addl_locales {
120 'FM' => 'Federated States of Micronesia',
121 'MH' => 'Marshall Islands',
123 'AA' => "Armed Forces Americas (except Canada)",
124 'AE' => "Armed Forces Europe / Canada / Middle East / Africa",
125 'AP' => "Armed Forces Pacific",
129 foreach my $country ( keys %addl ) {
130 foreach my $state ( keys %{ $addl{$country} } ) {
131 # $longname = $addl{$country}{$state};
132 _add_locale( 'country'=>$country, 'state'=>$state);
140 my( $country ) = shift;
142 my $subcountry = eval { new Locale::SubCountry($country) };
143 my @states = $subcountry ? $subcountry->all_codes : undef;
145 if ( !scalar(@states) || ( scalar(@states)==1 && !defined($states[0]) ) ) {
147 _add_locale( 'country'=>$country );
151 if ( $states[0] =~ /^(\d+|\w)$/ ) {
152 @states = map $subcountry->full_name($_), @states
155 foreach my $state ( @states ) {
156 _add_locale( 'country'=>$country, 'state'=>$state);
164 my $cust_main_county = new FS::cust_main_county( { 'tax'=>0, @_ });
165 my $error = $cust_main_county->insert;
166 die $error if $error;
169 sub populate_duplock {
171 return unless driver_name =~ /^mysql/i;
173 my $sth = dbh->prepare(
174 "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
175 ) or die dbh->errstr;
177 $sth->execute or die $sth->errstr;
181 sub populate_initial_data {
184 my $data = initial_data(%opt);
186 foreach my $table ( keys %$data ) {
188 #warn "popuilating $table\n";
190 my $class = "FS::$table";
194 $class->_populate_initial_data(%opt)
195 if $class->can('_populate_initial_data');
197 my @records = @{ $data->{$table} };
199 foreach my $record ( @records ) {
201 my $args = delete($record->{'_insert_args'}) || [];
202 my $object = $class->new( $record );
203 my $error = $object->insert( @$args );
204 die "error inserting record into $table: $error\n"
207 #my $pkey = $object->primary_key;
208 #my $pkeyvalue = $object->$pkey();
209 #warn " inserted $pkeyvalue\n";
220 my $cust_location = FS::cust_location->new({
221 'address1' => '1234 System Lane',
222 'city' => 'Systemtown',
228 #tie my %hash, 'Tie::DxHash',
229 tie my %hash, 'Tie::IxHash',
233 { 'username' => 'fs_bootstrap',
234 '_password' => 'changeme', #will trigger warning if you try to enable
236 'first' => 'Bootstrap',
243 { 'groupname' => 'Superuser' },
249 #XXX need default new-style billing events
251 # 'part_bill_event' => [
252 # { 'payby' => 'CARD',
253 # 'event' => 'Batch card',
255 # 'eventcode' => '$cust_bill->batch_card(%options);',
257 # 'plan' => 'batch-card',
259 # { 'payby' => 'BILL',
260 # 'event' => 'Send invoice',
262 # 'eventcode' => '$cust_bill->send();',
266 # { 'payby' => 'DCRD',
267 # 'event' => 'Send invoice',
269 # 'eventcode' => '$cust_bill->send();',
273 # { 'payby' => 'DCHK',
274 # 'event' => 'Send invoice',
276 # 'eventcode' => '$cust_bill->send();',
280 # { 'payby' => 'DCLN',
281 # 'event' => 'Suspend',
283 # 'eventcode' => '$cust_bill->suspend();',
285 # 'plan' => 'suspend',
287 # #{ 'payby' => 'DCLN',
288 # # 'event' => 'Retriable',
290 # # 'eventcode' => '$cust_bill_event->retriable();',
292 # # 'plan' => 'retriable',
296 #you must create a service definition. An example of a service definition
297 #would be a dial-up account or a domain. First, it is necessary to create a
298 #domain definition. Click on View/Edit service definitions and Add a new
299 #service definition with Table svc_domain (and no modifiers).
302 'svcdb' => 'svc_domain',
306 #Now that you have created your first service, you must create a package
307 #including this service which you can sell to customers. Zero, one, or many
308 #services are bundled into a package. Click on View/Edit package
309 #definitions and Add a new package definition which includes quantity 1 of
310 #the svc_domain service you created above.
312 { 'pkg' => 'System Domain',
313 'comment' => '(NOT FOR CUSTOMERS)',
317 'pkg_svc' => { 1 => 1 }, # XXX
318 'primary_svc' => 1, #XXX
327 #After you create your first package, then you must define who is able to
328 #sell that package by creating an agent type. An example of an agent type
329 #would be an internal sales representitive which sells regular and
330 #promotional packages, as opposed to an external sales representitive
331 #which would only sell regular packages of services. Click on View/Edit
332 #agent types and Add a new agent type.
334 { 'atype' => 'Internal' },
337 #Allow this agent type to sell the package you created above.
339 { 'typenum' => 1, #XXX
344 #After creating a new agent type, you must create an agent. Click on
345 #View/Edit agents and Add a new agent.
347 { 'agent' => 'Internal',
348 'typenum' => 1, # XXX
352 #Set up at least one Advertising source. Advertising sources will help you
353 #keep track of how effective your advertising is, tracking where customers
354 #heard of your service offerings. You must create at least one advertising
355 #source. If you do not wish to use the referral functionality, simply
356 #create a single advertising source only. Click on View/Edit advertising
357 #sources and Add a new advertising source.
359 { 'referral' => 'Internal', },
362 #Click on New Customer and create a new customer for your system accounts
363 #with billing type Complimentary. Leave the First package dropdown set to
366 { 'agentnum' => 1, #XXX
369 'last' => 'Accounts',
371 'payinfo' => 'system', #or something
372 'paydate' => '1/2037',
373 'bill_location' => $cust_location,
374 'ship_location' => $cust_location,
378 #From the Customer View screen of the newly created customer, order the
379 #package you defined above.
381 { 'custnum' => 1, #XXX
386 #From the Package View screen of the newly created package, choose
387 #(Provision) to add the customer's service for this new package.
388 #Add your own domain.
390 { 'domain' => $opt{'domain'},
393 'action' => 'N', #pseudo-field
397 #Go back to View/Edit service definitions on the main menu, and Add a new
398 #service definition with Table svc_acct. Select your domain in the domsvc
399 #Modifier. Set Fixed to define a service locked-in to this domain, or
400 #Default to define a service which may select from among this domain and
401 #the customer's domains.
412 'msg_template' => [],
420 sub populate_access {
422 foreach my $rightname ( FS::AccessRight->default_superuser_rights ) {
423 my $access_right = new FS::access_right {
424 'righttype' => 'FS::access_group',
425 'rightobjnum' => 1, #$supergroup->groupnum,
426 'rightname' => $rightname,
428 my $ar_error = $access_right->insert;
429 die $ar_error if $ar_error;
432 #foreach my $agent ( qsearch('agent', {} ) ) {
433 my $access_groupagent = new FS::access_groupagent {
434 'groupnum' => 1, #$supergroup->groupnum,
435 'agentnum' => 1, #$agent->agentnum,
437 my $aga_error = $access_groupagent->insert;
438 die $aga_error if $aga_error;
443 sub populate_msgcat {
445 foreach my $del_msgcat ( qsearch('msgcat', {}) ) {
446 my $error = $del_msgcat->delete;
447 die $error if $error;
450 my %messages = FS::msgcat::_legacy_messages();
452 foreach my $msgcode ( keys %messages ) {
453 foreach my $locale ( keys %{$messages{$msgcode}} ) {
454 my $msgcat = new FS::msgcat( {
455 'msgcode' => $msgcode,
457 'msg' => $messages{$msgcode}{$locale},
459 my $error = $msgcat->insert;
460 die $error if $error;