| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\AddressHelper; |
| 6 |
use FluentCart\App\Services\Localization\LocalizationManager; |
| 7 |
use FluentCart\Framework\Support\Collection; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
/** |
| 11 |
* OrderItem Model - DB Model for Order Items |
| 12 |
* |
| 13 |
* Database Model |
| 14 |
* |
| 15 |
* @package FluentCart\App\Models |
| 16 |
* |
| 17 |
* @version 1.0.0 |
| 18 |
*/ |
| 19 |
class OrderAddress extends Model |
| 20 |
{ |
| 21 |
protected $table = 'fct_order_addresses'; |
| 22 |
|
| 23 |
/** |
| 24 |
* The attributes that are mass assignable. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
protected $fillable = [ |
| 29 |
'id', |
| 30 |
'order_id', |
| 31 |
'type', |
| 32 |
'name', |
| 33 |
'address_1', |
| 34 |
'address_2', |
| 35 |
'city', |
| 36 |
'state', |
| 37 |
'postcode', |
| 38 |
'country', |
| 39 |
'meta', |
| 40 |
]; |
| 41 |
protected $appends = [ |
| 42 |
'email', |
| 43 |
'first_name', |
| 44 |
'last_name', |
| 45 |
'full_name', |
| 46 |
'formatted_address', |
| 47 |
'company_name', |
| 48 |
'vat_number', |
| 49 |
'legal_registration_id', |
| 50 |
'phone', |
| 51 |
'label' |
| 52 |
]; |
| 53 |
|
| 54 |
public function setMetaAttribute($value) |
| 55 |
{ |
| 56 |
if ($value) { |
| 57 |
$decoded = \json_encode($value, true); |
| 58 |
if (!($decoded)) { |
| 59 |
$decoded = '[]'; |
| 60 |
} |
| 61 |
} else { |
| 62 |
$decoded = '[]'; |
| 63 |
} |
| 64 |
|
| 65 |
$this->attributes['meta'] = $decoded; |
| 66 |
} |
| 67 |
|
| 68 |
public function getMetaAttribute($value) |
| 69 |
{ |
| 70 |
if (!$value) { |
| 71 |
return []; |
| 72 |
} |
| 73 |
|
| 74 |
return \json_decode($value, true); |
| 75 |
} |
| 76 |
|
| 77 |
public function getFullNameAttribute(): ?string |
| 78 |
{ |
| 79 |
return $this->name; |
| 80 |
} |
| 81 |
|
| 82 |
public function getFirstNameAttribute(): ?string |
| 83 |
{ |
| 84 |
return explode(" ", $this->name ?? '')[0]; |
| 85 |
} |
| 86 |
|
| 87 |
public function getLastNameAttribute(): ?string |
| 88 |
{ |
| 89 |
$nameParts = explode(" ", $this->name ?? ''); |
| 90 |
return end($nameParts); |
| 91 |
} |
| 92 |
|
| 93 |
public function order(): \FluentCart\Framework\Database\Orm\Relations\BelongsTo |
| 94 |
{ |
| 95 |
return $this->belongsTo(Order::class, 'order_id', 'id'); |
| 96 |
} |
| 97 |
|
| 98 |
public function getEmailAttribute(): ?string |
| 99 |
{ |
| 100 |
$order = $this->order; |
| 101 |
if ($order && $order->customer) { |
| 102 |
return $order->customer->email ?? null; |
| 103 |
} |
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
public function getFormattedAddressAttribute(): array |
| 108 |
{ |
| 109 |
return $this->getFormattedAddress(); |
| 110 |
} |
| 111 |
|
| 112 |
public function getFormattedAddress($filtered = false): array |
| 113 |
{ |
| 114 |
$address = [ |
| 115 |
'country' => AddressHelper::getCountryNameByCode($this->country), |
| 116 |
'state' => AddressHelper::getStateNameByCode($this->state, $this->country), |
| 117 |
'city' => $this->city, |
| 118 |
'postcode' => $this->postcode, |
| 119 |
'address_1' => $this->address_1, |
| 120 |
'address_2' => $this->address_2, |
| 121 |
'type' => $this->type, |
| 122 |
'name' => $this->name, |
| 123 |
'first_name' => $this->first_name, |
| 124 |
'last_name' => $this->last_name, |
| 125 |
'full_name' => $this->full_name, |
| 126 |
'email' => $this->email, |
| 127 |
'company_name' => $this->company_name, |
| 128 |
'label' => $this->label |
| 129 |
]; |
| 130 |
|
| 131 |
if ($filtered) { |
| 132 |
$address = array_filter($address); |
| 133 |
} |
| 134 |
|
| 135 |
$addressParts = [ |
| 136 |
trim(Arr::get($address, 'address_1') ?? ''), |
| 137 |
trim(Arr::get($address, 'address_2') ?? ''), |
| 138 |
trim(Arr::get($address, 'city') ?? ''), |
| 139 |
trim(Arr::get($address, 'state') ?? ''), |
| 140 |
trim(Arr::get($address, 'country') ?? ''), |
| 141 |
]; |
| 142 |
|
| 143 |
$addressParts = array_filter($addressParts, function ($part) { |
| 144 |
return $part !== ''; |
| 145 |
}); |
| 146 |
$address['full_address'] = implode(', ', $addressParts); |
| 147 |
|
| 148 |
|
| 149 |
return $address; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
public function getAddressAsText($isHtml = false, $includeName = true, $separator = ', '): string |
| 154 |
{ |
| 155 |
$address = $this->formatted_address; |
| 156 |
|
| 157 |
$formatted = array_filter([ |
| 158 |
$includeName ? $address['name'] : '', |
| 159 |
$address['address_1'], |
| 160 |
$address['address_2'], |
| 161 |
$address['city'], |
| 162 |
$address['state'], |
| 163 |
$address['postcode'], |
| 164 |
$address['country'] |
| 165 |
]); |
| 166 |
|
| 167 |
return implode($separator, $formatted); |
| 168 |
} |
| 169 |
|
| 170 |
public function getFormattedDataForCheckout($prefix = 'billing_') |
| 171 |
{ |
| 172 |
$data = [ |
| 173 |
'' . $prefix . 'address_id' => $this->id, |
| 174 |
'' . $prefix . 'full_name' => $this->name, |
| 175 |
'' . $prefix . 'address_1' => $this->address_1, |
| 176 |
'' . $prefix . 'address_2' => $this->address_2, |
| 177 |
'' . $prefix . 'city' => $this->city, |
| 178 |
'' . $prefix . 'state' => $this->state, |
| 179 |
'' . $prefix . 'phone' => $this->phone, |
| 180 |
'' . $prefix . 'postcode' => $this->postcode, |
| 181 |
'' . $prefix . 'country' => $this->country, |
| 182 |
'' . $prefix . 'company_name' => $this->company_name, |
| 183 |
'' . $prefix . 'vat_number' => $this->vat_number, |
| 184 |
'' . $prefix . 'legal_registration_id' => $this->legal_registration_id, |
| 185 |
]; |
| 186 |
|
| 187 |
if ($prefix === 'billing_') { |
| 188 |
unset($data['billing_full_name']); |
| 189 |
} |
| 190 |
|
| 191 |
return $data; |
| 192 |
} |
| 193 |
|
| 194 |
public function setCompanyNameAttribute($value) |
| 195 |
{ |
| 196 |
if (!$value) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
$meta = $this->meta; |
| 201 |
|
| 202 |
if (!is_array($meta)) { |
| 203 |
$meta = []; |
| 204 |
} |
| 205 |
|
| 206 |
Arr::set($meta, 'other_data.company_name', $value); |
| 207 |
|
| 208 |
$this->attributes['meta'] = json_encode($meta); |
| 209 |
} |
| 210 |
|
| 211 |
public function getCompanyNameAttribute() |
| 212 |
{ |
| 213 |
return Arr::get($this->meta, 'other_data.company_name', ''); |
| 214 |
} |
| 215 |
|
| 216 |
public function setVatNumberAttribute($value) |
| 217 |
{ |
| 218 |
$meta = is_array($this->meta) ? $this->meta : []; |
| 219 |
|
| 220 |
if ($value) { |
| 221 |
Arr::set($meta, 'other_data.vat_number', $value); |
| 222 |
} else { |
| 223 |
unset($meta['other_data']['vat_number']); |
| 224 |
} |
| 225 |
|
| 226 |
$this->attributes['meta'] = json_encode($meta); |
| 227 |
} |
| 228 |
|
| 229 |
public function getVatNumberAttribute() |
| 230 |
{ |
| 231 |
return Arr::get($this->meta, 'other_data.vat_number', ''); |
| 232 |
} |
| 233 |
|
| 234 |
public function setLegalRegistrationIdAttribute($value) |
| 235 |
{ |
| 236 |
$meta = is_array($this->meta) ? $this->meta : []; |
| 237 |
|
| 238 |
if ($value) { |
| 239 |
Arr::set($meta, 'other_data.legal_registration_id', $value); |
| 240 |
} else { |
| 241 |
unset($meta['other_data']['legal_registration_id']); |
| 242 |
} |
| 243 |
|
| 244 |
$this->attributes['meta'] = json_encode($meta); |
| 245 |
} |
| 246 |
|
| 247 |
public function getLegalRegistrationIdAttribute() |
| 248 |
{ |
| 249 |
return Arr::get($this->meta, 'other_data.legal_registration_id', ''); |
| 250 |
} |
| 251 |
|
| 252 |
public function setLabelAttribute($value) |
| 253 |
{ |
| 254 |
if (!$value) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
$meta = $this->meta; |
| 259 |
|
| 260 |
if (!is_array($meta)) { |
| 261 |
$meta = []; |
| 262 |
} |
| 263 |
|
| 264 |
Arr::set($meta, 'other_data.label', $value); |
| 265 |
|
| 266 |
$this->attributes['meta'] = json_encode($meta); |
| 267 |
} |
| 268 |
|
| 269 |
public function getLabelAttribute() |
| 270 |
{ |
| 271 |
return Arr::get($this->meta, 'other_data.label', ''); |
| 272 |
} |
| 273 |
|
| 274 |
public function setPhoneAttribute($value) |
| 275 |
{ |
| 276 |
if (!$value) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$meta = $this->meta; |
| 281 |
|
| 282 |
if (!is_array($meta)) { |
| 283 |
$meta = []; |
| 284 |
} |
| 285 |
|
| 286 |
Arr::set($meta, 'other_data.phone', $value); |
| 287 |
|
| 288 |
$this->attributes['meta'] = json_encode($meta); |
| 289 |
} |
| 290 |
|
| 291 |
public function getPhoneAttribute() |
| 292 |
{ |
| 293 |
return Arr::get($this->meta, 'other_data.phone', ''); |
| 294 |
} |
| 295 |
|
| 296 |
} |
| 297 |
|