Jump to content

What to use in 1.5.x instead of Tax::getApplicableTax?


Beluga

Recommended Posts

I think I'm pretty close to getting Cash on delivery with fixed and percentage fees working in PS 1.5, but this is giving me a Fatal error: Call to undefined method Tax::getApplicableTax()

/* Exclude VAT */
                    if (Tax::excludeTaxeOption())
                    {
                        $product['tax'] = 0;
                        $product['rate'] = 0;
                    }
                    else
                        $tax = Tax::getApplicableTax(intval($product['id_tax']), floatval($product['rate']));

I tried a change:

$tax = Tax::getProductTaxRate($this->id, $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});

But that brings the order total to 0 and gives payment error in the order (it validates, though). Actually, in the database the total_paid_real is correct, it includes the COD fee, but the total_paid_tax_incl and total_paid_tax_excl are 0 and this is what causes the payment error.

Edited by Beluga (see edit history)
Link to comment
Share on other sites

$tax = Tax::getProductTaxRate($this->id, $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});

It is unlikely that calling this function is having that effect.  This function simply locates the tax rule associated to the stated product, and returns the tax rate.

 

You are most likely doing something with the return value that is causing that.  You should further document what you are doing.

Link to comment
Share on other sites

Thank you for helping. The module (not mine) I'm working on to make compatible with 1.5.x is in the attachment of this post.

I have structured it properly with controllers, translations, views folders and I have organized the validation.php.

 

Here are the sections dealing with $tax in the main module file, cashondeliverywithfee.php, that I suspect might be having trouble:

    //Return the fee cost
    function getCost($params)
    {
    if(Configuration::get('COD_FEE_TYPE')==0)
    {
        return floatval(Configuration::get('COD_FEE'));
    }
    else if(Configuration::get('COD_FEE_TYPE')==1)
    {
        $minimalfee = floatval(Configuration::get('COD_FEE_MIN'));
        $maximalfee = floatval(Configuration::get('COD_FEE_MAX'));
        $cartvalue = floatval($params['cart']->getOrderTotal(true, 3));
        $percent = floatval(Configuration::get('COD_TAX'));
        $percent = $percent / 100;
        $fee = $cartvalue * $percent;
        
        if(($fee<$minimalfee)&($minimalfee!=0))
        {
            $fee=$minimalfee;
        }
        else if (($fee>$maximalfee)&($maximalfee!=0))
        {
            $fee=$maximalfee;
        }
        return floatval($fee);
    }
    else
    {
        $minimalfee = floatval(Configuration::get('COD_FEE_MIN'));
        $maximalfee = floatval(Configuration::get('COD_FEE_MAX'));
        $cartvalue = floatval($params['cart']->getOrderTotal(true, 3));
        $percent = floatval(Configuration::get('COD_TAX'));
        $percent = $percent / 100;
        $tax = floatval(Configuration::get('COD_FEE'));
        $fee = ($cartvalue * $percent) + $tax;
        
        if(($fee<$minimalfee)&($minimalfee!=0))
        {
            $fee=$minimalfee;
        }
        else if (($fee>$maximalfee)&($maximalfee!=0))
        {
            $fee=$maximalfee;
        }
        return floatval($fee);
    }
    }
    
    //Return the fee cost
    function getCostValidated($cart)
    {
    if(Configuration::get('COD_FEE_TYPE')==0)
    {
        return floatval(Configuration::get('COD_FEE'));
    }
    else if(Configuration::get('COD_FEE_TYPE')==1)
    {
        $minimalfee = floatval(Configuration::get('COD_FEE_MIN'));
        $maximalfee = floatval(Configuration::get('COD_FEE_MAX'));
        $cartvalue = floatval($cart->getOrderTotal(true, 3));
        $percent = floatval(Configuration::get('COD_TAX'));
        $percent = $percent / 100;
        $fee = $cartvalue * $percent;
        
        if(($fee<$minimalfee)&($minimalfee!=0))
        {
            $fee=$minimalfee;
        }
        else if (($fee>$maximalfee)&($maximalfee!=0))
        {
            $fee=$maximalfee;
        }
        return floatval($fee);
    }
    else
    {
        $minimalfee = floatval(Configuration::get('COD_FEE_MIN'));
        $maximalfee = floatval(Configuration::get('COD_FEE_MAX'));
        $cartvalue = floatval($params['cart']->getOrderTotal(true, 3));
        $percent = floatval(Configuration::get('COD_TAX'));
        $percent = $percent / 100;
        $tax = floatval(Configuration::get('COD_FEE'));
        $fee = ($cartvalue * $percent) + $tax;
        
        if(($fee<$minimalfee)&($minimalfee!=0))
        {
            $fee=$minimalfee;
        }
        else if (($fee>$maximalfee)&($maximalfee!=0))
        {
            $fee=$maximalfee;
        }
        return floatval($fee);
    }
    }

and this:

