Esempio n. 1
0
 /**
  * For a given {product, product attribute} gets warehouse list
  *
  * @param int $product_id ID of the product
  * @param int $product_attribute_id Optional, uses 0 if this product does not have attributes
  * @param int $shop_id Optional, ID of the shop. Uses the context shop id (@see JeproshopContext::shop)
  * @return array Warehouses (ID, reference/name concatenated)
  */
 public static function getProductWarehouseList($product_id, $product_attribute_id = 0, $shop_id = null)
 {
     $db = JFactory::getDBO();
     // if it's a pack, returns warehouses if and only if some products use the advanced stock management
     if (JeproshopProductPack::isPack($product_id)) {
         $warehouses = JeproshopWarehouseModelWarehouse::getPackWarehouses($product_id);
         /*$res = array();
         		foreach ($warehouses as $warehouse)
         			$res[]['id_warehouse'] = $warehouse; */
         return $warehouses;
     }
     $share_stock = false;
     if ($shop_id === null) {
         if (JeproshopShopModelShop::getShopContext() == JeproshopShopModelShop::CONTEXT_GROUP) {
             $shop_group = JeproshopShopModelShop::getContextShopGroup();
         } else {
             $shop_group = JeproshopContext::getContext()->shop->getShopGroup();
             $shop_id = (int) JeproshopContext::getContext()->shop->shop_id;
         }
         $share_stock = $shop_group->share_stock;
     } else {
         $shop_group = JeproshopShopModelShop::getGroupFromShop($shop_id);
         $share_stock = $shop_group->share_stock;
     }
     if ($share_stock) {
         $shop_ids = JeproshopShopModelShop::getShops(true, (int) $shop_group->shop_group_id, true);
     } else {
         $shop_ids = array((int) $shop_id);
     }
     $query = "SELECT warehouse_product_location.warehouse_id, CONCAT(warehouse.reference, ' - ', warehouse.name)";
     $query .= " AS name FROM " . $db->QuoteName('#__jeproshop_warehouse_product_location') . " AS warehouse_product_location";
     $query .= " INNER JOIN " . $db->quoteName('#__jeproshop_warehouse_shop') . " AS warehouse_shop ON(warehouse_shop.";
     $query .= "warehouse_id = warehouse_product_location.warehouse_id AND shop_id IN (" . implode(',', array_map('intval', $shop_ids));
     $query .= " )) INNER JOIN " . $db->quoteName('#__jeproshop_warehouse') . " AS warehouse ON (warehouse.warehouse_id = warehouse_shop.";
     $query .= "warehouse_id ) WHERE product_id = " . (int) $product_id . " AND product_attribute_id = " . (int) $product_attribute_id;
     $query .= " AND warehouse.deleted = 0 GROUP BY warehouse_product_location.warehouse_id";
     $db->setQuery($query);
     return $db->loadObjectList();
 }
