Esempio n. 1
0
 /**
  * This function is used to migrate the tax and shipping charge detail to new format.
  * OLD: order tax = SUM(item Tax) similary for shipping charges
  * NEW: Item level tax is not add in order tax fields. Order tax fields contain only order tax.
  *
  * @return  ''.
  *
  * @since   2.2.5
  */
 public function test()
 {
     $db = JFactory::getDBO();
     $comquick2cartHelper = new Comquick2cartHelper();
     $path = JPATH_SITE . '/components/com_quick2cart/models/cartcheckout.php';
     $ckoutModel = $comquick2cartHelper->loadqtcClass($path, "Quick2cartModelcartcheckout");
     // A.Check for column itemTaxShipIncluded. If present then only migration required else not.
     $query = "SHOW COLUMNS FROM `#__kart_orders`";
     $db->setQuery($query);
     $columns = $db->loadColumn();
     // B.If col is not present then add
     if (!in_array('itemTaxShipIncluded', $columns)) {
         // C. Else add column= 'itemTaxShipIncluded' to db
         $query = "ALTER TABLE  `#__kart_orders` ADD  `itemTaxShipIncluded` tinyint(1) NOT NULL DEFAULT '0' " . " COMMENT 'Flag : whether order tax and shipping is summation of order item tax and ship or not. 1 =>  orderTax = sum(order item tax)'";
         $db->setQuery($query);
         if (!$db->execute()) {
             echo JText::_('COM_QUICK2CART_UNABLE_TO_ALTER_COLUMN') . " - #__kart_orders.";
             echo $db->getErrorMsg();
             return 0;
         }
         // D. Set its value column value to 1
         $query = $db->getQuery(true);
         // Fields to update.
         $fields = array($db->quoteName('itemTaxShipIncluded') . ' = 1');
         // Conditions for which records should be updated.
         $conditions = array();
         $query->update($db->quoteName('#__kart_orders'))->set($fields);
         $db->setQuery($query);
         $result = $db->execute();
     }
     // E:
     $query = $db->getQuery(true);
     $query->select(' o.* ');
     $query->from('#__kart_orders as o');
     $query->where("(o.order_tax > 0 OR o.order_shipping > 0 )");
     $query->where("itemTaxShipIncluded=1");
     $query->order("o.id DESC");
     $db->setQuery($query);
     $orderList = $db->loadObjectList('id');
     foreach ($orderList as $order) {
         $query = $db->getQuery(true);
         $modifiedOrder = new stdClass();
         $modifiedOrder->id = $order->id;
         // Get order item details
         $query->select('order_id, sum(product_final_price) as totalItemprice,sum(item_tax) as totalItemTax,sum(item_shipcharges) as totalShipCharge');
         $query->from('#__kart_order_item as i');
         $query->where("order_id= " . $order->id);
         $db->setQuery($query);
         $itemDetail = $db->loadAssoc('id');
         // 1. Update original amount
         $modifiedOrder->original_amount = $itemDetail['totalItemprice'];
         // 2.update tax and ship
         $OrderLevelTax = $order->order_tax - $itemDetail['totalItemTax'];
         if ($OrderLevelTax >= 0) {
             $modifiedOrder->order_tax = $OrderLevelTax;
         } else {
             $modifiedOrder->order_tax = 0;
         }
         $OrderLevelShip = $order->order_shipping - $itemDetail['totalShipCharge'];
         if ($OrderLevelTax >= 0) {
             $modifiedOrder->order_shipping = $OrderLevelShip;
         } else {
             $modifiedOrder->order_shipping = 0;
         }
         // 3. Check for coupon
         $copDiscount = 0;
         if ($order->coupon_code) {
             $copDiscount = $ckoutModel->afterDiscountPrice($modifiedOrder->original_amount, $order->coupon_code, "", "order", $modifiedOrder->id);
             $copDiscount = $copDiscount >= 0 ? $copDiscount : 0;
         }
         // 4. Update amount column according to discount,tax,shipping details
         $modifiedOrder->amount = $modifiedOrder->original_amount + $modifiedOrder->order_tax + $modifiedOrder->order_shipping - $copDiscount;
         if (!$db->updateObject('#__kart_orders', $modifiedOrder, 'id')) {
             $this->setError($db->getErrorMsg());
             return 0;
         }
     }
 }
