Example #1
0
 /**
  * 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;
 }