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',
21 'setup_fee' => { 'name' => 'Setup fee for this package',
24 'recur_fee' => { 'name' => 'Base recurring fee for this package',
28 #'cdr_column' => { 'name' => 'Column from CDR records',
30 # 'select_enum' => [qw(
46 #false laziness w/flat.pm
47 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
49 'select_options' => \%temporalities,
52 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
53 ' of service at cancellation',
57 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
61 'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
62 'for one full period after that',
66 'recur_method' => { 'name' => 'Recurring fee method',
68 #'options' => \%recur_method,
70 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
73 #false laziness w/voip_cdr.pm
74 'output_format' => { 'name' => 'CDR invoice display format',
76 'select_options' => { FS::cdr::invoice_formats() },
77 'default' => 'simple2', #XXX test
80 'usage_section' => { 'name' => 'Section in which to place separate usage charges',
83 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
87 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
96 recur_temporality unused_credit recur_method cutoff_day
98 output_format usage_section summarize_usage usage_mandate
107 my($self, $cust_pkg ) = @_;
108 $self->option('setup_fee');
113 my($cust_pkg, $sdate, $details, $param ) = @_;
115 #my $last_bill = $cust_pkg->last_bill;
116 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
119 if $self->option('recur_temporality', 1) eq 'preceding'
120 && ( $last_bill eq '' || $last_bill == 0 );
122 # termination calculations
124 my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
125 die "no customer termination percentage" unless $term_percent;
127 my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
131 #find an svc_external record
132 my @svc_external = map { $_->svc_x }
133 grep { $_->part_svc->svcdb eq 'svc_external' }
136 die "cdr_termination package has no svc_external service"
137 unless @svc_external;
138 die "cdr_termination package has multiple svc_external services"
139 if scalar(@svc_external) > 1;
141 my $svc_external = $svc_external[0];
144 # - matching our customer via svc_external.id/title? (and via what field?)
146 #let's try carrierid for now, can always make it configurable or rewrite
147 my $cdr_column = 'carrierid';
149 my %hashref = ( 'freesidestatus' => 'done' );
151 # try matching on svc_external.id for now... (or title? if ints don't cut it)
152 $hashref{$cdr_column} = $svc_external[0]->id;
154 # - with no cdr_termination.status
156 my $termpart = 1; #or from an option
158 #false lazienss w/search/cdr.html (i should be a part_termination method)
160 "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
161 #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
163 "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
165 #may need to process in batches if there's waaay too many
168 #'addl_from' => $join_term,
169 'hashref' => \%hashref,
170 'extra_sql' => "$extra_sql FOR UPDATE",
173 foreach my $cdr (@cdrs) {
175 #add a cdr_termination record and the charges
178 #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
179 my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
181 my $cdr_termination = new FS::cdr_termination {
182 'acctid' => $cdr->acctid,
183 'termpart' => $termpart,
184 'rated_price' => $term_price,
188 my $error = $cdr_termination->insert;
189 die $error if $error; #next if $error; #or just skip this one??? why?
191 $charges += $term_price;
193 # and add a line to the invoice
195 my $call_details = $cdr->downstream_csv( 'format' => $output_format,
196 'charge' => $term_price,
199 my $classnum = ''; #usage class?
201 #option to turn off? or just use squelch_cdr for the customer probably
202 push @$details, [ 'C', $call_details, $term_price, $classnum ];
206 # eotermiation calculation
208 $charges += $self->calc_recur_Common(@_);