Logical problems for an inventory system in Rails

0

I have done my couple of applications in Rails, basically informative, but this time I was asked for an inventory system, I was trying to translate the logic before doing it, and I made a sketch in Excel, taking that idea, I wanted pass it to Rails, only that I find a small problem, in Excel, created 3 sheets, for Inventory, Inputs and Outputs, where the inputs or purchases to suppliers, fed to Inventory, but to prevent data from being created duplicates, manually added the product to Inventory and from purchases, by means of formulas, I only called the product code, and it brought me the data I needed, and only added the quantity, price and supplier, however, I am not sure if it is the correct way to do it, because through this logic, I take the idea that there will be 3 models, one of Inventory, Entries and Exits, the question is: who belongs to whom? thinking more in detail I deduce that inventory itself, are products, therefore, these products belong to an Entry, and outputs, always belong to Entries? I would greatly appreciate helping me to clarify this question of this trio of models, Regards!

    
asked by Hector Hernandez 08.06.2018 в 02:54
source

1 answer

0

Of course there could be many ways to do it, but maybe this will help you get started.

# esto es literalmente un producto con sus características
# no importa cuantos hay, aquí solo describes el producto
class Product < ApplicationRecord
  belongs_to :inventory 
  has_many :transactions
end

# Aquí puedes tener la cantidad de productos
# asi como las entradas y salidas
# incluso puedes tener varios inventarios (p.ej. por tienda)
class Inventory < ApplicationRecord
  has_many :products # podrás ver los productos en inventario
  has_many :transactions # las transacciones que han modificado el inventario
end

# Aquí puedes registrar las entradas y las salidas
class Transaction < ApplicationRecord
  belongs_to :product
  belongs_to :inventory

  def output
    # transacciones de salida
  end

  def input
    # transacciones de entrada
  end
end

Update based on comments:

class Producto < ApplicationRecord
  has_many :transaccions # así lo pluraliza rails
  has_many :ventas,  through: :transaccions
  has_many :compras, through: :transaccions
end

class Transaccion < ApplicationRecord
  belongs_to :producto
  belongs_to :venta,  optional: true
  belongs_to :compra, optional: true
end

class Venta < ApplicationRecord
  has_many :transaccions # así lo pluraliza rails
  has_many :productos, through: :transaccions

end

class Compra < ApplicationRecord
  has_many :transaccions # así lo pluraliza rails
  has_many :productos, through: :transaccions
end

# regresa todas las ventas de ese producto
producto.ventas

# regresa todas las compras de ese producto  
producto.compras 

# regresa todos los productos en determinda venta
venta.productos

# regresa todos los productos en determinada compra
compra.productos 

More information about has_many :through : link

    
answered by 08.06.2018 / 05:00
source