bb8a26df5ea0a59f5ee91da54fc94306f9793937
[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     'display_separate_cust'=> { 'name' => 'Separate customer from package display on invoices',
43                                 'type' => 'checkbox',
44                               },
45
46   },
47
48   'fieldorder' => [qw( cutoff_day add_full_period no_pkg_prorate ) ],
49
50   'weight' => 52,
51
52 );
53
54 #some false laziness-ish w/bulk.pm...  not a lot
55 sub calc_recur {
56   my $self = shift;
57   my($cust_pkg, $sdate, $details, $param ) = @_;
58
59   my $last_bill = $cust_pkg->last_bill;
60
61   return sprintf("%.2f", $self->SUPER::calc_recur(@_) )
62     unless $$sdate > $last_bill;
63
64   my $conf = new FS::Conf;
65   my $money_char = $conf->config('money_char') || '$';
66   my $date_format = $conf->config('date_format') || '%m/%d/%Y';
67
68   my $total_agent_charge = 0;
69
70   warn "$me billing for agent packages from ". time2str('%x', $last_bill).
71                                        " to ". time2str('%x', $$sdate). "\n"
72     if $DEBUG;
73
74   my $prorate_ratio =   ( $$sdate                     - $last_bill )
75                       / ( $self->add_freq($last_bill) - $last_bill );
76
77   #almost always just one,
78   #unless you have multiple agents with same master customer0
79   my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
80
81   foreach my $agent (@agents) {
82
83     warn "$me billing for agent ". $agent->agent. "\n"
84       if $DEBUG;
85
86     #not the most efficient to load them all into memory,
87     #but good enough for our current needs
88     my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );
89
90     foreach my $cust_main (@cust_main) {
91
92       warn "$me billing agent charges for ". $cust_main->name_short. "\n"
93         if $DEBUG;
94
95       #make sure setup dates are filled in
96       my $error = $cust_main->bill; #options don't propogate from freeside-daily
97       die "Error pre-billing agent customer: $error" if $error;
98
99       my @cust_pkg = grep { my $setup  = $_->get('setup');
100                             my $cancel = $_->get('cancel');
101
102                             $setup < $$sdate  # END
103                             && ( ! $cancel || $cancel > $last_bill ) #START
104                           }
105                      $cust_main->all_pkgs;
106
107       my $cust_details = 0;
108
109       foreach my $cust_pkg ( @cust_pkg ) {
110
111         warn "$me billing agent charges for pkgnum ". $cust_pkg->pkgnum. "\n"
112           if $DEBUG;
113
114         my $pkg_details = '';
115
116         my $cust_location = $cust_pkg->cust_location;
117         $pkg_details .= $cust_location->locationname. ': '
118           if $cust_location->locationname;
119
120         my $part_pkg = $cust_pkg->part_pkg;
121
122         # + something to identify package... primary service probably
123         # no... package def for now
124         $pkg_details .= $part_pkg->pkg. ': ';
125
126         my $pkg_charge = 0;
127
128         my $quantity = $cust_pkg->quantity || 1;
129
130         #option to not fallback? via options above
131         my $pkg_setup_fee  =
132           $part_pkg->setup_cost || $part_pkg->option('setup_fee');
133         my $pkg_base_recur =
134           $part_pkg->recur_cost || $part_pkg->base_recur_permonth($cust_pkg);
135
136         my $pkg_start = $cust_pkg->get('setup');
137         if ( $pkg_start < $last_bill ) {
138           $pkg_start = $last_bill;
139         } elsif ( $pkg_setup_fee ) {
140           $pkg_charge += $quantity * $pkg_setup_fee;
141           $pkg_details .= $money_char.
142                           sprintf('%.2f setup', $quantity * $pkg_setup_fee );
143           $pkg_details .= sprintf(" ($quantity \@ $money_char". '%.2f)',
144                                   $pkg_setup_fee )
145             if $quantity > 1;
146           $pkg_details .= ', ';
147         }
148
149         my $pkg_end = $cust_pkg->get('cancel');
150         $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;
151
152         my $pkg_recur_charge = $prorate_ratio * $pkg_base_recur;
153         $pkg_recur_charge *= ( $pkg_end - $pkg_start )
154                            / ( $$sdate  - $last_bill )
155           unless $self->option('no_pkg_prorate');
156
157         my $recur_charge += $pkg_recur_charge;
158
159         if ( $recur_charge ) {
160           $pkg_details .= $money_char.
161                           sprintf('%.2f', $quantity * $recur_charge );
162           $pkg_details .= sprintf(" ($quantity \@ $money_char". '%.2f)',
163                                   $recur_charge )
164             if $quantity > 1;
165           $pkg_details .= ' ('.  time2str($date_format, $pkg_start).
166                           ' - '. time2str($date_format, $pkg_end  ). ')';
167         }
168
169         $pkg_charge += $quantity * $recur_charge;
170
171         if ( $pkg_charge ) {
172           if ( $self->option('display_separate_cust') ) {
173             push @$details, $cust_main->name.':' unless $cust_details++;
174             push @$details, '    '.$pkg_details;
175           } else {
176             push @$details, $cust_main->name_short.': '. $pkg_details;
177           }
178         };
179
180         $total_agent_charge += $pkg_charge;
181
182       } #foreach $cust_pkg
183
184       push @$details, ' ' if $cust_details;
185
186     } #foreach $cust_main
187
188   } #foreach $agent;
189
190   my $charges = $total_agent_charge + $self->SUPER::calc_recur(@_); #prorate
191
192   sprintf('%.2f', $charges );
193
194 }
195
196 sub can_discount { 0; }
197
198 sub hide_svc_detail {
199   1;
200 }
201
202 sub is_free {
203   0;
204 }
205
206 1;
207