Annotation of RT/Invoicing/rt_invoices.pl, Revision 1.45
1.1 andrew 1: #!/usr/bin/perl
1.45 ! andrew 2: # $AFresh1: rt_invoices.pl,v 1.44 2011/12/30 05:01:41 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:
1.40 andrew 31: use lib './lib'; # XXX This is fragile, there are better ways
32: use RTI::Config;
33: use RTI::State;
1.45 ! andrew 34: use RTI::Util qw/ round ymd_to_DateTime /;
1.40 andrew 35:
1.1 andrew 36: my $config = RTI::Config->new();
1.6 andrew 37: my $state = RTI::State->new( $config->get('state') );
1.1 andrew 38:
1.6 andrew 39: my $rt_conf = $config->get('rt');
40: my $rt = RT::Client::REST->new(
41: server => $rt_conf->{server},
42: timeout => $rt_conf->{timeout},
43: );
44: my $tickets = RT::Client::REST::Ticket->new( rt => $rt );
1.1 andrew 45:
1.6 andrew 46: $rt->login( username => $rt_conf->{user}, password => $rt_conf->{pass} );
47:
1.22 andrew 48: #use YAML;
1.6 andrew 49: #print Dump $config, $state; exit;
1.1 andrew 50:
1.6 andrew 51: my $customers = $config->get('customers');
1.40 andrew 52: my $startdate = set_dates($customers);
1.1 andrew 53:
54: my @limits = map +{
55: attribute => 'status',
56: operator => '=',
57: value => $_,
58: aggregator => 'or',
59: },
60:
61: # XXX This should be a config option
62: qw/ open stalled resolved /;
63:
1.40 andrew 64: push @limits,
65: {
66: attribute => 'last_updated',
67: operator => '>=',
68: value => $startdate->ymd,
69: };
1.1 andrew 70:
71: my $results = $tickets->search(
72: limits => \@limits,
73: orderby => 'id',
74: );
75:
76: my $count = $results->count;
77: print "There are $count results that matched your query\n";
78:
79: my $iterator = $results->get_iterator;
80: while ( my $ticket = &$iterator ) {
1.43 andrew 81: my $cust = find_customer_for_ticket( $ticket, $customers );
1.6 andrew 82: if ( !$cust ) {
1.10 andrew 83: warn "No customer found for ticket " . $ticket->id;
1.7 andrew 84: next;
85: }
1.25 andrew 86:
1.41 andrew 87: next if $cust->{no_invoice};
1.25 andrew 88: $cust->{invoice} ||= make_invoice($cust);
1.44 andrew 89:
90: die "$cust->{id} has no open invoices [" . $ticket->id . ']'
91: unless $cust->{invoice};
1.1 andrew 92:
1.40 andrew 93: say 'Ticket ' . $ticket->id . " belongs to $cust->{id}";
94:
1.7 andrew 95: my $project = make_project( $ticket, $cust );
1.6 andrew 96: next unless @{ $project->{fees} } || @{ $project->{expenses} };
1.1 andrew 97:
1.6 andrew 98: foreach my $fee ( @{ $project->{fees} } ) {
1.25 andrew 99: my $hours = hours_for_date( $cust->{invoice}, $fee->{date} );
1.1 andrew 100:
1.38 andrew 101: my $type = 'unknown';
102: my $count = $fee->{count};
103: while ( $type && $count > 0 && $type ne 'default' ) {
1.35 andrew 104: $type = exists $hours->{ $fee->{type} }
105: && $hours->{ $fee->{type} } > 0 ? $fee->{type} : 'default';
106:
107: next unless exists $hours->{$type} && $hours->{$type} > 0;
108:
109: my $discount_time = 0;
1.38 andrew 110: if ( $hours->{$type} > $count ) {
111: $hours->{$type} -= $count;
112: $discount_time = $count;
1.35 andrew 113: }
114: else {
115: $discount_time = $hours->{$type};
116: $hours->{$type} = 0;
117: }
118:
119: if ($discount_time) {
120: $cust->{invoice}->{discount}->{amount}
121: += round( $discount_time * $fee->{rate} );
1.36 andrew 122: $cust->{invoice}->{discount}->{hours}{$type}
123: += $discount_time;
1.35 andrew 124:
125: $type = '' if $type eq 'default';
1.38 andrew 126: $count -= $discount_time;
1.35 andrew 127: $fee->{detail} .= " $discount_time $type Hours Discounted";
128: }
1.1 andrew 129: }
130: }
131:
1.25 andrew 132: push @{ $cust->{invoice}->{projects} }, $project;
1.1 andrew 133: }
134:
1.44 andrew 135:
136: if ( my $unpaid_invoices = $state->unpaid_invoices() ) {
137: foreach my $custid ( keys %{$unpaid_invoices} ) {
138: my %project = ( title => 'Unpaid Invoices', fees => [], );
139: my $past_due = 0;
140:
141: my $cust;
142: foreach ( @{$customers} ) {
143: if ( $_->{id} eq $custid ) {
144: $cust = $_;
145: last;
146: }
147: }
148: $cust ||= fake_customer($custid);
149:
150: foreach my $id ( sort { $a <=> $b }
151: keys %{ $unpaid_invoices->{$custid} } )
152: {
153: my $unpaid = $state->get_invoice($id);
154: my $invdate = ymd_to_DateTime( $unpaid->{invdate} );
155:
156: next
157: if $cust->{duedate}
158: && DateTime->compare( $invdate, $cust->{duedate} ) > 0;
159:
160: $past_due += $unpaid_invoices->{$custid}->{$id};
161: push @{ $project{fees} },
162: {
163: id => $id,
164: contents =>
165: sprintf( "Invoice %06d from %s", $id, $invdate->ymd ),
166: count => 1,
167: rate => $unpaid_invoices->{$custid}->{$id},
168: };
169: }
170:
171: if ($past_due) {
172: $cust->{invoice} ||= make_invoice();
173:
174: $cust->{invoice}->{past_due} = $past_due;
175: $cust->{invoice}->{total_due}
176: = $cust->{invoice}->{total} + $past_due;
177:
178: unshift @{ $cust->{invoice}->{projects} }, \%project;
179: }
180: }
181: }
182:
1.25 andrew 183: foreach my $cust ( @{$customers} ) {
1.6 andrew 184: my $invoice = $cust->{invoice};
1.25 andrew 185: next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} };
1.1 andrew 186:
1.43 andrew 187: $invoice->{custid} = $cust->{id};
1.40 andrew 188: $invoice->{transactions} = [];
1.7 andrew 189:
1.33 andrew 190: my %transactions;
1.1 andrew 191: foreach my $project ( @{ $invoice->{projects} } ) {
1.6 andrew 192: if ( $project->{transactions} ) {
1.33 andrew 193: %transactions = ( %transactions, %{ $project->{transactions} } );
1.6 andrew 194: }
1.1 andrew 195: my $subtotal = 0;
196: foreach my $fee ( @{ $project->{fees} } ) {
197: my $amount = round( $fee->{count} * $fee->{rate} );
198: $subtotal += $amount;
199: }
200: foreach my $expense ( @{ $project->{expenses} } ) {
201: $subtotal += round( $expense->{amount} );
202: }
203: $project->{total} = $subtotal;
204: $invoice->{total} += $subtotal;
205: }
1.40 andrew 206: @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions;
1.1 andrew 207:
208: if ( $invoice->{discount} ) {
209: my $c = "Included Hours\n";
210: if ( $invoice->{discount}{hours} ) {
211: foreach my $t ( keys %{ $invoice->{discount}{hours} } ) {
1.35 andrew 212: my $type = $t eq 'default' ? '' : $t;
213: $c .= "\n$invoice->{discount}{hours}{$t} $type hour";
1.1 andrew 214: $c .= 's' if $invoice->{discount}{hours}{$t} != 1;
215: $c .= "\n";
216: }
217: }
218: $invoice->{discount}{contents} = $c;
219: $invoice->{total} -= round( $invoice->{discount}{amount} );
220: }
221:
1.9 andrew 222: next unless $invoice->{total} > 0 || $invoice->{total_due};
223:
1.6 andrew 224: $invoice->{info} = $config->get('info');
1.19 andrew 225: my $from = $config->get('from');
226: $from = get_user($from) if !ref $from;
227:
1.40 andrew 228: $invoice->{id} = $state->next_invoice_id;
229: $invoice->{file} = sprintf 'invoice_%06d.pdf', $invoice->{id};
230:
1.19 andrew 231: $invoice->{organization} = $from->{organization} || $from->{name};
1.29 andrew 232: $invoice->{from} = make_address($from);
1.40 andrew 233: $invoice->{to} = make_address( $cust->{address} || $cust->{id} );
234: $invoice->{logo} = $config->get('logo');
1.6 andrew 235:
1.43 andrew 236: $state->add_invoice($invoice);
1.10 andrew 237:
1.18 andrew 238: my $invoice_dir = $config->get('invoice_dir');
239: File::Path::make_path($invoice_dir);
240: my $file = join '/', $invoice_dir, $invoice->{file};
241:
1.21 andrew 242: my $tt = Template->new( INCLUDE_PATH => $config->get('template_dir'), )
243: || die $Template::ERROR, "\n";
1.20 andrew 244:
245: $tt->process( $config->get('invoice_template'), $invoice, $file )
1.1 andrew 246: or die $tt->error . "\n";
247:
1.25 andrew 248: printf "Generated %s for %s: \$%.02f\n", $invoice->{file}, $cust->{id},
1.9 andrew 249: $invoice->{total};
1.40 andrew 250:
1.1 andrew 251: }
252:
1.6 andrew 253: $state->save;
1.1 andrew 254:
1.6 andrew 255: sub find_customer_for_ticket {
1.43 andrew 256: my ( $ticket, $customers ) = @_;
1.4 andrew 257:
1.25 andrew 258: foreach my $cust ( @{$customers} ) {
1.22 andrew 259: next unless $cust->{match};
1.6 andrew 260: foreach my $m ( @{ $cust->{match} } ) {
1.4 andrew 261: my $type = $m->{type};
1.29 andrew 262: my $match
263: = exists $m->{$type}
264: ? lc( $m->{$type} )
265: : qr/\Q$m->{regex}\E/;
266: my $thing = [ map {lc} $ticket->$type ];
1.4 andrew 267:
1.29 andrew 268: if ( !$match ) {
1.4 andrew 269: warn "Invalid match!";
1.22 andrew 270: next;
1.4 andrew 271: }
1.29 andrew 272: return $cust if ( $match ~~ $thing );
1.4 andrew 273: }
1.6 andrew 274: }
275:
1.44 andrew 276: return fake_customer($ticket->requestors);
277: }
278:
279: sub fake_customer {
280: my ($custid) = @_;
281: return unless $custid;
282:
1.40 andrew 283: my $cust = $config->new_customer;
1.25 andrew 284: push @{$customers}, $cust;
1.8 andrew 285:
1.44 andrew 286: $cust->{id} = $custid;
1.14 andrew 287: $cust->{match} = [
288: { type => 'requestors',
1.16 andrew 289: regex => $cust->{id},
1.14 andrew 290: }
291: ];
1.13 andrew 292:
1.40 andrew 293: set_cust_dates($cust);
1.8 andrew 294: return $cust;
295: }
296:
1.9 andrew 297: sub get_user {
1.8 andrew 298: my ($id) = @_;
299:
1.9 andrew 300: state %users;
301: return $users{$id} if $users{$id};
1.8 andrew 302:
303: my %map = (
304: address_one => 'addr1',
305: address_two => 'addr2',
306: email_address => 'email',
1.9 andrew 307: real_name => 'name',
308: name => 'username',
1.8 andrew 309: );
310:
1.12 andrew 311: my $users = RT::Client::REST::User->new( rt => $rt, id => $id );
1.8 andrew 312: $users->retrieve;
313:
1.9 andrew 314: my %user;
1.8 andrew 315: foreach my $m ( keys %{ $users->_attributes } ) {
316: next unless $users->can($m);
317:
318: my $v = $users->$m;
319: next unless $v;
320:
321: $m = $map{$m} if exists $map{$m};
322:
1.9 andrew 323: $user{$m} = $v;
1.8 andrew 324: }
1.6 andrew 325:
1.9 andrew 326: $users{$id} = \%user;
327: return \%user;
1.13 andrew 328: }
329:
330: sub make_invoice {
1.14 andrew 331: my ($cust) = @_;
332:
1.43 andrew 333: my %invoice
334: = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) );
335: $invoice{start} = $cust->{startinvoicedate}->clone
1.40 andrew 336: if $cust->{startinvoicedate};
1.16 andrew 337:
338: if ( $cust->{base_rate} ) {
1.43 andrew 339: my ( $project, $hours ) = make_base_project($cust);
1.16 andrew 340:
341: if ( @{ $project->{fees} } ) {
342: $invoice{end} = $project->{end};
343: $invoice{hours} = $hours;
344: push @{ $invoice{projects} }, $project;
345: }
346: }
347: elsif ( $cust->{hours} ) {
1.14 andrew 348: $invoice{hours} = [
1.16 andrew 349: { end => $invoice{end}->clone,
1.14 andrew 350: hours => $cust->{hours},
351: }
352: ];
353: }
1.13 andrew 354:
355: return \%invoice;
1.12 andrew 356: }
357:
1.16 andrew 358: sub make_base_project {
1.43 andrew 359: my ($cust) = @_;
1.16 andrew 360:
1.40 andrew 361: my $date = $cust->{billstart}->clone;
362: my $billend = $cust->{billend}->clone;
1.43 andrew 363: my ($freq) = get_billing_frequency($cust);
1.16 andrew 364:
365: my $title
1.40 andrew 366: = $cust->{frequency} == 1
1.16 andrew 367: ? ucfirst( $cust->{per} . 'ly' )
368: : $freq . ' ' . ucfirst( $cust->{per} );
369: $title .= ' Retainer';
370:
371: my %project = ( title => $title, start => $date->clone, fees => [], );
372: my @hours;
373:
1.28 andrew 374: while ( $date < $billend ) {
1.16 andrew 375: my $start = $date->clone;
376:
1.43 andrew 377: $date->add($freq);
1.16 andrew 378:
1.40 andrew 379: my $end = $date > $billend ? $billend->clone : $date->clone;
380: $end->subtract( seconds => 1 );
1.16 andrew 381:
382: $project{end} = $end->clone;
383:
384: push @{ $project{fees} },
385: {
386: count => 1,
387: rate => $cust->{base_rate},
388: contents => $start->ymd . ' to ' . $end->ymd,
389: };
390:
391: push @hours,
392: {
393: start => $start->clone,
394: end => $end->clone,
395: hours => { %{ $cust->{hours} } },
396: };
397: }
398:
399: return \%project, \@hours;
400: }
401:
1.12 andrew 402: sub make_address {
403: my ($addr) = @_;
404: my @adr;
1.16 andrew 405:
406: $addr = get_user($addr) unless ref $addr;
1.12 andrew 407:
408: if ( $addr->{organization} ) {
409: push @adr, $addr->{organization};
410: }
411: elsif ( $addr->{name} && !$addr->{attn} ) {
412: push @adr, $addr->{name};
413: }
414:
415: if ( ( $addr->{addr1} || $addr->{addr2} )
416: && $addr->{city}
417: && $addr->{state}
418: && $addr->{zip} )
419: {
420: push @adr, $addr->{attn} if $addr->{attn};
421: push @adr, $addr->{addr1} if $addr->{addr1};
422: push @adr, $addr->{addr2} if $addr->{addr2};
423: push @adr,
424: $addr->{city} . ', ' . $addr->{state} . ' ' . $addr->{zip};
425: }
426: else {
427: push @adr, $addr->{email} if $addr->{email};
428: }
429:
430: return join "\n\n", @adr;
1.6 andrew 431: }
432:
433: sub make_project {
1.7 andrew 434: my ( $ticket, $cust ) = @_;
1.6 andrew 435:
436: my %project = (
437: id => $ticket->id,
438: queue => $ticket->queue,
439: owner => $ticket->owner,
440: title => $ticket->subject,
441: detail => 'Ticket: '
442: . $ticket->id
1.17 andrew 443: . ' Status: '
444: . $ticket->status
1.6 andrew 445: . ' Requestors: '
446: . join( ', ', $ticket->requestors ),
447: fees => [],
448: expenses => [],
449: );
450:
451: my $txns = $ticket->transactions( type => [qw(Comment Correspond)] );
452: my $txn_i = $txns->get_iterator;
453: while ( my $txn = $txn_i->() ) {
1.33 andrew 454: next if $state->txn_is_invoiced( $txn->id );
455:
1.36 andrew 456: if ( my $expense = make_expense( $txn, $ticket ) ) {
1.33 andrew 457: push @{ $project{expenses} }, $expense;
458: $project{transactions}{ $txn->id } = 1;
459: }
460:
1.6 andrew 461: next unless $txn->time_taken;
462:
1.7 andrew 463: my $fee = make_fee( $txn, $cust->{rates}, $ticket );
464:
1.6 andrew 465: if ( !( $fee->{rate} && $fee->{count} ) ) {
466: warn "Invalid Fee, no rate or count";
467: next;
468: }
469:
1.7 andrew 470: my $invoice = $cust->{invoice};
1.29 andrew 471: if ( $invoice->{start} && $invoice->{start} > $fee->{date} ) {
472: warn "Ticket "
473: . $ticket->id
474: . " has uninvoiced Transaction "
475: . $txn->id . "\n";
1.27 andrew 476: next;
477: }
1.6 andrew 478: next if $invoice->{end} < $fee->{date};
479:
1.36 andrew 480: push @{ $project{fees} }, $fee;
1.33 andrew 481: $project{transactions}{ $txn->id } = 1;
1.4 andrew 482: }
1.6 andrew 483:
484: return \%project;
1.4 andrew 485: }
486:
487: sub make_fee {
1.7 andrew 488: my ( $txn, $rates, $ticket ) = @_;
489:
490: # XXX Only need $ticket for the alternate subject
1.4 andrew 491:
492: my $work_time = sprintf "%.03f", $txn->time_taken / 60;
493: my $work_type = $txn->cf('WorkType');
1.34 andrew 494:
495: if ( $work_type =~ s/\s*Onsite//i ) {
1.36 andrew 496:
1.34 andrew 497: # XXX Do something special for onsite activities
498: }
499:
500: $work_type =~ s/^\s+|\s+$//g;
501: $work_type ||= 'Normal';
1.4 andrew 502:
503: my %fee = (
504: id => $txn->id,
505: contents => $txn->created . ' ('
506: . $txn->id . ')' . "\n\n"
507: . ( $txn->data || $ticket->subject ),
508: count => $work_time,
509: type => $work_type,
1.6 andrew 510: date => ymd_to_DateTime( $txn->created ),
1.7 andrew 511: rate => $rates->{$work_type} || $rates->{default} || 0,
1.4 andrew 512: );
513:
514: if ( $work_type && $work_type ne 'Normal' ) {
515: $fee{detail} = $work_type . ' rate';
516: }
517:
518: return \%fee;
1.33 andrew 519: }
520:
521: sub make_expense {
522: my ( $txn, $ticket ) = @_;
523:
524: my $amount = $txn->cf('ExpenseAmount') or return;
525:
526: my %expense = (
527: id => $txn->id,
528: contents => $txn->created . ' ('
529: . $txn->id . ')' . "\n\n"
530: . ( $txn->data || $ticket->subject ),
531: amount => $amount,
532: date => ymd_to_DateTime( $txn->created ),
1.36 andrew 533:
1.33 andrew 534: # detail => ???,
535: );
536:
537: return \%expense;
1.4 andrew 538: }
539:
540: sub hours_for_date {
541: my ( $invoice, $date ) = @_;
542:
543: my $hours = {};
544: if ( $invoice->{hours} ) {
545: foreach my $h ( @{ $invoice->{hours} } ) {
546: next if $h->{start} && $h->{start} > $date;
547: next if $h->{end} < $date;
548:
549: $hours = $h->{hours};
550: last;
551: }
552: }
553: return $hours;
1.6 andrew 554: }
555:
1.40 andrew 556: sub get_billing_frequency {
557: my ($cust) = @_;
558: my $per = $cust->{per} || 'week';
559: my $freq = $cust->{frequency} || 1;
1.1 andrew 560:
1.40 andrew 561: my $day_method;
562: given ($per) {
563: when ('week') { $per = 'weeks'; $day_method = 'dow' }
564: when ('month') { $per = 'months'; $day_method = 'day' }
565: default { die "Unknown per [$per]\n" }
566: }
1.1 andrew 567:
1.43 andrew 568: return DateTime::Duration->new( $per => $freq ), $day_method;
1.1 andrew 569: }
570:
1.40 andrew 571: sub set_dates {
572: my ($customers) = @_;
1.43 andrew 573:
1.40 andrew 574: my $newest_invoice;
575: my $max_duration;
1.1 andrew 576:
1.40 andrew 577: foreach my $cust ( @{$customers} ) {
578: set_cust_dates($cust);
1.1 andrew 579:
1.40 andrew 580: my ($freq) = get_billing_frequency($cust);
581: $max_duration = $freq
582: if !$max_duration
583: || DateTime::Duration->compare( $freq, $max_duration ) > 0;
1.1 andrew 584:
1.40 andrew 585: if ( $cust->{startinvoicedate} ) {
586: my $date = $cust->{startinvoicedate}->clone;
587: $newest_invoice = $date->clone
588: if !$newest_invoice || $newest_invoice < $date;
1.1 andrew 589: }
590: }
591:
1.40 andrew 592: return $newest_invoice->clone->subtract($max_duration)
593: ->subtract( days => 1 );
1.1 andrew 594: }
595:
1.40 andrew 596: sub set_cust_dates {
597: my ($cust) = @_;
1.18 andrew 598:
1.40 andrew 599: my $day = $cust->{day} || 0;
600: my ( $freq, $day_method ) = get_billing_frequency($cust);
1.18 andrew 601:
1.41 andrew 602: my $end = DateTime->now( time_zone => 'local' )
1.40 andrew 603: ->set( hour => 0, minute => 0, second => 0 );
1.18 andrew 604:
1.41 andrew 605: my $start = $end->clone->subtract($freq);
1.1 andrew 606:
1.40 andrew 607: # XXX This is helpful, but monthly and billday > 28 == !!!
1.43 andrew 608: $end->subtract( days => 1 ) while $day && $end->$day_method != $day;
1.1 andrew 609:
1.40 andrew 610: my $lastinvoice = $state->last_invoice( $cust->{id} );
611: if ( $lastinvoice && $lastinvoice->{end} ) {
1.41 andrew 612: $start = ymd_to_DateTime( $lastinvoice->{end} )->add( days => 1 );
613: $cust->{startinvoicedate} = $start->clone;
1.6 andrew 614: }
1.42 andrew 615:
616: $cust->{duedate}
617: = $cust->{net}
618: ? DateTime->now->subtract( days => $cust->{net} )
619: : 0;
1.1 andrew 620:
1.41 andrew 621: $cust->{no_invoice} = 1 if $start->clone->add($freq) > $end;
622: $cust->{billend} = $end;
623: $cust->{billstart} = $start;
1.1 andrew 624: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>