From 9b9188a4e066526b888a8e01a550ca1b11b011e8 Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Thu, 5 Jan 2017 00:40:09 +0100
Subject: [PATCH] VMCoupon_V1.0: Add helper plugin to provide VM coupon
 discounts in the variables SalesPriceAfterCoupon and CouponValue

---
 plugins/vmcouponprice/Makefile                |  19 ++++
 plugins/vmcouponprice/index.html              |   0
 ...n-GB.plg_vmshipmentrules_vmcouponprice.ini |   6 ++
 ....plg_vmshipmentrules_vmcouponprice.sys.ini |   6 ++
 .../vmcouponprice/language/en-GB/index.html   |   1 +
 plugins/vmcouponprice/language/index.html     |   1 +
 ...ols_vmshipmentrules_vmcouponprice_v1.0.zip | Bin 0 -> 5720 bytes
 plugins/vmcouponprice/vmcouponprice.php       |  79 ++++++++++++++++
 .../vmcouponprice/vmcouponprice.script.php    |  88 ++++++++++++++++++
 plugins/vmcouponprice/vmcouponprice.xml       |  27 ++++++
 10 files changed, 227 insertions(+)
 create mode 100644 plugins/vmcouponprice/Makefile
 create mode 100644 plugins/vmcouponprice/index.html
 create mode 100644 plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini
 create mode 100644 plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini
 create mode 100644 plugins/vmcouponprice/language/en-GB/index.html
 create mode 100644 plugins/vmcouponprice/language/index.html
 create mode 100644 plugins/vmcouponprice/plg_opentools_vmshipmentrules_vmcouponprice_v1.0.zip
 create mode 100644 plugins/vmcouponprice/vmcouponprice.php
 create mode 100644 plugins/vmcouponprice/vmcouponprice.script.php
 create mode 100644 plugins/vmcouponprice/vmcouponprice.xml

