use strict;
use DB_File;
use Data::Dumper;
use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use Common::RSApp;


# The sale_run_map needs to be destroyed and rebuilt.
# I think I will still need to just link to the appropriate income item, since that
# ought to actually save me storage space and the join is quick.
# I don't know if I will need to index the income item id for the pre-run staging query.
#

my $app = Common::RSApp->new(clientID => 60);
my $dbo = Common::RSApp::GetClientDB();
my $dbh = $dbo->DBH();

my $selectSql = qq/
SELECT
sale_id,
statement_item_id
FROM sale_run_map
WHERE status='paid' AND run_type='ARTR'
/;

my $insertSql = qq/
INSERT INTO artist_royalty_paid_sales
SET sale_id=?, artist_royalty_income_item_id=?
/;


my $selectSTH = $dbh->prepare($selectSql);
my $insertSTH = $dbh->prepare($insertSql);

$selectSTH->execute();
while (my $arrayRef = $selectSTH->fetchrow_arrayref())
{
#    print $arrayRef->[0] . "\t" . $arrayRef->[1] . "\n";
    $insertSTH->execute(@$arrayRef);
}
