Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Client.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace App\Models\Common;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Common\AddressType;
  6. use App\Models\Accounting\Estimate;
  7. use App\Models\Accounting\Invoice;
  8. use App\Models\Accounting\RecurringInvoice;
  9. use App\Models\Accounting\Transaction;
  10. use App\Models\Setting\Currency;
  11. use Illuminate\Database\Eloquent\Factories\HasFactory;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  14. use Illuminate\Database\Eloquent\Relations\HasMany;
  15. use Illuminate\Database\Eloquent\Relations\MorphMany;
  16. use Illuminate\Database\Eloquent\Relations\MorphOne;
  17. class Client extends Model
  18. {
  19. use Blamable;
  20. use CompanyOwned;
  21. use HasFactory;
  22. protected $table = 'clients';
  23. protected $fillable = [
  24. 'company_id',
  25. 'name',
  26. 'currency_code',
  27. 'account_number',
  28. 'website',
  29. 'notes',
  30. 'created_by',
  31. 'updated_by',
  32. ];
  33. public static function createWithRelations(array $data): self
  34. {
  35. /** @var Client $client */
  36. $client = self::create($data);
  37. if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
  38. $client->primaryContact()->create([
  39. 'is_primary' => true,
  40. 'first_name' => $data['primaryContact']['first_name'],
  41. 'last_name' => $data['primaryContact']['last_name'],
  42. 'email' => $data['primaryContact']['email'],
  43. 'phones' => $data['primaryContact']['phones'] ?? [],
  44. ]);
  45. }
  46. if (isset($data['secondaryContacts'])) {
  47. foreach ($data['secondaryContacts'] as $contactData) {
  48. if (isset($contactData['first_name'])) {
  49. $client->secondaryContacts()->create([
  50. 'is_primary' => false,
  51. 'first_name' => $contactData['first_name'],
  52. 'last_name' => $contactData['last_name'],
  53. 'email' => $contactData['email'],
  54. 'phones' => $contactData['phones'] ?? [],
  55. ]);
  56. }
  57. }
  58. }
  59. if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
  60. $client->billingAddress()->create([
  61. 'type' => AddressType::Billing,
  62. 'address_line_1' => $data['billingAddress']['address_line_1'],
  63. 'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
  64. 'country_code' => $data['billingAddress']['country_code'] ?? null,
  65. 'state_id' => $data['billingAddress']['state_id'] ?? null,
  66. 'city' => $data['billingAddress']['city'] ?? null,
  67. 'postal_code' => $data['billingAddress']['postal_code'] ?? null,
  68. ]);
  69. }
  70. if (isset($data['shippingAddress'])) {
  71. $shippingData = $data['shippingAddress'];
  72. $shippingAddress = [
  73. 'type' => AddressType::Shipping,
  74. 'recipient' => $shippingData['recipient'] ?? null,
  75. 'phone' => $shippingData['phone'] ?? null,
  76. 'notes' => $shippingData['notes'] ?? null,
  77. ];
  78. if ($shippingData['same_as_billing'] ?? false) {
  79. $billingAddress = $client->billingAddress;
  80. if ($billingAddress) {
  81. $shippingAddress = [
  82. ...$shippingAddress,
  83. 'parent_address_id' => $billingAddress->id,
  84. 'address_line_1' => $billingAddress->address_line_1,
  85. 'address_line_2' => $billingAddress->address_line_2,
  86. 'country_code' => $billingAddress->country_code,
  87. 'state_id' => $billingAddress->state_id,
  88. 'city' => $billingAddress->city,
  89. 'postal_code' => $billingAddress->postal_code,
  90. ];
  91. $client->shippingAddress()->create($shippingAddress);
  92. }
  93. } elseif (isset($shippingData['address_line_1'])) {
  94. $shippingAddress = [
  95. ...$shippingAddress,
  96. 'address_line_1' => $shippingData['address_line_1'],
  97. 'address_line_2' => $shippingData['address_line_2'] ?? null,
  98. 'country_code' => $shippingData['country_code'] ?? null,
  99. 'state_id' => $shippingData['state_id'] ?? null,
  100. 'city' => $shippingData['city'] ?? null,
  101. 'postal_code' => $shippingData['postal_code'] ?? null,
  102. ];
  103. $client->shippingAddress()->create($shippingAddress);
  104. }
  105. }
  106. return $client;
  107. }
  108. public function updateWithRelations(array $data): self
  109. {
  110. $this->update($data);
  111. if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
  112. $this->primaryContact()->updateOrCreate(
  113. ['is_primary' => true],
  114. [
  115. 'first_name' => $data['primaryContact']['first_name'],
  116. 'last_name' => $data['primaryContact']['last_name'],
  117. 'email' => $data['primaryContact']['email'],
  118. 'phones' => $data['primaryContact']['phones'] ?? [],
  119. ]
  120. );
  121. }
  122. if (isset($data['secondaryContacts'])) {
  123. // Delete removed contacts
  124. $existingIds = collect($data['secondaryContacts'])->pluck('id')->filter()->all();
  125. $this->secondaryContacts()->whereNotIn('id', $existingIds)->delete();
  126. // Update or create contacts
  127. foreach ($data['secondaryContacts'] as $contactData) {
  128. if (isset($contactData['first_name'])) {
  129. $this->secondaryContacts()->updateOrCreate(
  130. ['id' => $contactData['id'] ?? null],
  131. [
  132. 'is_primary' => false,
  133. 'first_name' => $contactData['first_name'],
  134. 'last_name' => $contactData['last_name'],
  135. 'email' => $contactData['email'],
  136. 'phones' => $contactData['phones'] ?? [],
  137. ]
  138. );
  139. }
  140. }
  141. }
  142. if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
  143. $this->billingAddress()->updateOrCreate(
  144. ['type' => AddressType::Billing],
  145. [
  146. 'address_line_1' => $data['billingAddress']['address_line_1'],
  147. 'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
  148. 'country_code' => $data['billingAddress']['country_code'] ?? null,
  149. 'state_id' => $data['billingAddress']['state_id'] ?? null,
  150. 'city' => $data['billingAddress']['city'] ?? null,
  151. 'postal_code' => $data['billingAddress']['postal_code'] ?? null,
  152. ]
  153. );
  154. }
  155. if (isset($data['shippingAddress'])) {
  156. $shippingData = $data['shippingAddress'];
  157. $shippingAddress = [
  158. 'type' => AddressType::Shipping,
  159. 'recipient' => $shippingData['recipient'] ?? null,
  160. 'phone' => $shippingData['phone'] ?? null,
  161. 'notes' => $shippingData['notes'] ?? null,
  162. ];
  163. if ($shippingData['same_as_billing'] ?? false) {
  164. $billingAddress = $this->billingAddress;
  165. if ($billingAddress) {
  166. $shippingAddress = [
  167. ...$shippingAddress,
  168. 'parent_address_id' => $billingAddress->id,
  169. 'address_line_1' => $billingAddress->address_line_1,
  170. 'address_line_2' => $billingAddress->address_line_2,
  171. 'country_code' => $billingAddress->country_code,
  172. 'state_id' => $billingAddress->state_id,
  173. 'city' => $billingAddress->city,
  174. 'postal_code' => $billingAddress->postal_code,
  175. ];
  176. }
  177. } elseif (isset($shippingData['address_line_1'])) {
  178. $shippingAddress = [
  179. ...$shippingAddress,
  180. 'parent_address_id' => null,
  181. 'address_line_1' => $shippingData['address_line_1'],
  182. 'address_line_2' => $shippingData['address_line_2'] ?? null,
  183. 'country_code' => $shippingData['country_code'] ?? null,
  184. 'state_id' => $shippingData['state_id'] ?? null,
  185. 'city' => $shippingData['city'] ?? null,
  186. 'postal_code' => $shippingData['postal_code'] ?? null,
  187. ];
  188. }
  189. $this->shippingAddress()->updateOrCreate(
  190. ['type' => AddressType::Shipping],
  191. $shippingAddress
  192. );
  193. }
  194. return $this;
  195. }
  196. public function transactions(): MorphMany
  197. {
  198. return $this->morphMany(Transaction::class, 'payeeable');
  199. }
  200. public function contacts(): MorphMany
  201. {
  202. return $this->morphMany(Contact::class, 'contactable');
  203. }
  204. public function primaryContact(): MorphOne
  205. {
  206. return $this->morphOne(Contact::class, 'contactable')
  207. ->where('is_primary', true);
  208. }
  209. public function secondaryContacts(): MorphMany
  210. {
  211. return $this->morphMany(Contact::class, 'contactable')
  212. ->where('is_primary', false);
  213. }
  214. public function currency(): BelongsTo
  215. {
  216. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  217. }
  218. public function addresses(): MorphMany
  219. {
  220. return $this->morphMany(Address::class, 'addressable');
  221. }
  222. public function billingAddress(): MorphOne
  223. {
  224. return $this->morphOne(Address::class, 'addressable')
  225. ->where('type', AddressType::Billing);
  226. }
  227. public function shippingAddress(): MorphOne
  228. {
  229. return $this->morphOne(Address::class, 'addressable')
  230. ->where('type', AddressType::Shipping);
  231. }
  232. public function estimates(): HasMany
  233. {
  234. return $this->hasMany(Estimate::class);
  235. }
  236. public function invoices(): HasMany
  237. {
  238. return $this->hasMany(Invoice::class);
  239. }
  240. public function recurringInvoices(): HasMany
  241. {
  242. return $this->hasMany(RecurringInvoice::class);
  243. }
  244. }