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.

86 satır
2.9KB

  1. <?php
  2. namespace domain\Order\ProductOrder;
  3. use domain\Order\Order\Order;
  4. use domain\PointSale\PointSale\PointSale;
  5. use domain\Product\Product\Product;
  6. use domain\Product\Product\ProductSolver;
  7. use domain\User\User\User;
  8. use domain\User\UserProducer\UserProducer;
  9. use domain\_\AbstractBuilder;
  10. class ProductOrderBuilder extends AbstractBuilder
  11. {
  12. protected ProductSolver $productSolver;
  13. public function loadDependencies(): void
  14. {
  15. $this->productSolver = $this->loadService(ProductSolver::class);
  16. }
  17. public function instanciateProductOrder(Order $order, Product $product, float $quantity, float $price): ProductOrder
  18. {
  19. $productOrder = new ProductOrder();
  20. $productOrder->populateOrder($order);
  21. $productOrder->populateProduct($product);
  22. $productOrder->populateTaxRate($product->taxRate);
  23. $productOrder->quantity = $quantity;
  24. $productOrder->price = $price;
  25. $productOrder->unit = $product->unit;
  26. $productOrder->step = $product->step;
  27. return $productOrder;
  28. }
  29. public function createProductOrder(Order $order, Product $product, float $quantity, float $price): ProductOrder
  30. {
  31. $productOrder = $this->instanciateProductOrder($order, $product, $quantity, $price);
  32. $this->saveCreate($productOrder);
  33. return $productOrder;
  34. }
  35. public function updateProductOrderPrice(
  36. ProductOrder $productOrder,
  37. User $user = null,
  38. UserProducer $userProducer = null,
  39. PointSale $pointSale = null,
  40. int $quantity = 1)
  41. {
  42. $productOrder->price = $this->productSolver->getPrice(
  43. $productOrder->product,
  44. [
  45. 'user' => $user,
  46. 'user_producer' => $userProducer,
  47. 'point_sale' => $pointSale,
  48. 'quantity' => $quantity
  49. ]);
  50. $this->update($productOrder);
  51. return $productOrder;
  52. }
  53. public function updateProductOrderInvoicePrice(ProductOrder $productOrder, array $params = []): void
  54. {
  55. $productOrder->invoice_price = $this->productSolver->getPrice($productOrder->product, [
  56. 'user' => (isset($params['user']) && $params['user']) ? $params['user'] : null,
  57. 'user_producer' => (isset($params['user_producer']) && $params['user_producer']) ? $params['user_producer'] : null,
  58. 'point_sale' => (isset($params['point_sale']) && $params['point_sale']) ? $params['point_sale'] : null,
  59. 'quantity' => $productOrder->quantity
  60. ]);
  61. $this->update($productOrder);
  62. }
  63. public function updateProductOrderInvoicePriceByValue(ProductOrder $productOrder, float $invoicePrice)
  64. {
  65. $productOrder->invoice_price = $invoicePrice;
  66. $this->update($productOrder);
  67. }
  68. public function deleteProductOrdersByOrder(Order $order): void
  69. {
  70. ProductOrder::deleteAll(['id_order' => $order->id]);
  71. }
  72. }