fix TeleAPI import (what kind of crack was Christopher smoking that he couldn't fix...
[freeside.git] / FS / FS / cdr / telstra.pm
1 package FS::cdr::telstra;
2
3 use strict;
4 use vars qw( @ISA %info $tmp_mon $tmp_mday $tmp_year );
5 use Time::Local;
6 use FS::cdr;
7
8 # Telstra LinxOnline eBill format
9 #
10
11
12 @ISA = qw(FS::cdr);
13
14 my %cdr_type_of = (
15   'UIR' => 1,
16   #'SER' => 7,
17   #'OCR' => 8,
18 );
19
20 %info = (
21   'name'          => 'Telstra LinxOnline',
22   'weight'        => 215,
23   'header'        => 1,
24   'type'          => 'fixedlength',
25   # Wholesale Usage Information Record format
26   'fixedlength_format' => [ qw(
27     InterfaceRecordType:3:1:3
28     ServiceProviderCode:3:4:6
29     EventUniqueID:24:7:30
30     ProductBillingIdentifier:8:31:38
31     BillingElementCode:8:39:46
32     InvoiceArrangementID:10:47:56
33     ServiceArrangementID:10:57:66
34     FullNationalNumber:29:67:95
35     OriginatingNumber:25:96:120
36     DestinationNumber:25:121:145
37     OriginatingDateTime:18:146:163
38     ToArea:12:164:175
39     UnitQuantityDuration:27:176:202
40     CallTypeCode:3:203:205
41     RecordType:1:206:206
42     Price:15:207:221
43     DistanceRangeCode:4:222:225
44     ClosedUserGroupID:5:226:230
45     ReversalChargeIndicator:1:231:231
46     1900CallDescription:30:232:261
47     Filler:253:262:514
48   )],
49
50   'import_fields' => [
51     sub { # InterfaceRecordType: skip everything except usage records
52       my ($cdr, $field, $conf, $param) = @_;
53       $param->{skiprow} = 1 if !exists($cdr_type_of{$field});
54       $cdr->cdrtypenum(1);
55     },
56     skip(1), # service provider code
57     'uniqueid', # event file instance, sequence number, bill file ID
58              # together these form a unique record ID
59     skip(4), # product billing identifier, billing element, invoice
60              # arrangement, service arrangement
61     parse_phonenum('charged_party'), 
62              # "This is the billable number and represents the 
63              # service number transferred to the Service Provider as a 
64              # result of Product Redirection."
65     parse_phonenum('src'), # OriginatingNumber
66     parse_phonenum('dst'), # DestinationNumber
67     sub { # OriginatingDate and OriginatingTime, two fields in the spec
68       my ($cdr, $date) = @_;
69       $date =~ /^(\d{4})(\d{2})(\d{2})\s*(\d{2}):(\d{2}):(\d{2})$/
70         or die "unparseable date: $date";
71       $cdr->startdate(timelocal($6, $5, $4, $3, $2-1, $1));
72     },
73     skip(1), #ToArea
74     sub { # UnitOfMeasure, Quantity, CallDuration, three fields
75       my ($cdr, $field, $conf, $param) = @_;
76       my ($unit, $qty, $dur) = ($field =~ /^(.{5})(.{13})(.{9})$/);
77       $qty = $qty / 100000; # five decimal places
78       if( $unit =~ /^SEC/ ) {
79         $cdr->billsec($qty);
80         $cdr->duration($qty);
81       }
82       elsif( $unit =~ /^6SEC/ ) {
83         $cdr->billsec($qty*6);
84         $cdr->duration($qty*6);
85       }
86       elsif( $unit =~ /^MIN/ ) {
87         $cdr->billsec($qty*60);
88         $cdr->duration($qty*60);
89       }
90       else {
91         # For now, ignore units that don't convert to time
92         $param->{skiprow} = 1;
93       }
94     },
95     skip(2), # CallTypeCode, RecordType
96     sub { # Price
97       my ($cdr, $price) = @_;
98       $cdr->upstream_price($price / 10000000);
99     },
100     skip(5),
101   ],
102 );
103
104 sub skip {
105   map {''} (1..$_[0])
106 }
107
108 sub parse_phonenum {
109   my $field = shift;
110   return sub {
111     my ($cdr, $data) = @_;
112     my $phonenum;
113     my ($type) = ($data =~ /^(.)/); #network service type
114     if ($type eq 'A') {
115       # domestic number: area code length, then 10-digit number (maybe 
116       # padded with spaces), then extension info if it's the FNN/billable 
117       # number
118       ($phonenum) = ($data =~ /^.\d(.{0,10})/);
119       $phonenum =~ s/\s//g;
120     }
121     elsif ($type eq 'O') {
122       # international number: country code length, then 15-digit number
123       ($phonenum) = ($data =~ /^.\d(.{0,15})/);
124     }
125     else {
126       # other, take 18 characters
127       ($phonenum) = ($data =~ /^.(.{0,18})/);
128     }
129     $cdr->setfield($field, $phonenum);
130   }
131 }
132
133 1;