4 use vars qw( @ISA @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;
16 @ISA = qw( Exporter );
17 @EXPORT_OK = qw( create_initial_data enable_encryption );
21 FS::Setup - Database setup
29 Currently this module simply provides a place to store common subroutines for
40 sub create_initial_data {
43 my $oldAutoCommit = $FS::UID::AutoCommit;
44 local $FS::UID::AutoCommit = 0;
45 $FS::UID::AutoCommit = 0;
52 populate_initial_data(%opt);
62 if ( $oldAutoCommit ) {
63 dbh->commit or die dbh->errstr;
68 sub enable_encryption {
73 my $conf = new FS::Conf;
75 die "encryption key(s) already in place"
76 if $conf->exists('encryptionpublickey')
77 || $conf->exists('encryptionprivatekey');
80 my $rsa = Crypt::OpenSSL::RSA->generate_key($length);
82 $conf->set('encryption', 1);
83 $conf->set('encryptionmodule', 'Crypt::OpenSSL::RSA');
84 $conf->set('encryptionpublickey', $rsa->get_public_key_string );
85 $conf->set('encryptionprivatekey', $rsa->get_private_key_string );
89 sub populate_numbering {
90 eval "use FS::lata_Data;"; # this automatically populates the lata table, if unpopulated
91 eval "use FS::msa_Data;"; # this automatically populates the msa table, if unpopulated
94 sub populate_locales {
97 use FS::cust_main_county;
100 foreach my $country ( sort map uc($_), all_country_codes ) {
101 _add_country($country);
106 sub populate_addl_locales {
110 'FM' => 'Federated States of Micronesia',
111 'MH' => 'Marshall Islands',
113 'AA' => "Armed Forces Americas (except Canada)",
114 'AE' => "Armed Forces Europe / Canada / Middle East / Africa",
115 'AP' => "Armed Forces Pacific",
119 foreach my $country ( keys %addl ) {
120 foreach my $state ( keys %{ $addl{$country} } ) {
121 # $longname = $addl{$country}{$state};
122 _add_locale( 'country'=>$country, 'state'=>$state);
130 use Locale::SubCountry 1.42;
132 my( $country ) = shift;
134 my $subcountry = eval { new Locale::SubCountry($country) };
135 my @states = $subcountry ? $subcountry->all_codes : undef;
137 if ( !scalar(@states) || ( scalar(@states)==1 && !defined($states[0]) ) ) {
139 _add_locale( 'country'=>$country );
143 if ( $states[0] =~ /^(\d+|\w)$/ ) {
144 @states = map $subcountry->full_name($_), @states
147 foreach my $state ( @states ) {
148 _add_locale( 'country'=>$country, 'state'=>$state);
156 my $cust_main_county = new FS::cust_main_county( { 'tax'=>0, @_ });
157 my $error = $cust_main_county->insert;
158 die $error if $error;
161 sub populate_duplock {
163 return unless driver_name =~ /^mysql/i;
165 my $sth = dbh->prepare(
166 "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
167 ) or die dbh->errstr;
169 $sth->execute or die $sth->errstr;
173 sub populate_initial_data {
176 my $data = initial_data(%opt);
178 foreach my $table ( keys %$data ) {
180 #warn "popuilating $table\n";
182 my $class = "FS::$table";
186 $class->_populate_initial_data(%opt)
187 if $class->can('_populate_initial_data');
189 my @records = @{ $data->{$table} };
191 foreach my $record ( @records ) {
193 my $args = delete($record->{'_insert_args'}) || [];
194 my $object = $class->new( $record );
195 my $error = $object->insert( @$args );
196 die "error inserting record into $table: $error\n"
199 #my $pkey = $object->primary_key;
200 #my $pkeyvalue = $object->$pkey();
201 #warn " inserted $pkeyvalue\n";
212 my $cust_location = FS::cust_location->new({
213 'address1' => '1234 System Lane',
214 'city' => 'Systemtown',
220 #tie my %hash, 'Tie::DxHash',
221 tie my %hash, 'Tie::IxHash',
225 { 'username' => 'fs_bootstrap',
226 '_password' => 'changeme', #will trigger warning if you try to enable
228 'first' => 'Bootstrap',
235 { 'groupname' => 'Superuser' },
241 #XXX need default new-style billing events
243 # 'part_bill_event' => [
244 # { 'payby' => 'CARD',
245 # 'event' => 'Batch card',
247 # 'eventcode' => '$cust_bill->batch_card(%options);',
249 # 'plan' => 'batch-card',
251 # { 'payby' => 'BILL',
252 # 'event' => 'Send invoice',
254 # 'eventcode' => '$cust_bill->send();',
258 # { 'payby' => 'DCRD',
259 # 'event' => 'Send invoice',
261 # 'eventcode' => '$cust_bill->send();',
265 # { 'payby' => 'DCHK',
266 # 'event' => 'Send invoice',
268 # 'eventcode' => '$cust_bill->send();',
272 # { 'payby' => 'DCLN',
273 # 'event' => 'Suspend',
275 # 'eventcode' => '$cust_bill->suspend();',
277 # 'plan' => 'suspend',
279 # #{ 'payby' => 'DCLN',
280 # # 'event' => 'Retriable',
282 # # 'eventcode' => '$cust_bill_event->retriable();',
284 # # 'plan' => 'retriable',
288 #you must create a service definition. An example of a service definition
289 #would be a dial-up account or a domain. First, it is necessary to create a
290 #domain definition. Click on View/Edit service definitions and Add a new
291 #service definition with Table svc_domain (and no modifiers).
294 'svcdb' => 'svc_domain',
298 #Now that you have created your first service, you must create a package
299 #including this service which you can sell to customers. Zero, one, or many
300 #services are bundled into a package. Click on View/Edit package
301 #definitions and Add a new package definition which includes quantity 1 of
302 #the svc_domain service you created above.
304 { 'pkg' => 'System Domain',
305 'comment' => '(NOT FOR CUSTOMERS)',
309 'pkg_svc' => { 1 => 1 }, # XXX
310 'primary_svc' => 1, #XXX
319 #After you create your first package, then you must define who is able to
320 #sell that package by creating an agent type. An example of an agent type
321 #would be an internal sales representitive which sells regular and
322 #promotional packages, as opposed to an external sales representitive
323 #which would only sell regular packages of services. Click on View/Edit
324 #agent types and Add a new agent type.
326 { 'atype' => 'Internal' },
329 #Allow this agent type to sell the package you created above.
331 { 'typenum' => 1, #XXX
336 #After creating a new agent type, you must create an agent. Click on
337 #View/Edit agents and Add a new agent.
339 { 'agent' => 'Internal',
340 'typenum' => 1, # XXX
344 #Set up at least one Advertising source. Advertising sources will help you
345 #keep track of how effective your advertising is, tracking where customers
346 #heard of your service offerings. You must create at least one advertising
347 #source. If you do not wish to use the referral functionality, simply
348 #create a single advertising source only. Click on View/Edit advertising
349 #sources and Add a new advertising source.
351 { 'referral' => 'Internal', },
354 #Click on New Customer and create a new customer for your system accounts
355 #with billing type Complimentary. Leave the First package dropdown set to
358 { 'agentnum' => 1, #XXX
361 'last' => 'Accounts',
363 'payinfo' => 'system', #or something
364 'paydate' => '1/2037',
365 'bill_location' => $cust_location,
366 'ship_location' => $cust_location,
370 #From the Customer View screen of the newly created customer, order the
371 #package you defined above.
373 { 'custnum' => 1, #XXX
378 #From the Package View screen of the newly created package, choose
379 #(Provision) to add the customer's service for this new package.
380 #Add your own domain.
382 { 'domain' => $opt{'domain'},
385 'action' => 'N', #pseudo-field
389 #Go back to View/Edit service definitions on the main menu, and Add a new
390 #service definition with Table svc_acct. Select your domain in the domsvc
391 #Modifier. Set Fixed to define a service locked-in to this domain, or
392 #Default to define a service which may select from among this domain and
393 #the customer's domains.
404 'msg_template' => [],
412 sub populate_access {
415 use FS::access_right;
416 use FS::access_groupagent;
418 foreach my $rightname ( FS::AccessRight->default_superuser_rights ) {
419 my $access_right = new FS::access_right {
420 'righttype' => 'FS::access_group',
421 'rightobjnum' => 1, #$supergroup->groupnum,
422 'rightname' => $rightname,
424 my $ar_error = $access_right->insert;
425 die $ar_error if $ar_error;
428 #foreach my $agent ( qsearch('agent', {} ) ) {
429 my $access_groupagent = new FS::access_groupagent {
430 'groupnum' => 1, #$supergroup->groupnum,
431 'agentnum' => 1, #$agent->agentnum,
433 my $aga_error = $access_groupagent->insert;
434 die $aga_error if $aga_error;
439 sub populate_msgcat {
441 use FS::Record qw(qsearch);
444 foreach my $del_msgcat ( qsearch('msgcat', {}) ) {
445 my $error = $del_msgcat->delete;
446 die $error if $error;
449 my %messages = FS::msgcat::_legacy_messages();
451 foreach my $msgcode ( keys %messages ) {
452 foreach my $locale ( keys %{$messages{$msgcode}} ) {
453 my $msgcat = new FS::msgcat( {
454 'msgcode' => $msgcode,
456 'msg' => $messages{$msgcode}{$locale},
458 my $error = $msgcat->insert;
459 die $error if $error;