summaryrefslogtreecommitdiff
path: root/eg/TEMPLATE_cust_main.import
blob: f6d88c701a484ce4ddedf163a7905bd2a53daa55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl -w
#
# Template for importing legacy customer data

use strict;
use Date::Parse;
use FS::UID qw(adminsuidsetup datasrc);
use FS::Record qw(fields qsearch qsearchs);
use FS::cust_main;
use FS::cust_pkg;
use FS::cust_svc;
use FS::svc_acct;
use FS::pkg_svc;

my $user = shift or die &usage;
adminsuidsetup $user;

# use these for the imported cust_main records (unless you have these in legacy
# data)
my($agentnum)=4;
my($refnum)=5;

# map from legacy billing data to pkgpart, maps imported field
# LegacyBillingData to pkgpart.  your names and pkgparts will be different
my(%pkgpart)=(
  'Employee'          => 10,
  'Business'          => 11,
  'Individual'        => 12,
  'Basic PPP'         => 13,
  'Slave'             => 14,
  'Co-Located Server' => 15,
  'Virtual Web'       => 16,
  'Perk Mail'         => 17,
  'Credit Hold'       => 18,
);

my($file)="legacy_file";

open(CLIENT,$file) 
  or die "Can't open $file: $!"; 

# put a tab-separated header atop the file, or define @fields
#   (use these names or change them below)
#
# for cust_main
#   custnum - unique
#   last - (name)
#   first - (name)
#   company
#   address1
#   address2
#   city
#   state
#   zip
#   country
#   daytime - (phone)
#   night - (phone)
#   fax
#   payby - CARD, BILL or COMP
#   payinfo - Credit card #, P.O. # or COMP authorization
#   paydate - Expiration
#   tax - 'Y' for tax exempt
# for cust_pkg
#   LegacyBillingData - maps via %pkgpart above to a pkgpart
# for svc_acct
#   username

my($header);
$header=<CLIENT>;
chop $header;
my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
#print join("\n",@fields);

my($error);
my($link,$line)=(0,0);
while (<CLIENT>) {
  chop; 
  next if /^[\s\t]*$/; #skip any blank lines

  #define %svc hash for this record
  my(@record)=split(/\t/);
  my(%svc);
  foreach (@fields) {
    $svc{$_}=shift @record;   
  }

  # might need to massage some data like this
  $svc{'payby'} =~ s/^Credit Card$/CARD/io;
  $svc{'payby'} =~ s/^Check$/BILL/io;
  $svc{'payby'} =~ s/^Cash$/BILL/io;
  $svc{'payby'} =~ s/^$/BILL/o;
  $svc{'First'} =~ s/&/and/go; 
  $svc{'Zip'} =~ s/\s+$//go;

  my($cust_main) = new FS::cust_main ( {
    'custnum'  => $svc{'custnum'},
    'agentnum' => $agentnum,
    'last'     => $svc{'last'},
    'first'    => $svc{'first'},
    'company'  => $svc{'company'},
    'address1' => $svc{'address1'},
    'address2' => $svc{'address2'},
    'city'     => $svc{'city'},
    'state'    => $svc{'state'},
    'zip'      => $svc{'zip'},
    'country'  => $svc{'country'},
    'daytime'  => $svc{'daytime'},
    'night'    => $svc{'night'},
    'fax'      => $svc{'fax'},
    'payby'    => $svc{'payby'},
    'payinfo'  => $svc{'payinfo'},
    'paydate'  => $svc{'paydate'},
    'payname'  => $svc{'payname'},
    'tax'      => $svc{'tax'},
    'refnum'   => $refnum,
  } );

  $error=$cust_main->insert;

  if ( $error ) {
    warn $cust_main->_dump;
    warn map "$_: ". $svc{$_}. "|\n", keys %svc;
    die $error;
  }

  my($cust_pkg)=new FS::cust_pkg ( {
    'custnum' => $svc{'custnum'},
    'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
    'setup'   => '', 
    'bill'    => '',
    'susp'    => '',
    'expire'  => '',
    'cancel'  => '',
  } );

  $error=$cust_pkg->insert;
  if ( $error ) {
    warn $svc{'LegacyBillingData'};
    die $error;
  }

  unless ( $svc{'username'} ) {
    warn "Empty login";
  } else {
    #find svc_acct record (imported with bin/svc_acct.import) for this username
    my($svc_acct)=qsearchs('svc_acct',{'username'=>$svc{'username'}});
    unless ( $svc_acct ) {
      warn "username ", $svc{'username'}, " not found\n";
    } else {
      #link to the cust_pkg record we created above

      #find cust_svc record for this svc_acct record
      my($o_cust_svc)=qsearchs('cust_svc',{
        'svcnum' => $svc_acct->svcnum,
        'pkgnum' => '',
      } );
      unless ( $o_cust_svc ) {
        warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
      } else {

        #make sure this svcpart is in pkgpart
        my($pkg_svc)=qsearchs('pkg_svc',{
          'pkgpart'  => $pkgpart{$svc{'LegacyBillingData'}},
          'svcpart'  => $o_cust_svc->svcpart,
          'quantity' => 1,
        });
        unless ( $pkg_svc ) {
          warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
               " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
        } else {

          #create new cust_svc record linked to cust_pkg record 
          my($n_cust_svc) = new FS::cust_svc ({
            'svcnum'  => $o_cust_svc->svcnum,
            'pkgnum'  => $cust_pkg->pkgnum,
            'svcpart' => $pkg_svc->svcpart,
          });
          my($error) = $n_cust_svc->replace($o_cust_svc);
          die $error if $error;
          $link++;
        }
      }
    }
  }

  $line++;

}

warn "\n$link of $line lines linked\n";

# ---

sub usage {
  die "Usage:\n\n  cust_main.import user\n";
}