summaryrefslogtreecommitdiff
path: root/bin/reset-cust_credit-otaker
blob: 93002d05a63e4004213d2a2df114b45812be0418 (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
#!/usr/bin/perl -w

use strict;
use vars qw($opt_d);
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::cust_credit;
use FS::h_cust_credit;

getopts('d:');

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

die &usage
  unless ($opt_d);

$FS::Record::nowarn_identical = 1;

if ( $opt_d ) {
  $opt_d =~ /^(\d+)$/ or die "invalid date";
} else {
  die "no date specified\n";
}

my @cust_credit = qsearch('cust_credit', { otaker => $user } );
die "no credits found\n" unless @cust_credit;

my $cust_credit = new FS::cust_credit;
my @fields = grep { $_ !~ /^otaker|reason|reasonnum$/ } $cust_credit->fields;

foreach my $cust_credit ( @cust_credit ) {
  my %hash = $cust_credit->hash;
  foreach (qw(otaker reason reasonnum)) {
    delete $hash{$_};
  }
  $hash{'history_action'} = 'replace_old';
  my $h_cust_credit =
    qsearchs({ 'table'     => 'h_cust_credit',
               'hashref'   => \%hash,
               'select'    => '*',
               'extra_sql' =>  " AND history_date <= $opt_d",
               'order_by'  =>  'ORDER BY history_date DESC LIMIT 1',
            });
  if ($h_cust_credit) {
    $cust_credit->otaker($h_cust_credit->otaker);
    my $reason = $h_cust_credit->getfield('reason');
    if ($reason =~ /^\s*$/) {
      $reason = '(none)';
    }
    $cust_credit->otaker($h_cust_credit->otaker);
    $cust_credit->reason($reason);
    my $error = $cust_credit->replace
      if $cust_credit->modified;     
    die "error replacing cust_credit: $error\n"
      if $error;
  }else{
    warn "Skipping credit.crednum ". $cust_credit->crednum;
  }
}

sub usage {
  die "Usage:\n\n  reset-cust_credit-otaker -d epoch_date user\n";
}

=head1 NAME

reset-cust_credit-otaker - Command line tool to reset the otaker column for cust_credits to a previous value 

=head1 SYNOPSIS

  reset-cust_credit-otaker -d epoch_date user

=head1 DESCRIPTION

  Sets the otaker column of the cust_credit records specified by user and
  datespec to the value just prior to datespec.  

  The reasonnum of the cust_credit record is also set to reason record
  which matches the reason specified in the history.

=head1 SEE ALSO

L<FS::cust_credit>, L<FS::h_cust_credit>;

=cut