| Total Complexity | 129 |
| Total Lines | 1141 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Estimate 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 Estimate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Estimate extends DataObject implements PermissionProvider |
||
| 43 | { |
||
| 44 | private static $table_name = 'Estimate'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The amount of days that by default that this estimate |
||
| 48 | * will end (cease to be valid). |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | private static $default_end = 30; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Actions on an order are to determine what will happen on |
||
| 56 | * completion (the defaults are post or collect). |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | * @config |
||
| 60 | */ |
||
| 61 | private static $actions = [ |
||
| 62 | "post" => "Post", |
||
| 63 | "collect" => "Collect" |
||
| 64 | ]; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set the default action on our order. If we were using this module |
||
| 68 | * for a more POS type solution, we would probably change this to |
||
| 69 | * collect. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | * @config |
||
| 73 | */ |
||
| 74 | private static $default_action = "post"; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * What is the action string for a order to be posted |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | * @config |
||
| 81 | */ |
||
| 82 | private static $post_action = "post"; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * What is the action string for a order to be collected |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | * @config |
||
| 89 | */ |
||
| 90 | private static $collect_action = "collect"; |
||
| 91 | |||
| 92 | private static $db = [ |
||
| 93 | 'OrderNumber' => 'Varchar', |
||
| 94 | 'StartDate' => 'Date', |
||
| 95 | 'EndDate' => 'Date', |
||
| 96 | "Action" => "Varchar", |
||
| 97 | |||
| 98 | // Personal Details |
||
| 99 | 'Company' => 'Varchar', |
||
| 100 | 'FirstName' => 'Varchar', |
||
| 101 | 'Surname' => 'Varchar', |
||
| 102 | 'Email' => 'Varchar', |
||
| 103 | 'PhoneNumber' => 'Varchar', |
||
| 104 | |||
| 105 | // Billing Address |
||
| 106 | 'Address1' => 'Varchar', |
||
| 107 | 'Address2' => 'Varchar', |
||
| 108 | 'City' => 'Varchar', |
||
| 109 | 'PostCode' => 'Varchar', |
||
| 110 | 'Country' => 'Varchar', |
||
| 111 | |||
| 112 | // Delivery Details |
||
| 113 | 'DeliveryCompany' => 'Varchar', |
||
| 114 | 'DeliveryFirstName' => 'Varchar', |
||
| 115 | 'DeliverySurname' => 'Varchar', |
||
| 116 | 'DeliveryAddress1' => 'Varchar', |
||
| 117 | 'DeliveryAddress2' => 'Varchar', |
||
| 118 | 'DeliveryCity' => 'Varchar', |
||
| 119 | 'DeliveryPostCode' => 'Varchar', |
||
| 120 | 'DeliveryCountry' => 'Varchar', |
||
| 121 | |||
| 122 | // Discount Provided |
||
| 123 | "DiscountType" => "Varchar", |
||
| 124 | "DiscountAmount" => "Currency", |
||
| 125 | |||
| 126 | // Postage |
||
| 127 | "PostageType" => "Varchar", |
||
| 128 | "PostageCost" => "Currency", |
||
| 129 | "PostageTax" => "Currency", |
||
| 130 | |||
| 131 | // Access key (for viewing via non logged in users) |
||
| 132 | "AccessKey" => "Varchar(40)" |
||
| 133 | ]; |
||
| 134 | |||
| 135 | private static $has_one = [ |
||
| 136 | "Discount" => Discount::class, |
||
| 137 | "Postage" => PostageArea::class, |
||
| 138 | "Customer" => Contact::class |
||
| 139 | ]; |
||
| 140 | |||
| 141 | private static $has_many = [ |
||
| 142 | 'Items' => LineItem::class |
||
| 143 | ]; |
||
| 144 | |||
| 145 | private static $casting = [ |
||
| 146 | "PersonalDetails" => "Text", |
||
| 147 | 'BillingAddress' => 'Text', |
||
| 148 | 'CountryFull' => 'Varchar', |
||
| 149 | 'DeliveryAddress' => 'Text', |
||
| 150 | 'DeliveryCountryFull'=> 'Varchar', |
||
| 151 | 'SubTotal' => 'Currency', |
||
| 152 | 'TaxTotal' => 'Currency', |
||
| 153 | 'Total' => 'Currency', |
||
| 154 | 'TotalItems' => 'Int', |
||
| 155 | 'TotalWeight' => 'Decimal', |
||
| 156 | 'ItemSummary' => 'Text', |
||
| 157 | 'ItemSummaryHTML' => 'HTMLText', |
||
| 158 | 'TranslatedStatus' => 'Varchar', |
||
| 159 | 'PostageDetails' => "Varchar", |
||
| 160 | 'DiscountDetails' => "Varchar" |
||
| 161 | ]; |
||
| 162 | |||
| 163 | private static $defaults = [ |
||
| 164 | 'DiscountAmount' => 0, |
||
| 165 | "PostageCost" => 0 |
||
| 166 | ]; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Fields to show in summary views |
||
| 170 | * |
||
| 171 | * @var array |
||
| 172 | * @config |
||
| 173 | */ |
||
| 174 | private static $summary_fields = [ |
||
| 175 | 'OrderNumber' => '#', |
||
| 176 | 'StartDate' => 'Date', |
||
| 177 | 'EndDate' => 'Expires', |
||
| 178 | 'Company' => 'Company', |
||
| 179 | 'FirstName' => 'First Name', |
||
| 180 | 'Surname' => 'Surname', |
||
| 181 | 'Email' => 'Email', |
||
| 182 | 'PostCode' => 'Post Code', |
||
| 183 | "Total" => "Total", |
||
| 184 | "LastEdited" => "Last Edited" |
||
| 185 | ]; |
||
| 186 | |||
| 187 | private static $extensions = [ |
||
| 188 | Versioned::class . '.versioned', |
||
| 189 | ]; |
||
| 190 | |||
| 191 | private static $versioning = [ |
||
| 192 | "History" |
||
| 193 | ]; |
||
| 194 | |||
| 195 | private static $default_sort = [ |
||
| 196 | "StartDate" => "DESC" |
||
| 197 | ]; |
||
| 198 | |||
| 199 | public function populateDefaults() |
||
| 200 | { |
||
| 201 | parent::populateDefaults(); |
||
| 202 | $this->Action = $this->config()->get("default_action"); |
||
|
|
|||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Generate a link to view the associated front end |
||
| 207 | * display for this order |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function DisplayLink() |
||
| 212 | { |
||
| 213 | return Controller::join_links( |
||
| 214 | DisplayController::create()->AbsoluteLink("estimate"), |
||
| 215 | $this->ID, |
||
| 216 | $this->AccessKey |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Generate a link to view the associated front end |
||
| 222 | * display for this order |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function PDFLink() |
||
| 227 | { |
||
| 228 | return Controller::join_links( |
||
| 229 | DisplayController::create()->AbsoluteLink("estimatepdf"), |
||
| 230 | $this->ID, |
||
| 231 | $this->AccessKey |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Generate a string of the customer's personal details |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getPersonalDetails() |
||
| 241 | { |
||
| 242 | $return = []; |
||
| 243 | |||
| 244 | if ($this->Company) { |
||
| 245 | $return[] = $this->Company; |
||
| 246 | } |
||
| 247 | |||
| 248 | if ($this->FirstName) { |
||
| 249 | $return[] = $this->FirstName; |
||
| 250 | } |
||
| 251 | |||
| 252 | if ($this->Surname) { |
||
| 253 | $return[] = $this->Surname; |
||
| 254 | } |
||
| 255 | |||
| 256 | if ($this->Email) { |
||
| 257 | $return[] = $this->Email; |
||
| 258 | } |
||
| 259 | |||
| 260 | if ($this->PhoneNumber) { |
||
| 261 | $return[] = $this->PhoneNumber; |
||
| 262 | } |
||
| 263 | |||
| 264 | return implode(",\n", $return); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the complete billing address for this order |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getBillingAddress() |
||
| 273 | { |
||
| 274 | $address = ($this->Address1) ? $this->Address1 . ",\n" : ''; |
||
| 275 | $address .= ($this->Address2) ? $this->Address2 . ",\n" : ''; |
||
| 276 | $address .= ($this->City) ? $this->City . ",\n" : ''; |
||
| 277 | $address .= ($this->PostCode) ? $this->PostCode . ",\n" : ''; |
||
| 278 | $address .= ($this->Country) ? $this->Country : ''; |
||
| 279 | |||
| 280 | return $address; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the rendered name of the billing country, based on the local |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getCountryFull() |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the complete delivery address for this order |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function getDeliveryAddress() |
||
| 300 | { |
||
| 301 | $address = ($this->DeliveryAddress1) ? $this->DeliveryAddress1 . ",\n" : ''; |
||
| 302 | $address .= ($this->DeliveryAddress2) ? $this->DeliveryAddress2 . ",\n" : ''; |
||
| 303 | $address .= ($this->DeliveryCity) ? $this->DeliveryCity . ",\n" : ''; |
||
| 304 | $address .= ($this->DeliveryPostCode) ? $this->DeliveryPostCode . ",\n" : ''; |
||
| 305 | $address .= ($this->DeliveryCountry) ? $this->DeliveryCountry : ''; |
||
| 306 | |||
| 307 | return $address; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get the rendered name of the delivery country, based on the local |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getDeliveryCountryFull() |
||
| 316 | { |
||
| 317 | $list = i18n::getData()->getCountries(); |
||
| 318 | return (array_key_exists($this->DeliveryCountry, $list)) ? $list[$this->DeliveryCountry] : $this->DeliveryCountry; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Generate a string outlining the details of selected |
||
| 323 | * postage |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getPostageDetails() |
||
| 328 | { |
||
| 329 | if ($this->PostageType) { |
||
| 330 | return $this->PostageType . " (" . $this->dbObject("PostageCost")->Nice() . ")"; |
||
| 331 | } else { |
||
| 332 | return ""; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Generate a string outlining the details of selected |
||
| 338 | * discount |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getDiscountDetails() |
||
| 343 | { |
||
| 344 | if ($this->DiscountType) { |
||
| 345 | return $this->DiscountType . " (" . $this->dbObject("DiscountAmount")->Nice() . ")"; |
||
| 346 | } else { |
||
| 347 | return ""; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Find the total quantity of items in the shopping cart |
||
| 353 | * |
||
| 354 | * @return Int |
||
| 355 | */ |
||
| 356 | public function getTotalItems() |
||
| 357 | { |
||
| 358 | $total = 0; |
||
| 359 | |||
| 360 | foreach ($this->Items() as $item) { |
||
| 361 | $total += ($item->Quantity) ? $item->Quantity : 1; |
||
| 362 | } |
||
| 363 | |||
| 364 | $this->extend("updateTotalItems", $total); |
||
| 365 | |||
| 366 | return $total; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Find the total weight of all items in the shopping cart |
||
| 371 | * |
||
| 372 | * @return float |
||
| 373 | */ |
||
| 374 | public function getTotalWeight() |
||
| 375 | { |
||
| 376 | $total = 0; |
||
| 377 | |||
| 378 | foreach ($this->Items() as $item) { |
||
| 379 | if ($item->Weight && $item->Quantity) { |
||
| 380 | $total = $total + ($item->Weight * $item->Quantity); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | $this->extend("updateTotalWeight", $total); |
||
| 385 | |||
| 386 | return $total; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Total values of items in this order (without any tax) |
||
| 391 | * |
||
| 392 | * @return float |
||
| 393 | */ |
||
| 394 | public function getSubTotal() |
||
| 395 | { |
||
| 396 | $total = 0; |
||
| 397 | |||
| 398 | // Calculate total from items in the list |
||
| 399 | foreach ($this->Items() as $item) { |
||
| 400 | $total += $item->SubTotal; |
||
| 401 | } |
||
| 402 | |||
| 403 | $this->extend("updateSubTotal", $total); |
||
| 404 | |||
| 405 | return $total; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Total values of items in this order |
||
| 410 | * |
||
| 411 | * @return float |
||
| 412 | */ |
||
| 413 | public function getTaxTotal() |
||
| 414 | { |
||
| 415 | $total = 0; |
||
| 416 | $items = $this->Items(); |
||
| 417 | |||
| 418 | // Calculate total from items in the list |
||
| 419 | foreach ($items as $item) { |
||
| 420 | // If a discount applied, get the tax based on the |
||
| 421 | // discounted amount |
||
| 422 | if ($this->DiscountAmount > 0) { |
||
| 423 | $discount = $this->DiscountAmount / $this->TotalItems; |
||
| 424 | $price = $item->UnitPrice - $discount; |
||
| 425 | $tax = ($price / 100) * $item->TaxRate; |
||
| 426 | } else { |
||
| 427 | $tax = $item->UnitTax; |
||
| 428 | } |
||
| 429 | |||
| 430 | $total += $tax * $item->Quantity; |
||
| 431 | } |
||
| 432 | |||
| 433 | if ($this->PostageTax) { |
||
| 434 | $total += $this->PostageTax; |
||
| 435 | } |
||
| 436 | |||
| 437 | $this->extend("updateTaxTotal", $total); |
||
| 438 | |||
| 439 | $total = MathsHelper::round_up($total, 2); |
||
| 440 | |||
| 441 | return $total; |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get a list of all taxes used and and associated value |
||
| 446 | * |
||
| 447 | * @return ArrayList |
||
| 448 | */ |
||
| 449 | public function getTaxList() |
||
| 450 | { |
||
| 451 | $taxes = ArrayList::create(); |
||
| 452 | |||
| 453 | foreach ($this->Items() as $item) { |
||
| 454 | $existing = null; |
||
| 455 | $rate = $item->Tax(); |
||
| 456 | |||
| 457 | if ($rate->exists()) { |
||
| 458 | $existing = $taxes->find("ID", $rate->ID); |
||
| 459 | } |
||
| 460 | |||
| 461 | if (!$existing) { |
||
| 462 | $currency = DBCurrency::create(); |
||
| 463 | $currency->setValue($item->getTaxTotal()); |
||
| 464 | $taxes->push(ArrayData::create([ |
||
| 465 | "ID" => $rate->ID, |
||
| 466 | "Rate" => $rate, |
||
| 467 | "Total" => $currency |
||
| 468 | ])); |
||
| 469 | } elseif($rate && $existing) { |
||
| 470 | $existing->Total->setValue( |
||
| 471 | $existing->Total->getValue() + $item->getTaxTotal() |
||
| 472 | ); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | return $taxes; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Total of order including postage |
||
| 481 | * |
||
| 482 | * @return float |
||
| 483 | */ |
||
| 484 | public function getTotal() |
||
| 485 | { |
||
| 486 | $total = (($this->SubTotal + $this->Postage) - $this->DiscountAmount) + $this->TaxTotal; |
||
| 487 | |||
| 488 | $this->extend("updateTotal", $total); |
||
| 489 | |||
| 490 | return $total; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Factory method to convert this estimate to an |
||
| 495 | * order. |
||
| 496 | * |
||
| 497 | * This method writes and reloads the object so |
||
| 498 | * we are now working with the new object type |
||
| 499 | * |
||
| 500 | * @return Invoice The currently converted invoice |
||
| 501 | */ |
||
| 502 | public function convertToInvoice() |
||
| 503 | { |
||
| 504 | $this->ClassName = Invoice::class; |
||
| 505 | $this->write(); |
||
| 506 | |||
| 507 | // Get our new Invoice |
||
| 508 | $record = Invoice::get()->byID($this->ID); |
||
| 509 | $record->OrderNumber = null; |
||
| 510 | $record->StartDate = null; |
||
| 511 | $record->EndDate = null; |
||
| 512 | $record->write(); |
||
| 513 | |||
| 514 | return $record; |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Return a list string summarising each item in this order |
||
| 519 | * |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public function getItemSummary() |
||
| 523 | { |
||
| 524 | $return = ''; |
||
| 525 | |||
| 526 | foreach ($this->Items() as $item) { |
||
| 527 | $return .= "{$item->Quantity} x {$item->Title};\n"; |
||
| 528 | } |
||
| 529 | |||
| 530 | $this->extend("updateItemSummary", $return); |
||
| 531 | |||
| 532 | return $return; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Return a list string summarising each item in this order |
||
| 537 | * |
||
| 538 | * @return HTMLText |
||
| 539 | */ |
||
| 540 | public function getItemSummaryHTML() |
||
| 541 | { |
||
| 542 | $html = new HTMLText("ItemSummary"); |
||
| 543 | $html->setValue(nl2br($this->ItemSummary)); |
||
| 544 | |||
| 545 | $this->extend("updateItemSummaryHTML", $html); |
||
| 546 | |||
| 547 | return $html; |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Determine if the current estimate contains delivereable |
||
| 552 | * items. |
||
| 553 | * |
||
| 554 | * @return boolean |
||
| 555 | */ |
||
| 556 | public function isDeliverable() |
||
| 557 | { |
||
| 558 | foreach ($this->Items() as $item) { |
||
| 559 | if ($item->Deliverable) { |
||
| 560 | return true; |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | return false; |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Mark this Estimate for collection? |
||
| 569 | * |
||
| 570 | * @return boolean |
||
| 571 | */ |
||
| 572 | public function MarkForCollection() |
||
| 573 | { |
||
| 574 | $this->Action = $this->config()->collection_action; |
||
| 575 | return $this; |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Mark this Estimate for collection? |
||
| 580 | * |
||
| 581 | * @return boolean |
||
| 582 | */ |
||
| 583 | public function MarkForPost() |
||
| 584 | { |
||
| 585 | $this->Action = $this->config()->post_action; |
||
| 586 | return $this; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Is this Estimate marked for collection? |
||
| 591 | * |
||
| 592 | * @return boolean |
||
| 593 | */ |
||
| 594 | public function isCollection() |
||
| 595 | { |
||
| 596 | return ($this->Action == $this->config()->collection_action); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Is this Estimate marked for postage? |
||
| 601 | * |
||
| 602 | * @return boolean |
||
| 603 | */ |
||
| 604 | public function isPost() |
||
| 605 | { |
||
| 606 | return ($this->Action == $this->config()->post_action); |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Has this order got a discount applied? |
||
| 611 | * |
||
| 612 | * @return boolean |
||
| 613 | */ |
||
| 614 | public function hasDiscount() |
||
| 615 | { |
||
| 616 | return (ceil($this->DiscountAmount)) ? true : false; |
||
| 617 | } |
||
| 618 | |||
| 619 | public function getCMSFields() |
||
| 620 | { |
||
| 621 | |||
| 622 | $this->beforeUpdateCMSFields(function ($fields) { |
||
| 623 | $siteconfig = SiteConfig::current_site_config(); |
||
| 624 | |||
| 625 | $fields->removeByName("StartDate"); |
||
| 626 | $fields->removeByName("EndDate"); |
||
| 627 | $fields->removeByName("OrderNumber"); |
||
| 628 | $fields->removeByName("AccessKey"); |
||
| 629 | $fields->removeByName("Action"); |
||
| 630 | $fields->removeByName("DiscountID"); |
||
| 631 | $fields->removeByName("DiscountType"); |
||
| 632 | $fields->removeByName("DiscountAmount"); |
||
| 633 | $fields->removeByName("PostageType"); |
||
| 634 | $fields->removeByName("PostageCost"); |
||
| 635 | $fields->removeByName("PostageTax"); |
||
| 636 | $fields->removeByName("Items"); |
||
| 637 | |||
| 638 | $fields->addFieldsToTab( |
||
| 639 | "Root.Main", |
||
| 640 | [ |
||
| 641 | // Items field |
||
| 642 | GridField::create( |
||
| 643 | "Items", |
||
| 644 | "", |
||
| 645 | $this->Items(), |
||
| 646 | $config = GridFieldConfig::create() |
||
| 647 | ->addComponents( |
||
| 648 | new GridFieldButtonRow('before'), |
||
| 649 | new GridFieldTitleHeader(), |
||
| 650 | new GridFieldEditableColumns(), |
||
| 651 | new GridFieldEditButton(), |
||
| 652 | new GridFieldDetailForm(), |
||
| 653 | new GridFieldDeleteAction(), |
||
| 654 | new AddLineItem() |
||
| 655 | ) |
||
| 656 | ), |
||
| 657 | |||
| 658 | LiteralField::create( |
||
| 659 | "ItemsDivider", |
||
| 660 | '<div class="field form-group"></div>' |
||
| 661 | ), |
||
| 662 | |||
| 663 | // Discount |
||
| 664 | HeaderField::create( |
||
| 665 | "DiscountDetailsHeader", |
||
| 666 | _t("Orders.DiscountDetails", "Discount") |
||
| 667 | ), |
||
| 668 | DropdownField::create( |
||
| 669 | "DiscountID", |
||
| 670 | $this->fieldLabel("Discount"), |
||
| 671 | $siteconfig->Discounts()->map() |
||
| 672 | )->setEmptyString(_t( |
||
| 673 | "OrdersAdmin.ApplyADiscount", |
||
| 674 | "Apply a discount" |
||
| 675 | )), |
||
| 676 | ReadonlyField::create("DiscountDetails"), |
||
| 677 | |||
| 678 | // Sidebar |
||
| 679 | FieldGroup::create( |
||
| 680 | DateField::create("StartDate", _t("OrdersAdmin.Date", "Date")), |
||
| 681 | DateField::create("EndDate", _t("OrdersAdmin.Expires", "Expires")), |
||
| 682 | DropdownField::create( |
||
| 683 | 'Action', |
||
| 684 | $this->fieldLabel("Action"), |
||
| 685 | $this->config()->get("actions") |
||
| 686 | ), |
||
| 687 | ReadonlyField::create("OrderNumber", "#"), |
||
| 688 | ReadonlyField::create("SubTotalValue",_t("OrdersAdmin.SubTotal", "Sub Total")) |
||
| 689 | ->setValue($this->obj("SubTotal")->Nice()), |
||
| 690 | ReadonlyField::create("DiscountValue",_t("OrdersAdmin.Discount", "Discount")) |
||
| 691 | ->setValue($this->dbObject("DiscountAmount")->Nice()), |
||
| 692 | ReadonlyField::create("PostageValue",_t("OrdersAdmin.Postage", "Postage")) |
||
| 693 | ->setValue($this->dbObject("PostageCost")->Nice()), |
||
| 694 | ReadonlyField::create("TaxValue",_t("OrdersAdmin.Tax", "Tax")) |
||
| 695 | ->setValue($this->obj("TaxTotal")->Nice()), |
||
| 696 | ReadonlyField::create("TotalValue",_t("OrdersAdmin.Total", "Total")) |
||
| 697 | ->setValue($this->obj("Total")->Nice()) |
||
| 698 | )->setName("OrdersSidebar") |
||
| 699 | ->setTitle(_t("Orders.EstimateDetails", "Estimate Details")) |
||
| 700 | ->addExtraClass("order-admin-sidebar") |
||
| 701 | ] |
||
| 702 | ); |
||
| 703 | |||
| 704 | $fields->addFieldsToTab( |
||
| 705 | "Root.Customer", |
||
| 706 | [ |
||
| 707 | DropdownField::create( |
||
| 708 | 'CustomerID', |
||
| 709 | _t('OrdersAdmin.ExistingCustomer', 'Existing Customer'), |
||
| 710 | Contact::get()->map() |
||
| 711 | )->setEmptyString(_t( |
||
| 712 | "OrdersAdmin.SelectACustomer", |
||
| 713 | "Select existing customer" |
||
| 714 | )), |
||
| 715 | TextField::create("Company"), |
||
| 716 | TextField::create("FirstName"), |
||
| 717 | TextField::create("Surname"), |
||
| 718 | TextField::create("Address1"), |
||
| 719 | TextField::create("Address2"), |
||
| 720 | TextField::create("City"), |
||
| 721 | TextField::create("PostCode"), |
||
| 722 | DropdownField::create( |
||
| 723 | 'Country', |
||
| 724 | _t('OrdersAdmin.Country', 'Country'), |
||
| 725 | i18n::getData()->getCountries() |
||
| 726 | )->setEmptyString(""), |
||
| 727 | TextField::create("Email"), |
||
| 728 | TextField::create("PhoneNumber") |
||
| 729 | ] |
||
| 730 | ); |
||
| 731 | |||
| 732 | // Try and calculate valid postage areas |
||
| 733 | $calc = new ShippingCalculator($this->DeliveryPostCode,$this->DeliveryCountry); |
||
| 734 | $calc |
||
| 735 | ->setCost($this->SubTotal) |
||
| 736 | ->setWeight($this->TotalWeight) |
||
| 737 | ->setItems($this->TotalItems) |
||
| 738 | ->setDiscount($this->Discount()); |
||
| 739 | |||
| 740 | $postage_areas = $calc->getPostageAreas(); |
||
| 741 | |||
| 742 | if (!$postage_areas->exists()) { |
||
| 743 | $postage_areas = $siteconfig->PostageAreas(); |
||
| 744 | } |
||
| 745 | |||
| 746 | $fields->addFieldsToTab( |
||
| 747 | "Root.Delivery", |
||
| 748 | [ |
||
| 749 | HeaderField::create( |
||
| 750 | "DeliveryDetailsHeader", |
||
| 751 | _t("Orders.DeliveryDetails", "Delivery Details") |
||
| 752 | ), |
||
| 753 | TextField::create("DeliveryCompany"), |
||
| 754 | TextField::create("DeliveryFirstName"), |
||
| 755 | TextField::create("DeliverySurname"), |
||
| 756 | TextField::create("DeliveryAddress1"), |
||
| 757 | TextField::create("DeliveryAddress2"), |
||
| 758 | TextField::create("DeliveryCity"), |
||
| 759 | TextField::create("DeliveryPostCode"), |
||
| 760 | DropdownField::create( |
||
| 761 | 'DeliveryCountry', |
||
| 762 | _t('OrdersAdmin.Country', 'Country'), |
||
| 763 | i18n::getData()->getCountries() |
||
| 764 | )->setEmptyString(""), |
||
| 765 | |||
| 766 | // Postage |
||
| 767 | HeaderField::create( |
||
| 768 | "PostageDetailsHeader", |
||
| 769 | _t("Orders.PostageDetails", "Postage Details") |
||
| 770 | ), |
||
| 771 | DropdownField::create( |
||
| 772 | "PostageID", |
||
| 773 | $this->fieldLabel("PostageID"), |
||
| 774 | $postage_areas->map() |
||
| 775 | )->setEmptyString(_t( |
||
| 776 | "OrdersAdmin.SelectPostage", |
||
| 777 | "Select Postage" |
||
| 778 | )), |
||
| 779 | ReadonlyField::create("PostageDetails") |
||
| 780 | ] |
||
| 781 | ); |
||
| 782 | |||
| 783 | $root = $fields->findOrMakeTab("Root"); |
||
| 784 | |||
| 785 | if ($root) { |
||
| 786 | $root->addextraClass('orders-root'); |
||
| 787 | } |
||
| 788 | }); |
||
| 789 | |||
| 790 | return parent::getCMSFields(); |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Find the total discount based on discount items added. |
||
| 795 | * |
||
| 796 | * @return float |
||
| 797 | */ |
||
| 798 | protected function get_discount_amount() |
||
| 799 | { |
||
| 800 | $discount = $this->Discount(); |
||
| 801 | $total = 0; |
||
| 802 | $discount_amount = 0; |
||
| 803 | $items = $this->TotalItems; |
||
| 804 | |||
| 805 | foreach ($this->Items() as $item) { |
||
| 806 | if ($item->Price) { |
||
| 807 | $total += ($item->Price * $item->Quantity); |
||
| 808 | } |
||
| 809 | |||
| 810 | if ($item->Price && $discount && $discount->Amount) { |
||
| 811 | if ($discount->Type == "Fixed") { |
||
| 812 | $discount_amount = $discount_amount + ($discount->Amount / $items) * $item->Quantity; |
||
| 813 | } elseif ($discount->Type == "Percentage") { |
||
| 814 | $discount_amount = $discount_amount + (($item->Price / 100) * $discount->Amount) * $item->Quantity; |
||
| 815 | } |
||
| 816 | } |
||
| 817 | } |
||
| 818 | |||
| 819 | if ($discount_amount > $total) { |
||
| 820 | $discount_amount = $total; |
||
| 821 | } |
||
| 822 | |||
| 823 | $this->extend("augmentDiscountCalculation", $discount_amount); |
||
| 824 | |||
| 825 | return $discount_amount; |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Retrieve an order prefix from siteconfig |
||
| 830 | * for an Estimate |
||
| 831 | * |
||
| 832 | * @return string |
||
| 833 | */ |
||
| 834 | protected function get_prefix() |
||
| 835 | { |
||
| 836 | $config = SiteConfig::current_site_config(); |
||
| 837 | return $config->EstimateNumberPrefix; |
||
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Generate a randomised order number for this order. |
||
| 842 | * |
||
| 843 | * The order number is generated based on the current order |
||
| 844 | * ID and is padded to a multiple of 4 and we add "-" every |
||
| 845 | * 4 characters. |
||
| 846 | * |
||
| 847 | * We then add an order prefix (if one is set) or the current |
||
| 848 | * year. |
||
| 849 | * |
||
| 850 | * This keeps a consistent order number structure that allows |
||
| 851 | * for a large number of orders before changing. |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | protected function generate_order_number() |
||
| 856 | { |
||
| 857 | $config = SiteConfig::current_site_config(); |
||
| 858 | $default_length = $config->OrderNumberLength; |
||
| 859 | $length = strlen($this->ID); |
||
| 860 | $i = $length; |
||
| 861 | $prefix = $this->get_prefix(); |
||
| 862 | |||
| 863 | // Determine what the next multiple of 4 is |
||
| 864 | while ($i % 4 != 0) { |
||
| 865 | $i++; |
||
| 866 | } |
||
| 867 | |||
| 868 | $pad_amount = ($i >= $default_length) ? $i : $default_length; |
||
| 869 | $id_base = str_pad($this->ID, $pad_amount, "0", STR_PAD_LEFT); |
||
| 870 | $id_base = wordwrap($id_base, 4, "-", true); |
||
| 871 | |||
| 872 | $current_date = new DateTime(); |
||
| 873 | |||
| 874 | // Work out if an order prefix string has been set |
||
| 875 | if ($prefix) { |
||
| 876 | $order_num = $prefix . '-' . $id_base; |
||
| 877 | } else { |
||
| 878 | $order_num = $current_date->format("Y") . "-" . $id_base; |
||
| 879 | } |
||
| 880 | |||
| 881 | return $order_num; |
||
| 882 | } |
||
| 883 | |||
| 884 | protected function generate_random_string($length = 20) |
||
| 885 | { |
||
| 886 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 887 | $randomString = ''; |
||
| 888 | for ($i = 0; $i < $length; $i++) { |
||
| 889 | $randomString .= $characters[rand(0, strlen($characters) - 1)]; |
||
| 890 | } |
||
| 891 | return $randomString; |
||
| 892 | } |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Check if the currently generated order number |
||
| 896 | * is valid (not duplicated) |
||
| 897 | * |
||
| 898 | * @return boolean |
||
| 899 | */ |
||
| 900 | protected function validOrderNumber() |
||
| 901 | { |
||
| 902 | $existing = Estimate::get() |
||
| 903 | ->filterAny("OrderNumber", $this->OrderNumber) |
||
| 904 | ->first(); |
||
| 905 | |||
| 906 | return !($existing); |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Check if the access key generated for this estimate is |
||
| 911 | * valid (exists on another object) |
||
| 912 | * |
||
| 913 | * @return boolean |
||
| 914 | */ |
||
| 915 | protected function validAccessKey() |
||
| 916 | { |
||
| 917 | $existing = Estimate::get() |
||
| 918 | ->filter("AccessKey", $this->AccessKey) |
||
| 919 | ->first(); |
||
| 920 | |||
| 921 | return !($existing); |
||
| 922 | } |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Create a duplicate of this order/estimate as well as duplicating |
||
| 926 | * associated items |
||
| 927 | * |
||
| 928 | * @param $doWrite Perform a write() operation before returning the object. If this is true, it will create the |
||
| 929 | * duplicate in the database. |
||
| 930 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
| 931 | */ |
||
| 932 | public function duplicate($doWrite = true, $manyMany = 'many_many') |
||
| 933 | { |
||
| 934 | $clone = parent::duplicate($doWrite, $manyMany); |
||
| 935 | |||
| 936 | // Set up items |
||
| 937 | if ($doWrite) { |
||
| 938 | $clone->OrderNumber = ""; |
||
| 939 | $clone->write(); |
||
| 940 | |||
| 941 | foreach ($this->Items() as $item) { |
||
| 942 | $item_class = $item->ClassName; |
||
| 943 | $clone_item = new $item_class($item->toMap(), false, $this->model); |
||
| 944 | $clone_item->ID = 0; |
||
| 945 | $clone_item->ParentID = $clone->ID; |
||
| 946 | $clone_item->write(); |
||
| 947 | } |
||
| 948 | } |
||
| 949 | |||
| 950 | $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite); |
||
| 951 | |||
| 952 | return $clone; |
||
| 953 | } |
||
| 954 | |||
| 955 | public function onBeforeWrite() |
||
| 956 | { |
||
| 957 | parent::onBeforeWrite(); |
||
| 958 | |||
| 959 | // Ensure that this object has a non-conflicting Access Key |
||
| 960 | if (!$this->AccessKey) { |
||
| 961 | $this->AccessKey = $this->generate_random_string(40); |
||
| 962 | |||
| 963 | while (!$this->validAccessKey()) { |
||
| 964 | $this->AccessKey = $this->generate_random_string(40); |
||
| 965 | } |
||
| 966 | } |
||
| 967 | |||
| 968 | $contact = $this->Customer(); |
||
| 969 | |||
| 970 | // If a contact is assigned and no customer details set |
||
| 971 | // then use contact details |
||
| 972 | if (!$this->PersonalDetails && $contact->exists()) { |
||
| 973 | foreach (Config::inst()->get(Contact::class, "db") as $param => $value) { |
||
| 974 | $this->$param = $contact->$param; |
||
| 975 | } |
||
| 976 | } |
||
| 977 | |||
| 978 | // if Billing Address is not set, use customer's default |
||
| 979 | // location |
||
| 980 | if (!$this->BillingAddress && $contact->exists() && $contact->DefaultLocation()) { |
||
| 981 | $location = $contact->DefaultLocation(); |
||
| 982 | foreach (Config::inst()->get(ContactLocation::class, "db") as $param => $value) { |
||
| 983 | $this->$param = $location->$param; |
||
| 984 | } |
||
| 985 | } |
||
| 986 | |||
| 987 | |||
| 988 | // Is delivery address set, if not, set it here |
||
| 989 | if (!$this->DeliveryAddress && $this->BillingAddress) { |
||
| 990 | $this->DeliveryCompany = $this->Company; |
||
| 991 | $this->DeliveryFirstName = $this->FirstName; |
||
| 992 | $this->DeliverySurname = $this->Surname; |
||
| 993 | $this->DeliveryAddress1 = $this->Address1; |
||
| 994 | $this->DeliveryAddress2 = $this->Address2; |
||
| 995 | $this->DeliveryCity = $this->City; |
||
| 996 | $this->DeliveryPostCode = $this->PostCode; |
||
| 997 | $this->DeliveryCountry = $this->Country; |
||
| 998 | } |
||
| 999 | |||
| 1000 | // Assign discount info if needed |
||
| 1001 | if ($this->Discount()->exists()) { |
||
| 1002 | $this->DiscountAmount = $this->get_discount_amount(); |
||
| 1003 | $this->DiscountType = $this->Discount()->Title; |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | // Assign postage info if set |
||
| 1007 | if ($this->Postage()->exists()) { |
||
| 1008 | $postage = $this->Postage(); |
||
| 1009 | $this->PostageType = $postage->Title; |
||
| 1010 | $this->PostageCost = $postage->Cost; |
||
| 1011 | $this->PostageTax = $postage->TaxAmount; |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | // If date not set, make thie equal the created date |
||
| 1015 | if (!$this->StartDate) { |
||
| 1016 | $this->StartDate = $this->Created; |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | if (!$this->EndDate && $this->StartDate) { |
||
| 1020 | $start = new DateTime($this->StartDate); |
||
| 1021 | $start->modify("+ {$this->config()->default_end} days"); |
||
| 1022 | $this->EndDate = $start->format("Y-m-d"); |
||
| 1023 | } |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * API Callback after this object is written to the DB |
||
| 1028 | * |
||
| 1029 | */ |
||
| 1030 | public function onAfterWrite() |
||
| 1031 | { |
||
| 1032 | parent::onAfterWrite(); |
||
| 1033 | |||
| 1034 | // Check if an order number has been generated, if not, add it and save again |
||
| 1035 | if (!$this->OrderNumber) { |
||
| 1036 | $this->OrderNumber = $this->generate_order_number(); |
||
| 1037 | |||
| 1038 | while (!$this->validOrderNumber()) { |
||
| 1039 | $this->OrderNumber = $this->generate_order_number(); |
||
| 1040 | } |
||
| 1041 | $this->write(); |
||
| 1042 | } |
||
| 1043 | } |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * API Callback before this object is removed from to the DB |
||
| 1047 | * |
||
| 1048 | */ |
||
| 1049 | public function onBeforeDelete() |
||
| 1050 | { |
||
| 1051 | parent::onBeforeDelete(); |
||
| 1052 | |||
| 1053 | // Delete all items attached to this order |
||
| 1054 | foreach ($this->Items() as $item) { |
||
| 1055 | $item->delete(); |
||
| 1056 | } |
||
| 1057 | } |
||
| 1058 | |||
| 1059 | public function providePermissions() |
||
| 1060 | { |
||
| 1061 | return [ |
||
| 1062 | "ORDERS_VIEW_ESTIMATES" => [ |
||
| 1063 | 'name' => 'View any estimate', |
||
| 1064 | 'help' => 'Allow user to view any estimate', |
||
| 1065 | 'category' => 'Orders', |
||
| 1066 | 'sort' => 89 |
||
| 1067 | ], |
||
| 1068 | "ORDERS_CREATE_ESTIMATES" => [ |
||
| 1069 | 'name' => 'Create estimates', |
||
| 1070 | 'help' => 'Allow user to create new estimates', |
||
| 1071 | 'category' => 'Orders', |
||
| 1072 | 'sort' => 88 |
||
| 1073 | ], |
||
| 1074 | "ORDERS_EDIT_ESTIMATES" => [ |
||
| 1075 | 'name' => 'Edit any estimate', |
||
| 1076 | 'help' => 'Allow user to edit any estimate', |
||
| 1077 | 'category' => 'Orders', |
||
| 1078 | 'sort' => 87 |
||
| 1079 | ], |
||
| 1080 | "ORDERS_DELETE_ESTIMATES" => [ |
||
| 1081 | 'name' => 'Delete any estimate', |
||
| 1082 | 'help' => 'Allow user to delete any estimate', |
||
| 1083 | 'category' => 'Orders', |
||
| 1084 | 'sort' => 86 |
||
| 1085 | ] |
||
| 1086 | ]; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Only order creators or users with VIEW admin rights can view |
||
| 1091 | * |
||
| 1092 | * @return boolean |
||
| 1093 | */ |
||
| 1094 | public function canView($member = null) |
||
| 1095 | { |
||
| 1096 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 1097 | |||
| 1098 | if ($extended !== null) { |
||
| 1099 | return $extended; |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | if (!$member) { |
||
| 1103 | $member = Member::currentUser(); |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | if ($member && Permission::checkMember($member->ID, ["ADMIN", "ORDERS_VIEW_ESTIMATES"])) { |
||
| 1107 | return true; |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | return false; |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Anyone can create orders, even guest users |
||
| 1115 | * |
||
| 1116 | * @return boolean |
||
| 1117 | */ |
||
| 1118 | public function canCreate($member = null, $context = []) |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Only users with EDIT admin rights can view an order |
||
| 1139 | * |
||
| 1140 | * @return boolean |
||
| 1141 | */ |
||
| 1142 | public function canEdit($member = null) |
||
| 1143 | { |
||
| 1144 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 1145 | |||
| 1146 | if ($extended !== null) { |
||
| 1147 | return $extended; |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | if (!$member) { |
||
| 1151 | $member = Member::currentUser(); |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | if ($member && Permission::checkMember($member->ID, ["ADMIN", "ORDERS_EDIT_ESTIMATES"])) { |
||
| 1155 | return true; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | return false; |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * No one should be able to delete an order once it has been created |
||
| 1163 | * |
||
| 1164 | * @return boolean |
||
| 1165 | */ |
||
| 1166 | public function canDelete($member = null) |
||
| 1183 | } |
||
| 1184 | } |
||
| 1185 |