Company.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App;
  3. /**
  4. * Class Company
  5. * @property int $id
  6. * @property int $address_id
  7. * @property string $name
  8. * @property string $phone
  9. * @property string $fax
  10. * @property string $email
  11. * @property string $company_number
  12. * @property string $vat_number
  13. * @property string $dts_created
  14. * @property string $dts_updated
  15. * @property string $dts_deleted
  16. */
  17. class Company extends Model {
  18. protected static $table = "company";
  19. protected static $idField = "id";
  20. protected static $fillableFields = ["address_id", "name", "phone", "fax", "email", "company_number", "vat_number", "dts_created", "dts_updated", "dts_deleted"];
  21. // Static methods
  22. /**
  23. * @return Company[]
  24. */
  25. public static function getAll(): array {
  26. return self::query([], ["sort" => "name"]);
  27. }
  28. // Object methods
  29. /**
  30. * @return Address
  31. * @throws \Exception
  32. */
  33. public function getAddress(): Address {
  34. $address = Address::get($this->address_id);
  35. return $address ?: new Address();
  36. }
  37. public function getSlug(): string {
  38. return Util::slug("$this->name $this->id");
  39. }
  40. public function getUrl(): string {
  41. return sprintf("/companies/%s", $this->getSlug());
  42. }
  43. /**
  44. * @return CompanyBankDetails
  45. */
  46. public function getBankDetailsForCurrency(string $currency): CompanyBankDetails {
  47. $details = CompanyBankDetails::queryOne(["company_id" => $this->id, "currency" => $currency]);
  48. if(!$details) {
  49. $details = new CompanyBankDetails();
  50. $details->company_id = $this->id;
  51. $details->currency = $currency;
  52. $details->dts_created = Database::dts();
  53. }
  54. return $details;
  55. }
  56. }