Skip to content

Commit

Permalink
価格, 税率が null の場合を考慮
Browse files Browse the repository at this point in the history
  • Loading branch information
nanasess committed Aug 15, 2019
1 parent 07d835d commit 89f4199
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions data/class/SC_Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ public function isValidProductId($product_id, $include_hidden = false, $include_
/**
* 商品規格別の税率を取得する.
*
* 商品規格別税率の設定の無い商品は基本税率を適用する
*
* @param array $product_ids 取得対象の商品ID
* @param int $option_product_tax_rule 商品別税率オプション
* @return array 税率を含む商品ID, 商品規格IDごとの配列. $option_product_tax_rule が 0 の場合は空の配列を返す
Expand All @@ -762,19 +764,21 @@ protected static function getProductsClassRelateTaxRule(array $product_ids, $opt
return [];
}

$arrDefaultTaxRule = SC_Helper_TaxRule_Ex::getTaxRule();
$objQuery = SC_Query_Ex::getSingletonInstance();
$objQuery->setOrder('product_class_id');
$arrProductClasses = $objQuery->select(
'*,
(SELECT tax_rate FROM dtb_tax_rule
WHERE product_class_id = dtb_products_class.product_class_id
AND product_id = dtb_products_class.product_id
AND del_flg = 0 AND apply_date < CURRENT_TIMESTAMP) as tax_rate',
COALESCE(
(SELECT tax_rate FROM dtb_tax_rule
WHERE product_class_id = dtb_products_class.product_class_id
AND product_id = dtb_products_class.product_id
AND del_flg = 0 AND apply_date < CURRENT_TIMESTAMP)
, '.$arrDefaultTaxRule['tax_rate'].') as tax_rate',
'dtb_products_class',
'product_id IN ('.SC_Utils_Ex::repeatStrWithSeparator('?', count($product_ids)).') AND del_flg = 0',
$product_ids
);

$arrResult = [];
if (is_array($arrProductClasses)) {
foreach ($arrProductClasses as $arrProductClass) {
Expand All @@ -797,6 +801,10 @@ protected static function getProductsClassRelateTaxRule(array $product_ids, $opt
*/
protected static function findProductClassIdByRule($col, $rules, $type = 'max')
{
// 価格が null の商品規格は除外
$rules = array_filter($rules, function ($rule) use ($col) {
return $rule[$col] !== null;
});
return array_reduce($rules, function ($carry, $rule) use ($col, $rules, $type) {
if (SC_Product_Ex::checkPriceAndTaxRate($rules[$carry][$col], $rule[$col], $rules[$carry]['tax_rate'], $rule['tax_rate'], $type)) {
return $rule['product_class_id'];
Expand All @@ -807,7 +815,7 @@ protected static function findProductClassIdByRule($col, $rules, $type = 'max')
}

/**
* 金額と税率の max or min を評価する.
* 税込金額の max or min を評価する.
*
* @param int $carry_price 現在の金額
* @param int $price 比較対象の金額
Expand All @@ -818,13 +826,20 @@ protected static function findProductClassIdByRule($col, $rules, $type = 'max')
*/
protected static function checkPriceAndTaxRate($carry_price, $price, $carry_rate, $rate, $operator = 'max')
{
if ($price === null) {
return false;
}

$carry_intax = $carry_price + ($carry_price * ($carry_rate / 100));
$intax = $price + ($price * ($rate / 100));
// 比較するのみなので端数処理は考慮しない
switch ($operator) {
case 'min':
return ($carry_price >= $price && $carry_rate >= $rate);
return ($carry_intax >= $intax);
break;
case 'max':
default:
return ($carry_price <= $price && $carry_rate <= $rate);
return ($carry_intax <= $intax);
}
}
}

0 comments on commit 89f4199

Please sign in to comment.