Current File : /var/www/vinorea/modules/ipexportimport/classes/import/order/EIAConfigurationValidator.php
<?php
/**
 *
 * NOTICE OF LICENSE
 *
 *  @author    SmartPresta <tehran.alishov@gmail.com>
 *  @copyright 2024 SmartPresta
 *  @license   Commercial License
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class EIAConfigurationValidator
{

    const ONLY_INSERT_ORDERS = 'insert';
    const ONLY_UPDATE_ORDERS = 'update';
    const UPDATE_INSERT_ORDERS = 'update_insert';

    public static function validateImportSteps($tab, $data)
    {
        if(!$data['settings_name'] && ($tab !== 'general')){
            throw new Exception(EIATranslatorWrapper::l('Please enter import template name!'));
        }

        if($tab !== 'general' && $tab !== 'file' && !EIAConfigurationImportOrders::getFieldsFromFile()){
            throw new Exception(EIATranslatorWrapper::l('Please select file!'));
        }

        if($tab == 'import'){

            if(isset($data['import_method']) && $data['import_method'] !== self::UPDATE_INSERT_ORDERS && !$data['identify_order']){
                throw new Exception(EIATranslatorWrapper::l('Please select Order Identify'));
            }

            if(isset($data['identify_order']) && $data['identify_order'] == EIAIdentifyOrder::IDENTIFY_ORDER_ID && $data['fields']['orders']['id_order']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Order ID!'));
            }

            if(isset($data['identify_order']) && $data['identify_order'] == EIAIdentifyOrder::IDENTIFY_ORDER_REFERENCE && $data['fields']['orders']['order_reference']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Order Reference!'));
            }

            if(isset($data['identify_product']) && $data['identify_product'] ==  EIAOrderDetail::IDENTIFY_PRODUCT_ID && $data['fields']['products']['id_product']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product ID!'));
            }

            if(isset($data['identify_product']) && $data['identify_product'] == EIAOrderDetail::IDENTIFY_PRODUCT_REFERENCE && $data['fields']['products']['product_reference']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product Reference!'));
            }

            if(isset($data['identify_product']) && $data['identify_product'] == EIAOrderDetail::IDENTIFY_PRODUCT_EAN && $data['fields']['products']['product_ean_13']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product EAN 13!'));
            }

            if(isset($data['identify_product']) && $data['identify_product'] == EIAOrderDetail::IDENTIFY_PRODUCT_UPC && $data['fields']['products']['product_upc']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product UPC!'));
            }

            if(isset($data['identify_product']) && $data['identify_product'] == EIAOrderDetail::IDENTIFY_PRODUCT_MPN && $data['fields']['products']['product_mpn']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product MPN!'));
            }

            if(isset($data['identify_product_combination']) && $data['identify_product_combination'] == EIAOrderDetail::IDENTIFY_PRODUCT_ID && $data['fields']['products']['id_product_combination']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product Combination ID!'));
            }

            if(isset($data['identify_product_combination']) && $data['identify_product_combination'] == EIAOrderDetail::IDENTIFY_PRODUCT_REFERENCE && $data['fields']['products']['product_reference']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product Reference!'));
            }

            if(isset($data['identify_product_combination']) && $data['identify_product_combination'] == EIAOrderDetail::IDENTIFY_PRODUCT_EAN && $data['fields']['products']['product_ean_13']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product EAN 13!'));
            }

            if(isset($data['identify_product_combination']) && $data['identify_product_combination'] == EIAOrderDetail::IDENTIFY_PRODUCT_UPC && $data['fields']['products']['product_upc']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Product UPC!'));
            }

            if(isset($data['identify_product_combination']) && $data['identify_product_combination'] == EIAOrderDetail::IDENTIFY_PRODUCT_MPN && $data['fields']['products']['product_mpn']['val'] == 'no'){
                throw new \Exception(EIATranslatorWrapper::l('Please select field Product MPN!'));
            }

            if(isset($data['identify_customers']) && $data['identify_customers'] == EIAIdentifyCustomer::IDENTIFY_CUSTOMER_ID && $data['fields']['customers']['id_customer']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Customer ID!'));
            }

            if(isset($data['identify_customers']) && $data['identify_customers'] == EIAIdentifyCustomer::IDENTIFY_CUSTOMER_EMAIL && $data['fields']['customers']['customer_email']['val'] == 'no'){
                throw new Exception(EIATranslatorWrapper::l('Please select field Customer Email!'));
            }
            
            if(isset($data['identify_addresses']) && $data['identify_addresses']){
                foreach ($data['identify_addresses'] as $identify_address){
                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_ID && $data['fields']['shippingAddress']['shipping_address_id']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping address ID!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_ALIAS && $data['fields']['shippingAddress']['shipping_address_alias']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping address alias!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_COMPANY && $data['fields']['shippingAddress']['shipping_company']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping company!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_LASTNAME && $data['fields']['shippingAddress']['shipping_customer_last_name']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping customer last name!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_FIRSTNAME && $data['fields']['shippingAddress']['shipping_customer_first_name']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Customer Shipping customer first name!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_1 && $data['fields']['shippingAddress']['shipping_address_1']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping Address 1!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_2 && $data['fields']['shippingAddress']['shipping_address_2']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping Address 2!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_POSTAL && $data['fields']['shippingAddress']['shipping_zip_code']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Shipping Zip/Postal Code!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_ID && $data['fields']['invoiceAddress']['invoice_address_id']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice Address ID!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_ALIAS && $data['fields']['invoiceAddress']['invoice_address_alias']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice address alias!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_COMPANY && $data['fields']['invoiceAddress']['invoice_company']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice company!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_LASTNAME && $data['fields']['invoiceAddress']['invoice_customer_last_name']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice customer last name!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_FIRSTNAME && $data['fields']['invoiceAddress']['invoice_customer_first_name']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice Shipping customer first name!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_1 && $data['fields']['invoiceAddress']['invoice_address_1']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice Address 1!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_2 && $data['fields']['invoiceAddress']['invoice_address_2']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice Address 2!'));
                    }

                    if($identify_address == EIAIdentifyAddress::IDENTIFY_ADDRESS_POSTAL && $data['fields']['invoiceAddress']['invoice_zip_code']['val'] == 'no'){
                        throw new Exception(EIATranslatorWrapper::l('Please select field Invoice Zip/Postal Code!'));
                    }
                }
            }

            if(isset($data['import_method']) && $data['import_method'] == self::ONLY_INSERT_ORDERS ){
                if(isset($data['fields']) && $data['fields']){
                    foreach ($data['fields'] as $tab){
                        foreach ($tab as $field){
                            if($field['required'] && $field['val'] == 'no' && $data['import_method'] !== self::UPDATE_INSERT_ORDERS){
                                throw new Exception(EIATranslatorWrapper::l('Please select field ').$field['label']);
                            }
                        }
                    }
                }
            }

        }
        
    }

}