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

View File

@@ -195,8 +195,8 @@ def import_single_item(row, index, logger)
item = (Item.find_by code: code) || Item.new
item.name = row[lookup[:name_on_ribica]]
item.code = code
item.current_input_price = row[lookup[:current_input_price]] || 11.00
item.list_price = row[lookup[:list_price]] || 12.00
item.current_input_price = 0 #row[lookup[:current_input_price]] || 11.00
item.list_price = 0 #row[lookup[:list_price]] || 12.00
item.units_in_pack = row[lookup[:units_in_pack]]
item.description = row[lookup[:description]] || "default description"
item.stock = row[lookup[:stock]]
@@ -283,6 +283,7 @@ def do_import(validate_only)
end
namespace :ribica do
desc "Creates menu structure using the sections and subsections from items."
task copy_sections_to_menu: :environment do
Section.transaction do
Section.all.each do |section|
@@ -313,6 +314,7 @@ namespace :ribica do
end
namespace :ribica do
desc "Clears database."
task clear_database: :environment do
conn = ActiveRecord::Base.connection
@@ -323,18 +325,29 @@ namespace :ribica do
end
namespace :ribica do
desc "Checks for errors in csv file. "
task validate_items: :environment do
do_import true
end
end
namespace :ribica do
desc "Imports items from csv if everything is ok. "
task import_items: :environment do
do_import false
end
end
namespace :ribica do
desc "Updates prices from csv. "
task update_prices: :environment do
Item.update_prices(ENV['INPUT'])
end
end
namespace :ribica do
desc "Imports items from csv if everything is ok. "
task reindex: :environment do
es_client = Elasticsearch::Client.new log: true