DocumentItem.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App;
  3. use App\Traits\DateTimeTrait;
  4. use App\Traits\SoftDeleteTrait;
  5. use Exception;
  6. use JsonSerializable;
  7. /**
  8. * @property int $id
  9. * @property int $document_id
  10. * @property int $product_id
  11. * @property string $description
  12. * @property string $serial_number
  13. * @property int $quantity
  14. * @property string $unit_price
  15. * @property string $insurance_value
  16. */
  17. class DocumentItem extends Model implements JsonSerializable {
  18. protected static $table = "document_item";
  19. protected static $idField = "id";
  20. protected static $fillableFields = [
  21. "document_id", "product_id",
  22. "description", "serial_number",
  23. "quantity", "unit_price", "insurance_value",
  24. "dts_created", "dts_updated", "dts_deleted"
  25. ];
  26. use DateTimeTrait;
  27. use SoftDeleteTrait;
  28. // Object methods
  29. public function getAmount(): string {
  30. return bcmul($this->unit_price, $this->quantity, 2);
  31. }
  32. public function jsonSerialize(): array {
  33. return [
  34. "id" => (int) $this->id,
  35. "documentId" => (int) $this->document_id,
  36. "description" => $this->description,
  37. "serialNumber" => $this->serial_number,
  38. "quantity" => (int) $this->quantity,
  39. "unitPrice" => $this->unit_price,
  40. "insuranceValue" => $this->insurance_value,
  41. ];
  42. }
  43. /**
  44. * @return Document
  45. * @throws Exception
  46. */
  47. public function getDocument(): Document {
  48. return Document::get($this->document_id);
  49. }
  50. /**
  51. * @return Product|null
  52. * @throws Exception
  53. */
  54. public function getProduct(): ?Product {
  55. return Product::get($this->product_id);
  56. }
  57. public function getSerialNumberHtml(): string {
  58. return nl2br(htmlspecialchars($this->serial_number));
  59. }
  60. }