Class: Invoice

Inherits:
Object
  • Object
show all
Defined in:
lib/invoices/models/invoice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInvoice

Returns a new instance of Invoice.



4
5
6
7
8
9
10
11
# File 'lib/invoices/models/invoice.rb', line 4

def initialize
  calculate_number
  @line_items_array = []
  @git_log = []
  @total_hrs = 0
  @total_cost = 0
  @date = Time.now.strftime("%m/%d/%y")
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



3
4
5
# File 'lib/invoices/models/invoice.rb', line 3

def client_id
  @client_id
end

#dateObject

Returns the value of attribute date.



3
4
5
# File 'lib/invoices/models/invoice.rb', line 3

def date
  @date
end

#format_numberObject (readonly)

Returns the value of attribute format_number.



2
3
4
# File 'lib/invoices/models/invoice.rb', line 2

def format_number
  @format_number
end

#git_logObject (readonly)

Returns the value of attribute git_log.



2
3
4
# File 'lib/invoices/models/invoice.rb', line 2

def git_log
  @git_log
end

#hoursObject (readonly)

Returns the value of attribute hours.



2
3
4
# File 'lib/invoices/models/invoice.rb', line 2

def hours
  @hours
end

#line_items_arrayObject (readonly)

Returns the value of attribute line_items_array.



2
3
4
# File 'lib/invoices/models/invoice.rb', line 2

def line_items_array
  @line_items_array
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/invoices/models/invoice.rb', line 3

def number
  @number
end

#rateObject (readonly)

Returns the value of attribute rate.



2
3
4
# File 'lib/invoices/models/invoice.rb', line 2

def rate
  @rate
end

#rootObject (readonly)

Returns the value of attribute root.



2
3
4
# File 'lib/invoices/models/invoice.rb', line 2

def root
  @root
end

#total_costObject

Returns the value of attribute total_cost.



3
4
5
# File 'lib/invoices/models/invoice.rb', line 3

def total_cost
  @total_cost
end

#total_hrsObject

Returns the value of attribute total_hrs.



3
4
5
# File 'lib/invoices/models/invoice.rb', line 3

def total_hrs
  @total_hrs
end

Instance Method Details

#add_line_item(line_item) ⇒ Object



62
63
64
# File 'lib/invoices/models/invoice.rb', line 62

def add_line_item(line_item)
  @line_items_array << line_item
end

#calculate_numberObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/invoices/models/invoice.rb', line 12

def calculate_number
  def format(x)
    if x >=  1 && x < 10 then "000#{x}"
    elsif x >= 10 && x < 100 then "00#{x}"
    elsif x >= 100 && x < 1000 then "0#{x}"
    elsif x >= 100 && x < 10000 then "#{x}"
    # raise an exception for x >= 10000
    end
  end
  i = 1
  Dir.foreach(File.expand_path('~/invoices')) do |filename|
    if filename.include?("invoice#{format(i)}.txt")
      i += 1
    else
      format(i)
    end
  end
  @number = i
  @format_number = format(i)
end

#calculate_total_costObject



48
49
50
51
52
53
# File 'lib/invoices/models/invoice.rb', line 48

def calculate_total_cost
  @line_items_array.each do |line_item|
    @total_cost += line_item.cost
  end
  @total_cost
end

#calculate_total_hrsObject



42
43
44
45
46
47
# File 'lib/invoices/models/invoice.rb', line 42

def calculate_total_hrs
  @line_items_array.each do |line_item|
    @total_hrs += line_item.hrs
  end
  @total_hrs
end

#find_by_invoice_number(invoice_number, *boolean) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/invoices/models/invoice.rb', line 65

def find_by_invoice_number(invoice_number, *boolean)
  invoice = choose_db(*boolean).execute("select * from invoices where 
                     invoice_number = #{invoice_number}").first
  @number = invoice[0]
  @date = invoice[1]
  @client_id = invoice[2]
  @total_hrs = invoice[3]
  @total_cost = invoice[4]
  return self
end

#format(x) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/invoices/models/invoice.rb', line 13

def format(x)
  if x >=  1 && x < 10 then "000#{x}"
  elsif x >= 10 && x < 100 then "00#{x}"
  elsif x >= 100 && x < 1000 then "0#{x}"
  elsif x >= 100 && x < 10000 then "#{x}"
  # raise an exception for x >= 10000
  end
end

#git_rootObject



35
36
37
38
39
40
41
# File 'lib/invoices/models/invoice.rb', line 35

def git_root
  File.open("#{@root}/.git/logs/HEAD", "r") do |file|
    file.each_line do |line|
      @git_log << line if line.include?("commit")
    end
  end
end

#project_root(file) ⇒ Object



32
33
34
# File 'lib/invoices/models/invoice.rb', line 32

def project_root(file)
  @root = File.expand_path(file)
end

#save(*boolean) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/invoices/models/invoice.rb', line 54

def save(*boolean)
  calculate_total_hrs
  calculate_total_cost
  choose_db(*boolean).execute("INSERT INTO invoices 
             (invoice_number, date, client_id, total_hrs, total_cost) 
             VALUES (?, ?, ?, ?, ?)", 
             [@number, @date, @client_id, @total_hrs, @total_cost])
end