Esempio n. 2
0
 public function getPackageList($flush = false)
 {
     static $cache = array();
     if (isset($cache[(int) $this->cart_id . '_' . (int) $this->address_delivery_id]) && $cache[(int) $this->cart_id . '_' . (int) $this->address_delivery_id] !== false && !$flush) {
         return $cache[(int) $this->cart_id . '_' . (int) $this->address_delivery_id];
     }
     $product_list = $this->getProducts();
     // Step 1 : Get product informations (warehouse_list and carrier_list), count warehouse
     // Determine the best warehouse to determine the packages
     // For that we count the number of time we can use a warehouse for a specific delivery address
     $warehouse_count_by_address = array();
     $warehouse_carrier_list = array();
     $stock_management_active = JeproshopSettingModelSetting::getValue('advanced_stock_management');
     foreach ($product_list as &$product) {
         if ((int) $product->address_delivery_id == 0) {
             $product->address_delivery_id = (int) $this->address_delivery_id;
         }
         if (!isset($warehouse_count_by_address[$product->address_delivery_id])) {
             $warehouse_count_by_address[$product->address_delivery_id] = array();
         }
         $product->warehouse_list = array();
         if ($stock_management_active && ((int) $product['advanced_stock_management'] == 1 || Pack::usesAdvancedStockManagement((int) $product->product_id))) {
             $warehouse_list = Warehouse::getProductWarehouseList($product->product_id, $product->roduct_attribute_id, $this->shop_id);
             if (count($warehouse_list) == 0) {
                 $warehouse_list = Warehouse::getProductWarehouseList($product->product_id, $product->roduct_attribute_id);
             }
             // Does the product is in stock ?
             // If yes, get only warehouse where the product is in stock
             $warehouse_in_stock = array();
             $manager = StockManagerFactory::getManager();
             foreach ($warehouse_list as $key => $warehouse) {
                 $product_real_quantities = $manager->getProductRealQuantities($product->product_id, $product->product_attribute_id, array($warehouse->warehouse_id), true);
                 if ($product_real_quantities > 0 || Pack::isPack((int) $product->product_id)) {
                     $warehouse_in_stock[] = $warehouse;
                 }
             }
             if (!empty($warehouse_in_stock)) {
                 $warehouse_list = $warehouse_in_stock;
                 $product->in_stock = true;
             } else {
                 $product->in_stock = false;
             }
         } else {
             //simulate default warehouse
             $warehouse_list = array(0);
             $product->in_stock = StockAvailable::getQuantityAvailableByProduct($product->product_id, $product->product_attribute_id) > 0;
         }
         foreach ($warehouse_list as $warehouse) {
             if (!isset($warehouse_carrier_list[$warehouse->warehouse_id])) {
                 $warehouse_object = new JeproshopWarehouseModelWarehouse($warehouse->warehouse_id);
                 $warehouse_carrier_list[$warehouse->warehouse_id] = $warehouse_object->getCarriers();
             }
             $product->warehouse_list[] = $warehouse->warehouse_id;
             if (!isset($warehouse_count_by_address[$product->address_delivery_id][$warehouse->warehouse_id])) {
                 $warehouse_count_by_address[$product->address_delivery_id][$warehouse->warehouse_id] = 0;
             }
             $warehouse_count_by_address[$product->address_delivery_id][$warehouse->warehouse_id]++;
         }
     }
     unset($product);
     arsort($warehouse_count_by_address);
     // Step 2 : Group product by warehouse
     $grouped_by_warehouse = array();
     foreach ($product_list as &$product) {
         if (!isset($grouped_by_warehouse[$product->address_delivery_id])) {
             $grouped_by_warehouse[$product->address_delivery_id] = array('in_stock' => array(), 'out_of_stock' => array());
         }
         $product->carrier_list = array();
         $warehouse_id = 0;
         foreach ($warehouse_count_by_address[$product->address_delivery_id] as $war_id => $val) {
             if (in_array((int) $war_id, $product->warehouse_list)) {
                 $product->carrier_list = array_merge($product->carrier_list, Carrier::getAvailableCarrierList(new Product($product->product_id), $war_id, $product->address_delivery_id, null, $this));
                 if (!$warehouse_id) {
                     $warehouse_id = (int) $war_id;
                 }
             }
         }
         if (!isset($grouped_by_warehouse[$product->address_delivery_id]['in_stock'][$warehouse_id])) {
             $grouped_by_warehouse[$product->address_delivery_id]['in_stock'][$warehouse_id] = array();
             $grouped_by_warehouse[$product->address_delivery_id]['out_of_stock'][$warehouse_id] = array();
         }
         if (!$this->allow_separated_package) {
             $key = 'in_stock';
         } else {
             $key = $product->in_stock ? 'in_stock' : 'out_of_stock';
         }
         if (empty($product->carrier_list)) {
             $product->carrier_list = array(0);
         }
         $grouped_by_warehouse[$product->address_delivery_id][$key][$warehouse_id][] = $product;
     }
     unset($product);
     // Step 3 : grouped product from grouped_by_warehouse by available carriers
     $grouped_by_carriers = array();
     foreach ($grouped_by_warehouse as $address_delivery_id => $products_in_stock_list) {
         if (!isset($grouped_by_carriers[$address_delivery_id])) {
             $grouped_by_carriers[$address_delivery_id] = array('in_stock' => array(), 'out_of_stock' => array());
         }
         foreach ($products_in_stock_list as $key => $warehouse_list) {
             if (!isset($grouped_by_carriers[$address_delivery_id][$key])) {
                 $grouped_by_carriers[$address_delivery_id][$key] = array();
             }
             foreach ($warehouse_list as $warehouse_id => $product_list) {
                 if (!isset($grouped_by_carriers[$address_delivery_id][$key][$warehouse_id])) {
                     $grouped_by_carriers[$address_delivery_id][$key][$warehouse_id] = array();
                 }
                 foreach ($product_list as $product) {
                     $package_carriers_key = implode(',', $product->carrier_list);
                     if (!isset($grouped_by_carriers[$address_delivery_id][$key][$warehouse_id][$package_carriers_key])) {
                         $grouped_by_carriers[$address_delivery_id][$key][$warehouse_id][$package_carriers_key] = array('product_list' => array(), 'carrier_list' => $product->carrier_list, 'warehouse_list' => $product->warehouse_list);
                     }
                     $grouped_by_carriers[$address_delivery_id][$key][$warehouse_id][$package_carriers_key]['product_list'][] = $product;
                 }
             }
         }
     }
     $package_list = array();
     // Step 4 : merge product from grouped_by_carriers into $package to minimize the number of package
     foreach ($grouped_by_carriers as $address_delivery_id => $products_in_stock_list) {
         if (!isset($package_list[$address_delivery_id])) {
             $package_list[$address_delivery_id] = array('in_stock' => array(), 'out_of_stock' => array());
         }
         foreach ($products_in_stock_list as $key => $warehouse_list) {
             if (!isset($package_list[$address_delivery_id][$key])) {
                 $package_list[$address_delivery_id][$key] = array();
             }
             // Count occurrence of each carriers to minimize the number of packages
             $carrier_count = array();
             foreach ($warehouse_list as $warehouse_id => $products_grouped_by_carriers) {
                 foreach ($products_grouped_by_carriers as $data) {
                     foreach ($data['carrier_list'] as $carrier_id) {
                         if (!isset($carrier_count[$carrier_id])) {
                             $carrier_count[$carrier_id] = 0;
                         }
                         $carrier_count[$carrier_id]++;
                     }
                 }
             }
             arsort($carrier_count);
             foreach ($warehouse_list as $warehouse_id => $products_grouped_by_carriers) {
                 if (!isset($package_list[$address_delivery_id][$key][$warehouse_id])) {
                     $package_list[$address_delivery_id][$key][$warehouse_id] = array();
                 }
                 foreach ($products_grouped_by_carriers as $data) {
                     foreach ($carrier_count as $carrier_id => $rate) {
                         if (in_array($carrier_id, $data['carrier_list'])) {
                             if (!isset($package_list[$address_delivery_id][$key][$warehouse_id][$carrier_id])) {
                                 $package_list[$address_delivery_id][$key][$warehouse_id][$carrier_id] = array('carrier_list' => $data['carrier_list'], 'warehouse_list' => $data['warehouse_list'], 'product_list' => array());
                             }
                             $package_list[$address_delivery_id][$key][$warehouse_id][$carrier_id]['carrier_list'] = array_intersect($package_list[$address_delivery_id][$key][$warehouse_id][$carrier_id]['carrier_list'], $data['carrier_list']);
                             $package_list[$address_delivery_id][$key][$warehouse_id][$carrier_id]['product_list'] = array_merge($package_list[$address_delivery_id][$key][$warehouse_id][$carrier_id]['product_list'], $data['product_list']);
                             break;
                         }
                     }
                 }
             }
         }
     }
     // Step 5 : Reduce depth of $package_list
     $final_package_list = array();
     foreach ($package_list as $address_delivery_id => $products_in_stock_list) {
         if (!isset($final_package_list[$address_delivery_id])) {
             $final_package_list[$address_delivery_id] = array();
         }
         foreach ($products_in_stock_list as $key => $warehouse_list) {
             foreach ($warehouse_list as $warehouse_id => $products_grouped_by_carriers) {
                 foreach ($products_grouped_by_carriers as $data) {
                     $final_package_list[$address_delivery_id][] = array('product_list' => $data['product_list'], 'carrier_list' => $data['carrier_list'], 'warehouse_list' => $data['warehouse_list'], 'warehouse_id' => $warehouse_id);
                 }
             }
         }
     }
     $cache[(int) $this->cart_id] = $final_package_list;
     return $final_package_list;
 }
