#!/usr/bin/perl

use strict;

use Text::CSV_XS;

if ( $ARGV[0] && $ARGV[1] ) {
    createOutput();
} else {
    usage();
}

sub createOutput {
    open( my $fhOld, "<:encoding(utf8)", $ARGV[0] ) or usage();
    open( my $fhNew, "<:encoding(utf8)", $ARGV[1] ) or usage();
    binmode( STDOUT, ":utf8" );
    my $fhOut = \*STDOUT;

    my $cOld = Text::CSV_XS->new( { binary       => 1 } );
    my $cNew = Text::CSV_XS->new( { binary       => 1 } );
    my $cOut = Text::CSV_XS->new( { always_quote => 1, eol => $/ } );

    my $linenum = 1;
    while ( my $old = $cOld->getline($fhOld) ) {
        my $new = $cNew->getline($fhNew);

        if (   scalar(@$old) == 64
            && scalar(@$new) == 64
            && $old->[1] eq $new->[1]
            && $old->[2] eq $new->[2]
            && $old->[10] eq $new->[10]
            && $old->[11] eq $new->[11]
            && $old->[56] == $new->[56]
            && $old->[57] == $new->[57] ) {
            if ( $new->[11] =~ /REFUND/ ) {
                if ( $new->[60] > 0 ) {
                    $new->[60] = -$new->[60];
                } elsif ( $new->[60] < 0 ) {
                    die "Inconsistent refund! Line: $linenum\n";
                }
            }

            if ( $old->[60] != $new->[60] || $old->[54] != $new->[54] ) {
                my $what = 'is the what';

                if ( $old->[11] eq 'PREVIOUSLY_FULFILLED_PAID' && $old->[54] == -$old->[60] & $old->[60] != 0 ) {
                    $what = 'revenue = -incentive (a)';
                    if ( $new->[60] > 0 ) {
                        $new->[54] = -$new->[60];
                    } else {
                        $what      = 'skip (z)';
                        $new->[54] = 0;
                        $new->[11] = 'FULFILLED_NOT_PAID';
                    }
                } elsif ( $old->[11] eq 'PREVIOUSLY_REFUNDED_RECEIVED' && $old->[54] == -$new->[60] && $old->[60] eq '' ) {
                    die "zero revenue: $linenum" if $new->[60] == 0;

                    $what = 'revenue = -incentive (b)';
                    $new->[54] = -$new->[60];
                } elsif ( $old->[11] eq 'REFUNDED_RECEIVED' && $old->[54] eq -$new->[54] + $new->[60] ) {
                    die "zero revenue: $linenum" if $new->[60] == 0 & $new->[54] == 0;

                    $what = 'revenue = -revenue (c)';
                    $new->[54] = -$new->[54];
                } elsif ( $old->[11] eq 'REFUNDED_RECEIVED' && $old->[54] == -$new->[54] ) {
                    die "zero revenue: $linenum" if $new->[54] == 0;

                    $what = 'revenue = -revenue (d)';
                    $new->[54] = -$new->[54];
                } elsif ( $old->[11] eq 'PREVIOUSLY_REFUNDED_RECEIVED' && $old->[54] <= 1.70 && $old->[60] eq '' ) {
                    $what = 'revenue = -incentive (e)';
                    if ( $new->[60] < 0 ) {
                        $new->[54] = -$new->[60];
                    } else {
                        $what      = 'skip (y)';
                        $new->[54] = 0;
                        $new->[11] = 'REFUNDED_NOT_RECEIVED';
                    }
                } elsif ( $old->[11] eq 'REFUNDED_RECEIVED'
                    && $linenum =~ /^(659757|659843|660980|664303|664434|664810|666395|666396|668063|669266|669267|669271|669275|669276)$/ )
                {
                    $what = 'revenue = -revenue (f)';
                    $new->[54] = -$new->[54];
                } elsif ( $old->[11] eq 'FULFILLED_PAID'
                    && $linenum =~ /^(1005467|1007988|1008072|1017015|1033394|1034130|1127838|1140167|1140749|1699704)$/ ) {
                    $what = 'let it be (3)';
                } elsif ( $old->[54] + $old->[60] == $new->[54] + $new->[60] ) {
                    $what = 'let it be (1)';
                } elsif ( $old->[54] eq $new->[54] + $new->[60] ) {
                    die "zero revenue: $linenum" if $new->[60] == 0 & $new->[54] == 0;

                    $what = 'let it be (2)';
                }

                # print(join("\t", "$linenum", @$old[11,54,55,59,60,61,62,63], '|', @$new[54,55,59,60,61,62,63], "$what\n"));
            }

            $cOut->print( $fhOut, $new );
        } else {
            die "Transaction exception! Line: $linenum\n";
        }

        $linenum++;
    }

    close($fhOld);
    close($fhNew);
    close($fhOut);
}

sub usage {
    die( "Usage $0 /path/to/old /path/to/new\n" . "\n" . "\tupdated file is output to STDOUT" );
}

1;
