3 #script to move customers from one installation to another
4 # script is kinda-specific to a somewhat old source installation (1.7? older?)
5 # target installation has to be 1.9 (after 9/2009)
10 use FS::UID qw( adminsuidsetup dbh );
11 use FS::Schema qw( dbdef );
12 use FS::Record qw( qsearchs );
17 use FS::cust_bill_ApplicationCommon;
21 #ssh -p 2222 -L 1080:66.209.32.4:7219 -L 5454:localhost:5432 66.209.32.4
23 #my $source_datasrc = 'DBI:Pg:host=66.209.32.4;dbname=freeside;sslmode=require';
24 my $source_datasrc = 'DBI:Pg:host=localhost;port=5454;dbname=freeside';
25 my $source_user = 'readonly';
28 my @source_agents = ( 2, 7, 3, 4, 5, 1 );
31 my $dest_agent_typenum = 1; #?
33 my $dest_refnum = 1; #XXX
35 my $dest_legacy_credit_reasontype = 4;
37 my $dest_pkg_classnum = 1;
42 or die "Usage:\n (edit variables at top of script and then)\n".
43 " move-customers user\n";
46 $sdbh = DBI->connect($source_datasrc, $source_user, $source_pw)
49 import_table('pkg_class', 'nomap' => 1);
51 my $agent_sth = $sdbh->prepare(
52 'SELECT * FROM agent WHERE agentnum IN ( '. join(',', @source_agents ). ')'
53 ) or die $sdbh->errstr;
55 $agent_sth->execute or die $agent_sth->errstr;
59 $FS::cust_main::ignore_expired_card = 1;
60 $FS::cust_main::ignore_expired_card = 1;
62 $FS::part_pkg::skip_pkg_svc_hack = 1;
63 $FS::part_pkg::skip_pkg_svc_hack = 1;
65 $FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack = 1;
66 $FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack = 1;
68 while ( my $agentrow = $agent_sth->fetchrow_hashref ) {
70 my $src_agent = $agentrow->{'agent'};
72 warn "importing customers for $src_agent\n";
74 my $agent = qsearchs('agent', { 'agent' => $src_agent } );
78 warn " using existing agentnum ". $agent->agentnum. "\n";
81 warn "DELETING ALL CUSTOMERS OF $src_agent locally \n";
83 foreach my $statement (
84 'DELETE FROM cust_main WHERE agentnum = '. $agent->agentnum,
85 ( map { "DELETE FROM $_
86 WHERE 0 = ( SELECT COUNT(*) FROM cust_main
87 WHERE cust_main.custnum = $_.custnum )
98 #part_pkg, pkg_svc, part_svc, part_svc_column
103 my $sth = dbh->prepare($statement) or die dbh->errstr;
104 $sth->execute or die $sth->errstr;
108 dbh->commit or die dbh->errstr;
114 warn " creating new agent...\n";
116 $agent = new FS::agent { 'agent' => $src_agent,
117 'typenum' => $dest_agent_typenum };
118 my $error = $agent->insert;
119 die $error if $error;
121 warn " agentnum ". $agent->agentnum. "\n";
125 my $customer_sth = $sdbh->prepare(
126 'SELECT * FROM cust_main WHERE agentnum = '. $agentrow->{'agentnum'}
127 ) or die $sdbh->errstr;
129 $customer_sth->execute or die $customer_sth->errstr;
131 while ( my $customerrow = $customer_sth->fetchrow_hashref ) {
133 # warn Dumper($customerrow);
134 my $src_custnum = $customerrow->{'custnum'};
136 warn " $src_custnum has referral_custnum ". $customerrow->{'referral_custnum'}
137 if $customerrow->{'referral_custnum'};
139 my $cust_main = new FS::cust_main {
142 'referral_custnum' => '', #restore afterwords?
143 'refnum' => $dest_refnum,
144 'agentnum' => $agent->agentnum,
145 'agent_custid' => $src_custnum,
148 $cust_main->ship_country('') if $cust_main->ship_country eq ' ';
150 my $error = $cust_main->insert;
152 warn "*** WARNING: error importing customer src custnum $src_custnum: $error";
154 warn Dumper($cust_main) if $src_custnum == 6854;
158 warn "inserting dest customer ". $cust_main->custnum. " for $src_custnum\n";
160 $map{'cust_main'}->{$src_custnum} = $cust_main->custnum;
162 #easy direct cust_main relations:
164 #XXX ivan showing up as cust_pay otaker? just deal?
166 foreach my $table ( qw(
170 import_table( $table, 'custnum' => $src_custnum );
173 # crap, cust_credit.reason is text in old db
174 #*** WARNING: error importing cust_credit src crednum 2200: failed to set reason for [ FS::cust_credit ]: at ./move-customers line 232.
175 import_table( 'cust_credit', 'custnum' => $src_custnum,
176 'insert_opts' => [ 'reason_type' => $dest_legacy_credit_reasontype ],
177 'preinsert_callback' => sub {
178 my($row, $object) = @_;
179 $object->reason('(none)') if $object->get('reason') =~ /^\s*$/;
183 import_table( 'cust_refund', 'custnum' => $src_custnum,
184 'post_callback' => sub {
185 #my( $src_refundnum, $dst_refundnum ) = @_;
186 my $src_refundnum = shift;
188 # cust_credit_refund (map refundnum and crednum...)
189 import_table( 'cust_credit_refund',
190 'refundnum' => $src_refundnum,
191 'search' => 'refundum',
192 'map' => 'cust_refund',
193 'map2' => 'cust_credit',
194 'map2key' => 'crednum',
197 # cust_pay_refund (map refundnum and paynum...)
198 import_table( 'cust_pay_refund',
199 'refundnum' => $src_refundnum,
200 'search' => 'refundum',
201 'map' => 'cust_refund',
202 'map2' => 'cust_pay',
203 'map2key' => 'paynum',
209 # dunno what's up with this (ship_country ' ', fixed)
210 #*** WARNING: error importing customer src custnum 6854: Illegal (name) (error code illegal_name) ship_last: at ./move-customers line 129.
212 # XXX cust_pay_void (something w/ paynum??? huh) or just deal? there's only 110
214 # (not in old db: cust_attachment, cust_statement, cust_location,
215 # cust_main_exemption, cust_pay_pending )
216 # (not used in old db: cust_pay_batch, cust_tax_exempt)
217 # (not useful to migrate: queue)
219 #werid direct cust_main relations:
221 # cust_pkg (part_pkg, part_svc, etc.)
222 import_table( 'cust_pkg', 'custnum' => $src_custnum,
223 'preinsert_callback' => sub {
224 my($row, $object) = @_;
225 my $src_pkgpart = $row->{'pkgpart'} or die "wtf";
226 my $dest_pkgpart = $map{'part_pkg'}->{$src_pkgpart};
227 if ( $dest_pkgpart ) {
228 $object->pkgpart($dest_pkgpart);
232 my $sth = $sdbh->prepare(
233 "SELECT * FROM part_pkg WHERE pkgpart = $src_pkgpart"
234 ) or die $sdbh->errstr;
236 $sth->execute or die $sth->errstr;
238 my $part_pkg_row = $sth->fetchrow_hashref
239 or die "cust_pkg.pkgpart missing in part_pkg?!";
245 my $src_classnum = $part_pkg_row->{'classnum'};
246 $hashref->{'classnum'} = $map{'pkg_class'}->{ $src_classnum }
249 my $part_pkg = new FS::part_pkg $hashref;
250 my $error = $part_pkg->insert( 'options' => {} );
251 die "*** FATAL: error importing part_pkg src pkgpart $src_pkgpart ".
255 $map{ 'part_pkg' }->{ $part_pkg_row->{'pkgpart'} } = $part_pkg->pkgpart;
258 import_table( 'part_pkg_option',
259 'pkgpart' => $src_pkgpart,
260 'search' => 'pkgpart',
264 my $osth = $sdbh->prepare(
265 "SELECT * FROM part_pkg_option WHERE pkgpart = $src_pkgpart"
266 ) or die $sdbh->errstr;
268 # pkg_svc, part_svc, part_svc_column
269 import_table( 'pkg_svc',
270 'pkgpart' => $src_pkgpart,
271 'search' => 'pkgpart',
273 'preinsert_callback' => sub {
275 my($row, $object) = @_;
276 my $src_svcpart = $row->{'svcpart'} or die "wtf2";
277 my $dest_svcpart = $map{'part_svc'}->{$src_svcpart};
278 if ( $dest_svcpart ) {
279 $object->svcpart($dest_svcpart);
283 my $sth = $sdbh->prepare(
284 "SELECT * FROM part_svc WHERE svcpart = $src_svcpart"
285 ) or die $sdbh->errstr;
287 $sth->execute or die $sth->errstr;
289 my $part_svc_row = $sth->fetchrow_hashref
290 or die "svcpart missing in part_svc?!";
297 my $part_svc = new FS::part_svc $hashref;
298 $part_svc->disabled('') if $part_svc->disabled =~ /^\s+$/;
299 my $error = $part_svc->insert;
300 die "*** FATAL: error importing part_svc src svcpart $src_svcpart ".
304 $map{ 'part_svc' }->{ $part_svc_row->{'svcpart'} } = $part_svc->svcpart;
307 import_table( 'part_svc_column',
308 'svcpart' => $src_svcpart,
309 'search' => 'svcpart',
313 #what we came here for in the first place
314 $object->svcpart( $part_svc->svcpart );
319 #what we came here for in the first place
320 $object->pkgpart( $part_pkg->pkgpart );
324 # end of cust_pkg (part_pkg, part_svc, etc.)
326 # cust_bill (invnum move)
327 import_table( 'cust_bill', 'custnum' => $src_custnum,
328 'preinsert_callback' => sub {
329 my($row, $object) = @_;
330 $object->agent_invid( $row->{'invnum'} );
332 'post_callback' => sub {
333 #my( $src_invnum, $dst_invnum ) = @_;
334 my $src_invnum = shift;
336 # cust_bill_pkg ( map invnum and pkgnum... )
337 import_table( 'cust_bill_pkg',
338 'invnum' => $src_invnum,
339 'search' => 'invnum',
340 'map' => 'cust_bill',
341 'map2' => 'cust_pkg',
342 'map2key' => 'pkgnum',
343 'post_callback' => sub {
344 my $src_billpkgnum = shift;
346 import_table( 'cust_bill_pkg_detail',
347 'billpkgnum' => $src_billpkgnum,
348 'search' => 'billpkgnum',
349 'map' => 'cust_bill_pkg',
350 'addl_from' => 'left join cust_bill_pkg using ( invnum, pkgnum )',
356 # cust_credit_bill (map invnum and crednum... )
357 import_table( 'cust_credit_bill',
358 'invnum' => $src_invnum,
359 'search' => 'invnum',
360 'map' => 'cust_bill',
361 'map2' => 'cust_credit',
362 'map2key' => 'crednum',
363 'post_callback' => sub {
364 my $src_creditbillnum = shift;
365 #map creditbillnum and billpkgnum
366 import_table( 'cust_credit_bill_pkg',
367 'creditbillnum' => $src_creditbillnum,
368 'search' => 'creditbillnum',
369 'map' => 'cust_credit_bill',
370 'map2' => 'cust_bill_pkg',
371 'map2key' => 'billpkgnum',
377 # cust_bill_pay (map invnum and paynum...)
378 import_table( 'cust_bill_pay',
379 'invnum' => $src_invnum,
380 'search' => 'invnum',
381 'map' => 'cust_bill',
382 'map2' => 'cust_pay',
383 'map2key' => 'paynum',
384 'post_callback' => sub {
385 my $src_billpaynum = shift;
386 #map billpaynum and billpkgnum
387 import_table( 'cust_bill_pay_pkg',
388 'billpaynum' => $src_billpaynum,
389 'search' => 'billpaynum',
390 'map' => 'cust_bill_pay',
391 'map2' => 'cust_bill_pkg',
392 'map2key' => 'billpkgnum',
401 # XXX last of the stuff to import...
403 # cust_pkg_reason (shit, and bring in/remap reasons)
409 # (rest not in old db)
410 # svc_acct_pop??? looks like it
412 # (not in old db: cust_pkg_detail)
413 # (not used in old db: cust_bill_pay_batch, cust_pkg_option)
417 # (not in old db: cust_bill_pkg_display, cust_bill_pkg_tax_location,
418 # cust_bill_pkg_tax_rate_location, cust_tax_adjustment, cust_svc_option, )
419 # (not used in old db: cust_tax_exempt_pkg)
422 #need to do something about events. mark initial stuff as done or something?
423 # what else? that's it?
425 #do this last, so no notices go out
426 import_table( 'cust_main_invoice', 'custnum' => $src_custnum );
428 #dbh->commit or die dbh->errstr;
429 warn "customer ". $cust_main->custnum. " inserted\n";
437 my( $table, %opt ) = @_;
439 eval "use FS::$table;";
442 my $map = $opt{'map'} || 'cust_main';
443 my $search = $opt{'search'} || 'custnum';
445 $opt{'insert_opts'} ||= [];
447 my $primary_key = dbdef->table($table)->primary_key;
449 my $addl_from = defined($opt{'addl_from'}) ? $opt{'addl_from'} : '';
451 my $sth = $sdbh->prepare(
452 "SELECT * FROM $table $addl_from ".
453 ( $opt{'nomap'} ? '' : " WHERE $search = ". $opt{$search} )
454 ) or die $sdbh->errstr;
456 $sth->execute or die "(searching $table): ". $sth->errstr;
458 while ( my $row = $sth->fetchrow_hashref ) {
459 #my $src_custnum = $customerrow->{'custnum'};
465 $hashref->{ $search } = $map{$map}->{ $row->{$search} }
466 unless $opt{'nomap'};
468 if ( $opt{'map2'} ) {
469 my $key2 = $opt{'map2key'};
470 $hashref->{$key2} = $map{ $opt{'map2'} }->{ $row->{$key2} }
471 unless $opt{map2key} eq 'pkgnum' && $row->{$key2} eq '0';
472 #warn "map $opt{map2}.$opt{map2key}: ". $row->{$key2}. " to ". $map{ $opt{'map2'} }->{ $row->{$key2} };
475 my $object = eval "new FS::$table \$hashref;";
478 &{ $opt{preinsert_callback} }( $row, $object )
479 if $opt{preinsert_callback};
481 my $error = $object->insert( @{ $opt{'insert_opts'} } );
483 warn "*** WARNING: error importing $table src $primary_key ". $row->{$primary_key}. ": $error";
487 $map{ $table }->{ $row->{$primary_key} } = $object->get($primary_key);
489 &{ $opt{post_callback} }( $row->{$primary_key}, $object->get($primary_key) )
490 if $opt{post_callback};