-
Notifications
You must be signed in to change notification settings - Fork 0
/
Budget.pm
154 lines (110 loc) · 3.44 KB
/
Budget.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl
# Application budget IHM
package Budget;
use strict;
use warnings;
use Date::Calc qw(Delta_Days Time_to_Date);
use XML::Simple qw(:strict);
use Encode;
################################################################################
# Variables
################################################################################
################################################################################
# Functions
################################################################################
# Open homebank file to get categories
sub getXML {
my $ref = shift;
my $file = shift;
my $xs = XML::Simple->new();
$ref = $xs->XMLin( $file, ForceArray => 1, KeyAttr => [ ] );
return $ref;
}
# Get categories from homebank XML structure
sub getCategories {
my $ref = shift;
my %cat;
my $cat = $ref->{cat};
foreach my $key ( @$cat ) {
# don't consider top categories
if ( $key->{flags} ne "0" ) {
my $name = Encode::encode_utf8( $key->{name} );
my $k = Encode::encode_utf8( $key->{key} );
$cat{$k} = $name;
}
}
return %cat;
}
# Get operations from homebank XML structure
sub getOperations {
my $ref = shift;
my $cat = shift;
my $operations = [];
my $ope = $ref->{ope};
foreach my $key ( @$ope ) {
my $date = Encode::encode_utf8( $key->{date} );
my $amount = Encode::encode_utf8( $key->{amount} );
my $category = Encode::encode_utf8( $cat->{$key->{category}} );
my $wording = Encode::encode_utf8( $key->{wording} );
push @$operations, {date => $date,
pdate => timestampToDate($date),
amount => $amount,
category => $category,
wording => $wording};
}
return $operations;
}
sub dateToTimestamp {
my $myDate = shift;
my ($year, $month, $day);
my $da;
($day, $month, $year) = split(/\//, $myDate);
return 0 if !defined $day or !defined $month or !defined $year;
return 0 unless $day > 0 and $day < 32;
return 0 unless $month > 0 and $month < 13;
return 0 unless $year > 1900 and $year < 2100;
# Compute operation timestamp
my $days = Delta_Days( 1, 1, 1, $year, $month, $day);
$da = $days + 1;
return $da;
}
sub timestampToDate {
my $ts = shift;
$ts -= Delta_Days( 1, 1, 1, 1970, 1, 1) + 1;
$ts *= 3600 * 24;
my ($year, $month, $day);
($year, $month, $day) = Time_to_Date($ts);
return "$day/$month/$year";
}
sub insertOperation {
my $ref = shift;
my $da = shift;
my $am = shift;
my $ac = shift;
my $ca = shift;
my $wo = shift;
my $ope = {
'wording' => Encode::decode_utf8( $wo ),
'account' => Encode::decode_utf8( $ac ),
'date' => Encode::decode_utf8( $da ),
'dst_account' => '0',
'amount' => Encode::decode_utf8( $am ),
'category' => Encode::decode_utf8( $ca )
};
push @{$ref->{ope}}, $ope;
return $ref;
}
sub writeXML {
my $ref = shift;
my $out = shift;
my $xs = XML::Simple->new();
my $xml = $xs->XMLout( $ref, KeyAttr => [ ], RootName => "homebank",
XMLDecl => '<?xml version="1.0"?>' );
open(my $fh, '>', $out) or die "Could not open file '$out' $!";
print $fh Encode::encode_utf8( $xml );
close $fh;
}
################################################################################
# Entry point
################################################################################
1;