add cancel option, RT#38145
[freeside.git] / bin / cust_main-bulk_change
1 #!/usr/bin/perl
2
3 use strict;
4 use vars qw( $opt_a $opt_p $opt_t $opt_k $opt_c );
5 use Getopt::Std;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch qsearchs);
8 use FS::cust_main;
9 use FS::cust_tag;
10 use FS::cust_pkg;
11
12 getopts('a:p:t:k:c:');
13
14 my $user = shift or &usage;
15 adminsuidsetup $user;
16
17 $FS::cust_main::skip_fuzzyfiles = 1;
18 $FS::cust_main::skip_fuzzyfiles = 1;
19 $FS::cust_main::import = 1;
20 $FS::cust_main::import = 1;
21
22 while (<STDIN>) {
23
24   unless ( /^\s*(\d+)\s*$/ ) { 
25     warn "unparsable line: $_";
26     next;
27   }
28   my $custnum = $1;
29
30   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
31   unless ( $cust_main ) {
32     warn "unknown custnum $custnum\n";
33     next;
34   }
35
36   my %cust_tag = ( custnum=>$custnum, tagnum=>$opt_t );
37   if ( $opt_t && ! qsearchs('cust_tag', \%cust_tag) ) {
38     my $cust_tag = new FS::cust_tag \%cust_tag;
39     my $error = $cust_tag->insert;
40     die "$error\n" if $error;
41   }
42
43   if ( $opt_p || $opt_a ) {
44     $cust_main->agentnum($opt_a) if $opt_a;
45     $cust_main->payby($opt_p)    if $opt_p;
46
47     my $error = $cust_main->replace;
48     die "$error\n" if $error;
49   }
50
51   if ( $opt_k ) {
52     foreach my $k (split(/\s*,\s*/, $opt_k)) {
53       my($old, $new) = split(/\s*:\s*/, $k);
54       foreach my $cust_pkg ( qsearch('cust_pkg', {
55                                        'custnum' => $cust_main->custnum,
56                                        'pkgpart' => $old,
57                                     })
58                            )
59       {
60         $cust_pkg->pkgpart($new);
61         my $error = $cust_pkg->replace;
62         die "$error\n" if $error;
63       }
64     }
65   }
66
67   if ( $opt_c ) {
68     my @error = $cust_main->cancel( 'reason' => $opt_c );
69     die join(' / ', @error). "\n" if @error;
70   }
71
72 }
73
74 sub usage {
75   die "usage: cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] employee_username <custnums.txt\n";
76 }
77
78 =head1 NAME
79
80 cust_main-bulk_change
81
82 =head1 SYNOPSIS
83
84   cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] [ -c reasonnum ] username <custnums.txt
85
86 =head1 DESCRIPTION
87
88 Command-line tool to make bulk changes to a group of customers.
89
90 -a: new agentnum
91
92 -p: new payby, for example, I<CARD> or I<DCRD>
93
94 -t: tagnum to add if not present
95
96 -k: old_pkgpart:new_pkgpart, for example, I<5:4>.  Multiple entries can be comma-separated.
97
98 -c: Cancel customer
99
100 user: Employee username
101
102 =head1 BUGS
103
104 =head1 SEE ALSO
105
106 L<FS::payinfo_Mixin>, L<FS::cust_main>, L<FS::payby>
107
108 =cut
109
110 1;