finished price updating

This commit is contained in:
Senad Uka
2015-09-27 19:59:39 +02:00
parent 014416ca51
commit ae5b52165d
2 changed files with 43 additions and 2 deletions

View File

@@ -1,4 +1,11 @@
class Item < ActiveRecord::Base
CSV_COL = {
:code => 0,
:input_price => 1,
:list_price => 2
}
belongs_to :unit
has_many :multi_media_descriptions
belongs_to :sub_category
@@ -97,4 +104,25 @@ class Item < ActiveRecord::Base
return full_report
end
def self.update_prices(path)
nonexistent_codes = []
Item.transaction do
CSV.foreach(path) do |row|
code = row[CSV_COL[:code]].strip
item = Item.find_by_code(code)
if item.nil?
nonexistent_codes << code
else
item.current_input_price = row[CSV_COL[:input_price]]
item.list_price = row[CSV_COL[:list_price]]
item.save!
end
end
end
puts "Nonexistent codes: "
puts nonexistent_codes.join("\n")
end
end