1
0

Customer.php 3.4 KB

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