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