From 751950204615edf8f74efd8ec30f31817768f71d Mon Sep 17 00:00:00 2001 From: helpfulrobot Date: Fri, 1 Jan 2016 05:31:17 +1300 Subject: [PATCH] Converted to PSR-2 --- .../BulkPriceCatalogueController.php | 12 ++++--- code/extensions/BulkPriceProduct.php | 12 ++++--- code/extensions/BulkPriceProductCSV.php | 18 +++++----- code/extensions/BulkPriceShoppingCart.php | 36 +++++++++++-------- code/model/BulkPrice.php | 19 ++++++---- 5 files changed, 57 insertions(+), 40 deletions(-) diff --git a/code/extensions/BulkPriceCatalogueController.php b/code/extensions/BulkPriceCatalogueController.php index 8285507..de42992 100644 --- a/code/extensions/BulkPriceCatalogueController.php +++ b/code/extensions/BulkPriceCatalogueController.php @@ -7,13 +7,16 @@ * @author i-lateral (http://www.i-lateral.com) * @package commerce-bulkprice */ -class BulkPriceCatalogueController extends Extension { +class BulkPriceCatalogueController extends Extension +{ - public function BulkPriceTable() { - if($this->owner->dataRecord instanceOf Product) + public function BulkPriceTable() + { + if ($this->owner->dataRecord instanceof Product) { $prices = $this->owner->dataRecord->BulkPrices(); - else + } else { $prices = ArrayList::create(); + } $vars = array( "BulkPrices" => $prices, @@ -24,5 +27,4 @@ public function BulkPriceTable() { ->owner ->renderWith("BulkPriceTable", $vars); } - } diff --git a/code/extensions/BulkPriceProduct.php b/code/extensions/BulkPriceProduct.php index 2329f8b..d11a178 100644 --- a/code/extensions/BulkPriceProduct.php +++ b/code/extensions/BulkPriceProduct.php @@ -7,13 +7,15 @@ * @author i-lateral (http://www.i-lateral.com) * @package commerce-bulkprice */ -class BulkPriceProduct extends DataExtension { +class BulkPriceProduct extends DataExtension +{ private static $has_many = array( "BulkPrices" => "BulkPrice" ); - public function updateCMSFields(FieldList $fields) { + public function updateCMSFields(FieldList $fields) + { // Deal with product features $add_button = new GridFieldAddNewInlineButton('toolbar-header-left'); @@ -35,11 +37,11 @@ public function updateCMSFields(FieldList $fields) { $fields->addFieldToTab('Root.BulkPrices', $bulk_field); } - public function onBeforeDelete() { + public function onBeforeDelete() + { // Clean database before deletion - foreach($this->owner->BulkPrices() as $object) { + foreach ($this->owner->BulkPrices() as $object) { $object->delete(); } } - } diff --git a/code/extensions/BulkPriceProductCSV.php b/code/extensions/BulkPriceProductCSV.php index 5588dfb..6998ea6 100644 --- a/code/extensions/BulkPriceProductCSV.php +++ b/code/extensions/BulkPriceProductCSV.php @@ -1,16 +1,18 @@ BulkPrices() as $bulk_price) { + foreach ($product->BulkPrices() as $bulk_price) { $range = array(); // Determine whaty type of price we are dealing with - if(strpos($bulk_price->Quantity,"-") !== false) { // We are looking for a range + if (strpos($bulk_price->Quantity, "-") !== false) { // We are looking for a range $range = explode("-", $bulk_price->Quantity); - } elseif(strpos($bulk_price->Quantity,"+") !== false) { // We are looking this number or greater + } elseif (strpos($bulk_price->Quantity, "+") !== false) { // We are looking this number or greater $range[0] = str_replace("+", "", $bulk_price->Quantity); $range[1] = -1; // -1 means no upper limit } else { // Assume we are dealing with a single price @@ -32,17 +34,20 @@ private function calculate_bulk_price(Product $product, $item) { // Finally check if the current quantity sits in the // current range and amend price - if( + if ( ($range[1] == -1 && $item->Quantity >= $range[0]) || ($item->Quantity >= $range[0] && $item->Quantity <= $range[1]) - ) + ) { $price = $bulk_price->Price; + } } - if(!$price) $price = $product->Price; + if (!$price) { + $price = $product->Price; + } // Check for customisations that modify price - foreach($item->Customised as $custom_item) { + foreach ($item->Customised as $custom_item) { // If a customisation modifies price, adjust the price $price = (float)$price + (float)$custom_item->ModifyPrice; } @@ -54,17 +59,20 @@ private function calculate_bulk_price(Product $product, $item) { /** * Calculate the item price, based on any bulk discounts set */ - public function onBeforeAdd($item) { - if($product = Product::get()->byID($item->ProductID)) + public function onBeforeAdd($item) + { + if ($product = Product::get()->byID($item->ProductID)) { $item->Price = $this->calculate_bulk_price($product, $item); + } } /** * Calculate the item price, based on any bulk discounts set */ - public function onAfterUpdate($item) { - if($product = Product::get()->byID($item->ProductID)) + public function onAfterUpdate($item) + { + if ($product = Product::get()->byID($item->ProductID)) { $item->Price = $this->calculate_bulk_price($product, $item); + } } - } diff --git a/code/model/BulkPrice.php b/code/model/BulkPrice.php index 36bdeab..0ac7276 100644 --- a/code/model/BulkPrice.php +++ b/code/model/BulkPrice.php @@ -14,7 +14,8 @@ * @author i-lateral (http://www.i-lateral.com) * @package commerce-bulkprice */ -class BulkPrice extends DataObject { +class BulkPrice extends DataObject +{ private static $db = array( "Quantity" => "Varchar", @@ -30,27 +31,31 @@ class BulkPrice extends DataObject { "Price" ); - public function onBeforeWrite() { + public function onBeforeWrite() + { parent::onBeforeWrite(); // Ensure we strip any white space from the quantity field $this->Quantity = str_replace(" ", "", $this->Quantity); } - public function canView($member = false) { + public function canView($member = false) + { return $this->Parent()->canView($member); } - public function canCreate($member = null) { + public function canCreate($member = null) + { return $this->Parent()->canCreate($member); } - public function canEdit($member = null) { + public function canEdit($member = null) + { return $this->Parent()->canEdit($member); } - public function canDelete($member = null) { + public function canDelete($member = null) + { return $this->Parent()->canDelete($member); } - }