summaryrefslogtreecommitdiff
path: root/bin/opensrs_domain_pkgs
blob: 2420095508804aa04ce5306f18f5d10473396f2c (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
#!/usr/bin/perl -w

use strict;
use DateTime;
use Date::Format;
use Date::Parse;
use Net::OpenSRS;
use Net::Whois::Raw;
use Data::Dumper;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearchs qsearch);
use FS::Conf;
use FS::svc_domain;
use FS::part_pkg;
use FS::part_export;

my $exportnum = 1;
my $pkgpart = 631;
my $user = 'qis';

adminsuidsetup $user;

my $part_export = qsearchs('part_export' => { exportnum => $exportnum })
  or die "can't find export $exportnum\n";

my $srs = $part_export->get_srs;

my $rv = $srs->make_request(
  {
    action     => 'get_domains_by_expiredate',
    object     => 'domain',
    attributes => {
      exp_from => time2str('%Y-%m-%d', time() - 4*24*60*60),
      exp_to   => time2str('%Y-%m-%d', time() + 10*366*24*60*60),
      limit    => 10000,
    }
  }
);

die $rv->{response_text} unless $rv->{is_success};

my %domains = map { $_->{name}, $_ } @{ $rv->{attributes}->{exp_domains} };

# each is of form
#             {
#               'f_let_expire' => 'N',
#               'name' => 'wolfecpa.com',
#               'f_auto_renew' => 'N',
#               'expiredate' => '2017-09-16 04:00:00'
#             },

foreach my $svc_domain ( $part_export->svc_x ) {
  unless ( exists($domains{$svc_domain->domain}) ) {
    warn $svc_domain->domain. " not at registrar.  No action taken.\n";
    next;
  }

  $domains{$svc_domain->domain}{seen} = 1;

  unless ( $domains{$svc_domain->domain}{expiredate} =~
           /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/ )
  {
    warn "Can't parse expiration date for ". $svc_domain->domain. " skipping\n";
    next;
  }

  my ($year,$month,$day,$hour,$minute,$second) = ($1,$2,$3,$4,$5,$6);
  my $exp = DateTime->new( year   => $year,
                           month  => $month,
                           day    => $day,
                           hour   => $hour,
                           minute => $minute,
                           second => $second,
                           time_zone => 'America/New_York',#timezone of opensrs
                         );
  #my $expiretime = $exp->epoch;
  
  #set the bill date early enough to allow a couple chances to pay
  $month--;
  if ($month < 1) {
    $year--;
    $month=12;
  }
  my $bill = DateTime->new( year   => $year,
                            month  => $month,
                            day    => 1,
                            hour   => 0,
                            minute => 0,
                            second => 0,
                            time_zone => 'America/Chicago',#timezone of customer
                          );
  my $expiretime = $bill->epoch;

  my $error = $part_export->is_supported_domain($svc_domain);
  warn $error if $error;
  $error = undef;

  my $create = '';
  my $whois = whois($svc_domain->domain);
  $whois =~ /Record created on (\d{1,2}-\w{3}-\d{4})\./ && ($create = $1);
  my $createtime = str2time($create);

  unless ($createtime) {
    $exp->subtract( 'years' => 1 );
    $createtime = $exp->epoch;
  }

  my $new;
  my $cust_svc = $svc_domain->cust_svc;
  my $cust_pkg = $cust_svc->cust_pkg;
  unless ($cust_pkg) {
    warn $svc_domain->domain. " not linked to package.  No action taken.\n";
    next;
  }

  foreach my $pkg ( grep { $_->pkgpart == $pkgpart } $cust_pkg->cust_main->ncancelled_pkgs ) {
    next if $pkg->cust_svc;  # only handles simple 1 domain/package case
    $cust_svc->pkgnum($pkg->pkgnum);
    $error = $cust_svc->replace;
    die "error linking to empty package: $error\n" if $error;
    $cust_pkg = $pkg;
    last;
  }

  unless ($cust_pkg->pkgpart == $pkgpart) {
    $new = new FS::cust_pkg
      { custnum => $cust_pkg->custnum, pkgpart => $pkgpart };
    my $error = $new->insert;
    die "error inserting package: $error\n" if $error;
    $cust_svc->pkgnum($new->pkgnum);
    $error = $cust_svc->replace;
    die "error linking to new package: $error\n" if $error;
    $cust_pkg = $new;
  }

  # set dates on package if it was empty?
  $cust_pkg->bill($expiretime);
  $cust_pkg->setup($createtime);
  $error = $cust_pkg->replace;
  die $error if $error;
}