#!/usr/bin/perl -Tw
use strict;
-use DBI;
+use FS::DBI;
use HTML::TableParser;
use Date::Parse;
use Text::CSV_XS;
);
-$s_dbh = DBI->connect($s_dbname, $s_dbuser, $s_dbpass,
+$s_dbh = FS::DBI->connect($s_dbname, $s_dbuser, $s_dbpass,
{ 'AutoCommit' => 0,
'ChopBlanks' => 1,
'ShowErrorStatement' => 1
unless (scalar(@$cols) == $columncount);
my $sql = "INSERT INTO $sourcefile VALUES(".
- join(', ', map { $s_dbh->quote($_) } @$cols). ")";
+ join(', ', map { s/\s*(\S[\S ]*?)\s*$/$1/; $s_dbh->quote($_) } @$cols). ")";
$s_dbh->do($sql) or die "insert failed: ". $s_dbh->errstr;
$rowcount++;
warn "row $rowcount\n" unless ($rowcount % 1000);
'fields' => [ qw( username null finger _password status garbage ) ],
'fixup' => sub {
my $hash = shift;
- return 1
- if $object_map{'legacy_ppp'}{$hash->{'username'}};
+ #return 1
+ # if $object_map{'legacy_ppp'}{$hash->{'username'}};
delete $hash->{$_}
foreach qw (null status garbage);
$hash->{'svcpart'} = $legacy_email_svcpart;
sub pkg_freq {
my ( $href ) = ( shift );
- $href->{'one_time_list'}
+ my $once;
+ $href->{'one_time_list'} =~ /^\s*(\S[\S ]*?)\s*$/ && ($once = $1);
+ $once
? 0
-# : int(eval "$href->{'months_credit'} + 0");
- : int(eval "$href->{'month_credit'} + 0");
+ : int(eval "$href->{'months_credit'} + 0");
+# int(eval "$href->{'month_credit'} + 0");
+}
+
+sub account_id {
+ my $href = shift;
+ if ($href->{'slave_account_id'} =~ /^\s*(\S[\S ]*?)\s*$/) {
+ "slave:$1";
+ }else{
+ my $l = $href->{cbilling_cycle_login};
+ $l =~ /^\s*(\S[\S ]*?)\s*$/ && ($l = $1);
+ $l;
+ }
}
sub b_or {
my ( $field, $hash ) = ( shift, shift );
- $field = 'bill_'. $field
+ $field = 'billing_'. $field
if $hash->{'billing_use'} eq 'Billing Address';
$hash->{$field};
}
sub p_or {
my ( $field, $hash ) = ( shift, shift );
- $field = 'bill_'. $field
+ $field = 'billing_'. $field
if $hash->{'billing_use'} eq 'Billing Address';
my $ac = ( $hash->{$field. '_area_code'}
&& $hash->{$field. '_area_code'} =~ /^\d{3}$/ )
}
my @tables = (
-#part_pkg => { 'stable' => 'product',
-part_pkg => { 'stable' => 'billcycle',
+part_pkg => { 'stable' => 'product',
+#part_pkg => { 'stable' => 'billcycle',
'mapping' =>
{ 'pkg' => sub { my $href = shift;
$href->{'description'}
},
'comment' => 'product_id',
'freq' => sub { pkg_freq(shift) },
- 'recur' => sub { my $href = shift;
+ 'recur_fee'=> sub { my $href = shift;
my $price = ( pkg_freq($href)
? $href->{'unit_price'}
: 0
$part_pkg->$_('');
($_ => $v);
}
- qw (setup recur)
+ qw (setup_fee recur_fee)
};
my $error =
$part_pkg->insert(options=>$options);
$row->{'login'} =~ /^\s*(\S[\S ]*?)\s*$/
&& ($login = $1);
+ my ($first, $last, $company) =
+ ('', '', '');
+ $cust_main->first =~ /^\s*(\S[\S ]*?)\s*$/
+ && ($first = $1);
+ $cust_main->last =~ /^\s*(\S[\S ]*?)\s*$/
+ && ($last = $1);
+ $cust_main->company =~ /^\s*(\S[\S ]*?)\s*$/
+ && ($company = $1);
+
+ unless ($first || $last || $company) {
+ warn "bogus entry: ". $row->{'login'};
+ return 1;
+ }
+
my $id = $master_account
? 'slave:'. $customer_id
: $login;
+ #my $id = $login;
my $status = $row->{status};
my $cancelled = 0;
if ( $status ne 'Current'
+ && $status ne 'current'
&& $status ne 'On Hold' )
{
$cancelled = 1;
- $cancel{$id} =
+ $cancel{$login} =
str2time($row->{termination_date});
}
$susp{$id} = str2time($row->{hold_date})
my $svcnum =
$object_map{legacy_ppp}{$row->{'login'} };
- unless( $cancelled || $svcnum ) {
+ unless( $cancelled || $svcnum || $status eq 'Pn Hold' ) {
warn "can't find svc_acct for legacy ppp ".
$row->{'login'}, "\n";
}
$object_map{svc_acct}{$id} = $svcnum
unless $cancelled;
- $master_map{$login} = $row->{master_account}
- if $row->{master_account};
- return 1 if $row->{master_account};
+ $master_map{$login} = $master_account
+ if $master_account;
+ return 1 if $master_account;
$cust_main->ship_country('US')
if $cust_main->has_ship_address;
ut_name_fixup($cust_main, 'first');
}
$r;
},
- 'pkgpart' => sub { my $p = shift->{product_id};
+ 'pkgpart' => sub { my $href = shift;
+ my $p = $href->{product_id};
$p =~ /^\s*(\S[\S ]*?)\s*$/ && ($p = $1);
- $package_cache{$p}
- ? $package_cache{$p}->pkgpart
- : '';
+ my $pkg = $package_cache{$p}
+ if $package_cache{$p};
+
+ my $month = '';
+ $href->{month_credit} =~ /\s*(\S[\S ]*?)\s*$/ && ($month = $1);
+ $month = int(eval "$month + 0");
+
+ my $price = 0;
+ $href->{unit_price} =~ /\s*(\S[\S ]*?)\s*$/ && ($price = $1);
+ $price = eval "$price + 0";
+
+ if ($pkg) {
+ $pkg = ''
+ unless $pkg->freq + 0 == $month;
+
+ if ($pkg && ($pkg->freq + 0)) {
+ my $recur = 0;
+ $pkg->recur_fee =~ /\s*(\S[\S ]*?)\s*$/ && ($recur = $1);
+ $recur = eval "$recur + 0";
+ $pkg = ''
+ unless $recur == $price;
+ }
+
+ if ($pkg) {
+ $pkg = ''
+ unless $pkg->setuptax
+ eq ($href->{taxable} ? '' : 'Y');
+ }
+
+ }
+
+ unless ($pkg) {
+ my $pkghref = { 'pkg' => ($href->{description} ? $href->{description} : $href->{product_id} ),
+ 'comment' => $href->{product_id},
+ 'freq' => $month,
+ 'setuptax' => ($href->{'taxable'} ? '' : 'Y'),
+ 'recurtax' => ($href->{'taxable'} ? '' : 'Y'),
+ 'plan' => 'flat',
+ 'disabled' => 'Y',
+ };
+
+ my @pkgs = qsearch('part_pkg', $pkghref);
+ my $recur = sprintf("%.2f", ($month ? $price : 0));
+ for (@pkgs) {
+ my %options = $_->options;
+ if ($options{recur_fee} eq $recur) {
+ $pkg = $_;
+ last;
+ }
+ }
+
+ $pkghref->{recur_fee} = $recur
+ unless $pkg;
+
+ my $pkg_svc = {};
+
+ if ($month){
+ $pkg_svc->{$legacy_ppp_svcpart} = 1;
+ $pkg_svc->{$legacy_email_svcpart} =
+ $href->{emails_allowed}
+ if $href->{emails_allowed};
+ }
+ $pkghref->{pkg_svc} = $pkg_svc;
+ $pkghref->{primary_svc}
+ = ( $month
+ ? $legacy_ppp_svcpart
+ : '');
+ unless ($pkg) {
+ $pkg = new FS::part_pkg $pkghref;
+ my $options =
+ { map { my $v = $pkg->$_;
+ $pkg->$_('');
+ ($_ => $v);
+ }
+ qw (setup_fee recur_fee)
+ };
+ my $error =
+ $pkg->insert(options=>$options);
+ if ($error) {
+ warn "Error inserting pkg ".
+ join(", ", map{"$_ => ". $pkg->get($_)} fields $pkg).
+ ": $error\n";
+ $pkg = '';
+ }
+ }
+ }
+ $pkg ? $pkg->pkgpart : '';
},
'setup' => sub { str2time(shift->{creation_date}) },
- 'bill' => sub { my $href = shift;
- my $id = $href->{'slave_account_id'}
- ? 'slave:'. $href->{'slave_account_id'}
- : $href->{'cbilling_cycle_login'};
- $bill{$id};
+ 'bill' => sub { $bill{account_id(shift)}
+ #$bill{$href->{cbilling_cycle_login}};
},
- 'susp' => sub { my $href = shift;
- my $id = $href->{'slave_account_id'}
- ? 'slave:'. $href->{'slave_account_id'}
- : $href->{'cbilling_cycle_login'};
- $susp{$id};
+ 'susp' => sub { $susp{account_id(shift)}
+ #$susp{$href->{cbilling_cycle_login}};
},
- 'adjo' => sub { my $href = shift;
- my $id = $href->{'slave_account_id'}
- ? 'slave:'. $href->{'slave_account_id'}
- : $href->{'cbilling_cycle_login'};
- $adjo{$id};
+ 'adjo' => sub { $adjo{account_id(shift)}
+ #$adjo{$href->{cbilling_cycle_login}};
},
- 'cancel' => sub { my $href = shift;
- my $id = $href->{'slave_account_id'}
- ? 'slave:'. $href->{'slave_account_id'}
- : $href->{'cbilling_cycle_login'};
- $cancel{$id};
+ 'cancel' => sub { $cancel{account_id(shift)}
+ #$cancel{$href->{cbilling_cycle_login}};
},
},
'fixup' => sub { my ($object, $row) = (shift,shift);
},
'skey' => sub { my $object = shift;
my $href = shift;
- if ($href->{'slave_account_id'} =~ /^\s*(\S[\S ]*?)\s*$/) {
- "slave:$1";
- }else{
- my $id = $href->{'billing_cycle_item_id'};
- $cust_pkg_map{$id} = $object->pkgnum;
- $href->{'cbilling_cycle_login'};
- }
+ my $id = $href->{'billing_cycle_item_id'};
+ $id =~ /^\s*(\S[\S ]*?)\s*$/ && ($id = $1);
+ $cust_pkg_map{$id} = $object->pkgnum;
+ account_id($href);
},
'wrapup' => sub { for my $id (keys %{$object_map{'cust_pkg'}}){
my $cust_svc =
return 1 if ($sy == $cy && $sm < $cm);
return 1 if ($sy == $cy && $sm == $cm && $sd <= $cd);
}
- return 1 if $object_map{'cust_main'}{$object->username};
+ #return 1 if $object_map{'cust_main'}{$object->username};
+
+ my $email_name;
+ $row->{email_name} =~ /^\s*(\S[\S ]*?)\s*$/
+ && ($email_name = $1);
my $svcnum =
- $object_map{legacy_email}{$row->{'email_name'} };
+ $object_map{legacy_email}{$email_name}
+ if $email_name;
unless( $svcnum ) {
warn "can't find svc_acct for legacy email ".
$row->{'email_name'}, "\n";
return 1;
}
-
+
$object_map{svc_acct}{'email:'.$row->{'email_customer_id'}} = $svcnum;
return 1;
},
# my $href = shift;
# 'email:'. $href->{'email_customer_id'};
# },
- 'wrapup' => sub { for my $id (keys %{$object_map{'cust_pkg'}}){
+ 'wrapup' => sub { for my $id (keys %{$object_map{'svc_acct'}}){
next unless $id =~ /^email:(\d+)/;
my $custid = $1;
my $cust_svc =
next;
}
+ if ($cust_svc->pkgnum) {
+ warn "service already linked for $id\n";
+ next;
+ }
+
$cust_svc->
pkgnum($cust_pkg_map{$custid});
- my $error = $cust_svc->replace;
- warn "error linking legacy email $id: $error\n"
- if $error;
+ if ($cust_svc->pkgnum){
+ my $error = $cust_svc->replace;
+ warn "error linking legacy email $id: $error\n"
+ if $error;
+ }else{
+ warn "can't find package for $id\n"
+ }
}
},
},
}
+# link to any uncancelled package on customer
+foreach my $username ( keys %{$object_map{'legacy_email'}} ) {
+ my $cust_svc = qsearchs( 'cust_svc',
+ { 'svcnum' => $object_map{legacy_email}{$username} }
+ );
+ next unless $cust_svc;
+ next if $cust_svc->pkgnum;
+
+ my $custnum = $object_map{cust_main}{$username};
+ unless ($custnum) {
+ my $master = $master_map{$username};
+ $custnum = $object_map{'cust_main'}{$master}
+ if $master;
+ next unless $custnum;
+ }
+
+ #my $extra_sql = ' AND 0 != (select freq from part_pkg where '.
+ # 'cust_pkg.pkgpart = part_pkg.pkgpart )';
+ my $extra_sql = " AND 'Prior balance' != (select pkg from part_pkg where ".
+ "cust_pkg.pkgpart = part_pkg.pkgpart )";
+
+ my @cust_pkg = qsearch( {
+ 'table' => 'cust_pkg',
+ 'hashref' => { 'custnum' => $custnum,
+ 'cancel' => '',
+ },
+ 'extra_sql' => $extra_sql,
+ } );
+ next unless scalar(@cust_pkg);
+
+ $cust_svc->pkgnum($cust_pkg[0]->pkgnum);
+ $cust_svc->replace;
+}
+
+
if ($dry_run) {
$d_dbh->rollback;
}else{