/
builder.rb
883 lines (879 loc) · 30.5 KB
/
builder.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# encoding: utf-8
require 'nokogiri'
module Nokogiri
module XML
class Document < Nokogiri::XML::Node
attr_writer :tag_suffix
alias :create_element_origin :create_element
def create_element name, *args, &block
name += (@tag_suffix || '')
create_element_origin(name, *args, &block)
end
end
end
end
module Oddb2xml
class Builder
attr_accessor :subject, :index, :items, :flags, :lppvs,
:actions, :migel, :orphans, :fridges,
:infos, :packs,
:ean14, :tag_suffix
def initialize
@subject = nil
@index = {}
@items = {}
@flags = {}
@lppvs = {}
@infos = {}
@packs = {}
@migel = {}
@actions = []
@orphans = []
@fridges = []
@ean14 = true
@tag_suffix = nil
if block_given?
yield self
end
end
def to_xml(subject=nil)
if subject
self.send('build_' + subject.to_s)
elsif @subject
self.send('build_' + @subject.to_s)
end
end
def to_dat(subject=nil)
if subject
self.send('build_' + subject.to_s)
elsif @subject
self.send('build_' + @subject.to_s)
end
end
private
def prepare_articles(reset=false)
@articles = nil if reset
unless @articles
@articles = [] # base is 'DE'
@index['DE'].each_pair do |pharmacode, index|
object = {
:de => index,
:fr => @index['FR'][pharmacode],
}
# overwrite
if migel = @migel[pharmacode]
%w[de fr].each do |lang|
l = lang.intern
object[l][:ean] = migel[:ean]
object[l][:desc] = migel["desc_#{lang}".intern]
object[l][:additional_desc] = migel[:additional_desc]
object[l][:company_name] = migel[:company_name]
object[l][:company_ean] = migel[:company_ean]
end
@migel[pharmacode] = nil
end
if seq = @items[pharmacode]
object[:seq] = seq
end
@articles << object
end
# add
@migel.values.compact.each do |migel|
next if migel[:pharmacode].empty?
object = {}
%w[de fr].each do |lang|
pac = {
:ean => migel[:ean],
:pharmacode => migel[:pharmacode],
:status => 'I',
:stat_date => '',
:lang => lang.capitalize,
:desc => migel["desc_#{lang}".intern],
:atc_code => '',
:additional_desc => migel[:additional_desc],
:company_ean => migel[:company_ean],
:company_name => migel[:company_name],
}
object[lang.intern] = pac
end
@articles << object
end
end
end
def prepare_substances
unless @substances
@substances = []
@items.values.uniq.each do |seq|
seq[:substances].each do |sub|
@substances << sub[:name]
end
end
@substances.uniq!
@substances.sort!
end
end
def prepare_limitations
unless @limitations
@limitations = []
@items.values.uniq.each do |seq|
seq[:packages].each_value do |pac|
@limitations += pac[:limitations]
end
end
@limitations.uniq! {|lim| lim[:code] + lim[:type] }
@limitations.sort_by!{|lim| lim[:code] }
end
end
def prepare_interactions
unless @interactions
@interactions = []
@actions.each do |act|
@interactions << act
end
end
end
def prepare_codes
unless @codes
@codes = {
'X' => {:int => 11, :txt => 'Kontraindiziert'},
'E' => {:int => 12, :txt => 'Kontraindiziert'},
'D' => {:int => 13, :txt => 'Kombination meiden'},
'C' => {:int => 14, :txt => 'Monitorisieren'},
'B' => {:int => 15, :txt => 'Vorsichtsmassnahmen'},
'A' => {:int => 16, :txt => 'keine Massnahmen'}
}
end
end
def prepare_products
unless @products
# merge company info from swissINDEX
@products = []
@products = @items.values.uniq.map do |seq|
%w[de fr].each do |lang|
name_key = "company_name_#{lang}".intern
seq[name_key] = ''
if pharmacode = seq[:pharmacodes].first
indices = @index[lang.upcase]
if index = indices[pharmacode]
seq[name_key] = index[:company_name]
end
end
end
seq
end
end
end
def build_substance
prepare_substances
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.SUBSTANCE(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
@substances.each_with_index do |sub_name, i|
xml.SB('DT' => '') {
xml.SUBNO (i + 1).to_i
#xml.NAMD
#xml.ANAMD
#xml.NAMF
xml.NAML sub_name
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @substances.length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_limitation
prepare_limitations
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.LIMITATION(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
@limitations.each do |lim|
xml.LIM('DT' => '') {
xml.LIMCD lim[:key] # swissmedic_number8, swissmedic_number5 or pharmacode
xml.IT lim[:it]
xml.LIMTYP lim[:type]
xml.LIMVAL lim[:value]
xml.LIMNAMEBAG lim[:code] # original LIMCD
xml.LIMNIV lim[:niv]
xml.DSCRD lim[:desc_de]
xml.DSCRF lim[:desc_fr]
xml.VDAT lim[:vdate]
if lim[:del]
xml.DEL 3
end
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @limitations.length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_interaction
prepare_interactions
prepare_codes
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.INTERACTION(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
@interactions.sort_by{|ix| ix[:ixno] }.each do |ix|
xml.IX('DT' => '') {
xml.IXNO ix[:ixno]
xml.TITD ix[:title]
#xml.TITF
xml.GRP1D ix[:atc1]
#xml.GRP1F
xml.GRP2D ix[:atc2]
#xml.GRP2F
xml.EFFD ix[:effect]
#xml.EFFF
if dict = @codes[ix[:grad].upcase]
xml.RLV dict[:int]
xml.RLVD dict[:txt]
#xml.RLVF
end
#xml.EFFTXTD
#xml.EFFTXTF
xml.MECHD ix[:mechanism]
#xml.MECHF
xml.MEASD ix[:measures]
#xml.MEASF
#xml.REMD
#xml.REMF
#xml.LIT
xml.DEL false
#xml.IXMCH {
# xml.TYP
# xml.TYPD
# xml.TYPF
# xml.CD
# xml.CDD
# xml.CDF
# xml.TXTD
# xml.TXTF
#}
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @interactions.length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_code
prepare_codes
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.CODE(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
@codes.each_pair do |val, definition|
xml.CD('DT' => '') {
xml.CDTYP definition[:int]
xml.CDVAL val
xml.DSCRSD definition[:txt]
#xml.DSCRSF
#xml.DSCRMD
#xml.DSCRMF
#xml.DSCRD
#xml.DSCRF
xml.DEL false
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @codes.keys.length
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_product
prepare_substances
prepare_products
prepare_interactions
prepare_codes
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.PRODUCT(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
@products.each do |seq|
xml.PRD('DT' => '') {
xml.PRDNO seq[:product_key] unless seq[:product_key].empty?
%w[de fr].each do |l|
name = "name_#{l}".intern
desc = "desc_#{l}".intern
elem = "DSCR" + l[0].upcase
if !seq[name].empty? and !seq[desc].empty?
xml.send(elem, seq[name] + ' ' + seq[desc])
elsif !seq[desc].empty?
xml.send(elem, seq[desc])
end
end
#xml.BNAMD
#xml.BNAMF
#xml.ADNAMD
#xml.ADNAMF
#xml.SIZE
xml.ADINFD seq[:comment_de] unless seq[:comment_de].empty?
xml.ADINFF seq[:comment_fr] unless seq[:comment_fr].empty?
xml.GENCD seq[:org_gen_code] unless seq[:org_gen_code].empty?
#xml.GENGRP
xml.ATC seq[:atc_code] unless seq[:atc_code].empty?
xml.IT seq[:it_code] unless seq[:it_code].empty?
#xml.ITBAG
#xml.KONO
#xml.TRADE
#xml.PRTNO
#xml.MONO
#xml.CDGALD
#xml.CDGALF
#xml.FORMD
#xml.FORMF
#xml.DOSE
#xml.DOSEU
#xml.DRGFD
#xml.DRGFF
seq[:packages].values.first[:swissmedic_number8] =~ /(\d{5})(\d{3})/
xml.ORPH @orphans.include?($1.to_s) ? true : false
#xml.BIOPHA
#xml.BIOSIM
#xml.BFS
#xml.BLOOD
#xml.MSCD # always empty
#xml.DEL
xml.CPT {
#xml.CPTLNO
#xml.CNAMED
#xml.CNAMEF
#xml.IDXIND
#xml.DDDD
#xml.DDDU
#xml.DDDA
#xml.IDXIA
#xml.IXREL
#xml.GALF
#xml.DRGGRPCD
#xml.PRBSUIT
#xml.CSOLV
#xml.CSOLVQ
#xml.CSOLVQU
#xml.PHVAL
#xml.LSPNSOL
#xml.APDURSOL
#xml.EXCIP
#xml.EXCIPQ
#xml.EXCIPCD
#xml.EXCIPCF
#xml.PQTY
#xml.PQTYU
#xml.SIZEMM
#xml.WEIGHT
#xml.LOOKD
#xml.LOOKF
#xml.IMG2
seq[:substances].each do |sub|
xml.CPTCMP {
xml.LINE sub[:index] unless sub[:index].empty?
xml.SUBNO @substances.index(sub[:name]) + 1 if @substances.include?(sub[:name])
xml.QTY sub[:quantity] unless sub[:quantity].empty?
xml.QTYU sub[:unit] unless sub[:unit].empty?
#xml.WHK
}
end
@interactions.each do |ix|
if [ix[:act1], ix[:act2]].include?(seq[:atc_code])
xml.CPTIX {
xml.IXNO ix[:ixno]
#xml.GRP
xml.RLV @codes[ix[:grad]]
}
end
end
#xml.CPTROA {
#xml.SYSLOC
#xml.ROA
#}
}
#xml.PRDICD { # currently empty
#xml.ICD
#xml.RTYP
#xml.RSIG
#xml.REMD
#xml.REMF
#}
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @products.length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_article
prepare_limitations
prepare_articles
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.ARTICLE(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
@articles.each do |obj|
de_pac = obj[:de] # swiss index DE (base)
fr_pac = obj[:fr] # swiss index FR
bg_pac = nil # BAG XML (additional data)
if obj[:seq]
bg_pac = obj[:seq][:packages][de_pac[:pharmacode]]
end
if de_pac[:ean].length == 13
num = de_pac[:ean][4,8].intern # :swissmedic_number5
elsif bg_pac
num = bg_pac[:swissmedic_number8].intern
end
xml.ART('DT' => '') {
xml.PHAR de_pac[:pharmacode] unless de_pac[:pharmacode].empty?
#xml.GRPCD
#xml.CDS01
#xml.CDS02
if obj[:seq]
xml.PRDNO obj[:seq][:product_key] unless obj[:seq][:product_key].empty?
end
if bg_pac # bag xml
xml.SMCAT bg_pac[:swissmedic_category] unless bg_pac[:swissmedic_category].empty?
elsif @packs[num] and @packs[num][:swissmedic_category] # Packungen.xls
xml.SMCAT @packs[num][:swissmedic_category]
end
if bg_pac
xml.SMNO bg_pac[:swissmedic_number8] unless bg_pac[:swissmedic_number8].empty?
end
#xml.HOSPCD
#xml.CLINCD
#xml.ARTTYP
#xml.VAT
if de_pac
xml.SALECD de_pac[:status].empty? ? 'N' : de_pac[:status]
end
if bg_pac
#xml.INSLIM
xml.LIMPTS bg_pac[:limitation_points] unless bg_pac[:limitation_points].empty?
end
#xml.GRDFR
if bg_pac
if !bg_pac[:swissmedic_number8].empty? and
bg_pac[:swissmedic_number8].to_s =~ /(\d{5})(\d{3})/
xml.COOL 1 if @fridges.include?($1.to_s)
end
end
#xml.TEMP
unless de_pac[:ean].empty?
flag = @flags[de_pac[:ean]]
# as same flag
xml.CDBG (flag ? 'Y' : 'N')
xml.BG (flag ? 'Y' : 'N')
end
#xml.EXP
xml.QTY de_pac[:additional_desc] unless de_pac[:additional_desc].empty?
xml.DSCRD de_pac[:desc] unless de_pac[:desc].empty?
xml.DSCRF fr_pac[:desc] unless fr_pac[:desc].empty?
xml.SORTD de_pac[:desc].upcase unless de_pac[:desc].empty?
xml.SORTF fr_pac[:desc].upcase unless fr_pac[:desc].empty?
#xml.QTYUD
#xml.QTYUF
#xml.IMG
#xml.IMG2
#xml.PCKTYPD
#xml.PCKTYPF
#xml.MULT
if obj[:seq]
xml.SYN1D obj[:seq][:name_de] unless obj[:seq][:name_de].empty?
xml.SYN1F obj[:seq][:name_fr] unless obj[:seq][:name_fr].empty?
end
if obj[:seq]
case obj[:seq][:deductible]
when 'Y'; xml.SLOPLUS 1; # 20%
when 'N'; xml.SLOPLUS 2; # 10%
else xml.SLOPLUS '' # k.A.
end
end
#xml.NOPCS
#xml.HSCD
#xml.MINI
#xml.DEPCD
#xml.DEPOT
#xml.BAGSL
#xml.BAGSLC
#xml.LOACD
if de_pac[:status] == "I"
xml.OUTSAL de_pac[:stat_date] unless de_pac[:stat_date].empty?
end
#xml.STTOX
#xml.NOTI
#xml.GGL
#xml.CE
#xml.SMDAT
#xml.SMCDAT
#xml.SIST
#xml.ESIST
#xml.BIOCID
#xml.BAGNO
#xml.LIGHT
#xml.DEL
xml.ARTCOMP {
# use ean13(gln) as COMPNO
xml.COMPNO de_pac[:company_ean] unless de_pac[:company_ean].empty?
#xml.ROLE
#xml.ARTNO1
#xml.ARTNO2
#xml.ARTNO3
}
xml.ARTBAR {
xml.CDTYP 'E13'
xml.BC de_pac[:ean] unless de_pac[:ean].empty?
xml.BCSTAT 'A' # P is alternative
#xml.PHAR2
}
#xml.ARTCH {
#xml.PHAR2
#xml.CHTYPE
#xml.LINENO
#xml.NOUNITS
#}
if bg_pac
bg_pac[:prices].each_pair do |key, price|
xml.ARTPRI {
xml.VDAT price[:valid_date] unless price[:valid_date].empty?
xml.PTYP price[:price_code] unless price[:price_code].empty?
xml.PRICE price[:price] unless price[:price].empty?
}
end
end
#xml.ARTMIG {
#xml.VDAT
#xml.MIGCD
#xml.LINENO
#}
#xml.ARTDAN {
#xml.CDTYP
#xml.LINENO
#xml.CDVAL
#}
if bg_pac
bg_pac[:limitations].each do |lim|
xml.ARTLIM {
xml.LIMCD lim[:code] unless lim[:code].empty?
}
end
end
if @lppvs[de_pac[:ean]]
xml.ARTINS {
#xml.VDAT
#xml.INCD
xml.NINCD 20
}
end
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @articles.length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_fi
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.KOMPENDIUM(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
length = 0
%w[de fr].each do |lang|
length += @infos[lang].length
@infos[lang].each_with_index do |info, i|
xml.KMP(
'MONTYPE' => 'fi', # only
'LANG' => lang.upcase,
'DT' => '',
) {
unless info[:name].empty?
xml.name { xml.p info[:name] }
end
unless info[:owner].empty?
xml.owner { xml.p info[:owner] }
end
xml.monid info[:monid] unless info[:monid].empty?
xml.paragraph { xml.cdata info[:paragraph] unless info[:paragraph].empty? }
}
end
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD length
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
def build_fi_product
prepare_products
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T.%7N%z')
xml.KOMPENDIUM_PRODUCT(
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://wiki.oddb.org/wiki.php?pagename=Swissmedic.Datendeklaration',
'CREATION_DATETIME' => datetime,
'PROD_DATE' => datetime,
'VALID_DATE' => datetime,
) {
length = 0
%w[de fr].each do |lang|
info_index = {}
@infos[lang].each_with_index do |info, i|
info_index[info[:monid]] = i
end
# prod
@products.each do |seq|
seq[:packages].values.each do |pac|
if pac[:swissmedic_number8] =~ /(\d{5})(\d{3})/
number = $1.to_s
if i = info_index[number]
length += 1
xml.KP('DT' => '') {
xml.MONID @infos[lang][i][:monid]
xml.PRDNO seq[:product_key] unless seq[:product_key].empty?
# as orphans ?
xml.DEL @orphans.include?(number) ? true : false
}
end
end
end
end
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD length
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
_builder.to_xml
end
### --- see oddb2tdat
def format_price(price_str, len=6, int_len=4, frac_len=2)
price = price_str.split('.')
pre = ''
las = ''
pre = "%0#{int_len}d" % (price[0] ? price[0] : '0')
las = if price[1]
if price[1].size < frac_len
price[1] + "0"*(frac_len-price[2].to_s.size)
else
price[1][0,frac_len]
end
else
'0'*frac_len
end
(pre.to_s + las.to_s)[0,len]
end
def format_date(date_str, len=7)
date = date_str.gsub('.','')
if date.size < len
date = date + '0'*(len-date.size)
end
date[0,len]
end
DAT_LEN = {
:RECA => 2,
:CMUT => 1,
:PHAR => 7,
:ABEZ => 50,
:PRMO => 6,
:PRPU => 6,
:CKZL => 1,
:CLAG => 1,
:CBGG => 1,
:CIKS => 1,
:ITHE => 7,
:CEAN => 13,
:CMWS => 1,
}
def build_dat
prepare_articles
rows = []
@articles.each do |obj|
row = ''
de_pac = obj[:de]
# Oddb2tdat.parse
if obj[:de][:status] =~ /A|I/
pac,num = nil, nil
if obj[:seq]
pac = obj[:seq][:packages][de_pac[:pharmacode]]
end
# :swissmedic_numbers
if de_pac[:ean].length == 13
num = de_pac[:ean][4,8].intern # :swissmedic_number5
elsif pac
num = pac[:swissmedic_number8].intern
end
row << "%#{DAT_LEN[:RECA]}s" % '11'
row << "%#{DAT_LEN[:CMUT]}s" % if (phar = de_pac[:pharmacode] and phar.size > 3) # does not check expiration_date
obj[:de][:status] == "I" ? '3' : '1'
else
'3'
end
row << "%0#{DAT_LEN[:PHAR]}d" % de_pac[:pharmacode].to_i
row << "%-#{DAT_LEN[:ABEZ]}s" % (
de_pac[:desc].to_s.gsub(/"/, '') + " " +
(pac ? pac[:name_de].to_s : '') +
de_pac[:additional_desc]
).to_s[0, DAT_LEN[:ABEZ]].gsub(/"/, '')
row << "%#{DAT_LEN[:PRMO]}s" % (pac ? format_price(pac[:prices][:exf_price][:price].to_s) : ('0' * DAT_LEN[:PRMO]))
row << "%#{DAT_LEN[:PRPU]}s" % (pac ? format_price(pac[:prices][:pub_price][:price].to_s) : ('0' * DAT_LEN[:PRPU]))
row << "%#{DAT_LEN[:CKZL]}s" % if (@lppvs[de_pac[:ean]])
'2'
elsif pac # sl_entry
'1'
else
'3'
end
row << "%#{DAT_LEN[:CLAG]}s" % if ((num && num.to_s =~ /(\d{5})(\d{3})/) and
@fridges.include?($1.to_s))
'1'
else
'0'
end
row << "%#{DAT_LEN[:CBGG]}s" % if ((pac && pac[:narcosis_flag] == 'Y') or # BAGXml
(@flags[de_pac[:ean]])) # ywesee BM_update
'3'
else
'0'
end
row << "%#{DAT_LEN[:CIKS]}s" % if (pac && pac[:swissmedic_category] =~ /^[ABCDE]$/) # BAGXml
pac[:swissmedic_category]
elsif (@packs[num]) # Packungen.xls
@packs[num][:swissmedic_category]
else
'0'
end.gsub(/(\+|\s)/, '')
row << "%0#{DAT_LEN[:ITHE]}d" % if (@packs[num])
format_date(@packs[num][:ith_swissmedic])
else
('0' * DAT_LEN[:ITHE])
end.to_i
row << "%0#{DAT_LEN[:CEAN]}d" % de_pac[:ean].to_i
row << "%#{DAT_LEN[:CMWS]}s" % '2' # pharma
rows << row
end
end
rows.join("\n")
end
def build_with_migel_dat
reset = true
prepare_articles(reset)
rows = []
@articles.each do |obj|
row = ''
de_pac = obj[:de]
next if (!ean14 && de_pac[:ean].to_s.length != 13)
# Oddb2tdat.parse_migel
row << "%#{DAT_LEN[:RECA]}s" % '11'
row << "%#{DAT_LEN[:CMUT]}s" % if (phar = de_pac[:pharmacode] and phar.size > 3)
'1'
else
'3'
end
row << "%0#{DAT_LEN[:PHAR]}d" % de_pac[:pharmacode].to_i
row << "%-#{DAT_LEN[:ABEZ]}s" % (
de_pac[:desc].to_s.gsub(/"/, '') + " " +
de_pac[:additional_desc]
).to_s[0, DAT_LEN[:ABEZ]].gsub(/"/, '')
row << "%#{DAT_LEN[:PRMO]}s" % ('0' * DAT_LEN[:PRMO])
row << "%#{DAT_LEN[:PRPU]}s" % ('0' * DAT_LEN[:PRPU])
row << "%#{DAT_LEN[:CKZL]}s" % '3' # sl_entry and lppv
row << "%#{DAT_LEN[:CLAG]}s" % '0'
row << "%#{DAT_LEN[:CBGG]}s" % '0'
row << "%#{DAT_LEN[:CIKS]}s" % ' ' # no category
row << "%0#{DAT_LEN[:ITHE]}d" % 0
row << "%0#{DAT_LEN[:CEAN]}d" % (de_pac[:ean] ? de_pac[:ean].to_i : 0)
row << "%#{DAT_LEN[:CMWS]}s" % '1' # nonpharma
rows << row
end
rows.join("\n")
end
end
end