summaryrefslogtreecommitdiff
path: root/bin/cust_pkg-revert
blob: 1a4d456f51d3b200158a168ce08992f27e688875 (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
#!/usr/bin/perl

use strict;
use vars qw( $opt_d $opt_u $opt_r );
use Getopt::Std;
use Date::Parse qw(str2time);
use FS::UID qw(adminsuidsetup dbh);
use FS::Record qw(qsearch qsearchs);
use FS::cust_pkg;
use FS::h_cust_pkg;

getopts('d:u:r');

my $user = shift or &usage;
adminsuidsetup $user;

my $sdate = str2time($opt_d);
my $edate = $sdate + 86399;

my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;

my $fuzz = 1;

my $changed = 0;

foreach my $h_cust_pkg (
  qsearch({ table       => 'h_cust_pkg',
            hashref     => { history_user   => $opt_u,
                             history_action => 'replace_new',
                           },
            extra_sql   => ' AND history_date >= ? AND history_date <= ? ',
            extra_param => [ [$sdate,'int'], [$edate,'int'] ],
            #order_by    => 'ORDER BY history_date asc',
         })
) {
  my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $h_cust_pkg->pkgnum } );
  next if $cust_pkg->get('cancel');

  my($s, $e) = ($h_cust_pkg->history_date-$fuzz, $h_cust_pkg->history_date+$fuzz);

  my $old = qsearchs({
            table       => 'h_cust_pkg',
            hashref     => { history_user   => $opt_u,
                             history_action => 'replace_old',
                             pkgnum         => $h_cust_pkg->pkgnum,
                           },
            extra_sql   => ' AND history_date >= ? AND history_date <= ? ',
            extra_param => [ [$s,'int'], [$e,'int'] ],
  });

  my $diff = $h_cust_pkg->get('bill') - $old->get('bill');
  if ( $diff < 0 ) {
    warn "next bill date was decremented (not incremented) for pkgnum ". $cust_pkg->pkgnum. "; skipping\n";
    next;
  } elsif ( $diff == 0 ) {
    warn "next bill date was not changed for pkgnum ". $cust_pkg->pkgnum. "; skipping\n";
    next;
  }

  $changed++;

  #if ( $opt_r ) {
    my $days = ($diff / 86400);
    print "decrementing next bill for pkgnum ". $cust_pkg->pkgnum.
          " (custnum ". $cust_pkg->custnum. ") by $days days\n";
  #}

  $cust_pkg->set('bill', $cust_pkg->get('bill') - $diff );
  my $error = $cust_pkg->replace;
  die $error if $error;

}

if ( $opt_r ) {
  $dbh->rollback or die $dbh->errstr; #if $oldAutoCommit;
} else {
  $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
}

print "changed $changed packages\n";

sub usage {
  die "usage: cust_pkg-revert -d date -u history_username [ -r ] employee_username\n";
}

=head1 NAME

cust_pkg-revert

=head1 SYNOPSIS

  cust_pkg-revert -d date -u history_username [ -r ] employee_username

=head1 DESCRIPTION

Command-line tool to revert customer package changes from a specific day and user.
Currently only incrementing the next bill date (cust_pkg.bill) is reverted.

-d: Date of the changes to revert

-u: Username of the changes to revert

-r: dRy run

employee_username

=head1 BUGS

=head1 SEE ALSO

L<FS::part_pkg>

=cut

1;