package RPS::Import::RoughTradeDist;

use strict;


use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

use constant FIELD_UPC      => 3;
use constant FIELD_PRICE    => 4;
use constant FIELD_SALES    => 7;
use constant FIELD_RETURNS  => 8;
use constant FIELD_CURRENCY => 13;
use constant FIELD_TOTAL    => 14;
use constant FIELD_PERIOD   => 15;

my %field_map = (
    1 => {
        upc      => 3,
        price    => 4,
        sales    => 7,
        returns  => 8,
        currency => 13,
        total    => 14,
        period   => 15,
    },
    2 => {
        upc      => 3,
        sales    => 5,
        returns  => 6,
        price    => 11,
        currency => 13,
        total    => 14,
        period   => 15,
    },
    3 => {
        upc         => 2,
        sales       => 4,
        returns     => 5,
        price       => 10,
        currency    => 12,
        total       => 13,
        period      => 14,
    },
);

#public methods

sub _importLines
{
	my $self = shift;
	my %args = @_;

	my $retval = undef;
    my $lines = $args{lines};
    my $version = $args{file}->VersionNum();
    #my $version = $args{version}; # for cmd-line testing

    my $offsets = $field_map{$version};

    my $linenum = 1;
    foreach my $line(@$lines)
    {
        if ($linenum == 1) {
            $linenum++;
            next;
        }
        my $prodtype    = RPS::File::Sale::TYPE_PHYSICAL;
        my $price_level = RPS::File::Sale::PLEVEL_FULL;
        my $channel     = RPS::File::Sale::CHANNEL_RETAIL;
        my $upc         = $line->[$offsets->{'upc'}];
        my $currency    = $line->[$offsets->{'currency'}];

        my ($dateBegin, $dateEnd) = $self->_getDates($line->[$offsets->{'period'}]);
        my ($sales)	   	= $line->[$offsets->{'sales'}] =~ m/^(-?\d+)$/;
        my ($returns) 	= $line->[$offsets->{'returns'}] =~ m/^(-?\d+)$/;
        my ($price)    	= $line->[$offsets->{'price'}] =~ m/^(-?\d*\.?\d*)$/;
        my ($total)    	= $line->[$offsets->{'total'}] =~ m/^(-?\d*\.?\d*)$/;

        $returns = abs($returns);
        my $sales_price   = $sales * $price;
        my $returns_price = $returns * $price;

        if ($prodtype && ($sales || $returns) && $upc)
        {
            unless ($dateBegin && $dateEnd) {
                $self->errstr("couldn't get dates from ".$line->[$offsets->{'period'}]);
                return undef;
            }

            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->upc($upc);
            $sale->sales($sales);
            $sale->returns($returns);
            $sale->wholesalePrice($price);
            $sale->salesRevenue($sales_price);
            $sale->returnsRevenue($returns_price);
            $sale->totalRevenue($total);
            $sale->priceLevel($price_level);
            $sale->channel($channel);
            $sale->countryCode('DE');
            $sale->currencyCode($currency);
            $sale->lineNum($linenum);

            ##$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale(sale => $sale);
        }
        $linenum++;
    }
	return $linenum;
}

#
# Private methods
#


sub _getDates
{
	my $self = shift;
	my $date = shift;

    my ($dateBegin, $dateEnd);
    if ($date =~ m/^(\d\d)(\d\d)$/) {
        my $year  = $1 + 2000;
        my $month = $2;

        $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year, $month));
	    return ($dateBegin, $dateEnd);
    }
    return undef;
}

###
1;# Play nicely.
###
