-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added time-limited price lists with product special prices (#3628)
- Loading branch information
Showing
8 changed files
with
320 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Price; | ||
|
||
use DateTimeInterface; | ||
use Shopsys\FrameworkBundle\Component\Money\Money; | ||
use Shopsys\FrameworkBundle\Model\Pricing\Price; | ||
|
||
class PriceInfo | ||
{ | ||
public Money $priceWithoutVat; | ||
|
||
public Money $priceWithVat; | ||
|
||
public Money $vatAmount; | ||
|
||
public ?DateTimeInterface $nextPriceChange = null; | ||
|
||
public ?float $percentageDiscount = null; | ||
|
||
public Price $basicPrice; | ||
|
||
public bool $isPriceFrom; | ||
|
||
/** | ||
* @param \Shopsys\FrameworkBundle\Model\Pricing\Price $price | ||
*/ | ||
public function setSellingPrice(Price $price): void | ||
{ | ||
$this->priceWithoutVat = $price->getPriceWithoutVat(); | ||
$this->priceWithVat = $price->getPriceWithVat(); | ||
$this->vatAmount = $price->getVatAmount(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Price; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use LogicException; | ||
use Shopsys\FrameworkBundle\Component\Money\Money; | ||
use Shopsys\FrameworkBundle\Model\Pricing\SpecialPrice\SpecialPrice; | ||
use Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPrice; | ||
|
||
class PriceInfoFactory | ||
{ | ||
/** | ||
* @param \Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPrice $basicProductPrice | ||
* @param \Shopsys\FrameworkBundle\Model\Pricing\SpecialPrice\SpecialPrice|null $specialPrice | ||
* @return \Shopsys\FrontendApiBundle\Model\Price\PriceInfo | ||
*/ | ||
public function create( | ||
ProductPrice $basicProductPrice, | ||
?SpecialPrice $specialPrice, | ||
): PriceInfo { | ||
$priceInfo = new PriceInfo(); | ||
$priceInfo->basicPrice = $basicProductPrice; | ||
$priceInfo->isPriceFrom = $basicProductPrice->isPriceFrom(); | ||
|
||
if ($specialPrice === null) { | ||
$priceInfo->setSellingPrice($basicProductPrice); | ||
|
||
return $priceInfo; | ||
} | ||
|
||
$priceInfo->nextPriceChange = $this->determineNextPriceChange($specialPrice); | ||
|
||
if (!$specialPrice->isFuturePrice()) { | ||
$priceInfo->setSellingPrice($specialPrice->price); | ||
$priceInfo->percentageDiscount = $this->calculatePercentageDiscount($basicProductPrice->getPriceWithVat(), $specialPrice->price->getPriceWithVat()); | ||
} else { | ||
$priceInfo->setSellingPrice($basicProductPrice); | ||
} | ||
|
||
return $priceInfo; | ||
} | ||
|
||
/** | ||
* @return \Shopsys\FrontendApiBundle\Model\Price\PriceInfo | ||
*/ | ||
public function createHiddenPriceInfo(): PriceInfo | ||
{ | ||
return $this->create( | ||
ProductPrice::createHiddenProductPrice(), | ||
null, | ||
); | ||
} | ||
|
||
/** | ||
* @param \Shopsys\FrameworkBundle\Model\Pricing\SpecialPrice\SpecialPrice $specialPrice | ||
* @return \DateTimeInterface | ||
*/ | ||
protected function determineNextPriceChange(SpecialPrice $specialPrice): DateTimeInterface | ||
{ | ||
$now = new DateTimeImmutable(); | ||
|
||
$futureDates = []; | ||
|
||
if ($specialPrice->validFrom > $now) { | ||
$futureDates[] = $specialPrice->validFrom; | ||
} | ||
|
||
if ($specialPrice->validTo > $now) { | ||
$futureDates[] = $specialPrice->validTo; | ||
} | ||
|
||
if (count($futureDates) === 0) { | ||
throw new LogicException('Special price was selected, but the validity is in the past. Check the implementation of the special price selection algorithm.'); | ||
} | ||
|
||
return min($futureDates); | ||
} | ||
|
||
/** | ||
* @param \Shopsys\FrameworkBundle\Component\Money\Money $basicPriceWithVat | ||
* @param \Shopsys\FrameworkBundle\Component\Money\Money $specialPriceWithVat | ||
* @return float | ||
*/ | ||
protected function calculatePercentageDiscount( | ||
Money $basicPriceWithVat, | ||
Money $specialPriceWithVat, | ||
): float { | ||
$floatDiscount = $basicPriceWithVat | ||
->subtract($specialPriceWithVat) | ||
->divide($basicPriceWithVat->getAmount(), 6) | ||
->multiply(100); | ||
|
||
return max(1, floor((float)$floatDiscount->getAmount())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Price; | ||
|
||
use Shopsys\FrameworkBundle\Component\Domain\Domain; | ||
use Shopsys\FrameworkBundle\Model\Pricing\SpecialPrice\SpecialPriceFacade; | ||
use Shopsys\FrameworkBundle\Model\Product\Product; | ||
use Shopsys\FrameworkBundle\Model\Product\ProductCachedAttributesFacade; | ||
use Shopsys\FrameworkBundle\Model\Product\ProductTypeEnum; | ||
use Shopsys\FrontendApiBundle\Model\Price\PriceFacade; | ||
use Shopsys\FrontendApiBundle\Model\Price\PriceInfo; | ||
use Shopsys\FrontendApiBundle\Model\Price\PriceInfoFactory; | ||
use Shopsys\FrontendApiBundle\Model\Resolver\AbstractQuery; | ||
use Shopsys\FrontendApiBundle\Model\Resolver\Price\Exception\ProductPriceMissingUserError; | ||
|
||
class ProductPriceQuery extends AbstractQuery | ||
{ | ||
/** | ||
* @param \Shopsys\FrameworkBundle\Model\Pricing\SpecialPrice\SpecialPriceFacade $specialPriceFacade | ||
* @param \Shopsys\FrontendApiBundle\Model\Resolver\Price\SpecialPriceApiFactory $specialPriceApiFactory | ||
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain | ||
* @param \Shopsys\FrontendApiBundle\Model\Price\PriceFacade $priceFacade | ||
* @param \Shopsys\FrameworkBundle\Model\Product\ProductCachedAttributesFacade $productCachedAttributesFacade | ||
* @param \Shopsys\FrontendApiBundle\Model\Price\PriceInfoFactory $priceInfoFactory | ||
*/ | ||
public function __construct( | ||
protected readonly SpecialPriceFacade $specialPriceFacade, | ||
protected readonly SpecialPriceApiFactory $specialPriceApiFactory, | ||
protected readonly Domain $domain, | ||
protected readonly PriceFacade $priceFacade, | ||
protected readonly ProductCachedAttributesFacade $productCachedAttributesFacade, | ||
protected readonly PriceInfoFactory $priceInfoFactory, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array|\Shopsys\FrameworkBundle\Model\Product\Product $data | ||
* @return \Shopsys\FrontendApiBundle\Model\Price\PriceInfo | ||
*/ | ||
public function priceByProductQuery(Product|array $data): PriceInfo | ||
{ | ||
if ($this->isProductUponInquiry($data)) { | ||
return $this->priceInfoFactory->createHiddenPriceInfo(); | ||
} | ||
|
||
if ($data instanceof Product) { | ||
$basicProductPrice = $this->productCachedAttributesFacade->getProductBasicPrice($data); | ||
|
||
if ($basicProductPrice === null) { | ||
throw new ProductPriceMissingUserError('The product price is not set.'); | ||
} | ||
|
||
$specialPrice = $this->specialPriceFacade->findRelevantSpecialPrice($data, $this->domain->getId(), $basicProductPrice); | ||
} else { | ||
$basicProductPrice = $this->priceFacade->createProductPriceFromArrayForCurrentCustomer($data['prices']); | ||
$specialPrice = $this->specialPriceApiFactory->createSpecialPriceFromArray($data, $basicProductPrice); | ||
} | ||
|
||
return $this->priceInfoFactory->create( | ||
$basicProductPrice, | ||
$specialPrice, | ||
); | ||
} | ||
|
||
/** | ||
* @param \Shopsys\FrameworkBundle\Model\Product\Product|array $data | ||
* @return bool | ||
*/ | ||
protected function isProductUponInquiry(Product|array $data): bool | ||
{ | ||
$productType = $data instanceof Product ? $data->getProductType() : $data['product_type']; | ||
|
||
return $productType === ProductTypeEnum::TYPE_INQUIRY; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.