1
0

Sale.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App;
  3. use App\Constraint\NotEqualsConstraint;
  4. use App\Enums\DocumentTypes;
  5. use App\Enums\SaleStatuses;
  6. use App\Traits\DateTimeTrait;
  7. use App\Traits\SoftDeleteTrait;
  8. use Carbon\CarbonImmutable;
  9. /**
  10. * Sale Lead
  11. * @property int $id
  12. * @property int $company_id
  13. * @property int $location_id
  14. * @property int $user_id
  15. * @property int $status
  16. * @property string $currency
  17. * @property string $tax_rate
  18. * @property string $date_predicted_sold
  19. */
  20. class Sale extends Model {
  21. protected static $table = "sale";
  22. protected static $idField = "id";
  23. protected static $fillableFields = [
  24. "company_id", "location_id", "user_id",
  25. "status", "currency", "tax_rate",
  26. "date_predicted_sold",
  27. "dts_created", "dts_updated", "dts_deleted"
  28. ];
  29. use DateTimeTrait;
  30. use SoftDeleteTrait;
  31. // Static methods
  32. /**
  33. * @return int
  34. * @throws \Exception
  35. */
  36. public static function getOpenCount(): int {
  37. return self::count([
  38. "status" => new NotEqualsConstraint(SaleStatuses::COMPLETED),
  39. "dts_deleted" => null
  40. ]);
  41. }
  42. /**
  43. * @param int $number
  44. * @return Sale|null
  45. * @throws \Exception
  46. */
  47. public static function getByNumber(int $number): ?Sale {
  48. return self::get($number - 999);
  49. }
  50. // Object methods
  51. /**
  52. * @return Company
  53. * @throws \Exception
  54. */
  55. public function getCompany(): Company {
  56. return Company::get($this->company_id);
  57. }
  58. /**
  59. * @return Location
  60. * @throws \Exception
  61. */
  62. public function getLocation(): Location {
  63. return Location::get($this->location_id);
  64. }
  65. public function getNumber(): int {
  66. return $this->id + 999;
  67. }
  68. public function getReference(): string {
  69. return "L" . $this->getNumber();
  70. }
  71. public function getDisplayStatus(): string {
  72. return SaleStatuses::enumToText($this->status);
  73. }
  74. /**
  75. * @param array $nvp
  76. * @return SaleNote[]
  77. */
  78. public function getNotes(array $nvp = []): array {
  79. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  80. return SaleNote::query($nvp, ["sort" => "id"]);
  81. }
  82. /**
  83. * @param array $nvp
  84. * @return SaleItem[]
  85. */
  86. public function getItems(array $nvp = []): array {
  87. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  88. return SaleItem::query($nvp, ["sort" => "id"]);
  89. }
  90. public function getUrl(): string {
  91. return sprintf("/leads/%d", $this->getNumber());
  92. }
  93. public function getUser(): User {
  94. return User::get($this->user_id);
  95. }
  96. public function getDocuments(array $nvp = []): array {
  97. $nvp = array_merge($nvp, ["sale_id" => $this->id]);
  98. return Document::query($nvp, [
  99. "sort" => "id",
  100. "order" => "desc"
  101. ]);
  102. }
  103. /**
  104. * @return Document[]
  105. */
  106. public function getSalesOrders(): array {
  107. return $this->getDocuments(["type" => DocumentTypes::SALES_ORDER]);
  108. }
  109. /**
  110. * @return Ticket[]
  111. */
  112. public function getTickets(): array {
  113. return Ticket::query([
  114. "sale_id" => $this->id
  115. ], [
  116. "sort" => "id",
  117. "order" => "desc"
  118. ]);
  119. }
  120. public function getCurrency(): string {
  121. return $this->currency;
  122. }
  123. public function getTitle(): string {
  124. return __("Lead %s", $this->getReference());
  125. }
  126. public function getDisplayPredictedSoldDate(string $format = "d/m/Y"): string {
  127. if(!$this->date_predicted_sold) {
  128. return "";
  129. }
  130. return CarbonImmutable::parse($this->date_predicted_sold)->format($format);
  131. }
  132. /**
  133. * @return Document[]
  134. */
  135. public function getChildDocuments(): array {
  136. return Document::query([
  137. "sale_id" => $this->id,
  138. "type" => DocumentTypes::SALES_ORDER
  139. ], [
  140. "sort" => "type",
  141. ]);
  142. }
  143. public function getAmount(): string {
  144. $value = "0.00";
  145. foreach($this->getItems() as $item) {
  146. $value = bcadd($item->getAmount(), $value, 2);
  147. }
  148. return $value;
  149. }
  150. public function getDisplayAmount(): string {
  151. return Util::getCurrencyFormatter()->formatCurrency($this->getAmount(), $this->getCurrency());
  152. }
  153. }