Customer.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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->email = $request->email("email");
  47. $customer->vat_number = $request->string("vat_number");
  48. $address->address_1 = $request->string("address_1");
  49. $address->address_2 = $request->string("address_2");
  50. $address->city = $request->string("city");
  51. $address->county = $request->string("county");
  52. $address->postal_code = $request->string("postal_code");
  53. $address->country = $request->string("country");
  54. $address->latitude = $request->float("latitude");
  55. $address->longitude = $request->float("longitude");
  56. if(!$customer->name) {
  57. throw new RuntimeException(__("Name is required."));
  58. }
  59. $address->commit();
  60. $customer->address_id = $address->id;
  61. $customer->commit();
  62. return $customer;
  63. }
  64. // Object methods
  65. /**
  66. * @return Address
  67. * @throws \Exception
  68. */
  69. public function getAddress(): Address {
  70. $address = Address::get($this->address_id);
  71. return $address ?: new Address();
  72. }
  73. public function getSlug(): string {
  74. return Util::slug("$this->name $this->id");
  75. }
  76. public function getUrl(): string {
  77. return sprintf("/customers/%s", $this->getSlug());
  78. }
  79. /**
  80. * @param array $nvp
  81. * @return Location[]
  82. */
  83. public function getLocations(array $nvp = []): array {
  84. $nvp = array_merge($nvp, [
  85. "customer_id" => $this->id,
  86. "dts_deleted" => null
  87. ]);
  88. return Location::query($nvp, [
  89. "sort" => "name"
  90. ]);
  91. }
  92. }