src/Eccube/Service/TaxRuleService.php line 50

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Service;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\ProductClass;
  15. use Eccube\Repository\BaseInfoRepository;
  16. use Eccube\Repository\TaxRuleRepository;
  17. class TaxRuleService
  18. {
  19.     /**
  20.      * @var BaseInfo
  21.      */
  22.     protected $BaseInfo;
  23.     /**
  24.      * @var TaxRuleRepository
  25.      */
  26.     protected $taxRuleRepository;
  27.     public function __construct(TaxRuleRepository $taxRuleRepositoryBaseInfoRepository $baseInfoRepository)
  28.     {
  29.         $this->taxRuleRepository $taxRuleRepository;
  30.         $this->BaseInfo $baseInfoRepository->get();
  31.     }
  32.     /**
  33.      * 設定情報に基づいて税金の金額を返す
  34.      *
  35.      * @param  int                                    $price        計算対象の金額
  36.      * @param  int|\Eccube\Entity\Product|null        $product      商品
  37.      * @param  int|\Eccube\Entity\ProductClass|null   $productClass 商品規格
  38.      * @param  int|\Eccube\Entity\Master\Pref|null    $pref         都道府県
  39.      * @param  int|\Eccube\Entity\Master\Country|null $country      国
  40.      *
  41.      * @return double                                 税金付与した金額
  42.      */
  43.     public function getTax($price$product null$productClass null$pref null$country null)
  44.     {
  45.         /*
  46.          * 商品別税率が有効で商品別税率が設定されている場合は商品別税率
  47.          * 商品別税率が有効で商品別税率が設定されていない場合は基本税率
  48.          * 商品別税率が無効の場合は基本税率
  49.          */
  50.         /* @var $TaxRule \Eccube\Entity\TaxRule */
  51.         if ($this->BaseInfo->isOptionProductTaxRule() && $productClass) {
  52.             if ($productClass instanceof ProductClass) {
  53.                 $TaxRule $productClass->getTaxRule() ?: $this->taxRuleRepository->getByRule(nullnull$pref$country);
  54.             } else {
  55.                 $TaxRule $this->taxRuleRepository->getByRule($product$productClass$pref$country);
  56.             }
  57.         } else {
  58.             $TaxRule $this->taxRuleRepository->getByRule(nullnull$pref$country);
  59.         }
  60.         return $this->calcTax($price$TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(), $TaxRule->getTaxAdjust());
  61.     }
  62.     /**
  63.      * calcIncTax
  64.      *
  65.      * @param  int                                    $price        計算対象の金額
  66.      * @param  int|\Eccube\Entity\Product|null        $product      商品
  67.      * @param  int|\Eccube\Entity\ProductClass|null   $productClass 商品規格
  68.      * @param  int|\Eccube\Entity\Master\Pref|null    $pref         都道府県
  69.      * @param  int|\Eccube\Entity\Master\Country|null $country      国
  70.      *
  71.      * @return double
  72.      */
  73.     public function getPriceIncTax($price$product null$productClass null$pref null$country null)
  74.     {
  75.         return $price $this->getTax($price$product$productClass$pref$country);
  76.     }
  77.     /**
  78.      * 税金額を計算する
  79.      *
  80.      * @param  int    $price     計算対象の金額
  81.      * @param  int    $taxRate   税率(%単位)
  82.      * @param  int    $RoundingType  端数処理
  83.      * @param  int    $taxAdjust 調整額
  84.      *
  85.      * @return double 税金額
  86.      */
  87.     public function calcTax($price$taxRate$RoundingType$taxAdjust 0)
  88.     {
  89.         $tax $price $taxRate 100;
  90.         $roundTax self::roundByRoundingType($tax$RoundingType);
  91.         return $roundTax $taxAdjust;
  92.     }
  93.     /**
  94.      * 税込金額から税金額を計算する
  95.      *
  96.      * @param  int    $price     計算対象の金額
  97.      * @param  int    $taxRate   税率(%単位)
  98.      * @param  int    $RoundingType  端数処理
  99.      * @param  int    $taxAdjust 調整額
  100.      *
  101.      * @return float  税金額
  102.      */
  103.     public function calcTaxIncluded($price$taxRate$RoundingType$taxAdjust 0)
  104.     {
  105.         $tax = ($price $taxAdjust) * $taxRate / (100 $taxRate);
  106.         return self::roundByRoundingType($tax$RoundingType);
  107.     }
  108.     /**
  109.      * 課税規則に応じて端数処理を行う
  110.      *
  111.      * @param  integer $value    端数処理を行う数値
  112.      * @param integer $RoundingType
  113.      *
  114.      * @return double        端数処理後の数値
  115.      */
  116.     public static function roundByRoundingType($value$RoundingType)
  117.     {
  118.         switch ($RoundingType) {
  119.             // 四捨五入
  120.             case \Eccube\Entity\Master\RoundingType::ROUND:
  121.                 $ret round($value);
  122.                 break;
  123.             // 切り捨て
  124.             case \Eccube\Entity\Master\RoundingType::FLOOR:
  125.                 $ret floor($value);
  126.                 break;
  127.             // 切り上げ
  128.             case \Eccube\Entity\Master\RoundingType::CEIL:
  129.                 $ret ceil($value);
  130.                 break;
  131.             // デフォルト:切り上げ
  132.             default:
  133.                 $ret ceil($value);
  134.                 break;
  135.         }
  136.         return $ret;
  137.     }
  138. }