<?php
/**
* Copyright(c) 2019 SYSTEM_KD
* Date: 2019/03/23
*/
namespace Plugin\QuantityDiscountDx\Service\PurchaseFlow\Processor;
use Eccube\Annotation\CartFlow;
use Eccube\Annotation\ShoppingFlow;
use Eccube\Entity\ItemInterface;
use Eccube\Entity\OrderItem;
use Eccube\Entity\CartItem; //20240919@hanari
use Eccube\Service\PurchaseFlow\ItemPreprocessor;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Plugin\QuantityDiscountDx\Service\ConfigService;
use Plugin\QuantityDiscountDx\Service\QuantityDiscountService;
/**
* @CartFlow
* @ShoppingFlow
*
* Class QuantityDiscountPreprocessor
* @package Plugin\QuantityDiscountDx\Service\PurchaseFlow\Processor
*/
class QuantityDiscountPreprocessor implements ItemPreprocessor
{
/** @var QuantityDiscountService */
protected $quantityDiscountService;
/** @var ConfigService */
protected $configService;
public function __construct(
QuantityDiscountService $quantityDiscountService,
ConfigService $configService
)
{
$this->quantityDiscountService = $quantityDiscountService;
$this->configService = $configService;
}
/**
* 受注データ調整処理。
*
* @param ItemInterface $item
* @param PurchaseContext $context
*/
public function process(ItemInterface $item, PurchaseContext $context)
{
if (!$item->isProduct()) {
return;
}
if ($item instanceof OrderItem) {
// OrderItem
$incTax = false;
if (ConfigService::DISCOUNT_BEFORE_PRICE
== $this->configService->getKeyInteger(ConfigService::SETTING_KEY_DELIVERY_FEE_MODE)) {
return;
}
} else {
// CartItem
$incTax = true;
}
//20240915@hanari
dump('オプション情報処理');
if($this->quantityDiscountService->isDiscount($item)){
// 直接値引
dump('直接値引',$incTax);
if($incTax) {
$discountPrice = $this->quantityDiscountService->getProductClassPriceIncTax($item);
} else {
$discountPrice = $this->quantityDiscountService->getProductClassPrice($item);
}
}else{
//値引き前に戻す
//通常価格の取得
$discountPrice = $item->getProductClass()->getPrice02IncTax();
}
dump('オプション追加前',$discountPrice);
// オプション価格の追加 20240915@hanari
if(method_exists($item,'getCartItemOptions') && is_array( $item->getCartItemOptions() )){
dump($item);
$CartItemOptions = $item->getCartItemOptions();
$OptionPrice = 0;
$OptionTax = 0;
//$DeliveryDaysMax = 0;
foreach($CartItemOptions as $CartItemOption){
//dump($CartItemOption->getOptionPrice());
//dump($CartItemOption->getOptionTax());
/*
if($DeliveryDaysMax < $CartItemOption->getOptionDeliveryDays()){
$DeliveryDaysMax = intval($CartItemOption->getOptionDeliveryDays());
}
*/
//dump($DeliveryDaysMax);
$OptionPrice += $CartItemOption->getOptionPrice();
$OptionTax += $CartItemOption->getOptionTax();
}
/*
if($DeliveryDaysMax > $item->getProductClass()->getDeliveryDateDays()){
$item->getProductClass()->setDeliveryDateDays($DeliveryDaysMax);
}
*/
dump($item);
//オプション分を追加
$discountPrice += $OptionPrice + $OptionTax;
}
if($item instanceof OrderItem){
$discountPrice = $item->getPrice();
}
dump('オプション追加後',$discountPrice);
if ($discountPrice < 0) {
return;
}
$item->setPrice($discountPrice);
}
}