9816ac0b7425f259947971c6bf8df75009cc7d82
[kivitendo-erp.git] / SL / DATEV / KNEFile.pm
1 package SL::DATEV::KNEFile;
2
3 use strict;
4
5 sub new {
6   my $type = shift;
7   my $self = {};
8
9   bless $self, $type;
10
11   $self->_init(@_);
12
13   return $self;
14 }
15
16 sub _init {
17   my $self   = shift;
18   my %params = @_;
19
20   map { $self->{$_} = $params{$_} } keys %params;
21
22   $self->{remaining_bytes} = 250;
23   $self->{block_count}     =   0;
24   $self->{data}            = '';
25 }
26
27 sub get_data {
28   my $self = shift;
29
30   return $self->{data} || '';
31 }
32
33 sub get_block_count {
34   my $self = shift;
35
36   return $self->{block_count};
37 }
38
39 sub add_block {
40   my $self      = shift;
41   my $block     = shift;
42
43   my $block_len = length $block;
44
45
46   $self->flush() if ($block_len > $self->{remaining_bytes});
47
48   $self->{data}            .= $block;
49   $self->{remaining_bytes} -= $block_len;
50
51   return $self;
52 }
53
54 sub flush {
55   my $self = shift;
56
57   if (250 == $self->{remaining_bytes}) {
58     return $self;
59   }
60
61   my $num_zeros             = 6 + $self->{remaining_bytes};
62   $self->{data}            .= "\x00" x $num_zeros;
63
64   $self->{remaining_bytes}  = 250;
65   $self->{block_count}++;
66
67   return $self;
68 }
69
70 sub format_amount {
71   my $self   = shift;
72   my $amount = shift;
73   my $width  = shift;
74
75   $amount =~ s/-//;
76   my ($places, $decimal_places) = split m/\./, "$amount";
77
78   $places          *= 1;
79   $decimal_places ||= 0;
80
81   if (0 < $width) {
82     $width  -= 2;
83     $places  = sprintf("\%0${width}d", $places);
84   }
85
86   $decimal_places .= '0' if (2 > length $decimal_places);
87   $amount          = $places . substr($decimal_places, 0, 2);
88   $amount         *= 1 if (!$width);
89
90   return $amount;
91 }
92
93 1;