version 1.40, 2011/12/22 04:52:28 |
version 1.41, 2011/12/22 05:21:56 |
|
|
#!/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.40 2011/12/22 04:52:28 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; |
} |
} |
|
|
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->{no_invoice} = 1 if $start->clone->add($freq) > $end; |
|
$cust->{billend} = $end; |
|
$cust->{billstart} = $start; |
} |
} |