|
- <?php
-
- namespace common\logic\Order\ProductOrder;
-
- use common\logic\BaseService;
- use common\logic\BuilderInterface;
- use common\logic\Config\TaxRate\TaxRate;
- use common\logic\Order\Order\Order;
- use common\logic\PointSale\PointSale\PointSale;
- use common\logic\Product\Product\Product;
- use common\logic\Product\Product\ProductSolver;
- use common\logic\User\User\User;
- use common\logic\User\UserProducer\UserProducer;
-
- class ProductOrderBuilder extends BaseService implements BuilderInterface
- {
- protected ProductSolver $productSolver;
-
- public function __construct()
- {
- $this->productSolver = $this->loadService(ProductSolver::class);
- }
-
- public function instanciate(Order $order, Product $product, float $quantity, float $price): ProductOrder
- {
- $productOrder = new ProductOrder();
-
- $productOrder->populateOrder($order);
- $productOrder->populateProduct($product);
- $productOrder->populateTaxRate($product->taxRate);
- $productOrder->quantity = $quantity;
- $productOrder->price = $price;
- $productOrder->unit = $product->unit;
- $productOrder->step = $product->step;
-
- return $productOrder;
- }
-
- public function create(Order $order, Product $product, float $quantity, float $price): ProductOrder
- {
- $productOrder = $this->instanciate($order, $product, $quantity, $price);
- $productOrder->save();
- return $productOrder;
- }
-
- public function updatePrice(
- ProductOrder $productOrder,
- User $user = null,
- UserProducer $userProducer = null,
- PointSale $pointSale = null,
- int $quantity = 1)
- {
- $productOrder->price = $this->productSolver->getPrice(
- $productOrder->product,
- [
- 'user' => $user,
- 'user_producer' => $userProducer,
- 'point_sale' => $pointSale,
- 'quantity' => $quantity
- ]);
-
- $productOrder->save();
-
- return $productOrder;
- }
-
- public function updateInvoicePrice(ProductOrder $productOrder, array $params = []): void
- {
- $productOrder->invoice_price = $this->productSolver->getPrice($productOrder->product, [
- 'user' => isset($params['user']) ?? null,
- 'user_producer' => isset($params['user_producer']) ?? null,
- 'point_sale' => isset($params['point_sale']) ?? null,
- 'quantity' => $productOrder->quantity
- ]);
- $productOrder->save();
- }
-
- public function deleteByOrder(Order $order): void
- {
- ProductOrder::deleteAll(['id_order' => $order->id]);
- }
- }
|