I'm trying to create an initial number when the user saves the initial number of an invoice will continue with next value
Table example working:
|invoice_nums|
|id| |invoice_num|
1 3010
|invoices|
|id| |invoice_num| |amount|
1 3011 1000
2 3012 1000
3 3013 1000
Table example not working when i change the invoice_num value from InvoiceNums:
|invoice_nums|
|id| |invoice_num|
1 2010
|invoices|
|id| |invoice_num| |amount|
4 2014 1000 ###SHOULD BE 2011
5 2015 1000 ###SHOULD BE 2012
Here is my controller:
def new
@num_ini = InvoiceNum.first
@num_last = Invoice.last
@trick = Invoice.count(:all)
end
Here is my view:
<% @sum= "%06d" % (@num.invoice_num.next.to_i + @trick.to_i) %>
<%= @sum %>
I tried this in the view
<%= @sum= (@num_last.invoice_num.to_i || @num_ini.invoice_num.to_i )+ 1 %>
Also I tried this in the view:
<% if @num_ini.invoice_num.to_i > @num_last.invoice_num.to_i %>
<%= @sum= @num_ini.invoice_num.to_i + 1 %>
<% else %>
<%= @sum=@num_last.invoice_num.to_i + 1 %>
<% end %>
Please somebody can help me?
I'm trying to continue the invoice_num according the last invoice_num from the invoice_nums.
All help can be accepted.
class Invoice < ActiveRecord::Base
INITIAL_INVOICE_NUMBER = 2009
before_create :set_invoice_number
def set_invoice_number
(Invoice.maximum(:invoice_number) || self::INITIAL_INVOICE_NUMBER) + 1
end
end
Note:
I made the initial 2009
because of the +1
either that or change the line to a trinary condition