コード例 #1
0
ファイル: view.json.php プロジェクト: lenard112/cms
 function display($tpl = null)
 {
     $db = JFactory::getDBO();
     if ($virtuemart_media_id = vRequest::getInt('virtuemart_media_id')) {
         //$db = JFactory::getDBO();
         $query = 'SELECT `file_url`,`file_title` FROM `#__virtuemart_medias` where `virtuemart_media_id`=' . $virtuemart_media_id;
         $db->setQuery($query);
         $json = $db->loadObject();
         if (isset($json->file_url)) {
             $json->file_url = JURI::root() . $json->file_url;
             $json->msg = 'OK';
             echo json_encode($json);
         } else {
             $json->msg = '<b>' . vmText::_('COM_VIRTUEMART_NO_IMAGE_SET') . '</b>';
             echo json_encode($json);
         }
     } elseif ($custom_jplugin_id = vRequest::getInt('custom_jplugin_id')) {
         $table = '#__extensions';
         $ext_id = 'extension_id';
         $q = 'SELECT `params`,`element` FROM `' . $table . '` WHERE `' . $ext_id . '` = "' . $custom_jplugin_id . '"';
         $db->setQuery($q);
         $this->jCustom = $db->loadObject();
         $customModel = VmModel::getModel('custom');
         $this->custom = $customModel->getCustom();
         // Get the payment XML.
         $formFile = JPath::clean(JPATH_ROOT . DS . 'plugins' . DS . 'vmcustom' . DS . $this->jCustom->element . DS . $this->jCustom->element . '.xml');
         if (file_exists($formFile)) {
             VmConfig::loadJLang('plg_vmpsplugin', false);
             if (!class_exists('vmPlugin')) {
                 require JPATH_VM_PLUGINS . DS . 'vmplugin.php';
             }
             $filename = 'plg_vmcustom_' . $this->jCustom->element;
             vmPlugin::loadJLang($filename, 'vmcustom', $this->jCustom->element);
             $this->custom = VmModel::getModel('custom')->getCustom();
             $varsToPush = vmPlugin::getVarsToPushByXML($formFile, 'customForm');
             $this->custom->form = JForm::getInstance($this->jCustom->element, $formFile, array(), false, '//vmconfig | //config[not(//vmconfig)]');
             $this->custom->params = new stdClass();
             foreach ($varsToPush as $k => $field) {
                 if (strpos($k, '_') != 0) {
                     $this->custom->params->{$k} = $field[0];
                 }
             }
             $this->custom->form->bind($this->custom);
             $form = $this->custom->form;
             include JPATH_VM_ADMINISTRATOR . DS . 'fields' . DS . 'formrenderer.php';
             echo '<input type="hidden" value="' . $this->jCustom->element . '" name="custom_value">';
         } else {
             $this->custom->form = null;
             VmConfig::$echoDebug = 1;
             vmdebug('File does not exist ' . $formFile);
         }
     }
     jExit();
 }
