#!/usr/bin/perl
#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
use strict;

#use warnings;

use PDF::API2;

use Data::Dumper;
use Getopt::Std;
use Carp;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::RSMath;
use Common::Locale;
use Common::Client;

use lib '/app/tools/rps/lib';
use RPS::Statement::FastRender::Mechanical::US::Payee;
use RPS::DB::Item::MechanicalStatement;
use RPS::DB::Item::Payor;
use RPS::Mechanical::US::MechanicalRun;
use RPS::RoyaltyRun::Status;

use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

$SIG{__DIE__} = \&Carp::confess;

#
# A lot of this code is taken from this tutorial: http://rick.measham.id.au/pdf-api2/
#

# These constants will make it easier to convert between 'regular' units
# and postscript points.
# There are 72 postscript points in an inch, and 25.4 millimeters in an inch.
# All the PDF::API2 methods expect their size or coordinate arguments to be in points.
#
use constant mm => ( 25.4 / 72 );
use constant in => ( 1 / 72 );
use constant pt => 1;

# The standard page dimensions.  Note that we're creating _landscape_ docs.
#
use constant kPageHeight => ( 8.5 / in );
use constant kPageWidth  => ( 11 / in );

# The margins of the page.
# Remember that (0,0) in PDF coordinates is the bottom-left of the page.
#
use constant kLeftMargin   => 0.5 / in;
use constant kBottomMargin => 0.5 / in;
use constant kRightMargin  => ( kPageWidth - ( 0.5 / in ) );
use constant kTopMargin    => ( kPageHeight - ( 0.5 / in ) );

# We are going to keep track of every page we create, so that we
# can go back later and add page numbers
#
my @gPages;

# More nasty globals...
#
my $gRowData;
my $gRowProperties;

# We'll store a hashref with font info here, once we have a PDF object to work with.
#
my $font;

# We'll store the bookmarks here if I can ever figure out how this works
#
my $outline_root;

# This determines how much output we spew forth to STDERR.
# The user can set this with the -V command-line argument.
#
my $gVerbosityLevel = 1;

# Parse the command-line, then create the PDF!
#
my %options;
parseCommandLine( \%options );
createStatementPDF( $options{clientID}, $options{mechanicalStatementID}, $options{outputFile} );

# ----------------------------------------------------------------------------------------------

