Sha256: 74afd42b023d638816598fc23ee9db722d29a720fdac4f5f7eb1b13cf9184b9c

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'csv'

module Rawbotz
  class OrganicProductDeliveriesCSV
    include RawgentoModels

    def self.generate
      CSV.generate do |csv|
        csv << headings
        Supplier.find_each do |supplier|
          supplier_lines supplier, csv
        end
      end
    end

    private

    def self.headings
      ["product", "qty stocked", "stock date", "order date", "order nr", "supplier"]
    end

    def self.supplier_lines supplier, csv
      supplier.orders.find_each do |order|
        if order.order_items.where("num_stocked > 0").present?
          order_lines order, csv
        end
      end
    end

    def self.order_lines order, csv
      csv << ["Order #{order.remote_order_id} from #{order.supplier.name}"]
      order.order_items.where("num_stocked > 0").each do |item|
        csv << order_item_line(item)
      end
    end

    def self.order_item_line item
      [item.local_product.name,
       item.num_stocked,
       item.order.stocked_at,
       item.order.ordered_at,
       item.order.remote_order_id,
       item.order.supplier.name]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rawbotz-0.2.0 lib/rawbotz/processors/organic_product_deliveries_csv.rb