#!/usr/bin/perl
use strict;

use Data::Dumper;
use Getopt::Std;
use POSIX qw(ceil);
use POSIX ":sys_wait_h";
use Sys::Hostname;
use Carp;

use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::Assert;
use Common::TextProgressBar;
use Common::Timer;
use Common::DB::Item::Service;
use Common::RSMath;

#use lib '/app/tools/data_classes/lib';
#use File::Sale;
use lib '/app/tools/raptor/lib';

#use Raptor::Sale::Sale;
use Raptor::DB::Item::Sale;
use Raptor::DB::Item::File;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ArtistRoyaltyAlbum;
use RPS::DB::Item::ArtistRoyaltyRun;
use RPS::DB::Item::ArtistRoyaltyStatement;
use RPS::DB::Item::FinanceAccount;
use RPS::DB::Item::FinanceTransaction;
use RPS::DB::Item::ArtistRoyaltyAlbumBalanceAccount;

my $gVerbosityLevel = 1;

# Parse the command-line options.
#
my %options;
_parseCommandLine( \%options );

# Instantiate the application singleton object.
#
my $appSingleton = Common::RSApp->new( clientID => $options{clientID} );

my $db = Common::RSApp::GetClientDB();

# First, run through all the transactions and make note of their amount and balances.
#
my @data;
my $sth = $db->DoCmd("select * from finance_transaction");
while ( my $hr = $sth->fetchrow_hashref() ) {
    my $id      = $hr->{finance_transaction_id};
    my $amount  = $hr->{amount};
    my $balance = $hr->{balance};

    print "$id $amount $balance\n";

    # Note that we're now rounding to _2_ decimal places...
    #
    my $roundedAmount  = Common::RSMath::round( $amount,  2 );
    my $roundedBalance = Common::RSMath::round( $balance, 2 );
    print "$id $roundedAmount $roundedBalance\n";

    push @data, { id => $id, balance => $roundedBalance, amount => $roundedAmount };
}

$db->DoCmd("alter table finance_transaction change amount amount decimal(16,2) not null default '0.00'");
$db->DoCmd("alter table finance_transaction change balance balance decimal(16,2) not null default '0.00'");

foreach my $r (@data) {
    my $id             = $r->{id};
    my $roundedAmount  = $r->{amount};
    my $roundedBalance = $r->{balance};

    my $sql =
        "update finance_transaction set amount="
      . $db->DBQuote($roundedAmount)
      . ",balance="
      . $db->DBQuote($roundedBalance)
      . " WHERE finance_transaction_id=$id";
    $db->DoCmd($sql);
    print $sql . "\n";
    print $db->LastErrorStr() . "\n";
}

sub _usage {
    print "\nusage: $0 -c client_id [-V]\n";
    print "\n";
    print "Arguments:\n";
    print "\t-c <client_id>\t\tThe client_id of the client to process\n";
    print "\t-V <N>\t\t\tVerbosity level - 0 means no output\n";
}

sub _report {
    my ( $string, $verbosity ) = @_;
    $verbosity = 1 unless defined $verbosity;

    if ( $gVerbosityLevel >= $verbosity ) {
        print $string . "\n";
    }
}

# Pass dates in 'yyyy-mm-dd' format.
#
sub _parseCommandLine {
    my ($settings) = @_;

    my %opt;
    getopts( 'c:V:', \%opt );

    if ( !$opt{c} ) {
        _usage();
        exit(1);
    }
    $settings->{clientID} = $opt{c};

    if ( defined $opt{V} ) {
        $gVerbosityLevel = $opt{V};
    }
}

