summaryrefslogtreecommitdiff
path: root/FS/FS/cdr/broadsoft.pm
blob: ab4815095c94229cb3b598c3da03539cc720b323 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package FS::cdr::broadsoft;

=head1 NAME

FS::cdr::broadsoft - CDR parse module for Broadsoft

=head1 DESCRIPTION

Ref: BW-AccountingCDRInterfaceSpec-R22.pdf

=cut

use strict;
use base qw( FS::cdr );
use vars qw( %info );
use FS::cdr qw( _cdr_date_parser_maker _cdr_min_parser_maker );

%info = (
  name     => 'Broadsoft',
  weight   => 500,
  header   => 1,
  type     => 'csv',
  sep_char => ',',
  disabled => 0,

  #deal with broadsoft's awful non-standard CSV escaping :/
  row_callback => sub {
    my $line = shift;
    $line = qq("$line");       # put " at the beginning and end
    $line =~ s/(?<!\\),/","/g; # all commas not after a \ become ","
    $line =~ s/\\,/,/g;        # and now we can turn \, into ,
    #XXX embedded \r and \n ?  none in my test data, and might be better to 
    # leave escaped and deal with it from there?
    $line =~ s/\\\\/\\/g;     # undo double backslashes?  none in my test data

    #and now we have a properly formed CSV line
    $line;
  },

  import_fields => [

    # 1: recordId
    # 2: serviceProvider
    skip(2),

    # 3: type
    sub {
      my ( $cdr, $data, $conf, $param ) = @_;
      $param->{skiprow} = 1
        if lc($data) ne 'normal';
      '';
    },

    # 4: userNumber
    # 5: groupNumber
    skip(2),

    # 6: direction
    'dcontext',

    # 7: callingNumber
    trim('src'),

    # 8: callingPresentationINdicator
    skip(1),

    # 9: calledNumber
    trim('dst'),

    # 10: startTime
    _cdr_date_parser_maker('startdate'),

    # 11: userTimeZone
    skip(1),

    # 12: answerIndicator
    sub {
      my( $cdr, $data ) = @_;
      $cdr->disposition( $data =~ /^yes/i ? 'ANSWERED' : 'NO ANSWER');
    },

    # 13: answerTime
    _cdr_date_parser_maker('answerdate'),

    # 14: releaseTime
    _cdr_date_parser_maker('enddate'),

    # 15: terminationCause
    # 16: networkType
    # 17: carrierIdentificationCode
    # 18: dialedDigits
    # 19: callCategory
    # 20: networkCallType
    # 21: networkTranslatedNumber
    # 22: networkTranslatedGroup
    # 23: releasingParty
    # 24: route
    skip(10),

    # 25: networkCallID
    'sipcallid',

    # 26: codedc
    # 27: accessDeviceAddress
    # 28: accessCallID
    # 29: spare
    # 30: failoverCorrelationId
    # 31: spare
    # 32: group
    # 33: department
    skip(8),

    # 34: accountCode
    'accountcode',

    # 35: authorizationCode
    # 36: originalCalledNumber
    # 37: originalCalledPresentationIndicator
    # 38: originalCalledReason
    # 39: redirectingNumber
    # 40: redirectingPresentationIndicator
    # 41: redirectingReason
    # 42: chargeIndicator
    # 43: typeOfNetwork
    # 44: voicePortalCalling.invocationTime
    # 45: localCallId
    # 46: remoteCallId
    # 47: callingPartyCategory
    #
    # Also... cols 48 - 448 see Broadsoft documentation
    skip(87), #35-121 inclusive

    #122: otherPartyName
    'clid',

    #123: otherPartyNamePresentationIndicator
    sub {
      my( $cdr, $data ) = @_;
      $cdr->clid( $data ) unless $data =~ /^Public$/i;
    },

    skip(22), #124-145 inclusive

    # 146: chargedNumber
    'charged_party',

  ],

);

sub trim {
  my $fieldname = shift;
  return sub {
    my($cdr, $data) = @_;
    $data =~ s/^\+1//;
    $cdr->$fieldname($data);
    ''
  }
}

sub skip {
  map { undef } (1..$_[0]);
}

1;

__END__

list of freeside CDR fields, useful ones marked with *

           acctid - primary key
    *[1]   calldate - Call timestamp (SQL timestamp)
           clid - Caller*ID with text
7   *      src - Caller*ID number / Source number
9   *      dst - Destination extension
           dcontext - Destination context
           channel - Channel used
           dstchannel - Destination channel if appropriate
           lastapp - Last application if appropriate
           lastdata - Last application data
10  *      startdate - Start of call (UNIX-style integer timestamp)
13         answerdate - Answer time of call (UNIX-style integer timestamp)
14  *      enddate - End time of call (UNIX-style integer timestamp)
    *      duration - Total time in system, in seconds
    *      billsec - Total time call is up, in seconds
12  *[2]   disposition - What happened to the call: ANSWERED, NO ANSWER, BUSY
           amaflags - What flags to use: BILL, IGNORE etc, specified on a per
           channel basis like accountcode.
4   *[3]   accountcode - CDR account number to use: account
           uniqueid - Unique channel identifier
           userfield - CDR user-defined field
           cdr_type - CDR type - see FS::cdr_type (Usage = 1, S&E = 7, OC&C = 8)
    *[4]   charged_party - Service number to be billed
           upstream_currency - Wholesale currency from upstream
    *[5]   upstream_price - Wholesale price from upstream
           upstream_rateplanid - Upstream rate plan ID
           rated_price - Rated (or re-rated) price
           distance - km (need units field?)
           islocal - Local - 1, Non Local = 0
    *[6]   calltypenum - Type of call - see FS::cdr_calltype
           description - Description (cdr_type 7&8 only) (used for
           cust_bill_pkg.itemdesc)
           quantity - Number of items (cdr_type 7&8 only)
           carrierid - Upstream Carrier ID (see FS::cdr_carrier)
           upstream_rateid - Upstream Rate ID
           svcnum - Link to customer service (see FS::cust_svc)
           freesidestatus - NULL, done (or something)

[1] Auto-populated from startdate if not present
[2] Package options available to ignore calls without a specific disposition
[3] When using 'cdr-charged_party-accountcode' config
[4] Auto-populated from src (normal calls) or dst (toll free calls) if not present
[5] When using 'upstream_simple' rating method.
[6] Set to usage class classnum when using pre-rated CDRs and usage class-based
    taxation (local/intrastate/interstate/international)