File: dfinvoice-future

package info (click to toggle)
pilot-qof 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,348 kB
  • ctags: 684
  • sloc: sh: 10,533; ansic: 4,891; xml: 1,814; perl: 1,768; makefile: 374; sql: 8
file content (285 lines) | stat: -rwxr-xr-x 8,978 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/perl

#  This package is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 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.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

use POSIX qw(locale_h); # for setlocale
use strict;
use warnings;
use XML::QOFQSF qw(QSFParse QSFWrite);
use Date::Parse;
use Date::Format;
use Data::Dumper;
use Config::Auto;
use File::HomeDir;
use Number::Format;
use Text::FormatTable;
use Locale::gettext;
use vars qw / $amount $year $date $month $expenses 
$basefile $category $mileage %rates %branches $time
$template $filter @dates $datestr /;

# depends on pilot-qof

setlocale(LC_MESSAGES, "");
textdomain("datafreedom");
my $home = File::HomeDir->my_home;
# safeguard, just in case.
$home = "/tmp" if (!$home);
my $path =  "$home/.datafreedom";
mkdir $path if (! -d $path);
$mileage = 0;
my $cfile = $path . "/invoice.conf";
if (! -f $cfile)
{
	open (CFG, ">$cfile");
	print CFG _g("\# config file for datafreedom-perl invoices\n");
	print CFG _g("\# used by dfinvoice*\n");
	print CFG _g("\# use a [General] section:\n");
	print CFG "\# [General]\n";
	print CFG "\# category=mybusiness\n";
	print CFG "\# basefile=/home/user/pilot/data.xml\n";
	print CFG _g("\# Use one section per branch/location.\n");
	print CFG "\# [Acacia Avenue]\n";
	print CFG "\# code=1234\n";
	print CFG "\# rate=24\n";
	print CFG _g("\# Omit the currency symbols when specifying the rate\n");
	print CFG _g("\# and use . as the decimal point (i.e. locale C).\n");
	print CFG _g("\# e.g. for a rate of \$15.45\n");
	print CFG _g("\# rate=15.45\n");
	print CFG "[General]\n";
	print CFG "category=\n";
	print CFG "basefile=\n";
	print CFG "mileage=0\n";
	print CFG "default_rate=0\n";
	print CFG "default_code=0\n";
	close (CFG);
	die _g("Cannot create config file %s"). $cfile . ": $!\n" if (! -f $cfile);
}

my $config = Config::Auto::parse("$cfile", format => "ini");
$mileage = $config->{'General'}->{'mileage'}
	if (exists $config->{'General'}->{'mileage'});
$category = $config->{'General'}->{'category'}
	if (exists $config->{'General'}->{'category'});
$basefile = $config->{'General'}->{'basefile'}
	if (exists $config->{'General'}->{'basefile'});
if ((not defined $basefile) or (not -f $basefile))
{
	die "No basefile defined in $cfile.\n";
}
%rates = ();
%branches = ();
foreach my $f (sort keys %$config)
{
	next if ($f eq 'General');
	$rates{$f} = (defined $config->{$f}->{'rate'}) ? 
		$config->{$f}->{'rate'} : $config->{'General'}->{'default_rate'};
	$branches{$f} = (defined $config->{$f}->{'code'}) ?
		$config->{$f}->{'code'} : $config->{'General'}->{'default_code'};
}

$time = time;
my $advance = 1;

while( @ARGV ) {
	$_= shift( @ARGV );
	last if m/^--$/;
	if (!/^-/) {
		unshift(@ARGV,$_);
		last;
	}
	elsif (/^(-\?|-h|--help|--version)$/) {
		print _g("-m|--month - numbers of months to advance.");
		exit( 0 );
	}
	elsif (/^(-m|--month)$/) {
		$advance = shift;
	}
}

my $advmonth = `LC_ALL=C date -d+${advance}month +"%m"`;
chomp ($advmonth);
my $advyear = `LC_ALL=C date -d+${advance}month +"%Y"`;
chomp ($advyear);
$month = $advmonth;
$year = $advyear;
$template = "%Y-%m";
$filter = "${year}-${month}";
my $tmp = `mktemp /tmp/invoice.XXXXXX`;
chomp ($tmp);
my $catstr = ($category ne '') ? "-c $category" : "";
system ("pilot-qof $catstr -i $basefile -t $filter -d pilot_datebook -w $tmp");
my %obj = QSFParse("$tmp");
my $datebook = $obj{'pilot_datebook'};
%obj = ();
my %hdates=();
foreach my $d (@$datebook)
{
	$template = "%Y-%m-%d";
	my $date = time2str($template, $d->start_time);
	$hdates{$date} = $d->start_time;
}

