Data-BT-PhoneBill

 view release on metacpan or  search on metacpan

lib/Data/BT/PhoneBill.pm  view on Meta::CPAN


Any rebates applied to the call.

=cut

use strict;
use Text::CSV_XS;
use IO::File;

use overload 
 '<>' => \&next_call,
 fallback => 1;

sub new {
  my ($class, $file) = @_;
  my $fh = new IO::File $file, "r"  or die "Can't read $file: $!\n";
  my $headers;
  # Downloads now have blank lines at the top
  while (($headers = <$fh>) !~ /Date/) {
    die "Couldn't find header line" if eof($fh);
  }
  bless {
    _fh => $fh,
    _parser => Text::CSV_XS->new,
  }, $class;
}

sub _fh  { shift->{_fh}     }
sub _csv { shift->{_parser} }

sub next_call {
  my $self = shift;
  my $fh = $self->_fh;
  my $line = <$fh>;
  return unless defined $line;
  if ($self->_csv->parse($line)) {
    return Data::BT::PhoneBill::_Call->new($self->_csv->fields)
  } else {
    warn "Cannot parse: " . $self->_csv->error_input . "\n";
    return;
  }
}

# ==================================================================== #

package Data::BT::PhoneBill::_Call;

use Date::Simple;

#ChargeCode,InstallationNo,LineNo,ChargeCardNo,Date,Time,Destination,CalledNo,Duration,TxtDirectRebate,Cost
our @fields = qw(type installation line chargecard _date time destination 
    _number _duration rebate _cost);

for my $f (@fields) {
    no strict 'refs';
    *{$f} = sub { shift->{$f} };
}

sub new {
  my ($class, @data) = @_;
  bless { map { $fields[$_] => $data[$_] } 0..$#fields } => $class;
}

sub date {
  my @parts = split /\//, shift->_date;
  return Date::Simple->new(@parts[2,1,0]);
}

sub number {
  my $num = shift->_number;
  $num =~ s/\s+$//; $num;
}

sub duration { 
  my ($h, $m, $s) = split /:/, shift->_duration;
  return ($h * 60 * 60) + ($m * 60) + $s;
}

sub cost { shift->_cost * 100 }

1;

=head1 AUTHOR

Tony Bowden, with improvements from Simon Cozens 

=head1 BUGS and QUERIES

Please direct all correspondence regarding this module to:
  bug-Data-BT-PhoneBill@rt.cpan.org

=head1 COPYRIGHT AND LICENSE

  Copyright (C) 2001-2005 Tony Bowden.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License; either version
  2 of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=head1 SEE ALSO

The Perl.com article on Class::DBI is based around Data::BT::PhoneBill - 
  http://www.perl.com/pub/a/2002/11/27/classdbi.html

=cut

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.928 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )