Sale.php 2.9 KB

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