CustomerEntity.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App;
  3. use App\Constraint\NotInConstraint;
  4. use App\Enums\SalesOrderStatuses;
  5. use App\Traits\DateTimeTrait;
  6. use App\Traits\SoftDeleteTrait;
  7. use Exception;
  8. use RuntimeException;
  9. /**
  10. * @property int $id
  11. * @property int $customer_id
  12. * @property int $address_id
  13. * @property string $name
  14. * @property string $phone
  15. * @property string $fax
  16. * @property string $email
  17. * @property string $vat_number
  18. */
  19. class CustomerEntity extends Model {
  20. protected static $table = "customer_entity";
  21. protected static $idField = "id";
  22. protected static $fillableFields = ["customer_id", "address_id", "name", "phone", "fax", "email", "vat_number", "dts_created", "dts_updated", "dts_deleted"];
  23. use DateTimeTrait;
  24. use SoftDeleteTrait;
  25. // Static methods
  26. /**
  27. * @param Request $request
  28. * @param CustomerEntity $entity
  29. * @return CustomerEntity
  30. * @throws Exception
  31. */
  32. public static function updateFromRequest(Request $request, CustomerEntity $entity): CustomerEntity {
  33. $address = $entity->getAddress();
  34. $entity->name = $request->string("name");
  35. $entity->phone = $request->string("phone");
  36. $entity->email = $request->email("email");
  37. $entity->vat_number = $request->string("vat_number");
  38. $address->address_1 = $request->string("address_1");
  39. $address->address_2 = $request->string("address_2");
  40. $address->city = $request->string("city");
  41. $address->county = $request->string("county");
  42. $address->postal_code = $request->string("postal_code");
  43. $address->country = $request->string("country");
  44. $address->latitude = $request->float("latitude");
  45. $address->longitude = $request->float("longitude");
  46. if(!$entity->name) {
  47. throw new RuntimeException(__("Name is required."));
  48. }
  49. $address->commit();
  50. $entity->address_id = $address->id;
  51. $entity->commit();
  52. return $entity;
  53. }
  54. // Object methods
  55. /**
  56. * @return Customer
  57. * @throws Exception
  58. */
  59. public function getCustomer(): Customer {
  60. return Customer::require($this->customer_id);
  61. }
  62. /**
  63. * @return Address
  64. * @throws Exception
  65. */
  66. public function getAddress(): Address {
  67. $address = Address::get($this->address_id);
  68. return $address ?: new Address();
  69. }
  70. public function getSlug(): string {
  71. return Util::slug("$this->name $this->id");
  72. }
  73. public function getUrl(): string {
  74. return sprintf("/customers/%s/entities/%s/update", $this->getCustomer()->getSlug(), $this->getSlug());
  75. }
  76. /**
  77. * @param array $nvp
  78. * @return Location[]
  79. */
  80. public function getLocations(array $nvp = []): array {
  81. $nvp = array_merge($nvp, [
  82. "customer_entity_id" => $this->id,
  83. "dts_deleted" => null
  84. ]);
  85. return Location::query($nvp, [
  86. "sort" => "name"
  87. ]);
  88. }
  89. public function belongsToCustomer(Customer $customer): bool {
  90. return $customer->is($this->customer_id);
  91. }
  92. }