| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- /**
- * Class Customer
- * @property int $id
- * @property int $address_id
- * @property string $name
- * @property string $phone
- * @property string $fax
- * @property string $email
- * @property string $vat_number
- */
- class Customer extends Model {
- protected static $table = "customer";
- protected static $idField = "id";
- protected static $fillableFields = ["address_id", "name", "phone", "fax", "email", "vat_number", "dts_created", "dts_updated", "dts_deleted"];
- use DateTimeTrait;
- use SoftDeleteTrait;
- // Static methods
- /**
- * @return Customer[]
- */
- public static function getAll(): array {
- return self::query([], ["sort" => "name"]);
- }
- public static function getOptions(): array {
- $options = [];
- foreach(self::getAll() as $customer) {
- $options[$customer->id] = $customer->name;
- }
- return $options;
- }
- // Object methods
- /**
- * @return Address
- * @throws \Exception
- */
- public function getAddress(): Address {
- $address = Address::get($this->address_id);
- return $address ?: new Address();
- }
- public function getSlug(): string {
- return Util::slug("$this->name $this->id");
- }
- public function getUrl(): string {
- return sprintf("/customers/%s", $this->getSlug());
- }
- /**
- * @param array $nvp
- * @return Location[]
- */
- public function getLocations(array $nvp = []): array {
- $nvp = array_merge($nvp, [
- "customer_id" => $this->id,
- "dts_deleted" => null
- ]);
- return Location::query($nvp, [
- "sort" => "name"
- ]);
- }
- }
|