forked from zdavatz/sandoz.xmlconv.bbmb.ch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbmb2.rb
101 lines (99 loc) · 3.06 KB
/
bbmb2.rb
1
2
3
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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
# PostProcess::Bbmb2 -- xmlconv2 -- 25.08.2006 -- hwyss@ywesee.com
require 'drb'
module XmlConv
module PostProcess
module Bbmb2
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
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
def Bbmb2.order(delivery)
delivery.items.collect { |item|
data = item_ids(item)
data.store(:quantity, item.qty.to_i)
data
}
end
def Bbmb2.info(delivery)
info = {
:reference => iconv(delivery.customer_id),
}
lines = []
if(text = delivery.free_text)
info.store(:comment, iconv(text))
end
info
end
def Bbmb2.iconv(str)
@iconv ||= Iconv.new('utf8', 'latin1')
@iconv.iconv str
end
end
end
end