sub createStatementPDF {
    my ( $clientID, $mechanicalStatementID, $outFilePath ) = @_;

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

    # Instantiate the MechanicalStatement object.
    # This object will contain all the info we need to output the statement document.
    #
    my $statement = RPS::Statement::FastRender::Mechanical::US::Payee->new(
        mechanicalStatementID => $mechanicalStatementID,
        page                  => 'ALL',
    );

    # If an output file path was not specified, create one out of the statement id.
    #
    if ( !$outFilePath ) {
        $outFilePath = "mechanical_statement_$mechanicalStatementID.pdf";
    }

    # Instantiate the PDF object.
    #
    my $pdf = PDF::API2->new( -file => $outFilePath );

    #    my $pdf = PDF::API2->new();

    # Again, just getting ready for bookmarks
    #
    $outline_root = $pdf->outlines();

    $font = {
        Helvetica => {
            Bold   => $pdf->corefont( 'Helvetica-Bold',    -encoding => 'latin1' ),
            Roman  => $pdf->corefont( 'Helvetica',         -encoding => 'latin1' ),
            Italic => $pdf->corefont( 'Helvetica-Oblique', -encoding => 'latin1' ),
        },
        Times => {
            Bold   => $pdf->corefont( 'Times-Bold',   -encoding => 'latin1' ),
            Roman  => $pdf->corefont( 'Times',        -encoding => 'latin1' ),
            Italic => $pdf->corefont( 'Times-Italic', -encoding => 'latin1' ),
        },
    };

    # Going to package up all the PDF state stuff into a single hash.
    #
    my %pageState = (
        pdf  => $pdf,
        x    => kLeftMargin,
        y    => kTopMargin,
        font => {
            Helvetica => {
                Bold   => $pdf->corefont( 'Helvetica-Bold',    -encoding => 'latin1' ),
                Roman  => $pdf->corefont( 'Helvetica',         -encoding => 'latin1' ),
                Italic => $pdf->corefont( 'Helvetica-Oblique', -encoding => 'latin1' ),
            },
            Times => {
                Bold   => $pdf->corefont( 'Times-Bold',   -encoding => 'latin1' ),
                Roman  => $pdf->corefont( 'Times',        -encoding => 'latin1' ),
                Italic => $pdf->corefont( 'Times-Italic', -encoding => 'latin1' ),
            },
        },
    );

    $pageState{page} = newPage($pdf);

    # Add label logo to the first page, if desired
    #
    my $statementDBItem = RPS::DB::Item::MechanicalStatement->Lookup( mechanical_statement_id => $mechanicalStatementID );
    my $payorID         = $statementDBItem->payor_id;
    my $payor           = RPS::DB::Item::Payor->Lookup( payor_id => $payorID );
    my $showLogo        = $payor->show_logo;

    if ( $showLogo == 1 ) {
        my $logoName = $options{clientID} . '/' . $payorID;
        ( $pdf, $pageState{page} ) = EmbedImage::labelLogo( $pdf, $pageState{page}, $logoName, kTopMargin, kRightMargin );
    }

    # NOW - We loop through all the rows.  We're going to keep track of the current 'TABLE_CLASS' value, so we know
    # when we're switching tables.
    #
    my $currentTableObj = undef;

    no strict "refs";

    # !!! For now, we'll wrap this in an eval block.
    # If we run into a TableClass we don't have methods for, we'll just end up
    # stopping.  This way I can at least see 'valid' PDF files during development.
    #
    eval {
        while ( my $row = $statement->getNextRow() ) {

            #            print Dumper($row) . "\n";

            my $tableClass = $row->{TableClass};
            if ( !$currentTableObj || $currentTableObj->class() ne $tableClass ) {
                if ( $tableClass eq 'SUBPUBLISHER_TABLE' ) {
                    $pageState{page} = newPage($pdf);
                    $pageState{y}    = kTopMargin - 10;
                }
                $currentTableObj = $tableClass->new( pageState => \%pageState );
            }

            $currentTableObj->addRow($row);
        }
    };

    #print "LAST ERROR: $@\n";

    # remember to nuke the last object, so it can clean up
    #
    $currentTableObj = undef;

    # If the state of the run is not 'COMMITTED' or 'CLOSED', then we want to add a watermark
    # to the pages so that our users don't mail this statement out.
    #
    my $runID = $statementDBItem->mechanical_run_id();
    my $run   = RPS::Mechanical::US::MechanicalRun->new(
        mechanicalRunID => $runID,
        loadSubs        => 0
    );
    if (   RPS::RoyaltyRun::Status::kCommitted != $run->Status()
        && RPS::RoyaltyRun::Status::kClosed != $run->Status() ) {
        addDraftWatermark($pdf);
    }

    # Now add the page numbers, and other 'footer' stuff
    #
    addFooter($pdf);

    # All done, save and clean up
    #
    $pdf->save();

    #    print $pdf->stringify();

    $pdf->end();
}

sub addFooter {
    my ($pdf) = @_;

    my $pageCount = 1;
    my $numPages  = scalar @gPages;
    foreach my $page (@gPages) {
        my $text = $page->text;
        BaseTable::PrintTextCentered( $text, kBottomMargin, "$pageCount of $numPages", $font->{Helvetica}{Roman}, 12 / pt );
        $pageCount++;
    }
}

