// Address: added id_warehouse. Supplier: it is now possible to edit the address of a given supplier. Manufacturers: addresses are now correctly displayed. AdminSuppliers: addresses are now editable / when deleting a supplier (if possible <=> no current orders) address is now flaged deleted. AdminWarehouses: when deleting a warehouse, its address is now flagged as deleted.

This commit is contained in:
bMancone
2011-12-06 14:28:40 +00:00
parent d959f1d989
commit b59a66658f
8 changed files with 241 additions and 21 deletions
+32 -11
View File
@@ -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);
}
}
+8 -1
View File
@@ -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;
}
+23 -1
View File
@@ -418,4 +418,26 @@ class SupplyOrderCore extends ObjectModel
return ($res > 0);
}
}
/**
* 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);
}
}
@@ -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:'));
+151 -5
View File
@@ -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;
}
}
@@ -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;
}
}
+4 -1
View File
@@ -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,
+3 -1
View File
@@ -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`;
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`;