(start of) customer move script, RT#5351
[freeside.git] / bin / move-customers
1 #!/usr/bin/perl -w
2
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)
6
7 use strict;
8 use vars qw( $sdbh );
9 use DBI;
10 use FS::UID qw( adminsuidsetup dbh );
11 use FS::Schema qw( dbdef );
12 use FS::Record qw( qsearchs );
13 use FS::agent;
14 use FS::cust_main;
15 use FS::part_pkg;
16 use FS::part_svc;
17 use FS::cust_bill_ApplicationCommon;
18
19 my $DANGEROUS = 1;
20
21 #ssh -p 2222 -L 1080:66.209.32.4:7219 -L 5454:localhost:5432 66.209.32.4
22
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';
26 my $source_pw = '';
27
28 my @source_agents = ( 2, 7, 3, 4, 5, 1 );
29
30
31 my $dest_agent_typenum = 1; #?
32
33 my $dest_refnum = 1; #XXX
34
35 my $dest_legacy_credit_reasontype = 4;
36
37 my $dest_pkg_classnum = 1;
38
39 #--
40
41 my $user = shift
42   or die "Usage:\n  (edit variables at top of script and then)\n".
43          "  move-customers user\n";
44 adminsuidsetup $user;
45
46 $sdbh = DBI->connect($source_datasrc, $source_user, $source_pw)
47   or die $DBI::errstr;
48
49 import_table('pkg_class', 'nomap' => 1);
50
51 my $agent_sth = $sdbh->prepare(
52   'SELECT * FROM agent WHERE agentnum IN ( '. join(',', @source_agents ). ')'
53 ) or die $sdbh->errstr;
54
55 $agent_sth->execute or die $agent_sth->errstr;
56
57 my %map = ();
58
59 $FS::cust_main::ignore_expired_card = 1;
60 $FS::cust_main::ignore_expired_card = 1;
61
62 $FS::part_pkg::skip_pkg_svc_hack = 1;
63 $FS::part_pkg::skip_pkg_svc_hack = 1;
64
65 $FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack = 1;
66 $FS::cust_bill_ApplicationCommon::skip_apply_to_lineitems_hack = 1;
67
68 while ( my $agentrow = $agent_sth->fetchrow_hashref ) {
69
70   my $src_agent = $agentrow->{'agent'};
71
72   warn "importing customers for $src_agent\n";
73
74   my $agent = qsearchs('agent', { 'agent' => $src_agent } );
75
76   if ( $agent ) {
77
78     warn "  using existing agentnum ". $agent->agentnum. "\n";
79
80     if ( $DANGEROUS ) {
81       warn "DELETING ALL CUSTOMERS OF $src_agent locally \n";
82
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 )
88                 "
89               }
90               qw(
91                   cust_credit
92                   cust_main_invoice
93                   cust_main_note
94                   cust_pay
95                   cust_refund
96                 )
97         )
98         #part_pkg, pkg_svc, part_svc, part_svc_column
99         #pkg_class
100       ) {
101
102         #warn $statement;
103         my $sth = dbh->prepare($statement) or die dbh->errstr;
104         $sth->execute or die $sth->errstr;
105
106       }
107
108       dbh->commit or die dbh->errstr;
109
110     }
111
112   } else {
113
114     warn "  creating new agent...\n";
115
116     $agent = new FS::agent { 'agent' => $src_agent,
117                              'typenum' => $dest_agent_typenum };
118     my $error = $agent->insert;
119     die $error if $error;
120
121     warn "  agentnum ". $agent->agentnum. "\n";
122
123   }
124
125   my $customer_sth = $sdbh->prepare(
126     'SELECT * FROM cust_main WHERE agentnum = '. $agentrow->{'agentnum'}
127   ) or die $sdbh->errstr;
128
129   $customer_sth->execute or die $customer_sth->errstr;
130
131   while ( my $customerrow = $customer_sth->fetchrow_hashref ) {
132     #use Data::Dumper;
133     # warn Dumper($customerrow);
134     my $src_custnum = $customerrow->{'custnum'};
135
136     warn "   $src_custnum has referral_custnum ". $customerrow->{'referral_custnum'}
137       if $customerrow->{'referral_custnum'};
138
139     my $cust_main = new FS::cust_main {
140       %{ $customerrow },
141       'custnum'      => '',
142       'referral_custnum' => '', #restore afterwords?
143       'refnum'       => $dest_refnum,
144       'agentnum'     => $agent->agentnum,
145       'agent_custid' => $src_custnum,
146     };
147
148     $cust_main->ship_country('') if $cust_main->ship_country eq '  ';
149
150     my $error = $cust_main->insert;
151     if ( $error ) {
152       warn "*** WARNING: error importing customer src custnum $src_custnum: $error";
153       use Data::Dumper;
154       warn Dumper($cust_main) if $src_custnum == 6854;
155       next;
156     }
157
158     warn "inserting dest customer ". $cust_main->custnum. " for $src_custnum\n";
159
160     $map{'cust_main'}->{$src_custnum} = $cust_main->custnum;
161
162     #easy direct cust_main relations:
163
164     #XXX ivan showing up as cust_pay otaker?  just deal?
165
166     foreach my $table ( qw(
167       cust_main_note
168       cust_pay
169     ) ) {
170       import_table( $table, 'custnum' => $src_custnum );
171     }
172
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*$/;
180       },
181     );
182
183     import_table( 'cust_refund', 'custnum' => $src_custnum,
184       'post_callback' => sub {
185         #my( $src_refundnum, $dst_refundnum ) = @_;
186         my $src_refundnum = shift;
187
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',
195                     );
196
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',
204                     );
205
206       },
207     );
208
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.
211
212     # XXX cust_pay_void (something w/ paynum???  huh)  or just deal?  there's only 110
213
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)
218
219     #werid direct cust_main relations: 
220
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);
229           return;
230         }
231
232         my $sth = $sdbh->prepare(
233           "SELECT * FROM part_pkg WHERE pkgpart = $src_pkgpart"
234         ) or die $sdbh->errstr;
235
236         $sth->execute or die $sth->errstr;
237
238         my $part_pkg_row = $sth->fetchrow_hashref
239           or die "cust_pkg.pkgpart missing in part_pkg?!";
240
241         my $hashref = {
242           %{ $part_pkg_row },
243           'pkgpart'  => '',
244         };
245         my $src_classnum = $part_pkg_row->{'classnum'};
246         $hashref->{'classnum'} = $map{'pkg_class'}->{ $src_classnum }
247           if $src_classnum;
248
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 ".
252             ": $error"
253           if $error;
254
255         $map{ 'part_pkg' }->{ $part_pkg_row->{'pkgpart'} } = $part_pkg->pkgpart;
256         
257         # part_pkg_option
258         import_table( 'part_pkg_option',
259                       'pkgpart' => $src_pkgpart,
260                       'search' => 'pkgpart',
261                       'map'    => 'part_pkg',
262                     );
263         
264         my $osth = $sdbh->prepare(
265           "SELECT * FROM part_pkg_option WHERE pkgpart = $src_pkgpart"
266         ) or die $sdbh->errstr;
267
268         # pkg_svc, part_svc, part_svc_column
269         import_table( 'pkg_svc',
270           'pkgpart' => $src_pkgpart,
271           'search'  => 'pkgpart',
272           'map'     => 'part_pkg',
273           'preinsert_callback' => sub {
274
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);
280               return;
281             }
282
283             my $sth = $sdbh->prepare(
284               "SELECT * FROM part_svc WHERE svcpart = $src_svcpart"
285             ) or die $sdbh->errstr;
286
287             $sth->execute or die $sth->errstr;
288
289             my $part_svc_row = $sth->fetchrow_hashref
290               or die "svcpart missing in part_svc?!";
291
292             my $hashref = {
293               %{ $part_svc_row },
294               'svcpart' => '',
295             };
296
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 ".
301                 ": $error"
302               if $error;
303
304             $map{ 'part_svc' }->{ $part_svc_row->{'svcpart'} } = $part_svc->svcpart;
305
306             # part_svc_column
307             import_table( 'part_svc_column',
308                           'svcpart' => $src_svcpart,
309                           'search'  => 'svcpart',
310                           'map'     => 'part_svc',
311                         );
312         
313             #what we came here for in the first place
314             $object->svcpart( $part_svc->svcpart );
315
316           }
317         );
318
319         #what we came here for in the first place
320         $object->pkgpart( $part_pkg->pkgpart );
321
322       },
323     );
324     # end of cust_pkg (part_pkg, part_svc, etc.)
325
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'} );
331       },
332       'post_callback' => sub {
333         #my( $src_invnum, $dst_invnum ) = @_;
334         my $src_invnum = shift;
335
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;
345
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 )',
351                                     );
352
353                       },
354                     );
355
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',
372                                     );
373
374                       },
375                     );
376
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',
393                                     );
394                       },
395                     );
396       },
397     );
398
399     # ---
400
401     # XXX last of the stuff to import...
402     # &
403     # cust_pkg_reason (shit, and bring in/remap reasons)
404     # cust_svc
405     #  then
406     #    svc_acct
407     #     radius_usergroup
408     #    svc_domain
409     #    (rest not in old db)
410     #    svc_acct_pop??? looks like it
411     #
412     # (not in old db: cust_pkg_detail)
413     # (not used in old db: cust_bill_pay_batch, cust_pkg_option)
414
415     # ---
416
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)
420
421     #XXX then:
422     #need to do something about events. mark initial stuff as done or something?
423     # what else?  that's it?
424
425     #do this last, so no notices go out
426     import_table( 'cust_main_invoice', 'custnum' => $src_custnum );
427
428     #dbh->commit or die dbh->errstr;
429     warn "customer ". $cust_main->custnum. " inserted\n";
430     #exit;
431
432   }
433
434 }
435
436 sub import_table {
437   my( $table, %opt ) = @_;
438
439   eval "use FS::$table;";
440   die $@ if $@;
441
442   my $map = $opt{'map'} || 'cust_main';
443   my $search = $opt{'search'} || 'custnum';
444
445   $opt{'insert_opts'} ||= [];
446
447   my $primary_key = dbdef->table($table)->primary_key;
448
449   my $addl_from = defined($opt{'addl_from'}) ? $opt{'addl_from'} : '';
450
451   my $sth = $sdbh->prepare(
452     "SELECT * FROM $table $addl_from ".
453     ( $opt{'nomap'} ? '' : " WHERE $search = ". $opt{$search} )
454   ) or die $sdbh->errstr;
455
456   $sth->execute or die "(searching $table): ". $sth->errstr;
457
458   while ( my $row = $sth->fetchrow_hashref ) {
459     #my $src_custnum = $customerrow->{'custnum'};
460
461     my $hashref = {
462       %{ $row },
463       $primary_key => '',
464     };
465     $hashref->{ $search } = $map{$map}->{ $row->{$search} }
466       unless $opt{'nomap'};
467
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} };
473     }
474
475     my $object = eval "new FS::$table \$hashref;";
476     die $@ if $@;
477
478     &{ $opt{preinsert_callback} }( $row, $object )
479       if $opt{preinsert_callback};
480
481     my $error = $object->insert( @{ $opt{'insert_opts'} } );
482     if ( $error ) {
483       warn "*** WARNING: error importing $table src $primary_key ". $row->{$primary_key}. ": $error";
484       next;
485     }
486
487     $map{ $table }->{ $row->{$primary_key} } = $object->get($primary_key);
488
489     &{ $opt{post_callback} }( $row->{$primary_key}, $object->get($primary_key) )
490       if $opt{post_callback};
491
492   }
493
494 }
495
496 1;
497