Calculate stocks by the number of items in their content Rails

2

a small query, while I make an inventory system for a client, a little logic problem arises when calculating stocks, although they are perfectly calculated as Inputs - Outputs, obtaining the stock of all items, without However, there are some products that must be sold in fractions, 1 item can be sold in 1/2, 1/4, etc. how can I calculate the existence taking into account that when selling an item, the item is not sold, but a fraction thereof, so if I have 10 items, and half of one is sold, as result would have to have 9.5 items, a problem that at the same time involves the price given that the price is calculated for the complete item, but not for the fraction, I leave my migrations, models, and views, thank you very much, can help me:

Product Migration

class CreateProducts < ActiveRecord::Migration[5.2]
  def change
    create_table :products do |t|
      t.string :code
      t.string :name
      t.float :weight

      t.timestamps
    end
  end
end

Migration Entries

class CreateInputs < ActiveRecord::Migration[5.2]
  def change
    create_table :inputs do |t|
      t.string :invoice
      t.integer :product_id
      t.float :quantity
      t.float :price
      t.float :utility
      t.string :provider

      t.timestamps
    end
  end
end

Migration departures

class CreateOutputs < ActiveRecord::Migration[5.2]
  def change
    create_table :outputs do |t|
      t.string :invoice
      t.integer :product_id
      t.float :quantity
      t.string :customer

      t.timestamps
    end
  end
end

Model Products

class Product < ApplicationRecord

    has_many :inputs
    has_many :outputs

    validates :code, :name, :weight, presence: true
    validates :code, uniqueness: true

    # SUMAR CANTIDAD DE ITEM POR ENTRADA
    def purchase
        inputs.pluck(:quantity).sum
    end

    # SUMAR CANTIDAD DE ITEM POR SALIDA
    def sale
        outputs.pluck(:quantity).sum
    end

    # CALCULAR CANTIDAD DE EXISTENCIAS
    def stock
        inputs.pluck(:quantity).sum - outputs.pluck(:quantity).sum
    end

    # CALCULAR PRECIO DE PRODUCTOS
    def price
        self.inputs.sum(:price) / self.inputs.count
    end

    ...

end

Product Form

<div id="form-modal" class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">

      <div class="modal-header">
        Producto      
      </div>


      <%= form_with(model: @product, remote: true) do |form| %>

        <div class="modal-body">

          <div class="row">

            <div class="col-sm-12">
              <% if @product.errors.any? %>
                <div id="error_explanation">
                  <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

                  <ul>
                  <% @product.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                  <% end %>
                  </ul>
                </div>
              <% end %>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :codigo %>
                <%= form.text_field :code, placeholder: "Codigo del producto", class: "form-control", required: true %>      
              </div>
            </div>

            <div class="col-sm-12">
              <div class="form-group">
                <% form.label :nombre %>
                <%= form.text_field :name, placeholder: "Nombre del producto", class: "form-control", required: true %>
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :peso %>
                <%= form.select(:weight, options_for_select([['1', 1], ['3/4', 0.75], ['5/8', 0.65], ['1/2', 0.5], ['3/8', 0.38], ['1/4', 0.25], ['3/16', 0.19], ['1/8', 0.13], ['1/16', 0.06], ['1/32', 0.03]]), {}, { class: "form-control" })  %>
              </div>
            </div>

            <div class="col-sm-12 text-right">
              <div class="form-group">
                <hr>
                <div class="row">
                  <div class="col-lg-6"></div>
                  <div class="col-lg-3">
                    <button type="button" class="btn btn-outline-danger" data-dismiss="modal">Cancelar</button>                  
                  </div>
                  <div class="col-lg-3">
                    <%= form.submit "Enviar", class: "btn btn-outline-primary" %>                  
                  </div>
                </div>
              </div>
            </div>

          </div>

        </div>

      <% end %>

    </div>
  </div>
</div>

Purchase Form

<div id="form-modal" class="modal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">

      <div class="modal-header">
        Producto      
      </div>

      <%= form_with(model: @input, remote: true) do |form| %>

        <div class="modal-body">

          <div class="row">

            <div class="col-sm-12">
              <% if @input.errors.any? %>
                <div id="error_explanation">
                  <h2><%= pluralize(@input.errors.count, "error") %> prohibited this input from being saved:</h2>

                  <ul>
                  <% @input.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                  <% end %>
                  </ul>
                </div>
              <% end %>
            </div>  

            <div class="col-sm-12">
              <div class="form-group">
                <% form.label :invoice %>
                <%= form.text_field :invoice, placeholder: "Codigo de la factura", class: "form-control" %>    
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :product_id %>
                <%= form.collection_select :product_id, Product.all, :id, :code, { :include_blank => "Seleccionar producto" }, required: true, class: "form-control" %>
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :quantity %>
                <%= form.text_field :quantity, placeholder: "Cantidad", class: "form-control" %>
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :precio %>
                <%= form.text_field :price, placeholder: "Precio", class: "form-control" %>                    
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :utilidad %>
                <%= form.text_field :utility, placeholder: "Porcentaje de utilidad", class: "form-control" %>      
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :customer %>
                <%= form.text_field :provider, placeholder: "Proveedor", class: "form-control" %>
              </div>
            </div>

            <div class="col-sm-12 align-self-end">
              <div class="form-group">
                <hr>
                <div class="row">
                  <div class="col-lg-9"></div>
                  <div class="col-lg-3"><%= form.submit "Enviar", class: "btn btn-outline-primary" %></div>
                </div>
              </div>
            </div>

          </div>

        </div>

      <% end %>

    </div>
  </div>
</div>

Sales Form

<%= form_with(model: output, local: true) do |form| %>

        <div class="modal-body">

          <div class="row">

            <div class="col-sm-12">
              <% if output.errors.any? %>
                <div id="error_explanation">
                  <h2><%= pluralize(output.errors.count, "error") %> prohibited this output from being saved:</h2>

                  <ul>
                  <% output.errors.full_messages.each do |message| %>
                    <li><%= message %></li>
                  <% end %>
                  </ul>
                </div>
              <% end %>
            </div>  

            <div class="col-sm-12">
              <div class="form-group">
                <% form.label :invoice %>
                <%= form.text_field :invoice, placeholder: "Codigo de la factura", class: "form-control" %>    
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :product_id %>
                <%= form.collection_select :product_id, Product.all, :id, :code, { :include_blank => "Seleccionar producto" }, required: true, class: "form-control" %>
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :quantity %>
                <%= form.text_field :quantity, placeholder: "Cantidad", class: "form-control" %>
              </div>
            </div>

            <div class="col-sm-6">
              <div class="form-group">
                <% form.label :customer %>
                <%= form.text_field :customer, placeholder: "Cliente", class: "form-control" %>
              </div>
            </div>

            <div class="col-sm-12 align-self-end">
              <div class="form-group">
                <hr>
                <div class="row">
                  <div class="col-lg-9"></div>
                  <div class="col-lg-3"><%= form.submit "Enviar", class: "btn btn-outline-primary" %></div>
                </div>
              </div>
            </div>

          </div>

        </div>

<% end %>

As we can see in the image, the cells in red, that is, the cells necessary to calculate the existence, do not take into account the amount of content that the item (s) may have, as well as the price (Cells in blue) ), calculates them as a unit price without taking into account whether the item is complete or not, what would be the correct way to calculate this? I appreciate your help immensely, greetings!

    
asked by Hector Hernandez 12.07.2018 в 05:08
source

0 answers