# All the pages will _probably_ need the same settings.
#
sub newPage {
    my ($pdf) = @_;

    my $page = $pdf->page;

    # the mediabox is the size of the paper.
    #
    $page->mediabox( kPageWidth, kPageHeight );

    # the cropbox defines the margins, essentially.
    # the args are the coordinates for the left, bottom, right, and top (in that order)
    #

    # !!! Rather than set the cropbox right to the margins, I am going to make it larger.
    # !!! Basically, I want to have this thing render in the viewer with the appearance of having
    # !!! margins.
    #
    #    $page->cropbox(kLeftMargin, kBottomMargin, kRightMargin, kTopMargin);
    $page->cropbox( 5, 5, kPageWidth - 5, kPageHeight - 5 );

    # Not currently declaring a 'bleedbox' or an 'artbox'.  Check out the tutorial if you want to
    # know what those are... the url for that is at the top of this file.
    #
    push @gPages, $page;

    return $page;
}

sub formatNumber {
    my ($value) = @_;

    $value = Formats::formatNumber($value);

    return $value;
}

sub formatMoney {
    my ($value) = @_;

    # Use our own rounding algorithm.
    #
    my $rVal = Common::RSMath::round( $value, 2 );

    $rVal = Common::Client::Current()->Locale()->formatMoney( $rVal, "USD" );

    return $rVal;

    #my ($value) = @_;

    #$value = Formats::formatMoney($value);

    #return $value;
}

sub formatPercent {
    my ($value) = @_;

    $value = Formats::formatPercent($value);

    return $value;
}

sub _checkForRingtone {
    my ($incomeItem) = @_;

    my $productTypeID;

    if ( $incomeItem->TrackLicense->ProductTypeID() == RPS::DB::Item::Product::kProductTypeRingtone ) {
        $productTypeID = RPS::DB::Item::Product::kProductTypeRingtone;
    } else {
        $productTypeID = $incomeItem->ProductTypeID();
    }

    return $productTypeID;
}

# These functions map ids to their text representations.
# Basically, the first time we ask for a particular id->string mapping, we
# will query the correct database, and build a hash.  Subsequent calls just
# hit the hash.
#
# Because these tables are so similar, I abstracted the guts into the _genericMapAccessor
# method (to save myself some typing).
#
my %idMaps;

sub _genericMapAccessor {
    my ( $collectionAccessor, $idName, $id ) = @_;

    if ( !defined $idMaps{$collectionAccessor} ) {
        $idMaps{$collectionAccessor} = {};

        # This is a very naughty thing to do, but it works great in this context.
        #
        #        no strict 'refs';
        #        my $c = &$collectionAccessor();
        #        use strict 'refs';
        my $c = $collectionAccessor->GetAll();

        while ( my $item = $c->next() ) {
            if ( $idName ne 'product_type_id' ) {
                $idMaps{$collectionAccessor}{ $item->$idName() } = $item->name;
            } else {
                $idMaps{$collectionAccessor}{ $item->$idName() } =
                  $item->description;
            }
        }
    }

    return $idMaps{$collectionAccessor}{$id};
}

sub incomeSourceIDToName {
    my ($id) = @_;

    return _genericMapAccessor( 'RPS::DB::Item::IncomeSource', 'income_source_id', $id );
}

sub regionIDToName {
    my ($id) = @_;

    # we don't want to show Rest of World here
    if ( $id != 0 ) {
        return _genericMapAccessor( 'RPS::DB::Item::Region', 'region_id', $id );
    } else {
        return ' ';
    }
}

sub channelIDToName {
    my ($id) = @_;
    return _genericMapAccessor( 'RPS::DB::Item::Channel', 'channel_id', $id );
}

sub priceLevelIDToName {
    my ($id) = @_;
    return _genericMapAccessor( 'RPS::DB::Item::PriceLevel', 'price_level_id', $id );
}

sub contractRateTypeIDToName {
    my ($id) = @_;
    return _genericMapAccessor( 'RPS::DB::Item::ContractRateType', 'contract_rate_type_id', $id );
}

sub productCodeToName {
    my ($id) = @_;
    return _genericMapAccessor( 'RPS::DB::Item::ProductType', 'product_type_id', $id );
}

sub addDraftWatermark {
    my ($pdf) = @_;

    my $centerX = kPageWidth / 2;
    my $centerY = kPageHeight / 2;

    foreach my $page (@gPages) {
        my $text = $page->text(1);
        $text->font( $font->{Helvetica}{Bold}, 128 / pt );
        $text->transform(
            -translate => [ $centerX, $centerY ],
            -rotate    => 45
        );

        $text->fillcolor('lightgray');
        $text->text_center('DRAFT');
        $text->fillcolor('black');
    }
}

