| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App;
- use App\Constraint\NotInConstraint;
- use App\Enums\SalesOrderStatuses;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use RuntimeException;
- /**
- * 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;
- }
- /**
- * @param Request $request
- * @param Customer $customer
- * @return Customer
- * @throws \Exception
- */
- public static function updateFromRequest(Request $request, Customer $customer): Customer {
- $address = $customer->getAddress();
- $customer->name = $request->string("name");
- $customer->phone = $request->string("phone");
- $customer->email = $request->email("email");
- $customer->vat_number = $request->string("vat_number");
- $address->address_1 = $request->string("address_1");
- $address->address_2 = $request->string("address_2");
- $address->city = $request->string("city");
- $address->county = $request->string("county");
- $address->postal_code = $request->string("postal_code");
- $address->country = $request->string("country");
- $address->latitude = $request->float("latitude");
- $address->longitude = $request->float("longitude");
- if(!$customer->name) {
- throw new RuntimeException(__("Name is required."));
- }
- $address->commit();
- $customer->address_id = $address->id;
- $customer->commit();
- return $customer;
- }
- // 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"
- ]);
- }
- public function getActiveSalesOrders(): array {
- $location_ids = array_column($this->getLocations(), "id");
- return Document::query([
- "location_id" => $location_ids,
- "invoice_status" => new NotInConstraint(SalesOrderStatuses::VOID, SalesOrderStatuses::UNCOLLECTIBLE),
- "dts_deleted" => null,
- ], [
- "sort" => "date",
- "order" => "desc",
- ]);
- }
- }
|