|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Reservation.php |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bram de Leeuw |
|
6
|
|
|
* Date: 09/03/17 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
|
10
|
|
|
|
|
11
|
|
|
use CalendarEvent; |
|
12
|
|
|
use CalendarEvent_Controller; |
|
13
|
|
|
use DataObject; |
|
14
|
|
|
use Director; |
|
15
|
|
|
use Dompdf\Dompdf; |
|
16
|
|
|
use FieldList; |
|
17
|
|
|
use File; |
|
18
|
|
|
use Folder; |
|
19
|
|
|
use GridField; |
|
20
|
|
|
use GridFieldConfig_RecordViewer; |
|
21
|
|
|
use HasManyList; |
|
22
|
|
|
use ManyManyList; |
|
23
|
|
|
use Member; |
|
24
|
|
|
use ReadonlyField; |
|
25
|
|
|
use SiteConfig; |
|
26
|
|
|
use SSViewer; |
|
27
|
|
|
use Tab; |
|
28
|
|
|
use TabSet; |
|
29
|
|
|
use ViewableData; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class Reservation |
|
33
|
|
|
* |
|
34
|
|
|
* @package Broarm\EventTickets |
|
35
|
|
|
* |
|
36
|
|
|
* @property string Status |
|
37
|
|
|
* @property string Title |
|
38
|
|
|
* @property float Subtotal |
|
39
|
|
|
* @property float Total |
|
40
|
|
|
* @property string Email todo determine obsolete value |
|
41
|
|
|
* @property string Comments |
|
42
|
|
|
* @property string ReservationCode |
|
43
|
|
|
* @property string Gateway |
|
44
|
|
|
* |
|
45
|
|
|
* @property int EventID |
|
46
|
|
|
* @property int MainContactID |
|
47
|
|
|
* |
|
48
|
|
|
* @method CalendarEvent|TicketExtension Event |
|
49
|
|
|
* @method Attendee MainContact |
|
50
|
|
|
* @method HasManyList Payments |
|
51
|
|
|
* @method HasManyList Attendees |
|
52
|
|
|
* @method ManyManyList PriceModifiers |
|
53
|
|
|
*/ |
|
54
|
|
|
class Reservation extends DataObject |
|
55
|
|
|
{ |
|
56
|
|
|
/** |
|
57
|
|
|
* Time to wait before deleting the discarded cart |
|
58
|
|
|
* Give a string that is parsable by strtotime |
|
59
|
|
|
* |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
private static $delete_after = '+1 week'; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
private static $db = array( |
|
|
|
|
|
|
65
|
|
|
'Status' => 'Enum("CART,PENDING,PAID,CANCELED","CART")', |
|
66
|
|
|
'Title' => 'Varchar(255)', |
|
67
|
|
|
'Subtotal' => 'Currency', |
|
68
|
|
|
'Total' => 'Currency', |
|
69
|
|
|
'Email' => 'Varchar(255)', |
|
70
|
|
|
'Gateway' => 'Varchar(255)', |
|
71
|
|
|
'Comments' => 'Text', |
|
72
|
|
|
'ReservationCode' => 'Varchar(255)' |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
76
|
|
|
'Event' => 'CalendarEvent', |
|
77
|
|
|
//'TicketFile' => 'File', |
|
|
|
|
|
|
78
|
|
|
'MainContact' => 'Broarm\EventTickets\Attendee' |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
82
|
|
|
'Payments' => 'Payment', |
|
83
|
|
|
'Attendees' => 'Broarm\EventTickets\Attendee.Reservation' |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
|
|
87
|
|
|
'PriceModifiers' => 'Broarm\EventTickets\PriceModifier' |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
91
|
|
|
'ReservationCode' => 'unique("ReservationCode")' |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
95
|
|
|
'Title' => 'Customer', |
|
96
|
|
|
'Total.Nice' => 'Total', |
|
97
|
|
|
'State' => 'Status' |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
public function getCMSFields() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main'))); |
|
103
|
|
|
$gridFieldConfig = GridFieldConfig_RecordViewer::create(); |
|
104
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
|
105
|
|
|
ReadonlyField::create('ReservationCode', _t('Reservation.Code', 'Code')), |
|
106
|
|
|
ReadonlyField::create('Title', _t('Reservation.MainContact', 'Main contact')), |
|
107
|
|
|
ReadonlyField::create('Gateway', _t('Reservation.Gateway', 'Gateway')), |
|
108
|
|
|
ReadonlyField::create('Total', _t('Reservation.Total', 'Total')), |
|
109
|
|
|
ReadonlyField::create('Comments', _t('Reservation.Comments', 'Comments')), |
|
110
|
|
|
$reservationFileField = ReadonlyField::create( |
|
111
|
|
|
'ReservationFile', |
|
112
|
|
|
_t('Attendee.Reservation', 'Reservation'), |
|
113
|
|
|
"<a class='readonly' href='{$this->TicketFile()->Link()}' target='_blank'>Download reservation PDF</a>" |
|
|
|
|
|
|
114
|
|
|
), |
|
115
|
|
|
GridField::create('Attendees', 'Attendees', $this->Attendees(), $gridFieldConfig), |
|
|
|
|
|
|
116
|
|
|
GridField::create('Payments', 'Payments', $this->Payments(), $gridFieldConfig) |
|
|
|
|
|
|
117
|
|
|
)); |
|
118
|
|
|
$reservationFileField->dontEscape = true; |
|
119
|
|
|
$fields->addFieldsToTab('Root.Main', array()); |
|
120
|
|
|
$this->extend('updateCMSFields', $fields); |
|
121
|
|
|
return $fields; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @deprecated |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
public function TicketFile() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->Attendees()->first()->TicketFile(); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function onBeforeWrite() |
|
134
|
|
|
{ |
|
135
|
|
|
// Set the title to the name of the reservation holder |
|
136
|
|
|
$this->Title = $this->getName(); |
|
137
|
|
|
|
|
138
|
|
|
// Create a validation code to be used for confirmation and in the barcode |
|
139
|
|
|
if ($this->exists() && empty($this->ReservationCode)) { |
|
140
|
|
|
$this->ReservationCode = $this->createReservationCode(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
parent::onBeforeWrite(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function onBeforeDelete() |
|
147
|
|
|
{ |
|
148
|
|
|
// If a reservation is deleted remove the names from the guest list |
|
149
|
|
|
foreach ($this->Attendees() as $attendee) { |
|
|
|
|
|
|
150
|
|
|
/** @var Attendee $attendee */ |
|
151
|
|
|
if ($attendee->exists()) { |
|
152
|
|
|
$attendee->delete(); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// Make sure the ticket file is not downloadable |
|
157
|
|
|
//if ($this->TicketFile()->exists()) { |
|
|
|
|
|
|
158
|
|
|
// $this->TicketFile()->delete(); |
|
|
|
|
|
|
159
|
|
|
//} |
|
160
|
|
|
|
|
161
|
|
|
// Remove the folder |
|
162
|
|
|
if ($this->fileFolder()->exists()) { |
|
163
|
|
|
$this->fileFolder()->delete(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
parent::onBeforeDelete(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function singular_name() |
|
170
|
|
|
{ |
|
171
|
|
|
$name = explode('\\', parent::singular_name()); |
|
172
|
|
|
return trim(end($name)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Check if the cart is still in cart state and the delete_after time period has been exceeded |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
public function isDiscarded() |
|
181
|
|
|
{ |
|
182
|
|
|
$deleteAfter = strtotime(self::config()->get('delete_after'), strtotime($this->Created)); |
|
183
|
|
|
return ($this->Status === 'CART') && (time() > $deleteAfter); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get the full name |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getName() |
|
192
|
|
|
{ |
|
193
|
|
|
/** @var Attendee $attendee */ |
|
194
|
|
|
if ($this->MainContact()->exists()) { |
|
|
|
|
|
|
195
|
|
|
return $this->MainContact()->getName(); |
|
|
|
|
|
|
196
|
|
|
} else { |
|
197
|
|
|
return 'new reservation'; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Return the translated state |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getState() |
|
206
|
|
|
{ |
|
207
|
|
|
return _t("Reservation.{$this->Status}", $this->Status); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get the total by querying the sum of attendee ticket prices |
|
212
|
|
|
* |
|
213
|
|
|
* @return float |
|
214
|
|
|
*/ |
|
215
|
|
|
public function calculateTotal() |
|
216
|
|
|
{ |
|
217
|
|
|
$total = $this->Subtotal = $this->Attendees()->leftJoin( |
|
|
|
|
|
|
218
|
|
|
'Broarm\EventTickets\Ticket', |
|
219
|
|
|
'`Broarm\EventTickets\Attendee`.`TicketID` = `Broarm\EventTickets\Ticket`.`ID`' |
|
220
|
|
|
)->sum('Price'); |
|
221
|
|
|
|
|
222
|
|
|
// Calculate any price modifications if added |
|
223
|
|
|
if ($this->PriceModifiers()->exists()) { |
|
|
|
|
|
|
224
|
|
|
foreach ($this->PriceModifiers() as $priceModifier) { |
|
|
|
|
|
|
225
|
|
|
$priceModifier->updateTotal($total); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this->Total = $total; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Safely change to a state |
|
234
|
|
|
* |
|
235
|
|
|
* @param $state |
|
236
|
|
|
*/ |
|
237
|
|
|
public function changeState($state) |
|
238
|
|
|
{ |
|
239
|
|
|
$availableStates = $this->dbObject('Status')->enumValues(); |
|
240
|
|
|
if (in_array($state, $availableStates)) { |
|
241
|
|
|
$this->Status = $state; |
|
242
|
|
|
} else { |
|
243
|
|
|
user_error(_t('Reservation.STATE_CHANGE_ERROR', 'Selected state is not available')); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Set the main contact id |
|
249
|
|
|
* |
|
250
|
|
|
* @param $id |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setMainContact($id) { |
|
253
|
|
|
$this->MainContactID = $id; |
|
254
|
|
|
$this->write(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Create a reservation code |
|
259
|
|
|
* |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
|
|
public function createReservationCode() |
|
263
|
|
|
{ |
|
264
|
|
|
return uniqid($this->ID); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Create the folder for the qr code and ticket file |
|
269
|
|
|
* |
|
270
|
|
|
* @return Folder|DataObject|null |
|
271
|
|
|
*/ |
|
272
|
|
|
public function fileFolder() |
|
273
|
|
|
{ |
|
274
|
|
|
return Folder::find_or_make("/event-tickets/{$this->ReservationCode}/"); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Generate the qr codes and downloadable pdf |
|
279
|
|
|
*/ |
|
280
|
|
|
public function createFiles() |
|
281
|
|
|
{ |
|
282
|
|
|
$folder = $folder = $this->fileFolder(); |
|
283
|
|
|
/** @var Attendee $attendee */ |
|
284
|
|
|
foreach ($this->Attendees() as $attendee) { |
|
|
|
|
|
|
285
|
|
|
$attendee->createQRCode($folder); |
|
286
|
|
|
$attendee->createTicketFile($folder); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Create a Printable ticket file |
|
292
|
|
|
* |
|
293
|
|
|
* @param Folder $folder |
|
294
|
|
|
* |
|
295
|
|
|
* @return File |
|
296
|
|
|
* / |
|
297
|
|
|
private function createTicketFile(Folder $folder) |
|
298
|
|
|
{ |
|
299
|
|
|
// Find or make a folder |
|
300
|
|
|
$relativeFilePath = "/{$folder->Filename}{$this->ReservationCode}.pdf"; |
|
301
|
|
|
$absoluteFilePath = Director::baseFolder() . $relativeFilePath; |
|
302
|
|
|
|
|
303
|
|
|
if (!$file = File::get()->find('Filename', $relativeFilePath)) { |
|
304
|
|
|
$file = File::create(); |
|
305
|
|
|
$file->ParentID = $folder->ID; |
|
306
|
|
|
$file->OwnerID = (Member::currentUser()) ? Member::currentUser()->ID : 0; |
|
307
|
|
|
$file->Title = $this->ReservationCode; |
|
308
|
|
|
$file->setFilename($relativeFilePath); |
|
309
|
|
|
$file->write(); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
// Set the template and parse the data |
|
313
|
|
|
$template = new SSViewer('PrintableTicket'); |
|
314
|
|
|
$html = $template->process($this->getViewableData()); |
|
315
|
|
|
|
|
316
|
|
|
// Create a DomPDF instance |
|
317
|
|
|
$domPDF = new Dompdf(); |
|
318
|
|
|
$domPDF->loadHtml($html); |
|
319
|
|
|
$domPDF->setPaper('A4'); |
|
320
|
|
|
$domPDF->getOptions()->setDpi(250); |
|
321
|
|
|
$domPDF->render(); |
|
322
|
|
|
|
|
323
|
|
|
// Save the pdf stream as a file |
|
324
|
|
|
file_put_contents($absoluteFilePath, $domPDF->output()); |
|
325
|
|
|
|
|
326
|
|
|
// Attach the ticket file to the Attendee |
|
327
|
|
|
$this->TicketFileID = $file->ID; |
|
328
|
|
|
$this->write(); |
|
329
|
|
|
|
|
330
|
|
|
return $file; |
|
331
|
|
|
}*/ |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Get a viewable data object for this reservation |
|
335
|
|
|
* For use in the Email and print template |
|
336
|
|
|
* @deprecated |
|
337
|
|
|
* @return ViewableData |
|
338
|
|
|
* / |
|
339
|
|
|
public function getViewableData() |
|
340
|
|
|
{ |
|
341
|
|
|
$config = SiteConfig::current_site_config(); |
|
342
|
|
|
$data = $this->Me(); |
|
343
|
|
|
$data->CurrentDate = $this->Event()->getController()->CurrentDate(); |
|
344
|
|
|
$data->Logo = $config->TicketLogo(); |
|
345
|
|
|
$this->extend('updateViewableData', $data); |
|
346
|
|
|
return $data; |
|
347
|
|
|
}*/ |
|
348
|
|
|
|
|
349
|
|
|
public function canView($member = null) |
|
350
|
|
|
{ |
|
351
|
|
|
return $this->Event()->canView($member); |
|
|
|
|
|
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
public function canEdit($member = null) |
|
|
|
|
|
|
355
|
|
|
{ |
|
356
|
|
|
return $this->Event()->canEdit($member); |
|
|
|
|
|
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
public function canDelete($member = null) |
|
|
|
|
|
|
360
|
|
|
{ |
|
361
|
|
|
return $this->Event()->canDelete($member); |
|
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
public function canCreate($member = null) |
|
|
|
|
|
|
365
|
|
|
{ |
|
366
|
|
|
return $this->Event()->canCreate($member); |
|
|
|
|
|
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.