RT#18834 Cacti integration [phase one, simple but stable]
[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 $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 }
68
69 sub usage {
70   die "usage: cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] employee_username <custnums.txt\n";
71 }
72
73 =head1 NAME
74
75 cust_main-bulk_change
76
77 =head1 SYNOPSIS
78
79   cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] username <custnums.txt
80
81 =head1 DESCRIPTION
82
83 Command-line tool to make bulk changes to a group of customers.
84
85 -a: new agentnum
86
87 -p: new payby, for example, I<CARD> or I<DCRD>
88
89 -t: tagnum to add if not present
90
91 -k: old_pkgpart:new_pkgpart, for example, I<5:4>.  Multiple entries can be comma-separated.
92
93 user: Employee username
94
95 =head1 BUGS
96
97 =head1 SEE ALSO
98
99 L<FS::payinfo_Mixin>, L<FS::cust_main>, L<FS::payby>
100
101 =cut
102
103 1;