3 #script to move customers from one installation to another
4 # source is remote, destination is local
9 use FS::UID qw( adminsuidsetup dbh );
10 use FS::Schema qw( dbdef );
11 use FS::Record qw( qsearchs );
16 use FS::cust_bill_ApplicationCommon;
25 my $source_datasrc = 'dbi:Pg:dbname=benson';
27 my $source_user = 'freeside';
30 my $dest_agentnum = 3;
39 'CARD' => [ 13, 14, 15 ],
52 or die "Usage:\n (edit variables at top of script and then)\n".
53 " b-move-customers user\n";
56 $FS::cust_main::ignore_expired_card = 1;
57 $FS::cust_main::ignore_expired_card = 1;
58 $FS::part_pkg::skip_pkg_svc_hack = 1;
59 $FS::part_pkg::skip_pkg_svc_hack = 1;
60 $FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack = 1;
61 $FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack = 1;
62 $FS::svc_Common::noexport_hack = 1;
63 $FS::svc_Common::noexport_hack = 1;
64 $FS::svc_domain::whois_hack = 1;
65 $FS::svc_domain::whois_hack = 1;
66 $FS::cust_pkg::disable_agentcheck = 1;
67 $FS::cust_pkg::disable_agentcheck = 1;
69 my $void_paynum = 2147483646; #top of int range
73 # source(remote) setup
75 $sdbh = DBI->connect($source_datasrc, $source_user, $source_pw)
78 $sdbh->{ChopBlanks} = 1;
83 $map{'_DOMSVC'} = \%domsvc_map;
85 import_table('pkg_category', 'nomap' => 1);
86 import_table('pkg_class', 'nomap' => 1,
87 'preinsert_callback' => sub {
88 my($row, $object) = @_;
89 my $src_categorynum = $row->{'categorynum'};
90 my $dest_categorynum = $map{'pkg_category'}->{$src_categorynum};
91 if ( $dest_categorynum ) {
92 $object->categorynum($dest_categorynum);
97 import_table('reason_type', 'nomap' => 1);
98 foreach my $src_typenum ( keys %{ $map{'reason_type'} } ) {
99 import_table('reason', 'reason_type' => $src_typenum,
100 'search' => 'reason_type',
101 'map' => 'reason_type',
105 my $customer_sth = $sdbh->prepare(
106 "SELECT * FROM cust_main WHERE agentnum = $src_agentnum ORDER BY custnum"
107 ) or die $sdbh->errstr;
109 $customer_sth->execute or die $customer_sth->errstr;
113 while ( my $customerrow = $customer_sth->fetchrow_hashref ) {
115 my $src_custnum = $customerrow->{'custnum'};
117 if ( $customerrow->{'referral_custnum'} ) {
118 warn " $src_custnum has referral_custnum ". $customerrow->{'referral_custnum'};
119 $referrals{$src_custnum} = $customerrow->{'referral_custnum'};
122 my $cust_main = new FS::cust_main {
125 'referral_custnum' => '',
126 'refnum' => $dest_refnum,
127 'agentnum' => $dest_agentnum,
128 'agent_custid' => $src_custnum,
131 my $error = $cust_main->insert;
133 warn "*** WARNING: error importing customer src custnum $src_custnum: $error";
137 warn "inserting dest customer ". $cust_main->custnum. " for $src_custnum\n";
139 $map{'cust_main'}->{$src_custnum} = $cust_main->custnum;
141 #now import the relations, easy and hard:
143 import_table( 'cust_location', 'custnum' => $src_custnum );
145 import_table( 'cust_main_note', 'custnum' => $src_custnum );
147 import_table( 'cust_pay', 'custnum' => $src_custnum );
149 import_table( 'cust_credit', 'custnum' => $src_custnum,
150 'preinsert_callback' => sub {
151 my($row, $object) = @_;
152 my $src_reasonnum = $row->{'reasonnum'};
153 my $dest_reasonnum = $map{'reason'}->{$src_reasonnum};
154 if ( $dest_reasonnum ) {
155 $object->reasonnum($dest_reasonnum);
160 import_table( 'cust_refund', 'custnum' => $src_custnum,
161 'post_callback' => sub {
162 #my( $src_refundnum, $dst_refundnum ) = @_;
163 my $src_refundnum = shift;
165 # cust_pay_refund (map refundnum and paynum...)
166 import_table( 'cust_pay_refund',
167 'refundnum' => $src_refundnum,
168 'search' => 'refundnum',
169 'map' => 'cust_refund',
170 'map2' => 'cust_pay',
171 'map2key' => 'paynum',
178 import_table( 'cust_pay_void', 'custnum' => $src_custnum,
179 'preinsert_callback' => sub {
180 my($row, $object) = @_;
181 $object->paynum( $void_paynum-- );
185 # no data in old db for:
186 # cust_attachment, cust_statement, cdr, cdr_*, cust_bill_event,
187 # cust_main_exemption, cust_pay_batch, cust_tax_*, cust_recon,
188 # inventory_item, part_bill_event, part_device, part_export,
189 # part_pop_local, part_virtual_field, pay_batch, phone_*,
190 # payment_gateway_*, prepay_credit, port, radius_usergroup,
191 # rate_*, reg_code, reg_code_pkg, registrar, router,
192 # svc_acct, svc_acct_pop, svc_broadband, svc_external,
193 # svc_forward, svc_phone, svc_www, tax_*, usage_class, virtual_field
194 # appears to be unused in old db: inventory_class
197 #werid direct cust_main relations:
199 warn " inserting cust_pkg for src cust $src_custnum\n";
200 # cust_pkg (part_pkg, part_svc, etc.)
201 import_table( 'cust_pkg', 'custnum' => $src_custnum,
202 'preinsert_callback' => sub {
203 my($row, $object) = @_;
204 my $src_pkgpart = $row->{'pkgpart'} or die "wtf";
205 my $dest_pkgpart = $map{'part_pkg'}->{$src_pkgpart};
206 if ( $dest_pkgpart ) {
207 $object->pkgpart($dest_pkgpart);
211 my $sth = $sdbh->prepare(
212 "SELECT * FROM part_pkg WHERE pkgpart = $src_pkgpart"
213 ) or die $sdbh->errstr;
215 $sth->execute or die $sth->errstr;
217 my $part_pkg_row = $sth->fetchrow_hashref
218 or die "cust_pkg.pkgpart missing in part_pkg?!";
224 my $src_classnum = $part_pkg_row->{'classnum'};
225 $hashref->{'classnum'} = $map{'pkg_class'}->{ $src_classnum }
228 my $part_pkg = new FS::part_pkg $hashref;
230 #$part_pkg->setuptax('') if $part_pkg->setuptax =~ /^\s+$/;
231 #$part_pkg->recurtax('') if $part_pkg->recurtax =~ /^\s+$/;
233 my $error = $part_pkg->insert( 'options' => {} );
234 die "*** FATAL: error importing part_pkg src pkgpart $src_pkgpart ".
238 $map{ 'part_pkg' }->{ $part_pkg_row->{'pkgpart'} } = $part_pkg->pkgpart;
241 import_table( 'part_pkg_option',
242 'pkgpart' => $src_pkgpart,
243 'search' => 'pkgpart',
247 my $osth = $sdbh->prepare(
248 "SELECT * FROM part_pkg_option WHERE pkgpart = $src_pkgpart"
249 ) or die $sdbh->errstr;
251 # pkg_svc, part_svc, part_svc_column
252 import_table( 'pkg_svc',
253 'pkgpart' => $src_pkgpart,
254 'search' => 'pkgpart',
256 'preinsert_callback' => sub {
258 my($row, $object) = @_;
259 my $src_svcpart = $row->{'svcpart'} or die "wtf2";
260 my $dest_svcpart = $map{'part_svc'}->{$src_svcpart};
261 if ( $dest_svcpart ) {
262 $object->svcpart($dest_svcpart);
266 my $sth = $sdbh->prepare(
267 "SELECT * FROM part_svc WHERE svcpart = $src_svcpart"
268 ) or die $sdbh->errstr;
270 $sth->execute or die $sth->errstr;
272 my $part_svc_row = $sth->fetchrow_hashref
273 or die "svcpart missing in part_svc?!";
280 my $part_svc = new FS::part_svc $hashref;
281 $part_svc->disabled('') if $part_svc->disabled =~ /^\s+$/;
282 my $error = $part_svc->insert;
283 die "*** FATAL: error importing part_svc src svcpart $src_svcpart ".
287 $map{ 'part_svc' }->{ $part_svc_row->{'svcpart'} } = $part_svc->svcpart;
290 import_table( 'part_svc_column',
291 'svcpart' => $src_svcpart,
292 'search' => 'svcpart',
294 'preinsert_callback' => sub {
295 my($row, $object) = @_;
296 if ( $object->columnname eq 'domsvc' ) {
297 $object->columnvalue( $map{'_DOMSVC'}->{ $object->columnvalue } );
302 #what we came here for in the first place
303 $object->svcpart( $part_svc->svcpart );
308 #what we came here for in the first place
309 $object->pkgpart( $part_pkg->pkgpart );
313 'post_callback' => sub {
314 #my( $src_pkgnum, $dst_pkgnum ) = @_;
315 my $src_pkgnum = shift;
317 #XXX grr... action makes this very hard...
318 ## cust_pkg_reason (shit, and bring in/remap reasons)
319 #import_table( 'cust_pkg_reason',
320 # 'pkgnum' => $src_pkgnum,
321 # 'search' => 'pkgnum',
322 # 'map' => 'cust_pkg',
323 # 'map2' => 'reason',
324 # 'map2key' => 'reasonnum',
328 import_table( 'cust_svc',
329 'pkgnum' => $src_pkgnum,
330 'search' => 'pkgnum',
332 'map2' => 'part_svc',
333 'map2key' => 'svcpart',
334 'post_callback' => sub {
335 #my( $src_svcnum, $dst_svcnum ) = @_;
336 my $src_svcnum = shift;
339 import_table( 'svc_domain',
340 'svcnum' => $src_svcnum,
341 'search' => 'svcnum',
343 'noblank_primary' => 1,
349 import_table('cust_pkg_detail',
350 'pkgnum' => $src_pkgnum,
351 'search' => 'pkgnum',
358 # end of cust_pkg (part_pkg, part_svc, etc.)
360 warn " inserting cust_bill for src cust $src_custnum\n";
361 # cust_bill (invnum move)
362 import_table( 'cust_bill', 'custnum' => $src_custnum,
363 'preinsert_callback' => sub {
364 my($row, $object) = @_;
365 $object->agent_invid( $row->{'invnum'} );
367 'post_callback' => sub {
368 my( $src_invnum, $dst_invnum ) = @_;
369 #my $src_invnum = shift;
371 # cust_bill_pkg ( map invnum and pkgnum... )
372 import_table( 'cust_bill_pkg',
373 'invnum' => $src_invnum,
374 'search' => 'invnum',
375 'map' => 'cust_bill',
376 'map2' => 'cust_pkg',
377 'map2key' => 'pkgnum',
378 'post_callback' => sub {
379 my $src_billpkgnum = shift;
381 import_table( 'cust_bill_pkg_detail',
382 'cust_bill_pkg.billpkgnum' => $src_billpkgnum,
383 'search' => 'cust_bill_pkg.billpkgnum',
384 'map' => 'cust_bill_pkg',
385 'addl_from' => 'left join cust_bill_pkg using ( invnum, pkgnum )',
391 # cust_credit_bill (map invnum and crednum... )
392 import_table( 'cust_credit_bill',
393 'invnum' => $src_invnum,
394 'search' => 'invnum',
395 'map' => 'cust_bill',
396 'map2' => 'cust_credit',
397 'map2key' => 'crednum',
398 'post_callback' => sub {
399 my $src_creditbillnum = shift;
400 #map creditbillnum and billpkgnum
401 import_table( 'cust_credit_bill_pkg',
402 'creditbillnum' => $src_creditbillnum,
403 'search' => 'creditbillnum',
404 'map' => 'cust_credit_bill',
405 'map2' => 'cust_bill_pkg',
406 'map2key' => 'billpkgnum',
412 # cust_bill_pay (map invnum and paynum...)
413 import_table( 'cust_bill_pay',
414 'invnum' => $src_invnum,
415 'search' => 'invnum',
416 'map' => 'cust_bill',
417 'map2' => 'cust_pay',
418 'map2key' => 'paynum',
419 'post_callback' => sub {
420 my $src_billpaynum = shift;
421 #map billpaynum and billpkgnum
422 import_table( 'cust_bill_pay_pkg',
423 'billpaynum' => $src_billpaynum,
424 'search' => 'billpaynum',
425 'map' => 'cust_bill_pay',
426 'map2' => 'cust_bill_pkg',
427 'map2key' => 'billpkgnum',
432 #need to do something about events. mark initial stuff as done
433 foreach my $eventpart ( @{ $eventparts{$cust_main->payby} } ) {
435 my $cust_event = new FS::cust_event {
436 'eventpart' => $eventpart,
437 'tablenum' => $dst_invnum,
438 '_date' => time, # XXX something? probably not
442 my $error = $cust_event->insert;
443 die "*** FATAL: error inserting cust_event for eventpart $eventpart,".
444 " tablenum (invnum) $dst_invnum: $error"
454 # (not used in old db: cust_bill_pay_batch, cust_pkg_option)
458 # (not in old db: cust_bill_pkg_display, cust_bill_pkg_tax_location,
459 # cust_bill_pkg_tax_rate_location, cust_tax_adjustment, cust_svc_option, )
460 # (not used in old db: cust_tax_exempt_pkg)
462 #do this last, so no notices go out
463 import_table( 'cust_main_invoice', 'custnum' => $src_custnum );
465 #dbh->commit or die dbh->errstr;
466 warn "customer ". $cust_main->custnum. " inserted\n";
471 foreach my $agent_custid ( keys %referrals ) {
472 my $referred_cust = qsearchs('cust_main',
473 { 'agentnum' => $dest_agentnum,
474 'agent_custid' => $agent_custid,
477 $referred_cust->referral_custnum($map{'cust_main'}->{$referrals{$agent_custid}});
478 $referred_cust->replace;
482 warn "import successful!\n";
484 warn "rolling back (dry run)\n";
485 dbh->rollback or die dbh->errstr;
489 dbh->commit or die dbh->errstr;
494 my( $table, %opt ) = @_;
496 eval "use FS::$table;";
499 my $map = $opt{'map'} || 'cust_main';
500 my $search = $opt{'search'} || 'custnum';
502 $opt{'insert_opts'} ||= [];
504 my $primary_key = dbdef->table($table)->primary_key;
506 my $addl_from = defined($opt{'addl_from'}) ? $opt{'addl_from'} : '';
508 my $sth = $sdbh->prepare(
509 "SELECT * FROM $table $addl_from ".
510 ( $opt{'nomap'} ? '' : " WHERE $search = ". $opt{$search} )
511 ) or die $sdbh->errstr;
513 $sth->execute or die "(searching $table): ". $sth->errstr;
515 while ( my $row = $sth->fetchrow_hashref ) {
516 #my $src_custnum = $customerrow->{'custnum'};
518 my $hashref = { %$row };
519 $hashref->{$primary_key} = ''
520 unless $opt{'noblank_primary'};
521 $hashref->{ $search } = $map{$map}->{ $row->{$search} }
522 unless $opt{'nomap'};
524 if ( $opt{'map2'} ) {
525 my $key2 = $opt{'map2key'};
526 $hashref->{$key2} = $map{ $opt{'map2'} }->{ $row->{$key2} }
527 unless $opt{map2key} eq 'pkgnum' && ( $row->{$key2} eq '0'
528 || $row->{$key2} eq '-1'
530 or ! defined($row->{$key2})
531 or $row->{$key2} eq '';
532 #warn "map $opt{map2}.$opt{map2key}: ". $row->{$key2}. " to ". $map{ $opt{'map2'} }->{ $row->{$key2} };
535 if ( $opt{'map3'} ) {
536 my $key3 = $opt{'map3key'};
537 $hashref->{$key3} = $map{ $opt{'map3'} }->{ $row->{$key3} };
540 my $object = eval "new FS::$table \$hashref;";
543 &{ $opt{preinsert_callback} }( $row, $object )
544 if $opt{preinsert_callback};
546 my $error = $object->insert( @{ $opt{'insert_opts'} } );
548 warn "*** WARNING: error importing $table src $primary_key ". $row->{$primary_key}. ": $error";
552 $map{ $table }->{ $row->{$primary_key} } = $object->get($primary_key);
554 &{ $opt{post_callback} }( $row->{$primary_key}, $object->get($primary_key) )
555 if $opt{post_callback};