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