/** * Add an item within an order * * @param Cart $item * @throws Exception * @return unknown */ public static function addOrderItem(CartItem $item) { if (empty(self::$order)) { throw new Exception('The order has not been created yet', 1000); } try { // get the item subtotals $subtotals = $item->getSubtotals(); // get the options $options = $item->getOptions(); // Get the order object previously created $order = self::$order; // add a new item $orderitem = new OrdersItems(); $orderitem->order_id = $order['order_id']; if ("domain" == $item->getType()) { // Create a new domain order item $orderitem->tld_id = !empty($options['domain']['tld']) ? $options['domain']['tld'] : null; $orderitem->parameters = json_encode($options); } else { $orderitem->product_id = $item->getId(); $orderitem->parameters = json_encode(ProductsAttributes::getSystemAttributes($item->getId())); } $orderitem->status_id = Statuses::id("tobepaid", "orders"); $orderitem->date_start = date('Y-m-d H:i:s'); $orderitem->date_end = null; $orderitem->description = $item->getName(); $orderitem->quantity = $item->getQty(); $orderitem->price = $item->getUnitprice(); $orderitem->billing_cycle_id = $item->getBillingid(); $orderitem->vat = $subtotals['taxes']; $orderitem->percentage = $subtotals['percentage']; $orderitem->uuid = $item->getUid(); // Count of the day until the expiring if ($item->getIsrecurring()) { // get the months until of the next service expiration $months = $subtotals['months']; if ($months > 0) { $totmonths = intval($item->getQty() * $months); // Calculate the total of the months $date_end = Shineisp_Commons_Utilities::add_date(date('d-m-Y H:i:s'), null, $totmonths); $orderitem->date_end = Shineisp_Commons_Utilities::formatDateIn($date_end); } else { $orderitem->date_end = null; } } $orderitem->cost = $item->getCost(); $orderitem->setupfee = $subtotals['setupfee']; $orderitem->subtotal = $subtotals['subtotal']; // Save the order item if ($orderitem->trySave()) { // Attach all the the domain to the order if ("domain" == $item->getType()) { $domain = $options['domain']['name']; $tld = $options['domain']['tld']; $domainId = Domains::Create($domain, $tld, $order->customer_id, $orderitem->detail_id); if (is_numeric($domainId)) { $ordersitemsdomains = new OrdersItemsDomains(); $ordersitemsdomains->domain_id = $domainId; $ordersitemsdomains->order_id = $order['order_id']; $ordersitemsdomains->orderitem_id = $orderitem->detail_id; $ordersitemsdomains->save(); } } // Update the totals of the order self::updateTotalsOrder($order['order_id']); return $orderitem; } } catch (Exception $e) { Shineisp_Commons_Utilities::log('There was a problem during the order creation: ' . $e->getMessage()); } return false; }
/** * Calculate the subtotal for each product in the cart * * @param integer $id * @param integer $qty * @param boolean $isvatfree * @return ArrayObject */ private function calcSubtotal(CartItem $item, $isvatfree = false) { foreach ($this->items as $item) { $isrecurring = false; $months = 0; $percentage = 0; $tax = 0; if ("domain" == $item->getType()) { $item->setIsrecurring(true); // Get the billyng cycle / term / recurring period price if ($item->getTerm()) { $priceInfo = ProductsTranches::getTranchebyId($item->getTerm()); $item->setBillingid($priceInfo['BillingCycle']['billing_cycle_id']); $months = $priceInfo['BillingCycle']['months']; } else { $priceInfo = BillingCycle::getDefaultDomainBillingCycle(); $item->setBillingid($priceInfo['billing_cycle_id']); $months = $priceInfo['months']; } $unitprice = $item->getUnitPrice(); $setupfee = 0; // Calculate the price per Quantity $subtotal = $unitprice * $item->getQty(); // check the taxes if (Taxes::get_percentage($item->getTaxId())) { $percentage = Taxes::get_percentage($item->getTaxId()); $tax = $subtotal * $percentage / 100; $price = $subtotal * (100 + $percentage) / 100; } } else { // Get all the product information $product = Products::getAllInfo($item->getId()); // Check the type of the product and get the price information if ($product['ProductsAttributesGroups']['isrecurring']) { $item->setIsrecurring(true); // Get the billyng cycle / term / recurring period price $priceInfo = ProductsTranches::getTranchebyId($item->getTerm()); // Price multiplier $months = $priceInfo['BillingCycle']['months']; $unitprice = $priceInfo['price']; $setupfee = $priceInfo['setupfee']; // Calculate the price per the months per Quantity $subtotal = $unitprice * $months * $item->getQty(); $item->setBillingid($priceInfo['BillingCycle']['billing_cycle_id']); } else { $item->setIsrecurring(false); $unitprice = $product['price_1']; $setupfee = $product['setupfee']; // Calculate the price per Quantity $subtotal = $unitprice * $item->getQty(); } // check the taxes for each product if (!empty($product['tax_id']) && !$isvatfree) { if (!empty($product['Taxes']['percentage']) && is_numeric($product['Taxes']['percentage'])) { $percentage = $product['Taxes']['percentage']; $tax = $subtotal * $percentage / 100; $price = $subtotal * (100 + $percentage) / 100; } } } // ... and add the setup fees $price = $subtotal + $setupfee; $item->setSubtotals(array('months' => $months, 'subtotal' => $subtotal, 'setupfee' => $setupfee, 'price' => $price, 'taxes' => $tax, 'percentage' => $percentage)); } return $this->items; }