forked from zdavatz/oddb2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.rb
1303 lines (1292 loc) · 48.8 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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
require 'nokogiri'
require 'oddb2xml/util'
require 'oddb2xml/calc'
require 'csv'
class Numeric
# round a given number to the nearest step
def round_by(increment)
(self / increment).round * increment
end
end
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
XML_OPTIONS = {
'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' => Time.new.strftime('%FT%T%z'),
'PROD_DATE' => Time.new.strftime('%FT%T%z'),
'VALID_DATE' => Time.new.strftime('%FT%T%z'),
}
class Builder
attr_accessor :subject, :refdata, :items, :flags, :lppvs,
:actions, :migel, :orphan, :fridge,
:infos, :packs, :infos_zur_rose,
:ean14, :tag_suffix,
:companies, :people,
:xsd
def initialize(args = {})
@options = args
@subject = nil
@refdata = {}
@items = {} # Items from Preparations.xml in BAG, using GTINs as key
@flags = {}
@lppvs = {}
@infos = {}
@packs = {}
@migel = {}
@infos_zur_rose = {} # zurrose
@actions = []
@orphan = []
@fridge = []
@ean14 = false
@companies = []
@people = []
@tag_suffix = nil
@pharmacode = {} # index pharmacode => item
if block_given?
yield self
end
end
def to_xml(subject=nil)
Oddb2xml.log "to_xml subject #{subject} #{@subject} for #{self.class}"
if subject
self.send('build_' + subject.to_s)
elsif @subject
self.send('build_' + @subject.to_s)
end
end
def to_dat(subject=nil)
Oddb2xml.log "to_dat subject #{subject ? subject.to_s : @subject.to_s} for #{self.class}"
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
Oddb2xml.log("prepare_articles starting with #{@articles ? @articles.size : 'no'} articles.")
@articles = []
@refdata.each do |ean13, obj|
if migel = @migel[ean13]
# delete duplicates
@migel[ean13] = nil
end
if seq = @items[obj[:ean]]
obj[:seq] = seq
end
@articles << obj
@pharmacode[obj[:pharmacode]] = obj
end
# add
@migel.values.compact.each do |migel|
next unless migel[:pharmacode]
entry = {
:ean => migel[:ean].to_i,
:pharmacode => migel[:pharmacode],
:stat_date => '',
:desc_de => migel[:desc_de],
:desc_fr => migel[:desc_fr],
:atc_code => '',
:quantity => migel[:quantity],
:company_ean => migel[:company_ean],
:company_name => migel[:company_name],
:migel => true,
}
@articles << entry
end
nrAdded = 0
if @options[:extended]
Oddb2xml.log("prepare_articles extended prepare_local_index having already #{@articles.size} articles")
nrItems = 0
@infos_zur_rose.each{
|ean13, info|
nrItems += 1
pharmacode = info[:pharmacode]
if @pharmacode[pharmacode]
@pharmacode[pharmacode][:price] = info[:price]
@pharmacode[pharmacode][:pub_price] = info[:pub_price]
next
end
obj = {}
found = false
# existing = @refdata.values.find{ |x| x[:ean].eql? ean13 }
existing = @refdata[ean13]
if existing
found = true
existing[:price] = info[:price]
existing[:pub_price] = info[:pub_price]
else
entry = {
:desc => info[:description],
:desc_de => info[:description],
:status => info[:status] == '3' ? 'I' : 'A', # from ZurRose, we got 1,2 or 3 means aktive, aka available in trade
:atc_code => '',
:ean => ean13,
:pharmacode => pharmacode,
:price => info[:price],
:pub_price => info[:pub_price],
:type => info[:type],
}
if pharmacode
@refdata[pharmacode] = entry
else
@refdata[ean13] = entry
end
obj = entry
end
if not found and obj.size > 0
@articles << obj
nrAdded += 1
end
}
end
end
Oddb2xml.log("prepare_articles done. Added #{nrAdded} prices. Total #{@articles.size}")
end
def prepare_substances
unless @substances
Oddb2xml.log("prepare_substances from #{@items.size} items")
@substances = []
@items.values.uniq.each do |seq|
next unless seq[:substances]
seq[:substances].each do |sub|
@substances << sub[:name]
end
end
@substances.uniq!
@substances.sort!
Oddb2xml.log("prepare_substances done. Total #{@substances.size} from #{@items.size} items")
exit 2 if @options[:extended] and @substances.size == 0
end
end
def prepare_limitations
unless @limitations
Oddb2xml.log("prepare_limitations from #{@items.size} items")
limitations = []
@items.values.uniq.each do |seq|
next unless seq[:packages]
seq[:packages].each_value do |pac|
limitations += pac[:limitations]
end
end
# ID is no longer fixed TAG (swissmedicNo8, swissmedicNo5, pharmacode)
# limitation.xml needs all duplicate entries by this keys.
limitations.uniq! { |lim| lim[:id].to_s + lim[:code] + lim[:type] }
@limitations = limitations.sort_by {|lim| lim[:code] }
Oddb2xml.log("prepare_limitations done. Total #{@limitations.size} from #{@items.size} items")
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
@products = {}
@refdata.each_pair do |ean13, item|
next if item and item.is_a?(Hash) and item[:atc_code] and /^Q/i.match(item[:atc_code])
next if item[:prodno] and @products[item[:prodno]]
obj = {
:seq => @items[ean13] ? @items[ean13] : @items[item[:ean]],
:pac => nil,
:no8 => nil,
:ean => item[:ean],
:atc => item[:atc_code],
:ith => '',
:siz => '',
:eht => '',
:sub => '',
:comp => '',
}
if obj[:ean] # via EAN-Code
obj[:no8] = obj[:ean].to_s[4..11]
end
if obj[:no8] and ppac = @packs[obj[:no8].intern] and # Packungen.xls
!ppac[:is_tier]
# If RefData does not have EAN
if obj[:ean].nil?
obj[:ean] = ppac[:ean].to_i
end
# If RefData dose not have ATC-Code
if obj[:atc].nil? or obj[:atc].empty?
obj[:atc] = ppac[:atc_code].to_s
end
obj[:ith] = ppac[:ith_swissmedic]
obj[:siz] = ppac[:package_size]
obj[:eht] = ppac[:einheit_swissmedic]
obj[:sub] = ppac[:substance_swissmedic]
obj[:comp] = ppac[:composition_swissmedic]
end
if obj[:ean].to_s[0..3] == '7680'
if item[:prodno]
@products[item[:prodno]] = obj
else
@products[ean13] = obj
end
end
end
end
@products
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%z')
xml.SUBSTANCE(
XML_OPTIONS
) {
Oddb2xml.log "build_substance #{@substances.size} substances"
exit 2 if @options[:extended] and @substances.size == 0
@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
Oddb2xml.add_hash(_builder.to_xml)
end
def build_limitation
prepare_limitations
Oddb2xml.log "build_limitation #{@limitations.size} limitations"
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.LIMITATION(XML_OPTIONS) {
@limitations.each do |lim|
xml.LIM('DT' => '') {
case lim[:key]
when :swissmedic_number8
xml.SwissmedicNo8 lim[:id]
when :swissmedic_number5
xml.SwissmedicNo5 lim[:id]
when :pharmacode
xml.Pharmacode lim[:id]
end
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
Oddb2xml.add_hash(_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%z')
xml.INTERACTION(XML_OPTIONS) {
Oddb2xml.log "build_interaction #{@interactions.size} interactions"
@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 @codes and ix[:grad]
if dict = @codes[ix[:grad].upcase]
xml.RLV dict[:int]
xml.RLVD dict[:txt]
#xml.RLVF
end
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
Oddb2xml.add_hash(_builder.to_xml)
end
def build_code
prepare_codes
Oddb2xml.log "build_code #{@codes.size} codes"
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.CODE(XML_OPTIONS) {
@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
Oddb2xml.add_hash(_builder.to_xml)
end
def add_missing_products_from_swissmedic
Oddb2xml.log "build_product add_missing_products_from_swissmedic. Starting with #{@products.size} products and #{@packs.size} @packs"
ean13_to_product = {}
@products.each{
|ean13, obj|
ean13_to_product[ean13] = obj
}
ausgabe = File.open(File.join(WorkDir, 'missing_in_refdata.txt'), 'w+')
size_old = ean13_to_product.size
@missing = []
Oddb2xml.log "build_product add_missing_products_from_swissmedic. Imported #{size_old} ean13_to_product from @products. Checking #{@packs.size} @packs"
@packs.each_with_index {
|de_idx, i|
ean = de_idx[1][:ean].to_i
next if @refdata[ean]
list_code = de_idx[1][:list_code]
next if list_code and /Tierarzneimittel/.match(list_code)
ean13_to_product[ean] = de_idx[1]
@missing << de_idx[1]
ausgabe.puts "#{ean},#{de_idx[1][:sequence_name]}"
}
corrected_size = ean13_to_product.size
Oddb2xml.log "build_product add_missing_products_from_swissmedic. Added #{(corrected_size - size_old)} corrected_size #{corrected_size} size_old #{size_old} ean13_to_product."
end
def build_product
prepare_substances
prepare_products
prepare_interactions
prepare_codes
add_missing_products_from_swissmedic
Oddb2xml.log "build_product #{@products.size+@missing.size} products"
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
emitted = []
xml.PRODUCT(XML_OPTIONS) {
list = []
length = 0
@missing.each do |obj|
next if /^Q/i.match(obj[:atc])
if obj[:prodno]
next if emitted.index(obj[:prodno])
emitted << obj[:prodno]
end
length += 1
xml.PRD('DT' => obj[:last_change]) {
ean = obj[:ean].to_i
xml.GTIN ean
xml.PRODNO obj[:prodno] if obj[:prodno]
if refdata = @refdata[ean]
xml.DSCRD refdata[:desc_de]
xml.DSCRF refdata[:desc_fr]
else
xml.DSCRD obj[:sequence_name] if obj[:sequence_name]
xml.DSCRF obj[:sequence_name] if obj[:sequence_name]
end
xml.ATC obj[:atc_code] if obj[:atc_code] and !obj[:atc_code].empty?
xml.IT obj[:ith_swissmedic] if obj[:ith_swissmedic]
xml.CPT
xml.PackGrSwissmedic obj[:package_size] if obj[:package_size]
xml.EinheitSwissmedic obj[:einheit_swissmedic] if obj[:einheit_swissmedic]
xml.SubstanceSwissmedic obj[:substance_swissmedic] if obj[:substance_swissmedic]
xml.CompositionSwissmedic obj[:composition_swissmedic] if obj[:composition_swissmedic]
}
end
# @products.sort! { |a,b| a[:ean] <=> b[:ean] }
@products.each do |ean13, obj|
next if /^Q/i.match(obj[:atc])
seq = obj[:seq]
length += 1
xml.PRD('DT' => obj[:last_change]) {
ean = obj[:ean]
xml.GTIN ean
ppac = ((_ppac = @packs[ean.to_s[4..11].intern] and !_ppac[:is_tier]) ? _ppac : {})
unless ppac
ppac = @packs.find{|pac| pac.ean == ean }.first
end
xml.PRODNO ppac[:prodno] if ppac[:prodno] and !ppac[:prodno].empty?
if refdata = @refdata[ean]
xml.DSCRD refdata[:desc_de]
xml.DSCRF refdata[:desc_fr]
else
xml.DSCRD obj[:sequence_name] if obj[:sequence_name]
xml.DSCRF obj[:sequence_name] if obj[:sequence_name]
end
#xml.BNAMD
#xml.BNAMF
#xml.ADNAMD
#xml.ADNAMF
#xml.SIZE
if seq
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?
end
#xml.GENGRP
xml.ATC obj[:atc] unless obj[:atc].empty?
xml.IT obj[:ith] unless obj[:ith].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
obj[:no8] =~ /(\d{5})(\d{3})/
if @orphan.include?($1.to_s)
xml.ORPH true
end
#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
if seq
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
end
#xml.CPTROA {
#xml.SYSLOC
#xml.ROA
#}
}
#xml.PRDICD { # currently empty
#xml.ICD
#xml.RTYP
#xml.RSIG
#xml.REMD
#xml.REMF
#}
xml.PackGrSwissmedic obj[:siz] unless obj[:siz].empty?
xml.EinheitSwissmedic obj[:eht] unless obj[:eht].empty?
xml.SubstanceSwissmedic obj[:sub] unless obj[:sub].empty?
xml.CompositionSwissmedic obj[:comp] unless obj[:comp].empty?
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
Oddb2xml.add_hash(_builder.to_xml)
end
def build_calc
def emit_substance(xml, substance, emit_active=false)
xml.MORE_INFO substance.more_info if substance.more_info
xml.SUBSTANCE_NAME substance.name
xml.IS_ACTIVE_AGENT substance.is_active_agent if emit_active
if substance.dose
if substance.qty.is_a?(Float) or substance.qty.is_a?(Fixnum)
xml.QTY substance.qty
xml.UNIT substance.unit
else
xml.DOSE_TEXT substance.dose.to_s
end
end
if substance.chemical_substance
xml.CHEMICAL_SUBSTANCE {
emit_substance(xml, substance.chemical_substance)
}
end
if substance.salts and substance.salts.size > 0
xml.SALTS {
substance.salts.each { |salt|
xml.SALT {
emit_substance(xml, salt)
}
}
}
end
end
packungen_xlsx = File.join(Oddb2xml::WorkDir, "swissmedic_package.xlsx")
idx = 0
return unless File.exists?(packungen_xlsx)
workbook = RubyXL::Parser.parse(packungen_xlsx)
items = {}
row_nr = 0
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.ARTICLES(XML_OPTIONS) {
workbook.worksheets[0].each do |row|
row_nr += 1
next unless row and row.cells[0] and row.cells[0].value and row.cells[0].value.to_i > 0
iksnr = "%05i" % row.cells[0].value.to_i
seqnr = "%02d" % row.cells[1].value.to_i
if row_nr % 250 == 0
puts "#{Time.now}: At row #{row_nr} iksnr #{iksnr}";
$stdout.flush
end
ith = COLUMNS_JULY_2015.keys.index(:index_therapeuticus)
seq_name = COLUMNS_JULY_2015.keys.index(:name_base)
i_3 = COLUMNS_JULY_2015.keys.index(:ikscd)
p_1_2 = COLUMNS_JULY_2015.keys.index(:seqnr)
cat = COLUMNS_JULY_2015.keys.index(:ikscat)
siz = COLUMNS_JULY_2015.keys.index(:size)
atc = COLUMNS_JULY_2015.keys.index(:atc_class)
list_code = COLUMNS_JULY_2015.keys.index(:production_science)
unit = COLUMNS_JULY_2015.keys.index(:unit)
sub = COLUMNS_JULY_2015.keys.index(:substances)
comp = COLUMNS_JULY_2015.keys.index(:composition)
no8 = iksnr + sprintf('%03d',row.cells[i_3].value.to_i)
name = row.cells[seq_name] ? row.cells[seq_name].value : nil
atc_code = row.cells[atc] ? row.cells[atc].value : nil
list_code = row.cells[list_code] ? row.cells[list_code].value : nil
package_size = row.cells[siz] ? row.cells[siz].value : nil
unit = row.cells[unit] ? row.cells[unit].value : nil
active_substance = row.cells[sub] ? row.cells[sub].value : nil
composition = row.cells[comp] ? row.cells[comp].value : nil
# skip veterinary product
next if atc_code and /^Q/i.match(atc_code)
next if list_code and /Tierarzneimittel/.match(list_code)
begin
info = Calc.new(name, package_size, unit, active_substance, composition)
rescue
puts "#{Time.now}: #{row_nr} iksnr #{iksnr} rescue from Calc.new"
info = nil
end
ean12 = '7680' + no8
ean13 = (ean12 + Oddb2xml.calc_checksum(ean12))
items[ean13] = info
xml.ARTICLE {
xml.GTIN ean13
xml.NAME info.column_c
xml.PKG_SIZE info.pkg_size
xml.SELLING_UNITS info.selling_units
xml.MEASURE info.measure # Nur wenn Lösung wen Spalte M ml, Spritze
if info.galenic_form.is_a?(String)
xml.GALENIC_FORM info.galenic_form
xml.GALENIC_GROUP "Unknown"
else
xml.GALENIC_FORM info.galenic_form.description
xml.GALENIC_GROUP info.galenic_group ? info.galenic_group.description : "Unknown"
end
xml.COMPOSITIONS {
info.compositions.each { |composition|
xml.COMPOSITION {
xml.EXCIPIENS { emit_substance(xml, composition.excipiens) } if composition.excipiens
xml.LABEL composition.label if composition.label
xml.LABEL_DESCRIPTION composition.label_description if composition.label_description
xml.CORRESP composition.corresp if composition.corresp
if composition.substances and composition.substances.size > 0
xml.SUBSTANCES {
composition.substances.each { |substance| xml.SUBSTANCE { emit_substance(xml, substance, true) }}
}
end
}
}
}
} if info and info.compositions
end
}
end
csv_name = File.join(WorkDir, 'oddb_calc.csv')
CSV.open(csv_name, "w+", :col_sep => ';') do |csv|
csv << ['gtin'] + items.values.first.headers
items.each do |key, value|
if value and value.to_array
csv << [key] + value.to_array
else
puts "key #{key.inspect} WITHOUT #{value.inspect}"
end
end
end
Oddb2xml.add_hash(_builder.to_xml)
end
def build_article
prepare_limitations
prepare_articles
idx = 0
Oddb2xml.log "build_article #{idx} of #{@articles.size} articles"
_builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.doc.tag_suffix = @tag_suffix
datetime = Time.new.strftime('%FT%T%z')
xml.ARTICLE(XML_OPTIONS) {
@articles.sort! { |a,b| a[:ean] <=> b[:ean] }
@articles.each do |obj|
idx += 1
Oddb2xml.log "build_article #{idx} of #{@articles.size} articles" if idx % 500 == 0
item = @items[obj[:ean]]
pac,no8 = nil,obj[:ean].to_s[4..11] # BAG-XML(SL/LS)
pack_info = nil
pack_info = @packs[no8.intern] if no8 # info from Packungen.xlsx from swissmedic_info
ppac = nil # Packungen
ean = obj[:ean]
next if pack_info and /Tierarzneimittel/.match(pack_info[:list_code])
next if obj[:desc_de] and /ad us vet/i.match(obj[:desc_de])
pharma_code = obj[:pharmacode]
ean = 0 if sprintf('%013d', ean).match(/^000000/)
if obj[:seq]
pac = obj[:seq][:packages][obj[:pharmacode]]
pac = obj[:seq][:packages][ean] unless pac
else
pac = @items[ean][:packages][ean] if @items and ean and @items[ean] and @items[ean][:packages]
end
if no8
ppac = ((_ppac = pack_info and !_ppac[:is_tier]) ? _ppac : nil)
end
info_zur_rose = nil
if !@infos_zur_rose.empty? && ean && @infos_zur_rose[ean]
info_zur_rose = @infos_zur_rose[ean] # zurrose
end
xml.ART('DT' => obj[:last_change] ? obj[:last_change] : '') {
xml.REF_DATA (obj[:refdata] || @migel[pharma_code]) ? '1' : '0'
xml.PHAR sprintf('%07d', obj[:pharmacode]) if obj[:pharmacode]
#xml.GRPCD
#xml.CDS01
#xml.CDS02
if ppac
xml.SMCAT ppac[:swissmedic_category] unless ppac[:swissmedic_category].empty?
xml.GEN_PRODUCTION ppac[:gen_production] unless ppac[:gen_production].empty?
xml.INSULIN_CATEGORY ppac[:insulin_category] unless ppac[:insulin_category].empty?
xml.DRUG_INDEX ppac[:drug_index] unless ppac[:drug_index].empty?
end
if no8 and !no8.to_s.empty?
if ean and ean.to_s[0..3] == "7680"
xml.SMNO no8.to_s
end
end
if ppac
xml.PRODNO ppac[:prodno] if ppac[:prodno] and !ppac[:prodno].empty?
end
#xml.HOSPCD
#xml.CLINCD
#xml.ARTTYP
if info_zur_rose
xml.VAT info_zur_rose[:vat]
end
nincd = detect_nincd(obj)
(nincd and nincd == 13) ? xml.SALECD('A') : xml.SALECD( (info_zur_rose && info_zur_rose[:cmut] != '3') ? 'A' : 'I') # XML_OPTIONS
if pac and pac[:limitation_points]
#xml.INSLIM
xml.LIMPTS pac[:limitation_points] unless pac[:limitation_points].empty?
end
#xml.GRDFR
if no8 and !no8.empty? and
no8.to_s =~ /(\d{5})(\d{3})/
xml.COOL 1 if @fridge.include?($1.to_s)
end
#xml.TEMP
if ean
flag = (ppac && !ppac[:drug_index].empty?) ? true : false
# as same flag
xml.CDBG(flag ? 'Y' : 'N')
xml.BG(flag ? 'Y' : 'N')
end
#xml.EXP
if item and item[:substances] and substance = item[:substances].first
xml.QTY "#{substance[:quantity]}#{substance[:unit] ? ' ' + substance[:unit] : ''}"
end if false # TODO: get qty/unit from refdata name
xml.DSCRD obj[:desc_de] if obj[:desc_de] and not obj[:desc_de].empty?
xml.DSCRF obj[:desc_fr] if obj[:desc_fr] and not obj[:desc_fr].empty?
xml.DSCRF obj[:desc_de] if !obj[:desc_fr] or obj[:desc_fr].empty?
xml.SORTD obj[:desc_de].upcase if obj[:desc_de] and not obj[:desc_de].empty?
xml.SORTF obj[:desc_fr].upcase if obj[:desc_fr] and not obj[:desc_fr].empty?
xml.SORTF obj[:desc_de].upcase if !obj[:desc_fr] or obj[:desc_fr].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 obj[:stat_date]
xml.OUTSAL obj[:stat_date] if obj[:stat_date] and not obj[: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 obj[:company_ean] if obj[:company_ean] and obj[:company_ean].to_s.length > 1
#xml.ROLE
#xml.ARTNO1
#xml.ARTNO2
#xml.ARTNO3
}
xml.ARTBAR {
xml.CDTYP 'E13'
xml.BC /^9999|^0000|^0$/.match(ean.to_s) ? 0 : sprintf('%013d', ean)
xml.BCSTAT 'A' # P is alternative
#xml.PHAR2
} if ean
#xml.ARTCH {
#xml.PHAR2
#xml.CHTYPE
#xml.LINENO
#xml.NOUNITS
#}
if pac and pac[:prices]
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
if info_zur_rose
price = info_zur_rose[:price]
vdat = Time.parse(datetime).strftime("%d.%m.%Y")
xml.ARTPRI {
xml.VDAT vdat
xml.PTYP "ZURROSE"
xml.PRICE price
}
xml.ARTPRI {
xml.VDAT vdat
xml.PTYP "ZURROSEPUB"
xml.PRICE info_zur_rose[:pub_price]
}
xml.ARTPRI {
xml.VDAT vdat
xml.PTYP "RESELLERPUB"
xml.PRICE (price.to_f*(1 + (@options[:percent].to_f/100))).round_by(0.05).round(2)
} if @options[:percent] != nil
end
#xml.ARTMIG {
#xml.VDAT
#xml.MIGCD
#xml.LINENO
#}
#xml.ARTDAN {
#xml.CDTYP
#xml.LINENO
#xml.CDVAL
#}
#xml.ARTLIM {
# xml.LIMCD
#}
if nincd
xml.ARTINS {
#xml.VDAT
#xml.INCD
xml.NINCD nincd
}
end
}
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD @articles.length.to_s
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
Oddb2xml.log "build_article. Done #{idx} of #{@articles.size} articles"
Oddb2xml.add_hash(_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%z')
xml.KOMPENDIUM(XML_OPTIONS) {
length = 0
%w[de fr].each do |lang|
infos = @infos[lang].uniq {|i| i[:monid] }
length += infos.length
infos.each do |info|
xml.KMP(
'MONTYPE' => 'fi', # only
'LANG' => lang.upcase,
'DT' => ''
) {
unless info[:name].empty?
xml.name info[:name]
end
unless info[:owner].empty?
xml.owner info[:owner]
end
xml.monid info[:monid] unless info[:monid].empty?
xml.style { xml.cdata(info[:style]) } if info[:style]
xml.paragraph { xml.cdata(Nokogiri::HTML.fragment(info[:paragraph].to_html).to_html(:encoding => 'UTF-8')) } if info[:paragraph]
}
end
end
xml.RESULT {
xml.OK_ERROR 'OK'
xml.NBR_RECORD length
xml.ERROR_CODE ''
xml.MESSAGE ''
}
}
end
Oddb2xml.add_hash(_builder.to_xml)
end