1 package FS::part_pkg::cdr_termination;
4 use base qw( FS::part_pkg::recur_Common );
5 use vars qw( $DEBUG %info );
7 use FS::Record qw( qsearch ); #qsearchs );
9 use FS::cdr_termination;
11 tie my %temporalities, 'Tie::IxHash',
12 'upcoming' => "Upcoming (future)",
13 'preceding' => "Preceding (past)",
17 'name' => 'VoIP rating of CDR records for termination partners.',
18 'shortname' => 'VoIP/telco CDR termination',
19 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
21 #'cdr_column' => { 'name' => 'Column from CDR records',
23 # 'select_enum' => [qw(
39 #false laziness w/flat.pm
40 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
42 'select_options' => \%temporalities,
45 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
49 'recur_method' => { 'name' => 'Recurring fee method',
51 #'options' => \%recur_method,
53 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
56 #false laziness w/voip_cdr.pm
57 'output_format' => { 'name' => 'CDR invoice display format',
59 'select_options' => { FS::cdr::invoice_formats() },
60 'default' => 'simple2', #XXX test
63 'usage_section' => { 'name' => 'Section in which to place separate usage charges',
66 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
70 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
77 'fieldorder' => [qw( recur_temporality recur_method cutoff_day ),
78 FS::part_pkg::prorate_Mixin::fieldorder,
80 output_format usage_section summarize_usage usage_mandate
90 my($cust_pkg, $sdate, $details, $param ) = @_;
92 #my $last_bill = $cust_pkg->last_bill;
93 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
96 if $self->recur_temporality eq 'preceding'
97 && ( $last_bill eq '' || $last_bill == 0 );
99 # termination calculations
101 my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
102 die "no customer termination percentage" unless $term_percent;
104 my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
108 #find an svc_external record
109 my @svc_external = map { $_->svc_x }
110 grep { $_->part_svc->svcdb eq 'svc_external' }
113 die "cdr_termination package has no svc_external service"
114 unless @svc_external;
115 die "cdr_termination package has multiple svc_external services"
116 if scalar(@svc_external) > 1;
118 my $svc_external = $svc_external[0];
121 # - matching our customer via svc_external.id/title? (and via what field?)
123 #let's try carrierid for now, can always make it configurable or rewrite
124 my $cdr_column = 'carrierid';
126 my %hashref = ( 'freesidestatus' => 'done' );
128 # try matching on svc_external.id for now... (or title? if ints don't cut it)
129 $hashref{$cdr_column} = $svc_external[0]->id;
131 # - with no cdr_termination.status
133 my $termpart = 1; #or from an option
135 #false lazienss w/search/cdr.html (i should be a part_termination method)
137 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
138 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
140 "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
142 #may need to process in batches if there's waaay too many
145 #'addl_from' => $join_term,
146 'hashref' => \%hashref,
147 'extra_sql' => "$extra_sql FOR UPDATE",
150 foreach my $cdr (@cdrs) {
152 #add a cdr_termination record and the charges
155 #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
156 my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
158 my $cdr_termination = new FS::cdr_termination {
159 'acctid' => $cdr->acctid,
160 'termpart' => $termpart,
161 'rated_price' => $term_price,
165 my $error = $cdr_termination->insert;
166 die $error if $error; #next if $error; #or just skip this one??? why?
168 $charges += $term_price;
170 # and add a line to the invoice
172 my $call_details = $cdr->downstream_csv( 'format' => $output_format,
173 'charge' => $term_price,
176 my $classnum = ''; #usage class?
178 #option to turn off? or just use squelch_cdr for the customer probably
179 push @$details, [ 'C', $call_details, $term_price, $classnum ];
183 # eotermiation calculation
185 $charges += $self->calc_recur_Common(@_);