diff --git a/admin-dev/tabs/AdminShop.php b/admin-dev/tabs/AdminShop.php index b6eac9e43..aeb4faffb 100755 --- a/admin-dev/tabs/AdminShop.php +++ b/admin-dev/tabs/AdminShop.php @@ -34,7 +34,7 @@ class AdminShop extends AdminTab $this->table = 'shop'; $this->className = 'Shop'; $this->edit = true; - $this->delete = false; + $this->delete = true; $this->deleted = false; $this->_select = 'gs.name group_shop_name, cl.name category_name'; @@ -73,6 +73,20 @@ class AdminShop extends AdminTab $newShop->copyShopData((int)Tools::getValue('importFromShop'), $importData); } + public function getList($id_lang, $orderBy = NULL, $orderWay = NULL, $start = 0, $limit = NULL, $id_lang_shop = false) + { + parent::getList($id_lang, $orderBy, $orderWay, $start, $limit, $id_lang_shop); + $shop_delete_list = array(); + + // test store authorized to remove + foreach($this->_list as $shop) + { + if(Shop::has_dependency($shop['id_shop'])) + $shop_delete_list[] = $shop['id_shop']; + } + $this->_listSkipDelete = $shop_delete_list; + } + public function postProcess() { if ((Tools::isSubmit('status') || Tools::isSubmit('status'.$this->table) || (Tools::isSubmit('submitAdd'.$this->table) && Tools::getValue($this->identifier) && !Tools::getValue('active'))) && $this->loadObject() && $this->loadObject()->active) diff --git a/classes/shop/Shop.php b/classes/shop/Shop.php index 396d90769..94ed3d506 100644 --- a/classes/shop/Shop.php +++ b/classes/shop/Shop.php @@ -155,7 +155,7 @@ class ShopCore extends ObjectModel public function delete() { - if (!$res = parent::delete()) + if (self::has_dependency($this->id) || !$res = parent::delete()) return false; foreach (Shop::getAssoTables() as $table_name => $row) @@ -173,6 +173,27 @@ class ShopCore extends ObjectModel return $res; } + /** + * Detect dependency with customer or orders + * + * @return bool + */ + public static function has_dependency($id_shop) + { + $has_dependency = false; + $nbr_customer = (int)Db::getInstance()->getValue('SELECT `id_customer` FROM `'._DB_PREFIX_.'customer` WHERE `id_shop`='.(int)$id_shop); + if($nbr_customer) + $has_dependency = true; + else + { + $nbr_order= (int)Db::getInstance()->getValue('SELECT `id_order` FROM `'._DB_PREFIX_.'orders` WHERE `id_shop`='.(int)$id_shop); + if($nbr_order) + $has_dependency = true; + } + + return $has_dependency; + } + /** * Get a new instance of a shop *