PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Models / CustomerAddresses.php

CustomerAddresses.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Models/CustomerAddresses.php

206 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Models;
4
5 use FluentCart\App\Helpers\AddressHelper;
6 use FluentCart\App\Models\Concerns\CanSearch;
7 use FluentCart\App\Services\Localization\LocalizationManager;
8 use FluentCart\Framework\Support\Arr;
9
10 /**
11 * Order Model - DB Model for Orders
12 *
13 * Database Model
14 *
15 * @package FluentCart\App\Models
16 *
17 * @version 1.0.0
18 */
19 class CustomerAddresses extends Model
20 {
21 use CanSearch;
22
23 protected $table = 'fct_customer_addresses';
24
25 protected $appends = ['formatted_address', 'company_name', 'vat_number', 'legal_registration_id'];
26
27
28 /**
29 * The attributes that are mass assignable.
30 *
31 * @var array
32 */
33 protected $fillable = [
34 'customer_id',
35 'is_primary',
36 'type',
37 'status',
38 'label',
39 'name',
40 'address_1',
41 'address_2',
42 'city',
43 'state',
44 'postcode',
45 'country',
46 'phone',
47 'email',
48 'meta',
49 ];
50
51
52 public function setMetaAttribute($value)
53 {
54 if ($value) {
55 $decoded = \json_encode($value);
56 if (!$decoded) {
57 $decoded = '[]';
58 }
59 } else {
60 $decoded = '[]';
61 }
62
63 $this->attributes['meta'] = $decoded;
64 }
65
66 public function getMetaAttribute($value)
67 {
68 if (!$value) {
69 return [];
70 }
71
72 return \json_decode($value, true);
73 }
74
75 public function setCompanyNameAttribute($value)
76 {
77 $meta = is_array($this->meta) ? $this->meta : [];
78
79 if ($value) {
80 Arr::set($meta, 'other_data.company_name', $value);
81 } else {
82 unset($meta['other_data']['company_name']);
83 }
84
85 $this->attributes['meta'] = json_encode($meta);
86 }
87
88 public function getCompanyNameAttribute()
89 {
90 return Arr::get($this->meta, 'other_data.company_name', '');
91 }
92
93 public function setLegalRegistrationIdAttribute($value)
94 {
95 $meta = is_array($this->meta) ? $this->meta : [];
96
97 if ($value) {
98 Arr::set($meta, 'other_data.legal_registration_id', $value);
99 } else {
100 unset($meta['other_data']['legal_registration_id']);
101 }
102
103 $this->attributes['meta'] = json_encode($meta);
104 }
105
106 public function getLegalRegistrationIdAttribute()
107 {
108 return Arr::get($this->meta, 'other_data.legal_registration_id', '');
109 }
110
111 public function setVatNumberAttribute($value)
112 {
113 $meta = is_array($this->meta) ? $this->meta : [];
114
115 if ($value) {
116 Arr::set($meta, 'other_data.vat_number', $value);
117 } else {
118 unset($meta['other_data']['vat_number']);
119 }
120
121 $this->attributes['meta'] = json_encode($meta);
122 }
123
124 public function getVatNumberAttribute()
125 {
126 return Arr::get($this->meta, 'other_data.vat_number', '');
127 }
128
129 public function scopeOfActive($query)
130 {
131 return $query->where('status', 'active');
132 }
133
134 public function scopeOfArchived($query)
135 {
136 return $query->where('status', 'archived');
137 }
138
139 public function customer()
140 {
141 return $this->belongsTo(Customer::class, 'customer_id', 'id');
142 }
143
144 public function getFormattedAddressAttribute(): array
145 {
146 $formattedAddress = [
147 'country' => AddressHelper::getCountryNameByCode($this->country),
148 'state' => AddressHelper::getStateNameByCode($this->state, $this->country),
149 'city' => $this->city,
150 'postcode' => $this->postcode,
151 'address_1' => $this->address_1,
152 'address_2' => $this->address_2,
153 'type' => $this->type,
154 'name' => $this->name,
155 'first_name' => $this->first_name,
156 'last_name' => $this->last_name,
157 'full_name' => $this->full_name,
158 'company_name' => $this->company_name,
159 'vat_number' => $this->vat_number,
160 'legal_registration_id' => $this->legal_registration_id,
161 'label' => $this->label,
162 'phone' => $this->phone
163 ];
164
165 $addressParts = [
166 trim(Arr::get($formattedAddress, 'company_name') ?? ''),
167 trim(Arr::get($formattedAddress, 'address_1') ?? ''),
168 trim(Arr::get($formattedAddress, 'address_2') ?? ''),
169 trim(Arr::get($formattedAddress, 'city') ?? ''),
170 trim(Arr::get($formattedAddress, 'state') ?? ''),
171 trim(Arr::get($formattedAddress, 'country') ?? ''),
172 ];
173 $addressParts = array_filter($addressParts, function ($part) {
174 return $part !== '';
175 });
176 $fullAddress = implode(', ', $addressParts);
177
178 $formattedAddress['full_address'] = $fullAddress;
179
180 return $formattedAddress;
181 }
182
183 public function getFormattedDataForCheckout($prefix = 'billing_')
184 {
185 $data = [
186 '' . $prefix . 'full_name' => $this->name,
187 '' . $prefix . 'address_1' => $this->address_1,
188 '' . $prefix . 'address_2' => $this->address_2,
189 '' . $prefix . 'city' => $this->city,
190 '' . $prefix . 'state' => $this->state,
191 '' . $prefix . 'phone' => $this->phone,
192 '' . $prefix . 'postcode' => $this->postcode,
193 '' . $prefix . 'country' => $this->country,
194 '' . $prefix . 'company_name' => $this->company_name,
195 '' . $prefix . 'vat_number' => $this->vat_number,
196 '' . $prefix . 'legal_registration_id' => $this->legal_registration_id,
197 ];
198
199 // if ($prefix === 'billing_') {
200 // unset($data['billing_full_name']);
201 // }
202
203 return $data;
204 }
205 }
206