From ae5b52165db39bb81968194488d39cfd43048646 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 27 Sep 2015 19:59:39 +0200 Subject: [PATCH] finished price updating --- back-office/app/models/item.rb | 28 ++++++++++++++++++++++++++++ back-office/lib/tasks/ribica.rake | 17 +++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/back-office/app/models/item.rb b/back-office/app/models/item.rb index bf2b2e1..018bd08 100644 --- a/back-office/app/models/item.rb +++ b/back-office/app/models/item.rb @@ -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 diff --git a/back-office/lib/tasks/ribica.rake b/back-office/lib/tasks/ribica.rake index 51983ba..6a750fb 100644 --- a/back-office/lib/tasks/ribica.rake +++ b/back-office/lib/tasks/ribica.rake @@ -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