#
# Boring script stuff below...
#

sub parseCommandLine {
    my ($settings) = @_;

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

    if ( !$opt{s} || !$opt{c} ) {
        usage();
        exit(1);
    }
    $settings->{mechanicalStatementID} = $opt{s};
    $settings->{clientID}              = $opt{c};
    $settings->{outputFile}            = $opt{f};

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

sub usage {
    print STDERR "\nusage: $0 -c <client_id> -s <mechanical_statement_id> [-f <output file>]\n";
    print STDERR "\n";
    print STDERR "Arguments:\n";
    print STDERR "\t-c <client_id>\t\t\tThe client_id of the client to process\n";
    print STDERR "\t-s <mechanical_statement_id>\tThe id of the mechanical statement to convert to pdf\n";
    print STDERR "\t-f <output file>\tThe output file. Optional. If not provided, we'll make one up\n";
}

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

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

package BaseTable;
use strict;
use constant mm => ( 25.4 / 72 );
use constant in => ( 1 / 72 );
use constant pt => 1;

# The standard page dimensions.  Note that we're creating _landscape_ docs.
#
use constant kPageHeight => ( 8.5 / in );
use constant kPageWidth  => ( 11 / in );

# The margins of the page.
# Remember that (0,0) in PDF coordinates is the bottom-left of the page.
#
use constant kLeftMargin   => 0.5 / in;
use constant kBottomMargin => 0.5 / in;
use constant kRightMargin  => ( kPageWidth - ( 0.5 / in ) );
use constant kTopMargin    => ( kPageHeight - ( 0.5 / in ) );

sub new {
    my ( $class, %args ) = @_;
    my $self = bless {}, $class;
    return $self->_init(%args);
}

sub _init {
    my ( $self, %args ) = @_;
    $self->{_pageState} = $args{pageState};
    return $self;
}

sub class {
    my ($self) = @_;
    return ref($self);
}

sub addRow {
    my ( $self, $row ) = @_;

    # override this
    #
}

# A helper routine to print some text centered.
# 'y' will tell it where to start - 'x' is basically ignored.
#
sub PrintTextCentered {
    my ( $text, $y, $textToPrint, $fontToUse, $fontSize, $color ) = @_;

    $color = 'black' unless $color;

    $text->font( $fontToUse, $fontSize );
    $text->fillcolor($color);

    my @strings = split( /\n/, $textToPrint );
    foreach my $string (@strings) {

        # Remove any carriage returns that are left over from the new line splitting above.
        $string =~ s/\r//g;

        # Calculate how wide this string will be.
        #
        my $stringWidth = $text->advancewidth($string);

        $y -= $fontSize;

        # Calculate where 'X' needs to be
        #
        my $x = ( ( kRightMargin - kLeftMargin ) / 2 ) + kLeftMargin - ( $stringWidth / 2 );

        $text->translate( $x, $y );
        $text->text($string);

        # Move the cursor down the page.
        # add a little bitty bit of padding
        $y -= 1;
    }

    return $y;
}

# This just prints a line of text, left justified to the left margin
#
sub PrintText {
    my ( $text, $x, $y, $textToPrint, $fontToUse, $fontSize, $color ) = @_;

    $color = 'black' unless $color;

    $text->font( $fontToUse, $fontSize );
    $text->fillcolor($color);

    my @strings = split( /\n/, $textToPrint );
    foreach my $string (@strings) {

        # Remove any carriage returns that are left over from the new line splitting above.
        $string =~ s/\r//g;

        # Calculate how wide this string will be.
        #
        my $stringWidth = $text->advancewidth($string);

        $y -= $fontSize;

        $text->translate( $x, $y );
        $text->text($string);

        # Move the cursor down the page.
        # add a little bitty bit of padding
        $y -= 1;
    }

    return $y;
}

sub NewPage {
    my ($pdf) = @_;

    my $page = $pdf->page;

    # the mediabox is the size of the paper.
    #
    $page->mediabox( kPageWidth, kPageHeight );

    # the cropbox defines the margins, essentially.
    # the args are the coordinates for the left, bottom, right, and top (in that order)
    #

    # !!! Rather than set the cropbox right to the margins, I am going to make it larger.
    # !!! Basically, I want to have this thing render in the viewer with the appearance of having
    # !!! margins.
    #
    #    $page->cropbox(kLeftMargin, kBottomMargin, kRightMargin, kTopMargin);
    $page->cropbox( 5, 5, kPageWidth - 5, kPageHeight - 5 );

    # Not currently declaring a 'bleedbox' or an 'artbox'.  Check out the tutorial if you want to
    # know what those are... the url for that is at the top of this file.
    #
    push @gPages, $page;

    return $page;
}

1;

package STATEMENT_HEADER;
use strict;
use base 'BaseTable';

sub addRow {
    my ( $self, $row ) = @_;

    # I only expect 1 row in the header.
    #
    my $text = $self->{_pageState}{page}->text();

    my $cells = $row->{Cells};
    my $cell  = $cells->[0];

    # Step 1 - Put the text 'United States Mechanical Royalty Statement' at the top of the page.
    #
    $self->{_pageState}{y} = BaseTable::PrintTextCentered(
        $text, $self->{_pageState}{y},
        $cell->{DisplayValue},
        $self->{_pageState}{font}{Helvetica}{Bold},
        12 / BaseTable::pt
    );
}

1;

package BLANK;
use strict;
use base 'BaseTable';

sub addRow {
    my ( $self, $row ) = @_;

    $self->{_pageState}{y} -= 9 / BaseTable::pt;
}

1;

package LEFT_TEXT;
use strict;
use base 'BaseTable';

sub addRow {
    my ( $self, $row ) = @_;

    # Just print whatever we see.
    #
    my $text = $self->{_pageState}{page}->text();

    my $fontToUse = $self->{_pageState}{font}{Helvetica}{Roman};
    if ( 'BOLD' eq $row->{RowClass} ) {
        $fontToUse = $self->{_pageState}{font}{Helvetica}{Bold};
    }

    my $cells = $row->{Cells};
    my $cell  = $cells->[0];
    $self->{_pageState}{y} =
      BaseTable::PrintText( $text, $self->{_pageState}{x}, $self->{_pageState}{y}, $cell->{DisplayValue}, $fontToUse, 9 / BaseTable::pt, );
}

1;

package CENTER_TEXT;
use strict;
use base 'BaseTable';

sub addRow {
    my ( $self, $row ) = @_;

    # Just print whatever we see.
    #
    my $text = $self->{_pageState}{page}->text();

    my $fontToUse = $self->{_pageState}{font}{Helvetica}{Roman};
    if ( 'BOLD' eq $row->{RowClass} ) {
        $fontToUse = $self->{_pageState}{font}{Helvetica}{Bold};
    }

    my $cells = $row->{Cells};
    my $cell  = $cells->[0];
    $self->{_pageState}{y} =
      BaseTable::PrintTextCentered( $text, $self->{_pageState}{y}, $cell->{DisplayValue}, $fontToUse, 9 / BaseTable::pt, );
}

1;

package STANDARD_TABLE;

# This will be abstract...  the column properties tend to be what distinguish these.
#
use strict;
use base 'BaseTable';
use lib '/app/tools/common/lib';
use Common::Assert;
use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    # We build a table, in general, with two arrays:
    # - an array to hold the values to display.
    # - an array to hold any 'properties' (different font, bolding, etc).
    #
    $self->{_valueArray}       = [];
    $self->{_rowPropertyArray} = [];

    return $self;
}

