RT# 82137 - default payment amount now has processing fee in total if processing...
[freeside.git] / bin / opensrs_domain_pkgs
1 #!/usr/bin/perl -w
2
3 use strict;
4 use DateTime;
5 use Date::Format;
6 use Date::Parse;
7 use Net::OpenSRS;
8 use Net::Whois::Raw;
9 use Data::Dumper;
10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearchs qsearch);
12 use FS::Conf;
13 use FS::svc_domain;
14 use FS::part_pkg;
15 use FS::part_export;
16
17 my $exportnum = 1;
18 my $pkgpart = 631;
19 my $user = 'qis';
20
21 adminsuidsetup $user;
22
23 my $part_export = qsearchs('part_export' => { exportnum => $exportnum })
24   or die "can't find export $exportnum\n";
25
26 my $srs = $part_export->get_srs;
27
28 my $rv = $srs->make_request(
29   {
30     action     => 'get_domains_by_expiredate',
31     object     => 'domain',
32     attributes => {
33       exp_from => time2str('%Y-%m-%d', time() - 4*24*60*60),
34       exp_to   => time2str('%Y-%m-%d', time() + 10*366*24*60*60),
35       limit    => 10000,
36     }
37   }
38 );
39
40 die $rv->{response_text} unless $rv->{is_success};
41
42 my %domains = map { $_->{name}, $_ } @{ $rv->{attributes}->{exp_domains} };
43
44 # each is of form
45 #             {
46 #               'f_let_expire' => 'N',
47 #               'name' => 'wolfecpa.com',
48 #               'f_auto_renew' => 'N',
49 #               'expiredate' => '2017-09-16 04:00:00'
50 #             },
51
52 foreach my $svc_domain ( $part_export->svc_x ) {
53   unless ( exists($domains{$svc_domain->domain}) ) {
54     warn $svc_domain->domain. " not at registrar.  No action taken.\n";
55     next;
56   }
57
58   $domains{$svc_domain->domain}{seen} = 1;
59
60   unless ( $domains{$svc_domain->domain}{expiredate} =~
61            /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/ )
62   {
63     warn "Can't parse expiration date for ". $svc_domain->domain. " skipping\n";
64     next;
65   }
66
67   my ($year,$month,$day,$hour,$minute,$second) = ($1,$2,$3,$4,$5,$6);
68   my $exp = DateTime->new( year   => $year,
69                            month  => $month,
70                            day    => $day,
71                            hour   => $hour,
72                            minute => $minute,
73                            second => $second,
74                            time_zone => 'America/New_York',#timezone of opensrs
75                          );
76   #my $expiretime = $exp->epoch;
77   
78   #set the bill date early enough to allow a couple chances to pay
79   $month--;
80   if ($month < 1) {
81     $year--;
82     $month=12;
83   }
84   my $bill = DateTime->new( year   => $year,
85                             month  => $month,
86                             day    => 1,
87                             hour   => 0,
88                             minute => 0,
89                             second => 0,
90                             time_zone => 'America/Chicago',#timezone of customer
91                           );
92   my $expiretime = $bill->epoch;
93
94   my $error = $part_export->is_supported_domain($svc_domain);
95   warn $error if $error;
96   $error = undef;
97
98   my $create = '';
99   my $whois = whois($svc_domain->domain);
100   $whois =~ /Record created on (\d{1,2}-\w{3}-\d{4})\./ && ($create = $1);
101   my $createtime = str2time($create);
102
103   unless ($createtime) {
104     $exp->subtract( 'years' => 1 );
105     $createtime = $exp->epoch;
106   }
107
108   my $new;
109   my $cust_svc = $svc_domain->cust_svc;
110   my $cust_pkg = $cust_svc->cust_pkg;
111   unless ($cust_pkg) {
112     warn $svc_domain->domain. " not linked to package.  No action taken.\n";
113     next;
114   }
115
116   foreach my $pkg ( grep { $_->pkgpart == $pkgpart } $cust_pkg->cust_main->ncancelled_pkgs ) {
117     next if $pkg->cust_svc;  # only handles simple 1 domain/package case
118     $cust_svc->pkgnum($pkg->pkgnum);
119     $error = $cust_svc->replace;
120     die "error linking to empty package: $error\n" if $error;
121     $cust_pkg = $pkg;
122     last;
123   }
124
125   unless ($cust_pkg->pkgpart == $pkgpart) {
126     $new = new FS::cust_pkg
127       { custnum => $cust_pkg->custnum, pkgpart => $pkgpart };
128     my $error = $new->insert;
129     die "error inserting package: $error\n" if $error;
130     $cust_svc->pkgnum($new->pkgnum);
131     $error = $cust_svc->replace;
132     die "error linking to new package: $error\n" if $error;
133     $cust_pkg = $new;
134   }
135
136   # set dates on package if it was empty?
137   $cust_pkg->bill($expiretime);
138   $cust_pkg->setup($createtime);
139   $error = $cust_pkg->replace;
140   die $error if $error;
141 }
142