# Copyright (c) 2007-2011 bivio Software Artisans, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library 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. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: IIF.pm,v 1.1 2011/10/07 23:37:42 nagler Exp $ package Bivio::Biz::IIF; use strict; use Bivio::Base 'Bivio.UNIVERSAL'; use Bivio::Type::Date; use Bivio::Type::Amount; my($_IDI) = __PACKAGE__->instance_data_index; my($_D) = b_use('Type.Date'); my($_DT) = b_use('Type.DateTime'); my($_A) = b_use('Type.Amount'); my($_EOL) = "\r\n"; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.1 $ =~ /\d+/g); sub new { my($proto, $def) = @_; my($self) = shift->SUPER::new; $self->[$_IDI] = { def => $def, rows => [], }; return $self; } sub add_record { my($self, $type, $record) = @_; # destroys record contents my($fields) = $self->[$_IDI]; my($def) = grep($_->[0] eq $type, @{$fields->{def}}); Bivio::Die->die('invalid type: ', $type) unless $def; $record->{$type} = $type; push(@{$fields->{rows}}, [ map(exists($record->{$_}) ? _parse($self, $_, delete($record->{$_})) : '', @$def), ]); Bivio::Die->die('left-over fields: ', $record) if %$record; return; } sub to_string { my($self) = @_; my($fields) = $self->[$_IDI]; my($str) = @{$fields->{rows}} ? _out($self, $fields->{def}, '!') . _out($self, $fields->{rows}, '') : ''; return \$str; } sub _out { my($self, $rows, $prefix) = @_; return join($_EOL, map($prefix. join("\t", @$_), @$rows)) . $_EOL; } sub _parse { my($self, $type, $value) = @_; return '' unless defined($value); Bivio::Die->die('value contains a invalid character: ', $value) if $value =~ /\t|\r|\n/; # not YEARTODATE if ($type =~ /^(DATE|SERVICEDATE|DUEDATE|SHIPDATE)$/) { return $_D->to_string($value) if $_DT->is_date($value); return sprintf('%02d/%02d/%04d', ($_DT->local_to_parts($value))[4, 3, 5]); } elsif ($type =~ /^(AMOUNT|QNTY|PRICE)$/) { return $_A->to_literal($_A->from_literal_or_die($value)), } $value =~ s/"//g; return $value; } 1;