Skip to content

Commit

Permalink
fix: fix mailbox amount (#714)
Browse files Browse the repository at this point in the history
Co-authored-by: Joeri van Veen <joeri@myparcel.nl>
  • Loading branch information
Mark-Ernst and joerivanveen authored Oct 4, 2022
1 parent 793e78c commit c9970f5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 153 deletions.
1 change: 0 additions & 1 deletion Model/Quote/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ public function checkPackageType(string $carrier, ?string $country): string
$this->package->setCurrentCountry($country);
$this->package->setDigitalStampActive($canHaveDigitalStamp && $this->helper->getBoolConfig($carrierPath, 'digital_stamp/active'));
$this->package->setMailboxActive($canHaveMailbox && $this->helper->getBoolConfig($carrierPath, 'mailbox/active'));
$this->package->setWeightFromQuoteProducts($products);

return $this->package->selectPackageType($products, $carrierPath);
}
Expand Down
7 changes: 3 additions & 4 deletions Model/Rate/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(
$this->quote = $quote;
$this->parentMethods = explode(',', $this->myParcelHelper->getGeneralConfig('shipping_methods/methods'));
$package->setCurrentCountry(
$this->getQuoteFromCardOrSession()
$this->getQuoteFromCartOrSession()
->getShippingAddress()
->getCountryId()
);
Expand Down Expand Up @@ -180,9 +180,8 @@ private function addMyParcelRates(Method $parentRate): void
$this->package->setMailboxSettings($carrierPath);
$this->package->setDigitalStampSettings($carrierPath);

//TODO: get the correct packagetype from products story: MY-34504
$packageType = $this->package->selectPackageType(
$this->getQuoteFromCardOrSession()->getAllItems(),
$this->getQuoteFromCartOrSession()->getAllItems(),
$carrierPath
);

Expand Down Expand Up @@ -465,7 +464,7 @@ private function getPrice($settingPath): float
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getQuoteFromCardOrSession()
private function getQuoteFromCartOrSession()
{
if ($this->quote->getQuoteId() != null && $this->quote->getQuote()
&& $this->quote->getQuote() instanceof Countable
Expand Down
20 changes: 8 additions & 12 deletions Model/Sales/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class Package extends Data implements PackageInterface
private $digitalStampActive = false;

/**
* @var int
* @var float
*/
private $mailboxPercentage = 0;
private $mailboxPercentage = 0.0;

/**
* @var bool
Expand Down Expand Up @@ -178,9 +178,9 @@ public function setMailboxPercentage(float $percentage): void
}

/**
* @return bool
* @return float
*/
public function getMailboxPercentage(): bool
public function getMailboxPercentage(): float
{
return $this->mailboxPercentage;
}
Expand All @@ -201,17 +201,10 @@ public function setDigitalStampActive(bool $digitalStampActive): void
$this->digitalStampActive = $digitalStampActive;
}

/**
* @return bool
*/
public function isAllProductsFit(): bool
{
return $this->allProductsFit;
}


/**
* @param bool $allProductsFit
* @deprecated fit in what? use PackageRepository->selectPackageType() to get the relevant package type
*/
public function setAllProductsFit(bool $allProductsFit): void
{
Expand All @@ -225,6 +218,9 @@ public function setAllProductsFit(bool $allProductsFit): void
*/
public function getPackageType(): int
{
if (! isset($this->packageType)) {
throw new \RuntimeException('Use setPackageType() before you can getPackageType()');
}
return $this->packageType;
}

Expand Down
180 changes: 44 additions & 136 deletions Model/Sales/Repository/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,6 @@ class PackageRepository extends Package
*/
public $deliveryOptionsDisabled = false;

/**
* @var bool
*/
public $isPackage = true;

/**
* Get package type
*
* If package type is not set, calculate package type
*
* @return int 1|3
*/
public function getPackageType(): int
{
// return type if type is set
if (parent::getPackageType() !== null) {
return parent::getPackageType();
}

return parent::getPackageType();
}

/**
* @param array $products
* @param string $carrierPath
Expand All @@ -67,40 +45,47 @@ public function selectPackageType(array $products, string $carrierPath): string
return AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
}

$packageType = [];

if ($this->isMailboxActive() || $this->isDigitalStampActive()) {
foreach ($products as $product) {
$digitalStamp = $this->getAttributesProductsOptions($product, 'digital_stamp');
$mailbox = $this->getAttributesProductsOptions($product, 'fit_in_mailbox');
$isPackage = true;

if ($digitalStamp && $this->fitInDigitalStamp()) {
$packageType[] = AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP;
$isPackage = false;
continue;
}

if (isset($mailbox) && $this->fitInMailbox($product, $mailbox)) {
$packageType[] = AbstractConsignment::PACKAGE_TYPE_MAILBOX;
$isPackage = false;
continue;
}

if ($isPackage) {
$packageType[] = AbstractConsignment::PACKAGE_TYPE_PACKAGE;
break;
}
$this->setMailboxPercentage(0);
$weight = 0;
$digitalStamp = true;
foreach ($products as $product) {
$productQty = $product->getQty();
$productWeight = $product->getWeight();

if ($productQty < 1) {
continue;
}

if ($digitalStamp && ! $this->getAttributesProductsOptions($product, 'digital_stamp')) {
$digitalStamp = false;
}

$mailboxQty = $this->getAttributesProductsOptions($product, 'fit_in_mailbox');
if (0 === $mailboxQty) {
$mailboxQty = (int) ($this->getMaxMailboxWeight() / $productWeight);
}

if (0 !== $mailboxQty) {
$productPercentage = $productQty * 100 / $mailboxQty;
$mailboxPercentage = $this->getMailboxPercentage() + $productPercentage;
$this->setMailboxPercentage($mailboxPercentage);
}

if ($productWeight > 0) {
$weight += $productWeight * $productQty;
}
}
$this->setWeight($weight);

// Sort an array in reverse order, so that the largest package type appears at the bottom of the array
rsort($packageType);
if ($digitalStamp && $this->fitInDigitalStamp()) {
return AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME;
}

$packageType = array_pop($packageType);
$packageTypeNames = array_flip(AbstractConsignment::PACKAGE_TYPES_NAMES_IDS_MAP);
if ($this->fitInMailbox()) {
return AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME;
}

return $packageTypeNames[$packageType] ?? AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
return AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
}

/**
Expand All @@ -118,35 +103,14 @@ public function productWithoutDeliveryOptions(array $products): PackageRepositor
}

/**
* @param object $product
* @param int $mailbox
*
* @return bool
*/
public function fitInMailbox($product, int $maxAmountProductInMailbox): bool
public function fitInMailbox(): bool
{
$productPercentage = 0;

if (0 !== $maxAmountProductInMailbox && $product->getQty()) {
$productPercentage = $product->getQty() * 100 / $maxAmountProductInMailbox;
}

$mailboxPercentage = $this->getMailboxPercentage() + $productPercentage;
$maximumMailboxWeight = $this->getWeightTypeOfOption($this->getMaxMailboxWeight());
$orderWeight = $this->getWeightTypeOfOption($this->getWeight());
if (
$this->getCurrentCountry() === AbstractConsignment::CC_NL &&
$this->isMailboxActive() &&
$orderWeight &&
100 >= $mailboxPercentage &&
$orderWeight <= $maximumMailboxWeight
) {
$this->setMailboxPercentage($mailboxPercentage);

return true;
}

return false;
return $this->getCurrentCountry() === AbstractConsignment::CC_NL
&& $this->isMailboxActive()
&& $this->getWeight() <= $this->getMaxMailboxWeight()
&& $this->getMailboxPercentage() <= 100;
}

/**
Expand All @@ -157,34 +121,9 @@ public function fitInDigitalStamp(): bool
$orderWeight = $this->getWeightTypeOfOption($this->getWeight());
$maximumDigitalStampWeight = $this->getMaxDigitalStampWeight();

if (
$this->getCurrentCountry() === AbstractConsignment::CC_NL &&
$this->isDigitalStampActive() &&
$orderWeight <= $maximumDigitalStampWeight
) {
return true;
}
return false;
}

/**
* Set weight depend on product weight from product
*
* @param \Magento\Quote\Model\Quote\Item[] $products
*
* @return $this
*/
public function setWeightFromQuoteProducts(array $products)
{
if (empty($products)) {
return $this;
}

foreach ($products as $product) {
$this->setWeightFromOneQuoteProduct($product);
}

return $this;
return $this->getCurrentCountry() === AbstractConsignment::CC_NL
&& $this->isDigitalStampActive()
&& $orderWeight <= $maximumDigitalStampWeight;
}

/**
Expand Down Expand Up @@ -215,21 +154,6 @@ public function setMailboxSettings(string $carrierPath = self::XML_PATH_POSTNL_S
return $this;
}

/**
* @param $products
*
* @return float
*/
public function getProductsWeight(array $products): float
{
$weight = 0;
foreach ($products as $item) {
$weight += ($item->getWeight() * $item->getQty());
}

return $weight;
}

/**
* @param $products
*
Expand Down Expand Up @@ -393,20 +317,4 @@ private function getValueFromAttribute($connection, string $tableName, string $a

return $connection->fetchOne($sql);
}

/**
* @param \Magento\Quote\Model\Quote\Item $product
*
* @return $this
*/
private function setWeightFromOneQuoteProduct($product)
{
if ($product->getWeight() > 0) {
$this->addWeight($product->getWeight() * $product->getQty());
} else {
$this->setAllProductsFit(false);
}

return $this;
}
}

0 comments on commit c9970f5

Please sign in to comment.