| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App;
- use NumberFormatter;
- /**
- * @property int $id
- * @property int $sale_id
- * @property int $product_id
- * @property int $quantity
- * @property string $unit_price
- */
- class SaleItem extends Model implements \JsonSerializable {
- protected static $table = "sale_item";
- protected static $idField = "id";
- protected static $fillableFields = [
- "sale_id", "product_id",
- "quantity", "unit_price",
- ];
- /**
- * @return Sale
- * @throws \Exception
- */
- public function getSale(): Sale {
- return Sale::get($this->sale_id);
- }
- /**
- * @return Product
- * @throws \Exception
- */
- public function getProduct(): Product {
- return Product::get($this->product_id);
- }
- public function getDisplayPrice(): string {
- return (new NumberFormatter("en_GB", NumberFormatter::CURRENCY))->formatCurrency($this->unit_price * $this->quantity, $this->getCurrency());
- }
- private function getCurrency(): string {
- return $this->getSale()->getCurrency();
- }
- public function getAmount(): string {
- return bcmul($this->unit_price, $this->quantity, 2);
- }
- public function jsonSerialize(): array {
- return [
- "id" => (int) $this->id,
- "saleId" => (int) $this->sale_id,
- "productId" => (int) $this->product_id,
- "quantity" => (int) $this->quantity,
- "unitPrice" => $this->unit_price
- ];
- }
- }
|