Sale.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App;
  3. use App\Enums\DocumentTypes;
  4. use App\Enums\SaleStatuses;
  5. use App\Traits\DateTimeTrait;
  6. use App\Traits\SoftDeleteTrait;
  7. /**
  8. * @property int $id
  9. * @property int $company_id
  10. * @property int $location_id
  11. * @property int $user_id
  12. * @property int $status
  13. * @property string $currency
  14. * @property string $tax_rate
  15. */
  16. class Sale extends Model {
  17. protected static $table = "sale";
  18. protected static $idField = "id";
  19. protected static $fillableFields = [
  20. "company_id", "location_id", "user_id",
  21. "status", "currency", "tax_rate",
  22. "dts_created", "dts_updated", "dts_deleted"
  23. ];
  24. use DateTimeTrait;
  25. use SoftDeleteTrait;
  26. // Static methods
  27. /**
  28. * @param int $number
  29. * @return Sale|null
  30. * @throws \Exception
  31. */
  32. public static function getByNumber(int $number): ?Sale {
  33. return self::get($number - 999);
  34. }
  35. // Object methods
  36. /**
  37. * @return Company
  38. * @throws \Exception
  39. */
  40. public function getCompany(): Company {
  41. return Company::get($this->company_id);
  42. }
  43. /**
  44. * @return Location
  45. * @throws \Exception
  46. */
  47. public function getLocation(): Location {
  48. return Location::get($this->location_id);
  49. }
  50. public function getNumber(): int {
  51. return $this->id + 999;
  52. }
  53. public function getDisplayStatus(): string {
  54. return SaleStatuses::enumToText($this->status);
  55. }
  56. /**
  57. * @param array $nvp
  58. * @return SaleNote[]
  59. */
  60. public function getNotes(array $nvp = []): array {
  61. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  62. return SaleNote::query($nvp, ["sort" => "id"]);
  63. }
  64. /**
  65. * @param array $nvp
  66. * @return SaleItem[]
  67. */
  68. public function getItems(array $nvp = []): array {
  69. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  70. return SaleItem::query($nvp, ["sort" => "id"]);
  71. }
  72. public function getUrl(): string {
  73. return sprintf("/sales/%d", $this->getNumber());
  74. }
  75. public function getUser(): User {
  76. return User::get($this->user_id);
  77. }
  78. public function getDocuments(array $nvp = []): array {
  79. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  80. return Document::query($nvp, [
  81. "sort" => "id",
  82. "order" => "desc"
  83. ]);
  84. }
  85. /**
  86. * @return Document[]
  87. */
  88. public function getInvoices(): array {
  89. return $this->getDocuments(["type" => DocumentTypes::INVOICE]);
  90. }
  91. /**
  92. * @return Ticket[]
  93. */
  94. public function getTickets(): array {
  95. return Ticket::query([
  96. "sale_id" => $this->id
  97. ], [
  98. "sort" => "id",
  99. "order" => "desc"
  100. ]);
  101. }
  102. public function getCurrency(): string {
  103. return $this->currency;
  104. }
  105. public function getTitle(): string {
  106. return __("Sale %d - %s", $this->getNumber(), $this->getLocation()->getDisplayName());
  107. }
  108. }