#!/usr/bin/perl 
#

use strict;

PriceValidate->start();

package PriceValidate;

use lib '/app/tools/bookpub/lib';
use BookPub::Price::Validator;

use base 'BookPub::CronScript';

sub _options {
    {
        file_id => {
            short       => 'f',
            description => 'File ID to validate',
            parameter   => 'i'
        },
        sale_id => {
            short       => 's',
            description => 'Sale ID to validate',
            parameter   => 'i'
        },
        price_type_id => {
            short       => 'p',
            description => 'Price type id (limit validation to this price type)',
            parameter   => 'i'
        },
        catalog_import_id => {
            description => 'Catalog Import ID - Validate products affected by this import.',
            parameter   => 'i'
        },
        tolerance => {
            short       => 't',
            description => 'New tolerance, only used for clearing validation',
            parameter   => 'f'
        },
        clear => {
            description => 'Clear previous validation',
        },
        job => {
            short       => 'j',
            description => 'Create a job instead of executing now',
        },
    };
}

sub _process {
    my $self = shift;

    my $validator = new BookPub::Price::Validator(
        saleID          => $self->{_sale_id},
        fileID          => $self->{_file_id},
        catalogImportID => $self->{_catalog_import_id},
        priceTypeID     => $self->{_price_type_id}
    );

    $validator->clear( tolerance => $self->{_tolerance} ) if ( $self->{_clear} );

    if ( $self->{_job} ) {
        $validator->createJob( tolerance => $self->{_tolerance} );
    } else {
        $validator->run();
    }
}

1;
