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