1 package FS::part_pkg::agent;
4 use vars qw(@ISA $DEBUG $me %info);
6 use FS::Record qw( qsearch );
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);
17 $me = '[FS::part_pkg::agent]';
20 'name' => 'Wholesale bulk billing, for master customers of an agent.',
21 'shortname' => 'Wholesale bulk billing for agent.',
24 'setup_fee' => { 'name' => 'Setup fee for this package',
27 'recur_fee' => { 'name' => 'Base recurring fee for this package',
32 #'recur_method' => { 'name' => 'Recurring fee method',
34 # #'options' => \%recur_method,
36 # 'select_options' => \%recur_Common::recur_method,
38 'cutoff_day' => { 'name' => 'Billing Day (1 - 28)',
41 'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
42 'for one full period after that',
46 'no_pkg_prorate' => { 'name' => 'Disable prorating bulk packages (charge full price for packages active only a portion of the month)',
52 #'fieldorder' => [qw( setup_fee recur_fee recur_method cutoff_day ) ],
53 'fieldorder' => [qw( setup_fee recur_fee cutoff_day add_full_period no_pkg_prorate ) ],
59 #some false laziness-ish w/bulk.pm... not a lot
62 my($cust_pkg, $sdate, $details, $param ) = @_;
64 my $last_bill = $cust_pkg->last_bill;
66 return sprintf("%.2f", $self->SUPER::calc_recur(@_) )
67 unless $$sdate > $last_bill;
69 my $conf = new FS::Conf;
70 my $money_char = $conf->config('money_char') || '$';
72 my $total_agent_charge = 0;
74 warn "$me billing for agent packages from ". time2str('%x', $last_bill).
75 " to ". time2str('%x', $$sdate). "\n"
78 my $prorate_ratio = ( $$sdate - $last_bill )
79 / ( $self->add_freq($last_bill) - $last_bill );
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 } );
85 foreach my $agent (@agents) {
87 warn "$me billing for agent ". $agent->agent. "\n"
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 } );
94 foreach my $cust_main (@cust_main) {
96 warn "$me billing agent charges for ". $cust_main->name_short. "\n"
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;
103 my @cust_pkg = grep { my $setup = $_->get('setup');
104 my $cancel = $_->get('cancel');
106 $setup < $$sdate # END
107 && ( ! $cancel || $cancel > $last_bill ) #START
109 $cust_main->all_pkgs;
111 foreach my $cust_pkg ( @cust_pkg ) {
113 warn "$me billing agent charges for pkgnum ". $cust_pkg->pkgnum. "\n"
116 my $pkg_details = $cust_main->name_short. ': '; #name?
117 # + something to identify package... primary service probably
121 my $part_pkg = $cust_pkg->part_pkg;
122 #option to not fallback? via options above
124 $part_pkg->setup_cost || $part_pkg->option('setup_fee');
126 $part_pkg->recur_cost || $part_pkg->base_recur_permonth($cust_pkg);
128 my $pkg_start = $cust_pkg->get('setup');
129 if ( $pkg_start < $last_bill ) {
130 $pkg_start = $last_bill;
131 } elsif ( $pkg_setup_fee ) {
132 $pkg_charge += $pkg_setup_fee;
133 $pkg_details .= $money_char. sprintf('%.2f setup, ', $pkg_setup_fee );
136 my $pkg_end = $cust_pkg->get('cancel');
137 $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;
140 my $pkg_recur_charge = $prorate_ratio * $pkg_base_recur;
141 $pkg_recur_charge *= ( $pkg_end - $pkg_start )
142 / ( $$sdate - $last_bill )
143 unless $self->option('no_pkg_prorate');
145 my $recur_charge += $pkg_recur_charge;
147 $pkg_details .= $money_char. sprintf('%.2f', $recur_charge ).
148 ' ('. time2str('%x', $pkg_start).
149 ' - '. time2str('%x', $pkg_end ). ')'
152 $pkg_charge += $recur_charge;
154 push @$details, $pkg_details
156 $total_agent_charge += $pkg_charge;
160 } #foreach $cust_main
164 my $charges = $total_agent_charge + $self->SUPER::calc_recur(@_); #prorate
166 sprintf('%.2f', $charges );
170 sub can_discount { 0; }
172 sub hide_svc_detail {