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