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.auth_session.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
55
# File 'lib/bbmb/html/state/viral/customer.rb', line 42

def _transfer(order)
  if(io = user_input(:file_chooser) && (file = user_input(:file_chooser)[:tempfile]))
    file = user_input(:file_chooser)[:tempfile]
    BBMB::Util::TransferDat.parse( open(file.path)) { |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



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

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



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

def clear_favorites
  _customer.favorites.clear
  self
end

#clear_orderObject



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

def clear_order
  _customer.current_order.clear
  self
end

#delete_unavailableObject



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

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



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

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

#favorite_transferObject



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

def favorite_transfer
  _transfer(_customer.favorites)
end

#homeObject



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

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

#increment_orderObject



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

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

#orderObject



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

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



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

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

#order_transferObject



114
115
116
# File 'lib/bbmb/html/state/viral/customer.rb', line 114

def order_transfer
  _transfer(_customer.current_order)
end

#scanObject



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

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



138
139
140
# File 'lib/bbmb/html/state/viral/customer.rb', line 138

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