Class: ODDB::YdimPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/ydim.rb

Defined Under Namespace

Classes: DebitorFacade

Constant Summary

ICONV =
Iconv.new 'ISO-8859-1//TRANSLIT//IGNORE', 'UTF-8'
SECONDS_IN_DAY =
60*60*24
SALUTATIONS =
{
  'salutation_m' =>  'Herr',  
  'salutation_f' =>  'Frau',  
}

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Method Summary (collapse)

Methods inherited from Plugin

#initialize, #l10n_sessions, #log_info, #recipients, #report, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

This class inherits a constructor from ODDB::Plugin

Instance Method Details

- (Object) create_debitor(facade)



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
# File 'src/plugin/ydim.rb', line 52

def create_debitor(facade)
  ydim_connect { |client|
    debitor = client.create_debitor
    debitor.name = facade.fullname || facade.company_name || facade.contact
    contact = facade.contact.to_s.dup
    salutation = contact.slice!(/^(Herr|Frau)\s+/u).to_s.strip
    name_first, name_last = contact.split(' ', 2)
    debitor.salutation = SALUTATIONS[facade.salutation] || salutation
    debitor.contact_firstname = facade.name_first || name_first
    debitor.contact = facade.name_last || name_last
    debitor.address_lines = facade.ydim_address_lines || []
    debitor.location = facade.ydim_location.to_s
    debitor.debitor_type = case facade
                           when ODDB::Hospital
                             'dt_hospital'
                           when ODDB::Company
                             if(ba = facade.business_area)
                               ba.gsub(/^ba/u, 'dt')
                             else
                               'dt_pharma'
                             end
                           else
                             'dt_info'
                           end
    debitor. = facade.invoice_email
    debitor.odba_store
    facade.ydim_id = debitor.unique_id
    debitor
  }
end

- (Object) debitor_id(facade)



82
83
84
85
86
87
88
89
90
91
92
93
# File 'src/plugin/ydim.rb', line 82

def debitor_id(facade)
  ## since not all users can be associated with a business-object,
  #  ydim_id needs to be a yus_preference. However, if a business-object 
  #  exists, it should take precedence.
  if(id = facade.ydim_id)
    id
  elsif(!facade.force_new_ydim_debitor && debitor = identify_debitor(facade))
    debitor.unique_id
  else
    create_debitor(facade).unique_id
  end
end

- (Object) identify_debitor(facade)



94
95
96
97
98
99
100
101
102
# File 'src/plugin/ydim.rb', line 94

def identify_debitor(facade)
  ydim_connect { |client|
    term = facade.fullname || facade.company_name || facade.contact || facade.invoice_email
    if(debitor = client.search_debitors(term).first)
      facade.ydim_id = debitor.unique_id
      debitor
    end
  }
end

- (Object) inject(invoice)



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'src/plugin/ydim.rb', line 103

def inject(invoice)
  if(id = invoice.ydim_id)
    ydim_connect { |client| client.invoice(id) }
  elsif( = invoice.yus_name)
    items = invoice.items.values
    ydim_inv = inject_from_items(invoice_date(items), , items,
                                invoice.currency || 'CHF')
    ydim_inv.payment_received = invoice.payment_received?
    ydim_inv.odba_store
    invoice.ydim_id = ydim_inv.unique_id
    invoice.odba_store
  end
end

- (Object) inject_from_items(date, email, items, currency = 'CHF', service_date = date)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'src/plugin/ydim.rb', line 116

def inject_from_items(date, , items, currency='CHF', service_date=date)
  facade = DebitorFacade.new(, @app)
  ydim_connect { |client|
    ydim_inv = client.create_invoice(debitor_id(facade))
    ydim_inv.description = invoice_description(items, service_date)
    ydim_inv.date = date
    ydim_inv.currency = currency
    ydim_inv.payment_period = 30
    item_data = sort_items(items).collect { |item| 
      if(sprintf('%1.2f', item.quantity) == "0.00")
        ydim_inv.precision = 3
      end
      data = {}
      item.ydim_data.each do |key, value|
        data.store key, latin1(value)
      end
      data[:text] = latin1 item_text(item)
      data[:unit] = latin1 data[:unit]
      data
    }
    client.add_items(ydim_inv.unique_id, item_data)
    ydim_inv
  }
end

- (Object) invoice_date(items)



140
141
142
143
144
# File 'src/plugin/ydim.rb', line 140

def invoice_date(items)
  times = items.collect { |item| item.time }
  time = times.max
  Date.new(time.year, time.month, time.day)
end

- (Object) invoice_description(items, date = @@today)



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
# File 'src/plugin/ydim.rb', line 145

