Annotation of RT/Invoicing/rt_invoices.pl, Revision 1.25
1.1 andrew 1: #!/usr/bin/perl
1.25 ! andrew 2: # $AFresh1: rt_invoices.pl,v 1.24 2011/04/12 19:32:15 andrew Exp $
1.11 andrew 3: ########################################################################
4: # Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>
5: #
6: # Permission to use, copy, modify, and distribute this software for any
7: # purpose with or without fee is hereby granted, provided that the above
8: # copyright notice and this permission notice appear in all copies.
9: #
10: # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: ########################################################################
1.1 andrew 18: use strict;
19: use warnings;
20:
21: use 5.010;
22:
23: use Template;
24: use RT::Client::REST;
25: use RT::Client::REST::Ticket;
26: use RT::Client::REST::User;
27:
1.18 andrew 28: use File::Path;
1.1 andrew 29: use DateTime;
30:
31: my $config = RTI::Config->new();
1.6 andrew 32: my $state = RTI::State->new( $config->get('state') );
1.1 andrew 33:
1.6 andrew 34: my $rt_conf = $config->get('rt');
35: my $rt = RT::Client::REST->new(
36: server => $rt_conf->{server},
37: timeout => $rt_conf->{timeout},
38: );
39: my $tickets = RT::Client::REST::Ticket->new( rt => $rt );
1.1 andrew 40:
1.6 andrew 41: $rt->login( username => $rt_conf->{user}, password => $rt_conf->{pass} );
42:
1.22 andrew 43: #use YAML;
1.6 andrew 44: #print Dump $config, $state; exit;
1.1 andrew 45:
46: my $startdate;
1.3 andrew 47:
1.6 andrew 48: my $customers = $config->get('customers');
1.1 andrew 49:
50: my @limits = map +{
51: attribute => 'status',
52: operator => '=',
53: value => $_,
54: aggregator => 'or',
55: },
56:
57: # XXX This should be a config option
58: qw/ open stalled resolved /;
59:
60: if ($startdate) {
61: push @limits,
62: {
63: attribute => 'last_updated',
64: operator => '>=',
65: value => $startdate->ymd,
66: };
67: }
68:
69: my $results = $tickets->search(
70: limits => \@limits,
71: orderby => 'id',
72: );
73:
74: my $count = $results->count;
75: print "There are $count results that matched your query\n";
76:
77: my $iterator = $results->get_iterator;
78: while ( my $ticket = &$iterator ) {
1.25 ! andrew 79: my $cust = find_customer_for_ticket( $ticket );
1.6 andrew 80: if ( !$cust ) {
1.10 andrew 81: warn "No customer found for ticket " . $ticket->id;
1.7 andrew 82: next;
83: }
1.25 ! andrew 84:
! 85: say 'Ticket ' . $ticket->id . " belongs to $cust->{id}";
! 86:
! 87: $cust->{invoice} ||= make_invoice($cust);
! 88: if ( !$cust->{invoice}->{end} ) {
! 89: say " $cust->{id} has no open invoices [" . $ticket->id . ']';
1.4 andrew 90: next;
91: }
1.1 andrew 92:
1.7 andrew 93: my $project = make_project( $ticket, $cust );
1.6 andrew 94: next unless @{ $project->{fees} } || @{ $project->{expenses} };
1.1 andrew 95:
1.6 andrew 96: foreach my $fee ( @{ $project->{fees} } ) {
1.25 ! andrew 97: my $hours = hours_for_date( $cust->{invoice}, $fee->{date} );
1.1 andrew 98:
1.3 andrew 99: my $h_type
1.4 andrew 100: = exists $hours->{ $fee->{type} }
101: ? $fee->{type}
1.1 andrew 102: : 'default';
103:
1.4 andrew 104: next unless exists $hours->{$h_type} && $hours->{$h_type} > 0;
1.1 andrew 105:
106: my $discount_time = 0;
1.4 andrew 107: if ( $hours->{$h_type} > $fee->{count} ) {
108: $hours->{$h_type} -= $fee->{count};
109: $discount_time = $fee->{count};
1.1 andrew 110: }
111: else {
1.3 andrew 112: $discount_time = $hours->{$h_type};
113: $hours->{$h_type} = 0;
1.1 andrew 114: }
115:
116: if ($discount_time) {
1.25 ! andrew 117: $cust->{invoice}->{discount}->{amount}
1.15 andrew 118: += round( $discount_time * $fee->{rate} );
1.25 ! andrew 119: $cust->{invoice}->{discount}->{hours}{$h_type} += $discount_time;
1.4 andrew 120:
121: $h_type = '' if $h_type eq 'default';
122: $fee->{detail} = "$discount_time $h_type Hours Discounted";
1.1 andrew 123: }
124: }
125:
1.25 ! andrew 126: push @{ $cust->{invoice}->{projects} }, $project;
1.1 andrew 127: }
128:
1.25 ! andrew 129: foreach my $cust ( @{$customers} ) {
1.6 andrew 130: my $invoice = $cust->{invoice};
1.25 ! andrew 131: next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} };
1.1 andrew 132:
1.25 ! andrew 133: my %li = ( custid => $cust->{id}, invdate => DateTime->now->ymd, );
1.7 andrew 134:
1.1 andrew 135: foreach my $project ( @{ $invoice->{projects} } ) {
1.6 andrew 136: if ( $project->{transactions} ) {
1.7 andrew 137: push @{ $li{transactions} }, @{ $project->{transactions} };
1.6 andrew 138: }
1.1 andrew 139: my $subtotal = 0;
140: foreach my $fee ( @{ $project->{fees} } ) {
141: my $amount = round( $fee->{count} * $fee->{rate} );
142: $subtotal += $amount;
143: }
144: foreach my $expense ( @{ $project->{expenses} } ) {
145: $subtotal += round( $expense->{amount} );
146: }
147: $project->{total} = $subtotal;
148: $invoice->{total} += $subtotal;
149: }
150:
151: if ( $invoice->{discount} ) {
152: my $c = "Included Hours\n";
153: if ( $invoice->{discount}{hours} ) {
154: foreach my $t ( keys %{ $invoice->{discount}{hours} } ) {
155: $c .= "\n$invoice->{discount}{hours}{$t} $t hour";
156: $c .= 's' if $invoice->{discount}{hours}{$t} != 1;
157: $c .= "\n";
158: }
159: }
160: $invoice->{discount}{contents} = $c;
161: $invoice->{total} -= round( $invoice->{discount}{amount} );
162: }
163:
164: if ( $invoice->{past_due} ) {
165: $invoice->{total_due} = $invoice->{total} + $invoice->{past_due};
1.3 andrew 166: }
167:
1.9 andrew 168: next unless $invoice->{total} > 0 || $invoice->{total_due};
169:
1.6 andrew 170: $invoice->{info} = $config->get('info');
1.19 andrew 171: my $from = $config->get('from');
172: $from = get_user($from) if !ref $from;
173:
174: $invoice->{organization} = $from->{organization} || $from->{name};
1.21 andrew 175: $invoice->{from} = make_address($from);
1.25 ! andrew 176: $invoice->{to} = make_address( $cust->{address} || $cust->{id} );
1.6 andrew 177:
178: $state->{lastinvoice}++;
1.18 andrew 179: $invoice->{id} = $state->{lastinvoice};
1.17 andrew 180: $invoice->{file} = sprintf 'invoice_%06d.pdf', $state->{lastinvoice};
1.22 andrew 181: $invoice->{logo} = $config->get('logo');
1.6 andrew 182:
1.10 andrew 183: foreach my $k (qw/ file transactions start end total past_due total_due /)
184: {
185: my $v;
186: if ( $invoice->{$k} ) { $v = $invoice->{$k} }
187: elsif ( $cust->{$k} ) { $v = $cust->{$k} }
188:
1.12 andrew 189: if ( defined $v && length $v ) {
1.10 andrew 190: if ( ref $v eq 'DateTime' ) {
191: $li{$k} = $v->ymd;
192: }
193: else {
194: $li{$k} = $v;
195: }
196: }
197: }
198: $state->{invoice}->{ $li{end} }{ $invoice->{id} } = \%li;
199:
1.3 andrew 200: foreach my $key (qw/ start end /) {
201: if ( exists $invoice->{$key} ) {
202: $invoice->{$key} = $invoice->{$key}->strftime('%B %d, %Y');
203: }
1.1 andrew 204: }
1.18 andrew 205:
206: my $invoice_dir = $config->get('invoice_dir');
207: File::Path::make_path($invoice_dir);
208: my $file = join '/', $invoice_dir, $invoice->{file};
209:
1.21 andrew 210: my $tt = Template->new( INCLUDE_PATH => $config->get('template_dir'), )
211: || die $Template::ERROR, "\n";
1.20 andrew 212:
213: $tt->process( $config->get('invoice_template'), $invoice, $file )
1.1 andrew 214: or die $tt->error . "\n";
215:
1.25 ! andrew 216: printf "Generated %s for %s: \$%.02f\n", $invoice->{file}, $cust->{id},
1.9 andrew 217: $invoice->{total};
1.1 andrew 218: }
219:
1.6 andrew 220: $state->save;
1.1 andrew 221:
222: sub round {
223: my ($amount) = @_;
224:
225: #$amount =~ s/\.\d\d\K.*$//;
226: #return $amount;
227: return sprintf "%.02f", $amount;
1.4 andrew 228: }
229:
1.6 andrew 230: sub find_customer_for_ticket {
1.25 ! andrew 231: my ($ticket) = @_;
1.4 andrew 232:
1.25 ! andrew 233: foreach my $cust ( @{$customers} ) {
1.22 andrew 234: next unless $cust->{match};
1.6 andrew 235: foreach my $m ( @{ $cust->{match} } ) {
1.4 andrew 236: my $type = $m->{type};
1.22 andrew 237: my $match = exists $m->{$type} ? lc($m->{$type}) : qr/\Q$m->{regex}\E/;
238: my $thing = [ map { lc } $ticket->$type ];
1.4 andrew 239:
1.22 andrew 240: if (! $match) {
1.4 andrew 241: warn "Invalid match!";
1.22 andrew 242: next;
1.4 andrew 243: }
1.22 andrew 244: return $cust if ($match ~~ $thing);
1.4 andrew 245: }
1.6 andrew 246: }
247:
1.25 ! andrew 248: # Fake customer if we didn't find one
1.16 andrew 249: my $cust = $config->get('default') || {};
1.8 andrew 250:
1.16 andrew 251: ( $cust->{id} ) = $ticket->requestors;
252: return unless $cust->{id};
1.25 ! andrew 253: push @{$customers}, $cust;
1.8 andrew 254:
1.14 andrew 255: $cust->{match} = [
256: { type => 'requestors',
1.16 andrew 257: regex => $cust->{id},
1.14 andrew 258: }
259: ];
1.13 andrew 260:
1.8 andrew 261: return $cust;
262: }
263:
1.9 andrew 264: sub get_user {
1.8 andrew 265: my ($id) = @_;
266:
1.9 andrew 267: state %users;
268: return $users{$id} if $users{$id};
1.8 andrew 269:
270: my %map = (
271: address_one => 'addr1',
272: address_two => 'addr2',
273: email_address => 'email',
1.9 andrew 274: real_name => 'name',
275: name => 'username',
1.8 andrew 276: );
277:
1.12 andrew 278: my $users = RT::Client::REST::User->new( rt => $rt, id => $id );
1.8 andrew 279: $users->retrieve;
280:
1.9 andrew 281: my %user;
1.8 andrew 282: foreach my $m ( keys %{ $users->_attributes } ) {
283: next unless $users->can($m);
284:
285: my $v = $users->$m;
286: next unless $v;
287:
288: $m = $map{$m} if exists $map{$m};
289:
1.9 andrew 290: $user{$m} = $v;
1.8 andrew 291: }
1.6 andrew 292:
1.9 andrew 293: $users{$id} = \%user;
294: return \%user;
1.13 andrew 295: }
296:
297: sub make_invoice {
1.14 andrew 298: my ($cust) = @_;
299:
1.21 andrew 300: my $day = $cust->{day} ||= 0;
1.16 andrew 301: my $per = $cust->{per} ||= 'week';
302: my $freq = $cust->{frequency} ||= 1;
1.14 andrew 303:
1.16 andrew 304: my $day_method;
305: given ($per) {
306: when ('week') { $per = 'weeks'; $day_method = 'dow' }
307: when ('month') { $per = 'months'; $day_method = 'day' }
308: default { die "Unknown per [$per]\n" }
309: }
1.13 andrew 310:
1.24 andrew 311: my $billends = DateTime->now
312: ->subtract( days => 1 )
313: ->set( hour => 23, minute => 59, second => 59 );
1.16 andrew 314:
315: # XXX This is helpful, but monthly and billday > 28 == !!!
1.21 andrew 316: $billends->subtract( days => 1 )
317: while $day && $billends->$day_method != $day;
1.16 andrew 318:
1.24 andrew 319: my $date = $billends->clone->subtract( $per => $freq )
320: ->set( hour => 0, minute => 0, second => 0 );
1.16 andrew 321:
322: my %invoice = ( end => $billends->clone->subtract( seconds => 1 ) );
323:
324: my $lastinvoice = $state->last_invoice( $cust->{id} );
325: if ( $lastinvoice->{date} ) {
326: my $last_invoice_date = ymd_to_DateTime( $lastinvoice->{date} );
327: $date = $last_invoice_date->clone->add( days => 1 );
328:
329: $invoice{start} = $date->clone;
330:
331: $startdate = $date->clone->subtract( $per => $freq )
332: if !$startdate || $startdate > $date;
333: }
334:
1.23 andrew 335: # Is the start date more than $freq $per before the end date?
336: my $diff = $billends - $date;
1.25 ! andrew 337: return {} if $diff->in_units($per) < 1;
1.16 andrew 338:
339: if ( $cust->{base_rate} ) {
340: my ( $project, $hours ) = make_base_project(
341: $cust,
342: { startdate => $date,
343: enddate => $billends,
344: per => $per,
345: freq => $freq,
346: day => $day,
347: day_method => $day_method,
348: }
349: );
350:
351: if ( @{ $project->{fees} } ) {
352: $invoice{end} = $project->{end};
353: $invoice{hours} = $hours;
354: push @{ $invoice{projects} }, $project;
355: }
356: }
357: elsif ( $cust->{hours} ) {
1.14 andrew 358: $invoice{hours} = [
1.16 andrew 359: { end => $invoice{end}->clone,
1.14 andrew 360: hours => $cust->{hours},
361: }
362: ];
363: }
1.13 andrew 364:
1.25 ! andrew 365: return {} if $invoice{start} && $invoice{end} < $invoice{start};
1.16 andrew 366:
1.13 andrew 367: return \%invoice;
1.12 andrew 368: }
369:
1.16 andrew 370: sub make_base_project {
371: my ( $cust, $args ) = @_;
372:
373: my $date = $args->{startdate};
374: my $enddate = $args->{enddate};
375: my $per = $args->{per};
376: my $freq = $args->{freq};
377: my $day = $args->{day};
378: my $day_method = $args->{day_method};
379:
380: my $title
381: = $freq == 1
382: ? ucfirst( $cust->{per} . 'ly' )
383: : $freq . ' ' . ucfirst( $cust->{per} );
384: $title .= ' Retainer';
385:
386: my %project = ( title => $title, start => $date->clone, fees => [], );
387: my @hours;
388:
389: while ( $date < $enddate ) {
390: my $start = $date->clone;
391:
392: $date->add( $per => $freq );
393: $date = $enddate->clone if $date > $enddate;
394:
395: # XXX This is helpful, but monthly and billday > 28 == !!!
1.21 andrew 396: $date->subtract( days => 1 ) while $day && $date->$day_method != $day;
1.16 andrew 397:
398: my $end = $date->clone->subtract( seconds => 1 );
399:
400: last if $end < $start;
401:
402: $project{end} = $end->clone;
403:
404: push @{ $project{fees} },
405: {
406: count => 1,
407: rate => $cust->{base_rate},
408: contents => $start->ymd . ' to ' . $end->ymd,
409: };
410:
411: push @hours,
412: {
413: start => $start->clone,
414: end => $end->clone,
415: hours => { %{ $cust->{hours} } },
416: };
417: }
418:
419: return \%project, \@hours;
420: }
421:
1.12 andrew 422: sub make_address {
423: my ($addr) = @_;
424: my @adr;
1.16 andrew 425:
426: $addr = get_user($addr) unless ref $addr;
1.12 andrew 427:
428: if ( $addr->{organization} ) {
429: push @adr, $addr->{organization};
430: }
431: elsif ( $addr->{name} && !$addr->{attn} ) {
432: push @adr, $addr->{name};
433: }
434:
435: if ( ( $addr->{addr1} || $addr->{addr2} )
436: && $addr->{city}
437: && $addr->{state}
438: && $addr->{zip} )
439: {
440: push @adr, $addr->{attn} if $addr->{attn};
441: push @adr, $addr->{addr1} if $addr->{addr1};
442: push @adr, $addr->{addr2} if $addr->{addr2};
443: push @adr,
444: $addr->{city} . ', ' . $addr->{state} . ' ' . $addr->{zip};
445: }
446: else {
447: push @adr, $addr->{email} if $addr->{email};
448: }
449:
450: return join "\n\n", @adr;
1.6 andrew 451: }
452:
453: sub make_project {
1.7 andrew 454: my ( $ticket, $cust ) = @_;
1.6 andrew 455:
456: my %project = (
457: id => $ticket->id,
458: queue => $ticket->queue,
459: owner => $ticket->owner,
460: title => $ticket->subject,
461: detail => 'Ticket: '
462: . $ticket->id
1.17 andrew 463: . ' Status: '
464: . $ticket->status
1.6 andrew 465: . ' Requestors: '
466: . join( ', ', $ticket->requestors ),
467: fees => [],
468: expenses => [],
469: );
470:
471: my $txns = $ticket->transactions( type => [qw(Comment Correspond)] );
472: my $txn_i = $txns->get_iterator;
473: while ( my $txn = $txn_i->() ) {
474: next unless $txn->time_taken;
475: next if $state->txn_is_invoiced( $txn->id );
476:
1.7 andrew 477: my $fee = make_fee( $txn, $cust->{rates}, $ticket );
478:
1.6 andrew 479: if ( !( $fee->{rate} && $fee->{count} ) ) {
480: warn "Invalid Fee, no rate or count";
481: next;
482: }
483:
1.7 andrew 484: my $invoice = $cust->{invoice};
485: next
486: if $invoice->{start}
487: && $invoice->{start} > $fee->{date};
1.6 andrew 488: next if $invoice->{end} < $fee->{date};
489:
490: push @{ $project{fees} }, $fee;
491: push @{ $project{transactions} }, $txn->id;
1.4 andrew 492: }
1.6 andrew 493:
494: return \%project;
1.4 andrew 495: }
496:
497: sub make_fee {
1.7 andrew 498: my ( $txn, $rates, $ticket ) = @_;
499:
500: # XXX Only need $ticket for the alternate subject
1.4 andrew 501:
502: my $work_time = sprintf "%.03f", $txn->time_taken / 60;
503: my $work_type = $txn->cf('WorkType');
504:
505: my %fee = (
506: id => $txn->id,
507: contents => $txn->created . ' ('
508: . $txn->id . ')' . "\n\n"
509: . ( $txn->data || $ticket->subject ),
510: count => $work_time,
511: type => $work_type,
1.6 andrew 512: date => ymd_to_DateTime( $txn->created ),
1.7 andrew 513: rate => $rates->{$work_type} || $rates->{default} || 0,
1.4 andrew 514: );
515:
516: if ( $work_type && $work_type ne 'Normal' ) {
517: $fee{detail} = $work_type . ' rate';
518: }
519:
520: return \%fee;
521: }
522:
523: sub hours_for_date {
524: my ( $invoice, $date ) = @_;
525:
526: my $hours = {};
527: if ( $invoice->{hours} ) {
528: foreach my $h ( @{ $invoice->{hours} } ) {
529: next if $h->{start} && $h->{start} > $date;
530: next if $h->{end} < $date;
531:
532: $hours = $h->{hours};
533: last;
534: }
535: }
536: return $hours;
1.1 andrew 537: }
538:
1.6 andrew 539: sub ymd_to_DateTime {
540: my ($ymd) = @_;
1.10 andrew 541: my ( $date, $time ) = split /[\sT]/, $ymd;
1.6 andrew 542: my ( $year, $month, $day ) = split '-', $date;
543: my ( $hour, $minute, $second ) = split ':', $time if $time;
544:
545: return DateTime->new(
546: year => $year,
547: month => $month,
548: day => $day,
549: hour => $hour || 0,
550: minute => $minute || 0,
551: second => $second || 0,
552: );
553: }
554:
1.1 andrew 555: package RTI::Config;
556: use strict;
557: use warnings;
558:
559: use 5.010;
560:
1.5 andrew 561: use YAML::Any qw/ LoadFile Dump Load /;
1.22 andrew 562: use File::Spec;
1.1 andrew 563:
564: sub new {
565: my ( $class, $args ) = @_;
566:
567: my $self = { file => '', };
568: bless $self, $class;
1.5 andrew 569:
1.1 andrew 570: my $file = $args->{file} || $self->_find_config;
571: $self->read_config($file);
572:
573: return $self;
574: }
575:
576: sub _find_config {
577: my ($self) = @_;
578:
579: # XXX This needs to be better
580: foreach my $file (qw/ rt_invoice.conf rt_invoice.cfg .rt_invoicerc /) {
1.6 andrew 581: foreach my $dir ( '.', $ENV{HOME} . '/.rt_invoicing', $ENV{HOME} ) {
1.1 andrew 582: my $path = join '/', $dir, $file;
583: return $path if -e $path;
584: }
585: }
586: return;
587: }
588:
589: sub read_config {
590: my ( $self, $file ) = @_;
591:
592: $file ||= $self->{file};
593: die "$file: no such file\n" unless -e $file;
594:
595: my $c = LoadFile($file) or die "Unable to load $file\n";
596:
1.25 ! andrew 597: $c->{customers} ||= [];
1.1 andrew 598: if ( $c->{default} ) {
1.25 ! andrew 599: foreach my $cust ( @{ $c->{customers} } ) {
1.1 andrew 600: foreach my $k ( keys %{ $c->{default} } ) {
1.5 andrew 601: $cust->{$k} //= Load( Dump( $c->{default}->{$k} ) );
1.1 andrew 602: }
603: }
604: }
605:
606: $self->{_config} = $c;
607: $self->{file} = $file;
608: }
609:
610: sub get {
611: my ( $self, $key ) = @_;
1.6 andrew 612: my $value = Load( Dump( $self->{_config}->{$key} ) );
1.18 andrew 613:
614: return $value if $value;
1.22 andrew 615: my ($volume,$directories,$file) =File::Spec->splitpath(
616: File::Spec->rel2abs( $self->{file} ));
1.18 andrew 617:
618: given ($key) {
619: when ('state') {
620: $value = $self->{file};
621: $value =~ s/(?:\.[^.]+)?$/\.state/;
622: }
623: when ('invoice_dir') {
1.22 andrew 624: $value = File::Spec->catdir($volume, $directories, 'invoices' );
1.20 andrew 625: }
626: when ('template_dir') {
1.22 andrew 627: $value = File::Spec->catdir( $volume, $directories );
1.20 andrew 628: }
629: when ('invoice_template') {
630: $value = 'invoice.tex.tt';
1.22 andrew 631: }
632: when ('logo') {
633: $value = File::Spec->catfile($volume, $directories, 'Logo.pdf' );
1.6 andrew 634: }
635: }
1.18 andrew 636:
1.6 andrew 637: return $value;
1.1 andrew 638: }
639:
640: package RTI::State;
641: use strict;
642: use warnings;
643:
644: use 5.010;
645:
646: use YAML::Any qw/ LoadFile DumpFile /;
647:
1.6 andrew 648: my $file = '';
649:
1.1 andrew 650: sub new {
1.6 andrew 651: my $class;
652: ( $class, $file ) = @_;
653:
654: my $self = { lastinvoice => 0, };
655: if ( -e $file ) {
656: $self = LoadFile($file) or die "Unable to load state: $!";
657: }
1.1 andrew 658:
659: bless $self, $class;
1.6 andrew 660:
661: die "Need to pass filename to new: $!" unless $file;
1.1 andrew 662:
663: return $self;
1.6 andrew 664: }
665:
666: sub last_invoice {
667: my ( $self, $custid ) = @_;
668: state %table;
669: if ( !%table ) {
670: foreach my $date ( sort keys %{ $self->{invoice} } ) {
671: while ( my ( $id, $inv ) = each %{ $self->{invoice}->{$date} } ) {
672: next unless $inv->{custid};
673: $table{ $inv->{custid} } = {
674: id => $id,
675: date => $date,
676: %{$inv},
677: };
678: }
679: }
680: }
681: return $table{$custid} || {};
682: }
683:
684: sub txn_is_invoiced {
685: my ( $self, $txn ) = @_;
686: state %table;
687: if ( !%table ) {
688: foreach my $date ( sort keys %{ $self->{invoice} } ) {
689: foreach my $inv ( values %{ $self->{invoice}->{$date} } ) {
690: foreach my $t ( @{ $inv->{transactions} } ) {
691: $table{$t} = 1;
692: }
693: }
694: }
695: }
696: return $table{$txn};
697: }
698:
699: sub save {
700: my ($self) = @_;
701: DumpFile( $file, {%$self} ) or die "Unable to save state: $!";
1.1 andrew 702: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>