summaryrefslogtreecommitdiff
path: root/bin/part_pkg-bulk_change
blob: 5347da6beffd7daea17f2f3c3eb41b7a426c6692 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/perl

use strict;
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 );
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch qsearchs);
use FS::part_pkg;
use FS::part_pkg_option;

getopts('rm:C:p:o:v:t:sSzZe:u:');

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

my %search = ();
$search{'plan'} = $opt_p if $opt_p;
$search{'comment'} = $opt_m if $opt_m;

my $extra_sql = '';
$extra_sql = ( keys(%search) ? 'AND' : 'WHERE' ).
             " classnum NOT IN ($opt_C)"
  if $opt_C;

foreach my $part_pkg ( qsearch({ 'table'     => 'part_pkg',
                                 'hashref'   => \%search,
                                 'extra_sql' => $extra_sql,
                              })
                     )
{
  next if ! $part_pkg->freq && $opt_r;

  if ( $opt_o ) {

    my %hash = (
      'pkgpart'    => $part_pkg->pkgpart,
      'optionname' => $opt_o,
    );

    my $part_pkg_option = qsearchs('part_pkg_option', \%hash);

    if ( $part_pkg_option ) {
      next if $part_pkg_option->optionvalue eq $opt_v;
      $part_pkg_option->optionvalue($opt_v);
      my $error = $part_pkg_option->replace;
      die $error if $error;
    } else {
      $part_pkg_option = new FS::part_pkg_option { %hash,
                                                   'optionvalue'=>$opt_v,
                                                 };
      my $error = $part_pkg_option->insert;
      die $error if $error;
    }

  }

  if ( $opt_e ) {
    my %hash = (
      'pkgpart'    => $part_pkg->pkgpart,
      'optionname' => 'setup_fee',
    );

    my $part_pkg_option = qsearchs('part_pkg_option', \%hash);

    if ( $part_pkg_option ) {
      $part_pkg_option->optionvalue(
        sprintf('%.2f', $part_pkg_option->optionvalue * $opt_e)
      );
      my $error = $part_pkg_option->replace;
      die $error if $error;
    }
  }

  if ( $opt_u ) {
    my %hash = (
      'pkgpart'    => $part_pkg->pkgpart,
      'optionname' => 'recur_fee',
    );

    my $part_pkg_option = qsearchs('part_pkg_option', \%hash);

    if ( $part_pkg_option ) {
      $part_pkg_option->optionvalue(
        sprintf('%.2f', $part_pkg_option->optionvalue * $opt_u)
      );
      my $error = $part_pkg_option->replace;
      die $error if $error;
    }
  }

  if ( $opt_t || $opt_s || $opt_S || $opt_z || $opt_Z ) {

    $part_pkg->taxclass($opt_t) if $opt_t;
    $part_pkg->setup_show_zero('') if $opt_s;
    $part_pkg->setup_show_zero('Y') if $opt_S;
    $part_pkg->recur_show_zero('') if $opt_z;
    $part_pkg->recur_show_zero('Y') if $opt_Z;

    my $error = $part_pkg->replace;

  }

}

sub usage {
  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";
}

=head1 NAME

cust_main-bulk_change

=head1 SYNOPSIS

  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

=head1 DESCRIPTION

Command-line tool to change a set of package definitions.

Search options:

-r: recurring package definitions only

-p: packages with this price plan only

-m: packages with this comment only

-C: excluding package classnum or classnums (comma-separated list)

Change options:

-o: part_pkg_option optionname

-v: part_pkg_option optionvalue

-t: new taxclass

-s: Turn off "Show zero setup"

-S: Turn on "Show zero setup"

-z: Turn off "Show zero recurring"

-Z: Turn on "Show zero recurring"

-e: Multiply setup fee by this value (i.e. 1.05 for a 5% price increase)

-u: Multiply recurring fee by this value (i.e. 1.05 for a 5% price increase)

employee_username

=head1 BUGS

=head1 SEE ALSO

L<FS::part_pkg>

=cut

1;