Module: Rawbotz::MailTemplate

Defined in:
lib/rawbotz/mail_template.rb

Class Method Summary collapse

Class Method Details

.consume(template, order) ⇒ Object

Substitute certain patterns in template



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rawbotz/mail_template.rb', line 4

def self.consume template, order
  result = ""
  result = template.gsub(/SUPPLIERNAME/, order.supplier[:name])
  result = result.gsub(/PUBLIC_COMMENT/, order.public_comment || "")

  lines = result.split("\n")
  subject, lines = lines.partition{|l| l.start_with?("SUBJECT=")}
  product_line = lines.detect{|l| l.start_with?("* ")}
  order_lines = order.order_items.select{|o|o.num_wished.to_i != 0}.map do |oi|
    next if product_line.nil?
    order_item_line = product_line[2..-1]
    order_item_line.gsub!(/SUPPLIERSKU/, oi.local_product[:supplier_sku].to_s)
    order_item_line.gsub!(/QTY/, oi[:num_wished].to_s)
    if oi.local_product[:packsize].to_s != ""
      order_item_line.gsub!(/NUM_PACKS/, "%g" % (oi[:num_wished] / oi.local_product[:packsize].to_f))
    else
      order_item_line.gsub!(/NUM_PACKS/, '')
    end
    order_item_line.gsub!(/PACKSIZE/, oi.local_product[:packsize].to_s)
    if oi.local_product[:supplier_prod_name].to_s != ""
      order_item_line.gsub!(/PRODUCTNAME/, oi.local_product[:supplier_prod_name])
    else
      order_item_line.gsub!(/PRODUCTNAME/, oi.local_product[:name].to_s)
    end
    order_item_line
    #SUPPLIERSKU
    #SUPPLIERPRODUCTNAME
  end
  lines[lines.find_index(product_line)] = order_lines if product_line
  lines.flatten.join("\n")
end

.create_mailto_url(supplier, order) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rawbotz/mail_template.rb', line 49

def self.create_mailto_url supplier, order
  mail_preview = self.consume supplier[:order_template], order
  subject = self.extract_subject supplier[:order_template], order
  to_mail = (supplier[:email] || "").split[0]
  if supplier[:email].to_s == ""
    cc_mail = ""
  else
    cc_mail = supplier[:email].split[1..-1].map{|m| "cc=#{m}"}.join("&")
  end
  if cc_mail.to_s != ""
    cc_mail += "&"
  end
  "mailto:#{to_mail}?#{cc_mail}Subject=#{subject}&body=%s" % URI::escape(mail_preview).gsub(/&/,'%26')
end

.extract_subject(template, order) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rawbotz/mail_template.rb', line 36

def self.extract_subject template, order
  result = ""
  result = template.gsub(/SUPPLIERNAME/, order.supplier[:name])

  lines = result.split("\n")
  subject, lines = lines.partition{|l| l.start_with?("SUBJECT=")}
  if !subject.empty?
    subject[0][8..-1]
  else
    "Order"
  end
end