Merge branch 'github/pr/55_reprise'
[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   my $h_cust_pkg =
39     qsearchs({ table       => 'h_cust_pkg',
40                hashref     => { 
41                                 pkgnum         => $cust_pkg->pkgnum,
42                                 setup          => { op=>'!=', value=>'' },
43                                 ($opt_u ? ('susp' => { op=>'!=', value=>'' })
44                                         : ()
45                                 ),
46                                 ($opt_c ? ('cancel' => { op=>'!=', value=>'' })
47                                         : ()
48                                 ),
49                               },
50                extra_sql   => " AND history_action IN ('insert','replace_old')".
51                               ' AND history_date >= ? AND history_date <= ? ',
52                extra_param => [ [$sdate,'int'], [$edate,'int'] ],
53                order_by    => 'ORDER BY history_date DESC LIMIT 1',
54             })
55     or next;
56
57   $changed++;
58
59   #if ( $opt_r ) {
60     #print "restoring setup for pkgnum ". $cust_pkg->pkgnum.
61     #      " (custnum ". $cust_pkg->custnum.
62     #      ") to ". time2str('%D', $h_cust_pkg->setup). "\n";
63     print $cust_pkg->pkgnum. ','.
64           time2str('%D', $h_cust_pkg->setup). ','.
65           $cust_pkg->custnum. ','.
66           '"'. $cust_pkg->cust_main->name. '"'. "\n";
67   #}
68
69   #don't actually do it yet ...
70   #$cust_pkg->set('setup', $h_cust_pkg->setup);
71   #my $error = $cust_pkg->replace;
72   ##die $error if $error;
73   #warn "error changing pkgnum ". $cust_pkg->pkgnum.': '. $error."\n";
74
75 }
76
77 if ( $opt_r ) {
78   $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
79 } else {
80   $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
81 }
82
83 print "changed $changed packages\n";
84
85 sub usage {
86   die "usage: cust_pkg-restore_setup [ -s history_start ] [ -e history_end ] [-r ]  employee_username\n";
87 }
88
89 =head1 NAME
90
91 cust_pkg-restore_setup
92
93 =head1 SYNOPSIS
94
95   cust_pkg-restore_setup -d date -u history_username [ -r ] employee_username
96
97 =head1 DESCRIPTION
98
99 Command-line tool to restore removed setup dates.
100
101 -s: Start date of time period to restore changes from
102
103 -e: End date of time period to restore changes from
104
105 -u: sUspended packages only
106
107 -c: Cancelled packages only
108
109 -r: dRy run
110
111 employee_username
112
113 =head1 BUGS
114
115 =head1 SEE ALSO
116
117 L<FS::part_pkg>
118
119 =cut
120
121 1;
122