Exemple #1
0
 /**
  * Create an order using only one product
  * @param $product
  * @param $type [domain, service]
  * @param $iddomgen [id of the domain or the id of the generic product]
  * @param $customer_id
  * @param $amount
  * @param $note
  * @return void
  */
 public static function createOrderForSingleProduct(array $product, $type, $iddomgen, $customer_id, $amount, $note = "")
 {
     $order = new Orders();
     $customer = Customers::getAllInfo($customer_id);
     try {
         $tax = Taxes::getTaxbyProductID($product['id']);
         if (is_numeric($amount) && $amount > 0) {
             // Creating a new Order.
             $order->customer_id = $customer_id;
             $order->isp_id = $customer['isp_id'];
             $order->order_date = date('Y-m-d');
             $order->note = $note;
             $order->status_id = Statuses::id("processing", "orders");
             // Processing
             if (isset($tax['percentage']) && $tax['percentage'] > 0 && !Customers::isVATFree($order->customer_id)) {
                 $order->total = $amount / ((100 + $tax['percentage']) / 100);
                 $order->vat = $amount - $order->total;
                 $order->grandtotal = $amount;
             } else {
                 $order->total = $amount;
                 $order->vat = 0;
                 $order->grandtotal = $amount;
             }
             $order->save();
             $id = $order->getIncremented();
             // Attaching the order item to the order previously created.
             $orderitem = new OrdersItems();
             $date_end = Shineisp_Commons_Utilities::add_date(date('d-m-Y'), null, 12);
             // Fixed Renew
             $orderitem->order_id = $id;
             $orderitem->product_id = $product['id'];
             $orderitem->billing_cycle_id = 1;
             $orderitem->date_start = date('Y-m-d');
             $orderitem->date_end = $date_end;
             $orderitem->autorenew = true;
             $orderitem->description = $product['name'];
             $orderitem->cost = $product['cost'];
             $orderitem->quantity = 1;
             if (isset($tax['percentage']) && $tax['percentage'] > 0 && !Customers::isVATFree($order->customer_id)) {
                 $orderitem->price = $amount / ((100 + $tax['percentage']) / 100);
             } else {
                 $orderitem->price = $amount;
             }
             $orderitem->status_id = Statuses::id("processing", "orders");
             // Processing status set
             $orderitem->save();
             $detailid = $orderitem->getIncremented();
             // If the product type is not a domain we have to add a record in the Orders_items_domains table
             // in order to join the domain with the order detail
             if ($type != "domain") {
                 $ordersitemsdomains = new OrdersItemsDomains();
                 $ordersitemsdomains->domain_id = $iddomgen;
                 $ordersitemsdomains->order_id = $id;
                 $ordersitemsdomains->orderitem_id = $detailid;
                 $ordersitemsdomains->save();
                 Domains::setStatus($iddomgen, Statuses::id("processing", "domains"));
                 // Set the domains status as processing
                 Domains::setExpirationDate($iddomgen, $date_end);
                 // Set the new expiration date
             }
             return $id;
         }
     } catch (Exception $e) {
         echo $e->getMessage();
         die;
     }
 }