|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverShop\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
|
6
|
|
|
use SilverStripe\ORM\DB; |
|
7
|
|
|
use SilverStripe\ORM\FieldType\DBDate; |
|
8
|
|
|
use SilverStripe\Security\Member; |
|
9
|
|
|
use SilverStripe\Security\Security; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Data class that keeps a log of a single |
|
13
|
|
|
* status of an order. |
|
14
|
|
|
* |
|
15
|
|
|
* @property string $Title |
|
16
|
|
|
* @property string $Note |
|
17
|
|
|
* @property string $DispatchedBy |
|
18
|
|
|
* @property DBDate $DispatchedOn |
|
19
|
|
|
* @property string $DispatchTicket |
|
20
|
|
|
* @property string $PaymentCode |
|
21
|
|
|
* @property bool $PaymentOK |
|
22
|
|
|
* @property bool $SentToCustomer Whether or not this entry has been sent to the customer (e.g. via OrderEmailNotifier) |
|
23
|
|
|
* @property bool $VisibleToCustomer Whether or not this entry should be visible to the customer (e.g. on order details) |
|
24
|
|
|
* @property int $AuthorID |
|
25
|
|
|
* @property int $OrderID |
|
26
|
|
|
* |
|
27
|
|
|
* @method Member Author() |
|
28
|
|
|
* @method Order Order() |
|
29
|
|
|
*/ |
|
30
|
|
|
class OrderStatusLog extends DataObject |
|
31
|
|
|
{ |
|
32
|
|
|
private static $db = [ |
|
|
|
|
|
|
33
|
|
|
'Title' => 'Varchar(100)', |
|
34
|
|
|
'Note' => 'Text', |
|
35
|
|
|
'DispatchedBy' => 'Varchar(100)', |
|
36
|
|
|
'DispatchedOn' => 'Date', |
|
37
|
|
|
'DispatchTicket' => 'Varchar(100)', |
|
38
|
|
|
'PaymentCode' => 'Varchar(100)', |
|
39
|
|
|
'PaymentOK' => 'Boolean', |
|
40
|
|
|
'SentToCustomer' => 'Boolean', |
|
41
|
|
|
'VisibleToCustomer' => 'Boolean', |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
45
|
|
|
'Author' => Member::class, |
|
46
|
|
|
'Order' => Order::class, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
private static $searchable_fields = [ |
|
|
|
|
|
|
50
|
|
|
'Order.Reference' => [ |
|
51
|
|
|
'filter' => 'PartialMatchFilter', |
|
52
|
|
|
'title' => 'Order No' |
|
53
|
|
|
], |
|
54
|
|
|
'Order.FirstName' => [ |
|
55
|
|
|
'filter' => 'PartialMatchFilter', |
|
56
|
|
|
'title' => 'First Name' |
|
57
|
|
|
], |
|
58
|
|
|
'Order.Email' => [ |
|
59
|
|
|
'filter' => 'PartialMatchFilter', |
|
60
|
|
|
'title' => 'Email' |
|
61
|
|
|
] |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
65
|
|
|
'Order.Reference' => 'Order No', |
|
66
|
|
|
'Created' => 'Created', |
|
67
|
|
|
'Order.Name' => 'Name', |
|
68
|
|
|
'Order.LatestEmail' => 'Email', |
|
69
|
|
|
'Title' => 'Title', |
|
70
|
|
|
'SentToCustomer' => 'Emailed', |
|
71
|
|
|
'VisibleToCustomer' => 'Visible to customer?' |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
private static $singular_name = 'Order Log Entry'; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
private static $plural_name = 'Order Status Log Entries'; |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
private static $default_sort = '"Created" DESC'; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
private static $table_name = 'SilverShop_OrderStatusLog'; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
public function canCreate($member = null, $context = []) |
|
83
|
|
|
{ |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function canDelete($member = null) |
|
88
|
|
|
{ |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function canEdit($member = null) |
|
93
|
|
|
{ |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function populateDefaults() |
|
98
|
|
|
{ |
|
99
|
|
|
parent::populateDefaults(); |
|
100
|
|
|
$this->updateWithLastInfo(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function requireDefaultRecords() |
|
104
|
|
|
{ |
|
105
|
|
|
parent::requireDefaultRecords(); |
|
106
|
|
|
|
|
107
|
|
|
// If there are existing records with SentToCustomer=true and there are no records with VisibleToCustomer=true, |
|
108
|
|
|
// then we assume this is an upgrade (if a record was sent to the customer, then by definition it's visible to |
|
109
|
|
|
// the customer. However, we use the count check to ensure we only make the database change up until at least |
|
110
|
|
|
// one record has VisibleToCustomer = true (to avoid resetting it in future) |
|
111
|
|
|
if (OrderStatusLog::get()->filter('VisibleToCustomer', true)->count() == 0) { |
|
112
|
|
|
// We don't have any records with VisibleToCustomer true, so update all records with SentToCustomer = true |
|
113
|
|
|
$toUpdate = OrderStatusLog::get()->filter('SentToCustomer', true); |
|
114
|
|
|
$updated = 0; |
|
115
|
|
|
|
|
116
|
|
|
/** @var OrderStatusLog $log */ |
|
117
|
|
|
foreach ($toUpdate as $log) { |
|
118
|
|
|
$log->VisibleToCustomer = true; |
|
119
|
|
|
$log->write(); |
|
120
|
|
|
$updated++; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$message = sprintf( |
|
124
|
|
|
'Migrated %d records to new format (set VisibleToCustomer=true where SentToCustomer=true)', |
|
125
|
|
|
$updated |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
DB::alteration_message($message, 'changed'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function onBeforeWrite() |
|
133
|
|
|
{ |
|
134
|
|
|
parent::onBeforeWrite(); |
|
135
|
|
|
if (!$this->AuthorID && ($member = Security::getCurrentUser())) { |
|
136
|
|
|
$this->AuthorID = $member->ID; |
|
137
|
|
|
} |
|
138
|
|
|
if (!$this->Title) { |
|
139
|
|
|
$this->Title = 'Order Update'; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function validate() |
|
144
|
|
|
{ |
|
145
|
|
|
$validationResult = parent::validate(); |
|
146
|
|
|
if (!$this->OrderID) { |
|
147
|
|
|
$validationResult->addError('there is no order id for Order Status Log'); |
|
148
|
|
|
} |
|
149
|
|
|
return $validationResult; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
protected function updateWithLastInfo() |
|
153
|
|
|
{ |
|
154
|
|
|
if ($this->OrderID) { |
|
155
|
|
|
/** @var OrderStatusLog $latestLog */ |
|
156
|
|
|
$latestLog = OrderStatusLog::get() |
|
157
|
|
|
->filter('OrderID', $this->OrderID) |
|
158
|
|
|
->sort('Created', 'DESC') |
|
159
|
|
|
->first(); |
|
160
|
|
|
|
|
161
|
|
|
if ($latestLog) { |
|
|
|
|
|
|
162
|
|
|
$this->DispatchedBy = $latestLog->DispatchedBy; |
|
163
|
|
|
$this->DispatchedOn = $latestLog->DispatchedOn; |
|
164
|
|
|
$this->DispatchTicket = $latestLog->DispatchTicket; |
|
165
|
|
|
$this->PaymentCode = $latestLog->PaymentCode; |
|
166
|
|
|
$this->PaymentOK = $latestLog->PaymentOK; |
|
167
|
|
|
$this->SentToCustomer = $latestLog->SentToCustomer; |
|
168
|
|
|
$this->VisibleToCustomer = $latestLog->VisibleToCustomer; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|