package StandardLoader;
use strict;

use base qw(GenericLoader);

use CCExtensions;
use File::Spec;
use GenericLoaderUtilities;

use constant DELIMITER   => '\|';
use constant FIELDS      => qw(site_id email_offers non_enews_email_offers bill_first_name
                               bill_last_name bill_address1 bill_address2 bill_city bill_state bill_zip
                               bill_country telephone email card_type credit_card_number
                               expiration_date amount_paid ship_first_name ship_last_name ship_address1
                               ship_address2 ship_city ship_state ship_zip ship_country order_date
                               order_time price offer_id non_enews_order_id non_enews_line_number
                               non_enews_customer_id send_as_gift_flag gift_name gift_message);
use constant VALIDATIONS => @GenericLoader::ALL_VALIDATIONS;
use constant ORDER_DIR   => 'orders';
use constant ARCHIVE_DIR => 'archive';
use constant STATUS_DIR  => 'status';
use constant PGP_DIR     => 'Order_files';

sub post_load {
    my $class   = shift;
    my $hashref = shift;

    # manipulate fields that don't depend on other fields, order shouldn't matter
    $hashref->{bill_country}        = GenericLoaderUtilities::country_code($hashref->{bill_country});
    $hashref->{bill_name}           = join ' ', @$hashref{qw(bill_first_name bill_last_name)};
    $hashref->{bill_state}          = '' unless GenericLoaderUtilities::p_has_states($hashref->{bill_country});
    $hashref->{credit_card_number}  =~ tr/0-9//cd;
    $hashref->{expiration_date}     = GenericLoaderUtilities::mmyy_to_datetime($hashref->{expiration_date});
    $hashref->{order_date}          = GenericLoaderUtilities::mmddyyyy_to_datetime($hashref->{order_date});
    $hashref->{ship_country}        = GenericLoaderUtilities::country_code($hashref->{ship_country});
    $hashref->{ship_name}           = join ' ', @$hashref{qw(ship_first_name ship_last_name)};
    $hashref->{ship_state}          = '' unless GenericLoaderUtilities::p_has_states($hashref->{ship_country});
    $hashref->{spid}                = $class->SP_ID;

    # now manipulate fields that have some kind of order dependency

    # zip codes depend on country codes
    $hashref->{bill_zip}            = GenericLoaderUtilities::zip_code($hashref->{bill_zip},
                                                                       $hashref->{bill_country});
    $hashref->{ship_zip}            = GenericLoaderUtilities::zip_code($hashref->{ship_zip},
                                                                       $hashref->{ship_country});

    # continuous depends on offer_id
    $hashref->{continuous}          = GenericLoaderUtilities::get_continuous($hashref->{offer_id});

    # sp_order_id depends on site_id, order_date, non_enews_order_id, and non_enews_line_number
    $hashref->{sp_order_id}         = GenericLoaderUtilities::get_sp_order_id($hashref);

    # partial_card_number depends on credit_card_number being unencrypted
    $hashref->{partial_card_number} = GenericLoaderUtilities::partialize($hashref->{credit_card_number});

    # ok (and necessary) to encrypt the credit card now, ignore success or failure
    $hashref->{credit_card_number} = (CCExtensions::Encrypt($hashref->{credit_card_number}))[1];
}

1;
