|
1
|
|
|
<?php namespace Bedard\Shop\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Model; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Address Model. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Address extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string The database table used by the model. |
|
12
|
|
|
*/ |
|
13
|
|
|
public $table = 'bedard_shop_addresses'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array Default attributes |
|
17
|
|
|
*/ |
|
18
|
|
|
public $attributes = [ |
|
19
|
|
|
'additional_data' => '', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array Attribute casting |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $casts = [ |
|
26
|
|
|
'is_billing' => 'boolean', |
|
27
|
|
|
'is_primary' => 'boolean', |
|
28
|
|
|
'is_shipping' => 'boolean', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array Jsonable fields |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $jsonable = [ |
|
36
|
|
|
'additional_data', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array Guarded fields |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $guarded = ['*']; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array Fillable fields |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $fillable = [ |
|
48
|
|
|
'additional_data', |
|
49
|
|
|
'city', |
|
50
|
|
|
'country', |
|
51
|
|
|
'customer_id', |
|
52
|
|
|
'is_billing', |
|
53
|
|
|
'is_primary', |
|
54
|
|
|
'is_shipping', |
|
55
|
|
|
'line_1', |
|
56
|
|
|
'line_2', |
|
57
|
|
|
'line_3', |
|
58
|
|
|
'postcode', |
|
59
|
|
|
'province', |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array Relations |
|
64
|
|
|
*/ |
|
65
|
|
|
public $belongsTo = [ |
|
66
|
|
|
'customer' => [ |
|
67
|
|
|
'Bedard\Shop\Models\Customer', |
|
68
|
|
|
], |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* After save. |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function afterSave() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->updatePrimaryAddresses(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Select shipping addresses. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \October\Rain\Database\Builder $query |
|
85
|
|
|
* @return \October\Rain\Database\Builder |
|
86
|
|
|
*/ |
|
87
|
|
|
public function scopeIsShipping($query) |
|
88
|
|
|
{ |
|
89
|
|
|
return $query->whereIsShipping(true); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Select billing addresses. |
|
94
|
|
|
* |
|
95
|
|
|
* @param \October\Rain\Database\Builder $query |
|
96
|
|
|
* @return \October\Rain\Database\Builder |
|
97
|
|
|
*/ |
|
98
|
|
|
public function scopeIsBilling($query) |
|
99
|
|
|
{ |
|
100
|
|
|
return $query->whereIsBilling(true); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Select primary addresses. |
|
105
|
|
|
* |
|
106
|
|
|
* @param \October\Rain\Database\Builder $query |
|
107
|
|
|
* @return \October\Rain\Database\Builder |
|
108
|
|
|
*/ |
|
109
|
|
|
public function scopeIsPrimary($query) |
|
110
|
|
|
{ |
|
111
|
|
|
return $query->whereIsPrimary(true); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Ensure that only one billing and shipping address may be primary. |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function updatePrimaryAddresses() |
|
120
|
|
|
{ |
|
121
|
|
|
if (! $this->customer_id) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$query = self::whereCustomerId($this->customer_id) |
|
126
|
|
|
->where('id', '<>', $this->id) |
|
127
|
|
|
->isPrimary(); |
|
128
|
|
|
|
|
129
|
|
|
if ($this->is_shipping && $this->is_primary) { |
|
130
|
|
|
$shipping = $query->isShipping()->update(['is_primary' => false]); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ($this->is_billing && $this->is_primary) { |
|
134
|
|
|
$shipping = $query->isBilling()->update(['is_primary' => false]); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.