[BACK]Return to mk_invoice.pl CVS log [TXT][DIR] Up to [local] / RT / Invoicing

File: [local] / RT / Invoicing / mk_invoice.pl (download)

Revision 1.1, Thu Aug 20 02:11:55 2020 UTC (3 years, 8 months ago) by afresh1
Branch: MAIN
CVS Tags: HEAD

Add some helper tools

#!/usr/bin/perl
# $AFresh1: mk_invoice.pl,v 1.1 2020/08/20 02:11:55 afresh1 Exp $
########################################################################
# Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
########################################################################
use strict;
use warnings;

use 5.010;

use Template;

use File::Path;
use DateTime;

use List::Util qw/ sum /;

use lib './lib';    # XXX This is fragile, there are better ways
use RTI::Config;
use RTI::State;
use RTI::Util qw/ round ymd_to_DateTime /;

my $config = RTI::Config->new();
my $state  = RTI::State->new( $config->get('state') );

my ($customer, $input, $output) = (@ARGV, '-', '-');
die "Usage customer [input] [output]\n" unless $output;

my $customers = $config->get('customers');
my $startdate = set_dates($customers);

my $cust = find_cust($customer, $customers)
    or die "Unable to find cust $customer in config\n";

my @transactions;
{
	my $fh;
	if ( $input eq '-' ) {
		open $fh, '<&STDIN' or die "Unable to dup STDIN: $!";
	}
	else {
		open $fh, '<', $input or die "Unable to open $input: $!";
	}

	while (readline $fh) {
		next if /^\s*#/;
		chomp;
		my ($date, $hours, $description) = split /\s/, $_, 3;
		push @transactions, {
			raw => $_,
			date => $date,
			time_taken => $hours,
			description => $description,
		};
	}

	close $fh;
}

#use YAML;
#print Dump $config, $state, $customer, $input, $output, $cust, \@transactions; exit;

$cust->{invoice} = make_invoice($cust);
die "$cust->{id} has no open invoices"
    unless $cust->{invoice};

my $project = make_project($cust, \@transactions, title => "Hourly billing");

foreach my $fee ( @{ $project->{fees} } ) {
    my $hours = hours_for_date( $cust->{invoice}, $fee->{date} );

    my $type  = 'unknown';
    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;

        my $discount_time = 0;
        if ( $hours->{$type} > $count ) {
            $hours->{$type} -= $count;
            $discount_time = $count;
        }
        else {
            $discount_time = $hours->{$type};
            $hours->{$type} = 0;
        }

        if ($discount_time) {
            $cust->{invoice}->{discount}->{amount}
                += round( $discount_time * $fee->{rate} );
            $cust->{invoice}->{discount}->{hours}{$type}
                += $discount_time;

            $type = '' if $type eq 'default';
            $count -= $discount_time;
            $fee->{detail} .= " $discount_time $type Hours Discounted";
        }
    }
}


if ( my $unpaid_invoices = $state->unpaid_invoices() ) {
    foreach my $custid ( keys %{$unpaid_invoices} ) {
        my %project
            = ( title => 'Unpaid Invoices', fees => [], no_total => 1 );
        my $past_due = 0;
        my $unpaid   = 0;

        my $cust;
        foreach ( @{$customers} ) {
            if ( $_->{id} eq $custid ) {
                $cust = $_;
                last;
            }
        }
        $cust ||= fake_customer($custid);

        foreach my $id (
            sort { $a <=> $b }
            keys %{ $unpaid_invoices->{$custid} }
            )
        {
            my $unpaid  = $state->get_invoice($id);
            my $invdate = ymd_to_DateTime( $unpaid->{invdate} );

            my $content
                = sprintf( "Invoice %06d from %s", $id, $invdate->ymd );
            if ( $cust->{duedate} && $invdate < $cust->{duedate}) {
                $content = "PAST DUE: $content";
                $past_due += $unpaid_invoices->{$custid}->{$id};
            }
            else {
                $unpaid += $unpaid_invoices->{$custid}->{$id};
            }

            push @{ $project{fees} },
                {
                id       => $id,
                contents => $content,
                count    => 1,
                rate     => $unpaid_invoices->{$custid}->{$id},
                };
        }

        if ($past_due) {
            $cust->{invoice} ||= make_invoice($cust);

            $cust->{invoice}->{past_due} = $past_due;
            $cust->{invoice}->{unpaid}   = $unpaid;

            unshift @{ $cust->{invoice}->{projects} }, \%project;
        }
    }
}

