PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.23
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.23
1.6.5 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 All 48 releases
fluent-cart / app / Models / CustomerAddresses.php

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

171 lines 4.3 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'];
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 'company_name',
50 ];
51
52
53 public function setMetaAttribute($value)
54 {
55 if ($value) {
56 $decoded = \json_encode($value, true);
57 if (!($decoded)) {
58 $decoded = '[]';
59 }
60 } else {
61 $decoded = '[]';
62 }
63
64 $this->attributes['meta'] = $decoded;
65 }
66
67 public function getMetaAttribute($value)
68 {
69 if (!$value) {
70 return [];
71 }
72
73 return \json_decode($value, true);
74 }
75
76 public function setCompanyNameAttribute($value)
77 {
78 if (!$value) {
79 return;
80 }
81
82 $meta = $this->meta;
83
84 if (!is_array($meta)) {
85 $meta = [];
86 }
87
88 Arr::set($meta, 'other_data.company_name', $value);
89
90 $this->attributes['meta'] = json_encode($meta);
91 }
92
93 public function getCompanyNameAttribute()
94 {
95 return Arr::get($this->meta, 'other_data.company_name', '');
96 }
97
98 public function scopeOfActive($query)
99 {
100 return $query->where('status', 'active');
101 }
102
103 public function scopeOfArchived($query)
104 {
105 return $query->where('status', 'archived');
106 }
107
108 public function customer()
109 {
110 return $this->belongsTo(Customer::class, 'customer_id', 'id');
111 }
112
113 public function getFormattedAddressAttribute(): array
114 {
115 $formattedAddress = [
116 'country' => AddressHelper::getCountryNameByCode($this->country),
117 'state' => AddressHelper::getStateNameByCode($this->state, $this->country),
118 'city' => $this->city,
119 'postcode' => $this->postcode,
120 'address_1' => $this->address_1,
121 'address_2' => $this->address_2,
122 'type' => $this->type,
123 'name' => $this->name,
124 'first_name' => $this->first_name,
125 'last_name' => $this->last_name,
126 'full_name' => $this->full_name,
127 'company_name' => $this->company_name,
128 'label' => $this->label,
129 'phone' => $this->phone
130 ];
131
132 $addressParts = [
133 trim(Arr::get($formattedAddress, 'company_name') ?? ''),
134 trim(Arr::get($formattedAddress, 'address_1') ?? ''),
135 trim(Arr::get($formattedAddress, 'address_2') ?? ''),
136 trim(Arr::get($formattedAddress, 'city') ?? ''),
137 trim(Arr::get($formattedAddress, 'state') ?? ''),
138 trim(Arr::get($formattedAddress, 'country') ?? ''),
139 ];
140 $addressParts = array_filter($addressParts, function ($part) {
141 return $part !== '';
142 });
143 $fullAddress = implode(', ', $addressParts);
144
145 $formattedAddress['full_address'] = $fullAddress;
146
147 return $formattedAddress;
148 }
149
150 public function getFormattedDataForCheckout($prefix = 'billing_')
151 {
152 $data = [
153 '' . $prefix . 'full_name' => $this->name,
154 '' . $prefix . 'address_1' => $this->address_1,
155 '' . $prefix . 'address_2' => $this->address_2,
156 '' . $prefix . 'city' => $this->city,
157 '' . $prefix . 'state' => $this->state,
158 '' . $prefix . 'phone' => $this->phone,
159 '' . $prefix . 'postcode' => $this->postcode,
160 '' . $prefix . 'country' => $this->country,
161 '' . $prefix . 'company_name' => $this->company_name,
162 ];
163
164 // if ($prefix === 'billing_') {
165 // unset($data['billing_full_name']);
166 // }
167
168 return $data;
169 }
170 }
171