Created
August 23, 2018 07:56
-
-
Save evmorov/6563c0bc8bd48e797aef98fa595dc4ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# сохранить updated_at | |
old_entries = Nutrition::Warehouse::Entry.pluck(:id, :updated_at).to_h | |
# удалить транзакции списания и вернуть продукты на склад | |
Nutrition::Warehouse::Entry::Transaction.where(consumable_type: 'Nutrition::ActualMeal::Charge::Entry').destroy_all | |
Nutrition::ActualMenu.eager_load(actual_meals: :charge).joins(actual_meals: { charge: :entries }).distinct.each do |actual_menu| | |
# найти списания части меню | |
actual_meal_charge = actual_menu.actual_meals | |
.eager_load(:charge) | |
.where('nutrition_actual_meal_charges.user_id IS NOT NULL') | |
.first | |
.charge | |
# создать списание по меню, но не проставлять количество порций на тот момент | |
actual_menu_charge = actual_menu.create_charge( | |
user: actual_meal_charge.user, | |
date: actual_meal_charge.date, | |
disable_setting_charged_number_of_persons: true | |
) | |
warehouse_entries_to_amount = {} | |
actual_menu.actual_meals.each do |actual_meal| | |
# проставить количество порций | |
actual_meal.update(charged_number_of_persons: actual_meal.charge.number_of_persons) | |
# сохранить объект "продукт на складе - количество" | |
actual_meal.charge.entries.each do |entry| | |
warehouse_entry_id = entry.warehouse_entry_id | |
current_amount = warehouse_entries_to_amount[warehouse_entry_id] | |
warehouse_entries_to_amount[warehouse_entry_id] = {} unless current_amount | |
warehouse_entries_to_amount[warehouse_entry_id] = current_amount ? current_amount + entry.amount : entry.amount | |
end | |
end | |
warehouse_entries_to_amount.each do |warehouse_entry_id, amount| | |
# тип продукта по коду можно было и не сохранять - просто его находим | |
product_type_id = Nutrition::Warehouse::Entry.find(warehouse_entry_id).product.product_type.id | |
# создать пункты списания | |
actual_menu.charge.entries.create( | |
product_type_id: product_type_id, | |
warehouse_entry_id: warehouse_entry_id, | |
amount: amount | |
) | |
end | |
end | |
# проставить updated_at обратно | |
Nutrition::Warehouse::Entry.find_each { |e| e.touch(time: old_entries[e.id]) } | |
nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment