選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

82 行
2.6KB

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