Customer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 RuntimeException;
  8. /**
  9. * Class Customer
  10. * @property int $id
  11. * @property int $address_id
  12. * @property string $name
  13. * @property string $phone
  14. * @property string $fax
  15. * @property string $email
  16. * @property string $vat_number
  17. */
  18. class Customer extends Model {
  19. protected static $table = "customer";
  20. protected static $idField = "id";
  21. protected static $fillableFields = ["address_id", "name", "phone", "fax", "email", "vat_number", "dts_created", "dts_updated", "dts_deleted"];
  22. use DateTimeTrait;
  23. use SoftDeleteTrait;
  24. // Static methods
  25. /**
  26. * @return Customer[]
  27. */
  28. public static function getAll(): array {
  29. return self::query([], ["sort" => "name"]);
  30. }
  31. public static function getOptions(): array {
  32. $options = [];
  33. foreach(self::getAll() as $customer) {
  34. $options[$customer->id] = $customer->name;
  35. }
  36. return $options;
  37. }
  38. /**
  39. * @param Request $request
  40. * @param Customer $customer
  41. * @return Customer
  42. * @throws \Exception
  43. */
  44. public static function updateFromRequest(Request $request, Customer $customer): Customer {
  45. $address = $customer->getAddress();
  46. $customer->name = $request->string("name");
  47. $customer->phone = $request->string("phone");
  48. $customer->email = $request->email("email");
  49. $customer->vat_number = $request->string("vat_number");
  50. $address->address_1 = $request->string("address_1");
  51. $address->address_2 = $request->string("address_2");
  52. $address->city = $request->string("city");
  53. $address->county = $request->string("county");
  54. $address->postal_code = $request->string("postal_code");
  55. $address->country = $request->string("country");
  56. $address->latitude = $request->float("latitude");
  57. $address->longitude = $request->float("longitude");
  58. if(!$customer->name) {
  59. throw new RuntimeException(__("Name is required."));
  60. }
  61. $address->commit();
  62. $customer->address_id = $address->id;
  63. $customer->commit();
  64. return $customer;
  65. }
  66. // Object methods
  67. /**
  68. * @return Address
  69. * @throws \Exception
  70. */
  71. public function getAddress(): Address {
  72. $address = Address::get($this->address_id);
  73. return $address ?: new Address();
  74. }
  75. public function getSlug(): string {
  76. return Util::slug("$this->name $this->id");
  77. }
  78. public function getUrl(): string {
  79. return sprintf("/customers/%s", $this->getSlug());
  80. }
  81. /**
  82. * @return CustomerEntity[]
  83. */
  84. public function getEntities(): array {
  85. return CustomerEntity::query([
  86. "customer_id" => $this->id,
  87. "dts_deleted" => null
  88. ], [
  89. "sort" => "name"
  90. ]);
  91. }
  92. /**
  93. * @param array $nvp
  94. * @return Location[]
  95. */
  96. public function getLocations(array $nvp = []): array {
  97. $nvp = array_merge($nvp, [
  98. "customer_id" => $this->id,
  99. "dts_deleted" => null
  100. ]);
  101. return Location::query($nvp, [
  102. "sort" => "name"
  103. ]);
  104. }
  105. public function getActiveSalesOrders(): array {
  106. $location_ids = array_column($this->getLocations(), "id");
  107. return Document::query([
  108. "location_id" => $location_ids,
  109. "invoice_status" => new NotInConstraint(SalesOrderStatuses::VOID, SalesOrderStatuses::UNCOLLECTIBLE),
  110. "dts_deleted" => null,
  111. ], [
  112. "sort" => "date",
  113. "order" => "desc",
  114. ]);
  115. }
  116. }