Module: BBMB::Html::State::Viral::Customer

Includes:
SBSM::ViralState
Defined in:
lib/bbmb/html/state/viral/customer.rb

Constant Summary collapse

EVENT_MAP =
{
  :current_order    =>  State::CurrentOrder,
  :favorites        =>  State::Favorites,
  :orders           =>  State::Orders,
  :search           =>  State::Result,
  :search_favorites =>  State::FavoritesResult,
}

Instance Method Summary collapse

Instance Method Details

#_customerObject



25
26
27
# File 'lib/bbmb/html/state/viral/customer.rb', line 25

def _customer
  @customer ||= Model::Customer.find_by_email(@session.user.name)
end

#_increment_order(order) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bbmb/html/state/viral/customer.rb', line 28

def _increment_order(order)
  quantities = user_input(:quantity)
  if(error?)
    false
  else
    quantities.each { |article_number, quantity|
      if(prod = Model::Product.find_by_article_number(article_number))
        order.increment(quantity.to_i, prod)
      end
    }
    BBMB.persistence.save(order, _customer)
    true
  end
end

#_transfer(order) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bbmb/html/state/viral/customer.rb', line 42

def _transfer(order)
  if(io = user_input(:file_chooser))
    BBMB::Util::TransferDat.parse(io) { |info|
      if(product = Model::Product.find_by_pcode(info.pcode) \
         || Model::Product.find_by_ean13(info.ean13))
        order.increment(info.quantity, product)
      else
        order.unavailable.push(info)
      end
    }
  end
  self
end

#_update_order(order) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bbmb/html/state/viral/customer.rb', line 55

def _update_order(order)
  quantities = user_input(:quantity)
  if(error?)
    false
  else
    quantities.each { |article_number, quantity|
      order.add(quantity.to_i, 
                Model::Product.find_by_article_number(article_number))
    }
    BBMB.persistence.save(order, _customer)
    true
  end
end

#clear_favoritesObject



68
69
70
71
# File 'lib/bbmb/html/state/viral/customer.rb', line 68

def clear_favorites
  _customer.favorites.clear
  self
end

#clear_orderObject



72
73
74
75
# File 'lib/bbmb/html/state/viral/customer.rb', line 72

def clear_order
  _customer.current_order.clear
  self
end

#delete_unavailableObject



76
77
78
79
80
81
82
83
84
# File 'lib/bbmb/html/state/viral/customer.rb', line 76

def delete_unavailable
  if(@model.respond_to?(:unavailable) && (ids = user_input(:quantity)))
    ids.each { |id, qty|
      @model.unavailable.delete_at(id.to_i)
    }
    BBMB.persistence.save(@model)
  end
  self
end

#favorite_productObject



85
86
87
88
89
# File 'lib/bbmb/html/state/viral/customer.rb', line 85

def favorite_product
  if(_update_order(_customer.favorites))
    trigger(:favorites)
  end
end

#favorite_transferObject



90
91
92
# File 'lib/bbmb/html/state/viral/customer.rb', line 90

def favorite_transfer
  _transfer(_customer.favorites)
end

#homeObject



93
94
95
# File 'lib/bbmb/html/state/viral/customer.rb', line 93

def home
  trigger(@session.user.home || :current_order)
end

#increment_orderObject



96
97
98
99
100
# File 'lib/bbmb/html/state/viral/customer.rb', line 96

def increment_order
  if(_increment_order(_customer.current_order))
    trigger(:current_order)
  end
end

#orderObject



101
102
103
104
105
106
# File 'lib/bbmb/html/state/viral/customer.rb', line 101

def order
  if(order_id = @session.user_input(:order_id))
    customer_id, commit_id = order_id.split('-', 2)
    State::Order.new(@session, _customer.order(commit_id))
  end
end

#order_productObject



107
108
109
110
111
# File 'lib/bbmb/html/state/viral/customer.rb', line 107

def order_product
  if(_update_order(_customer.current_order))
    trigger(:current_order)
  end
end

#order_transferObject



112
113
114
# File 'lib/bbmb/html/state/viral/customer.rb', line 112

def order_transfer
  _transfer(_customer.current_order)
end

#scanObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bbmb/html/state/viral/customer.rb', line 115

def scan
  success = false
  if(port = user_input(:comport))
    @session.set_cookie_input(:comport, port)
  end
  if(@model.is_a?(Model::Order))
    user_input(:EAN_13).each { |key, quantity|
      success = true
      if(product = Model::Product.find_by_ean13(key))
        @model.increment(quantity.to_i, product)
      else
        info = Model::Order::Info.new
        info.ean13 = key
        info.quantity = quantity
        @model.unavailable.push(info)
      end
    }
    BBMB.persistence.save(@model)
  end
  State::Json.new(@session, {:success => success})
end

#zone_navigationObject



136
137
138
# File 'lib/bbmb/html/state/viral/customer.rb', line 136

def zone_navigation
  [ :current_order, :orders, :favorites ]
end