lundi 18 janvier 2016

How multiply quantity and price in each item

In Rails, Can not multiply the quantity x price in each items, used in the model file. How multiply the quantity x price in Ruby, And further, if I can do in javascript oriented as the total of each item with the prices and quantities in real time. Another thing, there is the possibility of a ruby code in real-time using erubis., I am attaching details:

ticket.rb

class Ticket < ActiveRecord::Base
  validates_presence_of :fecha, :customer_id, :status_id, 
  :impuesto, :moneda, :numero_boleta

  belongs_to :customer
  belongs_to :status
  belongs_to :sale
  has_many :detail_tickets, :dependent => :destroy

  accepts_nested_attributes_for :detail_tickets, :allow_destroy => true

  attr_accessible :fecha, :customer_id, :status_id, 
  :impuesto, :moneda, :numero_boleta, :detail_tickets_attributes

  def totalprice
    valor = 0.0
    self.detail_tickets.each do |p|
        valor = p.cantidad * p.price
    end
  end
end

detail_ticket.rb

class DetailTicket < ActiveRecord::Base
  attr_accessible :item, :code, :cantidad, :description, :price
  validates_presence_of :item, :code, :cantidad, :description, :price

  belongs_to :ticket
end

form.html.erb

 <%= simple_form_for @ticket do |f| %>
      <%= f.error_notification %>
        <div class="form-inputs">

        <table>
            <tr>
                <th class="text-center" width="450px"><%= f.input :fecha, label: 'Fecha' %></th>
                <%= f.input :impuesto, input_html: { value: '18.00'}, as: :hidden %>
                <th class="text-center" width="450px"><%= f.input :moneda, collection: ["S/.", "US$"], label: 'Tipo de Moneda' %></th>
            </tr>
        </table>

        <table>
            <tr>
                <th class="text-center" width="450px"><%= f.association :customer, label_method: "#{:nombre}", value_method: :id, prompt: "Debes buscar la empresa", error: 'empresa' %></th>
                <th class="text-center" width="450px"><%= f.association :status %></th>
            </tr>
        </table>

        <table>
            <tr>
                <th class="text-center" width="900px"><%= f.input :numero_boleta, label: 'Numero de Boleta', value: :id %></th>
            </tr>
        </table>
        <div>

        <table id="items">
              <tr>
                <th class="text-center" width="60px">ITEM</th>
                <th class="text-center" width="160px">CODIGO</th>
                <th class="text-center" width="225px">DESCRIPCION</th>
                <th class="text-center" width="100px">CANTIDAD</th>
                <th class="text-center" width="110px">PRECIO</th> 
                <th class="text-center" width="150px">TOTAL <%= @empresa.moneda1 %></th>
                <th></th>
              </tr>
        </table>
        <% @totales = 0.00 %>   
    <%= simple_nested_form_for @ticket, :wrapper => false do |g| %>
        <table id="detail_tickets">
        <%= g.simple_fields_for :detail_tickets do |p| %>
        <tr class="fields">
            <th align="center" width="60px" class="text-center"><%= p.input :item, label: false %></th>
            <th align="center" width="160px"><%=h p.input :code, label: false %></th>
            <th align="center" width="225px" class="description"><%=h p.input :description, label: false, input_html: {:rows => 3} %></th>
            <th id="qty" align="center" width="100px" class="text-center"><%=h p.input :cantidad, label: false %></th>
            <th id="price" align="center" width="110px" class="text-right"><%=h p.input :price, label: false %></th>
            <th align="right" width="150px" class="text-right"><%= number_with_precision(@ticket.totalprice, :delimiter => ',', :separator => '.') %></th>
            <th align="center" width="63px" class="text-center"><%= p.link_to_remove "", class: "btn btn-danger fa fa-trash" %></th>

Aucun commentaire:

Enregistrer un commentaire