コード例 #2
0
ファイル: cart.php プロジェクト: romuland/khparts
 /**
  * Add a product to the cart
  *
  * @author RolandD
  * @author Max Milbers
  * @access public
  */
 public function add($virtuemart_product_ids = null, &$errorMsg = '')
 {
     $mainframe = JFactory::getApplication();
     $success = false;
     $post = JRequest::get('default');
     if (empty($virtuemart_product_ids)) {
         $virtuemart_product_ids = JRequest::getVar('virtuemart_product_id', array(), 'default', 'array');
         //is sanitized then
     }
     if (empty($virtuemart_product_ids)) {
         $mainframe->enqueueMessage(JText::_('COM_VIRTUEMART_CART_ERROR_NO_PRODUCT_IDS', false));
         return false;
     }
     //Iterate through the prod_id's and perform an add to cart for each one
     foreach ($virtuemart_product_ids as $p_key => $virtuemart_product_id) {
         $quantityPost = (int) $post['quantity'][$p_key];
         if ($quantityPost === 0) {
             continue;
         }
         $tmpProduct = $this->getProduct((int) $virtuemart_product_id, $quantityPost);
         //			dump($tmpProduct,'my product add to cart before');
         // trying to save some space in the session table
         $product = new stdClass();
         $product->virtuemart_manufacturer_id = $tmpProduct->virtuemart_manufacturer_id;
         // 			$product -> mf_name = $tmpProduct -> mf_name;
         $product->slug = $tmpProduct->slug;
         // 			$product -> mf_desc = $tmpProduct -> mf_desc;
         // 			$product -> mf_url = $tmpProduct -> mf_url;
         $product->published = $tmpProduct->published;
         $product->virtuemart_product_price_id = $tmpProduct->virtuemart_product_price_id;
         $product->virtuemart_product_id = $tmpProduct->virtuemart_product_id;
         $product->virtuemart_shoppergroup_id = $tmpProduct->virtuemart_shoppergroup_id;
         $product->product_price = $tmpProduct->product_price;
         $product->override = $tmpProduct->override;
         $product->product_override_price = $tmpProduct->product_override_price;
         $product->product_tax_id = $tmpProduct->product_tax_id;
         $product->product_discount_id = $tmpProduct->product_discount_id;
         $product->product_currency = $tmpProduct->product_currency;
         // 			$product -> product_price_vdate = $tmpProduct -> product_price_vdate;
         // 			$product -> product_price_edate = $tmpProduct -> product_price_edate;
         $product->virtuemart_vendor_id = $tmpProduct->virtuemart_vendor_id;
         $product->product_parent_id = $tmpProduct->product_parent_id;
         $product->product_sku = $tmpProduct->product_sku;
         $product->product_name = $tmpProduct->product_name;
         $product->product_s_desc = $tmpProduct->product_s_desc;
         $product->product_weight = $tmpProduct->product_weight;
         $product->product_weight_uom = $tmpProduct->product_weight_uom;
         $product->product_length = $tmpProduct->product_length;
         $product->product_width = $tmpProduct->product_width;
         $product->product_height = $tmpProduct->product_height;
         $product->product_lwh_uom = $tmpProduct->product_lwh_uom;
         $product->product_in_stock = $tmpProduct->product_in_stock;
         $product->product_ordered = $tmpProduct->product_ordered;
         $product->product_sales = $tmpProduct->product_sales;
         $product->product_unit = $tmpProduct->product_unit;
         $product->product_packaging = $tmpProduct->product_packaging;
         $product->min_order_level = $tmpProduct->min_order_level;
         $product->max_order_level = $tmpProduct->max_order_level;
         $product->virtuemart_media_id = $tmpProduct->virtuemart_media_id;
         if (!empty($tmpProduct->images)) {
             $product->image = $tmpProduct->images[0];
         }
         $product->categories = $tmpProduct->categories;
         $product->virtuemart_category_id = $tmpProduct->virtuemart_category_id;
         $product->category_name = $tmpProduct->category_name;
         $product->link = $tmpProduct->link;
         $product->packaging = $tmpProduct->packaging;
         //$product -> customfields = empty($tmpProduct -> customfields)? array():$tmpProduct -> customfields ;
         //$product -> customfieldsCart = empty($tmpProduct -> customfieldsCart)? array(): $tmpProduct -> customfieldsCart;
         if (!empty($tmpProduct->customfieldsCart)) {
             $product->customfieldsCart = true;
         }
         //$product -> customsChilds = empty($tmpProduct -> customsChilds)? array(): $tmpProduct -> customsChilds;
         //Why reloading the product wiht same name $product ?
         // passed all from $tmpProduct and relaoding it second time ????
         // $tmpProduct = $this->getProduct((int) $virtuemart_product_id); seee before !!!
         // $product = $this->getProduct((int) $virtuemart_product_id);
         // Who ever noted that, yes that is exactly right that way, before we have a full object, with all functions
         // of all its parents, we only need the data of the product, so we create a dummy class which contains only the data
         // This is extremly important for performance reasons, else the sessions becomes too big.
         // Check if we have a product
         if ($product) {
             if (!empty($post['virtuemart_category_id'][$p_key])) {
                 $virtuemart_category_idPost = (int) $post['virtuemart_category_id'][$p_key];
                 $product->virtuemart_category_id = $virtuemart_category_idPost;
             }
             $productKey = $product->virtuemart_product_id;
             // INDEX NOT FOUND IN JSON HERE
             // changed name field you know exactly was this is
             if (isset($post['customPrice'])) {
                 $product->customPrices = $post['customPrice'];
                 if (isset($post['customPlugin'])) {
                     if (!class_exists('vmFilter')) {
                         require JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'vmfilter.php';
                     }
                     if (!is_array($post['customPlugin'])) {
                         $customPluginPost = (array) $post['customPlugin'];
                     } else {
                         $customPluginPost = $post['customPlugin'];
                     }
                     VmConfig::$echoDebug = TRUE;
                     foreach ($customPluginPost as &$customPlugin) {
                         if (is_array($customPlugin)) {
                             foreach ($customPlugin as &$customPlug) {
                                 if (is_array($customPlug)) {
                                     foreach ($customPlug as &$customPl) {
                                         $value = vmFilter::hl($customPl, array('deny_attribute' => '*'));
                                         $value = preg_replace('@<[\\/\\!]*?[^<>]*?>@si', '', $value);
                                         //remove all html tags
                                         $value = (string) preg_replace('#on[a-z](.+?)\\)#si', '', $value);
                                         //replace start of script onclick() onload()...
                                         $value = trim(str_replace('"', ' ', $value), "'");
                                         $customPl = (string) preg_replace('#^\'#si', '', $value);
                                     }
                                 }
                             }
                         }
                     }
                     $product->customPlugin = json_encode($customPluginPost);
                 }
                 $productKey .= '::';
                 foreach ($product->customPrices as $customPrice) {
                     foreach ($customPrice as $customId => $custom_fieldId) {
                         //MarkerVarMods
                         if (is_array($custom_fieldId)) {
                             foreach ($custom_fieldId as $userfieldId => $userfield) {
                                 //$productKey .= (int)$customId . ':' . (int)$userfieldId . ';';
                                 $productKey .= (int) $custom_fieldId . ':' . (int) $customId . ';';
                             }
                         } else {
                             //TODO productCartId
                             $productKey .= (int) $custom_fieldId . ':' . (int) $customId . ';';
                         }
                     }
                 }
             }
             // Add in the quantity in case the customfield plugins need it
             $product->quantity = (int) $quantityPost;
             if (!class_exists('vmCustomPlugin')) {
                 require JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php';
             }
             JPluginHelper::importPlugin('vmcustom');
             $dispatcher = JDispatcher::getInstance();
             // on returning false the product have not to be added to cart
             $addToCartReturnValues = $dispatcher->trigger('plgVmOnAddToCart', array(&$product));
             foreach ($addToCartReturnValues as $returnValue) {
                 if ($returnValue === false) {
                     continue 2;
                 }
             }
             if (array_key_exists($productKey, $this->products) && empty($product->customPlugin)) {
                 $errorMsg = JText::_('COM_VIRTUEMART_CART_PRODUCT_UPDATED');
                 $totalQuantity = $this->products[$productKey]->quantity + $quantityPost;
                 if ($this->checkForQuantities($product, $totalQuantity, $errorMsg)) {
                     $this->products[$productKey]->quantity = $totalQuantity;
                 } else {
                     continue;
                 }
             } else {
                 if (!empty($product->customPlugin)) {
                     $productKey .= count($this->products);
                 }
                 if ($this->checkForQuantities($product, $quantityPost, $errorMsg)) {
                     $this->products[$productKey] = $product;
                     $product->quantity = $quantityPost;
                     //$mainframe->enqueueMessage(JText::_('COM_VIRTUEMART_CART_PRODUCT_ADDED'));
                 } else {
                     // $errorMsg = JText::_('COM_VIRTUEMART_CART_PRODUCT_OUT_OF_STOCK');
                     continue;
                 }
             }
             $success = true;
         } else {
             $mainframe->enqueueMessage(JText::_('COM_VIRTUEMART_PRODUCT_NOT_FOUND', false));
             return false;
         }
     }
     if ($success == false) {
         return false;
     }
     // End Iteration through Prod id's
     $this->setCartIntoSession();
     return true;
 }
