Advertisement
Guest User

Untitled

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