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