Example #1
0
 /**
  * returns the rounded value of $value to specified precision, according to your configuration;
  *
  * @note : PHP 5.3.0 introduce a 3rd parameter mode in round function
  *
  * @param float $value
  * @param int $precision
  * @return float
  */
 public static function ps_round($value, $precision = 0)
 {
     if (Tools::$round_mode == null) {
         Tools::$round_mode = (int) Configuration::get('PS_PRICE_ROUND_MODE');
     }
     switch (Tools::$round_mode) {
         case PS_ROUND_UP:
             return Tools::ceilf($value, $precision);
         case PS_ROUND_DOWN:
             return Tools::floorf($value, $precision);
         case PS_ROUND_HALF_DOWN:
         case PS_ROUND_HALF_EVEN:
         case PS_ROUND_HALF_ODD:
             return Tools::math_round($value, $precision, Tools::$round_mode);
         case PS_ROUND_HALF_UP:
         default:
             return Tools::math_round($value, $precision, PS_ROUND_HALF_UP);
     }
 }
Example #2
0
    /**
     * Returns the correct product taxes breakdown.
     *
     * @since 1.5
     * @return array
     */
    public function getProductTaxesBreakdown($order = null)
    {
        Tools::$round_mode = $order->round_mode;
        $tmp_tax_infos = array();
        if ($this->useOneAfterAnotherTaxComputationMethod()) {
            // sum by taxes
            $taxes_infos = Db::getInstance()->executeS('
			SELECT t.`rate` AS `name`, t.`rate`, SUM(`total_amount`) AS `total_amount`
			FROM `' . _DB_PREFIX_ . 'order_detail_tax` odt
			LEFT JOIN `' . _DB_PREFIX_ . 'tax` t ON (t.`id_tax` = odt.`id_tax`)
			LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` od ON (od.`id_order_detail` = odt.`id_order_detail`)
			WHERE od.`id_order` = ' . (int) $this->id_order . '
			AND od.`id_order_invoice` = ' . (int) $this->id . '
			GROUP BY odt.`id_tax`
			');
            // format response
            foreach ($taxes_infos as $tax_infos) {
                $tmp_tax_infos[$tax_infos['rate']]['total_amount'] = $tax_infos['total_amount'];
                $tmp_tax_infos[$tax_infos['rate']]['name'] = $tax_infos['name'];
            }
        } else {
            // sum by order details in order to retrieve real taxes rate
            $taxes_infos = Db::getInstance()->executeS('
			SELECT t.`rate` AS `name`, od.`total_price_tax_excl` AS total_price_tax_excl, SUM(t.`rate`) AS rate, SUM(`total_amount`) AS `total_amount`, od.`ecotax`, od.`ecotax_tax_rate`, od.`product_quantity`
			FROM `' . _DB_PREFIX_ . 'order_detail_tax` odt
			LEFT JOIN `' . _DB_PREFIX_ . 'tax` t ON (t.`id_tax` = odt.`id_tax`)
			LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` od ON (od.`id_order_detail` = odt.`id_order_detail`)
			WHERE od.`id_order` = ' . (int) $this->id_order . '
			AND od.`id_order_invoice` = ' . (int) $this->id . '
			GROUP BY odt.`id_order_detail`
			');
            // sum by taxes
            $tmp_tax_infos = array();
            $shipping_tax_amount = 0;
            foreach ($order->getCartRules() as $cart_rule) {
                if ($cart_rule['free_shipping']) {
                    $shipping_tax_amount = $this->total_shipping_tax_excl;
                    break;
                }
            }
            foreach ($taxes_infos as $tax_infos) {
                if (!isset($tmp_tax_infos[$tax_infos['rate']])) {
                    $tmp_tax_infos[$tax_infos['rate']] = array('total_amount' => 0, 'name' => 0, 'total_price_tax_excl' => 0);
                }
                $ratio = $tax_infos['total_price_tax_excl'] / $this->total_products;
                $order_reduction_amount = ($this->total_discount_tax_excl - $shipping_tax_amount) * $ratio;
                $tmp_tax_infos[$tax_infos['rate']]['total_amount'] += $tax_infos['total_amount'] - Tools::ps_round($tax_infos['ecotax'] * $tax_infos['product_quantity'] * $tax_infos['ecotax_tax_rate'] / 100, _PS_PRICE_COMPUTE_PRECISION_);
                $tmp_tax_infos[$tax_infos['rate']]['name'] = $tax_infos['name'];
                $tmp_tax_infos[$tax_infos['rate']]['total_price_tax_excl'] += $tax_infos['total_price_tax_excl'] - $order_reduction_amount - Tools::ps_round($tax_infos['ecotax'] * $tax_infos['product_quantity'], _PS_PRICE_COMPUTE_PRECISION_);
            }
        }
        foreach ($tmp_tax_infos as &$tax) {
            $tax['total_amount'] = Tools::ps_round($tax['total_amount'], _PS_PRICE_DISPLAY_PRECISION_);
            $tax['total_price_tax_excl'] = Tools::ps_round($tax['total_price_tax_excl'], _PS_PRICE_DISPLAY_PRECISION_);
        }
        return $tmp_tax_infos;
    }