Class: BBMB::Model::Order
- Inherits:
-
Object
- Object
- BBMB::Model::Order
- Includes:
- Util::Numbers, Enumerable, ODBA::Persistable
- Defined in:
- lib/bbmb/model/order.rb,
lib/bbmb/persistence/odba/model/order.rb
Defined Under Namespace
Constant Summary collapse
- ODBA_SERIALIZABLE =
['@unavailable']
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#commit_id ⇒ Object
readonly
Returns the value of attribute commit_id.
-
#commit_time ⇒ Object
readonly
Returns the value of attribute commit_time.
-
#customer ⇒ Object
readonly
Returns the value of attribute customer.
-
#positions ⇒ Object
readonly
Returns the value of attribute positions.
-
#reference ⇒ Object
Returns the value of attribute reference.
-
#unavailable ⇒ Object
readonly
Returns the value of attribute unavailable.
Instance Method Summary collapse
- #__old_add__ ⇒ Object
- #__old_clear__ ⇒ Object
- #__old_commit__ ⇒ Object
- #add(quantity, product) ⇒ Object
- #additional_info ⇒ Object
- #calculate_effective_prices ⇒ Object
- #clear ⇒ Object
- #commit!(*args) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #filename ⇒ Object
- #i2_body ⇒ Object
- #i2_header ⇒ Object
- #i2_position(line, position, quantity) ⇒ Object
- #increment(quantity, product) ⇒ Object
-
#initialize(customer) ⇒ Order
constructor
A new instance of Order.
- #item_count ⇒ Object
- #order_id ⇒ Object
- #position(product) ⇒ Object
- #price_effective(pos) ⇒ Object
- #quantity(product) ⇒ Object
- #quota(article_number) ⇒ Object
- #reverse! ⇒ Object
- #size ⇒ Object
- #sort!(*args, &block) ⇒ Object
- #sort_by(*args, &block) ⇒ Object
- #to_csv ⇒ Object
- #to_i2 ⇒ Object
- #to_target_format ⇒ Object
- #total ⇒ Object
- #total_incl_vat ⇒ Object
- #vat ⇒ Object
Methods included from Util::Numbers
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
#comment ⇒ Object
Returns the value of attribute comment.
55 56 57 |
# File 'lib/bbmb/model/order.rb', line 55 def comment @comment end |
#commit_id ⇒ Object (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_time ⇒ Object (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 |
#customer ⇒ Object (readonly)
Returns the value of attribute customer.
54 55 56 |
# File 'lib/bbmb/model/order.rb', line 54 def customer @customer end |
#positions ⇒ Object (readonly)
Returns the value of attribute positions.
54 55 56 |
# File 'lib/bbmb/model/order.rb', line 54 def positions @positions end |
#reference ⇒ Object
Returns the value of attribute reference.
55 56 57 |
# File 'lib/bbmb/model/order.rb', line 55 def reference @reference end |
#unavailable ⇒ Object (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_info ⇒ Object
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_prices ⇒ Object
89 90 91 92 93 94 |
# File 'lib/bbmb/model/order.rb', line 89 def calculate_effective_prices delete_invalid_positions @positions.each { |pos| pos.price_effective = price_effective(pos) } end |
#clear ⇒ Object
95 96 97 98 |
# File 'lib/bbmb/model/order.rb', line 95 def clear @positions.clear @unavailable.clear end |
#commit!(*args) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bbmb/model/order.rb', line 99 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
109 110 111 |
# File 'lib/bbmb/model/order.rb', line 109 def each(&block) @positions.each(&block) end |
#empty? ⇒ Boolean
112 113 114 |
# File 'lib/bbmb/model/order.rb', line 112 def empty? @positions.empty? end |
#filename ⇒ Object
115 116 117 |
# File 'lib/bbmb/model/order.rb', line 115 def filename sprintf("%s-%s.txt", order_id, @commit_time.strftime('%Y%m%d%H%M%S')) end |
#i2_body ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bbmb/model/order.rb', line 128 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_header ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/bbmb/model/order.rb', line 141 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
167 168 169 170 171 172 173 |
# File 'lib/bbmb/model/order.rb', line 167 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
118 119 120 121 122 123 |
# File 'lib/bbmb/model/order.rb', line 118 def increment(quantity, product) if(pos = position(product)) quantity += pos.quantity end add(quantity, product) end |
#item_count ⇒ Object
124 125 126 127 |
# File 'lib/bbmb/model/order.rb', line 124 def item_count delete_invalid_positions @positions.inject(0) { |memo, pos| memo + pos.quantity } end |
#order_id ⇒ Object
174 175 176 |
# File 'lib/bbmb/model/order.rb', line 174 def order_id sprintf "%s-%s" ,@customer ? @customer.customer_id : 'no_customer', @commit_id end |
#position(product) ⇒ Object
177 178 179 |
# File 'lib/bbmb/model/order.rb', line 177 def position(product) @positions.find { |pos| pos.product == product } end |
#price_effective(pos) ⇒ Object
180 181 182 183 |
# File 'lib/bbmb/model/order.rb', line 180 def price_effective(pos) ((quota = quota(pos.article_number)) && quota.price) \ || pos.price_effective end |
#quantity(product) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/bbmb/model/order.rb', line 184 def quantity(product) if(pos = position(product)) pos.quantity else 0 end end |
#quota(article_number) ⇒ Object
191 192 193 |
# File 'lib/bbmb/model/order.rb', line 191 def quota(article_number) @customer.quota(article_number) end |
#reverse! ⇒ Object
194 195 196 |
# File 'lib/bbmb/model/order.rb', line 194 def reverse! @positions.reverse! end |
#size ⇒ Object
197 198 199 |
# File 'lib/bbmb/model/order.rb', line 197 def size @positions.size end |
#sort!(*args, &block) ⇒ Object
200 201 202 |
# File 'lib/bbmb/model/order.rb', line 200 def sort!(*args, &block) @positions.sort!(*args, &block) end |
#sort_by(*args, &block) ⇒ Object
203 204 205 206 207 |
# File 'lib/bbmb/model/order.rb', line 203 def sort_by(*args, &block) twin = dup twin.positions.replace @positions.sort_by(*args, &block) twin end |
#to_csv ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/bbmb/model/order.rb', line 218 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_i2 ⇒ Object
242 243 244 |
# File 'lib/bbmb/model/order.rb', line 242 def to_i2 i2_header << "\n" << i2_body << "\n" end |
#to_target_format ⇒ Object
245 246 247 |
# File 'lib/bbmb/model/order.rb', line 245 def to_target_format self.send("to_#{BBMB.config.target_format}") end |
#total ⇒ Object
208 209 210 211 212 |
# File 'lib/bbmb/model/order.rb', line 208 def total delete_invalid_positions res = @positions.inject(@shipping) { |memo, pos| pos.total + memo } res || Util::Money.new(0) end |