if ( my $credits = $state->credits ) {
    foreach my $custid ( keys %{$credits} ) {

        my $cust;
        foreach ( @{$customers} ) {
            if ( $_->{id} eq $custid ) {
                $cust = $_;
                last;
            }
        }

        next unless $cust;
        next unless $cust->{invoice};
        next unless $credits->{$custid} < 0;

        $cust->{invoice}->{credit} = $credits->{$custid};

        unshift @{ $cust->{invoice}->{projects} }, {
            title    => 'Credits',
            no_total => 1,
            fees     => [
                {   contents => 'Available Credit',
                    count    => 1,
                    rate     => -$credits->{$custid},
                }
            ],
        };
    }
}

foreach my $cust ( @{$customers} ) {
    my $invoice = $cust->{invoice} ||= make_invoice($cust);
    next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} };

    $invoice->{custid}       = $cust->{id};
    $invoice->{transactions} = [];

    my %transactions;
    foreach my $project ( @{ $invoice->{projects} } ) {
        if ( $project->{transactions} ) {
            %transactions = ( %transactions, %{ $project->{transactions} } );
        }
        my $subtotal = 0;
        foreach my $fee ( @{ $project->{fees} } ) {
            my $amount = round( $fee->{count} * $fee->{rate} );
            $subtotal += $amount;
        }
        foreach my $expense ( @{ $project->{expenses} } ) {
            $subtotal += round( $expense->{amount} );
        }
        $project->{total} = $subtotal;

        next if $project->{no_total};
        $invoice->{total} += $subtotal;
    }
    @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions;

    if ( $invoice->{discount} ) {
        my $c = "Included Hours\n";
        if ( $invoice->{discount}{hours} ) {
            foreach my $t ( keys %{ $invoice->{discount}{hours} } ) {
                my $type = $t eq 'default' ? '' : $t;
                $c .= "\n$invoice->{discount}{hours}{$t} $type hour";
                $c .= 's' if $invoice->{discount}{hours}{$t} != 1;
                $c .= "\n";
            }
        }
        $invoice->{discount}{contents} = $c;
        $invoice->{total} -= round( $invoice->{discount}{amount} );
    }

    if ($invoice->{past_due}) {
        $invoice->{total_due} 
            = sum( @{ $invoice }{ qw/ total past_due unpaid / } );
    }

    next unless $invoice->{total} > 0 || $invoice->{total_due};

    $invoice->{info} = $config->get('info');
    my $from = $config->get('from');

    $invoice->{id} = $state->next_invoice_id;
    $invoice->{file} = sprintf 'invoice_%06d.pdf', $invoice->{id};

    $invoice->{organization} = $from->{organization} || $from->{name};
    $invoice->{from} = make_address($from);
    $invoice->{to}   = make_address( $cust->{address} );
    $invoice->{logo} = $config->get('logo');

    $state->add_invoice($invoice);

    my $invoice_dir = $config->get('invoice_dir');
    File::Path::make_path($invoice_dir);
    my $file = join '/', $invoice_dir, $invoice->{file};

    my $tt = Template->new( INCLUDE_PATH => $config->get('template_dir'), )
        || die $Template::ERROR, "\n";

    $tt->process( $config->get('invoice_template'), $invoice, $file )
        or die $tt->error . "\n";

    printf "Generated %s for %s: \$%.02f\n", $invoice->{file}, $cust->{id},
        $invoice->{total};

}

$state->save;

