Module: XmlConv::PostProcess::Bbmb2

Defined in:
lib/postprocess/bbmb2.rb

Class Method Summary collapse

Class Method Details

.iconv(str) ⇒ Object



96
97
98
99
# File 'lib/postprocess/bbmb2.rb', line 96

def Bbmb2.iconv(str)
  @iconv ||= Iconv.new('utf8', 'latin1')
  @iconv.iconv str
end

.info(delivery) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/postprocess/bbmb2.rb', line 86

def Bbmb2.info(delivery)
  info = {
    :reference => iconv(delivery.customer_id),
  }
  lines = []
  if(text = delivery.free_text)
    info.store(:comment, iconv(text))
  end
  info
end

.inject(drb_url, idtype, transaction = nil) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/postprocess/bbmb2.rb', line 10

def Bbmb2.inject(drb_url, idtype, transaction=nil)
  if transaction.nil?
    transaction, idtype = idtype, nil
  end
  if(bdd = transaction.model)
    bbmb = DRbObject.new(nil, drb_url)
    messages = []
    bdd.deliveries.each_with_index { |delivery, idx|
      inject_id, order, info = nil
      begin
        customer = delivery.customer
        inject_id = customer.acc_id
        name = customer.name
        if ship = customer.ship_to
          name = ship.name
          if addr = ship.address
            name = addr.lines.first
          end
          inject_id ||= ship.acc_id
        end
        inject_id ||= customer.party_id
        order = order(delivery)
        info = info(delivery)
	options = {
		:deliver => true,
		:create_missing_customer => idtype.to_s,
		:transaction => transaction.transaction_id.to_s,
		:customer_name => name.to_s,
	}
	resp = bbmb.inject_order(inject_id, order, info, options)
        transaction.respond(idx, resp)
      rescue Exception => e
        transaction.respond(idx, :products => order)
        message = "Bestellung OK, Eintrag in BBMB Fehlgeschlagen:\n" \
          << e.class.to_s << "\n" \
          << e.message << "\n\n" \
          << e.backtrace.join("\n") << "\n\n" \
          << "hospital: #{inject_id}\n"
        if(order)
          message << "\norder: \n"
          order.each { |k|  message << "#{k.inspect}\n" }
        end
        if(info)
          message << "\ninfo: \n"
            info.each { |k,v| message << "#{k} => #{v}\n" }
        end
        messages.push message
      end
    }
    unless messages.empty?
      raise messages.join("\n\n")
    end
    transaction.status = :bbmb_ok
  end
end

.item_ids(item) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/postprocess/bbmb2.rb', line 65

def Bbmb2.item_ids(item)
  item.id_table.inject({}) { |memo, (domain, value)|
    key = case domain
          when 'et-nummer'
            :ean13
          when 'pharmacode'
            :pcode
          when 'lieferantenartikel'
            :article_number
          end
    memo.store(key, value.gsub(/^0+/, ''))
    memo
  }
end

.order(delivery) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/postprocess/bbmb2.rb', line 79

def Bbmb2.order(delivery)
  delivery.items.collect { |item|
    data = item_ids(item)
    data.store(:quantity, item.qty.to_i)
    data
  }
end