version 1.33, 2011/05/07 03:27:34 |
version 1.39, 2011/12/21 02:21:54 |
|
|
#!/usr/bin/perl |
#!/usr/bin/perl |
# $AFresh1: rt_invoices.pl,v 1.32 2011/05/07 01:09:43 andrew Exp $ |
# $AFresh1: rt_invoices.pl,v 1.38 2011/06/21 00:20:28 andrew Exp $ |
######################################################################## |
######################################################################## |
# Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com> |
# Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com> |
# |
# |
|
|
foreach my $fee ( @{ $project->{fees} } ) { |
foreach my $fee ( @{ $project->{fees} } ) { |
my $hours = hours_for_date( $cust->{invoice}, $fee->{date} ); |
my $hours = hours_for_date( $cust->{invoice}, $fee->{date} ); |
|
|
my $type = exists $hours->{ $fee->{type} } |
my $type = 'unknown'; |
&& $hours->{ $fee->{type} } > 0 ? $fee->{type} : 'default'; |
my $count = $fee->{count}; |
|
while ( $type && $count > 0 && $type ne 'default' ) { |
|
$type = exists $hours->{ $fee->{type} } |
|
&& $hours->{ $fee->{type} } > 0 ? $fee->{type} : 'default'; |
|
|
next unless exists $hours->{$type} && $hours->{$type} > 0; |
next unless exists $hours->{$type} && $hours->{$type} > 0; |
|
|
my $discount_time = 0; |
my $discount_time = 0; |
if ( $hours->{$type} > $fee->{count} ) { |
if ( $hours->{$type} > $count ) { |
$hours->{$type} -= $fee->{count}; |
$hours->{$type} -= $count; |
$discount_time = $fee->{count}; |
$discount_time = $count; |
} |
} |
else { |
else { |
$discount_time = $hours->{$type}; |
$discount_time = $hours->{$type}; |
$hours->{$type} = 0; |
$hours->{$type} = 0; |
} |
} |
|
|
if ($discount_time) { |
if ($discount_time) { |
$cust->{invoice}->{discount}->{amount} |
$cust->{invoice}->{discount}->{amount} |
+= round( $discount_time * $fee->{rate} ); |
+= round( $discount_time * $fee->{rate} ); |
$cust->{invoice}->{discount}->{hours}{$type} += $discount_time; |
$cust->{invoice}->{discount}->{hours}{$type} |
|
+= $discount_time; |
|
|
$type = '' if $type eq 'default'; |
$type = '' if $type eq 'default'; |
$fee->{detail} = "$discount_time $type Hours Discounted"; |
$count -= $discount_time; |
|
$fee->{detail} .= " $discount_time $type Hours Discounted"; |
|
} |
} |
} |
} |
} |
|
|
|
|
next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; |
next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; |
|
|
my %li = ( |
my %li = ( |
custid => $cust->{id}, |
custid => $cust->{id}, |
invdate => DateTime->now( time_zone => 'local' )->ymd, |
invdate => DateTime->now( time_zone => 'local' )->ymd, |
transactions => [], |
transactions => [], |
); |
); |
|
|
|
|
my $c = "Included Hours\n"; |
my $c = "Included Hours\n"; |
if ( $invoice->{discount}{hours} ) { |
if ( $invoice->{discount}{hours} ) { |
foreach my $t ( keys %{ $invoice->{discount}{hours} } ) { |
foreach my $t ( keys %{ $invoice->{discount}{hours} } ) { |
$c .= "\n$invoice->{discount}{hours}{$t} $t hour"; |
my $type = $t eq 'default' ? '' : $t; |
|
$c .= "\n$invoice->{discount}{hours}{$t} $type hour"; |
$c .= 's' if $invoice->{discount}{hours}{$t} != 1; |
$c .= 's' if $invoice->{discount}{hours}{$t} != 1; |
$c .= "\n"; |
$c .= "\n"; |
} |
} |
|
|
} |
} |
} |
} |
} |
} |
$state->{invoice}->{ $li{end} }{ $invoice->{id} } = \%li; |
$state->{invoice}->{ $invoice->{id} } = \%li; |
|
|
foreach my $key (qw/ start end /) { |
foreach my $key (qw/ start end /) { |
if ( exists $invoice->{$key} ) { |
if ( exists $invoice->{$key} ) { |
|
|
while ( my $txn = $txn_i->() ) { |
while ( my $txn = $txn_i->() ) { |
next if $state->txn_is_invoiced( $txn->id ); |
next if $state->txn_is_invoiced( $txn->id ); |
|
|
if (my $expense = make_expense( $txn, $ticket ) ) { |
if ( my $expense = make_expense( $txn, $ticket ) ) { |
push @{ $project{expenses} }, $expense; |
push @{ $project{expenses} }, $expense; |
$project{transactions}{ $txn->id } = 1; |
$project{transactions}{ $txn->id } = 1; |
} |
} |
|
|
} |
} |
next if $invoice->{end} < $fee->{date}; |
next if $invoice->{end} < $fee->{date}; |
|
|
push @{ $project{fees} }, $fee; |
push @{ $project{fees} }, $fee; |
$project{transactions}{ $txn->id } = 1; |
$project{transactions}{ $txn->id } = 1; |
} |
} |
|
|
|
|
my $work_time = sprintf "%.03f", $txn->time_taken / 60; |
my $work_time = sprintf "%.03f", $txn->time_taken / 60; |
my $work_type = $txn->cf('WorkType'); |
my $work_type = $txn->cf('WorkType'); |
|
|
|
if ( $work_type =~ s/\s*Onsite//i ) { |
|
|
|
# XXX Do something special for onsite activities |
|
} |
|
|
|
$work_type =~ s/^\s+|\s+$//g; |
|
$work_type ||= 'Normal'; |
|
|
my %fee = ( |
my %fee = ( |
id => $txn->id, |
id => $txn->id, |
contents => $txn->created . ' (' |
contents => $txn->created . ' (' |
|
|
. ( $txn->data || $ticket->subject ), |
. ( $txn->data || $ticket->subject ), |
amount => $amount, |
amount => $amount, |
date => ymd_to_DateTime( $txn->created ), |
date => ymd_to_DateTime( $txn->created ), |
|
|
# detail => ???, |
# detail => ???, |
); |
); |
|
|