Skip to content

Commit

Permalink
feat: allow custom amount of items in mailbox package (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
wthijmen authored Jul 1, 2022
1 parent 5d04892 commit 557f355
Show file tree
Hide file tree
Showing 11 changed files with 751 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Model/Sales/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ public function setMailboxActive(bool $mailboxActive): void
}

/**
* @param float $percentage
* @param float $percentage
*
* @return void
*/
public function setMailboxPercentage(float $percentage): void
{
Expand Down
12 changes: 9 additions & 3 deletions Model/Sales/Repository/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,22 @@ public function productWithoutDeliveryOptions(array $products): PackageRepositor
*
* @return bool
*/
public function fitInMailbox($product, int $mailbox): bool
public function fitInMailbox($product, int $maxAmountProductInMailbox): bool
{
$mailboxPercentage = $this->getMailboxPercentage() + $mailbox * $product->getQty();
$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 &&
($mailboxPercentage === 0 || $mailboxPercentage <= 100) &&
100 >= $mailboxPercentage &&
$orderWeight <= $maximumMailboxWeight
) {
$this->setMailboxPercentage($mailboxPercentage);
Expand Down
59 changes: 59 additions & 0 deletions Setup/Methods/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Magento\Setup\Methods;

use MyParcelNL\Magento\Setup\Methods\Interfaces\QueryInterface;

class Delete implements QueryInterface
{
/**
* @var string
*/
private $table;

/**
* @var array
*/
private $conditions = [];

/**
* @param string $table
*/
public function __construct(string $table)
{
$this->table = $table;
}

/**
* @return string
*/
public function __toString(): string
{
return sprintf(
'DELETE FROM %s%s',
$this->table,
empty($this->conditions)
? ''
: sprintf(
' WHERE %s',
implode(' AND ', $this->conditions)
)
);
}

/**
* @param string ...$where
*
* @return $this
*/
public function where(string ...$where): self
{
foreach ($where as $arg) {
$this->conditions[] = $arg;
}

return $this;
}
}
62 changes: 62 additions & 0 deletions Setup/Methods/Insert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Magento\Setup\Methods;

use MyParcelNL\Magento\Setup\Methods\Interfaces\QueryInterface;

class Insert implements QueryInterface
{
/**
* @var string
*/
private $table;

/**
* @var array
*/
private $columns = [];

/**
* @var array
*/
private $values = [];

/**
* @param string $table
*/
public function __construct(string $table)
{
$this->table = $table;
}

/**
* @return string
*/
public function __toString(): string
{
return sprintf(
'INSERT INTO %s (%s) VALUES (%s)',
$this->table,
implode(', ', $this->columns),
implode(', ', $this->values)
);
}

/**
* @param string ...$columns
*
* @return $this
*/
public function columns(string ...$columns): self
{
$this->columns = $columns;

foreach ($columns as $column) {
$this->values[] = sprintf(':%s', $column);
}

return $this;
}
}
10 changes: 10 additions & 0 deletions Setup/Methods/Interfaces/QueryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Magento\Setup\Methods\Interfaces;

interface QueryInterface
{
public function __toString(): string;
}
207 changes: 207 additions & 0 deletions Setup/Methods/Select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Magento\Setup\Methods;

use MyParcelNL\Magento\Setup\Methods\Interfaces\QueryInterface;

class Select implements QueryInterface
{
/**
* @var array
*/
private $fields = [];

/**
* @var array
*/
private $conditions = [];

/**
* @var array
*/
private $order = [];

/**
* @var array
*/
private $from = [];

/**
* @var array
*/
private $join = [];

/**
* @var array
*/
private $groupBy = [];

/**
* @var
*/
private $limit;

/**
* @var bool
*/
private $distinct = false;

/**
* @param array $select
*/
public function __construct(array $select)
{
$this->fields = $select;
}

/**
* @param string ...$select
*
* @return $this
*/
public function select(string ...$select): self
{
foreach ($select as $arg) {
$this->fields[] = $arg;
}

return $this;
}

/**
* @return string
*/
public function __toString(): string
{
return trim(
sprintf(
'SELECT %s%s FROM %s%s%s%s%s%s',
$this->distinct ? 'DISTINCT ' : '',
implode(', ', $this->fields),
implode(', ', $this->from),
empty($this->join)
? ''
: implode(' ', $this->join),
empty($this->conditions)
? ''
: sprintf(' WHERE %s', implode(' AND ', $this->conditions)),
empty($this->groupBy)
? ''
: sprintf(' GROUP BY %s', implode(', ', $this->groupBy)),
empty($this->order)
? ''
: sprintf(implode(', ', $this->order)),
empty($this->limit)
? ''
: sprintf(' LIMIT %s', $this->limit)
)
);
}

/**
* @param string ...$where
*
* @return $this
*/
public function where(string ...$where): self
{
foreach ($where as $arg) {
$this->conditions[] = $arg;
}

return $this;
}

/**
* @param string $table
* @param null|string $alias
*
* @return $this
*/
public function from(string $table, ?string $alias = null): self
{
$this->from[] = null === $alias ? $table : "${table} AS ${alias}";

return $this;
}

/**
* @param int $limit
*
* @return $this
*/
public function limit(int $limit): self
{
$this->limit = $limit;

return $this;
}

/**
* @param string ...$order
*
* @return $this
*/
public function orderBy(string ...$order): self
{
foreach ($order as $arg) {
$this->order[] = $arg;
}

return $this;
}

/**
* @param string ...$join
*
* @return $this
*/
public function innerJoin(string ...$join): self
{
foreach ($join as $arg) {
$this->join[] = sprintf('INNER JOIN %s', $arg);
}

return $this;
}

/**
* @param string ...$join
*
* @return $this
*/
public function leftJoin(string ...$join): self
{
foreach ($join as $arg) {
$this->join[] = sprintf('LEFT JOIN %s', $arg);
}

return $this;
}

/**
* @return $this
*/
public function distinct(): self
{
$this->distinct = true;

return $this;
}

/**
* @param string ...$groupBy
*
* @return $this
*/
public function groupBy(string ...$groupBy): self
{
foreach ($groupBy as $arg) {
$this->groupBy[] = $arg;
}

return $this;
}
}
Loading

0 comments on commit 557f355

Please sign in to comment.