Merge branch 'patch-20' of https://github.com/gjones2/Freeside
[freeside.git] / bin / cust_main-bulk_change
1 #!/usr/bin/perl
2
3 use strict;
4 use vars qw( $opt_p $opt_t );
5 use Getopt::Std;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearchs);
8 use FS::cust_main;
9 use FS::cust_tag;
10
11 getopts('p:t:');
12
13 my $user = shift or &usage;
14 adminsuidsetup $user;
15
16 $FS::cust_main::skip_fuzzyfiles = 1;
17 $FS::cust_main::skip_fuzzyfiles = 1;
18 $FS::cust_main::import = 1;
19 $FS::cust_main::import = 1;
20
21 while (<STDIN>) {
22
23   unless ( /^\s*(\d+)\s*$/ ) { 
24     warn "unparsable line: $_";
25     next;
26   }
27   my $custnum = $1;
28
29   my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
30   unless ( $cust_main ) {
31     warn "unknown custnum $custnum\n";
32     next;
33   }
34
35   my %cust_tag = ( custnum=>$custnum, tagnum=>$opt_t );
36   if ( $opt_t && ! qsearchs('cust_tag', \%cust_tag) ) {
37     my $cust_tag = new FS::cust_tag \%cust_tag;
38     my $error = $cust_tag->insert;
39     die "$error\n" if $error;
40   }
41
42   if ( $opt_p ) {
43     $cust_main->payby($opt_p);
44
45     my $error = $cust_main->replace;
46     die "$error\n" if $error;
47   }
48
49 }
50
51 sub usage {
52   die "usage: cust_main-bulk_change [ -p NEW_PAYBY ] [ -t tagnum ] employee_username <custnums.txt\n";
53 }
54
55 =head1 NAME
56
57 cust_main-bulk_change
58
59 =head1 SYNOPSIS
60
61   cust_main-bulk_change [ -p NEW_PAYBY ] [ -t tagnum ] username <custnums.txt
62
63 =head1 DESCRIPTION
64
65 Command-line tool to make bulk changes to a group of customers.
66
67 -p: new payby, for example, I<CARD> or I<DCRD>
68
69 -t: tagnum to add if not present
70
71 user: Employee username
72
73 =head1 BUGS
74
75 =head1 SEE ALSO
76
77 L<FS::payinfo_Mixin>, L<FS::cust_main>, L<FS::payby>
78
79 =cut
80
81 1;