| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,254 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+/** |
|
| 4 |
+ * Isotope eCommerce for Contao Open Source CMS |
|
| 5 |
+ * |
|
| 6 |
+ * Copyright (C) 2009-2016 terminal42 gmbh & Isotope eCommerce Workgroup |
|
| 7 |
+ * |
|
| 8 |
+ * @link https://isotopeecommerce.org |
|
| 9 |
+ * @license https://opensource.org/licenses/lgpl-3.0.html |
|
| 10 |
+ */ |
|
| 11 |
+ |
|
| 12 |
+namespace eSM_isotope_custom\Model\ProductCollectionSurcharge; |
|
| 13 |
+ |
|
| 14 |
+use Haste\Units\Mass\Weight; |
|
| 15 |
+use Isotope\Interfaces\IsotopeProductCollection; |
|
| 16 |
+use Isotope\Interfaces\IsotopeProductCollectionSurcharge; |
|
| 17 |
+use Isotope\Isotope; |
|
| 18 |
+use Isotope\Model\ProductCollectionSurcharge; |
|
| 19 |
+use Isotope\Model\Rule as RuleModel; |
|
| 20 |
+use Isotope\Model\ProductCollectionSurcharge\Rule; |
|
| 21 |
+ |
|
| 22 |
+/** |
|
| 23 |
+ * Class Payment |
|
| 24 |
+ * |
|
| 25 |
+ * Implements payment surcharge in product collection |
|
| 26 |
+ * @copyright Isotope eCommerce Workgroup 2009-2012 |
|
| 27 |
+ * @author Andreas Schempp <andreas.schempp@terminal42.ch> |
|
| 28 |
+ */ |
|
| 29 |
+class AffentalerRule extends ProductCollectionSurcharge implements IsotopeProductCollectionSurcharge |
|
| 30 |
+{
|
|
| 31 |
+ |
|
| 32 |
+ public static function createForRuleInCollection(RuleModel $objRule, IsotopeProductCollection $objCollection) |
|
| 33 |
+ {
|
|
| 34 |
+ // Cart subtotal |
|
| 35 |
+ if (($objRule->minSubtotal > 0 && $objCollection->getSubtotal() < $objRule->minSubtotal) || ($objRule->maxSubtotal > 0 && $objCollection->getSubtotal() > $objRule->maxSubtotal)) {
|
|
| 36 |
+ return null; |
|
| 37 |
+ } |
|
| 38 |
+ |
|
| 39 |
+ // Cart weight |
|
| 40 |
+ $objScale = Isotope::getCart()->addToScale(); |
|
| 41 |
+ |
|
| 42 |
+ if (($minWeight = Weight::createFromTimePeriod($objRule->minWeight)) !== null |
|
| 43 |
+ && $objScale->isLessThan($minWeight) |
|
| 44 |
+ ) {
|
|
| 45 |
+ return null; |
|
| 46 |
+ } |
|
| 47 |
+ |
|
| 48 |
+ if (($maxWeight = Weight::createFromTimePeriod($objRule->maxWeight)) !== null |
|
| 49 |
+ && $objScale->isMoreThan($maxWeight) |
|
| 50 |
+ ) {
|
|
| 51 |
+ return null; |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ $arrCollectionItems = $objCollection->getItems(); |
|
| 55 |
+ |
|
| 56 |
+ $blnMatch = false; |
|
| 57 |
+ $blnPercentage = $objRule->isPercentage(); |
|
| 58 |
+ $fltDiscount = $blnPercentage ? $objRule->getPercentage() : 0; |
|
| 59 |
+ $fltTotal = 0; |
|
| 60 |
+ $arrSubtract = array(); |
|
| 61 |
+ |
|
| 62 |
+ $objSurcharge = new static(); |
|
| 63 |
+ $objSurcharge->label = $objRule->getLabel(); |
|
| 64 |
+ $objSurcharge->price = $objRule->getPercentageLabel(); |
|
| 65 |
+ $objSurcharge->total_price = 0; |
|
| 66 |
+ $objSurcharge->tax_class = 0; |
|
| 67 |
+ $objSurcharge->before_tax = true; |
|
| 68 |
+ $objSurcharge->addToTotal = true; |
|
| 69 |
+ |
|
| 70 |
+ // Product or producttype restrictions |
|
| 71 |
+ if ($objRule->productRestrictions != '' && $objRule->productRestrictions != 'none') {
|
|
| 72 |
+ $arrLimit = \Database::getInstance()->execute("SELECT object_id FROM tl_iso_rule_restriction WHERE pid={$objRule->id} AND type='{$objRule->productRestrictions}'")->fetchEach('object_id');
|
|
| 73 |
+ |
|
| 74 |
+ if ($objRule->productRestrictions == 'pages' && !empty($arrLimit)) {
|
|
| 75 |
+ $arrLimit = \Database::getInstance()->execute("SELECT pid FROM " . \Isotope\Model\ProductCategory::getTable() . " WHERE page_id IN (" . implode(',', $arrLimit) . ")")->fetchEach('pid');
|
|
| 76 |
+ } |
|
| 77 |
+ |
|
| 78 |
+ if ($objRule->quantityMode == 'cart_products' || $objRule->quantityMode == 'cart_items') {
|
|
| 79 |
+ $intTotal = 0; |
|
| 80 |
+ foreach ($arrCollectionItems as $objItem) {
|
|
| 81 |
+ if (!$objItem->hasProduct()) {
|
|
| 82 |
+ continue; |
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ $objProduct = $objItem->getProduct(); |
|
| 86 |
+ |
|
| 87 |
+ if ((($objRule->productRestrictions == 'products' || $objRule->productRestrictions == 'variants' || $objRule->productRestrictions == 'pages') |
|
| 88 |
+ && (in_array($objProduct->id, $arrLimit) || ($objProduct->pid > 0 && in_array($objProduct->pid, $arrLimit)))) |
|
| 89 |
+ || ($objRule->productRestrictions == 'producttypes' && in_array($objProduct->type, $arrLimit)) |
|
| 90 |
+ ) {
|
|
| 91 |
+ $intTotal += $objRule->quantityMode == 'cart_items' ? $objItem->quantity : 1; |
|
| 92 |
+ } |
|
| 93 |
+ } |
|
| 94 |
+ } |
|
| 95 |
+ } else {
|
|
| 96 |
+ switch ($objRule->quantityMode) {
|
|
| 97 |
+ case 'cart_products': |
|
| 98 |
+ $intTotal = $objCollection->countItems(); |
|
| 99 |
+ break; |
|
| 100 |
+ |
|
| 101 |
+ case 'cart_items': |
|
| 102 |
+ $intTotal = $objCollection->sumItemsQuantity(); |
|
| 103 |
+ break; |
|
| 104 |
+ } |
|
| 105 |
+ } |
|
| 106 |
+ |
|
| 107 |
+ $intSubstitute = (($objRule->minItemQuantity-1) > 0 ? ($objRule->minItemQuantity-1) : 0); |
|
| 108 |
+ foreach ($arrCollectionItems as $objItem) {
|
|
| 109 |
+ if (!$objItem->hasProduct()) {
|
|
| 110 |
+ continue; |
|
| 111 |
+ } |
|
| 112 |
+ |
|
| 113 |
+ $objProduct = $objItem->getProduct(); |
|
| 114 |
+ |
|
| 115 |
+ // Product restrictions |
|
| 116 |
+ if ((($objRule->productRestrictions == 'products' || $objRule->productRestrictions == 'variants' || $objRule->productRestrictions == 'pages') |
|
| 117 |
+ && (!in_array($objProduct->id, $arrLimit) && ($objProduct->pid == 0 || !in_array($objProduct->pid, $arrLimit)))) |
|
| 118 |
+ || ($objRule->productRestrictions == 'producttypes' && !in_array($objProduct->type, $arrLimit)) |
|
| 119 |
+ ) {
|
|
| 120 |
+ continue; |
|
| 121 |
+ } elseif ($objRule->productRestrictions == 'attribute') {
|
|
| 122 |
+ switch ($objRule->attributeCondition) {
|
|
| 123 |
+ case 'eq': |
|
| 124 |
+ if (!($objProduct->{$objRule->attributeName} == $objRule->attributeValue)) {
|
|
| 125 |
+ continue(2); |
|
| 126 |
+ } |
|
| 127 |
+ break; |
|
| 128 |
+ |
|
| 129 |
+ case 'neq': |
|
| 130 |
+ if (!($objProduct->{$objRule->attributeName} != $objRule->attributeValue)) {
|
|
| 131 |
+ continue(2); |
|
| 132 |
+ } |
|
| 133 |
+ break; |
|
| 134 |
+ |
|
| 135 |
+ case 'lt': |
|
| 136 |
+ if (!($objProduct->{$objRule->attributeName} < $objRule->attributeValue)) {
|
|
| 137 |
+ continue(2); |
|
| 138 |
+ } |
|
| 139 |
+ break; |
|
| 140 |
+ |
|
| 141 |
+ case 'gt': |
|
| 142 |
+ if (!($objProduct->{$objRule->attributeName} > $objRule->attributeValue)) {
|
|
| 143 |
+ continue(2); |
|
| 144 |
+ } |
|
| 145 |
+ break; |
|
| 146 |
+ |
|
| 147 |
+ case 'elt': |
|
| 148 |
+ if (!($objProduct->{$objRule->attributeName} <= $objRule->attributeValue)) {
|
|
| 149 |
+ continue(2); |
|
| 150 |
+ } |
|
| 151 |
+ break; |
|
| 152 |
+ |
|
| 153 |
+ case 'egt': |
|
| 154 |
+ if (!($objProduct->{$objRule->attributeName} >= $objRule->attributeValue)) {
|
|
| 155 |
+ continue(2); |
|
| 156 |
+ } |
|
| 157 |
+ break; |
|
| 158 |
+ |
|
| 159 |
+ case 'starts': |
|
| 160 |
+ if (stripos($objProduct->{$objRule->attributeName}, $objRule->attributeValue) !== 0) {
|
|
| 161 |
+ continue(2); |
|
| 162 |
+ } |
|
| 163 |
+ break; |
|
| 164 |
+ |
|
| 165 |
+ case 'ends': |
|
| 166 |
+ if (strripos($objProduct->{$objRule->attributeName}, $objRule->attributeValue) !== (strlen($objProduct->{$objRule->attributeName}) - strlen($objRule->attributeValue))) {
|
|
| 167 |
+ continue(2); |
|
| 168 |
+ } |
|
| 169 |
+ break; |
|
| 170 |
+ |
|
| 171 |
+ case 'contains': |
|
| 172 |
+ if (stripos($objProduct->{$objRule->attributeName}, $objRule->attributeValue) === false) {
|
|
| 173 |
+ continue(2); |
|
| 174 |
+ } |
|
| 175 |
+ break; |
|
| 176 |
+ |
|
| 177 |
+ default: |
|
| 178 |
+ throw new \Exception('Unknown rule condition "' . $objRule->attributeCondition . '"');
|
|
| 179 |
+ } |
|
| 180 |
+ } |
|
| 181 |
+ |
|
| 182 |
+ // Because we apply to the quantity of only this product, we override $intTotal in every foreach loop |
|
| 183 |
+ // This matches tl_iso_rules.quantityMode="product_quantity" |
|
| 184 |
+ if ($objRule->quantityMode != 'cart_products' && $objRule->quantityMode != 'cart_items') {
|
|
| 185 |
+ $intTotal = $objItem->quantity; |
|
| 186 |
+ } |
|
| 187 |
+ |
|
| 188 |
+ // Quantity does not match, do not apply to this product |
|
| 189 |
+ if (($objRule->minItemQuantity > 0 && $objRule->minItemQuantity > $intTotal) || ($objRule->maxItemQuantity > 0 && $objRule->maxItemQuantity < $intTotal)) {
|
|
| 190 |
+ continue; |
|
| 191 |
+ } |
|
| 192 |
+ |
|
| 193 |
+ // Apply To |
|
| 194 |
+ switch ($objRule->applyTo) {
|
|
| 195 |
+ case 'products': |
|
| 196 |
+ $fltPrice = $blnPercentage ? ($objItem->getTotalPrice() / 100 * $fltDiscount) : $objRule->discount; |
|
| 197 |
+ $fltPrice = $fltPrice > 0 ? (floor($fltPrice * 100) / 100) : (ceil($fltPrice * 100) / 100); |
|
| 198 |
+ $objSurcharge->total_price += $fltPrice; |
|
| 199 |
+ $objSurcharge->setAmountForCollectionItem($fltPrice, $objItem); |
|
| 200 |
+ break; |
|
| 201 |
+ |
|
| 202 |
+ case 'items': |
|
| 203 |
+ if ($intSubstitute - $objItem->quantity > 0) |
|
| 204 |
+ {
|
|
| 205 |
+ $intItemSubstitute = $objItem->quantity; |
|
| 206 |
+ } else if ($intSubstitute > 0) {
|
|
| 207 |
+ $intItemSubstitute = $intSubstitute; |
|
| 208 |
+ } else {
|
|
| 209 |
+ $intItemSubstitute = 0; |
|
| 210 |
+ } |
|
| 211 |
+ $intSubstitute = $intSubstitute - $objItem->quantity; |
|
| 212 |
+ |
|
| 213 |
+ $fltPrice = ($blnPercentage ? ($objItem->getPrice() / 100 * $fltDiscount) : $objRule->discount) * ($objItem->quantity-$intItemSubstitute); |
|
| 214 |
+ $fltPrice = $fltPrice > 0 ? (floor($fltPrice * 100) / 100) : (ceil($fltPrice * 100) / 100); |
|
| 215 |
+ $objSurcharge->total_price += $fltPrice; |
|
| 216 |
+ $objSurcharge->setAmountForCollectionItem($fltPrice, $objItem); |
|
| 217 |
+ break; |
|
| 218 |
+ |
|
| 219 |
+ case 'subtotal': |
|
| 220 |
+ $blnMatch = true; |
|
| 221 |
+ $objSurcharge->total_price += $objItem->getTotalPrice(); |
|
| 222 |
+ |
|
| 223 |
+ if ($objRule->tax_class == -1) {
|
|
| 224 |
+ if ($blnPercentage) {
|
|
| 225 |
+ $fltPrice = $objItem->getTotalPrice() / 100 * $fltDiscount; |
|
| 226 |
+ $objSurcharge->setAmountForCollectionItem($fltPrice, $objItem); |
|
| 227 |
+ } else {
|
|
| 228 |
+ $arrSubtract[] = $objItem; |
|
| 229 |
+ $fltTotal += (float) $objItem->getTaxFreeTotalPrice(); |
|
| 230 |
+ } |
|
| 231 |
+ } |
|
| 232 |
+ break; |
|
| 233 |
+ } |
|
| 234 |
+ } |
|
| 235 |
+ |
|
| 236 |
+ if ($objRule->applyTo == 'subtotal' && $blnMatch) {
|
|
| 237 |
+ // discount total! not related to tax subtraction |
|
| 238 |
+ $fltPrice = $blnPercentage ? ($objSurcharge->total_price / 100 * $fltDiscount) : $objRule->discount; |
|
| 239 |
+ $objSurcharge->total_price = $fltPrice > 0 ? (floor(round($fltPrice * 100, 4)) / 100) : (ceil(round($fltPrice * 100, 4)) / 100); |
|
| 240 |
+ $objSurcharge->before_tax = ($objRule->tax_class != 0 ? true : false); |
|
| 241 |
+ $objSurcharge->tax_class = ($objRule->tax_class > 0 ? $objRule->tax_class : 0); |
|
| 242 |
+ |
|
| 243 |
+ // If fixed price discount with splitted taxes, calculate total amount of discount per taxed product |
|
| 244 |
+ if ($objRule->tax_class == -1 && !$blnPercentage) {
|
|
| 245 |
+ foreach ($arrSubtract as $objItem) {
|
|
| 246 |
+ $fltPrice = $objRule->discount / 100 * (100 / $fltTotal * $objItem->getTaxFreeTotalPrice()); |
|
| 247 |
+ $objSurcharge->setAmountForCollectionItem($fltPrice, $objItem); |
|
| 248 |
+ } |
|
| 249 |
+ } |
|
| 250 |
+ } |
|
| 251 |
+ |
|
| 252 |
+ return $objSurcharge->total_price == 0 ? null : $objSurcharge; |
|
| 253 |
+ } |
|
| 254 |
+} |