<?php
/**
* @version EC=CUBE4
* @copyright 株式会社 翔 kakeru.co.jp
* @author
* 2022年01月28日作成
*
* app/Customize/Service/LmDeliveryFee.php
*
*
* ランドマーク運賃・支払い方法の算出は、ここで計算する
*
*
*
* C= C= C= ┌(;・_・)┘トコトコ
******************************************************/
namespace Customize\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Eccube\Repository\DeliveryRepository;
use Eccube\Repository\DeliveryFeeRepository;
use Lm\Service\Db\SqlService;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class LmDeliveryFee
{
protected $Container;
protected $Request;
protected $DeliveryRepository;
protected $DeliveryFeeRepository;
protected $LmHelper;
protected $YamlService;
protected $Session;
const LM_Contact_Flg = 'LM_Contact_Flg';
public function __construct(
ContainerInterface $Container,
RequestStack $Request,
DeliveryRepository $DeliveryRepository,
DeliveryFeeRepository $DeliveryFeeRepository,
YamlService $YamlService,
LmHelper $LmHelper,
SessionInterface $session
) {
$this->Container = $Container;
$this->DeliveryRepository = $DeliveryRepository;
$this->DeliveryFeeRepository = $DeliveryFeeRepository;
$this->YamlService = $YamlService;
$this->LmHelper = $LmHelper;
$this->Request = $Request->getMasterRequest();
$this->Session = $session;
}
/**
* 離島中軽量を返す
*
* @param string PostalCode
* @param int $fee
*/
public function Island($PostalCode)
{
$Sql = new SqlService();
$PostalCode = preg_replace('/[^0-9]+/','',$PostalCode);
$PostalCode = substr_replace($PostalCode, '-', 3, 0);
$Sql = new SqlService();
$Island = $Sql->Table('island_table')
->set('island_postcode', $PostalCode)
->Find();
if(!$Island){
$PostalCode = substr(str_replace("-", "", $PostalCode), 0, 3);
$PostalCode .= "-0000";
$Island = $Sql->Table('island_table')
->set('island_postcode', $PostalCode)
->Find();
}
return $Island['island_price'] ?? 0;
}
/**
* デリバリーフィーの取得
*
*/
public function IsFee($Shipping)
{
$Pref = $Shipping->getPref();
##道府県が沖縄
if (47 == $Pref->getId()) {
return false;
}
$Delivery = $Shipping->getDelivery();
#佐川意外
if (1 != $Delivery->getId()) {
return true;
}
#離島
return $this->Island($Shipping->getPostalCode()) ? false : true;
}
/**
* 配送IDと県で 配送費を取得する
*
* @param int $Deliverryid
* @param int $Prefid
*
* @return int $frr
*
*/
public function getFee($DeliveryId, $PrefId)
{
$em = $this->Container->get('doctrine.orm.entity_manager');
$Qb = $em->createQueryBuilder();
$Delivery = $Qb
->select(['T1'])
->from("Eccube\\Entity\\DeliveryFee", 'T1')
->innerJoin('Eccube\\Entity\\Delivery', 'T2')
->innerJoin('Eccube\\Entity\\Master\\Pref', 'T3')
->where('T2.id = :DeliveryId')
->andWhere('T3.id = :PrefId')
->setParameter('DeliveryId', $DeliveryId)
->setParameter('PrefId', $PrefId)
->getQuery()
->getResult();
if (is_null($Delivery[0])) {
return null;
}
return $Delivery[0]->getFee();
}
/**
* #カートタイプに基づく配送業者を取得
*
* @return object $Deliverys
*/
public function GetLmDelivery($Carts = null)
{
$DeliverMap = $this->YamlService->GetYaml('DeliveryMapping,yaml');
$CartType = $this->LmHelper->getCartType();
if (!$this->LmHelper->hasNotEstimate($Carts) && ($this->LmHelper->hasSusoage($Carts) || $this->LmHelper->hasKyouei($Carts) || $this->LmHelper->hasShishu($Carts))) {
$CartType = CartService::CartTypeNormal;
}
$DeliveryId = $DeliverMap[$CartType] ?? 1;
return $this->DeliveryRepository->findBy(['id' => $DeliveryId]);
}
/**
* #カートタイプに基づく支払い方法の取得
* @param $Deliverys
* @param $Order
* @param $flg 0 OrderType 1 order helper
*
* @return array $Payment
*
*/
public function GetLmPayment($Order, $flg = 0)
{
$CartType = $this->LmHelper->getCartType();
$Payments = [];
foreach ($this->GetLmDelivery() as $Delivery) {
$PaymentOptions = $Delivery->getPaymentOptions();
foreach ($PaymentOptions as $PaymentOption) {
/** @var Payment $Payment */
$Payment = $PaymentOption->getPayment();
//アマゾンペイは アマゾン以外は 弾く
// if (12 == $Payment->getId() && !preg_match('/amazon_pay/', $this->Request->getUri())) {
// continue;
// }
if ($Payment->isVisible()) {
$PaymentFlg = true;
switch (true) {
#加工ありの場合代引きは無しに
case $CartType == 2 && $Payment->getId() == 4:
$PaymentFlg = false;
break;
#会社の場合後払いの表示
case $Order->getCompanyName() && $Payment->getId() == 10:
$PaymentFlg = false;
break;
#個人の場合後払いの表示
case !$Order->getCompanyName() && $Payment->getId() == 11:
$PaymentFlg = false;
break;
}
if ($PaymentFlg) {
if (0 == $flg) {
$Payments[$Delivery->getId()][] = $Payment;
} else {
$Payments[] = $Payment;
}
}
}
} //for
}
return $Payments;
}
/**
* 都道府県ID 郵便番号で 配送費を返す
* 加工で仕様するため 佐川
* @param int PrefId
* @param string PostalCode
*
* @return int $fee;
*
*/
public function GetDeliveryFee($PrefId,$PostalCode = null){
$DeliveryMap= $this->YamlService->GetYaml('DeliveryMapping,yaml');
switch($this->LmHelper->getCartType()){
case CartService::CartTypeCatalog:
case CartService::CartTypeSample:
return 0;
break;
case CartService::CartTypeNormal:
return $this->DeliveryFee($PrefId,$DeliveryMap[1]);
break;
case CartService::CartTypeEstimate:
default:
return $this->DeliveryFee($PrefId,$DeliveryMap[2]) + $this->Island($PostalCode);
break;
}//switch()
}
protected function DeliveryFee($PrefId,$DereyId){
if ($Fee = $this->DeliveryFeeRepository->findOneBy(['Pref'=>$PrefId,'Delivery'=>$DereyId])){
return $Fee->getFee();
}
return 0;
}
}