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