sub _columnProperties {
    my ($self) = @_;
    assert( 0, 'override' );
}

sub addRow {
    my ( $self, $row ) = @_;

    my @rowData;
    my %rowProperties;

    my %rowClasses;
    if ( $row->{RowClass} ) {
        foreach my $rowClass ( split( ' ', $row->{RowClass} ) ) {
            $rowClasses{$rowClass} = 1;
        }
    }

    $self->_setRowProperties( \%rowProperties, \%rowClasses );

    my $cells = $row->{Cells};
    foreach my $cell (@$cells) {
        push @rowData, $cell->{DisplayValue};
    }

    push @{ $self->{_valueArray} },    \@rowData;
    push @{ $self->{_propertyArray} }, \%rowProperties;

}

# Broke this out so we can extend it in subclasses (to add additional 'underline' classes, for example)
#
sub _setRowProperties {
    my ( $self, $rowProperties, $rowClasses ) = @_;

    if ( $rowClasses->{'BOLD'} ) {
        $rowProperties->{font} = $self->{_pageState}{font}{Helvetica}{Bold};
    }

    # JPK - going to have several 'UNDERLINED' row classes, I'm sure.
    #
    if ( $rowClasses->{'UNDERLINE_0_1'} ) {
        $rowProperties->{lines} = [ {
                start_col  => 0,
                end_col    => 1,
                top_pad    => 2,
                bottom_pad => -2,
            },
        ];
    }

    if ( $rowClasses->{'UNDERLINE_0_9'} ) {
        $rowProperties->{lines} = [ {
                start_col  => 0,
                end_col    => 9,
                top_pad    => 2,
                bottom_pad => -2,
            },
        ];
    }

    if ( $rowClasses->{'UNDERLINE_1'} ) {
        $rowProperties->{lines} = [ {
                start_col  => 1,
                end_col    => 1,
                top_pad    => 2,
                bottom_pad => -2,
            },
        ];
    }

    if ( $rowClasses->{'UNDERLINE_9'} ) {
        $rowProperties->{lines} = [ {
                start_col  => 9,
                end_col    => 9,
                top_pad    => 2,
                bottom_pad => -2,
            },
        ];
    }

    if ( $rowClasses->{'PAD_MINUS'} ) {
        $rowProperties->{pad} = -1;
    }

    if ( $rowClasses->{'REPEAT'} ) {
        $rowProperties->{repeat} = 1;
    }

    if ( $rowClasses->{'PAD_8'} ) {
        $rowProperties->{pad} = 8;
    }
}

