summaryrefslogtreecommitdiff
path: root/FS/FS/part_pkg/cdr_termination.pm
blob: 5413c185af86699e272da0cfc020383d7712b78f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package FS::part_pkg::cdr_termination;
use base qw( FS::part_pkg::recur_Common );

use strict;
use vars qw( $DEBUG %info );
use Tie::IxHash;
use FS::Record qw( qsearch ); #qsearchs );
use FS::cdr;
use FS::cdr_termination;

tie my %temporalities, 'Tie::IxHash',
  'upcoming'  => "Upcoming (future)",
  'preceding' => "Preceding (past)",
;

%info = (
  'name' => 'VoIP rating of CDR records for termination partners.',
  'shortname' => 'VoIP/telco CDR termination',
  'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
  'fields' => {
    #'cdr_column'    => { 'name' => 'Column from CDR records',
    #                     'type' => 'select',
    #                     'select_enum' => [qw(
    #                       dcontext
    #                       channel
    #                       dstchannel
    #                       lastapp
    #                       lastdata
    #                       accountcode
    #                       userfield
    #                       cdrtypenum
    #                       calltypenum
    #                       description
    #                       carrierid
    #                       upstream_rateid
    #                     )],
    #                   },

    #false laziness w/flat.pm
    'recur_temporality' => { 'name' => 'Charge recurring fee for period',
                             'type' => 'select',
                             'select_options' => \%temporalities,
                           },

    'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
                                   'subscription',
                         'default' => '1',
                       },
    'recur_method'  => { 'name' => 'Recurring fee method',
                         #'type' => 'radio',
                         #'options' => \%recur_method,
                         'type' => 'select',
                         'select_options' => \%FS::part_pkg::recur_Common::recur_method,
                       },

    #false laziness w/voip_cdr.pm
    'output_format' => { 'name' => 'CDR invoice display format',
                         'type' => 'select',
                         'select_options' => { FS::cdr::invoice_formats() },
                         'default'        => 'simple2', #XXX test
                       },

    'usage_section' => { 'name' => 'Section in which to place separate usage charges',
                       },

    'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
                          'type' => 'checkbox',
                        },

    'usage_mandate' => { 'name' => 'Always put usage details in separate section',
                          'type' => 'checkbox',
                       },
    #eofalse

  },
                       #cdr_column
  'fieldorder' => [ qw( recur_temporality recur_method cutoff_day ),
                    FS::part_pkg::prorate_Mixin::fieldorder, 
                    qw(
                       output_format usage_section summarize_usage usage_mandate
                    ),
                  ],

  'weight' => 48,

);

sub calc_recur {
  my $self = shift;
  my($cust_pkg, $sdate, $details, $param ) = @_;

  #my $last_bill = $cust_pkg->last_bill;
  my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup

  return 0
    if $self->recur_temporality eq 'preceding'
    && ( $last_bill eq '' || $last_bill == 0 );

  # termination calculations

  my $term_percent = $cust_pkg->cust_main->cdr_termination_percentage;
  die "no customer termination percentage" unless $term_percent;

  my $output_format = $self->option('output_format', 'Hush!') || 'simple2';

  my $charges = 0;

  #find an svc_external record
  my @svc_external = map { $_->svc_x }
                     $cust_pkg->cust_svc_unsorted( svcdb=>'svc_external' );

  die "cdr_termination package has no svc_external service"
    unless @svc_external;
  die "cdr_termination package has multiple svc_external services"
    if scalar(@svc_external) > 1;

  my $svc_external = $svc_external[0];

  # find CDRs:
  # - matching our customer via svc_external.id/title?  (and via what field?)

  #let's try carrierid for now, can always make it configurable or rewrite
  my $cdr_column = 'carrierid';

  my %hashref = ( 'freesidestatus' => 'done' );

  # try matching on svc_external.id for now... (or title?  if ints don't cut it)
  $hashref{$cdr_column} = $svc_external[0]->id; 

  # - with no cdr_termination.status

  my $termpart = 1; #or from an option

  #false lazienss w/search/cdr.html (i should be a part_termination method)
  my $where_term =
    "( cdr.acctid = cdr_termination.acctid AND termpart = $termpart ) ";
  #my $join_term = "LEFT JOIN cdr_termination ON ( $where_term )";
  my $extra_sql =
    "AND NOT EXISTS ( SELECT 1 FROM cdr_termination WHERE $where_term )";

  #may need to process in batches if there's waaay too many
  my @cdrs = qsearch({
    'table'     => 'cdr',
    #'addl_from' => $join_term,
    'hashref'   => \%hashref,
    'extra_sql' => "$extra_sql FOR UPDATE",
  });

  foreach my $cdr (@cdrs) {

    #add a cdr_termination record and the charges

    # XXX config?
    #my $term_price = sprintf('%.2f', $cdr->rated_price * $term_percent / 100 );
    my $term_price = sprintf('%.4f', $cdr->rated_price * $term_percent / 100 );

    my $cdr_termination = new FS::cdr_termination {
      'acctid'      => $cdr->acctid,
      'termpart'    => $termpart,
      'rated_price' => $term_price,
      'status'      => 'done',
    };

    my $error = $cdr_termination->insert;
    die $error if $error; #next if $error; #or just skip this one???  why?

    $charges += $term_price;

    # and add a line to the invoice

    my $call_details = $cdr->downstream_csv( 'format' => $output_format,
                                             'charge' => $term_price,
                                           );

    my $classnum = ''; #usage class?

    #option to turn off?  or just use squelch_cdr for the customer probably
    push @$details, [ 'C', $call_details, $term_price, $classnum ];

  }
    
  # eotermiation calculation

  $charges += ($cust_pkg->quantity || 1) * $self->calc_recur_Common(@_);

  $charges;
}

sub is_free { 0; }

sub can_usageprice { 0; }

1;