Esempio n. 1
0
 /**
  * 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;
 }