Esempio n. 3
0
 /**
  * For a given id_product, synchronizes StockAvailable::quantity with Stock::usable_quantity
  *
  * @param int $product_id
  * @param int $order_shop_id
  * @return bool
  */
 public static function synchronize($product_id, $order_shop_id = null)
 {
     if (!JeproshopTools::isUnsignedInt($product_id)) {
         return false;
     }
     $db = JFactory::getDBO();
     // gets warehouse ids grouped by shops
     $warehouse_ids = JeproshopWarehouseModelWarehouse::getWarehousesGroupedByShops();
     if ($order_shop_id !== null) {
         $order_warehouses = array();
         $warehouses = JeproshopWarehouseModelWarehouse::getWarehouses(false, (int) $order_shop_id);
         foreach ($warehouses as $warehouse) {
             $order_warehouses[] = $warehouse->warehouse_id;
         }
     }
     // gets all product attributes ids
     $product_attribute_ids = array();
     foreach (JeproshopProductModelProduct::getProductAttributesIds($product_id) as $product_attribute_id) {
         $product_attribute_ids[] = $product_attribute_id->product_attribute_id;
     }
     // Allow to order the product when out of stock?
     $out_of_stock = JeproshopStockAvailableModelStockAvailable::outOfStock($product_id);
     $manager = JeproshopStockManagerFactory::getManager();
     // loops on $ids_warehouse to synchronize quantities
     foreach ($warehouse_ids as $shop_id => $warehouses) {
         // first, checks if the product depends on stock for the given shop $id_shop
         if (JeproshopStockAvailableModelStockAvailable::dependsOnStock($product_id, $shop_id)) {
             // init quantity
             $product_quantity = 0;
             // if it's a simple product
             if (empty($product_attribute_ids)) {
                 $allowed_warehouse_for_product = JeproshopWarehouseModelWarehouse::getProductWarehouseList((int) $product_id, 0, (int) $shop_id);
                 $allowed_warehouse_for_product_clean = array();
                 foreach ($allowed_warehouse_for_product as $warehouse) {
                     $allowed_warehouse_for_product_clean[] = (int) $warehouse->warehouse_id;
                 }
                 $allowed_warehouse_for_product_clean = array_intersect($allowed_warehouse_for_product_clean, $warehouses);
                 if ($order_shop_id != null && !count(array_intersect($allowed_warehouse_for_product_clean, $order_warehouses))) {
                     continue;
                 }
                 $product_quantity = $manager->getProductRealQuantities($product_id, null, $allowed_warehouse_for_product_clean, true);
                 /*Hook::exec('actionUpdateQuantity',
                 		array(
                 		'id_product' => $id_product,
                 		'id_product_attribute' => 0,
                 		'quantity' => $product_quantity
                 		)
                 		);*/
             } else {
                 // else this product has attributes, hence loops on $ids_product_attribute
                 foreach ($product_attribute_ids as $product_attribute_id) {
                     $allowed_warehouse_for_combination = JeproshopWarehouseModelWarehouse::getProductWarehouseList((int) $product_id, (int) $product_attribute_id, (int) $shop_id);
                     $allowed_warehouse_for_combination_clean = array();
                     foreach ($allowed_warehouse_for_combination as $warehouse) {
                         $allowed_warehouse_for_combination_clean[] = (int) $warehouse->warehouse_id;
                     }
                     $allowed_warehouse_for_combination_clean = array_intersect($allowed_warehouse_for_combination_clean, $warehouses);
                     if ($order_shop_id != null && !count(array_intersect($allowed_warehouse_for_combination_clean, $order_warehouses))) {
                         continue;
                     }
                     $quantity = $manager->getProductRealQuantities($product_id, $product_attribute_id, $allowed_warehouse_for_combination_clean, true);
                     $query = new DbQuery();
                     $query->select('COUNT(*)');
                     $query->from('stock_available');
                     $query->where('id_product = ' . (int) $product_id . ' AND id_product_attribute = ' . (int) $product_attribute_id . StockAvailable::addSqlShopRestriction(null, $shop_id));
                     if ((int) Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query)) {
                         $query = array('table' => 'stock_available', 'data' => array('quantity' => $quantity), 'where' => 'id_product = ' . (int) $product_id . ' AND id_product_attribute = ' . (int) $product_attribute_id . StockAvailable::addSqlShopRestriction(null, $shop_id));
                         Db::getInstance()->update($query['table'], $query['data'], $query['where']);
                     } else {
                         $query = array('table' => 'stock_available', 'data' => array('quantity' => $quantity, 'depends_on_stock' => 1, 'out_of_stock' => $out_of_stock, 'id_product' => (int) $id_product, 'id_product_attribute' => (int) $id_product_attribute));
                         StockAvailable::addSqlShopParams($query['data']);
                         Db::getInstance()->insert($query['table'], $query['data']);
                     }
                     $product_quantity += $quantity;
                     Hook::exec('actionUpdateQuantity', array('id_product' => $id_product, 'id_product_attribute' => $id_product_attribute, 'quantity' => $quantity));
                 }
             }
             // updates
             // if $id_product has attributes, it also updates the sum for all attributes
             $query = array('table' => 'stock_available', 'data' => array('quantity' => $product_quantity), 'where' => 'id_product = ' . (int) $id_product . ' AND id_product_attribute = 0' . StockAvailable::addSqlShopRestriction(null, $shop_id));
             Db::getInstance()->update($query['table'], $query['data'], $query['where']);
         }
     }
     // In case there are no warehouses, removes product from StockAvailable
     if (count($warehouse_ids) == 0 && JeproshopStockAvailableModelStockAvailable::dependsOnStock((int) $product_id)) {
         $query = "UPDATE " . $db->quoteName('#__jeproshop_stock_available') . " SET " . $db->quoteName('quantity') . " = 0 ";
         $query .= " WHERE " . $db->quoteName('product_id') . " = " . (int) $product_id;
         $db->setQuery($query);
         $db->query();
     }
     JeproshopCache::clean('jeproshop_stock_available_get_quantity_available_by_product_' . (int) $product_id . '_*');
 }
