default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / bin / part_pkg-bulk_change
1 #!/usr/bin/perl
2
3 use strict;
4 use vars qw( $opt_r $opt_m $opt_C $opt_p $opt_o $opt_v $opt_t $opt_s $opt_S $opt_z $opt_Z $opt_e $opt_u );
5 use Getopt::Std;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch qsearchs);
8 use FS::part_pkg;
9 use FS::part_pkg_option;
10
11 getopts('rm:C:p:o:v:t:sSzZe:u:');
12
13 my $user = shift or &usage;
14 adminsuidsetup $user;
15
16 my %search = ();
17 $search{'plan'} = $opt_p if $opt_p;
18 $search{'comment'} = $opt_m if $opt_m;
19
20 my $extra_sql = '';
21 $extra_sql = ( keys(%search) ? 'AND' : 'WHERE' ).
22              " classnum NOT IN ($opt_C)"
23   if $opt_C;
24
25 foreach my $part_pkg ( qsearch({ 'table'     => 'part_pkg',
26                                  'hashref'   => \%search,
27                                  'extra_sql' => $extra_sql,
28                               })
29                      )
30 {
31   next if ! $part_pkg->freq && $opt_r;
32
33   if ( $opt_o ) {
34
35     my %hash = (
36       'pkgpart'    => $part_pkg->pkgpart,
37       'optionname' => $opt_o,
38     );
39
40     my $part_pkg_option = qsearchs('part_pkg_option', \%hash);
41
42     unless ( defined $opt_v ) {
43       my $error = $part_pkg_option && $part_pkg_option->delete;
44       die $error if $error;
45       next;
46     }
47
48     if ( $part_pkg_option ) {
49       next if $part_pkg_option->optionvalue eq $opt_v;
50       $part_pkg_option->optionvalue($opt_v);
51       my $error = $part_pkg_option->replace;
52       die $error if $error;
53     } else {
54       $part_pkg_option = new FS::part_pkg_option { %hash,
55                                                    'optionvalue'=>$opt_v,
56                                                  };
57       my $error = $part_pkg_option->insert;
58       die $error if $error;
59     }
60
61   }
62
63   if ( $opt_e ) {
64     my %hash = (
65       'pkgpart'    => $part_pkg->pkgpart,
66       'optionname' => 'setup_fee',
67     );
68
69     my $part_pkg_option = qsearchs('part_pkg_option', \%hash);
70
71     if ( $part_pkg_option ) {
72       $part_pkg_option->optionvalue(
73         sprintf('%.2f', $part_pkg_option->optionvalue * $opt_e)
74       );
75       my $error = $part_pkg_option->replace;
76       die $error if $error;
77     }
78   }
79
80   if ( $opt_u ) {
81     my %hash = (
82       'pkgpart'    => $part_pkg->pkgpart,
83       'optionname' => 'recur_fee',
84     );
85
86     my $part_pkg_option = qsearchs('part_pkg_option', \%hash);
87
88     if ( $part_pkg_option ) {
89       $part_pkg_option->optionvalue(
90         sprintf('%.2f', $part_pkg_option->optionvalue * $opt_u)
91       );
92       my $error = $part_pkg_option->replace;
93       die $error if $error;
94     }
95   }
96
97   if ( $opt_t || $opt_s || $opt_S || $opt_z || $opt_Z ) {
98
99     $part_pkg->taxclass($opt_t) if $opt_t;
100     $part_pkg->setup_show_zero('') if $opt_s;
101     $part_pkg->setup_show_zero('Y') if $opt_S;
102     $part_pkg->recur_show_zero('') if $opt_z;
103     $part_pkg->recur_show_zero('Y') if $opt_Z;
104
105     my $error = $part_pkg->replace;
106
107   }
108
109 }
110
111 sub usage {
112   die "usage: part_pkg-bulk_change [ -r ] [ -p plan ] [ -m comment ] [ -C classnum,classnum ] [ -o option_name -v option_value ] [ -t new_taxclass ] [ -s | -S ] [ -z | -Z ]  [ -e multiplier ] [ -u multiplier ] employee_username\n";
113 }
114
115 =head1 NAME
116
117 cust_main-bulk_change
118
119 =head1 SYNOPSIS
120
121   part_pkg-bulk_change [ -r ] [ -p plan ] [ -m comment ] [ -C classnum,classnum ] [ -o option_name -v option_value ] [ -t new_taxclass ] [ -s | -S ] [ -z | -Z ] [ -e multiplier ] [ -u multiplier ] employee_username
122
123 =head1 DESCRIPTION
124
125 Command-line tool to change a set of package definitions.
126
127 Search options:
128
129 -r: recurring package definitions only
130
131 -p: packages with this price plan only
132
133 -m: packages with this comment only
134
135 -C: excluding package classnum or classnums (comma-separated list)
136
137 Change options:
138
139 -o: part_pkg_option optionname (use without -v to unset)
140
141 -v: part_pkg_option optionvalue
142
143 -t: new taxclass
144
145 -s: Turn off "Show zero setup"
146
147 -S: Turn on "Show zero setup"
148
149 -z: Turn off "Show zero recurring"
150
151 -Z: Turn on "Show zero recurring"
152
153 -e: Multiply setup fee by this value (i.e. 1.05 for a 5% price increase)
154
155 -u: Multiply recurring fee by this value (i.e. 1.05 for a 5% price increase)
156
157 employee_username
158
159 =head1 BUGS
160
161 =head1 SEE ALSO
162
163 L<FS::part_pkg>
164
165 =cut
166
167 1;
168