sub find_cust {
	my ($id, $customers) = @_;
	foreach my $cust (@{ $customers }) {
		return $cust if $cust->{id} eq $id;
	}
	return fake_customer($id);
}

sub fake_customer {
    my ($custid) = @_;
    return unless $custid;

    my $cust = $config->new_customer;
    push @{$customers}, $cust;

    $cust->{id} = $custid;
    $cust->{match} = [
        {   type  => 'requestors',
            regex => $cust->{id},
        }
    ];

    set_cust_dates($cust);
    return $cust;
}

sub make_invoice {
    my ($cust) = @_;

    my %invoice = (
        end   => $cust->{billend}->clone->subtract( seconds => 1 ),
        total => 0,
    );
    $invoice{start} = $cust->{startinvoicedate}->clone
        if $cust->{startinvoicedate};

    if ( $cust->{base_rate} ) {
        my ( $project, $hours ) = make_base_project($cust);

        if ( @{ $project->{fees} } ) {
            $invoice{end}   = $project->{end};
            $invoice{hours} = $hours;
            push @{ $invoice{projects} }, $project;
        }
    }
    elsif ( $cust->{hours} ) {
        $invoice{hours} = [
            {   end   => $invoice{end}->clone,
                hours => $cust->{hours},
            }
        ];
    }

    return \%invoice;
}

sub make_base_project {
    my ($cust) = @_;

    my $date    = $cust->{billstart}->clone;
    my $billend = $cust->{billend}->clone;
    my ($freq)  = get_billing_frequency($cust);

    my $title
        = $cust->{frequency} == 1
        ? ucfirst( $cust->{per} . 'ly' )
        : $freq . ' ' . ucfirst( $cust->{per} );
    $title .= ' Retainer';

    my %project = ( title => $title, start => $date->clone, fees => [], );
    my @hours;

    while ( $date < $billend ) {
        my $start = $date->clone;

        $date->add_duration($freq);

        my $end = $date > $billend ? $billend->clone : $date->clone;
        $end->subtract( seconds => 1 );

        $project{end} = $end->clone;

        push @{ $project{fees} },
            {
            count    => 1,
            rate     => $cust->{base_rate},
            contents => $start->ymd . ' to ' . $end->ymd,
            };

        push @hours,
            {
            start => $start->clone,
            end   => $end->clone,
            hours => { %{ $cust->{hours} } },
            };
    }

    return \%project, \@hours;
}

sub make_address {
    my ($addr) = @_;
    my @adr;

    if ( $addr->{organization} ) {
        push @adr, $addr->{organization};
    }
    elsif ( $addr->{name} && !$addr->{attn} ) {
        push @adr, $addr->{name};
    }

    if (   ( $addr->{addr1} || $addr->{addr2} )
        && $addr->{city}
        && $addr->{state}
        && $addr->{zip} )
    {
        push @adr, $addr->{attn}  if $addr->{attn};
        push @adr, $addr->{addr1} if $addr->{addr1};
        push @adr, $addr->{addr2} if $addr->{addr2};
        push @adr,
            $addr->{city} . ', ' . $addr->{state} . '  ' . $addr->{zip};
    }
    else {
        push @adr, $addr->{email} if $addr->{email};
    }

    return join "\n\n", @adr;
}

sub make_project {
    my ( $cust, $transactions, %project ) = @_;

    $project{fees}     = [];
    $project{expenses} = [];

    die "Project title required!" unless $project{title};

    for my $txn (@{ $transactions }) {

        #if ( my $expense = make_expense( $txn, $ticket ) ) {
        #    push @{ $project{expenses} }, $expense;
        #    $project{transactions}{ $txn->id } = 1;
        #}

        next unless $txn->{time_taken};

        my $fee = make_fee( $txn, $cust->{rates} );

        if ( !( $fee->{rate} && $fee->{count} ) ) {
            warn "Invalid Fee, no rate or count";
            next;
        }

        my $invoice = $cust->{invoice};
        if ( $invoice->{start} && $invoice->{start} > $fee->{date} ) {
            warn "Uninvoiced Transaction $txn->{raw}\n";
            next;
        }
        next if $invoice->{end} < $fee->{date};

        push @{ $project{fees} }, $fee;
        #$project{transactions}{ $txn->id } = 1;
    }

    push @{ $cust->{invoice}->{projects} }, \%project;

    return \%project;
}

