contact search, RT#25687 (also possibly #25583 and #22991)
[freeside.git] / bin / cust_pkg-restore_setup
1 #!/usr/bin/perl
2
3 use strict;
4 use vars qw( $opt_s $opt_e $opt_r $opt_u $opt_c );
5 use Getopt::Std;
6 use Date::Parse qw(str2time);
7 use Date::Format;
8 use FS::UID qw(adminsuidsetup dbh);
9 use FS::Record qw(qsearch qsearchs);
10 use FS::cust_pkg;
11 use FS::h_cust_pkg;
12
13 getopts('s:e:ruc');
14
15 my $user = shift or &usage;
16 adminsuidsetup $user;
17
18 my $sdate = $opt_s ? str2time($opt_s) : 0;
19 my $edate = $opt_e ? str2time($opt_e) + 86399 #not strictly correct on time zone boundary days, but good enough for this purpose
20                    : 2147397248;
21
22 my $oldAutoCommit = $FS::UID::AutoCommit;
23 local $FS::UID::AutoCommit = 0;
24 my $dbh = dbh;
25
26 my $fuzz = 2;
27
28 my $changed = 0;
29
30 foreach my $cust_pkg (
31   qsearch({ table       => 'cust_pkg',
32             hashref     => { 
33                              setup          => '',
34                            },
35          })
36 ) {
37
38   #XXX only canceled packages?
39   #XXX only suspended packages?
40
41   my $h_cust_pkg =
42     qsearchs({ table       => 'h_cust_pkg',
43                hashref     => { 
44                                 pkgnum         => $cust_pkg->pkgnum,
45                                 history_action => 'replace_old',
46                                 setup          => { op=>'!=', value=>'' },
47                                 ($opt_u ? ('susp' => { op=>'!=', value=>'' })
48                                         : ()
49                                 ),
50                                 ($opt_c ? ('cancel' => { op=>'!=', value=>'' })
51                                         : ()
52                                 ),
53                               },
54                extra_sql   => ' AND history_date >= ? AND history_date <= ? ',
55                extra_param => [ [$sdate,'int'], [$edate,'int'] ],
56                order_by    => 'ORDER BY history_date DESC LIMIT 1',
57             })
58     or next;
59
60   $changed++;
61
62   #if ( $opt_r ) {
63     print "restoring setup for pkgnum ". $cust_pkg->pkgnum.
64           " (custnum ". $cust_pkg->custnum.
65           ") to ". time2str('%D', $h_cust_pkg->setup). "\n";
66   #}
67
68   $cust_pkg->set('setup', $h_cust_pkg->setup);
69   my $error = $cust_pkg->replace;
70   die $error if $error;
71
72 }
73
74 if ( $opt_r ) {
75   $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
76 } else {
77   $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
78 }
79
80 print "changed $changed packages\n";
81
82 sub usage {
83   die "usage: cust_pkg-restore_setup [ -s history_start ] [ -e history_end ] [-r ]  employee_username\n";
84 }
85
86 =head1 NAME
87
88 cust_pkg-restore_setup
89
90 =head1 SYNOPSIS
91
92   cust_pkg-restore_setup -d date -u history_username [ -r ] employee_username
93
94 =head1 DESCRIPTION
95
96 Command-line tool to restore removed setup dates.
97
98 -s: Start date of time period to restore changes from
99
100 -e: End date of time period to restore changes from
101
102 -u: sUspended packages only
103
104 -c: Cancelled packages only
105
106 -r: dRy run
107
108 employee_username
109
110 =head1 BUGS
111
112 =head1 SEE ALSO
113
114 L<FS::part_pkg>
115
116 =cut
117
118 1;
119