[BACK]Return to rt_invoices.pl CVS log [TXT][DIR] Up to [local] / RT / Invoicing

Annotation of RT/Invoicing/rt_invoices.pl, Revision 1.10

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>