summaryrefslogtreecommitdiff
path: root/bin/artera.import
blob: 716dddad07e0a2c106324532d2e1907b091b2876 (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
#!/usr/bin/perl -w

use strict;

use Text::CSV_XS;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearchs);
use FS::svc_external;
use FS::svc_domain;
use FS::svc_acct;

$FS::svc_Common::noexport_hack = 1;

my $svcpart = 30;

my $user = shift
  or die 'Usage:\n\n  artera.import user <artera_active_orders.csv';
adminsuidsetup $user;

##

my $csv = new Text::CSV_XS;

my $header = scalar(<>);

my( $num, $linked ) = ( 0, 0 );

while (<>) {
  my $status = $csv->parse($_)
    or die $csv->error_input;
  my($serial, $keycode, $name, $ordernum, $email) = $csv->fields();
  #warn join(" - ", $serial, $keycode, $name, $ordernum, $email ). "\n";

  $email =~ /^([^@]+)\@([^@]+)$/
    or die $email;
  my($username, $domain) = ( $1, $2 );
  my $svc_domain = qsearchs('svc_domain', { 'domain'  => $domain } );
  my $cust_svc = '';
  if ( $svc_domain ) {
    my $svc_acct = qsearchs('svc_acct', {
      'username' => $username,
      'domsvc'   => $svc_domain->svcnum,
    } );
    $cust_svc = $svc_acct->cust_svc
      if $svc_acct;
  #} else {
  #  warn "can't find domain $domain\n";
  }

  my $exist = qsearchs('svc_external', { 'id' => $serial } );
  next if $exist;

  my $svc_external = new FS::svc_external { 
    'svcpart' => $svcpart,
    'pkgnum'  => ( $cust_svc ? $cust_svc->pkgnum : '' ),
    'id'      => $serial,
    'title'   => $keycode,
  };
  #my $error = $svc_external->check;
  my $error = $svc_external->insert;
  if ( $cust_svc && $error =~ /^Already/ ) {
    warn $error;
    $svc_external->pkgnum('');
    $error = $svc_external->insert;
  }
  warn $error if $error;

  $num++;
  $linked++ if $cust_svc;
  #print "$num imported, $linked linked\n";

}

print "$num imported, $linked linked\n";