9 use FS::Record qw(qsearch qsearchs dbh);
11 getopts('cpiXr:t:u:vk:');
14 $FS::UID::AutoCommit = 0;
17 "Usage: bulk_void -s start -e end
26 -s, -e: date range (required)
27 -r: void reason text (required)
28 -c, -p, -i: void credits, payments, invoices
29 -u: specifies a filename of customer numbers - only void for those customers
30 -k: skip invoices with only this pkgpart
31 -t: only void payments with this payby
32 -v: verbose - show more detail
37 if (!$opt{start} or !$opt{end} or !$opt{r}) {
41 print "DRY RUN--changes will not be committed.\n" unless $opt{X};
44 $search{payby} = $opt{t} if $opt{t} && $opt{p};
46 my $extra_sql = (keys %search ? ' AND ' : ' WHERE ').
47 " _date >= $opt{start} AND _date <= $opt{end}";
50 my @custnums = map { chomp; $_; } read_file($opt{u});
51 $extra_sql .= ' AND custnum IN ('. join(',', @custnums). ') ';
63 foreach my $k (keys %tables) {
65 my $table = $tables{$k};
71 my $cursor = FS::Cursor->new({
74 extra_sql => $extra_sql,
77 while (my $record = $cursor->fetch) {
79 if ( $opt{k} && $opt{i} ) {
80 my @other_pkgs = grep { $_->pkgpart != $opt{k} }
81 grep $_, map $_->cust_pkg, $record->cust_bill_pkg;
82 next if ! @other_pkgs;
86 $pkey ||= $record->primary_key;
87 my $num = $record->get($pkey);
88 my $date = time2str('%x', $record->_date);
89 my $name = $record->cust_main->name;
90 warn "Voiding $table #$num ($date) for $name\n";
93 $error = $record->void($reason);
95 $error = "$table #" . $record->get($record->primary_key) . ": $error";
106 print " $table voided: $done_count\n errors: $error_count\n";
111 print "Committed changes.\n";
114 print "Reverted changes.\n";