Total Complexity | 54 |
Total Lines | 625 |
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 is being processed |
||
198 | * |
||
199 | * @var string |
||
200 | * @config |
||
201 | */ |
||
202 | private static $processing_status = "processing"; |
||
203 | |||
204 | /** |
||
205 | * The status which an order has been canceled. |
||
206 | * |
||
207 | * @var string |
||
208 | * @config |
||
209 | */ |
||
210 | private static $canceled_status = "canceled"; |
||
211 | |||
212 | /** |
||
213 | * The status which an order has been refunded. |
||
214 | * |
||
215 | * @var string |
||
216 | * @config |
||
217 | */ |
||
218 | private static $refunded_status = "refunded"; |
||
219 | |||
220 | /** |
||
221 | * The status which an order has been dispatched |
||
222 | * (sent to customer). |
||
223 | * |
||
224 | * @var string |
||
225 | * @config |
||
226 | */ |
||
227 | private static $dispatched_status = "dispatched"; |
||
228 | |||
229 | /** |
||
230 | * The status which an order has been marked collected |
||
231 | * (meaning goods collected from store). |
||
232 | * |
||
233 | * @var string |
||
234 | * @config |
||
235 | */ |
||
236 | private static $collected_status = "collected"; |
||
237 | |||
238 | private static $db = [ |
||
239 | 'Status' => "Varchar" |
||
240 | ]; |
||
241 | |||
242 | private static $casting = [ |
||
243 | 'TranslatedStatus' => 'Varchar' |
||
244 | ]; |
||
245 | |||
246 | private static $summary_fields = [ |
||
247 | "Status" => "Status" |
||
248 | ]; |
||
249 | |||
250 | public function populateDefaults() |
||
251 | { |
||
252 | parent::populateDefaults(); |
||
253 | $this->Status = $this->config()->get("default_status"); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Generate a translated variant of the current status |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getTranslatedStatus() |
||
262 | { |
||
263 | return _t('Orders.' . $this->Status, $this->Status); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Generate a link to view the associated front end |
||
268 | * display for this order |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | public function DisplayLink() |
||
273 | { |
||
274 | return Controller::join_links( |
||
275 | DisplayController::create()->AbsoluteLink("invoice"), |
||
276 | $this->ID, |
||
277 | $this->AccessKey |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Generate a link to view the associated front end |
||
283 | * display for this order |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | public function PDFLink() |
||
288 | { |
||
289 | return Controller::join_links( |
||
290 | DisplayController::create()->AbsoluteLink("invoicepdf"), |
||
291 | $this->ID, |
||
292 | $this->AccessKey |
||
293 | ); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Scaffold admin form feilds |
||
298 | * |
||
299 | * @return \SilverStripe\Forms\FieldList |
||
300 | */ |
||
301 | public function getCMSFields() |
||
302 | { |
||
303 | $this->beforeUpdateCMSFields(function ($fields) { |
||
304 | // Go through a convoluted process to find out field group |
||
305 | // as the default fieldlist doesn't seem to register it! |
||
306 | $main = $fields->findOrMakeTab("Root.Main"); |
||
307 | $sidebar = null; |
||
308 | |||
309 | $enddate_field = $fields->dataFieldByName("EndDate"); |
||
310 | $enddate_field->setTitle(_t("OrdersAdmin.Due", "Due")); |
||
311 | |||
312 | foreach ($main->getChildren() as $field) { |
||
313 | if ($field->getName() == "OrdersSidebar") { |
||
314 | $sidebar = $field; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | if ($sidebar) { |
||
319 | $sidebar->setTitle(_t( |
||
320 | "OrdersAdmin.InvoiceDetails", |
||
321 | "Invoice Details" |
||
322 | )); |
||
323 | $sidebar->insertBefore( |
||
324 | "Action", |
||
325 | DropdownField::create( |
||
326 | 'Status', |
||
327 | null, |
||
328 | $this->config()->get("statuses") |
||
329 | ) |
||
330 | ); |
||
331 | } |
||
332 | }); |
||
333 | |||
334 | return parent::getCMSFields(); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Has this order been paid for? We determine this |
||
339 | * by checking one of the pre-defined "paid_statuses" |
||
340 | * in the config variable: |
||
341 | * |
||
342 | * # Order.paid_statuses |
||
343 | * |
||
344 | * @return boolean |
||
345 | */ |
||
346 | public function isPaid() |
||
347 | { |
||
348 | $statuses = $this->config()->get("paid_statuses"); |
||
349 | |||
350 | if (!is_array($statuses)) { |
||
351 | return $this->Status == $statuses; |
||
352 | } else { |
||
353 | return in_array($this->Status, $statuses); |
||
354 | } |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Mark this order as "complete" which generally is intended |
||
359 | * to mean "paid for, ready for processing". |
||
360 | * |
||
361 | * @return Order |
||
362 | */ |
||
363 | public function markComplete() |
||
364 | { |
||
365 | $this->Status = $this->config()->get("completion_status"); |
||
366 | return $this; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Mark this order as "paid" |
||
371 | * |
||
372 | * @param string $reference the unique reference from the gateway |
||
373 | * @return Order |
||
374 | */ |
||
375 | public function markPaid() |
||
376 | { |
||
377 | $this->Status = $this->config()->get("paid_status"); |
||
378 | return $this; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Mark this order as "part paid". |
||
383 | * |
||
384 | * @return Order |
||
385 | */ |
||
386 | public function markPartPaid() |
||
387 | { |
||
388 | $this->Status = $this->config()->get("part_paid_status"); |
||
389 | return $this; |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Mark this order as "pending" (awaiting payment to clear/reconcile). |
||
394 | * |
||
395 | * @return Order |
||
396 | */ |
||
397 | public function markPending() |
||
398 | { |
||
399 | $this->Status = $this->config()->get("pending_status"); |
||
400 | return $this; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Mark this order as "pending" (awaiting payment to clear/reconcile). |
||
405 | * |
||
406 | * @return Order |
||
407 | */ |
||
408 | public function markProcessing() |
||
409 | { |
||
410 | $this->Status = $this->config()->get("processing_status"); |
||
411 | return $this; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Mark this order as "canceled". |
||
416 | * |
||
417 | * @return Order |
||
418 | */ |
||
419 | public function markCanceled() |
||
420 | { |
||
421 | $this->Status = $this->config()->get("canceled_status"); |
||
422 | return $this; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Mark this order as "refunded". |
||
427 | * |
||
428 | * @return Order |
||
429 | */ |
||
430 | public function markRefunded() |
||
431 | { |
||
432 | $this->Status = $this->config()->get("refunded_status"); |
||
433 | return $this; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Mark this order as "dispatched". |
||
438 | * |
||
439 | * @return Order |
||
440 | */ |
||
441 | public function markDispatched() |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Mark this order as "collected". |
||
449 | * |
||
450 | * @return Order |
||
451 | */ |
||
452 | public function markCollected() |
||
453 | { |
||
454 | $this->Status = $this->config()->get("collected_status"); |
||
455 | return $this; |
||
456 | } |
||
457 | |||
458 | protected function validOrderNumber() |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Retrieve an order prefix from siteconfig |
||
469 | * for an Estimate |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | protected function get_prefix() |
||
474 | { |
||
475 | $config = SiteConfig::current_site_config(); |
||
476 | return $config->InvoiceNumberPrefix; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * API Callback after this object is written to the DB |
||
481 | * |
||
482 | */ |
||
483 | public function onBeforeWrite() |
||
484 | { |
||
485 | parent::onBeforeWrite(); |
||
486 | |||
487 | if (!$this->Status) { |
||
488 | $this->Status = $this->config()->get("default_status"); |
||
489 | } |
||
490 | |||
491 | if (!$this->Action) { |
||
492 | $this->Action = $this->config()->get("default_action"); |
||
493 | } |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * API Callback after this object is written to the DB |
||
498 | * |
||
499 | */ |
||
500 | public function onAfterWrite() |
||
501 | { |
||
502 | parent::onAfterWrite(); |
||
503 | |||
504 | // Deal with sending the status emails |
||
505 | if ($this->isChanged('Status')) { |
||
506 | $notifications = Notification::get() |
||
507 | ->filter("Status", $this->Status); |
||
508 | |||
509 | // Loop through available notifications and send |
||
510 | foreach ($notifications as $notification) { |
||
511 | $notification->sendNotification($this); |
||
512 | } |
||
513 | } |
||
514 | } |
||
515 | |||
516 | public function providePermissions() |
||
517 | { |
||
518 | return [ |
||
519 | "ORDERS_VIEW_INVOICES" => [ |
||
520 | 'name' => 'View any invoice', |
||
521 | 'help' => 'Allow user to view any invoice', |
||
522 | 'category' => 'Orders', |
||
523 | 'sort' => 99 |
||
524 | ], |
||
525 | "ORDERS_CREATE_INVOICES" => [ |
||
526 | 'name' => 'Create invoices', |
||
527 | 'help' => 'Allow user to create new invoices', |
||
528 | 'category' => 'Orders', |
||
529 | 'sort' => 98 |
||
530 | ], |
||
531 | "ORDERS_STATUS_INVOICES" => [ |
||
532 | 'name' => 'Change status of any invoice', |
||
533 | 'help' => 'Allow user to change the status of any invoice', |
||
534 | 'category' => 'Orders', |
||
535 | 'sort' => 97 |
||
536 | ], |
||
537 | "ORDERS_EDIT_INVOICES" => [ |
||
538 | 'name' => 'Edit any invoice', |
||
539 | 'help' => 'Allow user to edit any invoice', |
||
540 | 'category' => 'Orders', |
||
541 | 'sort' => 96 |
||
542 | ], |
||
543 | "ORDERS_DELETE_INVOICES" => [ |
||
544 | 'name' => 'Delete any invoice', |
||
545 | 'help' => 'Allow user to delete any invoice', |
||
546 | 'category' => 'Orders', |
||
547 | 'sort' => 95 |
||
548 | ] |
||
549 | ]; |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Only order creators or users with VIEW admin rights can view |
||
554 | * |
||
555 | * @return Boolean |
||
556 | */ |
||
557 | public function canView($member = null) |
||
558 | { |
||
559 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
560 | |||
561 | if ($extended !== null) { |
||
562 | return $extended; |
||
563 | } |
||
564 | |||
565 | if (!$member) { |
||
566 | $member = Member::currentUser(); |
||
567 | } |
||
568 | |||
569 | if ($member && Permission::checkMember($member->ID, ["ADMIN", "ORDERS_VIEW_INVOICES"])) { |
||
570 | return true; |
||
571 | } |
||
572 | |||
573 | return false; |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Anyone can create orders, even guest users |
||
578 | * |
||
579 | * @return Boolean |
||
580 | */ |
||
581 | public function canCreate($member = null, $context = []) |
||
582 | { |
||
583 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
584 | |||
585 | if ($extended !== null) { |
||
586 | return $extended; |
||
587 | } |
||
588 | |||
589 | if (!$member) { |
||
590 | $member = Member::currentUser(); |
||
591 | } |
||
592 | |||
593 | if ($member && Permission::checkMember($member->ID, ["ADMIN", "ORDERS_CREATE_INVOICES"])) { |
||
594 | return true; |
||
595 | } |
||
596 | |||
597 | return false; |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Only users with EDIT admin rights can view an order |
||
602 | * |
||
603 | * @return Boolean |
||
604 | */ |
||
605 | public function canEdit($member = null) |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Only users with EDIT admin rights can view an order |
||
630 | * |
||
631 | * @return Boolean |
||
632 | */ |
||
633 | public function canChangeStatus($member = null) |
||
650 | } |
||
651 | |||
652 | /** |
||
653 | * No one should be able to delete an order once it has been created |
||
654 | * |
||
655 | * @return Boolean |
||
656 | */ |
||
657 | public function canDelete($member = null) |
||
658 | { |
||
659 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
660 | |||
661 | if ($extended !== null) { |
||
662 | return $extended; |
||
674 | } |
||
675 | } |
||
676 |