Company.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public static function getOptions(): array {
  29. $options = [];
  30. foreach(self::getAll() as $company) {
  31. $options[$company->id] = $company->name;
  32. }
  33. return $options;
  34. }
  35. // Object methods
  36. /**
  37. * @return Address
  38. * @throws \Exception
  39. */
  40. public function getAddress(): Address {
  41. $address = Address::get($this->address_id);
  42. return $address ?: new Address();
  43. }
  44. public function getSlug(): string {
  45. return Util::slug("$this->name $this->id");
  46. }
  47. public function getUrl(): string {
  48. return sprintf("/companies/%s", $this->getSlug());
  49. }
  50. /**
  51. * @return CompanyBankDetails
  52. */
  53. public function getBankDetailsForCurrency(string $currency): CompanyBankDetails {
  54. $details = CompanyBankDetails::queryOne(["company_id" => $this->id, "currency" => $currency]);
  55. if(!$details) {
  56. $details = new CompanyBankDetails();
  57. $details->company_id = $this->id;
  58. $details->currency = $currency;
  59. $details->dts_created = Database::dts();
  60. }
  61. return $details;
  62. }
  63. }