sub DESTROY {
    my ($self) = @_;

    my $fontSize = 7 / BaseTable::pt;
    my $tableObj = TableObj->new(
        $self->{_pageState}{pdf},
        $self->{_pageState}{page},
        $self->{_valueArray},
        bottom_margin => BaseTable::kBottomMargin + 20,
        new_page_y    => BaseTable::kTopMargin - 20,
        new_page_func => \&BaseTable::NewPage,
        font          => $self->{_pageState}{font}{Helvetica}{Roman},
        font_size     => $fontSize,
        column_props  => $self->_columnProperties(),
        row_props     => $self->{_propertyArray},
    );

    my ( $newPage, $newY ) = $tableObj->print( $self->{_pageState}{x}, $self->{_pageState}{y} );
    $self->{_pageState}{page} = $newPage;
    $self->{_pageState}{y}    = $newY;

}

1;

package BALANCE_TABLE;
use strict;
use base 'STANDARD_TABLE';
use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

sub _columnProperties {
    my ($self) = @_;
    my $fontSize = 7 / BaseTable::pt;
    return [ {
            font      => $self->{_pageState}{font}{Helvetica}{Bold},
            font_size => $fontSize,

            #                justify => 'center',
            min_w => 1.25 / BaseTable::in,
            max_w => 1.25 / BaseTable::in,
        },
        {
            font      => $self->{_pageState}{font}{Helvetica}{Roman},
            font_size => $fontSize,
            justify   => 'right',
            min_w     => 1.25 / BaseTable::in,
            max_w     => 1.25 / BaseTable::in,
        },
        {
            font      => $self->{font}{Helvetica}{Roman},
            font_size => $fontSize,

            #                justify => 'right',
            min_w => 1.0 / BaseTable::in,
            pad   => ( 1 / 4 ) / BaseTable::in,
        },
        {
            font      => $self->{font}{Helvetica}{Roman},
            font_size => $fontSize,

            #                justify => 'right',
            min_w => 1.0 / BaseTable::in,
        },
        {
            font      => $self->{font}{Helvetica}{Roman},
            font_size => $fontSize,

            #justify => 'right',
            min_w => 1.0 / BaseTable::in,
        },
    ];
}

