<?php

class Product extends ProductCore
{
    /**
     * Price calculation / Get product price
     *
     * @param int    $id_shop Shop id
     * @param int    $id_product Product id
     * @param int    $id_product_attribute Product attribute id
     * @param int    $id_country Country id
     * @param int    $id_state State id
     * @param string $zipcode
     * @param int    $id_currency Currency id
     * @param int    $id_group Group id
     * @param int    $quantity Quantity Required for Specific prices : quantity discount application
     * @param bool   $use_tax with (1) or without (0) tax
     * @param int    $decimals Number of decimals returned
     * @param bool   $only_reduc Returns only the reduction amount
     * @param bool   $use_reduc Set if the returned amount will include reduction
     * @param bool   $with_ecotax insert ecotax in price output.
     * @param null   $specific_price If a specific price applies regarding the previous parameters,
     *                               this variable is filled with the corresponding SpecificPrice object
     * @param bool   $use_group_reduction
     * @param int    $id_customer
     * @param bool   $use_customer_price
     * @param int    $id_cart
     * @param int    $real_quantity
     * @return float Product price
     **/
    public static function priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency,
                                            $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $with_ecotax, &$specific_price, $use_group_reduction,
                                            $id_customer = 0, $use_customer_price = true, $id_cart = 0, $real_quantity = 0)
    {
        $price = parent::priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $zipcode, $id_currency,
            $id_group, $quantity, $use_tax, $decimals, $only_reduc, $use_reduc, $with_ecotax, $specific_price, $use_group_reduction,
            $id_customer = 0, $use_customer_price = true, $id_cart, $real_quantity);

        require_once _PS_MODULE_DIR_ . 'myswissstyle/require.php';

        if (STYLESWISS_ADMIN_MODE == false) {
            $design_price = SwissStyle\Payment\Controller\Payment::getActualDesignPriceForProduct($id_product);

            if ($design_price > 0) {
                if ($use_tax) {

                    static $address = null;
                    static $context = null;


                    if ($address === null)
                        $address = new Address();

                    if ($context == null)
                        $context = Context::getContext()->cloneContext();


                    // Tax
                    $address->id_country = $id_country;
                    $address->id_state = $id_state;
                    $address->postcode = $zipcode;

                    $tax_manager = TaxManagerFactory::getManager($address, Product::getIdTaxRulesGroupByIdProduct((int)$id_product, $context));
                    $product_tax_calculator = $tax_manager->getTaxCalculator();

                    $design_price = $product_tax_calculator->addTaxes($design_price);
                }

                $price += Tools::convertPrice($design_price, $id_currency);
            }
        }

        return $price;
    }

}