SELECT
  `pos`.`Stock_Movements`.`productId` AS `productId`,
  `pos`.`Stock_Movements`.`inventoryId` AS `inventoryId`,
  sum(
    (
      CASE
        WHEN (`pos`.`Stock_Movements`.`type` = 'IN') THEN `pos`.`Stock_Movements`.`quantity`
        WHEN (`pos`.`Stock_Movements`.`type` = 'OUT') THEN -(`pos`.`Stock_Movements`.`quantity`)
        WHEN (`pos`.`Stock_Movements`.`type` = 'ADJUST') THEN `pos`.`Stock_Movements`.`quantity`
        ELSE 0
      END
    )
  ) AS `stock`
FROM
  `pos`.`Stock_Movements`
GROUP BY
  `pos`.`Stock_Movements`.`productId`,
  `pos`.`Stock_Movements`.`inventoryId`