/** * saveAll * Save all the data * @param $params * @return Boolean */ public static function saveAll($id, $params) { $date_end = null; if (!is_array($params)) { return false; } $details = Doctrine::getTable('OrdersItems')->find($id); // Generic price setting $rowtotal = $params['price']; $vat = null; $percentage = null; // Get the taxes applied $tax = Taxes::getTaxbyProductID($params['product_id']); if ($tax['percentage'] > 0) { $rowtotal = $params['price'] * (100 + $tax['percentage']) / 100; $vat = $params['price'] * $tax['percentage'] / 100; $percentage = $tax['percentage']; } else { if (!empty($params['parameters'])) { // it is a domain $domainparams = json_decode($params['parameters'], true); if (!empty($domainparams['domain']['tld'])) { $tax = Taxes::getTaxbyTldID($domainparams['domain']['tld']); if ($tax['percentage'] > 0) { $vat = $params['price'] * $tax['percentage'] / 100; $percentage = $tax['percentage']; $rowtotal = $params['price'] * (100 + $tax['percentage']) / 100; } } } } if (!empty($params['billing_cycle_id'])) { $months = BillingCycle::getMonthsNumber($params['billing_cycle_id']); } if ($months > 0 && is_numeric($params['product_id']) && $params['product_id'] > 0) { // only for the product and services. Domains excluded $rowtotal = $rowtotal * $months; } $params['date_end'] = Shineisp_Commons_Utilities::add_date($params['date_start'], null, $months); $details->quantity = $params['quantity']; $details->date_start = Shineisp_Commons_Utilities::formatDateIn($params['date_start']); $details->date_end = Shineisp_Commons_Utilities::formatDateIn($params['date_end']); $details->billing_cycle_id = !empty($params['billing_cycle_id']) ? $params['billing_cycle_id'] : null; $details->price = $params['price']; $details->vat = $vat; $details->percentage = $percentage; $details->cost = $params['cost']; $details->product_id = is_numeric($params['product_id']) && $params['product_id'] > 0 ? $params['product_id'] : NULL; $details->setupfee = $params['setupfee']; $details->discount = $params['discount']; $details->subtotal = $rowtotal * $params['quantity']; $details->status_id = $params['status_id']; $details->description = $params['description']; $details->parameters = $params['parameters']; if ($details->trySave()) { OrdersItems::setAutorenew($id, $params['autorenew']); // Remove all domains OrdersItemsDomains::removeAllDomains($id); if ($params['domains_selected']) { foreach ($params['domains_selected'] as $domain) { OrdersItemsDomains::addDomain($details['order_id'], $domain); } } return true; } return false; }
/** * updateTotalsOrder * Get a list ready for the html select object * @return array */ public static function updateTotalsOrder($id) { $total = 0; $vat = 0; $discount = 0; $costs = 0; try { $order = self::find($id); // If the status is COMPLETE the totals will be not changed if ($order->status_id == Statuses::id("complete", "orders") || $order->status_id == Statuses::id("paid", "orders") || $order->status_id == Statuses::id("processing", "orders")) { return $order->total + $order->vat; } if ($order && is_numeric($id)) { // Get all the order details $details = OrdersItems::getAllDetails($id, null, true); if (!empty($details[0])) { $isTaxFree = Customers::isTaxFree($details[0]['Orders']['Customers']['customer_id']); $isVATFree = Customers::isVATFree($details[0]['Orders']['Customers']['customer_id']); foreach ($details as $detail) { $type = !empty($detail['product_id']) && is_numeric($detail['product_id']) ? "product" : "domain"; $product = $detail['Products']; if ($type == "domain") { // Calculate the price per Quantity $subtotal = $detail['price'] * $detail['quantity']; $setupfee = 0; } elseif ($type == "product") { $isRecurring = Products::isRecurring($detail['product_id']); if ($isRecurring) { $setupfee = $detail['setupfee']; $subtotal = $detail['price']; // Price multiplier $months = $detail['BillingCycle']['months']; if (!empty($months) && is_numeric($months)) { $subtotal = $detail['price'] * $months; } // Calculate the price per the months per Quantity $subtotal = $subtotal * $detail['quantity']; if (!empty($detail['discount'])) { $discount = $subtotal * $detail['discount'] / 100; } } else { $setupfee = $detail['setupfee']; // Calculate the price per Quantity $subtotal = $detail['price'] * $detail['quantity']; if (!empty($detail['discount'])) { $discount = $subtotal * $detail['discount'] / 100; } } } // ... and add the setup fees $price = $subtotal + $setupfee - $discount; $total += $price; $costs += $detail['cost'] * $detail['quantity']; if (!$isTaxFree && !$isVATFree) { // If the product is a domain if ($type == "domain") { $tax = Taxes::getTaxbyTldID($detail['tld_id']); } else { // If not $tax = Taxes::getTaxbyProductID($detail['product_id']); } // if the price is negative the tax MUST not be refunded // because already payed by the hoster if (!empty($tax['percentage']) && $detail['price'] > 0) { $vat += is_numeric($price) ? $price * $tax['percentage'] / 100 : 0; } } } $order->vat = $vat; $order->total = $total; $order->cost = $costs; $order->grandtotal = $total + $vat; // Update the order data $order->save(); return $total + $vat; } return 0; } } catch (Exception $e) { die($e->getMessage()); } }