We all remember problems we had in school, stated likes:
> Alice recieve 10 candies from her grandmother and later gives 2 candies to Bob.
> How many candies has Alice left ?
Using ERP5 Universal Business Model this simple problem will be modeled using:
- resource: The resource are candies
- node: Alice, Bob and the Grandmother are nodes
- movements: When Alice gives 2 candies to Bob, there is a movement where Alice is _source_ and Bob is _destination_. The resource of this movement is candy.
A simple solution to "how many candies Alice has left" is achived by summing the quantities of incoming movements, ie. movements for which Alice is the destination, and then substracting the quantities of outgoing movements, ie. movements for which Alice is the source.
Which in pseudo SQL format, means:
```#SQL
SELECT SUM(quantity) FROM movements WHERE Source = 'Alice'
- SUM(quantity) FROM movements Where Destination = 'Alice'
```
Which leads to complex and unefficient queries.
The underlying idea of Inventory is to see all movements from the source's point of view and from the destination's point of view. We can say that an incoming movement increase the inventory of the destination *node* while decreasing the inventory of the node at the other side of the movement, that we call *mirror node* in Inventory API terminology.
We store the movements using the following data structure:
Using this data structure querying inventory of Alice is much efficient:
```SQL
SELECT SUM(quantity) FROM stock WHERE Node = 'Alice'
```
Using ERP5 API, this call would be :
```#python
return portal_simulation.getInventory(
node_uid=alice.getUid()
)
```
---
## Dates
We are usually interested in knowing the stock level at a certain point of time. This is rather easy, because we usually record the point in time when the movement occured.
It may happen that the movement itself takes some time and that there is a difference between the time when resource beeing leaves the source and the time when it arrives at destination.
To support this, movements have two properties:
-*StartDate*:When the movement begins and the quantity is removed from source
-*StopDate*:When the movements finishes and the quantity is added at destination
Let's imagine that on Wednesday Alice send 3 candies to her cousin Carol that lives far away. Carol will only recieve the candies after two days, because it is the time the post office will take to deliver. This movement is depicted as M3 in the table below: