176965baf26c829de1076b1288fc332a0e8b3d2b
[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 $search{'classnum'} = { op=>'NOT IN', value=>"($opt_C)" } if $opt_C;
20
21 foreach my $part_pkg ( qsearch('part_pkg',\%search) ) {
22   next if ! $part_pkg->freq && $opt_r;
23
24   if ( $opt_o ) {
25
26     my %hash = (
27       'pkgpart'    => $part_pkg->pkgpart,
28       'optionname' => $opt_o,
29     );
30
31     my $part_pkg_option = qsearchs('part_pkg_option', \%hash);
32
33     if ( $part_pkg_option ) {
34       next if $part_pkg_option->optionvalue eq $opt_v;
35       $part_pkg_option->optionvalue($opt_v);
36       my $error = $part_pkg_option->replace;
37       die $error if $error;
38     } else {
39       $part_pkg_option = new FS::part_pkg_option { %hash,
40                                                    'optionvalue'=>$opt_v,
41                                                  };
42       my $error = $part_pkg_option->insert;
43       die $error if $error;
44     }
45
46   }
47
48   if ( $opt_e ) {
49     my %hash = (
50       'pkgpart'    => $part_pkg->pkgpart,
51       'optionname' => 'setup_fee',
52     );
53
54     my $part_pkg_option = qsearchs('part_pkg_option', \%hash);
55
56     if ( $part_pkg_option ) {
57       $part_pkg_option->optionvalue(
58         sprintf('%.2f', $part_pkg_option->optionvalue * $opt_e)
59       );
60       my $error = $part_pkg_option->replace;
61       die $error if $error;
62     }
63   }
64
65   if ( $opt_u ) {
66     my %hash = (
67       'pkgpart'    => $part_pkg->pkgpart,
68       'optionname' => 'recur_fee',
69     );
70
71     my $part_pkg_option = qsearchs('part_pkg_option', \%hash);
72
73     if ( $part_pkg_option ) {
74       $part_pkg_option->optionvalue(
75         sprintf('%.2f', $part_pkg_option->optionvalue * $opt_u)
76       );
77       my $error = $part_pkg_option->replace;
78       die $error if $error;
79     }
80   }
81
82   if ( $opt_t || $opt_s || $opt_S || $opt_z || $opt_Z ) {
83
84     $part_pkg->taxclass($opt_t) if $opt_t;
85     $part_pkg->setup_show_zero('') if $opt_s;
86     $part_pkg->setup_show_zero('Y') if $opt_S;
87     $part_pkg->recur_show_zero('') if $opt_z;
88     $part_pkg->recur_show_zero('Y') if $opt_Z;
89
90     my $error = $part_pkg->replace;
91
92   }
93
94 }
95
96 sub usage {
97   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";
98 }
99
100 =head1 NAME
101
102 cust_main-bulk_change
103
104 =head1 SYNOPSIS
105
106   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
107
108 =head1 DESCRIPTION
109
110 Command-line tool to change a set of package definitions.
111
112 Search options:
113
114 -r: recurring package definitions only
115
116 -p: packages with this price plan only
117
118 -m: packages with this comment only
119
120 -C: excluding package classnum or classnums (comma-separated list)
121
122 Change options:
123
124 -o: part_pkg_option optionname
125
126 -v: part_pkg_option optionvalue
127
128 -t: new taxclass
129
130 -s: Turn off "Show zero setup"
131
132 -S: Turn on "Show zero setup"
133
134 -z: Turn off "Show zero recurring"
135
136 -Z: Turn on "Show zero recurring"
137
138 -e: Multiply setup fee by this value (i.e. 1.05 for a 5% price increase)
139
140 -u: Multiply recurring fee by this value (i.e. 1.05 for a 5% price increase)
141
142 employee_username
143
144 =head1 BUGS
145
146 =head1 SEE ALSO
147
148 L<FS::part_pkg>
149
150 =cut
151
152 1;
153