Module: Shoppy::OrdersHelper

Included in:
OrdersHelper
Defined in:
app/helpers/shoppy/orders_helper.rb

Class Method Summary collapse

Class Method Details

.calculate_points(order) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/shoppy/orders_helper.rb', line 4

def self.calculate_points(order)
  Customer.transaction do
    c = order.customer
    ois = order.order_lines
    ois.each do |ol|
      c.points += (ol.variant.points * ol.quantity)
    end
    c.save
    return true
  end
end

.new_order(customer, order_lines_hash, customer_address) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/shoppy/orders_helper.rb', line 16

def self.new_order(customer, order_lines_hash, customer_address)
  OrderLine.transaction do
    order = Order.new
    order.customer = customer
    order.customer_name         = customer.name
    order.customer_email        = customer.email
    order.customer_phone        = customer.phone
    order.customer_add_line1    = customer_address.add_line1
    order.customer_add_line2    = customer_address.add_line2
    order.customer_city         = customer_address.city
    order.customer_state        = customer_address.state
    order.customer_country      = customer_address.country
    order.customer_zip          = customer_address.zip
    order.save
    order_total_price = 0.0
    ols = []
    order_lines_hash.each do |variant_id, quantity|
      v = Variant.find_by(id: variant_id)
      if v && quantity > 0
        ol = OrderLine.new
        ol.order = order
        ol.variant = Variant.find(variant_id.to_i)
        ol.quantity = quantity.to_i
        ol.price_per_one = ol.variant.price
        line_totalprice = ol.quantity * ol.price_per_one
        order_total_price += line_totalprice
        ols += [ol]
      end
    end
    order.total_price = order_total_price
    order.date = Date.today
    order.order_lines = ols
    if ols.count > 0 && order.save
      OrdersHelper::calculate_points(order)
      return order
    else
      return nil
    end
  end
end