/**
  * Public function that creates a single instance
  */
 public static function getInstance()
 {
     if (!isset(self::$_instance)) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Example #2
0
 /**
  * Get shopping cart summary.
  *
  * @param integer $type
  * @param string $transaction_id
  * @param array $recipient
  * @return array
  */
 private function getCartSummary($type, $recipient, $transaction_id)
 {
     $result = array();
     $default_language = MainLanguageHandler::getInstance()->getDefaultLanguage();
     // prepare params
     $shipping = 0;
     $handling = 0;
     $total_money = 0;
     $total_weight = 0;
     $delivery_method = null;
     $items_by_uid = array();
     $items_for_checkout = array();
     $delivery_items = array();
     $delivery_prices = array();
     $map_id_to_uid = array();
     if (isset($_SESSION['recurring_plan'])) {
         $plan_name = $_SESSION['recurring_plan'];
         $payment_method = $this->getPaymentMethod(null);
         // get selected recurring plan
         $plans = $payment_method->get_recurring_plans();
         if (isset($plans[$plan_name])) {
             $plan = $plans[$plan_name];
             $handling = $plan['setup_price'];
             $total_money = $plan['price'];
         }
     } else {
         // colect ids from session
         $cart = isset($_SESSION['shopping_cart']) ? $_SESSION['shopping_cart'] : array();
         $ids = array_keys($cart);
         if (count($cart) == 0) {
             return $result;
         }
         // get managers
         $manager = ShopItemManager::getInstance();
         // get items from database and prepare result
         $items = $manager->getItems($manager->getFieldNames(), array('uid' => $ids));
         // parse items from database
         foreach ($items as $item) {
             $db_item = array('id' => $item->id, 'name' => $item->name, 'price' => $item->price, 'tax' => $item->tax, 'weight' => $item->weight);
             $items_by_uid[$item->uid] = $db_item;
             $map_id_to_uid[$item->id] = $item->uid;
         }
         // prepare items for checkout
         foreach ($cart as $uid => $item) {
             // include all item variations in preparation
             if (count($item['variations']) > 0) {
                 foreach ($item['variations'] as $variation_id => $data) {
                     // add items to checkout list
                     $properties = $data;
                     foreach ($this->excluded_properties as $key) {
                         if (isset($properties[$key])) {
                             unset($properties[$key]);
                         }
                     }
                     $new_item = $items_by_uid[$uid];
                     $new_item['count'] = $data['count'];
                     $new_item['description'] = implode(', ', array_values($properties));
                     // add item to list for delivery estimation
                     $delivery_items[] = array('properties' => array(), 'package' => 1, 'weight' => 0.5, 'package_type' => 0, 'width' => 2, 'height' => 5, 'length' => 15, 'units' => 1, 'count' => $data['count']);
                     // add item to the list
                     $items_for_checkout[] = $new_item;
                     // include item data in summary
                     $tax = $new_item['tax'];
                     $price = $new_item['price'];
                     $weight = $new_item['weight'];
                     $total_money += $price * (1 + $tax / 100) * $data['count'];
                     $total_weight += $weight * $data['count'];
                 }
             }
         }
     }
     // only get delivery method prices if request was made by client-side script
     if (_AJAX_REQUEST) {
         // get prefered method
         if (isset($_SESSION['delivery_method']) && array_key_exists($_SESSION['delivery_method'], $this->delivery_methods)) {
             $delivery_method = $this->delivery_methods[$_SESSION['delivery_method']];
         }
         // if there is a delivery method selected, get price estimation for items
         // TODO: Instead of picking up the first warehouse we need to choose proper one based on item property.
         if (!is_null($delivery_method)) {
             $currency_manager = ShopCurrenciesManager::getInstance();
             $warehouse_manager = ShopWarehouseManager::getInstance();
             $warehouse = $warehouse_manager->getSingleItem($warehouse_manager->getFieldNames(), array());
             // get currency associated with transaction
             $currency = $currency_manager->getSingleItem($currency_manager->getFieldNames(), array('id' => $currency_id));
             if (is_object($currency)) {
                 $preferred_currency = $currency->currency;
             } else {
                 $preferred_currency = 'EUR';
             }
             if (is_object($warehouse)) {
                 $shipper = array('street' => array($warehouse->street, $warehouse->street2), 'city' => $warehouse->city, 'zip_code' => $warehouse->zip, 'state' => $warehouse->state, 'country' => $warehouse->country);
                 // get types and prices from delivery method provider
                 $delivery_prices = $delivery_method->getDeliveryTypes($delivery_items, $shipper, $recipient, $transaction_id, $preferred_currency);
                 // convert prices and format timestamps
                 $language_handler = MainLanguageHandler::getInstance();
                 $date_format = $language_handler->getText('format_date');
                 if (count($delivery_prices) > 0) {
                     for ($i = 0; $i < count($delivery_prices); $i++) {
                         $delivery = $delivery_prices[$i];
                         // format starting date
                         if (!is_null($delivery[3])) {
                             $delivery[3] = date($date_format, $delivery[3]);
                         }
                         // format ending date
                         if (!is_null($delivery[4])) {
                             $delivery[4] = date($date_format, $delivery[4]);
                         }
                         // store delivery back to the original array
                         $delivery_prices[$i] = $delivery;
                     }
                 }
             }
         }
     }
     $result = array('items_for_checkout' => $items_for_checkout, 'shipping' => $shipping, 'handling' => $handling, 'weight' => $total_weight, 'total' => $total_money, 'delivery_method' => is_null($delivery_method) ? '' : $delivery_method->getName(), 'delivery_prices' => $delivery_prices);
     return $result;
 }
 /**
  * Handle tag list tag drawing
  *
  * @param array $tag_params
  * @param array $children
  */
 public function tag_WarehouseList($tag_params, $children)
 {
     $manager = ShopWarehouseManager::getInstance();
     $conditions = array();
     $template = $this->_parent->loadTemplate($tag_params, 'warehouse_list_item.xml');
     $items = $manager->getItems($manager->getFieldNames(), $conditions);
     if (count($items) > 0) {
         foreach ($items as $item) {
             $params = array('name' => $item->name, 'street' => $item->street, 'street2' => $item->street2, 'city' => $item->city, 'zip' => $item->zip, 'country' => $item->country, 'state' => $item->state, 'item_change' => url_MakeHyperlink($this->_parent->getLanguageConstant('change'), window_Open('shop_warehouse_change', 300, $this->_parent->getLanguageConstant('title_warehouse_change'), true, true, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'warehouses'), array('sub_action', 'change'), array('id', $item->id)))), 'item_delete' => url_MakeHyperlink($this->_parent->getLanguageConstant('delete'), window_Open('shop_warehouse_delete', 400, $this->_parent->getLanguageConstant('title_warehouse_delete'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'warehouses'), array('sub_action', 'delete'), array('id', $item->id)))));
             $template->setLocalParams($params);
             $template->restoreXML();
             $template->parse();
         }
     }
 }