sub make_fee {
    my ( $txn, $rates ) = @_;

    my $work_time = $txn->{time_taken};
    my $work_type = $txn->{work_type} // '';

    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 = (
        contents => "$txn->{date} - $txn->{description}",
        count => $work_time,
        type  => $work_type,
        date  => ymd_to_DateTime( $txn->{date} ),
        rate  => $rates->{$work_type} || $rates->{default} || 0,
    );

    if ( $work_type && $work_type ne 'Normal' ) {
        $fee{detail} = $work_type . ' rate';
    }

    return \%fee;
}

sub make_expense {
    my ( $txn, $ticket ) = @_;

    my $amount = $txn->cf('ExpenseAmount') or return;

    my %expense = (
        id       => $txn->id,
        contents => $txn->created . ' ('
            . $txn->id . ')' . "\n\n"
            . ( $txn->data || $ticket->subject ),
        amount => $amount,
        date   => ymd_to_DateTime( $txn->created ),

        # detail => ???,
    );

    return \%expense;
}

sub hours_for_date {
    my ( $invoice, $date ) = @_;

    my $hours = {};
    if ( $invoice->{hours} ) {
        foreach my $h ( @{ $invoice->{hours} } ) {
            next if $h->{start} && $h->{start} > $date;
            next if $h->{end} < $date;

            $hours = $h->{hours};
            last;
        }
    }
    return $hours;
}

sub get_billing_frequency {
    my ($cust) = @_;
    my $per  = $cust->{per}       || 'week';
    my $freq = $cust->{frequency} || 1;

    my $day_method 
        = $per eq 'week'  ? 'dow'
        : $per eq 'month' ? 'day' 
        : die "Unknown per [$per]\n";

    return DateTime::Duration->new( "${per}s" => $freq ), $day_method;
}

sub set_dates {
    my ($customers) = @_;

    my $newest_invoice;
    my $max_duration;

    foreach my $cust ( @{$customers} ) {
        set_cust_dates($cust);

        my ($freq) = get_billing_frequency($cust);
        $max_duration = $freq
            if !$max_duration
                || DateTime::Duration->compare( $freq, $max_duration ) > 0;

        if ( $cust->{startinvoicedate} ) {
            my $date = $cust->{startinvoicedate}->clone;
            $newest_invoice = $date->clone
                if !$newest_invoice || $newest_invoice < $date;
        }
    }

    $newest_invoice ||= DateTime->now;

    return $newest_invoice->clone->subtract_duration($max_duration)
        ->subtract( days => 1 );
}

sub set_cust_dates {
    my ($cust) = @_;

    my $day = $cust->{day} || 0;
    my ( $freq, $day_method ) = get_billing_frequency($cust);

    my $end = DateTime->now( time_zone => 'local' )
        ->set( hour => 0, minute => 0, second => 0 );

    my $start = $end->clone->subtract_duration($freq);

    # XXX This is helpful, but monthly and billday > 28 == !!!
    $end->subtract( days => 1 ) while $day && $end->$day_method != $day;

    my $lastinvoice = $state->last_invoice( $cust->{id} );
    if ( $lastinvoice && $lastinvoice->{end} ) {
        $start = ymd_to_DateTime( $lastinvoice->{end} )->add( days => 1 );
        $cust->{startinvoicedate} = $start->clone;
    }

    $cust->{duedate}
        = $cust->{net}
        ? DateTime->now->subtract( days => $cust->{net} )
        : 0;

    $cust->{no_invoice} = 1 if $start->clone->add_duration($freq) > $end;
    $cust->{billend}    = $end;
    $cust->{billstart}  = $start;
}