class SeedGenerator < ApplicationRecord

  def self.generate_id(model, prefix)
    seed = SeedGenerator.find_by_model(model)
    new_receipt_no = 0

    if (seed.nil?)
      seed = SeedGenerator.new()
      seed.model = model
      new_receipt_no = seed.next
      seed.save

    else
      current_no = seed.next
      seed.next = seed.next + seed.increase_by
      seed.current = current_no
      seed.save
    end

    padding_len = 15 - prefix.length
    saleOrderId = prefix +"-"+ seed.current.to_s.to_s.rjust((14-prefix.length)+1,'0')
    return saleOrderId

  end

  def self.new_receipt_no
    seed = SeedGenerator.find_by_model("sale")
    new_receipt_no = 0
    if (seed.nil?)
      seed = SeedGenerator.new()
      seed.model = "sale"
      new_receipt_no = seed.next
      seed.save

    else
      current_no = seed.next
      seed.next = seed.next + seed.increase_by
      seed.current = current_no
      seed.save
    end

    return seed.current
  end
end
