fix agent bulk billing with quantities, RT#31610
[freeside.git] / FS / FS / part_pkg / agent.pm
1 package FS::part_pkg::agent;
2
3 use strict;
4 use vars qw(@ISA $DEBUG $me %info);
5 use Date::Format;
6 use FS::Record qw( qsearch );
7 use FS::agent;
8 use FS::cust_main;
9
10 #use FS::part_pkg::recur_Common;;
11 #@ISA = qw(FS::part_pkg::recur_Common);
12 use FS::part_pkg::prorate;
13 @ISA = qw(FS::part_pkg::prorate);
14
15 $DEBUG = 0;
16
17 $me = '[FS::part_pkg::agent]';
18
19 %info = (
20   'name'      => 'Wholesale bulk billing, for master customers of an agent.',
21   'shortname' => 'Wholesale bulk billing for agent.',
22   'inherit_fields' => [qw( prorate global_Mixin)],
23   'fields' => {
24     #'recur_method'  => { 'name' => 'Recurring fee method',
25     #                     #'type' => 'radio',
26     #                     #'options' => \%recur_method,
27     #                     'type' => 'select',
28     #                     'select_options' => \%recur_Common::recur_method,
29     #                   },
30     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28)',
31                          'default' => '1',
32                        },
33     'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
34                                     'for one full period after that',
35                           'type' => 'checkbox',
36                         },
37
38     'no_pkg_prorate'   => { 'name' => 'Disable prorating bulk packages (charge full price for packages active only a portion of the month)',
39                             'type' => 'checkbox',
40                           },
41
42   },
43
44   'fieldorder' => [qw( cutoff_day add_full_period no_pkg_prorate ) ],
45
46   'weight' => 52,
47
48 );
49
50 #some false laziness-ish w/bulk.pm...  not a lot
51 sub calc_recur {
52   my $self = shift;
53   my($cust_pkg, $sdate, $details, $param ) = @_;
54
55   my $last_bill = $cust_pkg->last_bill;
56
57   return sprintf("%.2f", $self->SUPER::calc_recur(@_) )
58     unless $$sdate > $last_bill;
59
60   my $conf = new FS::Conf;
61   my $money_char = $conf->config('money_char') || '$';
62   my $date_format = $conf->config('date_format') || '%m/%d/%Y';
63
64   my $total_agent_charge = 0;
65
66   warn "$me billing for agent packages from ". time2str('%x', $last_bill).
67                                        " to ". time2str('%x', $$sdate). "\n"
68     if $DEBUG;
69
70   my $prorate_ratio =   ( $$sdate                     - $last_bill )
71                       / ( $self->add_freq($last_bill) - $last_bill );
72
73   #almost always just one,
74   #unless you have multiple agents with same master customer0
75   my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
76
77   foreach my $agent (@agents) {
78
79     warn "$me billing for agent ". $agent->agent. "\n"
80       if $DEBUG;
81
82     #not the most efficient to load them all into memory,
83     #but good enough for our current needs
84     my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );
85
86     foreach my $cust_main (@cust_main) {
87
88       warn "$me billing agent charges for ". $cust_main->name_short. "\n"
89         if $DEBUG;
90
91       #make sure setup dates are filled in
92       my $error = $cust_main->bill; #options don't propogate from freeside-daily
93       die "Error pre-billing agent customer: $error" if $error;
94
95       my @cust_pkg = grep { my $setup  = $_->get('setup');
96                             my $cancel = $_->get('cancel');
97
98                             $setup < $$sdate  # END
99                             && ( ! $cancel || $cancel > $last_bill ) #START
100                           }
101                      $cust_main->all_pkgs;
102
103       foreach my $cust_pkg ( @cust_pkg ) {
104
105         warn "$me billing agent charges for pkgnum ". $cust_pkg->pkgnum. "\n"
106           if $DEBUG;
107
108         my $pkg_details = $cust_main->name_short. ': '; #name?
109
110         my $cust_location = $cust_pkg->cust_location;
111         $pkg_details .= $cust_location->locationname. ': '
112           if $cust_location->locationname;
113
114         my $part_pkg = $cust_pkg->part_pkg;
115
116         # + something to identify package... primary service probably
117         # no... package def for now
118         $pkg_details .= $part_pkg->pkg. ': ';
119
120         my $pkg_charge = 0;
121
122         my $quantity = $cust_pkg->quantity || 1;
123
124         #option to not fallback? via options above
125         my $pkg_setup_fee  =
126           $part_pkg->setup_cost || $part_pkg->option('setup_fee');
127         my $pkg_base_recur =
128           $part_pkg->recur_cost || $part_pkg->base_recur_permonth($cust_pkg);
129
130         my $pkg_start = $cust_pkg->get('setup');
131         if ( $pkg_start < $last_bill ) {
132           $pkg_start = $last_bill;
133         } elsif ( $pkg_setup_fee ) {
134           $pkg_charge += $quantity * $pkg_setup_fee;
135           $pkg_details .= $money_char.
136                           sprintf('%.2f setup', $quantity * $pkg_setup_fee );
137           $pkg_details .= sprintf(" ($quantity \@ $money_char". '%.2f)',
138                                   $pkg_setup_fee )
139             if $quantity > 1;
140           $pkg_details .= ', ';
141         }
142
143         my $pkg_end = $cust_pkg->get('cancel');
144         $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;
145
146         my $pkg_recur_charge = $prorate_ratio * $pkg_base_recur;
147         $pkg_recur_charge *= ( $pkg_end - $pkg_start )
148                            / ( $$sdate  - $last_bill )
149           unless $self->option('no_pkg_prorate');
150
151         my $recur_charge += $pkg_recur_charge;
152
153         if ( $recur_charge ) {
154           $pkg_details .= $money_char.
155                           sprintf('%.2f', $quantity * $recur_charge );
156           $pkg_details .= sprintf(" ($quantity \@ $money_char". '%.2f)',
157                                   $recur_charge )
158             if $quantity > 1;
159           $pkg_details .= ' ('.  time2str($date_format, $pkg_start).
160                           ' - '. time2str($date_format, $pkg_end  ). ')';
161         }
162
163         $pkg_charge += $quantity * $recur_charge;
164
165         push @$details, $pkg_details
166           if $pkg_charge;
167         $total_agent_charge += $pkg_charge;
168
169       } #foreach $cust_pkg
170
171     } #foreach $cust_main
172
173   } #foreach $agent;
174
175   my $charges = $total_agent_charge + $self->SUPER::calc_recur(@_); #prorate
176
177   sprintf('%.2f', $charges );
178
179 }
180
181 sub can_discount { 0; }
182
183 sub hide_svc_detail {
184   1;
185 }
186
187 sub is_free {
188   0;
189 }
190
191 1;
192