1
0

SaleItem.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App;
  3. use NumberFormatter;
  4. /**
  5. * @property int $id
  6. * @property int $sale_id
  7. * @property int $product_id
  8. * @property int $quantity
  9. * @property string $unit_price
  10. */
  11. class SaleItem extends Model implements \JsonSerializable {
  12. protected static $table = "sale_item";
  13. protected static $idField = "id";
  14. protected static $fillableFields = [
  15. "sale_id", "product_id",
  16. "quantity", "unit_price",
  17. ];
  18. /**
  19. * @return Sale
  20. * @throws \Exception
  21. */
  22. public function getSale(): Sale {
  23. return Sale::get($this->sale_id);
  24. }
  25. /**
  26. * @return Product
  27. * @throws \Exception
  28. */
  29. public function getProduct(): Product {
  30. return Product::get($this->product_id);
  31. }
  32. public function getDisplayPrice(): string {
  33. return (new NumberFormatter("en_GB", NumberFormatter::CURRENCY))->formatCurrency($this->unit_price * $this->quantity, $this->getCurrency());
  34. }
  35. private function getCurrency(): string {
  36. return $this->getSale()->getCurrency();
  37. }
  38. public function getAmount(): string {
  39. return bcmul($this->unit_price, $this->quantity, 2);
  40. }
  41. public function jsonSerialize(): array {
  42. return [
  43. "id" => (int) $this->id,
  44. "saleId" => (int) $this->sale_id,
  45. "productId" => (int) $this->product_id,
  46. "quantity" => (int) $this->quantity,
  47. "unitPrice" => $this->unit_price
  48. ];
  49. }
  50. }