diff --git a/plugins/vmcouponprice/Makefile b/plugins/vmcouponprice/Makefile
new file mode 100644
index 0000000..df0c814
--- /dev/null
+++ b/plugins/vmcouponprice/Makefile
@@ -0,0 +1,19 @@
+BASE=vmcouponprice
+PLUGINTYPE=vmshipmentrules
+ZIPBASE=opentools_vmshipmentrules
+VERSION=1.0
+
+PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html
+
+TRANSLATIONS=$(call wildcard,language/en*/*.plg_$(PLUGINTYPE)_$(BASE).*ini) 
+INDEXFILES=language/index.html $(call wildcard,language/**/index.html)
+ZIPFILE=plg_$(ZIPBASE)_$(BASE)_v$(VERSION).zip
+
+all: zip
+
+zip: $(PLUGINFILES) $(TRANSLATIONS) $(ADVANCEDFILES) $(INDEXFILES)
+	@echo "Packing all files into distribution file $(ZIPFILE):"
+	@zip -r $(ZIPFILE) $(PLUGINFILES) $(TRANSLATIONS) $(INDEXFILES)
+
+clean:
+	rm -f $(ZIPFILE)
diff --git a/plugins/vmcouponprice/index.html b/plugins/vmcouponprice/index.html
new file mode 100644
index 0000000..e69de29
diff --git a/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini
new file mode 100644
index 0000000..56172e5
--- /dev/null
+++ b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini
@@ -0,0 +1,6 @@
+; VM Rule-based Shipping plugin: Extension plugin to provide coupon-related variables
+; Copyright (C)  2017 Reinhold Kainhofer, Open Tools. All rights reserved.
+; License http://www.gnu.org/licenses/gpl.html GNU/GPL
+; Note : All ini files need to be saved as UTF-8 - No BOM
+VMSHIPMENTRULES_VMCOUPONPRICE="VM Shipping by Rules - Coupon handler"
+VMSHIPMENTRULES_VMCOUPONPRICE_DESC="<p>This plugin extends the shipping by rules plugin with two variables (couponValue and AmountAfterCoupon / salesPriceAfterCoupon) that take into account coupon discounts by the stock virtuemart coupon system.</p><p>By default, VirtueMart evaluates shipping costs first and coupons afterwards, so when shipping is calculated, the coupon discount is not yet calculated in general. To work around this issue, this small plugin adds to trickery to manually calculate the coupon during shipping cost evaluation. As this might interfere with VM (we have not yet run into issues, but who knows...), this functionality is not included by default, but only with this small extension plugin.</p>"
diff --git a/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini
new file mode 100644
index 0000000..56172e5
--- /dev/null
+++ b/plugins/vmcouponprice/language/en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini
@@ -0,0 +1,6 @@
+; VM Rule-based Shipping plugin: Extension plugin to provide coupon-related variables
+; Copyright (C)  2017 Reinhold Kainhofer, Open Tools. All rights reserved.
+; License http://www.gnu.org/licenses/gpl.html GNU/GPL
+; Note : All ini files need to be saved as UTF-8 - No BOM
+VMSHIPMENTRULES_VMCOUPONPRICE="VM Shipping by Rules - Coupon handler"
+VMSHIPMENTRULES_VMCOUPONPRICE_DESC="<p>This plugin extends the shipping by rules plugin with two variables (couponValue and AmountAfterCoupon / salesPriceAfterCoupon) that take into account coupon discounts by the stock virtuemart coupon system.</p><p>By default, VirtueMart evaluates shipping costs first and coupons afterwards, so when shipping is calculated, the coupon discount is not yet calculated in general. To work around this issue, this small plugin adds to trickery to manually calculate the coupon during shipping cost evaluation. As this might interfere with VM (we have not yet run into issues, but who knows...), this functionality is not included by default, but only with this small extension plugin.</p>"
diff --git a/plugins/vmcouponprice/language/en-GB/index.html b/plugins/vmcouponprice/language/en-GB/index.html
new file mode 100644
index 0000000..2efb97f
--- /dev/null
+++ b/plugins/vmcouponprice/language/en-GB/index.html
@@ -0,0 +1 @@
+<!DOCTYPE html><title></title>
diff --git a/plugins/vmcouponprice/language/index.html b/plugins/vmcouponprice/language/index.html
new file mode 100644
index 0000000..2efb97f
--- /dev/null
+++ b/plugins/vmcouponprice/language/index.html
@@ -0,0 +1 @@
+<!DOCTYPE html><title></title>
diff --git a/plugins/vmcouponprice/plg_opentools_vmshipmentrules_vmcouponprice_v1.0.zip b/plugins/vmcouponprice/plg_opentools_vmshipmentrules_vmcouponprice_v1.0.zip
new file mode 100644
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zo9ZtX?O%7@4h%UNV|LF_g4sG(%4^b-XA5dxmx@(!sK);Gb9&|TbB<)rxiVobiL=Tj
z%dC2P=gZZ_=*<2lk4VW*Xerr_{FGc=S*!<3%`p|Z5g<I8nnzPJ9aA%?Y+n{8)G5Qj
zz#z}Sz#xpAnu#mqHwTt{Ea&UwDF{8Gaq{fO&6+25PI_+Le6jsP3<DzDpj#w~Y!Rda
zpoVpfOmfVS8V7T%OafeiF)}bPY-t2BQL7d3$QdhS<P0qn26!Wk#q2a88|%l$fHok9
z+=XIgV8+sYLN*<<4To&HEEl@z;1(S#12dM^9K={$H8{jrP$+C^EaPE7sn1y%n6cL7
zh(N%s%^~JN!gd!A14VT^D+4o@dL7~)NZewU!N~Tv@RMynyhLVYV8&7!Bf9}TK?^e=
uyCFyj$qmQ}n{eQw8!U-p@NQ&-G4l;B<5}52am~TN!63oRz@Q}z;sF4AhP{6P

literal 0
HcmV?d00001

diff --git a/plugins/vmcouponprice/vmcouponprice.php b/plugins/vmcouponprice/vmcouponprice.php
new file mode 100644
index 0000000..c8d8e6b
--- /dev/null
+++ b/plugins/vmcouponprice/vmcouponprice.php
@@ -0,0 +1,79 @@
+<?php
+
+defined ('_JEXEC') or die('Restricted access');
+
+/**
+ * Plugin providing Custom variables for VM Shipping by Rules
+ *
+ * @subpackage Plugins - VmShipmentRules
+ * @copyright Copyright (C) 2017 Reinhold Kainhofer, office@open-tools.net
+ * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
+ * VirtueMart is free software. This version may have been modified pursuant
+ * to the GNU General Public License, and as distributed it includes or
+ * is derivative of works licensed under the GNU General Public License or
+ * other free or open source software licenses.
+ *
+ * http://open-tools.net/
+ *
+ */
+if (!class_exists ('VmPlugin')) {
+	require(JPATH_VM_PLUGINS . DS . 'vmplugin.php');
+}
+
+/** Extension plugin for the "Shipping by Rules" shipping plugin for VirtueMart
+ */
+class plgVmShipmentRulesVMCouponPrice extends VmPlugin {
+	/**  Trigger to add variables to the cart values
+	  *  You can add new variables to the $cartvals array or modify existing ones. They will be directly 
+	  *  available in all rules.
+	  *  This trigger will be first called right before any rule is evaluated. In that case, $products 
+	  *  will contain all products in the cart and $cart_prices will be an arrow containing the calculated
+	  *  prices of the order.
+	  *  Please notice that this function might also be called for only a subset of products of the cart
+	  *  when the plugin evaluates a scoping function like evaluate_for_categories(...).
+	  */
+	function onVmShippingRulesGetCartValues(&$cartvals, $cart, $products, $method) {
+		// The coupon code is in general not yet set (will be set by the cart controller AFTER shipping is handled! 
+		// -> set the coupon code now from the coupon_code REQUEST variable.
+		// If the coupon is not changed, the coupon variables are already properly set in the cartPrices
+		$coupon_code = trim(vRequest::getString('coupon_code', ''));
+		if(!empty($coupon_code)){
+			$this->couponHandler(/*$cart->couponCode*/$coupon_code, $cart);
+		}
+		$cartvals['salespriceaftercoupon']= $cart->cartPrices['salesPrice'] + (isset($cart->cartPrices['salesPriceCoupon'])?$cart->cartPrices['salesPriceCoupon']:0);
+		$cartvals['amountaftercoupon']= $cart->cartPrices['salesPrice'] + (isset($cart->cartPrices['salesPriceCoupon'])?$cart->cartPrices['salesPriceCoupon']:0);
+		$cartvals['couponvalue'] = isset($cart->cartPrices['couponValue'])?$cart->cartPrices['couponValue']:0;
+		
+	}
+	
+	# As the coupon handler in calculationh.php is protected, we need to 
+	# duplicate all code here instead of calling the VM coupon handler directly....
+	protected function couponHandler($_code, &$cart) {
+		JPluginHelper::importPlugin('vmcoupon');
+		$dispatcher = JDispatcher::getInstance();
+		$returnValues = $dispatcher->trigger('plgVmCouponHandler', array($cart->couponCode, &$cart->cartData, &$cart->cartPrices));
+		if(!empty($returnValues)){
+			foreach ($returnValues as $returnValue) {
+				if ($returnValue !== null  ) {
+					return $returnValue;
+				}
+			}
+		}
+
+		if (!class_exists('CouponHelper'))
+			require(VMPATH_SITE . DS . 'helpers' . DS . 'coupon.php');
+		if (!($_data = CouponHelper::getCouponDetails($_code))) {
+			return; // TODO give some error here
+		}
+
+		$_value_is_total = ($_data->percent_or_total == 'total');
+
+		$cart->cartPrices['salesPriceCoupon'] = ($_value_is_total ? $_data->coupon_value * -1 : ($cart->cartPrices['salesPrice'] * ($_data->coupon_value / 100)) * -1);
+		$cart->cartPrices['couponTax'] = 0;
+		$cart->cartPrices['couponValue'] = $cart->cartPrices['salesPriceCoupon'] - $cart->cartPrices['couponTax'];
+
+	}
+	
+}
+
+// No closing tag
diff --git a/plugins/vmcouponprice/vmcouponprice.script.php b/plugins/vmcouponprice/vmcouponprice.script.php
new file mode 100644
index 0000000..3671a9f
--- /dev/null
+++ b/plugins/vmcouponprice/vmcouponprice.script.php
@@ -0,0 +1,88 @@
+<?php
+defined('_JEXEC') or die('Restricted access');
+
+/**
+ * Installation script for the plugin
+ *
+ * @copyright Copyright (C) 2014 Reinhold Kainhofer, office@open-tools.net
+ * @license GPL v3+,  http://www.gnu.org/copyleft/gpl.html 
+ */
+
+// Adjust the class name. It has to be of the form:
+//     plgVmShipmentVMCouponPriceInstallerScript
+class plgVmShipmentRulesVMCouponPriceInstallerScript
+{
+    /**
+     * Constructor
+     *
+     * @param   JAdapterInstance  $adapter  The object responsible for running this script
+     */
+//     public function __constructor(JAdapterInstance $adapter);
+ 
+    /**
+     * Called before any type of action
+     *
+     * @param   string  $route  Which action is happening (install|uninstall|discover_install)
+     * @param   JAdapterInstance  $adapter  The object responsible for running this script
+     *
+     * @return  boolean  True on success
+     */
+//     public function preflight($route, JAdapterInstance $adapter);
+ 
+    /**
+     * Called after any type of action
+     *
+     * @param   string  $route  Which action is happening (install|uninstall|discover_install)
+     * @param   JAdapterInstance  $adapter  The object responsible for running this script
+     *
+     * @return  boolean  True on success
+     */
+//     public function postflight($route, JAdapterInstance $adapter);
+ 
+    /**
+     * Called on installation
+     *
+     * @param   JAdapterInstance  $adapter  The object responsible for running this script
+     *
+     * @return  boolean  True on success
+     */
+    public function install(JAdapterInstance $adapter)
+    {
+        // enabling plugin upon installation
+        $db = JFactory::getDBO();
+        $db->setQuery('update #__extensions set enabled = 1 where type = "plugin" and element = "vmcouponprice" and folder = "vmshipmentrules"');
+        $db->query();
+        
+        return True;
+    }
+ 
+    /**
+     * Called on update
+     *
+     * @param   JAdapterInstance  $adapter  The object responsible for running this script
+     *
+     * @return  boolean  True on success
+     */
+//     public function update(JAdapterInstance $adapter)
+//     {
+//         jimport( 'joomla.filesystem.file' ); 
+//         $file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "en-GB" . DS . "en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini";
+//         if (JFile::exists($file)) JFile::delete($file); 
+//         $file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "de-DE" . DS . "de-DE.plg_vmshipmentrules_vmcouponprice.sys.ini"; 
+//         if (JFile::exists($file)) JFile::delete($file); 
+//         return true;
+//     }
+ 
+    /**
+     * Called on uninstallation
+     *
+     * @param   JAdapterInstance  $adapter  The object responsible for running this script
+     */
+//     public function uninstall(JAdapterInstance $adapter)
+//     {
+//         // Remove plugin table
+//         $db =& JFactory::getDBO();
+//         $db->setQuery('DROP TABLE IF EXISTS `#__virtuemart_vmshipmentrulesles_vmcouponprice`;');
+//         $db->query();
+//     }
+}
diff --git a/plugins/vmcouponprice/vmcouponprice.xml b/plugins/vmcouponprice/vmcouponprice.xml
new file mode 100644
index 0000000..1f3a8e2
--- /dev/null
+++ b/plugins/vmcouponprice/vmcouponprice.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<extension version="2.5" type="plugin" group="vmshipmentrules" method="upgrade">
+    <name>VM Shipping by Rules - Coupon handler</name>
+    <creationDate>2017-01-04</creationDate>
+    <author>Reinhold Kainhofer, Open Tools</author>
+    <authorUrl>http://www.open-tools.net</authorUrl>
+    <copyright>Copyright (C) 2017, Reinhold Kainhofer</copyright>
+    <license>GPL v3+</license>
+    <version>1.0</version>
+    <description>VMSHIPMENTRULES_VMCOUPONPRICE_DESC</description>
+    <files>
+        <filename plugin="vmcouponprice">vmcouponprice.php</filename>
+        <folder>language</folder>
+    </files>
+    <languages folder="language">
+        <language tag="en-GB">en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.ini</language>
+        <language tag="en-GB">en-GB/en-GB.plg_vmshipmentrules_vmcouponprice.sys.ini</language>
+    </languages>
+    <scriptfile>vmcouponprice.script.php</scriptfile>
+
+    <!-- VM 3.x support (fields rather than params): -->
+    <vmconfig></vmconfig>
+
+    <!-- VM 2.0 support (params rather than fields): -->
+    <params></params>
+
+</extension>
-- 
GitLab