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

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

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

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