Esempio n. 4
0
 public function renderView($tpl = NULL)
 {
     if (!isset($this->context) || $this->context == null) {
         $this->context = JeproshopContext::getContext();
     }
     $app = JFactory::getApplication();
     $order = new JeproshopOrderModelOrder($app->input->get('order_id'));
     if (!JeproshopTools::isLoadedObject($order, 'order_id')) {
         JError::raiseError(500, JText::_('COM_JEPROSHOP_THE_ORDER_CANNOT_BE_FOUND_WITHIN_YOUR_DATA_BASE_MESSAGE'));
     }
     $customer = new JeproshopCustomerModelCustomer($order->customer_id);
     $carrier = new JeproshopCarrierModelCarrier($order->carrier_id);
     $products = $this->getProducts($order);
     $currency = new JeproshopCurrencyModelCurrency((int) $order->currency_id);
     // Carrier module call
     $carrier_module_call = null;
     if ($carrier->is_module) {
         /*$module = Module::getInstanceByName($carrier->external_module_name);
         		if (method_exists($module, 'displayInfoByCart'))
         			$carrier_module_call = call_user_func(array($module, 'displayInfoByCart'), $order->id_cart); */
     }
     // Retrieve addresses information
     $addressInvoice = new JeproshopAddressModelAddress($order->address_invoice_id, $this->context->language->lang_id);
     if (JeproshopTools::isLoadedObject($addressInvoice, 'address_id') && $addressInvoice->state_id) {
         $invoiceState = new JeproshopStateModelState((int) $addressInvoice->state_id);
     }
     if ($order->address_invoice_id == $order->address_delivery_id) {
         $addressDelivery = $addressInvoice;
         if (isset($invoiceState)) {
             $deliveryState = $invoiceState;
         }
     } else {
         $addressDelivery = new JeproshopAddressModelAddress($order->address_delivery_id, $this->context->language->lang_id);
         if (JeproshopTools::isLoadedObject($addressDelivery, 'address_id') && $addressDelivery->state_id) {
             $deliveryState = new JeproshopStateModelState((int) $addressDelivery->state_id);
         }
     }
     $title = JText::_('COM_JEPROSHOP_ORDER_LABEL') . ' ';
     //todo learn how to display
     //$toolbar_title = sprintf($this->l('Order #%1$d (%2$s) - %3$s %4$s'), $order->order_id, $order->reference, $customer->firstname, $customer->lastname);
     if (JeproshopShopModelShop::isFeaturePublished()) {
         $shop = new JeproshopShopModelShop((int) $order->shop_id);
         //$this->toolbar_title .= ' - '.sprintf($this->l('Shop: %s'), $shop->name);
     }
     JToolBarHelper::title($title);
     // gets warehouses to ship products, if and only if advanced stock management is activated
     $warehouse_list = null;
     $order_details = $order->getOrderDetailList();
     foreach ($order_details as $order_detail) {
         $product = new JeproshopProductModelProduct($order_detail->product_id);
         if (JeproshopSettingModelSetting::getValue('advanced_stock_management') && $product->advanced_stock_management) {
             $warehouses = JeproshopWarehouseModelWarehouse::getWarehousesByProductId($order_detail->product_id, $order_detail->product_attribute_id);
             foreach ($warehouses as $warehouse) {
                 if (!isset($warehouse_list[$warehouse->warehouse_id])) {
                     $warehouse_list[$warehouse->warehouse_id] = $warehouse;
                 }
             }
         }
     }
     $payment_methods = array();
     /*foreach (PaymentModule::getInstalledPaymentModules() as $payment)
     		{
     			$module = Module::getInstanceByName($payment['name']);
     			if (Validate::isLoadedObject($module) && $module->active)
     				$payment_methods[] = $module->displayName;
     		}*/
     // display warning if there are products out of stock
     $display_out_of_stock_warning = false;
     $current_order_status = $order->getCurrentOrderStatus();
     if (JeproshopSettingModelSetting::getValue('stock_management') && (!JeproshopTools::isLoadedObject($current_order_status, 'order_id') || $current_order_status->delivery != 1 && $current_order_status->shipped != 1)) {
         $display_out_of_stock_warning = true;
     }
     // products current stock (from stock_available)
     foreach ($products as &$product) {
         $product->current_stock = JeproshopStockAvailableModelStockAvailable::getQuantityAvailableByProduct($product->product_id, $product->product_attribute_id, $product->shop_id);
         $resume = JeproshopOrderSlipModelOrderSlip::getProductSlipResume($product->order_detail_id);
         $product->quantity_refundable = $product->product_quantity - $resume->product_quantity;
         $product->amount_refundable = $product->total_price_tax_incl - $resume->amount_tax_incl;
         $product->amount_refund = JeproshopTools::displayPrice($resume->amount_tax_incl, $currency);
         $product->refund_history = JeproshopOrderSlipModelOrderSlip::getProductSlipDetail($product->order_detail_id);
         $product->return_history = JeproshopOrderReturnModelOrderReturn::getProductReturnDetail($product->order_detail_id);
         // if the current stock requires a warning
         if ($product->current_stock == 0 && $display_out_of_stock_warning) {
             JError::raiseWarning(500, JText::_('COM_JEPROSHOP_THIS_PRODUCT_IS_OUT_OF_STOCK_LABEL') . ' : ' . $product->product_name);
         }
         if ($product->warehouse_id != 0) {
             $warehouse = new JeproshopWarehouseModelWarehouse((int) $product->warehouse_id);
             $product->warehouse_name = $warehouse->name;
         } else {
             $product->warehouse_name = '--';
         }
     }
     //$gender = new Gender((int)$customer->id_gender, $this->context->language->id);
     $history = $order->getHistory($this->context->language->lang_id);
     foreach ($history as &$order_state) {
         $order_state->text_color = JeproshopTools::getBrightness($order_state->color) < 128 ? 'white' : 'black';
     }
     $this->setLayout('view');
     $this->assignRef('order', $order);
     $cart = new JeproshopCartModelCart($order->cart_id);
     $this->assignRef('cart', $cart);
     $this->assignRef('customer', $customer);
     $customer_addresses = $customer->getAddresses($this->context->language->lang_id);
     $this->assignRef('customer_addresses', $customer_addresses);
     $this->assignRef('delivery_address', $addressDelivery);
     $this->assignRef('deliveryState', isset($deliveryState) ? $deliveryState : null);
     $this->assignRef('invoice_address', $addressInvoice);
     $this->assignRef('invoiceState', isset($invoiceState) ? $invoiceState : null);
     $customerStats = $customer->getStats();
     $this->assignRef('customerStats', $customerStats);
     $this->assignRef('products', $products);
     $discounts = $order->getCartRules();
     $this->assignRef('discounts', $discounts);
     $orderTotalPaid = $order->getOrdersTotalPaid();
     $this->assignRef('orders_total_paid_tax_incl', $orderTotalPaid);
     // Get the sum of total_paid_tax_incl of the order with similar reference
     $totalPaid = $order->getTotalPaid();
     $this->assignRef('total_paid', $totalPaid);
     $returns = JeproshopOrderReturnModelOrderReturn::getOrdersReturn($order->customer_id, $order->order_id);
     $this->assignRef('returns', $returns);
     $customerThreads = JeproshopCustomerThreadModelCustomerThread::getCustomerMessages($order->customer_id);
     $this->assignRef('customer_thread_message', $customerThreads);
     $orderMessages = JeproshopOrderMessageModelOrderMessage::getOrderMessages($order->lang_id);
     $this->assignRef('order_messages', $orderMessages);
     $messages = JeproshopMessageModelMessage::getMessagesByOrderId($order->order_id, true);
     $this->assignRef('messages', $messages);
     $carrier = new JeproshopCarrierModelCarrier($order->carrier_id);
     $this->assignRef('carrier', $carrier);
     $this->assignRef('history', $history);
     $statues = JeproshopOrderStatusModelOrderStatus::getOrderStatus($this->context->language->lang_id);
     $this->assignRef('order_statues', $statues);
     $this->assignRef('warehouse_list', $warehouse_list);
     $sources = JeproshopConnectionSourceModelConnectionSource::getOrderSources($order->order_id);
     $this->assignRef('sources', $sources);
     $orderStatus = $order->getCurrentOrderStatus();
     $this->assignRef('current_status', $orderStatus);
     $this->assignRef('currency', new JeproshopCurrencyModelCurrency($order->currency_id));
     $currencies = JeproshopCurrencyModelCurrency::getCurrenciesByShopId($order->shop_id);
     $this->assignRef('currencies', $currencies);
     $previousOrder = $order->getPreviousOrderId();
     $this->assignRef('previousOrder', $previousOrder);
     $nextOrder = $order->getNextOrderId();
     $this->assignRef('nextOrder', $nextOrder);
     //$this->assignRef('current_index', self::$currentIndex);
     $this->assignRef('carrier_module_call', $carrier_module_call);
     $this->assignRef('iso_code_lang', $this->context->language->iso_code);
     $this->assignRef('lang_id', $this->context->language->lang_id);
     $can_edit = true;
     $this->assignRef('can_edit', $can_edit);
     //($this->tabAccess['edit'] == 1));
     $this->assignRef('current_id_lang', $this->context->language->lang_id);
     $invoiceCollection = $order->getInvoicesCollection();
     $this->assignRef('invoices_collection', $invoiceCollection);
     $unPaid = $order->getNotPaidInvoicesCollection();
     $this->assignRef('not_paid_invoices_collection', $unPaid);
     $this->assignRef('payment_methods', $payment_methods);
     $invoiceAllowed = JeproshopSettingModelSetting::getValue('invoice_allowed');
     $this->assignRef('invoice_management_active', $invoiceAllowed);
     $display_warehouse = (int) JeproshopSettingModelSetting::getValue('advanced_stock_management');
     $this->assignRef('display_warehouse', $display_warehouse);
     $stockManagement = JeproshopSettingModelSetting::getValue('stock_management');
     $this->assignRef('stock_management', $stockManagement);
     /*$this->assignRef('HOOK_CONTENT_ORDER', Hook::exec('displayAdminOrderContentOrder', array(
     				'order' => $order,
     				'products' => $products,
     				'customer' => $customer)
     				),
     		$this->assignRef('HOOK_CONTENT_SHIP' => Hook::exec('displayAdminOrderContentShip', array(
     				'order' => $order,
     				'products' => $products,
     				'customer' => $customer)
     				),
     		$this->assignRef('HOOK_TAB_ORDER' => Hook::exec('displayAdminOrderTabOrder', array(
     				'order' => $order,
     				'products' => $products,
     				'customer' => $customer)
     				
     		$this->assignRef('HOOK_TAB_SHIP' => Hook::exec('displayAdminOrderTabShip', array(
     		$this->assignRef('order' => $order,
     		$this->assignRef('products' => $products,
     		$this->assignRef('customer' => $customer) */
     $this->addToolBar();
     $this->sideBar = JHtmlSideBar::render();
     parent::display($tpl);
 }
