| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App;
- use App\Enums\DocumentTypes;
- use App\Enums\SaleStatuses;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use Carbon\CarbonImmutable;
- /**
- * Sale Lead
- * @property int $id
- * @property int $company_id
- * @property int $location_id
- * @property int $user_id
- * @property int $status
- * @property string $currency
- * @property string $tax_rate
- * @property string $date_predicted_sold
- */
- class Sale extends Model {
- protected static $table = "sale";
- protected static $idField = "id";
- protected static $fillableFields = [
- "company_id", "location_id", "user_id",
- "status", "currency", "tax_rate",
- "date_predicted_sold",
- "dts_created", "dts_updated", "dts_deleted"
- ];
- use DateTimeTrait;
- use SoftDeleteTrait;
- // Static methods
- /**
- * @param int $number
- * @return Sale|null
- * @throws \Exception
- */
- public static function getByNumber(int $number): ?Sale {
- return self::get($number - 999);
- }
- // Object methods
- /**
- * @return Company
- * @throws \Exception
- */
- public function getCompany(): Company {
- return Company::get($this->company_id);
- }
- /**
- * @return Location
- * @throws \Exception
- */
- public function getLocation(): Location {
- return Location::get($this->location_id);
- }
- public function getNumber(): int {
- return $this->id + 999;
- }
- public function getDisplayStatus(): string {
- return SaleStatuses::enumToText($this->status);
- }
- /**
- * @param array $nvp
- * @return SaleNote[]
- */
- public function getNotes(array $nvp = []): array {
- $nvp = array_merge($nvp, ["sale_id" => $this->id]);
- return SaleNote::query($nvp, ["sort" => "id"]);
- }
- /**
- * @param array $nvp
- * @return SaleItem[]
- */
- public function getItems(array $nvp = []): array {
- $nvp = array_merge($nvp, ["sale_id" => $this->id]);
- return SaleItem::query($nvp, ["sort" => "id"]);
- }
- public function getUrl(): string {
- return sprintf("/leads/%d", $this->getNumber());
- }
- public function getUser(): User {
- return User::get($this->user_id);
- }
- public function getDocuments(array $nvp = []): array {
- $nvp = array_merge($nvp, ["sale_id" => $this->id]);
- return Document::query($nvp, [
- "sort" => "id",
- "order" => "desc"
- ]);
- }
- /**
- * @return Document[]
- */
- public function getSalesOrders(): array {
- return $this->getDocuments(["type" => DocumentTypes::SALES_ORDER]);
- }
- /**
- * @return Ticket[]
- */
- public function getTickets(): array {
- return Ticket::query([
- "sale_id" => $this->id
- ], [
- "sort" => "id",
- "order" => "desc"
- ]);
- }
- public function getCurrency(): string {
- return $this->currency;
- }
- public function getTitle(): string {
- return __("Lead %d - %s", $this->getNumber(), $this->getLocation()->getDisplayName());
- }
- public function getDisplayPredictedSoldDate(string $format = "d/m/Y"): string {
- if(!$this->date_predicted_sold) {
- return "";
- }
- return CarbonImmutable::parse($this->date_predicted_sold)->format($format);
- }
- }
|