Class: BBMB::Model::Customer

Inherits:
Object
  • Object
show all
Includes:
ODBA::Persistable
Defined in:
lib/bbmb/model/customer.rb,
lib/bbmb/persistence/odba/model/customer.rb

Constant Summary collapse

MUTEX =
Mutex.new
ODBA_PREFETCH =
true
ODBA_SERIALIZABLE =
['@protected']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(customer_id, email = nil) ⇒ Customer

Returns a new instance of Customer.



17
18
19
20
21
22
23
24
# File 'lib/bbmb/model/customer.rb', line 17

def initialize(customer_id, email=nil)
  @archive = {}
  self.customer_id = customer_id if customer_id
  @email = email
  @favorites = Order.new(self)
  @protected = {}
  @quotas = []
end

Instance Attribute Details

#address1Object

Returns the value of attribute address1.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def address1
  @address1
end

#address2Object

Returns the value of attribute address2.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def address2
  @address2
end

#address3Object

Returns the value of attribute address3.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def address3
  @address3
end

#archiveObject (readonly)

Returns the value of attribute archive.



12
13
14
# File 'lib/bbmb/model/customer.rb', line 12

def archive
  @archive
end

#cantonObject

Returns the value of attribute canton.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def canton
  @canton
end

#cityObject

Returns the value of attribute city.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def city
  @city
end

#customer_idObject

Returns the value of attribute customer_id.



12
13
14
# File 'lib/bbmb/model/customer.rb', line 12

def customer_id
  @customer_id
end

#drtitleObject

Returns the value of attribute drtitle.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def drtitle
  @drtitle
end

#ean13Object

Returns the value of attribute ean13.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def ean13
  @ean13
end

#emailObject

Returns the value of attribute email.



12
13
14
# File 'lib/bbmb/model/customer.rb', line 12

def email
  @email
end

#faxObject

Returns the value of attribute fax.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def fax
  @fax
end

#firstnameObject

Returns the value of attribute firstname.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def firstname
  @firstname
end

#languageObject

Returns the value of attribute language.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def language
  @language
end

#lastnameObject

Returns the value of attribute lastname.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def lastname
  @lastname
end

#order_confirmationObject

Returns the value of attribute order_confirmation.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def order_confirmation
  @order_confirmation
end

#organisationObject

Returns the value of attribute organisation.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def organisation
  @organisation
end

#phone_businessObject

Returns the value of attribute phone_business.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def phone_business
  @phone_business
end

#phone_mobileObject

Returns the value of attribute phone_mobile.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def phone_mobile
  @phone_mobile
end

#phone_privateObject

Returns the value of attribute phone_private.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def phone_private
  @phone_private
end

#plzObject

Returns the value of attribute plz.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def plz
  @plz
end

#quotasObject (readonly)

Returns the value of attribute quotas.



12
13
14
# File 'lib/bbmb/model/customer.rb', line 12

def quotas
  @quotas
end

#statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def status
  @status
end

#terms_last_acceptedObject

Returns the value of attribute terms_last_accepted.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def terms_last_accepted
  @terms_last_accepted
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/bbmb/model/customer.rb', line 13

def title
  @title
end

Instance Method Details

#__old_commit_order__Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/bbmb/persistence/odba/model/customer.rb', line 27

def commit_order!(commit_time = Time.now)
  MUTEX.synchronize {
    id = @archive.keys.max.to_i.next
    order = current_order
    order.commit!(id, commit_time)
    @archive.store(id, order)
    @current_order = nil
    order
  }
end

#__old_current_order__Object



15
16
17
# File 'lib/bbmb/persistence/odba/model/customer.rb', line 15

def current_order
  @current_order ||= Order.new(self)
end

#__old_favorites__Object



41
42
43
# File 'lib/bbmb/persistence/odba/model/customer.rb', line 41

def favorites
  @favorites ||= Order.new(self)
end

#__old_inject_order__Object



34
35
36
37
38
39
40
41
# File 'lib/bbmb/persistence/odba/model/customer.rb', line 34

def inject_order(order, commit_time = Time.now)
  MUTEX.synchronize {
    id = @archive.keys.max.to_i.next
    order.commit!(id, commit_time)
    @archive.store(id, order)
    order
  }
end

#add_quota(quota) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/bbmb/model/customer.rb', line 25

def add_quota(quota)
  unless @quotas.is_a?(Array)
    puts "customer #{customer_id}: Fixing quotas #{@quotas} -> []"
    @quotas = []
    odba_store;
  end
  @quotas.push(quota).uniq!
  quota
end

#address_linesObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/bbmb/model/customer.rb', line 34

def address_lines
  [
    @organisation,
    [@drtitle, @firstname, @lastname].compact.join(' '),
    @address1,
    @address2,
    @address3,
    [@plz, @city].compact.join(' '),
  ].compact
end

#commit_order!(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/bbmb/model/customer.rb', line 44

def commit_order!(commit_time = Time.now)
  MUTEX.synchronize {
    id = @archive.keys.max.to_i.next
    order = current_order
    order.commit!(id, commit_time)
    @archive.store(id, order)
    @current_order = nil
    order
  }
end

#current_orderObject



54
55
56
# File 'lib/bbmb/model/customer.rb', line 54

def current_order
  @current_order ||= Order.new(self)
end

#favoritesObject



84
85
86
# File 'lib/bbmb/model/customer.rb', line 84

def favorites
  @favorites ||= Order.new(self)
end

#inject_order(*args) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/bbmb/model/customer.rb', line 87

def inject_order(order, commit_time = Time.now)
  MUTEX.synchronize {
    id = @archive.keys.max.to_i.next
    order.commit!(id, commit_time)
    @archive.store(id, order)
    order
  }
end

#order(commit_id) ⇒ Object



95
96
97
# File 'lib/bbmb/model/customer.rb', line 95

def order(commit_id)
  @archive[commit_id.to_i]
end

#ordersObject



98
99
100
# File 'lib/bbmb/model/customer.rb', line 98

def orders
  @archive.values
end

#protect!(key) ⇒ Object



101
102
103
# File 'lib/bbmb/model/customer.rb', line 101

def protect!(key)
  @protected.store(key, true)
end

#protects?(key) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/bbmb/model/customer.rb', line 104

def protects?(key)
  @protected.fetch(key, false)
end

#quota(article_id) ⇒ Object



64
65
66
# File 'lib/bbmb/model/customer.rb', line 64

def quota(article_id)
  @quotas.compact.find { |quota| quota.article_number == article_id }
end

#set_email_without_yus(email) ⇒ Object

This method is only for the importer!



68
69
70
71
72
73
# File 'lib/bbmb/model/customer.rb', line 68

def set_email_without_yus(email)
  raise "Invalid email address: nil" unless email
  email = email.encode('UTF-8')
  return if @email.eql?(email)
  @email = email
end

#turnoverObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bbmb/model/customer.rb', line 107

def turnover
  orders.inject(0) do
    |memo, order|
    begin
      order.total + memo
    rescue => error
      SBSM.info "turnover error for #{order.order_id} returning memo"
      memo
    end
  end
end