diff --git a/classes/Address.php b/classes/Address.php index 3357ad57e..76651b940 100644 --- a/classes/Address.php +++ b/classes/Address.php @@ -27,15 +27,21 @@ class AddressCore extends ObjectModel { - /** @var integer Customer id which address belongs */ + /** @var integer Customer id which address belongs to */ public $id_customer = null; - /** @var integer Manufacturer id which address belongs */ + /** @var integer Manufacturer id which address belongs to */ public $id_manufacturer = null; - /** @var integer Supplier id which address belongs */ + /** @var integer Supplier id which address belongs to */ public $id_supplier = null; + /** + * @since 1.5.0 + * @var int Warehouse id which address belongs to + */ + public $id_warehouse = null; + /** @var integer Country id */ public $id_country; @@ -101,7 +107,8 @@ class AddressCore extends ObjectModel 'address1' => 128, 'address2' => 128, 'postcode' => 12, 'city' => 64, 'other' => 300, 'phone' => 16, 'phone_mobile' => 16, 'dni' => 16); protected $fieldsValidate = array('id_customer' => 'isNullOrUnsignedId', 'id_manufacturer' => 'isNullOrUnsignedId', - 'id_supplier' => 'isNullOrUnsignedId', 'id_country' => 'isUnsignedId', 'id_state' => 'isNullOrUnsignedId', + 'id_supplier' => 'isNullOrUnsignedId', 'id_warehouse' => 'isNullOrUnsignedId', + 'id_country' => 'isUnsignedId', 'id_state' => 'isNullOrUnsignedId', 'alias' => 'isGenericName', 'company' => 'isGenericName', 'lastname' => 'isName','vat_number' => 'isGenericName', 'firstname' => 'isName', 'address1' => 'isAddress', 'address2' => 'isAddress', 'postcode'=>'isPostCode', 'city' => 'isCityName', 'other' => 'isMessage', @@ -121,6 +128,7 @@ class AddressCore extends ObjectModel 'id_customer' => array('xlink_resource'=> 'customers'), 'id_manufacturer' => array('xlink_resource'=> 'manufacturers'), 'id_supplier' => array('xlink_resource'=> 'suppliers'), + 'id_warehouse' => array('xlink_resource'=> 'warehouse'), 'id_country' => array('xlink_resource'=> 'countries'), 'id_state' => array('xlink_resource'=> 'states'), ), @@ -177,9 +185,6 @@ class AddressCore extends ObjectModel return $out; } - - - public function getFields() { $this->validateFields(); @@ -188,6 +193,7 @@ class AddressCore extends ObjectModel $fields['id_customer'] = is_null($this->id_customer) ? 0 : (int)$this->id_customer; $fields['id_manufacturer'] = is_null($this->id_manufacturer) ? 0 : (int)$this->id_manufacturer; $fields['id_supplier'] = is_null($this->id_supplier) ? 0 : (int)$this->id_supplier; + $fields['id_warehouse'] = is_null($this->id_warehouse) ? 0 : (int)$this->id_warehouse; $fields['id_country'] = (int)$this->id_country; $fields['id_state'] = (int)$this->id_state; $fields['alias'] = pSQL($this->alias); @@ -249,7 +255,7 @@ class AddressCore extends ObjectModel */ public static function isCountryActiveById($id_address) { - if (!$result = Db::getInstance()->getRow(' + if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT c.`active` FROM `'._DB_PREFIX_.'address` a LEFT JOIN `'._DB_PREFIX_.'country` c ON c.`id_country` = a.`id_country` @@ -265,7 +271,7 @@ class AddressCore extends ObjectModel */ public function isUsed() { - $result = Db::getInstance()->getRow(' + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT COUNT(`id_order`) AS used FROM `'._DB_PREFIX_.'orders` WHERE `id_address_delivery` = '.(int)$this->id.' @@ -293,7 +299,7 @@ class AddressCore extends ObjectModel */ public static function addressExists($id_address) { - $row = Db::getInstance()->getRow(' + $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT `id_address` FROM '._DB_PREFIX_.'address a WHERE a.`id_address` = '.(int)$id_address); @@ -303,7 +309,7 @@ class AddressCore extends ObjectModel public static function getFirstCustomerAddressId($id_customer, $active = true) { - return Db::getInstance()->getValue(' + return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_address` FROM `'._DB_PREFIX_.'address` WHERE `id_customer` = '.(int)$id_customer.' AND `deleted` = 0'.($active ? ' AND `active` = 1' : '') @@ -336,5 +342,20 @@ class AddressCore extends ObjectModel return $address; } + + /** + * Returns id_address for a given id_supplier + * @since 1.5.0 + * @param int $id_supplier + * @return int $id_address + */ + public static function getAddressIdBySupplierId($id_supplier) + { + $query = new DbQuery(); + $query->select('id_address'); + $query->from('address'); + $query->where('id_supplier = '.(int)$id_supplier); + return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); + } } diff --git a/classes/Supplier.php b/classes/Supplier.php index 02a395cc6..8ef14547d 100644 --- a/classes/Supplier.php +++ b/classes/Supplier.php @@ -59,9 +59,15 @@ class SupplierCore extends ObjectModel /** @var boolean active */ public $active; + /** + * @since 1.5.0 + * @var int address + * */ + public $id_address; + protected $fieldsRequired = array('name'); protected $fieldsSize = array('name' => 64); - protected $fieldsValidate = array('name' => 'isCatalogName'); + protected $fieldsValidate = array('name' => 'isCatalogName', 'id_address' => 'isUnsignedId'); protected $fieldsSizeLang = array('meta_title' => 128, 'meta_description' => 255, 'meta_keywords' => 255); protected $fieldsValidateLang = array( 'description' => 'isGenericName', @@ -104,6 +110,7 @@ class SupplierCore extends ObjectModel $fields['date_add'] = pSQL($this->date_add); $fields['date_upd'] = pSQL($this->date_upd); $fields['active'] = (int)$this->active; + $fields['id_address'] = (int)$this->id_address; return $fields; } diff --git a/classes/stock/SupplyOrder.php b/classes/stock/SupplyOrder.php index d763b45c5..bf028464d 100755 --- a/classes/stock/SupplyOrder.php +++ b/classes/stock/SupplyOrder.php @@ -418,4 +418,26 @@ class SupplyOrderCore extends ObjectModel return ($res > 0); } -} \ No newline at end of file + /** + * For a given $id_supplier, tells if it has pending supply orders + * + * @param int $id_supplier + * @return bool + */ + public static function supplierHasPendingOrders($id_supplier) + { + if (!$id_supplier) + return false; + + $query = new DbQuery(); + $query->select('COUNT(so.id_supply_order) as supply_orders'); + $query->from('supply_order so'); + $query->leftJoin('supply_order_state sos ON (so.id_supply_order_state = sos.id_supply_order_state)'); + $query->where('sos.enclosed != 1'); + $query->where('so.id_supplier = '.(int)$id_supplier); + + $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); + return ($res > 0); + } + +} diff --git a/controllers/admin/AdminManufacturersController.php b/controllers/admin/AdminManufacturersController.php index f6c8731e3..591da4487 100644 --- a/controllers/admin/AdminManufacturersController.php +++ b/controllers/admin/AdminManufacturersController.php @@ -190,7 +190,7 @@ class AdminManufacturersControllerCore extends AdminController $this->_join .= ' LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (a.`id_manufacturer` = m.`id_manufacturer`)'; - $this->_where = 'AND a.`id_customer` = 0'; + $this->_where = 'AND a.`id_customer` = 0 AND a.`id_supplier` = 0 AND a.`id_warehouse` = 0'; $this->context->smarty->assign('title_list', $this->l('Manufacturers addresses:')); diff --git a/controllers/admin/AdminSuppliersController.php b/controllers/admin/AdminSuppliersController.php index 98a62a71b..3c75aa024 100644 --- a/controllers/admin/AdminSuppliersController.php +++ b/controllers/admin/AdminSuppliersController.php @@ -86,6 +86,67 @@ class AdminSuppliersControllerCore extends AdminController 'hint' => $this->l('Invalid characters:').' <>;=#{}', 'desc' => $this->l('Will appear in supplier list') ), + array( + 'type' => 'text', + 'label' => $this->l('Phone:'), + 'name' => 'phone', + 'size' => 15, + 'maxlength' => 16, + 'desc' => $this->l('Phone number of this supplier') + ), + array( + 'type' => 'text', + 'label' => $this->l('Adress:'), + 'name' => 'address', + 'size' => 100, + 'maxlength' => 128, + 'required' => true + ), + array( + 'type' => 'text', + 'label' => $this->l('Adress:').' (2)', + 'name' => 'address2', + 'size' => 100, + 'maxlength' => 128, + ), + array( + 'type' => 'text', + 'label' => $this->l('Postcode/ Zip Code:'), + 'name' => 'postcode', + 'size' => 10, + 'maxlength' => 12, + 'required' => true, + ), + array( + 'type' => 'text', + 'label' => $this->l('City:'), + 'name' => 'city', + 'size' => 20, + 'maxlength' => 32, + 'required' => true, + ), + array( + 'type' => 'select', + 'label' => $this->l('Country:'), + 'name' => 'id_country', + 'required' => true, + 'options' => array( + 'query' => Country::getCountries($this->context->language->id, false), + 'id' => 'id_country', + 'name' => 'name' + ), + 'desc' => $this->l('Country where the state, region or city is located') + ), + array( + 'type' => 'select', + 'label' => $this->l('State'), + 'name' => 'id_state', + 'required' => true, + 'options' => array( + 'id' => 'id_state', + 'name' => 'name' + ) + ), array( 'type' => 'file', 'label' => $this->l('Logo:'), @@ -210,16 +271,101 @@ class AdminSuppliersControllerCore extends AdminController public function afterImageUpload() { /* Generate image with differents size */ - if (($id_supplier = (int)(Tools::getValue('id_supplier'))) AND isset($_FILES) AND count($_FILES) AND file_exists(_PS_SUPP_IMG_DIR_.$id_supplier.'.jpg')) + if (($id_supplier = (int)Tools::getValue('id_supplier')) AND + isset($_FILES) AND count($_FILES) AND file_exists(_PS_SUPP_IMG_DIR_.$id_supplier.'.jpg')) { - $imagesTypes = ImageType::getImagesTypes('suppliers'); - foreach ($imagesTypes AS $k => $imageType) + $images_types = ImageType::getImagesTypes('suppliers'); + foreach ($images_types as $k => $image_type) { $file = _PS_SUPP_IMG_DIR_.$id_supplier.'.jpg'; - imageResize($file, _PS_SUPP_IMG_DIR_.$id_supplier.'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height'])); + imageResize($file, _PS_SUPP_IMG_DIR_.$id_supplier.'-'.stripslashes($image_type['name']).'.jpg', (int)$image_type['width'], (int)$image_type['height']); } } } + + /** + * AdminController::postProcess() override + * @see AdminController::postProcess() + */ + public function postProcess() + { + // checks access + if (Tools::isSubmit('submitAdd'.$this->table) && !($this->tabAccess['add'] === '1')) + { + $this->_errors[] = Tools::displayError('You do not have the required permissions to add suppliers.'); + return parent::postProcess(); + } + + if (Tools::isSubmit('submitAdd'.$this->table)) + { + if (!($obj = $this->loadObject(true))) + return; + + // updates/creates address if it does not exist + if (Tools::isSubmit('id_address') && (int)Tools::getValue('id_address') > 0) + $address = new Address((int)Tools::getValue('id_address')); // updates address + else + $address = new Address(); // creates address + + $address->alias = Tools::getValue('name', null); + $address->lastname = 'supplier'; // skip problem with numeric characters in supplier name + $address->firstname = 'supplier'; // skip problem with numeric characters in supplier name + $address->address1 = Tools::getValue('address', null); + $address->address2 = Tools::getValue('address2', null); + $address->postcode = Tools::getValue('postcode', null); + $address->phone = Tools::getValue('phone', null); + $address->id_country = Tools::getValue('id_country', null); + $address->id_state = Tools::getValue('id_state', null); + $address->city = Tools::getValue('city', null); + + $validation = $address->validateController(); + + // checks address validity + if (count($validation) > 0) + { + foreach ($validation as $item) + $this->_errors[] = $item; + $this->_errors[] = Tools::displayError('The address is not correct. Check if all required fields are filled.'); + } + else + { + if (Tools::isSubmit('id_address') && Tools::getValue('id_address') > 0) + $address->update(); + else + { + $address->save(); + $_POST['id_address'] = $address->id; + } + } + return parent::postProcess(); + } + else if (Tools::isSubmit('delete'.$this->table)) + if (!($obj = $this->loadObject(true))) + return; + else if (SupplyOrder::supplierHasPendingOrders($obj->id)) + $this->_errors[] = $this->l('It is not possible to delete a supplier if there is/are pending supply order(s).'); + else + { + $address = new Address($obj->id_address); + $address->deleted = 1; + $address->save(); + return parent::postProcess(); + } + } + + /** + * @see AdminController::afterAdd() + */ + public function afterAdd($object) + { + $address = new Address($object->id_address); + if (Validate::isLoadedObject($address)) + { + $address->id_supplier = $object->id_address; + $address->save(); + } + return true; + } + } - diff --git a/controllers/admin/AdminWarehousesController.php b/controllers/admin/AdminWarehousesController.php index e45db7349..6d4354413 100644 --- a/controllers/admin/AdminWarehousesController.php +++ b/controllers/admin/AdminWarehousesController.php @@ -440,7 +440,12 @@ class AdminWarehousesControllerCore extends AdminController else if (SupplyOrder::warehouseHasPendingOrders($obj->id)) $this->_errors[] = $this->l('It is not possible to delete a Warehouse if it has pending supply orders.'); else + { + $address = new Address($obj->id_address); + $address->deleted = 1; + $address->save(); return parent::postProcess(); + } } /** @@ -475,4 +480,18 @@ class AdminWarehousesControllerCore extends AdminController return parent::renderView(); } + + /** + * @see AdminController::afterAdd() + */ + public function afterAdd($object) + { + $address = new Address($object->id_address); + if (Validate::isLoadedObject($address)) + { + $address->id_warehouse = $object->id_address; + $address->save(); + } + return true; + } } diff --git a/install-dev/sql/db.sql b/install-dev/sql/db.sql index 2385af730..d2c3d54cd 100644 --- a/install-dev/sql/db.sql +++ b/install-dev/sql/db.sql @@ -23,6 +23,7 @@ CREATE TABLE `PREFIX_address` ( `id_customer` int(10) unsigned NOT NULL default '0', `id_manufacturer` int(10) unsigned NOT NULL default '0', `id_supplier` int(10) unsigned NOT NULL default '0', + `id_warehouse` int(10) unsigned NOT NULL default '0', `alias` varchar(32) NOT NULL, `company` varchar(32) default NULL, `lastname` varchar(32) NOT NULL, @@ -45,7 +46,8 @@ CREATE TABLE `PREFIX_address` ( KEY `id_country` (`id_country`), KEY `id_state` (`id_state`), KEY `id_manufacturer` (`id_manufacturer`), - KEY `id_supplier` (`id_supplier`) + KEY `id_supplier` (`id_supplier`), + KEY `id_warehouse` (`id_warehouse`), ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; CREATE TABLE `PREFIX_alias` ( @@ -1611,6 +1613,7 @@ CREATE TABLE `PREFIX_subdomain` ( CREATE TABLE `PREFIX_supplier` ( `id_supplier` int(10) unsigned NOT NULL auto_increment, + `id_address` int(10) unsigned NOT NULL, `name` varchar(64) NOT NULL, `date_add` datetime NOT NULL, `date_upd` datetime NOT NULL, diff --git a/install-dev/sql/upgrade/1.5.0.2.sql b/install-dev/sql/upgrade/1.5.0.2.sql index 340d6d453..7d6c56b5e 100644 --- a/install-dev/sql/upgrade/1.5.0.2.sql +++ b/install-dev/sql/upgrade/1.5.0.2.sql @@ -9,4 +9,6 @@ INSERT INTO `PREFIX_access` (`id_profile`, `id_tab`, `view`, `add`, `edit`, `del ALTER TABLE `PREFIX_orders` DROP COLUMN `id_warehouse`; -ALTER TABLE `PREFIX_order_detail` ADD COLUMN `id_warehouse` int(10) unsigned DEFAULT 0 AFTER `id_order_invoice`; \ No newline at end of file +ALTER TABLE `PREFIX_order_detail` ADD COLUMN `id_warehouse` int(10) unsigned DEFAULT 0 AFTER `id_order_invoice`; +ALTER TABLE `PREFIX_suplier` ADD COLUMN `id_address` int(10) unsigned NOT NULL AFTER `id_supplier`; +ALTER TABLE `PREFIX_address` ADD COLUMN `id_warehouse` int(10) unsigned NOT NULL DEFAULT 0 AFTER `id_supplier`;