RT#38973: Bill for time worked on ticket resolution [fully functional]
[freeside.git] / FS / FS / part_pkg / rt_field.pm
1 package FS::part_pkg::rt_field;
2
3 use strict;
4 use FS::Conf;
5 use FS::TicketSystem;
6 use FS::Record qw(qsearchs qsearch);
7 use FS::part_pkg::recur_Common;
8 use FS::part_pkg::global_Mixin;
9 use FS::rt_field_charge;
10
11 our @ISA = qw(FS::part_pkg::recur_Common);
12
13 our $DEBUG = 0;
14
15 use vars qw( $conf $money_char );
16
17 FS::UID->install_callback( sub {
18   $conf = new FS::Conf;
19   $money_char = $conf->config('money_char') || '$';
20 });
21
22 my %custom_field = (
23   'type'        => 'select-rt-customfield',
24   'lookuptype'  => 'RT::Queue-RT::Ticket',
25 );
26
27 my %multiple = (
28   'multiple' => 1,
29   'parse' => sub { @_ }, # because /edit/process/part_pkg.pm doesn't grok select multiple
30 );
31
32 our %info = (
33   'name'      =>  'Bill from custom fields in resolved RT tickets',
34   'shortname' =>  'RT custom rate',
35   'weight'    => 65,
36   'inherit_fields' => [ 'global_Mixin' ],
37   'fields'    =>  {
38     'queueids'       => { 'name' => 'Queues',
39                           'type' => 'select-rt-queue',
40                           %multiple,
41                         },
42     'unit_field'     => { 'name' => 'Units field',
43                           %custom_field,
44                           'validate' => sub { return ${$_[1]} ? '' : 'Units field must be specified' },
45                         },
46     'rate_field'     => { 'name' => 'Charge per unit (from RT field)',
47                           %custom_field,
48                           'empty_label' => '',
49                         },
50         'rate_flat'      => { 'name' => 'Charge per unit (flat)',
51                           'validate' => \&FS::part_pkg::global_Mixin::validate_moneyn },
52     'display_fields' => { 'name' => 'Display fields',
53                           %custom_field,
54                           %multiple,
55                         },
56     # from global_Mixin, but don't get used by this at all
57     'unused_credit_cancel'  => {'disabled' => 1},
58     'unused_credit_suspend' => {'disabled' => 1},
59     'unused_credit_change'  => {'disabled' => 1},
60   },
61   'validate' => sub {
62     my $options = shift;
63     return 'Rate must be specified'
64       unless $options->{'rate_field'} or $options->{'rate_flat'};
65     return 'Cannot specify both flat rate and rate field'
66       if $options->{'rate_field'} and $options->{'rate_flat'};
67     return '';
68   },
69   'fieldorder' => [ 'queueids', 'unit_field', 'rate_field', 'rate_flat', 'display_fields' ]
70 );
71
72 sub price_info {
73     my $self = shift;
74     my $str = $self->SUPER::price_info;
75     $str .= ' plus ' if $str;
76     $str .= 'charge from RT';
77 # takes way too long just to get a package label
78 #    FS::TicketSystem->init();
79 #    my %custom_fields = FS::TicketSystem->custom_fields();
80 #    my $rate = $self->option('rate_flat',1);
81 #    my $rate_field = $self->option('rate_field',1);
82 #    my $unit_field = $self->option('unit_field');
83 #    $str .= $rate
84 #            ? $money_char . sprintf("%.2",$rate)
85 #            : $custom_fields{$rate_field};
86 #    $str .= ' x ' . $custom_fields{$unit_field};
87     return $str;
88 }
89
90 sub calc_setup {
91   my($self, $cust_pkg ) = @_;
92   $self->option('setup_fee');
93 }
94
95 sub calc_recur {
96   my $self = shift;
97   my($cust_pkg, $sdate, $details, $param ) = @_;
98
99   my $charges = 0;
100
101   $charges += $self->calc_usage(@_);
102   $charges += ($cust_pkg->quantity || 1) * $self->calc_recur_Common(@_);
103
104   $charges;
105
106 }
107
108 sub can_discount { 0; }
109
110 sub calc_usage {
111   my $self = shift;
112   my($cust_pkg, $sdate, $details, $param ) = @_;
113
114   FS::TicketSystem->init();
115
116   my %queues = FS::TicketSystem->queues(undef,'SeeCustomField');
117
118   my @tickets;
119   foreach my $queueid (
120     split(', ',$self->option('queueids',1) || '')
121   ) {
122
123     die "Insufficient permission to invoice package"
124       unless exists $queues{$queueid};
125
126     # load all resolved tickets since pkg was ordered
127     # will subtract previous charges below
128     # only way to be sure we've caught everything
129     my $tickets = FS::TicketSystem->customer_tickets({
130       number   => $cust_pkg->custnum, 
131       limit    => 10000, # arbitrarily large
132       status   => 'resolved',
133       queueid  => $queueid,
134       resolved => $cust_pkg->order_date, # or setup? but this is mainly for installations,
135                                          # and workflow might resolve tickets before first bill...
136                                          # for now, expect pkg to be ordered before tickets get resolved,
137                                          # easy enough to make a pkg option to use setup/sdate instead
138     });
139     push @tickets, @$tickets;
140   };
141
142   my $rate = $self->option('rate_flat',1);
143   my $rate_field = $self->option('rate_field',1);
144   my $unit_field = $self->option('unit_field');
145   my @display_fields = split(', ',$self->option('display_fields',1) || '');
146
147   my %custom_fields = FS::TicketSystem->custom_fields();
148   my $rate_label = $rate
149                    ? ''
150                    : ' ' . $custom_fields{$rate_field};
151   my $unit_label = $custom_fields{$unit_field};
152
153   $rate_field = 'CF.{' . $rate_field . '}' if $rate_field;
154   $unit_field = 'CF.{' . $unit_field . '}';
155
156   my $charges = 0;
157   foreach my $ticket ( @tickets ) {
158     next unless $ticket->{$unit_field};
159     next unless $rate || $ticket->{$rate_field};
160     my $trate = $rate || $ticket->{$rate_field};
161     my $tunit = $ticket->{$unit_field};
162     my $subcharge = sprintf('%.2f', $trate * $tunit);
163     my $precharge = _previous_charges( $cust_pkg->pkgnum, $ticket->{'id'} );
164     $subcharge -= $precharge;
165
166     # if field values for previous charges increased,
167     # we can make additional charges here and now,
168     # but if field values were decreased, we just ignore--
169     # credits will have to be applied manually later, if that's what's intended
170     next if $subcharge <= 0;
171
172     my $rt_field_charge = new FS::rt_field_charge {
173       'pkgnum' => $cust_pkg->pkgnum,
174       'ticketid' => $ticket->{'id'},
175       'rate' => $trate,
176       'units' => $tunit,
177       'charge' => $subcharge,
178       '_date' => $$sdate,
179     };
180     my $error = $rt_field_charge->insert;
181     die "Error inserting rt_field_charge: $error" if $error;
182     push @$details, $money_char . sprintf('%.2f',$trate) . $rate_label . ' x ' . $tunit . ' ' . $unit_label;
183     push @$details, ' - ' . $money_char . sprintf('%.2f',$precharge) . ' previously charged' if $precharge;
184     foreach my $field (
185       sort { $ticket->{'_cf_sort_order'}{$a} <=> $ticket->{'_cf_sort_order'}{$b} } @display_fields
186     ) {
187       my $label = $custom_fields{$field};
188       my $value = $ticket->{'CF.{' . $field . '}'};
189       push @$details, $label . ': ' . $value if $value;
190     }
191     $charges += $subcharge;
192   }
193   return $charges;
194 }
195
196 sub _previous_charges {
197   my ($pkgnum, $ticketid) = @_;
198   my $prev = 0;
199   foreach my $rt_field_charge (
200     qsearch('rt_field_charge', { pkgnum => $pkgnum, ticketid => $ticketid })
201   ) {
202     $prev += $rt_field_charge->charge;
203   }
204   return $prev;
205 }
206
207 1;