1 package FS::part_pkg::agent_cdr;
2 use base qw( FS::part_pkg::recur_Common );
4 #kind of glommed together from cdr_termination, agent, voip_cdr
5 # some false laziness w/ all of them
8 use vars qw( $DEBUG $me %info );
9 use FS::Record qw( qsearch );
10 use FS::PagedSearch qw( psearch );
17 $me = '[FS::part_pkg::agent_cdr]';
19 tie my %temporalities, 'Tie::IxHash',
20 'upcoming' => "Upcoming (future)",
21 'preceding' => "Preceding (past)",
25 'name' => 'Wholesale CDR cost billing, for master customers of an agent.',
26 'shortname' => 'Wholesale CDR cost billing for agent',
27 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
28 'fields' => { #false laziness w/cdr_termination
30 #false laziness w/flat.pm
31 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
33 'select_options' => \%temporalities,
36 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
40 'recur_method' => { 'name' => 'Recurring fee method',
42 #'options' => \%recur_method,
44 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
47 #false laziness w/voip_cdr.pm
48 'output_format' => { 'name' => 'CDR invoice display format',
50 'select_options' => { FS::cdr::invoice_formats() },
51 'default' => 'simple2', #with source
54 'usage_section' => { 'name' => 'Section in which to place separate usage charges',
57 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
61 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
68 'fieldorder' => [ qw( recur_temporality recur_method cutoff_day ),
69 FS::part_pkg::prorate_Mixin::fieldorder,
71 output_format usage_section summarize_usage usage_mandate
80 my( $self, $cust_pkg, $sdate, $details, $param ) = @_;
82 #my $last_bill = $cust_pkg->last_bill;
83 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
86 if $self->recur_temporality eq 'preceding'
87 && ( $last_bill eq '' || $last_bill == 0 );
91 my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
95 #false laziness w/agent.pm
96 #almost always just one,
97 #unless you have multiple agents with same master customer0
98 my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
100 foreach my $agent (@agents) {
102 warn "$me billing wholesale CDRs for agent ". $agent->agent. "\n"
105 #not the most efficient to load them all into memory,
106 #but good enough for our current needs
107 my @cust_main = qsearch('cust_main', { 'agentnum' => $agent->agentnum } );
109 foreach my $cust_main (@cust_main) {
111 warn "$me billing agent wholesale CDRs for ". $cust_main->name_short. "\n"
114 #eofalse laziness w/agent.pm
117 foreach my $cust_pkg ( $cust_main->cust_pkg ) {
118 push @svcnum, map $_->svcnum, $cust_pkg->cust_svc( svcdb=>'svc_phone' );
123 #false laziness w/cdr_termination
125 my $termpart = 1; #or from an option -- we're not termination, we're wholesale? for now, use one or the other
127 #false lazienss w/search/cdr.html (i should be a part_termination method)
129 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
130 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
132 "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
134 #eofalse laziness w/cdr_termination.pm
136 #false laziness w/ svc_phone->psearch_cdrs, kinda
137 my $cdr_search = psearch({
139 #'addl_from' => $join_term,
141 'extra_sql' => " WHERE freesidestatus IN ( 'rated', 'done' ) ".
142 " AND svcnum IN (". join(',', @svcnum). ") ".
144 'order_by' => 'ORDER BY startdate FOR UPDATE ',
148 #false laziness w/voip_cdr
149 $cdr_search->limit(1000);
150 $cdr_search->increment(0); #because we're adding cdr_termination as we go?
151 while ( my $cdr = $cdr_search->fetch ) {
153 my $cost = $cdr->rate_cost;
154 #XXX exception handling? return undef? (and err?) ref to a scalar err?
156 #false laziness w/cdr_termination
158 #add a cdr_termination record and the charges
160 my $cdr_termination = new FS::cdr_termination {
161 'acctid' => $cdr->acctid,
162 'termpart' => $termpart,
163 'rated_price' => $cost,
167 my $error = $cdr_termination->insert;
168 die $error if $error; #next if $error; #or just skip this one??? why?
172 # and add a line to the invoice
174 my $call_details = $cdr->downstream_csv( 'format' => $output_format,
177 my $classnum = ''; #usage class?
179 #option to turn off? or just use squelch_cdr for the customer probably
180 # XXX use detail_format for this at some point
181 push @$details, { 'format' => 'C',
182 'detail' => $call_details,
184 'classnum' => $classnum };
186 #eofalse laziness w/cdr_termination
196 $charges += ($cust_pkg->quantity || 1)
197 * $self->calc_recur_Common($cust_pkg, $sdate, $details, $param);
202 sub can_discount { 0; }
204 #? sub hide_svc_detail { 1; }
208 sub can_usageprice { 0; }