svc_hardware: better error messages for bad hw_addr when not validating as a MAC...
[freeside.git] / bin / cust_pkg-revert
1 #!/usr/bin/perl
2
3 use strict;
4 use vars qw( $opt_d $opt_u $opt_r );
5 use Getopt::Std;
6 use Date::Parse qw(str2time);
7 use FS::UID qw(adminsuidsetup dbh);
8 use FS::Record qw(qsearch qsearchs);
9 use FS::cust_pkg;
10 use FS::h_cust_pkg;
11
12 getopts('d:u:r');
13
14 my $user = shift or &usage;
15 adminsuidsetup $user;
16
17 my $sdate = str2time($opt_d);
18 my $edate = $sdate + 86399;
19
20 my $oldAutoCommit = $FS::UID::AutoCommit;
21 local $FS::UID::AutoCommit = 0;
22 my $dbh = dbh;
23
24 my $fuzz = 1;
25
26 my $changed = 0;
27
28 foreach my $h_cust_pkg (
29   qsearch({ table       => 'h_cust_pkg',
30             hashref     => { history_user   => $opt_u,
31                              history_action => 'replace_new',
32                            },
33             extra_sql   => ' AND history_date >= ? AND history_date <= ? ',
34             extra_param => [ [$sdate,'int'], [$edate,'int'] ],
35             #order_by    => 'ORDER BY history_date asc',
36          })
37 ) {
38   my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $h_cust_pkg->pkgnum } );
39   next if $cust_pkg->get('cancel');
40
41   my($s, $e) = ($h_cust_pkg->history_date-$fuzz, $h_cust_pkg->history_date+$fuzz);
42
43   my $old = qsearchs({
44             table       => 'h_cust_pkg',
45             hashref     => { history_user   => $opt_u,
46                              history_action => 'replace_old',
47                              pkgnum         => $h_cust_pkg->pkgnum,
48                            },
49             extra_sql   => ' AND history_date >= ? AND history_date <= ? ',
50             extra_param => [ [$s,'int'], [$e,'int'] ],
51   });
52
53   my $diff = $h_cust_pkg->get('bill') - $old->get('bill');
54   if ( $diff < 0 ) {
55     warn "next bill date was decremented (not incremented) for pkgnum ". $cust_pkg->pkgnum. "; skipping\n";
56     next;
57   } elsif ( $diff == 0 ) {
58     warn "next bill date was not changed for pkgnum ". $cust_pkg->pkgnum. "; skipping\n";
59     next;
60   }
61
62   $changed++;
63
64   #if ( $opt_r ) {
65     my $days = ($diff / 86400);
66     print "decrementing next bill for pkgnum ". $cust_pkg->pkgnum.
67           " (custnum ". $cust_pkg->custnum. ") by $days days\n";
68   #}
69
70   $cust_pkg->set('bill', $cust_pkg->get('bill') - $diff );
71   my $error = $cust_pkg->replace;
72   die $error if $error;
73
74 }
75
76 if ( $opt_r ) {
77   $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
78 } else {
79   $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
80 }
81
82 print "changed $changed packages\n";
83
84 sub usage {
85   die "usage: cust_pkg-revert -d date -u history_username [ -r ] employee_username\n";
86 }
87
88 =head1 NAME
89
90 cust_pkg-revert
91
92 =head1 SYNOPSIS
93
94   cust_pkg-revert -d date -u history_username [ -r ] employee_username
95
96 =head1 DESCRIPTION
97
98 Command-line tool to revert customer package changes from a specific day and user.
99 Currently only incrementing the next bill date (cust_pkg.bill) is reverted.
100
101 -d: Date of the changes to revert
102
103 -u: Username of the changes to revert
104
105 -r: dRy run
106
107 employee_username
108
109 =head1 BUGS
110
111 =head1 SEE ALSO
112
113 L<FS::part_pkg>
114
115 =cut
116
117 1;
118