コード例 #3
0
ファイル: calculationh.php プロジェクト: lenard112/cms
 public function getCheckoutPrices(&$cart)
 {
     //vmdebug('in function getCheckoutPrices in function getCheckoutPrices');
     $this->_cart =& $cart;
     $this->inCart = TRUE;
     //$pricesPerId = array();
     $resultWithTax = 0.0;
     $resultWithOutTax = 0.0;
     $this->_cart->cartData['VatTax'] = array();
     $this->_cart->cartPrices['basePrice'] = 0;
     $this->_cart->cartPrices['basePriceWithTax'] = 0;
     $this->_cart->cartPrices['discountedPriceWithoutTax'] = 0;
     $this->_cart->cartPrices['salesPrice'] = 0;
     $this->_cart->cartPrices['taxAmount'] = 0;
     $this->_cart->cartPrices['salesPriceWithDiscount'] = 0;
     $this->_cart->cartPrices['discountAmount'] = 0;
     $this->_cart->cartPrices['priceWithoutTax'] = 0;
     $this->_cart->cartPrices['subTotalProducts'] = 0;
     $this->_cart->cartPrices['billTotal'] = 0;
     $this->_cart->cartData['duty'] = 1;
     $this->_cart->cartData['payment'] = 0;
     //could be automatically set to a default set in the globalconfig
     $this->_cart->cartData['paymentName'] = '';
     $cartpaymentTax = 0;
     $this->setCountryState();
     $this->_amountCart = 0;
     //$this->_cart->cartData['totalProduct'] = count($this->_cart->products);
     $customfieldModel = VmModel::getModel('customfields');
     //vmdebug('my variant mod',$this->_cart->products);
     foreach ($this->_cart->products as $cprdkey => $productCart) {
         //$product = $productModel->getProduct($product->virtuemart_product_id,false,false,true);
         //$productId = $product->virtuemart_product_id;
         if (empty($this->_cart->products[$cprdkey]->quantity) || empty($this->_cart->products[$cprdkey]->virtuemart_product_id)) {
             if (!is_object($this->_cart->products[$cprdkey])) {
                 //vmError( 'Error the product for calculation is not an object');
                 //vmdebug( 'Error the product for calculation is not an object',$product);
             } else {
                 vmError('Error the quantity of the product for calculation is 0, please notify the shopowner, the product id ' . $this->_cart->products[$cprdkey]->virtuemart_product_id);
             }
             continue;
         }
         $this->productCurrency = isset($this->_cart->products[$cprdkey]->product_currency) ? $this->_cart->products[$cprdkey]->product_currency : 0;
         VmConfig::$echoDebug = true;
         $variantmod = $customfieldModel->calculateModificators($this->_cart->products[$cprdkey]);
         //vmdebug('get cart prices '.$cprdkey,$variantmod);
         //$product->allPrices = $pricesPerId[$cprdkey] = $this->getProductPrices($product, $variantmod, $product->quantity, true, false);
         $productPrice = $this->getProductPrices($this->_cart->products[$cprdkey], $variantmod, $this->_cart->products[$cprdkey]->quantity);
         $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice] = $productPrice;
         $this->_cart->cartPrices[$cprdkey] = $productPrice;
         //$this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice];
         $this->_amountCart += $this->_cart->products[$cprdkey]->quantity;
         VmConfig::$echoDebug = false;
         if ($this->_currencyDisplay->_priceConfig['basePrice']) {
             $this->_cart->cartPrices['basePrice'] += self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['basePrice'], 'basePrice') * $this->_cart->products[$cprdkey]->quantity;
         }
         //				$this->_cart->cartPrices['basePriceVariant'] = $this->_cart->cartPrices['basePriceVariant'] + $pricesPerId[$product->virtuemart_product_id]['basePriceVariant']*$product->quantity;
         if ($this->_currencyDisplay->_priceConfig['basePriceWithTax']) {
             $this->_cart->cartPrices['basePriceWithTax'] += self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['basePriceWithTax']) * $this->_cart->products[$cprdkey]->quantity;
         }
         if ($this->_currencyDisplay->_priceConfig['discountedPriceWithoutTax']) {
             $this->_cart->cartPrices['discountedPriceWithoutTax'] += self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['discountedPriceWithoutTax'], 'discountedPriceWithoutTax') * $this->_cart->products[$cprdkey]->quantity;
         }
         if ($this->_currencyDisplay->_priceConfig['salesPrice']) {
             //				$this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['salesPrice'],'salesPrice') * $this->_cart->products[$cprdkey]->quantity;
             $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal_with_tax'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['salesPrice'], 'salesPrice') * $this->_cart->products[$cprdkey]->quantity;
             //				$this->_cart->cartPrices['salesPrice'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
             $this->_cart->cartPrices['salesPrice'] += $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal_with_tax'];
         }
         if ($this->_currencyDisplay->_priceConfig['taxAmount']) {
             //				$this->_cart->cartPrices[$cprdkey]['subtotal_tax_amount'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['taxAmount'],'taxAmount') * $this->_cart->products[$cprdkey]->quantity;
             $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal_tax_amount'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['taxAmount'], 'taxAmount') * $this->_cart->products[$cprdkey]->quantity;
             //				$this->_cart->cartPrices['taxAmount'] += $this->_cart->cartPrices[$cprdkey]['subtotal_tax_amount'];
             $this->_cart->cartPrices['taxAmount'] += $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal_tax_amount'];
         }
         if ($this->_currencyDisplay->_priceConfig['salesPriceWithDiscount']) {
             $this->_cart->cartPrices['salesPriceWithDiscount'] += self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['salesPriceWithDiscount'], 'salesPriceWithDiscount') * $this->_cart->products[$cprdkey]->quantity;
         }
         if ($this->_currencyDisplay->_priceConfig['discountAmount']) {
             //				$this->_cart->cartPrices[$cprdkey]['subtotal_discount'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['discountAmount'],'discountAmount') * $this->_cart->products[$cprdkey]->quantity;
             $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal_discount'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['discountAmount'], 'discountAmount') * $this->_cart->products[$cprdkey]->quantity;
             //				$this->_cart->cartPrices['discountAmount'] += $this->_cart->cartPrices[$cprdkey]['subtotal_discount'];
             $this->_cart->cartPrices['discountAmount'] += $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal_discount'];
         }
         if ($this->_currencyDisplay->_priceConfig['priceWithoutTax']) {
             //				$this->_cart->cartPrices[$cprdkey]['subtotal'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['priceWithoutTax'],'priceWithoutTax') * $this->_cart->products[$cprdkey]->quantity;
             $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal'] = self::roundInternal($this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['priceWithoutTax'], 'priceWithoutTax') * $this->_cart->products[$cprdkey]->quantity;
             //				$this->_cart->cartPrices['priceWithoutTax'] += $this->_cart->cartPrices[$cprdkey]['subtotal'];
             $this->_cart->cartPrices['priceWithoutTax'] += $this->_cart->products[$cprdkey]->allPrices[$this->_cart->products[$cprdkey]->selectedPrice]['subtotal'];
         }
     }
     $this->_product = null;
     $this->_cart->cartData['DBTaxRulesBill'] = $this->gatherEffectingRulesForBill('DBTaxBill');
     $this->_cart->cartData['taxRulesBill'] = $this->gatherEffectingRulesForBill('TaxBill');
     $this->_cart->cartData['DATaxRulesBill'] = $this->gatherEffectingRulesForBill('DATaxBill');
     $this->_cart->cartPrices['salesPriceDBT'] = array();
     $this->_cart->cartPrices['taxRulesBill'] = array();
     $this->_cart->cartPrices['DATaxRulesBill'] = array();
     foreach ($this->_cart->products as $cprdkey => $product) {
         //for Rules with Categories
         foreach ($this->_cart->cartData['DBTaxRulesBill'] as &$dbrule) {
             if (!empty($dbrule['calc_categories'])) {
                 if (!isset($dbrule['subTotal'])) {
                     $dbrule['subTotal'] = 0.0;
                 }
                 $set = array_intersect($dbrule['calc_categories'], $product->categories);
                 if (count($set) > 0) {
                     //foreach($set as $s){
                     $dbrule['subTotal'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                     vmdebug('DB Rule ' . $dbrule['calc_name'] . ' is per category subTotal ' . $dbrule['subTotal']);
                     // subarray with subTotal for each taxID necessary to calculate tax correct if there are more than one VatTaxes
                     if (!isset($dbrule['subTotalPerTaxID'])) {
                         $dbrule['subTotalPerTaxID'] = array();
                     }
                     if ($product->product_tax_id != 0) {
                         if (!isset($dbrule['subTotalPerTaxID'][$product->product_tax_id])) {
                             $dbrule['subTotalPerTaxID'][$product->product_tax_id] = 0.0;
                         }
                         $dbrule['subTotalPerTaxID'][$product->product_tax_id] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                     } else {
                         foreach ($this->allrules[$product->virtuemart_vendor_id]['VatTax'] as $virtuemart_calc_id => $rule) {
                             $set = array_intersect($rule['cats'], $product->categories);
                             if (count($set) > 0) {
                                 if (!isset($dbrule['subTotalPerTaxID'][$virtuemart_calc_id])) {
                                     $dbrule['subTotalPerTaxID'][$virtuemart_calc_id] = 0.0;
                                 }
                                 $dbrule['subTotalPerTaxID'][$virtuemart_calc_id] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                             }
                         }
                     }
                     //}
                 }
             }
         }
         // subTotal for each taxID necessary, equal if calc_categories exists ore not
         if (!empty($this->_cart->cartData['taxRulesBill'])) {
             foreach ($this->_cart->cartData['taxRulesBill'] as $k => &$trule) {
                 if (!isset($trule['subTotal'])) {
                     $trule['subTotal'] = 0.0;
                 }
                 if ($product->product_tax_id != 0) {
                     if ($product->product_tax_id == $k) {
                         $trule['subTotal'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                     }
                 } elseif (!empty($trule['calc_categories'])) {
                     $set = array_intersect($trule['calc_categories'], $product->categories);
                     if (count($set) > 0) {
                         $trule['subTotal'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                         vmdebug('DB Rule ' . $trule['calc_name'] . ' is per category subTotal ' . $trule['subTotal']);
                     }
                 } else {
                     $trule['subTotal'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                 }
             }
         }
         /*
         foreach($this->_cart->cartData['taxRulesBill'] as $k=>&$trule){
         	if(!empty($trule['calc_categories'])){
         		if(!isset($trule['subTotal'])) $trule['subTotal'] = 0.0;
         		$set = array_intersect($trule['calc_categories'],$product->categories);
         		if(count($set)>0){
         			//foreach($set as $s){
         				$trule['subTotal'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
         				vmdebug('DB Rule '.$trule['calc_name'].' is per category subTotal '.$trule['subTotal']);
         			//}
         		}
         	}
         }
         */
         foreach ($this->_cart->cartData['DATaxRulesBill'] as &$darule) {
             if (!empty($darule['calc_categories'])) {
                 if (!isset($darule['subTotal'])) {
                     $darule['subTotal'] = 0.0;
                 }
                 $set = array_intersect($darule['calc_categories'], $product->categories);
                 if (count($set) > 0) {
                     if (!isset($darule['subTotal'])) {
                         $darule['subTotal'] = 0.0;
                     }
                     //foreach($set as $s){
                     $darule['subTotal'] += $this->_cart->cartPrices[$cprdkey]['subtotal_with_tax'];
                     //}
                 }
             }
         }
     }
     // Calculate the discount from all rules before tax to calculate billTotal
     $cartdiscountBeforeTax = $this->roundInternal($this->cartRuleCalculation($this->_cart->cartData['DBTaxRulesBill'], $this->_cart->cartPrices['salesPrice']));
     // We need the discount per category for each taxID to reduce the total discount before calculate percentage from hole cart discounts
     $categorydiscountBeforeTax = 0;
     foreach ($this->_cart->cartData['DBTaxRulesBill'] as &$rule) {
         if (!empty($rule['subTotalPerTaxID'])) {
             foreach ($rule['subTotalPerTaxID'] as $k => $DBTax) {
                 $this->roundInternal($this->cartRuleCalculation($this->_cart->cartData['DBTaxRulesBill'], $this->_cart->cartPrices['salesPrice'], $k, true));
                 if (!empty($this->_cart->cartData['VatTax'][$k]['DBTax'][$rule['virtuemart_calc_id'] . 'DBTax'])) {
                     $categorydiscountBeforeTax += $this->_cart->cartData['VatTax'][$k]['DBTax'][$rule['virtuemart_calc_id'] . 'DBTax'];
                 }
                 //			vmdebug('$categorydiscountBeforeTax',$categorydiscountBeforeTax);
             }
         }
     }
     // combine the discounts before tax for each taxID
     foreach ($this->_cart->cartData['VatTax'] as &$rule) {
         if (!empty($rule['DBTax'])) {
             $sum = 0;
             foreach ($rule['DBTax'] as $key => $val) {
                 $sum += $val;
             }
             $rule['DBTax'] = $sum;
         }
     }
     // calculate the new subTotal with discounts before tax, necessary for billTotal
     $toTax = $this->_cart->cartPrices['salesPrice'] + $cartdiscountBeforeTax;
     //Avalara wants to calculate the tax of the shipment. Only disadvantage to set shipping here is that the discounts per bill respectivly the tax per bill
     // is not considered.
     $this->calculateShipmentPrice();
     // next step is handling a coupon, if given
     $this->_cart->cartData['vmVat'] = TRUE;
     $this->_cart->cartPrices['salesPriceCoupon'] = 0.0;
     if (!empty($this->_cart->couponCode)) {
         $this->couponHandler($this->_cart->couponCode);
     }
     // now calculate the discount for hole cart and reduce subTotal for each taxRulesBill, to calculate correct tax, also if there are more than one tax rules
     $totalDiscountBeforeTax = $cartdiscountBeforeTax - $categorydiscountBeforeTax + $this->_cart->cartPrices['salesPriceCoupon'];
     foreach ($this->_cart->cartData['taxRulesBill'] as $k => &$rule) {
         if (!empty($rule['subTotal'])) {
             $rule['percentage'] = $rule['subTotal'] / $this->_cart->cartPrices['salesPrice'];
             if (isset($this->_cart->cartData['VatTax'][$k]['DBTax'])) {
                 $rule['subTotal'] += $this->_cart->cartData['VatTax'][$k]['DBTax'];
             }
             $rule['subTotal'] += $totalDiscountBeforeTax * $rule['percentage'];
         }
     }
     // now each taxRule subTotal is reduced with DBTax and we can calculate the cartTax
     $cartTax = $this->roundInternal($this->cartRuleCalculation($this->_cart->cartData['taxRulesBill'], $toTax));
     // toDisc is new subTotal after tax, now it comes discount afterTax and we can calculate the final cart price with tax.
     $toDisc = $toTax + $cartTax;
     $cartdiscountAfterTax = $this->roundInternal($this->cartRuleCalculation($this->_cart->cartData['DATaxRulesBill'], $toDisc));
     $this->_cart->cartPrices['withTax'] = $toDisc + $cartdiscountAfterTax;
     $this->calculatePaymentPrice();
     //		$sub =!empty($this->_cart->cartPrices['discountedPriceWithoutTax'])? $this->_cart->cartPrices['discountedPriceWithoutTax']:$this->_cart->cartPrices['basePrice'];
     if ($this->_currencyDisplay->_priceConfig['salesPrice']) {
         $this->_cart->cartPrices['billSub'] = $this->_cart->cartPrices['basePrice'] + $this->_cart->cartPrices['shipmentValue'] + $this->_cart->cartPrices['paymentValue'];
     }
     //		$this->_cart->cartPrices['billSub']  = $sub + $this->_cart->cartPrices['shipmentValue'] + $this->_cart->cartPrices['paymentValue'];
     if ($this->_currencyDisplay->_priceConfig['discountAmount']) {
         $this->_cart->cartPrices['billDiscountAmount'] = $this->_cart->cartPrices['discountAmount'] + $cartdiscountBeforeTax + $cartdiscountAfterTax;
     }
     // + $this->_cart->cartPrices['shipmentValue'] + $this->_cart->cartPrices['paymentValue'] ;
     if ($this->_cart->cartPrices['salesPriceShipment'] < 0) {
         $this->_cart->cartPrices['billDiscountAmount'] += $this->_cart->cartPrices['salesPriceShipment'];
     }
     if ($this->_cart->cartPrices['salesPricePayment'] < 0) {
         $this->_cart->cartPrices['billDiscountAmount'] += $this->_cart->cartPrices['salesPricePayment'];
     }
     if ($this->_currencyDisplay->_priceConfig['taxAmount']) {
         $this->_cart->cartPrices['billTaxAmount'] = $this->_cart->cartPrices['taxAmount'] + $this->_cart->cartPrices['shipmentTax'] + $this->_cart->cartPrices['paymentTax'] + $cartTax;
     }
     //+ $this->_cart->cartPrices['withTax'] - $toTax
     //The coupon handling is only necessary if a salesPrice is displayed, otherwise we have a kind of catalogue mode
     if ($this->_currencyDisplay->_priceConfig['salesPrice']) {
         $this->_cart->cartPrices['billTotal'] = $this->_cart->cartPrices['salesPriceShipment'] + $this->_cart->cartPrices['salesPricePayment'] + $this->_cart->cartPrices['withTax'] + $this->_cart->cartPrices['salesPriceCoupon'];
         if (empty($this->_cart->cartPrices['billTotal']) or $this->_cart->cartPrices['billTotal'] < 0) {
             $this->_cart->cartPrices['billTotal'] = 0.0;
         }
         if ($this->_cart->cartData['vmVat'] and (!empty($cartdiscountBeforeTax) and isset($this->_cart->cartData['VatTax']) and count($this->_cart->cartData['VatTax']) > 0) or !empty($this->_cart->couponCode)) {
             //$this->_revert = true;
             $allTotalTax = 0.0;
             $totalDiscount = $cartdiscountBeforeTax - $categorydiscountBeforeTax + $this->_cart->cartPrices['salesPriceCoupon'];
             //	vmdebug(' salesPriceCoupon = '. $this->_cart->cartPrices['salesPriceCoupon'].'     billDiscountAmount = '.$this->_cart->cartPrices['billDiscountAmount']);
             foreach ($this->_cart->cartData['VatTax'] as &$vattax) {
                 //$vattax['DBTax'] = var_dump(array_sum($vattax['DBTax']));
                 if (isset($vattax['subTotal'])) {
                     $vattax['percentage'] = $vattax['subTotal'] / $this->_cart->cartPrices['salesPrice'];
                 }
                 $vattax['DBTax'] = isset($vattax['DBTax']) ? $vattax['DBTax'] : 0;
                 if (isset($vattax['calc_value']) && isset($vattax['percentage'])) {
                     $vattax['discountTaxAmount'] = round(($totalDiscount * $vattax['percentage'] + $vattax['DBTax']) / (100 + $vattax['calc_value']) * $vattax['calc_value'], $this->_currencyDisplay->_priceConfig['taxAmount'][1]);
                 }
                 //$vattax['subTotal'] = $vattax['subTotal'] - $vattax['percentage'] * $totalDiscount;
                 if (isset($vattax['discountTaxAmount'])) {
                     $this->_cart->cartPrices['billTaxAmount'] += $vattax['discountTaxAmount'];
                 }
                 $allTotalTax += $totalDiscount;
                 //$this->_cart->cartPrices['billTaxAmount'] += $vattax['subTotal'];
                 //vmdebug('my vattax recalc data the percentage = '.$vattax['percentage'].'  salesPrice = '.$this->_cart->cartPrices['salesPrice'].'  $totalDiscount = '. $totalDiscount.'  subtotal = '.$vattax['subTotal']);
             }
         }
         if ($this->_cart->cartPrices['billTaxAmount'] < 0) {
             $this->_cart->cartPrices['billTaxAmount'] = 0.0;
         }
     }
     //Calculate VatTax result
     if ($this->_cart->cartPrices['shipment_calc_id']) {
         foreach ($this->_cart->cartPrices['shipment_calc_id'] as $calcID) {
             if (isset($this->_cart->cartPrices['shipmentTaxPerID'][$calcID])) {
                 $this->_cart->cartData['VatTax'][$calcID]['shipmentTax'] = $this->_cart->cartPrices['shipmentTaxPerID'][$calcID];
             }
         }
     }
     if ($this->_cart->cartPrices['payment_calc_id']) {
         foreach ($this->_cart->cartPrices['payment_calc_id'] as $calcID) {
             if (isset($this->_cart->cartPrices['shipmentTaxPerID'][$calcID])) {
                 $this->_cart->cartData['VatTax'][$calcID]['paymentTax'] = $this->_cart->cartPrices['paymentTaxPerID'][$calcID];
             }
         }
     }
     foreach ($this->_cart->cartData['VatTax'] as $k => &$vattax) {
         $vattax['result'] = isset($vattax['taxAmount']) ? $vattax['taxAmount'] : 0;
         if (isset($vattax['discountTaxAmount'])) {
             $vattax['result'] += $vattax['discountTaxAmount'];
         }
         if (isset($vattax['shipmentTax'])) {
             $vattax['result'] += $vattax['shipmentTax'];
         }
         if (isset($vattax['paymentTax'])) {
             $vattax['result'] += $vattax['paymentTax'];
         }
         if (!isset($vattax['virtuemart_calc_id'])) {
             $vattax['virtuemart_calc_id'] = $this->getCalcRuleData($k)->virtuemart_calc_id;
         }
         if (!isset($vattax['calc_name'])) {
             $vattax['calc_name'] = $this->getCalcRuleData($k)->calc_name;
         }
         if (!isset($vattax['calc_value'])) {
             $vattax['calc_value'] = $this->getCalcRuleData($k)->calc_value;
         }
     }
     foreach ($this->_cart->cartData['taxRulesBill'] as $k => &$rule) {
         $this->_cart->cartData['VatTax'][$k]['result'] = isset($this->_cart->cartData['VatTax'][$k]['result']) ? $this->_cart->cartData['VatTax'][$k]['result'] : 0;
         $this->_cart->cartData['VatTax'][$k]['result'] += round($this->_cart->cartPrices[$rule['virtuemart_calc_id'] . 'Diff'], $this->_currencyDisplay->_priceConfig['salesPrice'][1]);
         if (!isset($this->_cart->cartData['VatTax'][$k]['virtuemart_calc_id'])) {
             $this->_cart->cartData['VatTax'][$k]['virtuemart_calc_id'] = $rule['virtuemart_calc_id'];
         }
         if (!isset($this->_cart->cartData['VatTax'][$k]['calc_name'])) {
             $this->_cart->cartData['VatTax'][$k]['calc_name'] = $rule['calc_name'];
         }
         if (!isset($this->_cart->cartData['VatTax'][$k]['calc_value'])) {
             $this->_cart->cartData['VatTax'][$k]['calc_value'] = $rule['calc_value'];
         }
     }
 }