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