Esempio n. 5
0
 /**
  * For a given {product, warehouse}, gets the carrier available
  *
  * @param JeproshopProductModelProduct $product The id of the product, or an array with at least the package size and weight
  * @param int $warehouse_id
  * @param int $address_delivery_id
  * @param int $shop_id
  * @param $cart
  * @return array
  */
 public static function getAvailableCarrierList(JeproshopProductModelProduct $product, $warehouse_id, $address_delivery_id = null, $shop_id = null, $cart = null)
 {
     if (is_null($shop_id)) {
         $shop_id = JeproshopContext::getContext()->shop->shop_id;
     }
     if (is_null($cart)) {
         $cart = JeproshopContext::getContext()->cart;
     }
     $address_id = (int) (!is_null($address_delivery_id) && $address_delivery_id != 0 ? $address_delivery_id : $cart->address_delivery_id);
     if ($address_id) {
         $address = new JeproshopAddressModelAddress($address_id);
         $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId($address->address_id);
         // Check the country of the address is activated
         if (!JeproshopAddressModelAddress::isCountryActiveById($address->address_id)) {
             return array();
         }
     } else {
         $country = new JeproshopCountryModelCountry(JeproshopSettingModelSetting::getValue('default_country'));
         $izone_id = $country->zone_id;
     }
     // Does the product is linked with carriers?
     $query = new DbQuery();
     $query->select('id_carrier');
     $query->from('product_carrier', 'pc');
     $query->innerJoin('carrier', 'c', 'c.id_reference = pc.id_carrier_reference AND c.deleted = 0');
     $query->where('pc.id_product = ' . (int) $product->product_id);
     $query->where('pc.id_shop = ' . (int) $shop_id);
     $cache_id = 'Carrier::getAvailableCarrierList_' . (int) $product->id . '-' . (int) $id_shop;
     if (!Cache::isStored($cache_id)) {
         $carriers_for_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
         Cache::store($cache_id, $carriers_for_product);
     }
     $carriers_for_product = Cache::retrieve($cache_id);
     $carrier_list = array();
     if (!empty($carriers_for_product)) {
         //the product is linked with carriers
         foreach ($carriers_for_product as $carrier) {
             //check if the linked carriers are available in current zone
             if (Carrier::checkCarrierZone($carrier['id_carrier'], $id_zone)) {
                 $carrier_list[] = $carrier['id_carrier'];
             }
         }
         if (empty($carrier_list)) {
             return array();
         }
         //no linked carrier are available for this zone
     }
     // The product is not directly linked with a carrier
     // Get all the carriers linked to a warehouse
     if ($warehouse_id) {
         $warehouse = new JeproshopWarehouseModelWarehouse($warehouse_id);
         $warehouse_carrier_list = $warehouse->getCarriers();
     }
     $available_carrier_list = array();
     $customer = new JeproshopCustomerModelCustomer($cart->customer_id);
     $carriers = JeproshopCarrierModelCarrier::getCarriersForOrder($zone_id, $customer->getGroups(), $cart);
     foreach ($carriers as $carrier) {
         $available_carrier_list[] = $carrier->carrier_id;
     }
     if ($carrier_list) {
         $carrier_list = array_intersect($available_carrier_list, $carrier_list);
     } else {
         $carrier_list = $available_carrier_list;
     }
     if (isset($warehouse_carrier_list)) {
         $carrier_list = array_intersect($carrier_list, $warehouse_carrier_list);
     }
     if ($product->width > 0 || $product->height > 0 || $product->depth > 0 || $product->weight > 0) {
         foreach ($carrier_list as $key => $carrier_id) {
             $carrier = new JeproshopCarrierModelCarrier($carrier_id);
             if ($carrier->max_width > 0 && $carrier->max_width < $product->width || $carrier->max_height > 0 && $carrier->max_height < $product->height || $carrier->max_depth > 0 && $carrier->max_depth < $product->depth || $carrier->max_weight > 0 && $carrier->max_weight < $product->weight) {
                 unset($carrier_list[$key]);
             }
         }
     }
     return $carrier_list;
 }