Esempio n. 2
0
 /**
  * Updatecart updates the cart, Attributes, shipping charges.
  *
  * @since   2.2.2
  *
  * @return   status.
  */
 public function updateOrderCartItemAttributes()
 {
     $jinput = JFactory::getApplication()->input;
     $post = $jinput->post;
     // Get parsed form data
     parse_str($post->get('formData', '', 'STRING'), $formData);
     $comquick2cartHelper = new Comquick2cartHelper();
     $path = JPATH_SITE . '/components/com_quick2cart/models/cartcheckout.php';
     $checkoutModel = $comquick2cartHelper->loadqtcClass($path, "Quick2cartModelcartcheckout");
     $order_id = $jinput->get('order_id', '', 'INT');
     $orderInfo = $this->getorderinfo($order_id);
     $userId = $orderInfo['order_info'][0]->user_id;
     $orderItems = $orderInfo['items'];
     // $cart_item_id = $post->get('order_item_id', '', 'INT');
     $item_id = $post->get('item_id', '', 'INT');
     $cartItems = $formData['cartDetail'];
     // Required while order update
     $modifiedOrder = new stdClass();
     $modifiedOrder->id = $order_id;
     $modifiedOrder->coupon_code = $orderInfo['order_info'][0]->coupon_code;
     $totalItemTax = 0;
     $totalItemShip = 0;
     $modifiedOrder->original_amount = 0;
     // Update items first
     foreach ($cartItems as $cartIndex => $item) {
         $dbOrderItemDetail = array();
         // Get order item details
         foreach ($orderItems as $oitem) {
             if ($cartIndex == $oitem->order_item_id) {
                 $dbOrderItemDetail = $oitem;
                 break;
             }
         }
         $orderItem = new stdClass();
         $orderItem->order_item_id = $item['order_item_id'];
         $orderItem->product_quantity = $item['cart_count'];
         $orderItem->product_item_price = $dbOrderItemDetail->product_item_price;
         $orderItem->product_attributes_price = 0;
         $OrderitemAttrTbIds = array();
         $product_attribute_names = array();
         // Check for count
         // 1. If item has attribute
         if (!empty($item['attrDetail'])) {
             // Get attribute list for order items
             $DBoptionList = $this->getOrderItemOptionList($item['order_item_id']);
             foreach ($item['attrDetail'] as $key => $attr) {
                 /* Control come if user has changed option value to not compulsory. (none)
                 			Just removing the empty option field
                 			*/
                 if ($attr['type'] == 'Select' && empty($attr['value'])) {
                     continue;
                 }
                 // If type  = select and option present in Db attribute array then nothing have to update
                 // For text type, we hv to update
                 if ($attr['type'] == "Textbox") {
                     $itemattributeoption_id = $attr['itemattributeoption_id'];
                     // Update option - text field value and update option entry
                     $optionDetails['orderitemattribute_name'] = $attr['value'];
                 } elseif ($attr['type'] == 'Select') {
                     $itemattributeoption_id = $attr['value'];
                 }
                 // Get attribute option details
                 $optDetail = $this->productHelper->getOrderAttributeOptionDetails($item['order_item_id'], $itemattributeoption_id);
                 // For new option, it will be empty
                 if (empty($optDetail)) {
                     // Get option details
                     $result = $this->productHelper->getAttributeOptionDetails($itemattributeoption_id);
                     $optionDetails['order_item_id'] = $item['order_item_id'];
                     $optionDetails['itemattributeoption_id'] = $result['itemattributeoption_id'];
                     $optionDetails['orderitemattribute_name'] = $result['itemattributeoption_name'];
                     // It is recalculated in next function - insertOptionToOrderItems
                     $optionDetails['orderitemattribute_price'] = 0;
                     $optionDetails['orderitemattribute_prefix'] = $result['itemattributeoption_prefix'];
                 } else {
                     $optionDetails = $optDetail;
                 }
                 // Insert/update option and get latest option detail
                 $newOptionDetails = $this->productHelper->insertOptionToOrderItems($optionDetails);
                 if ($newOptionDetails['orderitemattribute_prefix'] == '+') {
                     $orderItem->product_attributes_price += $newOptionDetails['orderitemattribute_price'];
                 } elseif ($newOptionDetails['orderitemattribute_prefix'] == '-') {
                     $orderItem->product_attributes_price -= $newOptionDetails['orderitemattribute_price'];
                 }
                 $OrderitemAttrTbIds[] = $newOptionDetails['itemattributeoption_id'];
                 $product_attribute_names[] = $newOptionDetails['orderitemattribute_name'];
                 // Remove from DBoptionList
                 if ($itemattributeoption_id && isset($DBoptionList[$itemattributeoption_id])) {
                     unset($DBoptionList[$itemattributeoption_id]);
                 }
             }
             // Delete extra options
             $this->deleteExtraOrderoptions($DBoptionList);
             // Get attribute ids and names
             $orderItem->product_attributes = !empty($OrderitemAttrTbIds) ? implode(',', $OrderitemAttrTbIds) : '';
             $orderItem->product_attribute_names = !empty($product_attribute_names) ? implode(',', $product_attribute_names) : '';
         }
         // ProductPrice = Base price + attribute price) * qty
         $productPrice = ((double) $orderItem->product_item_price + (double) $orderItem->product_attributes_price) * $orderItem->product_quantity;
         $orderItem->original_price = $productPrice;
         // Check for item discount and update product price
         if (isset($dbOrderItemDetail->params)) {
             $param = json_decode($dbOrderItemDetail->params, true);
             $cop_code = isset($param['coupon_code']) ? $param['coupon_code'] : '';
             if ($cop_code) {
                 $path = JPATH_SITE . "/components/com_quick2cart/models/cartcheckout.php";
                 $checkoutModel = $this->comquick2cartHelper->loadqtcClass($path, 'Quick2cartModelcartcheckout');
                 $valid = $checkoutModel->getcoupon($cop_code, $userId, "order", $order_id);
                 if (!empty($valid)) {
                     if ($valid[0]->val_type == 1) {
                         $cval = $valid[0]->value / 100 * $productPrice;
                     } else {
                         $cval = $valid[0]->value;
                     }
                     $afterDiscount = $productPrice - $cval;
                     if ($afterDiscount <= 0) {
                         $afterDiscount = 0;
                     }
                     $productPrice = $afterDiscount >= 0 ? $afterDiscount : $totalamt;
                 }
             }
         }
         // Leval of taxation order leval or item leval
         $taxShipLeval = isset($formData['editLeval_taxship']) ? $formData['editLeval_taxship'] : '';
         $itemTaxShipDetail = isset($formData['itemTaxShipDetail']) ? $formData['itemTaxShipDetail'] : '';
         if (isset($itemTaxShipDetail[$orderItem->order_item_id]['tax'])) {
             // Update the item tax detail
             $DBitem_tax_detail = array();
             if (!empty($dbOrderItemDetail->item_tax_detail)) {
                 $DBitem_tax_detail = json_decode($dbOrderItemDetail->item_tax_detail, true);
             }
             $DBitem_tax_detail['adminUpdate'] = "Admin updated the tax. Old tax = " . $dbOrderItemDetail->item_tax . " && new = " . $itemTaxShipDetail[$orderItem->order_item_id]['tax'];
             $orderItem->item_tax_detail = json_encode($DBitem_tax_detail);
             // Update the item tax
             $orderItem->item_tax = $dbOrderItemDetail->item_tax = $itemTaxShipDetail[$orderItem->order_item_id]['tax'];
             $totalItemTax += $orderItem->item_tax;
         }
         if (isset($itemTaxShipDetail[$orderItem->order_item_id]['ship'])) {
             // Update the item ship detail
             $DBitem_ship_detail = array();
             if (!empty($dbOrderItemDetail->item_shipDetail)) {
                 $DBitem_ship_detail = json_decode($dbOrderItemDetail->item_shipDetail, true);
             }
             $DBitem_ship_detail['adminUpdate'] = "Admin updated the ship. Old tax = " . $dbOrderItemDetail->item_shipcharges . " && new = " . $itemTaxShipDetail[$orderItem->order_item_id]['ship'];
             $orderItem->item_tax_detail = json_encode($DBitem_tax_detail);
             $orderItem->item_shipcharges = $dbOrderItemDetail->item_shipcharges = $itemTaxShipDetail[$orderItem->order_item_id]['ship'];
             $totalItemShip += $orderItem->item_shipcharges;
         }
         $orderItem->product_final_price = $productPrice + $dbOrderItemDetail->item_tax + $dbOrderItemDetail->item_shipcharges;
         $modifiedOrder->original_amount += $orderItem->product_final_price;
         try {
             $db = JFactory::getDbo();
             if (!$db->updateObject('#__kart_order_item', $orderItem, 'order_item_id')) {
                 echo $db->stderr();
             }
         } catch (Exception $e) {
             $this->setError($e->getMessage());
         }
     }
     // NOW: Update the order table
     $copDiscount = 0;
     if ($modifiedOrder->coupon_code) {
         $copDiscount = $checkoutModel->afterDiscountPrice($modifiedOrder->original_amount, $modifiedOrder->coupon_code, "", "order", $modifiedOrder->id);
         $copDiscount = $copDiscount >= 0 ? $copDiscount : 0;
     }
     // Get order detail
     $dbOrderDetail = $this->getOrderEntry($modifiedOrder->id);
     $modifiedOrder->order_tax_details = $dbOrderDetail->order_tax_details;
     $modifiedOrder->order_tax = $dbOrderDetail->order_tax;
     $modifiedOrder->order_shipping_details = $dbOrderDetail->order_shipping_details;
     $modifiedOrder->order_shipping = $dbOrderDetail->order_shipping;
     // 4. Update amount column according to discount,tax,shipping details
     if (isset($formData['OrderTaxShipDetail'])) {
         $newOrderTax = isset($formData['OrderTaxShipDetail']['tax']) ? $formData['OrderTaxShipDetail']['tax'] : 0;
         // If tax is updated then only update field
         if ($newOrderTax != $dbOrderDetail->order_tax) {
             // Update the item tax detail
             $DBTax_detail = array();
             if (!empty($dbOrderDetail->order_tax_details)) {
                 $DBTax_detail = json_decode($dbOrderDetail->order_tax_details, true);
             }
             $DBTax_detail['adminUpdate'][]['old'] = $dbOrderDetail->order_tax;
             $DBTax_detail['adminUpdate'][]['new'] = $newOrderTax;
             $modifiedOrder->order_tax_details = json_encode($DBTax_detail);
             $modifiedOrder->order_tax = $newOrderTax;
         }
         $newOrderShip = isset($formData['OrderTaxShipDetail']['ship']) ? $formData['OrderTaxShipDetail']['ship'] : 0;
         // If shipping is updated then only update field
         if ($newOrderShip != $dbOrderDetail->order_shipping) {
             // Update the item tax detail
             $DBTax_detail = array();
             if (!empty($dbOrderDetail->order_shipping_details)) {
                 $DBTax_detail = json_decode($dbOrderDetail->order_shipping_details, true);
             }
             $DBTax_detail['adminUpdate'][]['old'] = $dbOrderDetail->order_shipping;
             $DBTax_detail['adminUpdate'][]['new'] = $newOrderShip;
             $modifiedOrder->order_shipping_details = json_encode($DBTax_detail);
             $modifiedOrder->order_shipping = $newOrderShip;
         }
         $modifiedOrder->amount = $modifiedOrder->original_amount + $modifiedOrder->order_tax + $modifiedOrder->order_shipping - $copDiscount;
     } else {
         $modifiedOrder->amount = $modifiedOrder->original_amount;
         // + $totalItemTax + $totalItemShip - $copDiscount;
     }
     if (!$db->updateObject('#__kart_orders', $modifiedOrder, 'id')) {
         $this->setError($db->getErrorMsg());
         return 0;
     }
     return 1;
 }