Class: BBMB::Model::Order

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

Defined Under Namespace

Classes: Info, Position

Constant Summary collapse

ODBA_SERIALIZABLE =
['@unavailable']

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Numbers

append_features

Constructor Details

#initialize(customer) ⇒ Order

Returns a new instance of Order.



58
59
60
61
62
# File 'lib/bbmb/model/order.rb', line 58

def initialize(customer)
  @customer = customer
  @positions = []
  @unavailable = []
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



55
56
57
# File 'lib/bbmb/model/order.rb', line 55

def comment
  @comment
end

#commit_idObject (readonly)

Returns the value of attribute commit_id.



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

def commit_id
  @commit_id
end

#commit_timeObject (readonly)

Returns the value of attribute commit_time.



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

def commit_time
  @commit_time
end

#customerObject (readonly)

Returns the value of attribute customer.



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

def customer
  @customer
end

#positionsObject (readonly)

Returns the value of attribute positions.



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

def positions
  @positions
end

#referenceObject

Returns the value of attribute reference.



55
56
57
# File 'lib/bbmb/model/order.rb', line 55

def reference
  @reference
end

#unavailableObject (readonly)

Returns the value of attribute unavailable.



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

def unavailable
  @unavailable
end

Instance Method Details

#__old_add__Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bbmb/persistence/odba/model/order.rb', line 20

def add(quantity, product)
  pos = nil
  if(pos = position(product))
    if(quantity.zero?)
      @positions.delete(pos)
    else
      pos.quantity = quantity
    end
  elsif(quantity.nonzero?)
    pos = Position.new(quantity, product)
    @positions.push(pos)
  end
  if(pos && quantity.nonzero?)
    pos.price_effective = price_effective(pos)
  end
  pos
end

#__old_clear__Object



32
33
34
35
# File 'lib/bbmb/persistence/odba/model/order.rb', line 32

def clear
  @positions.clear
  @unavailable.clear
end

#__old_commit__Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/bbmb/persistence/odba/model/order.rb', line 42

def commit!(commit_id, commit_time)
  raise "can't commit empty order" if(empty?)
  calculate_effective_prices
  @positions.each { |pos|
    pos.commit!
  }
  @unavailable.clear
  @commit_time = commit_time
  @commit_id = commit_id
end

#add(quantity, product) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bbmb/model/order.rb', line 63

def add(quantity, product)
  pos = nil
  if(pos = position(product))
    if(quantity.zero?)
      @positions.delete(pos)
    else
      pos.quantity = quantity
    end
  elsif(quantity.nonzero?)
    pos = Position.new(quantity, product)
    @positions.push(pos)
  end
  if(pos && quantity.nonzero?)
    pos.price_effective = price_effective(pos)
  end
  pos
end

#additional_infoObject



80
81
82
83
84
85
86
87
88
# File 'lib/bbmb/model/order.rb', line 80

def additional_info
  info = {}
  [ :comment, :priority, :reference ].each { |key|
    if(value = self.send(key))
      info.store(key, value)
    end
  }
  info
end

#calculate_effective_pricesObject



89
90
91
92
93
# File 'lib/bbmb/model/order.rb', line 89

def calculate_effective_prices
  @positions.each { |pos|
    pos.price_effective = price_effective(pos)
  }
end

#clearObject



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

def clear
  @positions.clear
  @unavailable.clear
end

#commit!(*args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/bbmb/model/order.rb', line 98

def commit!(commit_id, commit_time)
  raise "can't commit empty order" if(empty?)
  calculate_effective_prices
  @positions.each { |pos|
    pos.commit!
  }
  @unavailable.clear
  @commit_time = commit_time
  @commit_id = commit_id
end

#each(&block) ⇒ Object



108
109
110
# File 'lib/bbmb/model/order.rb', line 108

def each(&block)
  @positions.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/bbmb/model/order.rb', line 111

def empty?
  @positions.empty?
end

#filenameObject



114
115
116
# File 'lib/bbmb/model/order.rb', line 114

def filename
  sprintf("%s-%s.txt", order_id, @commit_time.strftime('%Y%m%d%H%M%S'))
end

#i2_bodyObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bbmb/model/order.rb', line 126

def i2_body
  lines = []
  offset = 1
  @positions.each_with_index { |position, idx|
    lines.push *i2_position(idx + offset, position, position.quantity)
    if(freebies = position.freebies)
      offset += 1
      lines.push *i2_position(idx + offset, position, freebies)
      lines.push "603:21"
    end
  }
  lines.join("\n")
end

#i2_headerObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/bbmb/model/order.rb', line 139