Esempio n. 6
0
 private function initQuantitiesForm()
 {
     if (!$this->context->controller->default_form_language) {
         $this->languages = $this->context->controller->getLanguages();
     }
     if ($this->product->product_id) {
         if ($this->product_exists_in_shop) {
             //Get all product_attribute_id
             $attributes = $this->product->getAttributesResume($this->context->language->lang_id);
             if (empty($attributes)) {
                 $attributes[] = new JObject();
                 $attributes[0]->set('product_attribute_id', 0);
                 $attributes[0]->set('attribute_designation', '');
             }
             /** get available quantities **/
             $available_quantity = array();
             $product_designation = array();
             foreach ($attributes as $attribute) {
                 $product_attribute_id = is_object($attribute) ? $attribute->product_attribute_id : $attribute['product_attribute_id'];
                 $attribute_designation = is_object($attribute) ? $attribute->attribute_designation : $attribute['attribute_designation'];
                 // Get available quantity for the current product attribute in the current shop
                 $available_quantity[$product_attribute_id] = JeproshopStockAvailableModelStockAvailable::getQuantityAvailableByProduct((int) $this->product->product_id, $product_attribute_id);
                 // Get all product designation
                 $product_designation[$product_attribute_id] = rtrim($this->product->name[$this->context->language->lang_id] . ' - ' . $attribute_designation, ' - ');
             }
             $show_quantities = true;
             $shop_context = JeproshopShopModelShop::getShopContext();
             $shop_group = new JeproshopShopGroupModelShopGroup((int) JeproshopShopModelShop::getContextShopGroupID());
             // if we are in all shops context, it's not possible to manage quantities at this level
             if (JeproshopShopModelShop::isFeaturePublished() && $shop_context == JeproshopShopModelShop::CONTEXT_ALL) {
                 $show_quantities = false;
                 // if we are in group shop context
             } elseif (JeproshopShopModelShop::isFeaturePublished() && $shop_context == JeproshopShopModelShop::CONTEXT_GROUP) {
                 // if quantities are not shared between shops of the group, it's not possible to manage them at group level
                 if (!$shop_group->share_stock) {
                     $show_quantities = false;
                 }
             } else {
                 // if we are in shop context
                 // if quantities are shared between shops of the group, it's not possible to manage them for a given shop
                 if ($shop_group->share_stock) {
                     $show_quantities = false;
                 }
             }
             $stock_management = JeproshopSettingModelSetting::getValue('stock_management');
             $this->assignRef('stock_management', $stock_management);
             $has_attribute = $this->product->hasAttributes();
             $this->assignRef('has_attribute', $has_attribute);
             // Check if product has combination, to display the available date only for the product or for each combination
             $db = JFactory::getDBO();
             if (JeproshopCombinationModelCombination::isFeaturePublished()) {
                 $query = "SELECT COUNT(product_id) FROM " . $db->quoteName('#__jeproshop_product_attribute') . " WHERE ";
                 $query .= " product_id = " . (int) $this->product->product_id;
                 $db->setQuery($query);
                 $countAttributes = (int) $db->loadResult();
             } else {
                 $countAttributes = false;
             }
             $this->assignRef('count_attributes', $countAttributes);
             // if advanced stock management is active, checks associations
             $advanced_stock_management_warning = false;
             if (JeproshopSettingModelSetting::getValue('advanced_stock_management') && $this->product->advanced_stock_management) {
                 $product_attributes = JeproshopProductModelProduct::getProductAttributesIds($this->product->product_id);
                 $warehouses = array();
                 if (!$product_attributes) {
                     $warehouses[] = JeproshopWarehouseModelWarehouse::getProductWarehouseList($this->product->product_id, 0);
                 }
                 foreach ($product_attributes as $product_attribute) {
                     $ws = JeproshopWarehouseModelWarehouse::getProductWarehouseList($this->product->product_id, $product_attribute->product_attribute_id);
                     if ($ws) {
                         $warehouses[] = $ws;
                     }
                 }
                 $warehouses = JeproshopTools::arrayUnique($warehouses);
                 if (empty($warehouses)) {
                     $advanced_stock_management_warning = true;
                 }
             }
             if ($advanced_stock_management_warning) {
                 JError::raiseWarning(500, JText::_('If you wish to use the advanced stock management, you must:'));
                 JError::raiseWarning(500, '- ' . JText::_('associate your products with warehouses.'));
                 JError::raiseWarning(500, '- ' . JText::_('associate your warehouses with carriers.'));
                 JError::raiseWarning(500, '- ' . JText::_('associate your warehouses with the appropriate shops.'));
             }
             $pack_quantity = null;
             // if product is a pack
             if (JeproshopProductPack::isPack($this->product->product_id)) {
                 $items = JeproshopProductPack::getItems((int) $this->product->product_id, JeproshopSettingModelSetting::getValue('default_lang'));
                 // gets an array of quantities (quantity for the product / quantity in pack)
                 $pack_quantities = array();
                 foreach ($items as $item) {
                     if (!$item->isAvailableWhenOutOfStock((int) $item->out_of_stock)) {
                         $pack_id_product_attribute = JeproshopProductModelProduct::getDefaultAttribute($item->product_id, 1);
                         $pack_quantities[] = JeproshopProductModelProduct::getQuantity($item->id, $pack_id_product_attribute) / ($item->pack_quantity !== 0 ? $item->pack_quantity : 1);
                     }
                 }
                 // gets the minimum
                 if (count($pack_quantities)) {
                     $pack_quantity = $pack_quantities[0];
                     foreach ($pack_quantities as $value) {
                         if ($pack_quantity > $value) {
                             $pack_quantity = $value;
                         }
                     }
                 }
                 if (!JeproshopWarehouseModelWarehouse::getPackWarehouses((int) $this->product->product_id)) {
                     $this->displayWarning($this->l('You must have a common warehouse between this pack and its product.'));
                 }
             }
             $this->assignRef('attributes', $attributes);
             $this->assignRef('available_quantity', $available_quantity);
             $this->assignRef('pack_quantity', $pack_quantity);
             $stock_management_active = JeproshopSettingModelSetting::getValue('advanced_stock_management');
             $this->assignRef('stock_management_active', $stock_management_active);
             $this->assignRef('product_designation', $product_designation);
             $this->assignRef('show_quantities', $show_quantities);
             $order_out_of_stock = JeproshopSettingModelSetting::getValue('allow_out_of_stock_ordering');
             $this->assignRef('order_out_of_stock', $order_out_of_stock);
             /*'token_preferences' => Tools::getAdminTokenLite('AdminPPreferences'),
                       'token' => $this->token,
                       'languages' => $this->_languages,
                       'id_lang' => $this->context->language->id
               ));*/
         } else {
             JError::raiseWarning(500, JText::_('You must save the product in this shop before managing quantities.'));
         }
     } else {
         JError::raiseWarning(500, JText::_('You must save this product before managing quantities.'));
     }
 }
