module Magelex::MagentoCSV

Constants

MONEY_FIELDS

Public Class Methods

init_bill(row) click to toggle source

Creates a bill with basic information from csv line

# File lib/magelex/magento_csv.rb, line 31
def self.init_bill row
  bill = Magelex::LexwareBill.new

  # TODO: defining a attribute|colum- map would be nicer
  bill.order_nr      = row['Order Number']
  bill.customer_name = row['Billing Name']
  bill.country_code  = row['Shipping Country']
  bill.date          = row['Order Date']

  bill.status        = row['Order Status']

  bill.shipping_cost = row['Order Shipping']
  if (12.59..12.61).include? bill.shipping_cost
    Magelex::logger.info "Correcting shipping cost of #{bill.order_nr} (12.6 -> 15 / 1.19 €)" if bill.complete?
    bill.shipping_cost = 15 / 1.19
  elsif bill.shipping_cost == 4.15
    Magelex::logger.info "Correcting shipping cost of #{bill.order_nr} (4.15 -> 4.95 / 1.19 €)" if bill.complete?
    bill.shipping_cost = 4.95 / 1.19
  end
  bill.total         = row['Order Grand Total']
  bill
end
parse(string) click to toggle source
# File lib/magelex/magento_csv.rb, line 54
def self.parse string
  bills = []
  current_bill = Magelex::LexwareBill.new

  CSV::parse(string, :headers => :first_row,
             converters: [:all, :german_money_amount, :order_date]) do |row|
    # Multiple rows (with same order_nr) define one order
    # One order will be mapped to one bill
    if current_bill.order_nr != row['Order Number']
      current_bill = init_bill row
    end

    current_bill.add_item(row['Item Total'],
                          row['Item Tax'],
                          row['Item Name'],
                          row['Item Discount'],
                          row['Item Original Price'].to_f * row['Item Qty Ordered'].to_i)

    if !bills.include? (current_bill)
      bills << current_bill
    end
  end
  bills
end
read(filename) click to toggle source

Reads file and returns lexware_bills

# File lib/magelex/magento_csv.rb, line 26
def self.read filename
  self.parse(File.read filename)
end