This commit was manufactured by cvs2svn to create branch
[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     'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
62                                     'for one full period after that',
63                           'type' => 'checkbox',
64                         },
65
66     'recur_method'  => { 'name' => 'Recurring fee method',
67                          #'type' => 'radio',
68                          #'options' => \%recur_method,
69                          'type' => 'select',
70                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
71                        },
72
73     #false laziness w/voip_cdr.pm
74     'output_format' => { 'name' => 'CDR invoice display format',
75                          'type' => 'select',
76                          'select_options' => { FS::cdr::invoice_formats() },
77                          'default'        => 'simple2', #XXX test
78                        },
79
80     'usage_section' => { 'name' => 'Section in which to place separate usage charges',
81                        },
82
83     'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
84                           'type' => 'checkbox',
85                         },
86
87     'usage_mandate' => { 'name' => 'Always put usage details in separate section',
88                           'type' => 'checkbox',
89                        },
90     #eofalse
91
92   },
93                        #cdr_column
94   'fieldorder' => [qw(
95                        setup_fee recur_fee
96                        recur_temporality unused_credit recur_method cutoff_day
97                        add_full_period
98                        output_format usage_section summarize_usage usage_mandate
99                      )
100                   ],
101
102   'weight' => 48,
103
104 );
105
106 sub calc_setup {
107   my($self, $cust_pkg ) = @_;
108   $self->option('setup_fee');
109 }
110
111 sub calc_recur {
112   my $self = shift;
113   my($cust_pkg, $sdate, $details, $param ) = @_;
114
115   #my $last_bill = $cust_pkg->last_bill;
116   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
117
118   return 0
119     if $self->option('recur_temporality', 1) eq 'preceding'
120     && ( $last_bill eq '' || $last_bill == 0 );
121
122   # termination calculations
123
124   my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
125   die "no customer termination percentage" unless $term_percent;
126
127   my $output_format = $self->option('output_format', 'Hush!') || 'simple2';
128
129   my $charges = 0;
130
131   #find an svc_external record
132   my @svc_external = map  { $_->svc_x }
133                      grep { $_->part_svc->svcdb eq 'svc_external' }
134                      $cust_pkg->cust_svc;
135
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;
140
141   my $svc_external = $svc_external[0];
142
143   # find CDRs:
144   # - matching our customer via svc_external.id/title?  (and via what field?)
145
146   #let's try carrierid for now, can always make it configurable or rewrite
147   my $cdr_column = 'carrierid';
148
149   my %hashref = ( 'freesidestatus' => 'done' );
150
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; 
153
154   # - with no cdr_termination.status
155
156   my $termpart = 1; #or from an option
157
158   #false lazienss w/search/cdr.html (i should be a part_termination method)
159   my $where_term =
160     "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
161   #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
162   my $extra_sql =
163     "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";
164
165   #may need to process in batches if there's waaay too many
166   my @cdrs = qsearch({
167     'table'     => 'cdr',
168     #'addl_from' => $join_term,
169     'hashref'   => \%hashref,
170     'extra_sql' => "$extra_sql FOR UPDATE",
171   });
172
173   foreach my $cdr (@cdrs) {
174
175     #add a cdr_termination record and the charges
176
177     # XXX config?
178     #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
179     my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );
180
181     my $cdr_termination = new FS::cdr_termination {
182       'acctid'      => $cdr->acctid,
183       'termpart'    => $termpart,
184       'rated_price' => $term_price,
185       'status'      => 'done',
186     };
187
188     my $error = $cdr_termination->insert;
189     die $error if $error; #next if $error; #or just skip this one???  why?
190
191     $charges += $term_price;
192
193     # and add a line to the invoice
194
195     my $call_details = $cdr->downstream_csv( 'format' => $output_format,
196                                              'charge' => $term_price,
197                                            );
198
199     my $classnum = ''; #usage class?
200
201     #option to turn off?  or just use squelch_cdr for the customer probably
202     push @$details, [ 'C', $call_details, $term_price, $classnum ];
203
204   }
205     
206   # eotermiation calculation
207
208   $charges += $self->calc_recur_Common(@_);
209
210   $charges;
211 }
212
213 sub is_free {
214   0;
215 }
216
217 1;