@dates = sort keys %hdates;
printf _g("Found %d working days in %s.\n"), scalar @dates, $filter;
unlink ($tmp);
my %payment = ();
my %invoiced = ();
my $template = "%A, %o %B %Y";
my $x = new Number::Format;
my $grand = 0;
my $table = Text::FormatTable->new('|20l|5l|12l|34l|10r|');
print "\n";
$table->rule('-');
$table->head(_g("Branch Name"), "#", _g("Date"), _g("Details"), _g("Amount"));
my $total_amount = 0;
$table->rule('=');
my %db = ();
my %guid=();
foreach my $d (@$datebook)
{
	$template = "%Y-%m-%d";
	$date = time2str($template, $d->start_time);
	$template = "%a %e %b";
	$datestr = time2str($template, $d->start_time);
	my $hours = ($d->end_time - $d->start_time)/(60*60);
	next if (exists $guid{$d->guid});
	$guid{$d->guid}++;
	my $thisrate = (exists $rates{$d->description}) ?
		$rates{$d->description} : $config->{'General'}->{'default_rate'};
	# bug - description needs 0.05
	$total_amount += $thisrate * $hours;
	$amount = $x->format_price(($thisrate * $hours), 2, 'CURRENCY_SYMBOL');
	$thisrate = $x->format_price($thisrate, 2, 'CURRENCY_SYMBOL');
	chomp ($amount);
	my %c = ('description', $d->description,
		'date', $datestr,
		'hours', $hours,
		'rate', $thisrate,
		'amount', $amount,);
	$db{$d->start_time} = \%c;
}
foreach my $d (sort keys %db)
{
	my $code = (not exists $branches{$db{$d}{'description'}}) ?
		"" : $branches{$db{$d}{'description'}};
	$table->row($db{$d}{'description'}, $code,
		$db{$d}{'date'}, $db{$d}{'hours'}." hours at ". $db{$d}{'rate'}.
		" per hour", $db{$d}{'amount'});
}
$amount = $x->format_price($total_amount, 2, 'CURRENCY_SYMBOL');
$table->rule('-');
$table->row(_g("Total"), "", "", _g("(without expenses)"), $amount);
$grand += $total_amount;

$table->rule('-');
print $table->render(20);
$amount = $x->format_price($grand, 2, 'CURRENCY_SYMBOL');
# Translators: vars are: year, month, amount
printf _g("Estimated total for %d-%d: %s"), $year, $month, ": ".$amount."\n\n";

sub _g {
	return gettext(shift);
}

=head1 NAME

dfinvoice-future - parse a QSF XML file show future bookings

=head1 SYNOPSIS

 dfinvoice-future [-m|--month INTEGER]

=head1 Summary

Using a configuration file in the user home directory,
F</home/user/.datafreedom/invoice.conf>, the script reads the
Palm data synchronised using C<pilot-qof --invoice-city>, combines the
data with location and rate details in the configuration file and
outputs a table summarising the invoices and costs.

Example configuration:

 [General]
 mileage=0.30
 default_rate=24
 default_code=0
 basefile=/home/user/pilot-qof/offline.xml
 category=mybusiness

 [Acacia Avenue]
 rate=25
 code=1234

This configuration sets a mileage rate (expenses charged for personal
transport for business purposes, per unit distance) and a default
hourly rate for the work itself. Other expenses are read in from the
C<pilot-qof> data in the F<basefile>. If C<category> is set, the string
is passed to the C<--category> option of C<pilot-qof> to isolate
specific categories of appointments, expenses and contacts from the rest
of your data. (Only one category is supported.)

For places where the rates differ, location-specific sections can be
added - the hourly rate overrides the default rate and the code is
meant to be a shorthand for the location itself. e.g. if Head Office
etc. puts a code in the invoice payment notice to represent the location
or the cost code and similar, to help you identify the appropriate line
of content in the notice itself.

=head1 Issues

The simple table formatting used in the output appears to have a minor
issue with UTF-8 - if the currency symbol for your locale uses UTF-8,
the rows using the symbol will be shortened by one character per use
of the UTF-8 symbol, causing the column markers to be mis-aligned.

=head1 DATAFREEDOM

These scripts developed from the 'pilot-qof' package but now include
support for other packages and formats and will continue to be extended
along the lines of http://www.data-freedom.org/ - liberating user data 
from the application. Therefore, the datafreedom scripts use a 'df' prefix.

The scripts continue to be developed within the pilot-qof CVS until such
time as the scripts are sufficiently cohesive to form a new source package.

Please feel free to contribute any of your own scripts, under the provisions
of the GNU General Public Licence v3 or later, via the QOF-devel mailing list.
http://lists.sourceforge.net/lists/listinfo/qof-devel

=head1 VERSION

Version 0.0.1

=head1 OBJECTS

pilot_expenses is part of pilot-qof.
Can also be used with gpe-expenses - compatibility with the
default SQLite gpe-expenses backend is pending.

L<http://qof.sourceforge.net/>

L<http://pilot-qof.sourceforge.net/>

L<http://gpe-expenses.sourceforge.net/>

=head1 AUTHOR

Neil Williams, C<< <codehelp at debian.org> >>

=head1 BUGS

Please report bugs via the datafreedom-perl package
in the Debian BTS or via the pilot-qof project and the
SourceForge trackers.