def i2_header
  lines = [
    "001:7601001000681",
    "002:ORDERX",
    "003:220",
    "010:%s" % filename,
    "100:%s" % BBMB.config.i2_100,
    "101:%s" % @reference,
    "201:CU",
    "202:%s" % @customer.customer_id,
    "201:BY",
    "202:1075",
    "231:%s" % @customer.organisation,
  ]
  if(@comment && !@comment.empty?)
    lines.push "236:%s" % _formatted_comment
  end
  lines.push "237:61"
  if(@priority)
    lines.push "238:%i" % @priority
  end
  lines.push "250:ADE",
              sprintf("251:%i%05i", @customer.customer_id, @commit_id),
              "300:4", "301:%s" % @commit_time.strftime('%Y%m%d')
  lines.join("\n")
end

#i2_position(line, position, quantity) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/bbmb/model/order.rb', line 165

def i2_position(line, position, quantity)
  ["500:%i" % line,
    "501:%s" % position.ean13,
    "502:%s" % position.article_number,
    "520:%s" % quantity,
    "521:PCE", "540:2", "541:%s" % @commit_time.strftime('%Y%m%d')]
end

#increment(quantity, product) ⇒ Object



117
118
119
120
121
122
# File 'lib/bbmb/model/order.rb', line 117

def increment(quantity, product)
  if(pos = position(product))
    quantity += pos.quantity
  end
  add(quantity, product)
end

#item_countObject



123
124
125
# File 'lib/bbmb/model/order.rb', line 123

def item_count
  @positions.inject(0) { |memo, pos| memo + pos.quantity }
end

#order_idObject



172
173
174
# File 'lib/bbmb/model/order.rb', line 172

def order_id
  sprintf "%s-%s", @customer.customer_id, @commit_id
end

#position(product) ⇒ Object



175
176
177
# File 'lib/bbmb/model/order.rb', line 175

def position(product)
  @positions.find { |pos| pos.product == product }
end

#price_effective(pos) ⇒ Object



178
179
180
181
# File 'lib/bbmb/model/order.rb', line 178

def price_effective(pos)
  ((quota = quota(pos.article_number)) && quota.price) \
    || pos.price_effective
end

#quantity(product) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/bbmb/model/order.rb', line 182

def quantity(product)
  if(pos = position(product))
    pos.quantity
  else
    0
  end
end

#quota(article_number) ⇒ Object



189
190
191
# File 'lib/bbmb/model/order.rb', line 189

def quota(article_number)
  @customer.quota(article_number)
end

#reverse!Object



192
193
194
# File 'lib/bbmb/model/order.rb', line 192

def reverse!
  @positions.reverse!
end

#sizeObject



195
196
197
# File 'lib/bbmb/model/order.rb', line 195

def size
  @positions.size
end

#sort!(*args, &block) ⇒ Object



198
199
200
# File 'lib/bbmb/model/order.rb', line 198

def sort!(*args, &block)
  @positions.sort!(*args, &block)
end

#sort_by(*args, &block) ⇒ Object



201
202
203
204
205
# File 'lib/bbmb/model/order.rb', line 201

def sort_by(*args, &block)
  twin = dup
  twin.positions.replace @positions.sort_by(*args, &block)
  twin
end

#to_csvObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/bbmb/model/order.rb', line 223

def to_csv
  result = ''
  BBMB.config.target_format_fs ||= ','
  BBMB.config.target_format_rs ||= "\n"
  CSV.generate(result, { :col_sep => BBMB.config.target_format_fs,
                         :row_sep => BBMB.config.target_format_rs}) { |writer|
    @positions.each { |position|
      writer << [
        @customer.customer_id,
        @customer.ean13,
        @commit_time.strftime('%d%m%Y'),
        @commit_id,
        position.pcode,
        position.ean13,
        position.article_number,
        position.quantity,
        position.price,
        @reference,
        _formatted_comment(' '),
      ]
    }
  }
  result
end

#to_i2Object



247
248
249
# File 'lib/bbmb/model/order.rb', line 247

def to_i2
  i2_header << "\n" << i2_body << "\n"
end

#to_target_formatObject



250
251
252
# File 'lib/bbmb/model/order.rb', line 250

def to_target_format
  self.send("to_#{BBMB.config.target_format}")
end

#totalObject



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/bbmb/model/order.rb', line 206

def total
  @positions.delete_if do |pos|
    begin
      pos.total
      false
    rescue => error
      SBSM.info "Deleting an invalid position from order #{order_id}"
      true
    end
  end
  @positions.inject(@shipping) { |memo, pos| pos.total + memo }
end

#total_incl_vatObject



218
219
220
221
222
# File 'lib/bbmb/model/order.rb', line 218

def total_incl_vat
  if rate = BBMB.config.vat_rate
    total * (100 + rate.to_f) / 100
  end
end

#vatObject



253
254
255
256
257
# File 'lib/bbmb/model/order.rb', line 253

def vat
  if rate = BBMB.config.vat_rate
    total * rate.to_f / 100
  end
end