Skip to content
Snippets Groups Projects
Commit d8729628 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Version 1.1.0pre: Implement (max|min)(Length|Width|Height|Volume) and volume variables

parent 28c5eb3e
No related branches found
No related tags found
No related merge requests found
BASE=rules_shipping BASE=rules_shipping
BASE_ADV=rules_shipping_advanced BASE_ADV=rules_shipping_advanced
PLUGINTYPE=vmshipment PLUGINTYPE=vmshipment
VERSION=1.0.0 VERSION=1.1.0
PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html
PLUGINFILES_ADV=$(BASE_ADV).php $(BASE).php $(BASE_ADV).script.php $(BASE_ADV).xml index.html PLUGINFILES_ADV=$(BASE_ADV).php $(BASE).php $(BASE_ADV).script.php $(BASE_ADV).xml index.html
......
...@@ -14,7 +14,7 @@ VMSHIPMENT_RULES_HELP="<div style='background: #DDDDDD; padding:5px; margin:3px; ...@@ -14,7 +14,7 @@ VMSHIPMENT_RULES_HELP="<div style='background: #DDDDDD; padding:5px; margin:3px;
<ul><li>Each line contains one rule consisting of several <b>conditions and assignments in arbitrary order and separated by semicolons</b>. </li> <ul><li>Each line contains one rule consisting of several <b>conditions and assignments in arbitrary order and separated by semicolons</b>. </li>
<li>The <b>first matching rule is used</b> to calculate the shipping costs.</li> <li>The <b>first matching rule is used</b> to calculate the shipping costs.</li>
<li>All rule parts with <b>comparison operators (&lt;, &lt;=, =&lt;, ==, !=, &lt;&gt;, &gt;=, =&gt;, &gt;)</b> are conditions.</li> <li>All rule parts with <b>comparison operators (&lt;, &lt;=, =&lt;, ==, !=, &lt;&gt;, &gt;=, =&gt;, &gt;)</b> are conditions.</li>
<li> Supported <b>variables (case-insensitive)</b>: <b><tt>Amount</tt></b>, <b><tt>Weight</tt></b>, <b><tt>ZIP</tt></b>, <b><tt>Products</tt></b> (number of different products order), <b><tt>Articles</tt></b> (each product counted with its quantity).</li> <li> Supported <b>variables (case-insensitive)</b>: <b><tt>Amount</tt></b>, <b><tt>Weight</tt></b>, <b><tt>ZIP</tt></b>, <b><tt>Products</tt></b> (number of different products), <b><tt>Articles</tt></b> (counted with quantity), <b><tt>Volume</tt></b> (total volume of the order) and <b><tt>MinVolume</tt>, <tt>MinLength</tt>, <tt>MinWidth</tt>, <tt>MinHeight</tt></b> as well as the same variables with <tt>Max</tt> instead of <tt>Min</tt>.</li>
<li>A condition can consist of multiple chained comparisons.</li> <li>A condition can consist of multiple chained comparisons.</li>
<li>All rule parts of the form <b><tt>[VARIABLE]=VALUE</tt> are assignments</b>, with <tt>[VARIABLE]</tt> being one of <b><tt>Name</tt></b> (optional name of the rule, displayed in the cart and invoice), <b><tt>Shipping</tt></b> (shipping cost if the rule matches). The <tt>Shipping=</tt> can be left out. I.e. a rule part consisting only of a numerical value is understood as shipping cost.</li> <li>All rule parts of the form <b><tt>[VARIABLE]=VALUE</tt> are assignments</b>, with <tt>[VARIABLE]</tt> being one of <b><tt>Name</tt></b> (optional name of the rule, displayed in the cart and invoice), <b><tt>Shipping</tt></b> (shipping cost if the rule matches). The <tt>Shipping=</tt> can be left out. I.e. a rule part consisting only of a numerical value is understood as shipping cost.</li>
</ul> </ul>
...@@ -24,7 +24,7 @@ VMSHIPMENT_RULES_HELP_ADV="<div style='background: #DDDDDD; padding:5px; margin: ...@@ -24,7 +24,7 @@ VMSHIPMENT_RULES_HELP_ADV="<div style='background: #DDDDDD; padding:5px; margin:
<ul><li>Each line contains one rule consisting of several <b>conditions and assignments in arbitrary order and separated by semicolons</b>. </li> <ul><li>Each line contains one rule consisting of several <b>conditions and assignments in arbitrary order and separated by semicolons</b>. </li>
<li>The <b>first matching rule is used</b> to calculate the shipping costs.</li> <li>The <b>first matching rule is used</b> to calculate the shipping costs.</li>
<li>All rule parts with <b>comparison operators (&lt;, &lt;=, =&lt;, ==, !=, &lt;&gt;, &gt;=, =&gt;, &gt;)</b> are conditions.</li> <li>All rule parts with <b>comparison operators (&lt;, &lt;=, =&lt;, ==, !=, &lt;&gt;, &gt;=, =&gt;, &gt;)</b> are conditions.</li>
<li> Supported <b>variables (case-insensitive)</b>: <b><tt>Amount</tt></b>, <b><tt>Weight</tt></b>, <b><tt>ZIP</tt></b>, <b><tt>Products</tt></b> (number of different products order), <b><tt>Articles</tt></b> (each product counted with its quantity).</li> <li> Supported <b>variables (case-insensitive)</b>: <b><tt>Amount</tt></b>, <b><tt>Weight</tt></b>, <b><tt>ZIP</tt></b>, <b><tt>Products</tt></b> (number of different products), <b><tt>Articles</tt></b> (counted with quantity), <b><tt>Volume</tt></b> (total volume of the order) and <b><tt>MinVolume</tt>, <tt>MinLength</tt>, <tt>MinWidth</tt>, <tt>MinHeight</tt></b> as well as the same variables with <tt>Max</tt> instead of <tt>Min</tt>.</li>
<li>A condition can consist of multiple chained comparisons.</li> <li>A condition can consist of multiple chained comparisons.</li>
<li>All rule parts of the form <b><tt>[VARIABLE]=VALUE</tt> are assignments</b>, with <tt>[VARIABLE]</tt> being one of <b><tt>Name</tt></b> (optional name of the rule, displayed in the cart and invoice), <b><tt>Shipping</tt></b> (shipping cost if the rule matches). The <tt>Shipping=</tt> can be left out. I.e. a rule part consisting only of a numerical value is understood as shipping cost.</li> <li>All rule parts of the form <b><tt>[VARIABLE]=VALUE</tt> are assignments</b>, with <tt>[VARIABLE]</tt> being one of <b><tt>Name</tt></b> (optional name of the rule, displayed in the cart and invoice), <b><tt>Shipping</tt></b> (shipping cost if the rule matches). The <tt>Shipping=</tt> can be left out. I.e. a rule part consisting only of a numerical value is understood as shipping cost.</li>
<li>In the advanced version, all expressions (conditions and shipping costs) may contain <b>arbitrary basic arithmetic expressions (+, -, *, /, %, ^ and parentheses)</b> of the above variables.</li> <li>In the advanced version, all expressions (conditions and shipping costs) may contain <b>arbitrary basic arithmetic expressions (+, -, *, /, %, ^ and parentheses)</b> of the above variables.</li>
......
File added
File added
...@@ -218,14 +218,8 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin { ...@@ -218,14 +218,8 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin {
*/ */
function getCosts (VirtueMartCart $cart, $method, $cart_prices) { function getCosts (VirtueMartCart $cart, $method, $cart_prices) {
if (empty($method->rules)) $this->parseMethodRules($method); if (empty($method->rules)) $this->parseMethodRules($method);
$cartvals = $this->getCartValues ($cart, $cart_prices);
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
$cartvals = array('weight' => $this->getOrderWeight ($cart, $method->weight_unit),
'zip' => $address['zip'],
'articles' => $this->getOrderArticles ($cart),
'products' => $this->getOrderProducts ($cart),
'amount' => $cart_prices['salesPrice'],
'country' => $address['virtuemart_country_id']);
foreach ($method->rules as $r) { foreach ($method->rules as $r) {
if ($r->matches($cartvals)) { if ($r->matches($cartvals)) {
$method->tax_id = $r->tax_id; $method->tax_id = $r->tax_id;
...@@ -276,6 +270,35 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin { ...@@ -276,6 +270,35 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin {
return $articles; return $articles;
} }
protected function getOrderDimensions (VirtueMartCart $cart) {
/* Cache the value in a static variable and calculate it only once! */
static $calculated = 0;
static $dimensions=array(
'volume' => 0,
'maxvolume' => 0, 'minvolume' => 9999999999,
'maxlength' => 0, 'minlength' => 9999999999,
'maxwidth' => 0, 'minwidth' => 9999999999,
'maxheight' => 0, 'minheight' => 9999999999,
);
if ($calculated==0) {
$calculated=1;
foreach ($cart->products as $product) {
$volume = $product->product_length * $product->product_width * $product->product_height;
$dimensions['volume'] += $volume * $product->quantity;
$dimensions['maxvolume'] = max ($dimensions['maxvolume'], $volume);
$dimensions['minvolume'] = min ($dimensions['minvolume'], $volume);
$dimensions['maxlength'] = max ($dimensions['maxlength'], $product->product_length);
$dimensions['minlength'] = min ($dimensions['minlength'], $product->product_length);
$dimensions['maxwidth'] = max ($dimensions['maxwidth'], $product->product_width);
$dimensions['minwidth'] = min ($dimensions['minwidth'], $product->product_width);
$dimensions['maxheight'] = max ($dimensions['maxheight'], $product->product_height);
$dimensions['minheight'] = min ($dimensions['minheight'], $product->product_height);
$articles += $product->quantity;
}
}
return $dimensions;
}
protected function getOrderProducts (VirtueMartCart $cart) { protected function getOrderProducts (VirtueMartCart $cart) {
/* Cache the value in a static variable and calculate it only once! */ /* Cache the value in a static variable and calculate it only once! */
static $products = 0; static $products = 0;
...@@ -284,18 +307,10 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin { ...@@ -284,18 +307,10 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin {
} }
return $products; return $products;
} }
protected function getCartValues (VirtueMartCart $cart, $cart_prices) {
/**
* @param \VirtueMartCart $cart
* @param int $method
* @param array $cart_prices
* @return bool
*/
protected function checkConditions ($cart, $method, $cart_prices) {
if (empty($method->rules)) $this->parseMethodRules($method);
$orderWeight = $this->getOrderWeight ($cart, $method->weight_unit); $orderWeight = $this->getOrderWeight ($cart, $method->weight_unit);
$dimensions = $this->getOrderDimensions ($cart);
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST); $address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
$products = 0; $products = 0;
...@@ -304,13 +319,36 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin { ...@@ -304,13 +319,36 @@ class plgVmShipmentRules_Shipping extends vmPSPlugin {
$products += 1; $products += 1;
$articles += $product->quantity; $articles += $product->quantity;
} }
$cartvals = array('weight'=>$orderWeight, $cartvals = array('weight'=>$orderWeight,
'zip'=>$address['zip'], 'zip'=>$address['zip'],
'articles'=>$articles, 'articles'=>$articles,
'products'=>$products, 'products'=>$products,
'amount'=>$cart_prices['salesPrice'], 'amount'=>$cart_prices['salesPrice'],
'country'=>$address['virtuemart_country_id']); 'country'=>$address['virtuemart_country_id'],
'volume' => $dimensions['volume'],
'maxvolume' => $dimensions['maxvolume'],
'minvolume' => $dimensions['minvolume'],
'maxlength' => $dimensions['maxlength'],
'minlength' => $dimensions['minlength'],
'maxwidth' => $dimensions['maxwidth'],
'minwidth' => $dimensions['minwidth'],
'maxheight' => $dimensions['maxheight'],
'minheight' => $dimensions['minheight']
);
return $cartvals;
}
/**
* @param \VirtueMartCart $cart
* @param int $method
* @param array $cart_prices
* @return bool
*/
protected function checkConditions ($cart, $method, $cart_prices) {
if (empty($method->rules)) $this->parseMethodRules($method);
$cartvals = $this->getCartValues ($cart, $cart_prices);
foreach ($method->rules as $r) { foreach ($method->rules as $r) {
if ($r->matches($cartvals)) { if ($r->matches($cartvals)) {
$method->matched_rule = $r; $method->matched_rule = $r;
...@@ -438,8 +476,9 @@ class ShippingRule { ...@@ -438,8 +476,9 @@ class ShippingRule {
var $rulestring = ''; var $rulestring = '';
var $countries = array(); var $countries = array();
var $tax_id = 0; var $tax_id = 0;
var $conditions=array(); var $conditions = array();
var $shipping=0; var $shipping = 0;
var $shippingWithTax = 0;
var $name = ''; var $name = '';
function __construct ($rule, $countries, $tax_id) { function __construct ($rule, $countries, $tax_id) {
...@@ -453,6 +492,14 @@ class ShippingRule { ...@@ -453,6 +492,14 @@ class ShippingRule {
$this->parseRule($rule); $this->parseRule($rule);
} }
function handleAssignment ($variable, $value, $rulepart) {
switch ($variable) {
case 'shipping': $this->shipping = $this->parseShippingTerm($value); break;
case 'shippingwithtax': $this->shippingWithTax = $this->parseShippingTerm($value); break;
case 'name': $this->name = $value; break;
default: JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_UNKNOWN_VARIABLE', $variable, $rulepart), 'error');
}
}
function parseRule($rule) { function parseRule($rule) {
$ruleparts=explode(';', $rule); $ruleparts=explode(';', $rule);
$operators = array('<', '<=', '=', '>', '>=', '=>', '=<', '<>', '!=', '=='); $operators = array('<', '<=', '=', '>', '>=', '=>', '=<', '<>', '!=', '==');
...@@ -464,11 +511,7 @@ class ShippingRule { ...@@ -464,11 +511,7 @@ class ShippingRule {
if (count($atoms)==1) { if (count($atoms)==1) {
$this->shipping = $this->parseShippingTerm($atoms[0]); $this->shipping = $this->parseShippingTerm($atoms[0]);
} elseif ($atoms[1]=='=') { } elseif ($atoms[1]=='=') {
switch (strtolower($atoms[0])) { $this->handleAssignment (strtolower($atoms[0]), $atoms[2], $p);
case 'shipping': $this->shipping = $this->parseShippingTerm($atoms[2]); break;
case 'name': $this->name = $atoms[2]; break;
default: JFactory::getApplication()->enqueueMessage(JText::sprintf('VMSHIPMENT_RULES_UNKNOWN_VARIABLE', $atoms[0], $p), 'error');
}
} else { } else {
// Conditions, need at least three atoms! // Conditions, need at least three atoms!
while (count($atoms)>1) { while (count($atoms)>1) {
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<authorUrl>http://www.kainhofer.com</authorUrl> <authorUrl>http://www.kainhofer.com</authorUrl>
<copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright> <copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright>
<license>GPL v3+</license> <license>GPL v3+</license>
<version>1.0.0</version> <version>1.1.0</version>
<description>VMSHIPMENT_RULES_DESC</description> <description>VMSHIPMENT_RULES_DESC</description>
<files> <files>
<filename plugin="rules_shipping">rules_shipping.php</filename> <filename plugin="rules_shipping">rules_shipping.php</filename>
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<authorUrl>http://www.kainhofer.com</authorUrl> <authorUrl>http://www.kainhofer.com</authorUrl>
<copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright> <copyright>Copyright (C) 2013, Reinhold Kainhofer</copyright>
<license>GPL v3+</license> <license>GPL v3+</license>
<version>1.0.0</version> <version>1.1.0</version>
<description>VMSHIPMENT_RULES_ADV_DESC</description> <description>VMSHIPMENT_RULES_ADV_DESC</description>
<files> <files>
<filename plugin="rules_shipping_advanced">rules_shipping_advanced.php</filename> <filename plugin="rules_shipping_advanced">rules_shipping_advanced.php</filename>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment