Advertisement
Guest User

Untitled

a guest
Aug 30th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.16 KB | None | 0 0
  1. (-> {
  2.   ApplicationRecord.logger.level = 1
  3.   range = Date.new(2018, 2, 28).all_day
  4.  
  5.   Branch.switch_each do |branch|
  6.     movements =
  7.       Inventory::Movement.
  8.       where(created_at: range).
  9.       preload(:product).
  10.       sort_by { |movement| movement.product.code.downcase }
  11.  
  12.     next if movements.empty?
  13.     path = Rails.root.join("tmp/#{branch.code}-inventory-movements-2018-02-28.csv").to_s
  14.     count = movements.size
  15.  
  16.     CSV.open path, 'wb' do |csv|
  17.       csv << ['Code', 'Description', 'Qty', 'Supplier price']
  18.  
  19.       movements.each.with_index 1 do |movement, i|
  20.         product = movement.product
  21.         supplier_prices = Product::SupplierPricesPresenter.new(product).supplier_prices
  22.         last_price = supplier_prices.find { |supplier_price| supplier_price[:time] <= range.last }.try!(:[], :price)
  23.  
  24.         csv << [
  25.           product.code,
  26.           product.description,
  27.           movement.inventory_summary_quantity,
  28.           ActiveSupport::NumberHelper.number_to_currency(last_price, unit: 'R')
  29.         ]
  30.  
  31.         print "\r#{path} (#{i}/#{count})"
  32.       end
  33.     end
  34.  
  35.     puts
  36.   end
  37.  
  38.   ApplicationRecord.logger.level = 0
  39.   true
  40. }).call
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement