You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 line
3.1KB

  1. <?php
  2. namespace domain\Order\ProductOrder;
  3. use common\helpers\Price;
  4. use domain\Config\Unit\UnitSolver;
  5. use domain\Order\Order\Order;
  6. use domain\Product\Product\ProductSolver;
  7. use domain\_\AbstractService;
  8. use domain\_\SolverInterface;
  9. class ProductOrderSolver extends AbstractService implements SolverInterface
  10. {
  11. protected UnitSolver $unitSolver;
  12. protected ProductSolver $productSolver;
  13. public function loadDependencies(): void
  14. {
  15. $this->unitSolver = $this->loadService(UnitSolver::class);
  16. $this->productSolver = $this->loadService(ProductSolver::class);
  17. }
  18. public function getQuantityPriorityPieces(ProductOrder $productOrder): string
  19. {
  20. $quantityPieces = $this->getQuantityPieces($productOrder);
  21. if($quantityPieces) {
  22. return $quantityPieces;
  23. }
  24. else {
  25. return $this->getQuantityWithUnit($productOrder);
  26. }
  27. }
  28. public function getQuantityWithUnit(ProductOrder $productOrder): string
  29. {
  30. return $productOrder->quantity.' '.$this->productSolver->strUnit($productOrder->product);
  31. }
  32. public function getQuantityPieces(ProductOrder $productOrder): float
  33. {
  34. if ($productOrder->unit == 'piece') {
  35. return $productOrder->quantity;
  36. }
  37. else {
  38. if ($productOrder->product && $productOrder->product->weight > 0) {
  39. return $productOrder->quantity / $this->productSolver->getWeightUnitReference($productOrder->product);
  40. }
  41. }
  42. return 0;
  43. }
  44. public function getQuantityToCoefficientOfUnit(ProductOrder $productOrder): float
  45. {
  46. return $productOrder->quantity * $this->unitSolver->getUnitCoefficient($productOrder->unit);
  47. }
  48. public function getPriceWithTax(ProductOrder $productOrder)
  49. {
  50. return Price::getPriceWithTax($productOrder->price, $productOrder->taxRate->value);
  51. }
  52. public function getPriceByTypeTotal(ProductOrder $productOrder, string $typeTotal = Order::AMOUNT_TOTAL)
  53. {
  54. if ($typeTotal == Order::INVOICE_AMOUNT_TOTAL && $productOrder->invoice_price !== null) {
  55. return $productOrder->invoice_price;
  56. }
  57. return $productOrder->price;
  58. }
  59. public function getPriceUnitReference(ProductOrder $productOrder, bool $isInvoicePrice = false): ?float
  60. {
  61. $productOrderPrice = $productOrder->price;
  62. if($isInvoicePrice) {
  63. $productOrderPrice = $this->getInvoicePrice($productOrder);
  64. }
  65. if($productOrder->unit == 'piece') {
  66. if($productOrder->product->weight) {
  67. $price = (1000 * $productOrderPrice) / $productOrder->product->weight;
  68. }
  69. else {
  70. return null;
  71. }
  72. }
  73. else {
  74. $price = $productOrderPrice;
  75. }
  76. return $price;
  77. }
  78. public function getInvoicePrice(ProductOrder $productOrder)
  79. {
  80. if($productOrder->invoice_price) {
  81. return $productOrder->invoice_price;
  82. }
  83. else {
  84. return $productOrder->price;
  85. }
  86. }
  87. }