This commit was manufactured by cvs2svn to create tag 'freeside_2_1_1'.
[freeside.git] / FS / FS / part_pkg / cdr_termination.pm
1 package FS::part_pkg::cdr_termination;
2
3 use strict;
4 use base qw( FS::part_pkg::recur_Common );
5 use vars qw( $DEBUG %info );
6 use Tie::IxHash;
7 use FS::Record qw( qsearch ); #qsearchs );
8 use FS::cdr;
9 use FS::cdr_termination;
10
11 tie my %temporalities, 'Tie::IxHash',
12   'upcoming'  => "Upcoming (future)",
13   'preceding' => "Preceding (past)",
14 ;
15
16 %info = (
17   'name' => 'VoIP rating of CDR records for termination partners.',
18   'shortname' => 'VoIP/telco CDR termination',
19   'fields' => {
20
21     'setup_fee'     => { 'name' => 'Setup fee for this package',
22                          'default' => 0,
23                        },
24     'recur_fee'     => { 'name' => 'Base recurring fee for this package',
25                          'default' => 0,
26                        },
27
28     #'cdr_column'    => { 'name' => 'Column from CDR records',
29     #                     'type' => 'select',
30     #                     'select_enum' => [qw(
31     #                       dcontext
32     #                       channel
33     #                       dstchannel
34     #                       lastapp
35     #                       lastdata
36     #                       accountcode
37     #                       userfield
38     #                       cdrtypenum
39     #                       calltypenum
40     #                       description
41     #                       carrierid
42     #                       upstream_rateid
43     #                     )],
44     #                   },
45
46     #false laziness w/flat.pm
47     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
48                              'type' => 'select',
49                              'select_options' => \%temporalities,
50                            },
51
52     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
53                                    ' of service at cancellation',
54                          'type' => 'checkbox',
55                        },
56
57     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
58                                    'subscription',
59                          'default' => '1',
60                        },
61
62     'recur_method'  => { 'name' => 'Recurring fee method',
63                          #'type' => 'radio',
64                          #'options' => \%recur_method,
65                          'type' => 'select',
66                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
67                        },
68
69     #false laziness w/voip_cdr.pm
70     'output_format' => { 'name' => 'CDR invoice display format',
71                          'type' => 'select',
72                          'select_options' => { FS::cdr::invoice_formats() },
73                          'default'        => 'simple2', #XXX test
74                        },
75
76     'usage_section' => { 'name' => 'Section in which to place separate usage charges',
77                        },
78
79     'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
80                           'type' => 'checkbox',
81                         },
82
83     'usage_mandate' => { 'name' => 'Always put usage details in separate section',
84                           'type' => 'checkbox',
85                        },
86     #eofalse
87
88   },
89                        #cdr_column
90   'fieldorder' => [qw(
91                        setup_fee recur_fee
92                        recur_temporality unused_credit recur_method cutoff_day
93                        output_format usage_section summarize_usage usage_mandate
94                      )
95                   ],
96
97   'weight' => 48,
98
99 );
100
101 sub calc_setup {
102   my($self, $cust_pkg ) = @_;
103   $self->option('setup_fee');
104 }
105
106 sub calc_recur {
107   my $self = shift;
108   my($cust_pkg, $sdate, $details, $param ) = @_;
109
110   #my $last_bill = $cust_pkg->last_bill;
111   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
112
113   return 0
114     if $self->option('recur_temporality', 1) eq 'preceding'
115     && ( $last_bill eq '' || $last_bill == 0 );
116
117   # termination calculations
118
119   my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
120   die "no customer termination percentage" unless $term_percent;
121
122   my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
123
124   my $charges = 0;
125
126   #find an svc_external record
127   my @svc_external = map  { $_->svc_x }
128                      grep { $_->part_svc->svcdb eq 'svc_external' }
129                      $cust_pkg->cust_svc;
130
131   die "cdr_termination package has no svc_external service"
132     unless @svc_external;
133   die "cdr_termination package has multiple svc_external services"
134     if scalar(@svc_external) > 1;
135
136   my $svc_external = $svc_external[0];
137
138   # find CDRs:
139   # - matching our customer via svc_external.id/title?  (and via what field?)
140
141   #let's try carrierid for now, can always make it configurable or rewrite
142   my $cdr_column = 'carrierid';
143
144   my %hashref = ( 'freesidestatus' => 'done' );
145
146   # try matching on svc_external.id for now... (or title?  if ints don't cut it)
147   $hashref{$cdr_column} = $svc_external[0]->id; 
148
149   # - with no cdr_termination.status
150
151   my $termpart = 1; #or from an option
152
153   #false lazienss w/search/cdr.html (i should be a part_termination method)
154   my $where_term =
155     "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
156   #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
157   my $extra_sql =
158     "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
159
160   #may need to process in batches if there's waaay too many
161   my @cdrs = qsearch({
162     'table'     => 'cdr',
163     #'addl_from' => $join_term,
164     'hashref'   => \%hashref,
165     'extra_sql' => "$extra_sql FOR UPDATE",
166   });
167
168   foreach my $cdr (@cdrs) {
169
170     #add a cdr_termination record and the charges
171
172     # XXX config?
173     #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
174     my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
175
176     my $cdr_termination = new FS::cdr_termination {
177       'acctid'      => $cdr->acctid,
178       'termpart'    => $termpart,
179       'rated_price' => $term_price,
180       'status'      => 'done',
181     };
182
183     my $error = $cdr_termination->insert;
184     die $error if $error; #next if $error; #or just skip this one???  why?
185
186     $charges += $term_price;
187
188     # and add a line to the invoice
189
190     my $call_details = $cdr->downstream_csv( 'format' => $output_format,
191                                              'charge' => $term_price,
192                                            );
193
194     my $classnum = ''; #usage class?
195
196     #option to turn off?  or just use squelch_cdr for the customer probably
197     push @$details, [ 'C', $call_details, $term_price, $classnum ];
198
199   }
200     
201   # eotermiation calculation
202
203   $charges += $self->calc_recur_Common(@_);
204
205   $charges;
206 }
207
208 sub is_free {
209   0;
210 }
211
212 1;