Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created November 25, 2013 18:14
Show Gist options
  • Save vanmichael/7645852 to your computer and use it in GitHub Desktop.
Save vanmichael/7645852 to your computer and use it in GitHub Desktop.
#By Van and Collin
require './register.rb'
require 'pry'
cashier = Register.new
cashier.load_products("products.csv")
while cashier.get_selection != 'done'
if cashier.menu_selection == '1'
while cashier.get_item != 'done' do
cashier.get_item_qty
cashier.calculate_subtotal
cashier.add_to_invoice
end
cashier.get_amount_tendered
cashier.calculate_change
cashier.get_date_time
cashier.add_to_csv
end
if cashier.menu_selection == '2'
cashier.load_invoices('invoices.csv')
while cashier.get_report_date != 'done'
if cashier.transactions[cashier.report_date].nil?
else
cashier.get_daily_sales(cashier.report_date)
cashier.get_daily_purchase(cashier.report_date)
cashier.get_net_profit
cashier.display_profits
cashier.display_item_list(cashier.report_date)
end
end
end
end
require 'csv'
require 'pry'
require 'date'
class Register
attr_reader :products, :item, :invoice, :transactions, :gross_sales, :purchase_amount, :menu_selection, :report_date
def initialize
@products = {}
@invoice = {}
@transactions = {}
@subtotal = 0
end
def load_products(file)
CSV.foreach(file, headers: true) do |line|
@products[line["SKU"]] = {name: line["Name"], Retail_Price: line["Retail_Price"], Purchasing_Price: line["Purchasing_Price"]}
end
end
def get_selection
puts "Please make a selection"
puts "Enter 1 to start a new transaction!"
puts "Enter 2 to retrieve a daily report!"
puts "Enter done when finished"
puts 'Make Your Selection!'
@menu_selection = gets.chomp
end
def get_item_qty
puts 'Enter how many!'
@qty = gets.chomp
end
def get_item
puts 'Enter an Item! Type done when finished'
@item = gets.chomp
end
def calculate_subtotal
@subtotal += (@products[@item][:Retail_Price].to_f * @qty.to_f)
puts "Subtotal : #{@subtotal}"
end
def add_to_invoice
@invoice[@item] = {} if @invoice[@item].nil?
@invoice[@item][:type] = @products[@item][:name]
@invoice[@item][:qty] = 0 if @invoice[@item][:qty].nil?
@invoice[@item][:qty] += @qty.to_i
end
def display_invoice
puts @invoice
end
def get_date_time
@date = Time.new
@date = "#{@date.year}#{@date.month}#{@date.day}"
@time = Time.new
@time = "#{@time.hour}#{@time.min}#{@time.sec}"
end
def add_to_csv
CSV.open("invoices.csv", "a+") do |csv|
csv << ["Date", "Time", "SKU", "Name", "Price", "Qty"] if csv.count < 1
@invoice.each do |key,value|
csv << [@date, @time, key, @invoice[key][:type], @products[key][:Retail_Price], @invoice[key][:qty]]
end
end
@invoice = {}
end
def load_invoices(file)
CSV.foreach(file, headers: true) do |line|
date = line["Date"]
if @transactions[date].nil?
@transactions[date] = {}
end
time = line["Time"]
if @transactions[date][time].nil?
@transactions[date][time] = []
end
@transactions[date][time] << [line["SKU"], line["Name"], line["Price"], line["Qty"]]
end
end
def get_daily_sales(date)
@gross_sales = 0
@transactions[date].each do |key,value|
value.each do |index|
@gross_sales += (index[2].to_f*index[3].to_f)
end
end
end
def get_daily_purchase(date)
@purchase_amount = 0
@transactions[date].each do |key,value|
value.each do |index|
@purchase_amount += index[3].to_f * @products[index[0]][:Purchasing_Price].to_f
end
end
end
def get_net_profit
@net_profit = @gross_sales - @purchase_amount
end
def display_profits
puts "The total sales was : #{format_decimals(@gross_sales)} and the Net Profit was : #{format_decimals(@net_profit)}"
end
def display_item_list(date)
@transactions[date].each_with_index do |(key,value),index|
puts "Order #{index + 1} completed on #{date} at #{key}"
value.each do |type|
puts "#{type[3]} #{type[1]} Coffee for #{format_decimals(type[2].to_f * type[3].to_f)}"
end
end
end
def get_amount_tendered
puts 'Enter tendered amount'
@tendered_amount = gets.chomp
end
def calculate_change
puts "Change due: $#{format_decimals(@tendered_amount.to_f - @subtotal.to_f)}"
end
def format_decimals(input)
sprintf("%.02f", input)
end
def get_report_date
puts 'Please enter a date to search, or enter done when finished'
@report_date = gets.chomp
puts 'Please enter a valid date' if @transactions[@report_date].nil?
return @report_date
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment