Customer.php 2.6 KB

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