version 1.40, 2011/12/22 04:52:28 |
version 1.42, 2011/12/30 03:20:45 |
|
|
#!/usr/bin/perl |
#!/usr/bin/perl |
# $AFresh1: rt_invoices.pl,v 1.38 2011/06/21 00:20:28 andrew Exp $ |
# $AFresh1: rt_invoices.pl,v 1.41 2011/12/22 05:21:56 andrew Exp $ |
######################################################################## |
######################################################################## |
# Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com> |
# Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com> |
# |
# |
|
|
next; |
next; |
} |
} |
|
|
next if $cust->{no_invoices}; |
next if $cust->{no_invoice}; |
$cust->{invoice} ||= make_invoice($cust); |
$cust->{invoice} ||= make_invoice($cust); |
if ( !$cust->{invoice} ) { |
if ( !$cust->{invoice} ) { |
$cust->{no_invoices} = 1; |
$cust->{no_invoice} = 1; |
say "$cust->{id} has no open invoices [" . $ticket->id . ']'; |
say "$cust->{id} has no open invoices [" . $ticket->id . ']'; |
next; |
next; |
} |
} |
|
|
$invoice->{total} -= round( $invoice->{discount}{amount} ); |
$invoice->{total} -= round( $invoice->{discount}{amount} ); |
} |
} |
|
|
$invoice->{past_due} = 0; |
|
if (my $unpaid_invoices = $state->unpaid_invoices($cust->{id})) { |
if (my $unpaid_invoices = $state->unpaid_invoices($cust->{id})) { |
my %project = ( title => 'Unpaid Invoices', fees => [], ); |
my %project = ( title => 'Unpaid Invoices', fees => [], ); |
|
my $past_due = 0; |
|
|
foreach my $id ( sort { $a <=> $b } keys %{$unpaid_invoices} ) { |
foreach my $id ( sort { $a <=> $b } keys %{$unpaid_invoices} ) { |
my $unpaid = $state->get_invoice($id); |
my $unpaid = $state->get_invoice($id); |
$invoice->{past_due} += $unpaid_invoices->{$id}; |
my $invdate = ymd_to_DateTime( $unpaid->{invdate} ); |
|
|
|
next |
|
if $cust->{duedate} |
|
&& DateTime->compare( $invdate, $cust->{duedate} ) > 0; |
|
|
|
$past_due += $unpaid_invoices->{$id}; |
push @{ $project{fees} }, { |
push @{ $project{fees} }, { |
id => $id, |
id => $id, |
contents => sprintf( |
contents => sprintf( |
"Invoice %06d from %s", |
"Invoice %06d from %s", |
$id, ymd_to_DateTime( $unpaid->{invdate} )->ymd |
$id, $invdate->ymd |
), |
), |
count => 1, |
count => 1, |
rate => $unpaid_invoices->{$id}, |
rate => $unpaid_invoices->{$id}, |
}; |
}; |
} |
} |
unshift @{ $invoice->{projects} }, \%project; |
|
} |
|
|
|
if ( $invoice->{past_due} ) { |
if ($past_due) { |
$invoice->{total_due} = $invoice->{total} + $invoice->{past_due}; |
$invoice->{past_due} = $past_due; |
|
$invoice->{total_due} = $invoice->{total} + $invoice->{past_due}; |
|
|
|
unshift @{ $invoice->{projects} }, \%project; |
|
} |
} |
} |
|
|
next unless $invoice->{total} > 0 || $invoice->{total_due}; |
next unless $invoice->{total} > 0 || $invoice->{total_due}; |
|
|
sub make_invoice { |
sub make_invoice { |
my ($cust) = @_; |
my ($cust) = @_; |
|
|
|
return if $cust->{no_invoice}; |
return unless $cust->{billstart}; |
return unless $cust->{billstart}; |
|
|
my ($freq) = get_billing_frequency($cust); |
|
return if $cust->{billstart}->clone->add($freq) > $cust->{billend}; |
|
|
|
my %invoice = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); |
my %invoice = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); |
$invoice{start} = $cust->{startinvoicedate}->clone |
$invoice{start} = $cust->{startinvoicedate}->clone |
if $cust->{startinvoicedate}; |
if $cust->{startinvoicedate}; |
|
|
my $day = $cust->{day} || 0; |
my $day = $cust->{day} || 0; |
my ( $freq, $day_method ) = get_billing_frequency($cust); |
my ( $freq, $day_method ) = get_billing_frequency($cust); |
|
|
my $billend = DateTime->now( time_zone => 'local' ) |
my $end = DateTime->now( time_zone => 'local' ) |
->set( hour => 0, minute => 0, second => 0 ); |
->set( hour => 0, minute => 0, second => 0 ); |
|
|
my $date = $billend->clone->subtract($freq); |
my $start = $end->clone->subtract($freq); |
|
|
# XXX This is helpful, but monthly and billday > 28 == !!! |
# XXX This is helpful, but monthly and billday > 28 == !!! |
$billend->subtract( days => 1 ) |
$end->subtract( days => 1 ) |
while $day && $billend->$day_method != $day; |
while $day && $end->$day_method != $day; |
|
|
$cust->{billend} = $billend; |
|
|
|
my $lastinvoice = $state->last_invoice( $cust->{id} ); |
my $lastinvoice = $state->last_invoice( $cust->{id} ); |
if ( $lastinvoice && $lastinvoice->{end} ) { |
if ( $lastinvoice && $lastinvoice->{end} ) { |
$date = ymd_to_DateTime( $lastinvoice->{end} )->add( days => 1 ); |
$start = ymd_to_DateTime( $lastinvoice->{end} )->add( days => 1 ); |
$cust->{startinvoicedate} = $date->clone; |
$cust->{startinvoicedate} = $start->clone; |
} |
} |
|
|
$cust->{billstart} = $date; |
$cust->{duedate} |
|
= $cust->{net} |
|
? DateTime->now->subtract( days => $cust->{net} ) |
|
: 0; |
|
|
|
$cust->{no_invoice} = 1 if $start->clone->add($freq) > $end; |
|
$cust->{billend} = $end; |
|
$cust->{billstart} = $start; |
} |
} |