Esempio n. 7
0
 /**
  * Post treatment for warehouses
  */
 public function processWarehouses()
 {
     $app = JFactory::getApplication();
     $product = new JeproshopProductModelProduct((int) $app->input->get('product_id'));
     if ((int) $app->input->get('warehouse_loaded') === 1 && JeproshopTools::isLoadedObject($product, 'product_id')) {
         // Get all id_product_attribute
         $warehouse_attributes = $product->getAttributesResume($this->context->language->lang_id);
         if (empty($warehouse_attributes)) {
             $attribute = new JObject();
             $attribute->set('product_attribute_id', 0);
             $attribute->set('attribute_designation', '');
             $warehouse_attributes[] = $attribute;
         }
         // Get all available warehouses
         $warehouses = JeproshopWarehouseModelWarehouse::getWarehouses(true);
         // Get already associated warehouses
         $associated_warehouses_collection = JeproshopWarehouseProductLocationModelWarehouseProductLocation::getCollection($product->product_id);
         $elements_to_manage = array();
         // get form information
         foreach ($warehouse_attributes as $attribute) {
             foreach ($warehouses as $warehouse) {
                 $key = $warehouse->warehouse_id . '_' . $product->product_id . '_' . $attribute->product_attribute_id;
                 // get elements to manage
                 if ($app->input->get('check_warehouse_' . $key)) {
                     $location = $app->input->get('location_warehouse_' . $key, '');
                     $elements_to_manage[$key] = $location;
                 }
             }
         }
         // Delete entry if necessary
         foreach ($associated_warehouses_collection as $awc) {
             if (!array_key_exists($awc->warehouse_id . '_' . $awc->product_id . '_' . $awc->product_attribute_id, $elements_to_manage)) {
                 $awc->delete();
             }
         }
         // Manage locations
         foreach ($elements_to_manage as $key => $location) {
             $params = explode('_', $key);
             $wpl_id = (int) JeproshopWarehouseProductLocationModelWarehouseProductLocation::getIdByProductAndWarehouse((int) $params[1], (int) $params[2], (int) $params[0]);
             if (empty($wpl_id)) {
                 //create new record
                 $warehouse_location_entity = new JeproshopWarehouseProductLocationModelWarehouseProductLocation();
                 $warehouse_location_entity->product_id = (int) $params[1];
                 $warehouse_location_entity->product_attribute_id = (int) $params[2];
                 $warehouse_location_entity->warehouse_id = (int) $params[0];
                 $warehouse_location_entity->location = JFactory::getDBO()->query($location);
                 $warehouse_location_entity->save();
             } else {
                 $warehouse_location_entity = new WarehouseProductLocation((int) $wpl_id);
                 $location = $location;
                 if ($location != $warehouse_location_entity->location) {
                     $warehouse_location_entity->location = $location;
                     $warehouse_location_entity->update();
                 }
             }
         }
         JeproshopStockAvailableModelStockAvailable::synchronize((int) $product->product_id);
     }
 }