Total Complexity | 53 |
Total Lines | 606 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Invoice often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Invoice, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Invoice extends Estimate implements PermissionProvider |
||
50 | { |
||
51 | |||
52 | private static $table_name = 'Invoice'; |
||
|
|||
53 | |||
54 | /** |
||
55 | * The amount of days that by default that this estimate |
||
56 | * will end (cease to be valid). |
||
57 | * |
||
58 | * @var integer |
||
59 | */ |
||
60 | private static $default_end = 0; |
||
61 | |||
62 | /** |
||
63 | * List of possible statuses this order can have. Rather than using |
||
64 | * an enum, we load this as a config variable that can be changed |
||
65 | * more freely. |
||
66 | * |
||
67 | * @var array |
||
68 | * @config |
||
69 | */ |
||
70 | private static $statuses = [ |
||
71 | "incomplete" => "Incomplete", |
||
72 | "failed" => "Failed", |
||
73 | "cancelled" => "Cancelled", |
||
74 | "pending" => "Pending", |
||
75 | "part-paid" => "Part Paid", |
||
76 | "paid" => "Paid", |
||
77 | "processing" => "Processing", |
||
78 | "ready" => "Ready", |
||
79 | "dispatched" => "Dispatched", |
||
80 | "collected" => "Collected", |
||
81 | "refunded" => "Refunded" |
||
82 | ]; |
||
83 | |||
84 | /** |
||
85 | * What statuses does an invoice need to be considered |
||
86 | * "outstanding" (meaning not dispatched or complete]). |
||
87 | * |
||
88 | * @var array |
||
89 | * @config |
||
90 | */ |
||
91 | private static $outstanding_statuses = [ |
||
92 | "part-paid", |
||
93 | "paid", |
||
94 | "processing" |
||
95 | ]; |
||
96 | |||
97 | /** |
||
98 | * What statuses does an order need to be considered |
||
99 | * "historic" (meaning dispatched/completed, etc) |
||
100 | * |
||
101 | * |
||
102 | * @var array |
||
103 | * @config |
||
104 | */ |
||
105 | private static $historic_statuses = [ |
||
106 | "dispatched", |
||
107 | "collected", |
||
108 | "canceled" |
||
109 | ]; |
||
110 | |||
111 | /** |
||
112 | * What statuses are considered "paid for". Meaning |
||
113 | * they are complete, ready for processing, etc. |
||
114 | * |
||
115 | * @var array |
||
116 | * @config |
||
117 | */ |
||
118 | private static $paid_statuses = [ |
||
119 | "paid", |
||
120 | "processing", |
||
121 | "ready", |
||
122 | "dispatched", |
||
123 | "collected" |
||
124 | ]; |
||
125 | |||
126 | /** |
||
127 | * List of statuses that allow editing of an order. We can use this |
||
128 | * to fix certain orders in the CMS |
||
129 | * |
||
130 | * @var array |
||
131 | * @config |
||
132 | */ |
||
133 | private static $editable_statuses = [ |
||
134 | "", |
||
135 | "incomplete", |
||
136 | "pending", |
||
137 | "part-paid", |
||
138 | "paid", |
||
139 | "failed", |
||
140 | "cancelled" |
||
141 | ]; |
||
142 | |||
143 | /** |
||
144 | * Set the default status for a new order, if this is set to null or |
||
145 | * blank, it will not be used. |
||
146 | * |
||
147 | * @var string |
||
148 | * @config |
||
149 | */ |
||
150 | private static $default_status = "incomplete"; |
||
151 | |||
152 | /** |
||
153 | * The status which an order is considered "complete". |
||
154 | * |
||
155 | * @var string |
||
156 | * @config |
||
157 | */ |
||
158 | private static $completion_status = "paid"; |
||
159 | |||
160 | /** |
||
161 | * The status which an order has been marked pending |
||
162 | * (meaning we are awaiting payment). |
||
163 | * |
||
164 | * @var string |
||
165 | * @config |
||
166 | */ |
||
167 | private static $pending_status = "pending"; |
||
168 | |||
169 | /** |
||
170 | * The status which an order is considered "paid" (meaning |
||
171 | * ready for processing, dispatch, etc). |
||
172 | * |
||
173 | * @var string |
||
174 | * @config |
||
175 | */ |
||
176 | private static $paid_status = "paid"; |
||
177 | |||
178 | /** |
||
179 | * The status which an order is considered "part paid" (meaning |
||
180 | * partially paid, possibly deposit paid). |
||
181 | * |
||
182 | * @var string |
||
183 | * @config |
||
184 | */ |
||
185 | private static $part_paid_status = "part-paid"; |
||
186 | |||
187 | /** |
||
188 | * The status which an order has not been completed (meaning |
||
189 | * it is not ready for processing, dispatch, etc). |
||
190 | * |
||
191 | * @var string |
||
192 | * @config |
||
193 | */ |
||
194 | private static $incomplete_status = "incomplete"; |
||
195 | |||
196 | /** |
||
197 | * The status which an order has been canceled. |
||
198 | * |
||
199 | * @var string |
||
200 | * @config |
||
201 | */ |
||
202 | private static $canceled_status = "canceled"; |
||
203 | |||
204 | /** |
||
205 | * The status which an order has been refunded. |
||
206 | * |
||
207 | * @var string |
||
208 | * @config |
||
209 | */ |
||
210 | private static $refunded_status = "refunded"; |
||
211 | |||
212 | /** |
||
213 | * The status which an order has been dispatched |
||
214 | * (sent to customer). |
||
215 | * |
||
216 | * @var string |
||
217 | * @config |
||
218 | */ |
||
219 | private static $dispatched_status = "dispatched"; |
||
220 | |||
221 | /** |
||
222 | * The status which an order has been marked collected |
||
223 | * (meaning goods collected from store). |
||
224 | * |
||
225 | * @var string |
||
226 | * @config |
||
227 | */ |
||
228 | private static $collected_status = "collected"; |
||
229 | |||
230 | private static $db = [ |
||
231 | 'Status' => "Varchar" |
||
232 | ]; |
||
233 | |||
234 | private static $casting = [ |
||
235 | 'TranslatedStatus' => 'Varchar' |
||
236 | ]; |
||
237 | |||
238 | private static $summary_fields = [ |
||
239 | "Status" => "Status" |
||
240 | ]; |
||
241 | |||
242 | public function populateDefaults() |
||
243 | { |
||
244 | parent::populateDefaults(); |
||
245 | $this->Status = $this->config()->get("default_status"); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Generate a translated variant of the current status |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getTranslatedStatus() |
||
254 | { |
||
255 | return _t('Orders.' . $this->Status, $this->Status); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Generate a link to view the associated front end |
||
260 | * display for this order |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function DisplayLink() |
||
265 | { |
||
266 | return Controller::join_links( |
||
267 | DisplayController::create()->AbsoluteLink("invoice"), |
||
268 | $this->ID, |
||
269 | $this->AccessKey |
||
270 | ); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Generate a link to view the associated front end |
||
275 | * display for this order |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public function PDFLink() |
||
280 | { |
||
281 | return Controller::join_links( |
||
282 | DisplayController::create()->AbsoluteLink("invoicepdf"), |
||
283 | $this->ID, |
||
284 | $this->AccessKey |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Scaffold admin form feilds |
||
290 | * |
||
291 | * @return \SilverStripe\Forms\FieldList |
||
292 | */ |
||
293 | public function getCMSFields() |
||
294 | { |
||
295 | $this->beforeUpdateCMSFields(function ($fields) { |
||
296 | // Go through a convoluted process to find out field group |
||
297 | // as the default fieldlist doesn't seem to register it! |
||
298 | $main = $fields->findOrMakeTab("Root.Main"); |
||
299 | $sidebar = null; |
||
300 | |||
301 | $enddate_field = $fields->dataFieldByName("EndDate"); |
||
302 | $enddate_field->setTitle(_t("OrdersAdmin.Due", "Due")); |
||
303 | |||
304 | foreach ($main->getChildren() as $field) { |
||
305 | if ($field->getName() == "OrdersSidebar") { |
||
306 | $sidebar = $field; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | if ($sidebar) { |
||
311 | $sidebar->setTitle(_t( |
||
312 | "OrdersAdmin.InvoiceDetails", |
||
313 | "Invoice Details" |
||
314 | )); |
||
315 | $sidebar->insertBefore( |
||
316 | "Action", |
||
317 | DropdownField::create( |
||
318 | 'Status', |
||
319 | null, |
||
320 | $this->config()->get("statuses") |
||
321 | ) |
||
322 | ); |
||
323 | } |
||
324 | }); |
||
325 | |||
326 | return parent::getCMSFields(); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Has this order been paid for? We determine this |
||
331 | * by checking one of the pre-defined "paid_statuses" |
||
332 | * in the config variable: |
||
333 | * |
||
334 | * # Order.paid_statuses |
||
335 | * |
||
336 | * @return boolean |
||
337 | */ |
||
338 | public function isPaid() |
||
339 | { |
||
340 | $statuses = $this->config()->get("paid_statuses"); |
||
341 | |||
342 | if (!is_array($statuses)) { |
||
343 | return $this->Status == $statuses; |
||
344 | } else { |
||
345 | return in_array($this->Status, $statuses); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Mark this order as "complete" which generally is intended |
||
351 | * to mean "paid for, ready for processing". |
||
352 | * |
||
353 | * @return Order |
||
354 | */ |
||
355 | public function markComplete() |
||
356 | { |
||
357 | $this->Status = $this->config()->get("completion_status"); |
||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Mark this order as "paid" |
||
363 | * |
||
364 | * @param string $reference the unique reference from the gateway |
||
365 | * @return Order |
||
366 | */ |
||
367 | public function markPaid() |
||
368 | { |
||
369 | $this->Status = $this->config()->get("paid_status"); |
||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Mark this order as "part paid". |
||
375 | * |
||
376 | * @return Order |
||
377 | */ |
||
378 | public function markPartPaid() |
||
379 | { |
||
380 | $this->Status = $this->config()->get("part_paid_status"); |
||
381 | return $this; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Mark this order as "pending" (awaiting payment to clear/reconcile). |
||
386 | * |
||
387 | * @return Order |
||
388 | */ |
||
389 | public function markPending() |
||
390 | { |
||
391 | $this->Status = $this->config()->get("pending_status"); |
||
392 | return $this; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Mark this order as "canceled". |
||
397 | * |
||
398 | * @return Order |
||
399 | */ |
||
400 | public function markCanceled() |
||
401 | { |
||
402 | $this->Status = $this->config()->get("canceled_status"); |
||
403 | return $this; |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Mark this order as "refunded". |
||
408 | * |
||
409 | * @return Order |
||
410 | */ |
||
411 | public function markRefunded() |
||
412 | { |
||
413 | $this->Status = $this->config()->get("refunded_status"); |
||
414 | return $this; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Mark this order as "dispatched". |
||
419 | * |
||
420 | * @return Order |
||
421 | */ |
||
422 | public function markDispatched() |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Mark this order as "collected". |
||
430 | * |
||
431 | * @return Order |
||
432 | */ |
||
433 | public function markCollected() |
||
434 | { |
||
435 | $this->Status = $this->config()->get("collected_status"); |
||
436 | return $this; |
||
437 | } |
||
438 | |||
439 | protected function validOrderNumber() |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Retrieve an order prefix from siteconfig |
||
450 | * for an Estimate |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | protected function get_prefix() |
||
455 | { |
||
456 | $config = SiteConfig::current_site_config(); |
||
457 | return $config->InvoiceNumberPrefix; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * API Callback after this object is written to the DB |
||
462 | * |
||
463 | */ |
||
464 | public function onBeforeWrite() |
||
465 | { |
||
466 | parent::onBeforeWrite(); |
||
467 | |||
468 | if (!$this->Status) { |
||
469 | $this->Status = $this->config()->get("default_status"); |
||
470 | } |
||
471 | |||
472 | if (!$this->Action) { |
||
473 | $this->Action = $this->config()->get("default_action"); |
||
474 | } |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * API Callback after this object is written to the DB |
||
479 | * |
||
480 | */ |
||
481 | public function onAfterWrite() |
||
482 | { |
||
483 | parent::onAfterWrite(); |
||
484 | |||
485 | // Deal with sending the status emails |
||
486 | if ($this->isChanged('Status')) { |
||
487 | $notifications = Notification::get() |
||
488 | ->filter("Status", $this->Status); |
||
489 | |||
490 | // Loop through available notifications and send |
||
491 | foreach ($notifications as $notification) { |
||
492 | $notification->sendNotification($this); |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 | |||
497 | public function providePermissions() |
||
498 | { |
||
499 | return [ |
||
500 | "ORDERS_VIEW_INVOICES" => [ |
||
501 | 'name' => 'View any invoice', |
||
502 | 'help' => 'Allow user to view any invoice', |
||
503 | 'category' => 'Orders', |
||
504 | 'sort' => 99 |
||
505 | ], |
||
506 | "ORDERS_CREATE_INVOICES" => [ |
||
507 | 'name' => 'Create invoices', |
||
508 | 'help' => 'Allow user to create new invoices', |
||
509 | 'category' => 'Orders', |
||
510 | 'sort' => 98 |
||
511 | ], |
||
512 | "ORDERS_STATUS_INVOICES" => [ |
||
513 | 'name' => 'Change status of any invoice', |
||
514 | 'help' => 'Allow user to change the status of any invoice', |
||
515 | 'category' => 'Orders', |
||
516 | 'sort' => 97 |
||
517 | ], |
||
518 | "ORDERS_EDIT_INVOICES" => [ |
||
519 | 'name' => 'Edit any invoice', |
||
520 | 'help' => 'Allow user to edit any invoice', |
||
521 | 'category' => 'Orders', |
||
522 | 'sort' => 96 |
||
523 | ], |
||
524 | "ORDERS_DELETE_INVOICES" => [ |
||
525 | 'name' => 'Delete any invoice', |
||
526 | 'help' => 'Allow user to delete any invoice', |
||
527 | 'category' => 'Orders', |
||
528 | 'sort' => 95 |
||
529 | ] |
||
530 | ]; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Only order creators or users with VIEW admin rights can view |
||
535 | * |
||
536 | * @return Boolean |
||
537 | */ |
||
538 | public function canView($member = null) |
||
539 | { |
||
540 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
541 | |||
542 | if ($extended !== null) { |
||
543 | return $extended; |
||
544 | } |
||
545 | |||
546 | if (!$member) { |
||
547 | $member = Member::currentUser(); |
||
548 | } |
||
549 | |||
550 | if ($member && Permission::checkMember($member->ID, ["ADMIN", "ORDERS_VIEW_INVOICES"])) { |
||
551 | return true; |
||
552 | } |
||
553 | |||
554 | return false; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Anyone can create orders, even guest users |
||
559 | * |
||
560 | * @return Boolean |
||
561 | */ |
||
562 | public function canCreate($member = null, $context = []) |
||
563 | { |
||
564 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
565 | |||
566 | if ($extended !== null) { |
||
567 | return $extended; |
||
568 | } |
||
569 | |||
570 | if (!$member) { |
||
571 | $member = Member::currentUser(); |
||
572 | } |
||
573 | |||
574 | if ($member && Permission::checkMember($member->ID, ["ADMIN", "ORDERS_CREATE_INVOICES"])) { |
||
575 | return true; |
||
576 | } |
||
577 | |||
578 | return false; |
||
579 | } |
||
580 | |||
581 | /** |
||
582 | * Only users with EDIT admin rights can view an order |
||
583 | * |
||
584 | * @return Boolean |
||
585 | */ |
||
586 | public function canEdit($member = null) |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Only users with EDIT admin rights can view an order |
||
611 | * |
||
612 | * @return Boolean |
||
613 | */ |
||
614 | public function canChangeStatus($member = null) |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * No one should be able to delete an order once it has been created |
||
635 | * |
||
636 | * @return Boolean |
||
637 | */ |
||
638 | public function canDelete($member = null) |
||
639 | { |
||
640 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
641 | |||
642 | if ($extended !== null) { |
||
655 | } |
||
656 | } |
||
657 |