summaryrefslogtreecommitdiff
path: root/bin/cust_main-bulk_change
blob: 47a9ae30a1ab14ff36f15d39e817f641b7b9bb6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl

use strict;
use vars qw( $opt_a $opt_p $opt_t $opt_k $opt_c $opt_v $opt_E );
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::cust_main;
use FS::cust_main::Search;
use FS::cust_tag;
use FS::cust_pkg;

getopts('a:p:t:k:c:vE');

my $user = shift or &usage;
adminsuidsetup $user;

$FS::cust_main::skip_fuzzyfiles = 1;
$FS::cust_main::skip_fuzzyfiles = 1;
$FS::cust_main::import = 1;
$FS::cust_main::import = 1;

while (<STDIN>) {

  my ($custnum,$cust_main);
  if ($opt_E) {
    # don't really need to validate input here, search will quote safely
    unless ( /^\s*(.*)\s*$/ ) { 
      warn "unparsable line: $_";
      next;
    }
    my @cust_main = FS::cust_main::Search::email_search( 'email' => $1 );
    if (@cust_main > 1) {
      warn "muliple matching customers for address, skipping: $_";
      next;
    } elsif (@cust_main < 1) {
      warn "could not find matching customer for address, skipping: $_";
      next;
    }
    $cust_main = $cust_main[0];
    $custnum = $cust_main->custnum;
  } else {
    unless ( /^\s*(\d+)\s*$/ ) { 
      warn "unparsable line: $_";
      next;
    }
    my $custnum = $1;
    $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
    unless ( $cust_main ) {
      warn "unknown custnum $custnum\n";
      next;
    }
  }

  if ( $opt_v ) {
    print $cust_main->custnum . ": " . $cust_main->name . "\n";
  }

  my %cust_tag = ( custnum=>$custnum, tagnum=>$opt_t );
  if ( $opt_t && ! qsearchs('cust_tag', \%cust_tag) ) {
    my $cust_tag = new FS::cust_tag \%cust_tag;
    my $error = $cust_tag->insert;
    die "$error\n" if $error;
  }

  if ( $opt_p || $opt_a ) {
    $cust_main->agentnum($opt_a) if $opt_a;
    $cust_main->payby($opt_p)    if $opt_p;

    my $error = $cust_main->replace;
    die "$error\n" if $error;
  }

  if ( $opt_k ) {
    foreach my $k (split(/\s*,\s*/, $opt_k)) {
      my($old, $new) = split(/\s*:\s*/, $k);
      foreach my $cust_pkg ( qsearch('cust_pkg', {
                                       'custnum' => $cust_main->custnum,
                                       'pkgpart' => $old,
                                    })
                           )
      {
        $cust_pkg->pkgpart($new);
        my $error = $cust_pkg->replace;
        die "$error\n" if $error;
      }
    }
  }

  if ( $opt_c ) {
    my @error = $cust_main->cancel( 'reason' => $opt_c );
    die join(' / ', @error). "\n" if @error;
  }

}

sub usage {
  die "usage: cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] [-v] [-E] employee_username <custnums.txt\n";
}

=head1 NAME

cust_main-bulk_change

=head1 SYNOPSIS

  cust_main-bulk_change [ -a agentnum ] [ -p NEW_PAYBY ] [ -t tagnum ] [ -k old_pkgpart:new_pkgpart,... ] [ -c reasonnum ] username <custnums.txt

=head1 DESCRIPTION

Command-line tool to make bulk changes to a group of customers.

-a: new agentnum

-p: new payby, for example, I<CARD> or I<DCRD>

-t: tagnum to add if not present

-k: old_pkgpart:new_pkgpart, for example, I<5:4>.  Multiple entries can be comma-separated.

-c: Cancel customer

-v: verbose

-E: input is customer emails, rather than customer numbers

user: Employee username

=head1 BUGS

=head1 SEE ALSO

L<FS::payinfo_Mixin>, L<FS::cust_main>, L<FS::payby>

=cut

1;