/** * Calculate taxes on list of products * * @param $products Array of products * @param $source Source of tax calculation (final_price '1', product_price '2', orderitem_price '3') * @param $billing_address Actual customer's billing address * @param $shipping_address Actual customer's shipping address * @param $tax_type for the future use * * @return Associative array with indexes product_id of products with arrays with list of their tax rates (names and rates) */ public static function calculateTax($products, $source = 1, $billing_address = null, $shipping_address = null, $tax_type = null) { $result = new stdClass(); $result->tax_total = 0.0; $result->tax_rate_rates = array(); $result->tax_class_rates = array(); $result->product_taxes = array(); if (!is_array($products)) { return $result; } Tienda::load('TiendaHelperShipping', 'helpers.shipping'); Tienda::load('TiendaQuery', 'library.query'); Tienda::load('TiendaTools', 'library.tools'); if ($billing_address) { $billing_zones = TiendaHelperShipping::getGeoZones($billing_address->zone_id, '1', $billing_address->postal_code); } else { $billing_zones = array(); } if (!empty($billing_zones)) { foreach ($billing_zones as $key => $value) { $billing_zones[$key] = $value->geozone_id; } } //load the default geozones when user is logged out and the config is to show tax if (empty($billing_zones)) { $geozones = TiendaHelperUser::getGeoZones(JFactory::getUser()->id); if (empty($geozones)) { // use the default $billing_zones = array(Tienda::getInstance()->get('default_tax_geozone')); } else { foreach ($geozones as $key => $value) { $billing_zones[$key] = $value->geozone_id; } } } return TiendaHelperTax::calculateGeozonesTax($products, $source, $billing_zones); }
/** * Calculates the tax totals for the order * using the array of items in the order object * * @return unknown_type */ function calculateTaxTotals() { $config = Tienda::getInstance(); $show_tax = $config->get('display_prices_with_tax'); $calc_tax_shipping = $config->get('calc_tax_shipping'); Tienda::load("TiendaHelperTax", 'helpers.tax'); $this->_taxrate_amounts = array(); $this->_taxclass_amounts = array(); $items = $this->getItems(); $shipping_address = $this->getShippingAddress(); $billing_address = $this->getBillingAddress(); if ($calc_tax_shipping) { $taxes = TiendaHelperTax::calculateTax($items, 1, $shipping_address, $shipping_address); } else { $taxes = TiendaHelperTax::calculateTax($items, 1, $billing_address, $shipping_address); } foreach ($items as $item) { $item->orderitem_tax = $taxes->product_taxes[$item->product_id]; if ($show_tax) { $item->orderitem_final_price += $item->orderitem_tax; } } foreach (@$taxes->tax_rate_rates as $key => $value) { $applied_tax = $value->applied_tax; // add this as one of the taxrates applicable to this order if (empty($this->_taxrates[$key])) { $this->_taxrates[$key] = $value; } // track the total amount of tax applied to this order for this taxrate if (empty($this->_taxrate_amounts[$key])) { $this->_taxrate_amounts[$key] = 0; } $this->_taxrate_amounts[$key] += $applied_tax; } foreach (@$taxes->tax_class_rates as $key => $value) { $applied_tax = $value->applied_tax; // add this as one of the taxclasses applicable to this order if (empty($this->_taxclasses[$key])) { $this->_taxclasses[$key] = $value; } // track the total amount of tax applied to this order for this taxclass if (empty($this->_taxclass_amounts[$key])) { $this->_taxclass_amounts[$key] = 0; } $this->_taxclass_amounts[$key] += $applied_tax; } $this->order_tax = $taxes->tax_total; // Allow tax calculation to be modified via plugins $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger("onCalculateTaxTotals", array($this)); }
/** * * Method to check config, user group and product state (if recurs). * Then get right values accordingly * @param array $items - cart items * @param boolean - config to show tax or not * @return object */ function checkItems(&$items, $show_tax = false) { if (empty($items)) { return array(); } Tienda::load('TiendaHelperUser', 'helpers.user'); Tienda::load("TiendaHelperProduct", 'helpers.product'); Tienda::load('TiendaHelperTax', 'helpers.tax'); if ($show_tax) { $taxes = TiendaHelperTax::calculateTax($items, 1); } $subtotal = 0; foreach ($items as $item) { if ($show_tax) { $item->product_price = $item->product_price + $taxes->product_taxes[$item->product_id]; $item->taxtotal = $taxes->product_taxes[$item->product_id]; } $item->subtotal = $item->product_price * $item->product_qty; $subtotal = $subtotal + $item->subtotal; } $cartObj = new stdClass(); $cartObj->items = $items; $cartObj->subtotal = $subtotal; return $cartObj; }
public function deleteCartItem() { $response = new stdClass(); $response->html = ''; $response->error = false; $user = JFactory::getUser(); $model = $this->getModel('carts'); $table = $model->getTable(); $id = JRequest::getInt('cartitem_id'); $keys = array('user_id' => $user->id, 'cart_id' => $id); $table->load($keys); if (!empty($table->cart_id)) { if ($table->delete()) { $response->html = JText::_('COM_TIENDA_CARTITEM_DELETED'); } else { $response->html = JText::_('COM_TIENDA_DELETE_FAILED'); $response->error = true; } } else { $response->html = JText::_('COM_TIENDA_INVALID_REQUEST'); $response->error = true; } // we deleted the item so we have to recalculate the subtotal $response->subtotal = 0; if ($response->error == false) { $show_tax = $this->defines->get('display_prices_with_tax'); $model = $this->getModel($this->get('suffix')); $this->_setModelState(); $items = $model->getList(); Tienda::load('TiendaHelperUser', 'helpers.user'); Tienda::load('TiendaHelperTax', 'helpers.tax'); if ($show_tax) { $taxes = TiendaHelperTax::calculateTax($items, 2); } foreach ($items as $item) { if ($show_tax) { $item->product_price += $taxes->product_taxes[$item->product_id]; } $response->subtotal += $item->product_price * $item->product_qty; } $response->subtotal = TiendaHelperBase::currency($response->subtotal); } echo json_encode($response); return; }
/** * Prepares data for and returns the html of the order summary layout. * This assumes that $this->_order has already had its properties set * * @return unknown_type */ public function getOrderSummary($layout = 'cart') { // get the order object $order = $this->_order; // a TableOrders object (see constructor) Tienda::load('TiendaHelperCoupon', 'helpers.coupon'); // Coupons $coupons_id = array(); $coupons = $order->getCoupons(); foreach ($coupons as $cg) { foreach ($cg as $c) { if ($c->coupon_type == '1') { $coupons_id = array_merge($coupons_id, TiendaHelperCoupon::getCouponProductIds(@$c->coupon_id)); } } } $model = $this->getModel('carts'); $user = $this->user; $model->setState('filter_user', $user->id); $view = $this->getView($this->get('suffix'), 'html'); $view->set('_controller', $this->get('suffix')); $view->set('_view', $this->get('suffix')); $view->set('_doTask', true); $view->set('hidemenu', true); $view->setModel($model, true); $view->assign('state', $model->getState()); $view->assign('coupons', $coupons_id); $config = $this->defines; $show_tax = $config->get('display_prices_with_tax'); $view->assign('show_tax', $show_tax); $view->assign('using_default_geozone', false); $view->assign('order', $order); Tienda::load("TiendaHelperBase", 'helpers._base'); $product_helper = TiendaHelperBase::getInstance('Product'); $order_helper = TiendaHelperBase::getInstance('Order'); $tax_sum = 0; $orderitems = $order->getItems(); $taxes = TiendaHelperTax::calculateTax($orderitems, 4, $order->getBillingAddress(), $order->getShippingAddress()); foreach ($orderitems as $item) { $item->price = $item->orderitem_final_price / $item->orderitem_quantity; if ($show_tax) { $order->order_subtotal += $item->orderitem_tax; } } $view->assign('orderitems', $orderitems); // Checking whether shipping is required $showShipping = false; $cartsModel = $this->getModel('carts'); if ($isShippingEnabled = $cartsModel->getShippingIsEnabled()) { $showShipping = true; $view->assign('shipping_total', $order->getShippingTotal()); } $view->assign('showShipping', $showShipping); //START onDisplayOrderItem: trigger plugins for extra orderitem information if (!empty($orderitems)) { $onDisplayOrderItem = $order_helper->onDisplayOrderItems($orderitems); $view->assign('onDisplayOrderItem', $onDisplayOrderItem); } //END onDisplayOrderItem $coupons_present = false; $model = JModel::getInstance('Coupons', 'TiendaModel'); $model->setState('filter_enabled', '1'); if ($coupons = $model->getList()) { $coupons_present = true; } $view->assign('coupons_present', $coupons_present); $view->setLayout($layout); $view->setTask(true); ob_start(); $view->display(); $html = ob_get_contents(); ob_end_clean(); return $html; }
/** * Gets a product's related items * formatted for display * * @param int $address_id * @return string html */ function getRelationshipsHtml($view, $product_id, $relation_type = 'relates') { $html = ''; $validation = ""; // get the list JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/models'); $model = JModel::getInstance('ProductRelations', 'TiendaModel'); $model->setState('filter_relation', $relation_type); $user = JFactory::getUser(); $model->setState('filter_group', $relation_type); switch ($relation_type) { case "requires": $model->setState('filter_product_from', $product_id); $check_quantity = false; $layout = 'product_requirements'; break; case "parent": case "child": case "children": $model->setState('filter_product_from', $product_id); $check_quantity = true; $validation = "index.php?option=com_tienda&view=products&task=validateChildren&format=raw"; $layout = 'product_children'; break; case "relates": $model->setState('filter_product', $product_id); $check_quantity = false; $layout = 'product_relations'; break; default: return $html; break; } $query = $model->getQuery(); $query->order('p_from.ordering ASC, p_to.ordering ASC'); if ($items = $model->getList()) { $filter_category = $model->getState('filter_category', JRequest::getVar('filter_category')); if (empty($filter_category)) { $categories = Tienda::getClass('TiendaHelperProduct', 'helpers.product')->getCategories($product_id); if (!empty($categories)) { $filter_category = $categories[0]; } } $userId = JFactory::getUser()->id; $config = Tienda::getInstance(); $show_tax = $config->get('display_prices_with_tax'); Tienda::load('TiendaHelperTax', 'helpers.tax'); if ($show_tax) { $taxes = TiendaHelperTax::calculateTax($items, 2); } foreach ($items as $key => $item) { if ($check_quantity) { // TODO Unset $items[$key] if // this is out of stock && // check_inventory && // item for sale } if ($item->product_id_from == $product_id) { // display the _product_to $item->product_id = $item->product_id_to; $item->product_name = $item->product_name_to; $item->product_model = $item->product_model_to; $item->product_sku = $item->product_sku_to; $item->product_price = $item->product_price_to; } else { // display the _product_from $item->product_id = $item->product_id_from; $item->product_name = $item->product_name_from; $item->product_model = $item->product_model_from; $item->product_sku = $item->product_sku_from; $item->product_price = $item->product_price_from; } $itemid = Tienda::getClass("TiendaHelperRoute", 'helpers.route')->product($item->product_id, $filter_category, true); $item->itemid = JRequest::getInt('Itemid', $itemid); $item->tax = 0; if ($show_tax) { $tax = $taxes->product_taxes[$item->product_id]; $item->taxtotal = $tax; $item->tax = $tax; } } } else { return ''; } $view->setModel($model, true); $lyt = $view->getLayout(); $view->setLayout($layout); $product_relations = new stdClass(); $product_relations->items = $items; $product_relations->product_id = $product_id; $product_relations->show = $product_id; $product_relations->filter_category = $filter_category; $product_relations->validation = $validation; $product_relations->show_tax = $show_tax; $view->product_relations_data = $product_relations; ob_start(); $view->display(); $html = ob_get_contents(); ob_end_clean(); unset($view->product_relations_data); $view->setLayout($lyt); return $html; }
function getOrderSummary(&$order) { $model = $this->getModel('carts'); JLoader::register("TiendaViewPOS", JPATH_ADMINISTRATOR . "/components/com_tienda/views/pos/view.html.php"); $view = new TiendaViewPOS(); $view->set('_controller', 'pos'); $view->set('_view', 'pos'); $view->set('_doTask', true); $view->set('hidemenu', false); $view->setModel($model, true); $view->assign('state', $model->getState()); $config = Tienda::getInstance(); $show_tax = $config->get('display_prices_with_tax'); $view->assign('show_tax', $show_tax); $view->assign('using_default_geozone', false); $view->assign('order', $order); $orderitems = $order->getItems(); Tienda::load("TiendaHelperTax", 'helpers.tax'); if ($show_tax) { $geozones = $order->getBillingGeoZones(); if (empty($geozones)) { // use the default $view->assign('using_default_geozone', true); $geozones = array($config->get('default_tax_geozone')); } else { foreach ($geozones as $key => $value) { $geozones[$key] = $value->geozone_id; } } $taxes = TiendaHelperTax::calculateGeozonesTax($orderitems, 4, $geozones); } $product_helper = TiendaHelperBase::getInstance('Product'); $order_helper = TiendaHelperBase::getInstance('Order'); $showShipping = false; $tax_sum = 0; $notfoundShipping = true; foreach ($orderitems as &$item) { //check shipping if required if ($notfoundShipping && ($isShippingEnabled = $product_helper->isShippingEnabled($item->product_id))) { $showShipping = true; $notfoundShipping = false; } if ($show_tax) { $item->price = $item->orderitem_price + floatval($item->orderitem_attributes_price) + $taxes->product_taxes[$item->product_id]; $item->orderitem_final_price = $item->price * $item->orderitem_quantity; $order->order_subtotal += $taxes->product_taxes[$item->product_id] * $item->orderitem_quantity; } else { $item->price = $item->orderitem_price + floatval($item->orderitem_attributes_price); } } $view->assign('orderitems', $orderitems); if ($showShipping) { $view->assign('shipping_total', $order->getShippingTotal()); $view->assign('showShipping', $showShipping); } //START onDisplayOrderItem: trigger plugins for extra orderitem information if (!empty($orderitems)) { $onDisplayOrderItem = $order_helper->onDisplayOrderItems($orderitems); $view->assign('onDisplayOrderItem', $onDisplayOrderItem); } //END onDisplayOrderItem $view->setLayout('ordersummary'); ob_start(); $view->display(); $html = ob_get_contents(); ob_end_clean(); return $html; }
/** * Get the cart button form for a specific product * * @param int $product_id The id of the product * @return html The add to cart form */ public static function getCartButton($product_id, $layout = 'product_buy', $values = array(), &$callback_js = '') { if (is_array($values) && !count($values)) { $values = JRequest::get('request'); } $html = ''; $page = JRequest::getVar('page', 'product'); $isPOS = $page == 'pos'; if ($isPOS) { JLoader::register("TiendaViewPOS", JPATH_ADMINISTRATOR . "/components/com_tienda/views/pos/view.html.php"); $view = new TiendaViewPOS(); } else { JLoader::register("TiendaViewProducts", JPATH_SITE . "/components/com_tienda/views/products/view.html.php"); $view = new TiendaViewProducts(); } $model = JModel::getInstance('Products', 'TiendaModel'); $model->setId($product_id); $model->setState('task', 'product_buy'); Tienda::load('TiendaHelperBase', 'helpers._base'); $helper_product = TiendaHelperBase::getInstance('Product'); Tienda::load('TiendaHelperUser', 'helpers.user'); $user_id = JFactory::getUser()->id; if ($isPOS) { $user_id = JRequest::getInt('user_id', $user_id); } $filter_group = TiendaHelperUser::getUserGroup($user_id, $product_id); $qty = isset($values['product_qty']) && !empty($values['product_qty']) ? $values['product_qty'] : 1; $model->setState('filter_group', $filter_group); $model->setState('product.qty', $qty); $model->setState('user.id', $user_id); $row = $model->getItem(false, true, false); if ($row->product_notforsale || Tienda::getInstance()->get('shop_enabled') == '0') { return $html; } // This enable this helper method to be used outside of tienda if ($isPOS) { $view->set('_controller', 'pos'); $view->set('_view', 'pos'); } else { $view->addTemplatePath(JPATH_SITE . '/components/com_tienda/views/products/tmpl'); $view->addTemplatePath(JPATH_SITE . '/templates/' . JFactory::getApplication('site')->getTemplate() . '/html/com_tienda/products/'); // add extra templates $view->addTemplatePath(Tienda::getPath('product_buy_templates')); $view->set('_controller', 'products'); $view->set('_view', 'products'); } $view->set('_doTask', true); $view->set('hidemenu', true); $view->setModel($model, true); $view->setLayout($layout); $view->product_id = $product_id; $view->values = $values; $filter_category = $model->getState('filter_category', JRequest::getInt('filter_category', (int) @$values['filter_category'])); $view->filter_category = $filter_category; if ($isPOS) { $view->validation = "index.php?option=com_tienda&view=pos&task=validate&format=raw"; } else { $view->validation = "index.php?option=com_tienda&view=products&task=validate&format=raw"; } $config = Tienda::getInstance(); // TODO What about this?? $show_shipping = $config->get('display_prices_with_shipping'); if ($show_shipping) { $article_link = $config->get('article_shipping', ''); $shipping_cost_link = JRoute::_('index.php?option=com_content&view=article&id=' . $article_link); $view->shipping_cost_link = $shipping_cost_link; } $quantity_min = 1; if ($row->quantity_restriction) { $quantity_min = $row->quantity_min; } $invalidQuantity = '0'; $attributes = array(); $attr_orig = array(); if (empty($values)) { $product_qty = $quantity_min; // get the default set of attribute_csv if (!isset($row->default_attributes)) { $default_attributes = $helper_product->getDefaultAttributes($product_id); } else { $default_attributes = $row->default_attributes; } sort($default_attributes); $attributes_csv = implode(',', $default_attributes); $availableQuantity = $helper_product->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $invalidQuantity = '1'; } $attr_orig = $attributes = $default_attributes; } else { $product_qty = !empty($values['product_qty']) ? (int) $values['product_qty'] : $quantity_min; // TODO only display attributes available based on the first selected attribute? foreach ($values as $key => $value) { if (substr($key, 0, 10) == 'attribute_') { if (empty($value)) { $attributes[$key] = 0; } else { $attributes[$key] = $value; } } } if (!count($attributes)) { // no attributes are selected -> use default if (!isset($row->default_attributes)) { $attributes = $helper_product->getDefaultAttributes($product_id); } else { $attributes = $row->default_attributes; } } $attr_orig = $attributes; sort($attributes); // Add 0 to attributes to include all the root attributes //$attributes[] = 0;//remove this one. its causing the getAvailableQuantity to not get quantity because of wrong csv // For getting child opts $view->selected_opts = json_encode(array_merge($attributes, array('0'))); $attributes_csv = implode(',', $attributes); // Integrity checks on quantity being added if ($product_qty < 0) { $product_qty = '1'; } // using a helper file to determine the product's information related to inventory $availableQuantity = $helper_product->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $invalidQuantity = '1'; } } // adjust the displayed price based on the selected or default attributes TiendaHelperProduct::calculateProductAttributeProperty($row, $attr_orig, 'price', 'product_weight'); $show_tax = $config->get('display_prices_with_tax'); $show_product = $config->get('display_category_cartbuttons'); $view->show_tax = $show_tax; $row->tax = '0'; $row->taxtotal = '0'; if ($show_tax) { // finish TiendaHelperUser::getGeoZone -- that's why this isn't working Tienda::load('TiendaHelperUser', 'helpers.user'); $geozones_user = TiendaHelperUser::getGeoZones($user_id); if (empty($geozones_user)) { $geozones = array(Tienda::getInstance()->get('default_tax_geozone')); } else { $geozones = array(); foreach ($geozones_user as $value) { $geozones[] = $value->geozone_id; } } Tienda::load('TiendaHelperTax', 'helpers.tax'); $product = new stdClass(); $product->product_price = $row->price; $product->product_id = $product_id; $tax = TiendaHelperTax::calculateGeozonesTax(array($product), 2, $geozones); $row->taxtotal = $tax->tax_total; $row->tax = $tax->tax_total; } $row->_product_quantity = $product_qty; if ($page == 'product' || $isPOS) { $display_cartbutton = Tienda::getInstance()->get('display_product_cartbuttons', '1'); } else { $display_cartbutton = Tienda::getInstance()->get('display_category_cartbuttons', '1'); } $view->page = $page; $view->display_cartbutton = $display_cartbutton; $view->availableQuantity = $availableQuantity; $view->invalidQuantity = $invalidQuantity; if ($isPOS) { $view->product = $row; } else { $view->item = $row; } $dispatcher = JDispatcher::getInstance(); ob_start(); $dispatcher->trigger('onDisplayProductAttributeOptions', array($row->product_id)); $view->onDisplayProductAttributeOptions = ob_get_contents(); ob_end_clean(); $html = $view->loadTemplate(); if (isset($view->callback_js) && !empty($view->callback_js)) { $callback_js = $view->callback_js; } return $html; }