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