Sale.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 getReference(): string {
  58. return "L" . $this->getNumber();
  59. }
  60. public function getDisplayStatus(): string {
  61. return SaleStatuses::enumToText($this->status);
  62. }
  63. /**
  64. * @param array $nvp
  65. * @return SaleNote[]
  66. */
  67. public function getNotes(array $nvp = []): array {
  68. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  69. return SaleNote::query($nvp, ["sort" => "id"]);
  70. }
  71. /**
  72. * @param array $nvp
  73. * @return SaleItem[]
  74. */
  75. public function getItems(array $nvp = []): array {
  76. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  77. return SaleItem::query($nvp, ["sort" => "id"]);
  78. }
  79. public function getUrl(): string {
  80. return sprintf("/leads/%d", $this->getNumber());
  81. }
  82. public function getUser(): User {
  83. return User::get($this->user_id);
  84. }
  85. public function getDocuments(array $nvp = []): array {
  86. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  87. return Document::query($nvp, [
  88. "sort" => "id",
  89. "order" => "desc"
  90. ]);
  91. }
  92. /**
  93. * @return Document[]
  94. */
  95. public function getSalesOrders(): array {
  96. return $this->getDocuments(["type" => DocumentTypes::SALES_ORDER]);
  97. }
  98. /**
  99. * @return Ticket[]
  100. */
  101. public function getTickets(): array {
  102. return Ticket::query([
  103. "sale_id" => $this->id
  104. ], [
  105. "sort" => "id",
  106. "order" => "desc"
  107. ]);
  108. }
  109. public function getCurrency(): string {
  110. return $this->currency;
  111. }
  112. public function getTitle(): string {
  113. return __("Lead %s", $this->getReference());
  114. }
  115. public function getDisplayPredictedSoldDate(string $format = "d/m/Y"): string {
  116. if(!$this->date_predicted_sold) {
  117. return "";
  118. }
  119. return CarbonImmutable::parse($this->date_predicted_sold)->format($format);
  120. }
  121. /**
  122. * @return Document[]
  123. */
  124. public function getChildDocuments(): array {
  125. return Document::query([
  126. "sale_id" => $this->id,
  127. "type" => DocumentTypes::SALES_ORDER
  128. ], [
  129. "sort" => "type",
  130. ]);
  131. }
  132. public function getAmount(): string {
  133. $value = "0.00";
  134. foreach($this->getItems() as $item) {
  135. $value = bcadd($item->getAmount(), $value, 2);
  136. }
  137. return $value;
  138. }
  139. public function getDisplayAmount(): string {
  140. return Util::getCurrencyFormatter()->formatCurrency($this->getAmount(), $this->getCurrency());
  141. }
  142. }