if ($product['cart_quantity'] > 1){
                        $quantityDiscount = SpecificPrice::getQuantityDiscount((int)$product['id_product'], Shop::getCurrentShop(), (int)$cart->id_currency, (int)$vat_address->id_country, (int)$customer->id_default_group, (int)$product['cart_quantity']);
                        $unitPrice = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 2, NULL, false, true, 1, false, (int)$order->id_customer, NULL, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
                        $reduc = $quantityDiscount ? ((Product::getTaxCalculationMethod((int)$order->id_customer) == PS_TAX_EXC ? Tools::ps_round($unitPrice, 2) : $unitPrice) - $quantityDiscount['price'] * (1 + $tax_rate / 100)) : 0.00;
                        $price -= $reduc / (1 + floatval($tax) / 100);                                                                                                   
                    }

                    

                    /* Query */
                    $query .= '('.intval($order->id).',
                        '.intval($product['id_product']).',
                        '.(isset($product['id_product_attribute']) ? intval($product['id_product_attribute']) : 'NULL').',
                        \''.pSQL($product['name'].((isset($product['attributes']) AND $product['attributes'] != NULL) ? ' - '.$product['attributes'] : '')).'\',
                        '.intval($product['cart_quantity']).',
                        '.floatval($price).',
                        '.floatval($reduc).',
                        '.(empty($product['ean13']) ? 'NULL' : '\''.pSQL($product['ean13']).'\'').',
                        '.(empty($product['reference']) ? 'NULL' : '\''.pSQL($product['reference']).'\'').',
                        '.(empty($product['supplier_reference']) ? 'NULL' : '\''.pSQL($product['supplier_reference']).'\'').',
                        '.floatval($product['weight']).',
                        \''.(!$tax ? '' : pSQL($product['tax'])).'\',
                        '.floatval($tax).',
                        '.floatval($product['ecotax']).',
                        \''.pSQL($deadline).'\',
                        \''.pSQL($download_hash).'\'),';

                    $priceWithTax = number_format($price * (($tax + 100) / 100), 2, '.', '');

Here is my modified validation.php:

class CashondeliveryWithFeeValidationModuleFrontController extends ModuleFrontController
{
    public $ssl = true;
    public function postProcess()
    {
        $cashOnDelivery = new CashOnDeliveryWithFee();
        $CODfee = $cashOnDelivery->getCostValidated($this->context->cart);
        $confirm = Tools::getValue('confirm');


        if ($this->context->cart->id_customer == 0 || $this->context->cart->id_address_delivery == 0 || $this->context->cart->id_address_invoice == 0 || !$this->module->active)
        Tools::redirectLink(__PS_BASE_URI__.'order.php?step=1');

        // Check that this payment option is still available in case the customer changed his address just before the end of the checkout process
        $authorized = false;
        foreach (Module::getPaymentModules() as $module)
            if ($module['name'] == 'cashondeliverywithfee')
            {
                $authorized = true;
                break;
            }
        if (!$authorized)
            die(Tools::displayError('This payment method is not available.'));

        $customer = new Customer($this->context->cart->id_customer);
        if (!Validate::isLoadedObject($customer))
            Tools::redirectLink(__PS_BASE_URI__.'order.php?step=1');

        if (Tools::getValue('confirm'))
        {
            $customer = new Customer((int)$this->context->cart->id_customer);
            $total = ($this->context->cart->getOrderTotal(true, Cart::BOTH)+$CODfee);
            /*$this->module->validateOrder((int)$this->context->cart->id, Configuration::get('PS_OS_PREPARATION'), $total, $this->module->displayName, null, array(), null, false, $customer->secure_key);*/
            $cashOnDelivery->validateOrderCOD((int)$this->context->cart->id, Configuration::get('PS_OS_PREPARATION'), $total, $cashOnDelivery->displayName);
            $order = new Order(intval($cashOnDelivery->currentOrder));
            Tools::redirectLink(__PS_BASE_URI__.'order-confirmation.php?key='.$customer->secure_key.'&id_cart='.(int)$this->context->cart->id.'&id_module='.(int)$this->module->id.'&id_order='.(int)$this->module->currentOrder);
        }
        
    }
    public function initContent()
    {
    $cashOnDelivery = new CashOnDeliveryWithFee();
    $CODfee = $cashOnDelivery->getCostValidated($this->context->cart);
        $this->display_column_left = false;
        parent::initContent();    
            $this->context->smarty->assign(array(
                'total' => ($this->context->cart->getOrderTotal(true, Cart::BOTH)+$CODfee),
                'this_path' => $this->module->getPathUri(),//keep for retro compat
                'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->module->name.'/',
            ));
            $this->setTemplate('validation.tpl');

    }
}

Here is the original validation.php:

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../header.php');
include(dirname(__FILE__).'/cashondeliverywithfee.php');

$confirm = Tools::getValue('confirm');

/* Validate order */
if ($confirm)
{
     $cashOnDelivery = new CashOnDeliveryWithFee();
    $total = floatval(number_format($cart->getOrderTotal(true, 3), 2, '.', ''));
    $cashOnDelivery->validateOrderCOD(intval($cart->id), _PS_OS_PREPARATION_, $total, $cashOnDelivery->displayName);
    $order = new Order(intval($cashOnDelivery->currentOrder));
    Tools::redirectLink(__PS_BASE_URI__.'order-confirmation.php?id_cart='.intval($cart->id).'&id_module='.intval($cashOnDelivery->id).'&id_order='.intval($cashOnDelivery->currentOrder));
}
else
{
/* or ask for confirmation */
    $cashOnDelivery = new CashOnDeliveryWithFee();    
    
    $CODfee = $cashOnDelivery->getCostValidated($cart);
    $cartcost = $cart->getOrderTotal(true, 3);
    $total = $CODfee + $cartcost;
    
    $smarty->assign(array(
        'currency' => new Currency(intval($cart->id_currency)),
        'total' => number_format(floatval( $total ), 2, '.', ''),
        'this_path_ssl' => (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'modules/cashondeliverywithfee/'
    ));

    $smarty->assign('this_path', __PS_BASE_URI__.'modules/cashondeliverywithfee/');
 
    //AddOnCompany 21/06/2012
    echo Module::display(dirname(__FILE__), 'validation.tpl');
}

include(dirname(__FILE__).'/../../footer.php');

Is it supposed to overwrite the original order with one that has the fee added? Is that why it has $order = new Order(intval($cashOnDelivery->currentOrder));

Edited by Beluga (see edit history)
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...