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