 |
| [Fonte: http://www.revistamedievo.com] |
Hoje vou tentar criar um modelo simples para composição de preços para produtos manufaturados com múltiplas matérias primas e mútiplas etapas de trabalho. E que suporte historiamento de precificação, ou seja, parâmetros variáveis com o tempo afetam a determinação de um preço e a cada determinação de preço deve ser mantido um histórico.
Ademais, cada precificação é composta, além da matéria e do trabalho, de outros fatores mercadológicos, que incidem sobre o preço final ou sobre cada elemento da composição sob forma de um índice de correção. Cada aplicação de índice deve também também ser historiada e rastreável, ou seja, a aplicação de um desconto sobre uma determinada matéria por razão X deve ser armazenada para futura auditoria.
Estou estudando os modelos de relacionamento em árvore e se eles serão capazes de implementar este modelo, e sobretudo, se serão escaláveis para futuras adições de complexidade.
A questão é se seria possível modelar em uma entidade só "coisas" que são diversas (mão de obra VS matéria prima).
From now on, it's about to get technical, so I decided to write in plain english.
1st attempt: simple hierarchical tree model:
Every tree has a root, roots might have zero or more nodes and nodes might have zero or more nodes. Each node has only one parent node.
In this model, the price composition is as follows:
Given a node, its price is the sum of the price of each individual children multiplied by a factor inherent and unique to this node.
The exception being for those nodes that *do not* have children, in which case, their price is explicitly assigned and multiplied by a factor inherent and unique to itself.
The simplest representation for this model would require a single entitie: Node.
Node
--------------------
id
parent_id
price
factor
This model may, howerver, be further normalized, considering several nodes might share the same factor and would benefit from depending on a single entry of a different entity. Thus:
Node
--------------------
id
parent_id
price
factor_id
Factor
--------------------
id
factor
Howerver, it gets misty as to how the price property behaves given that there is no easy awareness of the tree's depth, and the aggregate calculations might get nasty.
2nd Attempt: Nested Set Model
Nested set models use two properties, lft and rgt, to localize each node in its set hierarchy. This localizations makes for an easier querying when dealing with unlimited depth sets (and thus, trees).
 |
| Graphical representation of Nested Set Model, with lft and rgt properties depicted. [source: dev.mysql.com] |
MySQL has a good descritption of
Nested Set Models.
With this Model, I am able to easily query prices as aggregate functions using one single entity:
CREATE TABLE `nested_set` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text,
`value` int(11) DEFAULT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
Querying a price is as simple as creating the following view:
CREATE VIEW `pricing` AS select `node`.`id` AS `id`,`node`.`name` AS `name`,sum(`children`.`value`) AS `price` from (`nested_set` `node` join `nested_set` `children`) where (`children`.`lft` between `node`.`lft` and `node`.`rgt`) group by `node`.`name` order by `node`.`id`