Example #1
0
 /**
  * saveAll
  * Save all the data in the database
  * @param array $params
  * @param integer $id
  */
 public static function saveAll($params, $id = "")
 {
     $orders = new Orders();
     $translator = Shineisp_Registry::getInstance()->Zend_Translate;
     $currentStatus = "";
     try {
         // Set the new values
         if (is_numeric($id)) {
             $orders = Doctrine::getTable('Orders')->find($id);
             $currentStatus = $orders->status_id;
             // used to detect status changes
         }
         if (!empty($params) && is_array($params)) {
             $params['date_start'] = !empty($params['date_start']) ? $params['date_start'] : new Zend_Date();
             $params['order_date'] = !empty($params['order_date']) ? $params['order_date'] : new Zend_Date();
             $customer = Customers::getAllInfo($params['customer_id']);
             $isp_id = $customer['isp_id'];
             $orders->order_date = Shineisp_Commons_Utilities::formatDateIn($params['order_date']);
             $orders->customer_id = $params['customer_id'];
             $orders->isp_id = $isp_id;
             $orders->status_id = $params['status_id'];
             $orders->invoice_id = !empty($params['invoice_id']) ? $params['invoice_id'] : null;
             $orders->note = $params['note'];
             $orders->is_renewal = $params['is_renewal'] == 1 ? 1 : 0;
             $orders->expiring_date = Shineisp_Commons_Utilities::formatDateIn($params['expiring_date']);
             $orders->vat = $params['vat'];
             $orders->total = $params['total'];
             $orders->grandtotal = $params['total'] + $params['vat'];
             // Save the data
             $orders->save();
             $id = is_numeric($id) ? $id : $orders->getIncremented();
             // Status changed? Let's call set_status. This is needed to properly log all status change.
             if (isset($params['status_id']) && $params['status_id'] != $currentStatus) {
                 self::logStatusChange($id, $params['status_id']);
             }
             // Add a fastlink to a order
             $link_exist = Fastlinks::findlinks($id, 'orders');
             $link = new Fastlinks();
             if (count($link_exist) == 0) {
                 $link->controller = "orders";
                 $link->action = "edit";
                 $link->params = json_encode(array('id' => $id));
                 $link->customer_id = $params['customer_id'];
                 $link->sqltable = "orders";
                 $link->id = $id;
                 $link->code = Shineisp_Commons_Utilities::GenerateRandomString();
             } else {
                 $link = Doctrine::getTable('Fastlinks')->find($link_exist[0]['fastlink_id']);
                 $link->code = $params['fastlink'];
             }
             $link->save();
             // Save the message note and send an alert
             if (!empty($params['message'])) {
                 $order = self::getAllInfo($id, null, true);
                 $link = Fastlinks::findlinks($id, $params['customer_id'], 'orders');
                 $isp = Isp::find($isp_id);
                 $retval = Shineisp_Commons_Utilities::getEmailTemplate('order_message');
                 if ($retval) {
                     $in_reply_to = md5($id);
                     // Save the message written by the ISP owner
                     Messages::addMessage($params['message'], null, null, $id, null, $isp_id);
                     // Create the array with all the placeholders
                     $placeholders['fullname'] = $order[0]['Customers']['firstname'] . " " . $order[0]['Customers']['lastname'];
                     $placeholders['url'] = "http://" . $_SERVER['HTTP_HOST'] . "/index/link/id/" . $link[0]['code'];
                     $placeholders['orderid'] = sprintf("%03s", $id) . " - " . Shineisp_Commons_Utilities::formatDateOut($order[0]['order_date']);
                     $placeholders['messagetype'] = $translator->translate('Order Details');
                     $placeholders['message'] = $params['message'];
                     Shineisp_Commons_Utilities::sendEmailTemplate(Contacts::getEmails($order[0]['Customers']['customer_id']), 'order_message', $placeholders, $in_reply_to, null, null, null, $order[0]['Customers']['language_id']);
                     // Change the URL for the administrator
                     $placeholders['url'] = "http://" . $_SERVER['HTTP_HOST'] . "/admin/login/link/id/" . $link[0]['code'] . "/keypass/" . Shineisp_Commons_Hasher::hash_string($isp->email);
                     // Send a message to the administrator
                     Shineisp_Commons_Utilities::sendEmailTemplate($isp->email, 'order_message_admin', $placeholders, $in_reply_to);
                 }
             }
             // Saving the domain
             if (!empty($params['domains_selected'])) {
                 self::SaveDomainsDetails($params, $id);
             } else {
                 if (!empty($params['products'])) {
                     $date_end = null;
                     // Get the product information
                     $product = Products::getAllInfo($params['products']);
                     // Manage the details of the order
                     if (!empty($params['billingcycle_id'])) {
                         $months = BillingCycle::getMonthsNumber($params['billingcycle_id']);
                         // Add months to the starting date
                         if ($months > 0) {
                             $params['date_end'] = Shineisp_Commons_Utilities::add_date($params['date_start'], null, $months);
                         }
                     }
                     // Format the dates before to save them in the database
                     $params['date_end'] = Shineisp_Commons_Utilities::formatDateIn($params['date_end']);
                     $params['date_start'] = Shineisp_Commons_Utilities::formatDateIn($params['date_start']);
                     if (!empty($product['Taxes']['tax_id'])) {
                         $vat = $params['price'] * $product['Taxes']['percentage'] / 100;
                         $subtotal = $params['price'] * ($product['Taxes']['percentage'] + 100) / 100;
                         $percentage = $product['Taxes']['percentage'];
                     } else {
                         $vat = 0;
                         $subtotal = $params['price'];
                         $percentage = 0;
                     }
                     $details = new OrdersItems();
                     $details->order_id = $id;
                     $details->quantity = $params['quantity'];
                     $details->cost = Products::getCost($params['products']);
                     $details->price = $params['price'];
                     $details->date_start = $params['date_start'];
                     $details->date_end = $params['date_end'];
                     $details->billing_cycle_id = !empty($params['billingcycle_id']) ? $params['billingcycle_id'] : null;
                     $details->product_id = $params['products'];
                     $details->description = $params['description'];
                     $details->status_id = $params['status_id'];
                     $details->vat = $vat;
                     $details->percentage = $percentage;
                     $details->subtotal = $subtotal;
                     if ($product['type'] == "hosting") {
                         // Get all the product attributes
                         $attrs = ProductsAttributes::getAttributebyProductID($params['products']);
                         // Create the system attributes parameters
                         foreach ($attrs as $attr) {
                             if ($attr['system'] && !empty($attr['ProductsAttributesIndexes'][0]['value'])) {
                                 $hostingplan[$attr['code']] = $attr['ProductsAttributesIndexes'][0]['value'];
                             }
                         }
                         $details->parameters = json_encode($hostingplan);
                     }
                     $details->save();
                     $detail_id = $details->getIncremented();
                     // Attaching of the service a particular domain. It's not a mandatory field because the services and the products can be bought also without a domain
                     if (is_numeric($params['referdomain']) && $params['referdomain'] > 0) {
                         $ordersitemsdomains = new OrdersItemsDomains();
                         $ordersitemsdomains->domain_id = $params['referdomain'];
                         $ordersitemsdomains->order_id = $id;
                         $ordersitemsdomains->orderitem_id = $detail_id;
                         $ordersitemsdomains->save();
                     }
                 }
             }
             // Handle the payment transaction
             if (!empty($params['paymentdate'])) {
                 Payments::addPayment($id, $params['reference'], $params['bank_id'], $params['confirmed'], $params['income'], $params['paymentdate'], $params['customer_id'], $params['payment_description']);
             }
             // Set the status of the order
             OrdersItems::setNewStatus($id, $params['status_id']);
             // Update the totals of the order selected
             self::updateTotalsOrder($id);
             return $id;
         }
     } catch (Exception $e) {
         die($e->getMessage());
     }
     return false;
 }