1;

package PUBLISHER_TABLE;
use strict;
use base 'STANDARD_TABLE';
use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

sub _columnProperties {
    my ($self) = @_;
    my $fontSize = 7 / BaseTable::pt;
    return [ {
            font      => $self->{_pageState}{font}{Helvetica}{Roman},
            font_size => $fontSize,
            min_w     => 0.5 / BaseTable::in,
        },
        {
            font      => $self->{_pageState}{font}{Helvetica}{Roman},
            font_size => $fontSize,
            min_w     => 0.5 / BaseTable::in,
            justify   => 'right',
        },
    ];
}

1;

package SUBPUBLISHER_TABLE;
use strict;
use base 'STANDARD_TABLE';
use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

sub _columnProperties {
    my ($self) = @_;
    my $fontSize = 10 / BaseTable::pt;
    return [ {
            font       => $self->{_pageState}{font}{Helvetica}{BOLD},
            font_size  => $fontSize,
            min_w      => 10 / BaseTable::in,
            justify    => 'center',
            bottom_pad => 4,
        },
    ];
}

1;

package DETAILS_TABLE;
use strict;
use base 'STANDARD_TABLE';
use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

sub _underline {
    my ($self) = @_;
    return [ {
            start_col => 0,
            end_col   => 1,
            top_pad   => 2,
        },
    ];
}

sub _columnProperties {
    my ($self) = @_;
    my $fontSize = 7 / BaseTable::pt;

    # Force the columns to be fixed width.
    #
    return [ {

            # track title
            min_w => 2 / BaseTable::in,
            max_w => 2 / BaseTable::in,
            pad   => 0,
        },
        {

            # album title
            min_w => 1.8 / BaseTable::in,
            max_w => 1.8 / BaseTable::in,
            pad   => 0,
        },
        {

            # upc
            min_w => 0.75 / BaseTable::in,
            max_w => 0.75 / BaseTable::in,
            pad   => 0,
        },
        {

            # product config
            min_w => 0.75 / BaseTable::in,
            max_w => 0.75 / BaseTable::in,
            pad   => 0,
        },
        {

            # region
            min_w => 0.65 / BaseTable::in,
            man_w => 0.65 / BaseTable::in,
            pad   => 0,
        },
        {

            # rate period
            min_w => 0.60 / BaseTable::in,
            max_w => 0.60 / BaseTable::in,
            pad   => 0,
        },
        {

            # share
            min_w => 0.5 / BaseTable::in,
            max_w => 0.5 / BaseTable::in,
            pad   => 0,
        },
        {

            # net rate
            min_w => 0.5 / BaseTable::in,
            max_w => 0.5 / BaseTable::in,
            pad   => 0,
        },
        {

            # net units
            justify => 'right',
            min_w   => 1.45 / BaseTable::in,
            max_w   => 1.45 / BaseTable::in,
            pad     => 0,
        },
        {

            # amount due
            justify => 'right',
            min_w   => 1 / BaseTable::in,
            max_w   => 1 / BaseTable::in,
            pad     => 0,
        },
    ];

}

1;

package TOTAL_TABLE;
use strict;
use base 'STANDARD_TABLE';
use lib '/app/tools/rps/bin/statements';
use TableObj;
use Formats;
use EmbedImage;

sub _columnProperties {
    my ($self) = @_;
    my $fontSize = 7 / BaseTable::pt;
    return [ {
            font    => $self->{_pageState}{font}{Helvetica}{Bold},
            min_w   => 9 / BaseTable::in,
            pad     => 0,
            justify => 'right',
        },
        {
            min_w   => 1 / BaseTable::in,
            justify => 'right',
            pad     => 0,
        },
    ];
}

1;

