Class: LucaDeal::Customer

Inherits:
LucaRecord::Base
  • Object
show all
Defined in:
lib/luca_deal/customer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCustomer

Returns a new instance of Customer.



15
16
17
# File 'lib/luca_deal/customer.rb', line 15

def initialize
  @date = Date.today
end

Class Method Details

.create(obj) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/luca_deal/customer.rb', line 38

def self.create(obj)
  raise ':name is required' if obj[:name].nil?

  contacts = obj[:contact]&.map { |c| { 'mail' => c[:mail] } }&.compact
  contacts ||= [{
    'mail' => '_MAIL_ADDRESS_FOR_CONTACT_'
  }]
  h = {
    'name' => obj[:name],
    'address' => obj[:address] || '_CUSTOMER_ADDRESS_FOR_INVOICE_',
    'address2' => obj[:address2] || '_CUSTOMER_ADDRESS_FOR_INVOICE_',
    'contacts' => contacts
  }
  super(h)
end

Instance Method Details

#describe(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/luca_deal/customer.rb', line 23

def describe(id)
  customer = parse_current(self.class.find(id))
  contracts = Contract.all.select { |contract| contract['customer_id'] == customer['id'] }
  if !contracts.empty?
    customer['contracts'] = contracts.map do |c|
      {
        'id' => c['id'],
        'effective' => c['terms']['effective'],
        'defunct' => c['terms']['defunct']
      }
    end
  end
  readable(customer)
end

#list_nameObject



19
20
21
# File 'lib/luca_deal/customer.rb', line 19

def list_name
  self.class.all.map { |dat| parse_current(dat).sort.to_h }
end