How does the Stock Information of a product get updated automatically?

How does the Stock Information of a product get updated automatically?

The sales reps need to be cogniance of the available stock in the inventory before confirming a booking with the customer. When a booking is done manually they can check and update the customer and the inventory management team if the stock is running low. 
But, in cases where the order processing is done through automated systems it is important to have a way in which there is no dependency on manual updations and the system automatically stocks the inventory based on different factors. 

To facilitate this, we have an automatic system to update the stock information based on these three factors:
  1. quantity in stock
  2. quantity in demand
  3. quantity ordered
Quantity in Stock
The Quantity in stock adjusts according to the details in Invoice and Purchase Order, like this:
  1. Quantity in stock increases when a purchase order is delivered.
  2. Quantity in stock decreases when an already delivered purchase order is cancelled.
  3. Quantity in stock decreases with an invoice creation (this includes when a quote or sales order is converted to an invoice).
  4. Quantity in stock increases with an invoice cancellation.
Quantity in Demand
The quantity in demand adjusts according to the fate of the sales orders, like this: 
  1. Quantity in demand increases when a sales order is created.
  2. Quantity in demand decreases when a sales order is delivered or cancelled.
  3. Quantity in demand also decreases when a sales order is converted to an invoice. The status of the sales order thus converted automatically changed to "delivered".
The ordered quantity adjusts according to the fate of the purchase orders, like this: 
  1. Quantity ordered increases when a purchase order is created.
  2. Quantity ordered decreases when a purchases order is delivered or cancelled.
Read this example to understand the above logic clearly.