don't redirect to a GET with sensitive data, RT#26099
[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 );
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:');
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
20 while (<STDIN>) {
21
22   unless ( /^\s*(\d+)\s*$/ ) { 
23     warn "unparsable line: $_";
24     next;
25   }
26   my $custnum = $1;
27
28   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
29   unless ( $cust_main ) {
30     warn "unknown custnum $custnum\n";
31     next;
32   }
33
34   my %cust_tag = ( custnum=>$custnum, tagnum=>$opt_t );
35   if ( $opt_t && ! qsearchs('cust_tag', \%cust_tag) ) {
36     my $cust_tag = new FS::cust_tag \%cust_tag;
37     my $error = $cust_tag->insert;
38     die "$error\n" if $error;
39   }
40
41   if ( $opt_p || $opt_a ) {
42     $cust_main->agentnum($opt_a) if $opt_a;
43     $cust_main->payby($opt_p)    if $opt_p;
44
45     my $error = $cust_main->replace;
46     die "$error\n" if $error;
47   }
48
49   if ( $opt_k ) {
50     foreach my $k (split(/\s*,\s*/, $opt_k)) {
51       my($old, $new) = split(/\s*:\s*/, $k);
52       foreach my $cust_pkg ( qsearch('cust_pkg', {
53                                        'custnum' => $cust_main->custnum,
54                                        'pkgpart' => $old,
55                                     })
56                            )
57       {
58         $cust_pkg->pkgpart($new);
59         my $error = $cust_pkg->replace;
60         die "$error\n" if $error;
61       }
62     }
63   }
64
65 }
66
67 sub usage {
68   die "usage: cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] employee_username <custnums.txt\n";
69 }
70
71 =head1 NAME
72
73 cust_main-bulk_change
74
75 =head1 SYNOPSIS
76
77   cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] username <custnums.txt
78
79 =head1 DESCRIPTION
80
81 Command-line tool to make bulk changes to a group of customers.
82
83 -a: new agentnum
84
85 -p: new payby, for example, I<CARD> or I<DCRD>
86
87 -t: tagnum to add if not present
88
89 -k: old_pkgpart:new_pkgpart, for example, I<5:4>.  Multiple entries can be comma-separated.
90
91 user: Employee username
92
93 =head1 BUGS
94
95 =head1 SEE ALSO
96
97 L<FS::payinfo_Mixin>, L<FS::cust_main>, L<FS::payby>
98
99 =cut
100
101 1;