// Stock: Update Supplier order management and database in consequence

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9729 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
dSevere
2011-10-28 16:04:21 +00:00
parent d9f8dfcce8
commit 4e04df6d98
5 changed files with 337 additions and 53 deletions
+147 -19
View File
@@ -48,7 +48,7 @@ class SupplierOrderCore extends ObjectModel
/**
* @var int State of the order
*/
public $id_state;
public $id_supplier_order_state;
/**
* @var int Currency used for the order
@@ -83,51 +83,50 @@ class SupplierOrderCore extends ObjectModel
/**
* @var float Total price without tax
*/
public $total_te;
public $total_te = 0;
/**
* @var float Total price after discount, without tax
*/
public $total_with_discount_te;
public $total_with_discount_te = 0;
/**
* @var float Total price with tax
*/
public $total_ti;
public $total_ti = 0;
/**
* @var float Total tax value
*/
public $total_tax = 0;
/**
* @var float Supplier discount rate (for the whole order)
*/
public $discount_rate;
public $discount_rate = 0;
/**
* @var float Supplier discount value without tax (for the whole order)
*/
public $discount_value_te;
public $discount_value_te = 0;
protected $fieldsRequired = array(
'id_supplier',
'id_employee',
'id_warehouse',
'id_state',
'id_supplier_order_state',
'id_currency',
'id_ref_currency',
'reference',
'date_add',
'date_upd',
'date_delivery_expected',
'total_te',
'total_with_discount_te',
'total_ti',
'discount_rate',
'discount_value_te'
'date_delivery_expected'
);
protected $fieldsValidate = array(
'id_supplier' => 'isUnsignedId',
'id_employee' => 'isUnsignedId',
'id_warehouse' => 'isUnsignedId',
'id_state' => 'isUnsignedId',
'id_supplier_order_state' => 'isUnsignedId',
'id_currency' => 'isUnsignedId',
'id_ref_currency' => 'isUnsignedId',
'reference' => 'isGenericName',
@@ -137,6 +136,7 @@ class SupplierOrderCore extends ObjectModel
'total_te' => 'isPrice',
'total_with_discount_te' => 'isPrice',
'total_ti' => 'isPrice',
'total_tax' => 'isPrice',
'discount_rate' => 'isFloat',
'discount_value_te' => 'isPrice'
);
@@ -158,19 +158,147 @@ class SupplierOrderCore extends ObjectModel
$fields['id_supplier'] = (int)$this->id_supplier;
$fields['id_employee'] = (int)$this->id_employee;
$fields['id_warehouse'] = (int)$this->id_warehouse;
$fields['id_state'] = (int)$this->id_state;
$fields['id_supplier_order_state'] = (int)$this->id_supplier_order_state;
$fields['id_currency'] = (int)$this->id_currency;
$fields['id_ref_currency'] = (int)$this->id_ref_currency;
$fields['reference'] = pSQL($this->reference);
$fields['date_add'] = pSQL($this->date_add);
$fields['date_upd'] = pSQL($this->date_upd);
$fields['date_delivery_expected'] = pSQL($this->delivery_date_expected);
$fields['date_delivery_expected'] = pSQL($this->date_delivery_expected);
$fields['total_te'] = (float)$this->total_te;
$fields['total_with_discount_te'] = (float)$this->total_with_discount_te;
$fields['total_ti'] = (float)$this->total_ti;
$fields['discount_rate'] = (float)$this->order_discount_rate;
$fields['discount_value'] = (float)$this->order_discount_value;
$fields['total_tax'] = (float)$this->total_tax;
$fields['discount_rate'] = (float)$this->discount_rate;
$fields['discount_value_te'] = (float)$this->discount_value_te;
return $fields;
}
/**
* @see ObjectModel::update()
*/
public function update($null_values = false)
{
$this->calculatePrices();
return parent::update($null_values);
}
/**
* @see ObjectModel::update()
*/
public function add($autodate = true, $null_values = false)
{
$this->calculatePrices();
return parent::add($autodate, $null_values);
}
/**
* Check all products in this order and calculate prices
* Apply global discount if necessary
*
* @return array
*/
protected function calculatePrices()
{
$this->total_te = 0;
$this->total_with_discount_te = 0;
$this->total_tax = 0;
$this->total_ti = 0;
$is_discount = false;
if (is_numeric($this->discount_rate) && (float)$this->discount_rate > 0)
$is_discount = true;
// get all product entries in this order
$entries = $this->getEntriesCollection();
foreach ($entries as $entry)
{
// apply global discount rate on each product if possible
if ($is_discount)
{
$entry->applyGlobalDiscount((float)$this->discount_rate);
$entry->save();
}
// add new prices to the total
$this->total_te += $entry->price_with_discount_te;
$this->total_with_discount_te += $entry->price_with_order_discount_te;
$this->total_tax += $entry->tax_value_with_order_discount;
$this->total_ti = $this->total_tax + $this->total_with_discount_te;
}
// apply global discount rate if possible
if ($is_discount)
$this->discount_value_te = $this->total_te - $this->total_with_discount_te;
}
/**
* Retrieves the product entries collection for the current order
*
* @return array
*/
protected function getEntriesCollection()
{
// build query
$query = new DbQuery();
$query->select('s.*');
$query->from('supplier_order_detail s');
$query->where('s.id_supplier_order = '.(int)$this->id);
$results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
return ObjectModel::hydrateCollection('SupplierOrderDetail', $results);
}
/**
* Check if the current state allow to edit the current order
*
* @return bool
*/
protected function isEditable()
{
// build query
$query = new DbQuery();
$query->select('s.editable');
$query->from('supplier_order_state s');
$query->where('s.id_supplier_order_state = '.(int)$this->id_supplier_order_state);
return (Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query) == 1);
}
/**
* Check if the current state allow to generate delivery_note for this order
*
* @return bool
*/
protected function isDeliveryNoteAvailable()
{
// build query
$query = new DbQuery();
$query->select('s.delivery_note');
$query->from('supplier_order_state s');
$query->where('s.id_supplier_order_state = '.(int)$this->id_supplier_order_state);
return (Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query) == 1);
}
/**
* Check if the current state allow add products in stock
*
* @return bool
*/
protected function isInReceiptState()
{
// build query
$query = new DbQuery();
$query->select('s.receipt_state');
$query->from('supplier_order_state s');
$query->where('s.id_supplier_order_state = '.(int)$this->id_supplier_order_state);
return (Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query) == 1);
}
}
+79 -11
View File
@@ -58,53 +58,53 @@ class SupplierOrderDetailCore extends ObjectModel
/**
* @var float Unit price without discount, without tax
*/
public $unit_price_te;
public $unit_price_te = 0;
/**
* @var int Quantity ordered
*/
public $quantity;
public $quantity = 0;
/**
* @var float This defines the price of the product, considering the number of units to buy.
* ($unit_price_te * $quantity), without discount, without tax
*/
public $price_te;
public $price_te = 0;
/**
* @var float Supplier discount rate for a given product
*/
public $discount_rate;
public $discount_rate = 0;
/**
* @var float Supplier discount value (($discount_rate / 100) * $price_te), without tax
*/
public $discount_value_te;
public $discount_value_te = 0;
/**
* @var float ($price_te - $discount_value_te), with discount, without tax
*/
public $price_with_discount_te;
public $price_with_discount_te = 0;
/**
* @var int Tax rate for the given product
*/
public $tax_rate;
public $tax_rate = 0;
/**
* @var float Tax value for the given product
*/
public $tax_value;
public $tax_value = 0;
/**
* @var float ($price_with_discount_te + $tax_value)
*/
public $price_ti;
public $price_ti = 0;
/**
* @var float Tax value of the given product after applying the global order discount (i.e. if SupplierOrder::discount_rate is set)
*/
public $tax_value_with_order_discount;
public $tax_value_with_order_discount = 0;
/**
* @var float This is like $price_with_discount_te, considering the global order discount.
@@ -117,7 +117,7 @@ class SupplierOrderDetailCore extends ObjectModel
'id_product',
'id_product_attribute',
'id_currency',
'excange_rate',
'exchange_rate',
'unit_price_te',
'quantity',
'price_te',
@@ -160,6 +160,9 @@ class SupplierOrderDetailCore extends ObjectModel
*/
protected $identifier = 'id_supplier_order_detail';
/**
* @see ObjectModel::getFields()
*/
public function getFields()
{
$this->validateFields();
@@ -183,4 +186,69 @@ class SupplierOrderDetailCore extends ObjectModel
return $fields;
}
/**
* @see ObjectModel::update()
*/
public function update($null_values = false)
{
$this->calculatePrices();
parent::update($null_values);
}
/**
* @see ObjectModel::add()
*/
public function add($autodate = true, $null_values = false)
{
$this->calculatePrices();
parent::add($autodate, $null_values);
}
/**
* Determine all price for this product based on its quantity and unit price
* Apply discount if necessary
* Calculate tax value in function of tax rate
*
* @return array
*/
protected function calculatePrices()
{
// calcul entry price
$htis->price_te = (float)$this->unit_price_te * (int)$this->quantity;
// calcul entry discount value
if ($this->discount_rate != null && is_numeric($this->discount_rate) && $this->discount_rate > 0)
$htis->discount_value_te = (float)$htis->price_te * ((float)$this->discount_rate / 100);
// calcul entry price with discount
$this->price_with_discount_te = $htis->price_te - $htis->discount_value_te;
// calcul tax value
$this->tax_value = $this->price_with_discount_te * ((float)$this->tax_rate / 100);
$this->price_ti = $this->price_with_discount_te - $this->tax_value;
// define default values for order discount fields
$this->tax_value_with_order_discount = $this->tax_value;
$this->price_with_order_dscount_te = $this->price_with_discount_te;
}
/**
* Apply a global order discount rate on the current product entity
*
* @param $discount_rate The discount rate in percent (Ex. 5 for 5 percents)
*/
public function applyGlobalDiscount($discount_rate)
{
if ($discount_rate != null && is_numeric($discount_rate) && (float)$discount_rate > 0)
{
// calculate new price, with global order discount, tax ecluded
$this->price_with_order_dscount_te = $this->price_with_discount_te - ($this->price_with_discount_te * ((float)$discount_rate / 100));
// calculate new tax value, with global order discount
$this->tax_value_with_order_discount = $this->price_with_order_dscount_te * ((float)$this->tax_rate / 100);
}
}
}
+43 -4
View File
@@ -50,10 +50,22 @@ class SupplierOrderStateCore extends ObjectModel
*/
public $receipt_state;
/**
* @var bool Tells if the the order is in a state corresponding to a product pending receipt
*/
public $pending_receipt;
/**
* @var string Display state in the specified color (Ex. #FFFF00)
*/
public $color;
protected $fieldsValidate = array(
'delivery_note' => 'isBool',
'editable' => 'isBool',
'receipt_state' => 'isBool'
'receipt_state' => 'isBool',
'pending_receipt' => 'isBool',
'color' => 'isColor'
);
protected $fieldsRequiredLang = array('name');
@@ -70,19 +82,46 @@ class SupplierOrderStateCore extends ObjectModel
*/
protected $identifier = 'id_supplier_order_state';
/**
* @see ObjectModel::getFields()
*/
public function getFields()
{
$this->validateFields();
$fields['delivery_note'] = (int)$this->delivery_note;
$fields['editable'] = (int)$this->editable;
$fields['receipt_state'] = (int)$this->receipt_state;
$fields['delivery_note'] = (bool)$this->delivery_note;
$fields['editable'] = (bool)$this->editable;
$fields['receipt_state'] = (bool)$this->receipt_state;
$fields['pending_receipt'] = (bool)$this->pending_receipt;
$fields['color'] = pSQL($this->color);
return $fields;
}
/**
* @see ObjectModel::getTranslationsFieldsChild()
*/
public function getTranslationsFieldsChild()
{
$this->validateFieldsLang();
return $this->getTranslationsFields(array('name'));
}
/**
* Gets the list of supplier order states
*
* @param int $id_lang The language id
* @return array
*/
public static function getSupplierOrderStates($id_lang = 0)
{
if ($id_lang == 0)
$id_lang = Context::getContext()->language->id;
$query = new DbQuery();
$query->select('sl.name, s.id_supplier_order_state');
$query->from('supplier_order_state s');
$query->leftjoin('supplier_order_state_lang sl ON (s.id_supplier_order_state = sl.id_supplier_order_state AND sl.id_lang='.(int)$id_lang.')');
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
}
}
+16 -13
View File
@@ -2013,7 +2013,7 @@ CREATE TABLE `PREFIX_warehouse_carrier` (
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_stock_available` (
`id_stock_available` INT(11) UNSIGNED NOT NULL,
`id_stock_available` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_product` INT(11) UNSIGNED NOT NULL,
`id_product_attribute` INT(11) UNSIGNED NOT NULL,
`id_shop` INT(11) UNSIGNED NOT NULL,
@@ -2027,19 +2027,20 @@ CREATE TABLE `PREFIX_stock_available` (
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_order` (
`id_supplier_order` INT(11) UNSIGNED NOT NULL,
`id_supplier_order` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_supplier` INT(11) UNSIGNED NOT NULL,
`id_employee` INT(11) UNSIGNED NOT NULL,
`id_warehouse` INT(11) UNSIGNED NOT NULL,
`id_state` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_state` INT(11) UNSIGNED NOT NULL,
`id_currency` INT(11) UNSIGNED NOT NULL,
`id_ref_currency` INT(11) UNSIGNED NOT NULL,
`reference` VARCHAR(32) DEFAULT NULL,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
`date_delivery_expected` DATETIME NOT NULL,
`date_delivery_expected` DATETIME DEFAULT NULL,
`total_te` DECIMAL(20,6) DEFAULT '0.000000',
`total_with_discount_te` DECIMAL(20,6) DEFAULT '0.000000',
`total_tax` DECIMAL(20,6) DEFAULT '0.000000',
`total_ti` DECIMAL(20,6) DEFAULT '0.000000',
`discount_rate` DECIMAL(20,6) DEFAULT '0.000000',
`discount_value_te` DECIMAL(20,6) DEFAULT '0.000000',
@@ -2050,7 +2051,7 @@ CREATE TABLE `PREFIX_supplier_order` (
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_order_detail` (
`id_supplier_order_detail` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_detail` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_supplier_order` INT(11) UNSIGNED NOT NULL,
`id_product` INT(11) UNSIGNED NOT NULL,
`id_product_attribute` INT(11) UNSIGNED NOT NULL,
@@ -2075,7 +2076,7 @@ CREATE TABLE `PREFIX_supplier_order_detail` (
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_order_history` (
`id_supplier_order_history` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_history` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_supplier_order` INT(11) UNSIGNED NOT NULL,
`id_employee` INT(11) UNSIGNED NOT NULL,
`id_state` INT(11) UNSIGNED NOT NULL,
@@ -2087,30 +2088,32 @@ CREATE TABLE `PREFIX_supplier_order_history` (
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_order_state` (
`id_state` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_state` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`delivery_note` tinyint(1) NOT NULL DEFAULT 0,
`editable` tinyint(1) NOT NULL DEFAULT 0,
`receipt_state` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id_state`)
`pending_receipt` tinyint(1) NOT NULL DEFAULT 0,
`color` VARCHAR(32) DEFAULT NULL,
PRIMARY KEY (`id_supplier_order_state`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_order_state_lang` (
`id_state` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_state` INT(11) UNSIGNED NOT NULL,
`id_lang` INT(11) UNSIGNED NOT NULL,
`name` VARCHAR(128) DEFAULT NULL,
PRIMARY KEY (`id_state`, `id_lang`)
PRIMARY KEY (`id_supplier_order_state`, `id_lang`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_order_receipt_history` (
`id_supplier_order_receipt_history` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_receipt_history` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_supplier_order_detail` INT(11) UNSIGNED NOT NULL,
`id_employee` INT(11) UNSIGNED NOT NULL,
`id_state` INT(11) UNSIGNED NOT NULL,
`id_supplier_order_state` INT(11) UNSIGNED NOT NULL,
`quantity` INT(11) UNSIGNED NOT NULL,
`date_add` DATETIME NOT NULL,
PRIMARY KEY (`id_supplier_order_receipt_history`),
KEY `id_supplier_order_detail` (`id_supplier_order_detail`),
KEY `id_state` (`id_state`)
KEY `id_supplier_order_state` (`id_supplier_order_state`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8;
CREATE TABLE `PREFIX_supplier_rates` (
+52 -6
View File
@@ -866,7 +866,8 @@ INSERT INTO `PREFIX_tab` (`id_tab`, `class_name`, `id_parent`, `position`) VALUE
(96, 'AdminWarehouses', 95, 1),
(97, 'AdminStockManagement', 95, 2),
(98, 'AdminStockInstantState', 95, 4),
(99, 'AdminStockCover', 95, 5);
(99, 'AdminStockCover', 95, 5),
(100, 'AdminSupplierOrders', 95, 6);
INSERT INTO `PREFIX_access` (`id_profile`, `id_tab`, `view`, `add`, `edit`, `delete`) (SELECT 1, id_tab, 1, 1, 1, 1 FROM `PREFIX_tab`);
@@ -890,7 +891,8 @@ INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(1, 96, 'Warehouses'),
(1, 97, 'Stock Management'),
(1, 98, 'Stock instant state'),
(1, 99, 'Stock cover');
(1, 99, 'Stock cover'),
(1, 100, 'Supplier orders');
INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(2, 1, 'Catalogue'),(2, 2, 'Clients'),(2, 3, 'Commandes'),(2, 4, 'Paiement'),(2, 5, 'Transport'),
@@ -912,7 +914,8 @@ INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(2, 96, 'Entrepôts'),
(2, 97, 'Gestion du stock'),
(2, 98, 'Etat instantané du stock'),
(2, 99, 'Couverture de stock');
(2, 99, 'Couverture de stock'),
(2, 100, 'Commandes fournisseurs');
INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(3, 1, 'Catálogo'),(3, 2, 'Clientes'),(3, 3, 'Pedidos'),(3, 4, 'Pago'),(3, 5, 'Transporte'),
@@ -933,7 +936,8 @@ INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(3, 96, 'Warehouses'),
(3, 97, 'Stock Management'),
(3, 98, 'Stock instant state'),
(3, 99, 'Stock cover');
(3, 99, 'Stock cover'),
(3, 100, 'Supplier orders');
INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(4, 1, 'Katalog'),(4, 2, 'Kunden'),(4, 3, 'Bestellungen'),(4, 4, 'Zahlung'),
@@ -955,7 +959,8 @@ INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(4, 96, 'Warehouses'),
(4, 97, 'Stock Management'),
(4, 98, 'Stock instant state'),
(4, 99, 'Stock cover');
(4, 99, 'Stock cover'),
(4, 100, 'Supplier orders');
INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(5, 1, 'Catalogo'),(5, 2, 'Clienti'),(5, 3, 'Ordini'),(5, 4, 'Pagamento'),
@@ -977,7 +982,8 @@ INSERT INTO `PREFIX_tab_lang` (`id_lang`, `id_tab`, `name`) VALUES
(5, 96, 'Warehouses'),
(5, 97, 'Stock Management'),
(5, 98, 'Stock instant state'),
(5, 99, 'Stock cover');
(5, 99, 'Stock cover'),
(5, 100, 'Supplier orders');
INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`)
(SELECT `id_tab`, id_lang, (SELECT tl.`name`
@@ -1412,3 +1418,43 @@ address2
city State:name postcode
Country:name
phone' WHERE `PREFIX_address_format`.`id_country` = 4;
INSERT INTO `ps_supplier_order_state` (`id_supplier_order_state`, `delivery_note`, `editable`, `receipt_state`, `pending_receipt`, `color`) VALUES
(1, 0, 1, 0, 0, '#ffe1af'),
(2, 1, 0, 0, 0, '#e7fbff'),
(3, 0, 0, 0, 1, '#ffdbfd'),
(4, 0, 0, 1, 1, '#ffd3d3'),
(5, 0, 0, 1, 0, '#d8ffd7'),
(6, 0, 0, 0, 0, '#cccccc');
INSERT INTO `ps_supplier_order_state_lang` (`id_supplier_order_state`, `id_lang`, `name`) VALUES
(1, 1, 'creation in progress'),
(1, 2, 'Création en cours'),
(1, 3, 'Création in progress'),
(1, 4, 'Création in progress'),
(1, 5, 'Création in progress'),
(2, 1, 'Order validated'),
(2, 2, 'Commande validée'),
(2, 3, 'Order validated'),
(2, 4, 'Order validated'),
(2, 5, 'Order validated'),
(3, 1, 'Pending receipt'),
(3, 2, 'Attente de réception'),
(3, 3, 'Pending receipt'),
(3, 4, 'Pending receipt'),
(3, 5, 'Pending receipt'),
(4, 1, 'Order received in part'),
(4, 2, 'Commande réceptionnée partiellement'),
(4, 3, 'Order received in part'),
(4, 4, 'Order received in part'),
(4, 5, 'Order received in part'),
(5, 1, 'Order received completely'),
(5, 2, 'Commande réceptionnée totalement'),
(5, 3, 'Order received completely'),
(5, 4, 'Order received completely'),
(5, 5, 'Order received completely'),
(6, 1, 'order fenced'),
(6, 2, 'Commande cloturée'),
(6, 3, 'order fenced'),
(6, 4, 'order fenced'),
(6, 5, 'order fenced');