|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
-
- namespace domain\Order\ProductOrder;
-
- use common\helpers\Price;
- use domain\Config\Unit\UnitSolver;
- use domain\Order\Order\Order;
- use domain\Product\Product\ProductSolver;
- use domain\_\AbstractService;
- use domain\_\SolverInterface;
-
- class ProductOrderSolver extends AbstractService implements SolverInterface
- {
- protected UnitSolver $unitSolver;
- protected ProductSolver $productSolver;
-
- public function loadDependencies(): void
- {
- $this->unitSolver = $this->loadService(UnitSolver::class);
- $this->productSolver = $this->loadService(ProductSolver::class);
- }
-
- public function getQuantityPriorityPieces(ProductOrder $productOrder): string
- {
- $quantityPieces = $this->getQuantityPieces($productOrder);
- if($quantityPieces) {
- return $quantityPieces;
- }
- else {
- return $this->getQuantityWithUnit($productOrder);
- }
- }
-
- public function getQuantityWithUnit(ProductOrder $productOrder): string
- {
- return $productOrder->quantity.' '.$this->productSolver->strUnit($productOrder->product);
- }
-
- public function getQuantityPieces(ProductOrder $productOrder): float
- {
- if ($productOrder->unit == 'piece') {
- return $productOrder->quantity;
- }
- else {
- if ($productOrder->product && $productOrder->product->weight > 0) {
- return $productOrder->quantity / $this->productSolver->getWeightUnitReference($productOrder->product);
- }
- }
-
- return 0;
- }
-
- public function getQuantityToCoefficientOfUnit(ProductOrder $productOrder): float
- {
- return $productOrder->quantity * $this->unitSolver->getUnitCoefficient($productOrder->unit);
- }
-
- public function getPriceWithTax(ProductOrder $productOrder)
- {
- return Price::getPriceWithTax($productOrder->price, $productOrder->taxRate->value);
- }
-
- public function getPriceByTypeTotal(ProductOrder $productOrder, string $typeTotal = Order::AMOUNT_TOTAL)
- {
- if ($typeTotal == Order::INVOICE_AMOUNT_TOTAL && $productOrder->invoice_price !== null) {
- return $productOrder->invoice_price;
- }
-
- return $productOrder->price;
- }
-
- public function getPriceUnitReference(ProductOrder $productOrder, bool $isInvoicePrice = false): ?float
- {
- $productOrderPrice = $productOrder->price;
- if($isInvoicePrice) {
- $productOrderPrice = $this->getInvoicePrice($productOrder);
- }
-
- if($productOrder->unit == 'piece') {
- if($productOrder->product->weight) {
- $price = (1000 * $productOrderPrice) / $productOrder->product->weight;
- }
- else {
- return null;
- }
- }
- else {
- $price = $productOrderPrice;
- }
-
- return $price;
- }
-
- public function getInvoicePrice(ProductOrder $productOrder)
- {
- if($productOrder->invoice_price) {
- return $productOrder->invoice_price;
- }
- else {
- return $productOrder->price;
- }
- }
- }
|