def invoice_description(items, date=@@today)
  types = []
  times = []
  items.each { |item| 
    types.push(item.type) 
    times.push(item.time) 
  }
  time = times.min
  last = times.max
  year = time.year
  if(types.include?(:poweruser))
    sprintf("PowerUser %s - %s", time.strftime('%d.%m.%Y'),
            items.first.expiry_time.strftime('%d.%m.%Y'))
  elsif(types.include?(:csv_export))
    fmt = '%d.%m.%Y'
    timstr = time.strftime(fmt)
    expstr = last.strftime(fmt)
    if(timstr == expstr)
      timstr = time.strftime("%m/%Y")
    end
    sprintf("%i x CSV-Download %s", items.size, timstr)
  elsif(types.include?(:download))
    sprintf('%i x Download %s', items.size, time.strftime('%d.%m.%Y'))
  elsif(types.include?(:index))
    sprintf("Firmenverzeichnis %i/%i", year, year.next)
  elsif(types.include?(:lookandfeel))
    sprintf("Lookandfeel-Integration %i/%i", year, year.next)
  else
    year = date.year
    count = items.select { |item| item.type == :annual_fee }.size
    pointers = items.collect { |item| item.item_pointer }.compact.uniq
    if((ptr = pointers.first) && ptr.resolve(@app).is_a?(Registration))
      sprintf("%i x Fachinfo-Upload %i/%i", count, year, year.next)
    else
      sprintf("%i x Patinfo-Upload %i/%i", count, year, year.next)
    end
  end
end

- (Object) item_name(item)



183
184
185
186
187
188
189
190
191
192
# File 'src/plugin/ydim.rb', line 183

def item_name(item)
  name = ''
  if(data = item.data)
    name = data[:name].to_s.strip
  end
  if(name.empty? && (ptr = item.item_pointer))
    name = resolved_name(ptr).to_s.strip
  end
  name unless(name.empty?)
end

- (Object) item_text(item)



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'src/plugin/ydim.rb', line 193

def item_text(item)
  lines = [item.text, item_name(item)]
  if(data = item.data) 
    first_date = data[:first_valid_date]
    first_date ||= Date.new(item.time.year, item.time.month, item.time.day)
    last_date = data[:last_valid_date]
    days = data[:days]
    if(last_date && days)
      lines.push(sprintf("%s - %s", 
        first_date.strftime("%d.%m.%Y"), last_date.strftime("%d.%m.%Y")))
      lines.push(sprintf("%i Tage", days))
      if last_date > (first_date >> 12)
        annual_date = last_date << 12
        lines.push "Diese Rechnungsposition wird in der n\\344chsten Jahresrechnung _nicht_ vorkommen.\nDie n\\344chste Jahresrechnung wird am \#{annual_date.strftime '%d.%m.%Y'} versandt.\n"
      end
    end
  end
  lines.compact.join("\n")
end

- (Object) latin1(text)



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'src/plugin/ydim.rb', line 216

def latin1(text)
  if text.is_a?(String)
    String.new ICONV.iconv(text)
  elsif text.is_a?(Hash)
    res = {}
    text.each do |key, value|
      res.store latin1(key), latin1(value)
    end
    res
  else
    text
  end
rescue Iconv::IllegalSequence
  String.new text
end

- (Object) resolved_name(pointer)



231
232
233
234
# File 'src/plugin/ydim.rb', line 231

def resolved_name(pointer)
  pointer.resolve(@app).name
rescue StandardError
end

- (Object) send_invoice(ydim_invoice_id)



235
236
237
# File 'src/plugin/ydim.rb', line 235

def send_invoice(ydim_invoice_id)
  ydim_connect { |client| client.send_invoice(ydim_invoice_id) }
end

- (Object) sort_items(items)



238
239
240
241
242
243
244
245
# File 'src/plugin/ydim.rb', line 238

def sort_items(items)
  items.sort_by { |item|
    [ item_text(item),
      item.time.to_i / SECONDS_IN_DAY,
      (item.type == :activation) ? 0 : 1,
      item.type.to_s ]
  }
end

- (Object) ydim_connect(&block)



246
247
248
249
250
251
252
253
254
255
# File 'src/plugin/ydim.rb', line 246

def ydim_connect(&block)
  config = YDIM::Client::CONFIG
  server = DRbObject.new(nil, config.server_url)
  client = YDIM::Client.new(config)
  key = OpenSSL::PKey::DSA.new(File.read(config.private_key))
  client.(server, key)
  block.call(client)
ensure
  client.logout if(client)
end