Company.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 $invoice_footer
  14. * @property string $dts_created
  15. * @property string $dts_updated
  16. * @property string $dts_deleted
  17. */
  18. class Company extends Model {
  19. protected static $table = "company";
  20. protected static $idField = "id";
  21. protected static $fillableFields = [
  22. "address_id",
  23. "name", "phone", "fax", "email",
  24. "company_number", "vat_number", "invoice_footer",
  25. "dts_created", "dts_updated", "dts_deleted"
  26. ];
  27. // Static methods
  28. /**
  29. * @return Company[]
  30. */
  31. public static function getAll(): array {
  32. return self::query([], ["sort" => "name"]);
  33. }
  34. public static function getOptions(): array {
  35. $options = [];
  36. foreach(self::getAll() as $company) {
  37. $options[$company->id] = $company->name;
  38. }
  39. return $options;
  40. }
  41. // Object methods
  42. /**
  43. * @return Address
  44. * @throws \Exception
  45. */
  46. public function getAddress(): Address {
  47. $address = Address::get($this->address_id);
  48. return $address ?: new Address();
  49. }
  50. public function getSlug(): string {
  51. return Util::slug("$this->name $this->id");
  52. }
  53. public function getUrl(): string {
  54. return sprintf("/companies/%s", $this->getSlug());
  55. }
  56. /**
  57. * @return CompanyBankDetails
  58. */
  59. public function getBankDetailsForCurrency(string $currency): CompanyBankDetails {
  60. $details = CompanyBankDetails::queryOne(["company_id" => $this->id, "currency" => $currency]);
  61. if(!$details) {
  62. $details = new CompanyBankDetails();
  63. $details->company_id = $this->id;
  64. $details->currency = $currency;
  65. $details->dts_created = Database::dts();
  66. }
  67. return $details;
  68. }
  69. public function getInvoiceFooterHtml(): string {
  70. return nl2br(htmlspecialchars($this->invoice_footer));
  71. }
  72. }