Module: ODDB::Util::Ydim

Defined in:
lib/oddb/util/ydim.rb

Constant Summary collapse

DATE_FMT =
"%d.%m.%Y"
SALUTATIONS =
{
  'salutation_m'  =>  'Herr',  
  'salutation_f'  =>  'Frau',  
}

Class Method Summary collapse

Class Method Details

.connect(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/oddb/util/ydim.rb', line 18

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

.create_debitor(email) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/oddb/util/ydim.rb', line 28

def Ydim.create_debitor(email)
  connect { |client|
    keys = [ :salutation, :name_last, :name_first ]
    info = Util::Yus.get_preferences(email, keys)
    debitor = client.create_debitor
    debitor.salutation = SALUTATIONS[info[:salutation]]
    debitor.contact_firstname = info[:name_first]
    debitor.contact = info[:name_last]
    debitor.debitor_type = 'dt_info'
    debitor.email = email
    debitor.save
    debitor
  }
end

.debitor_id(email) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/oddb/util/ydim.rb', line 42

def Ydim.debitor_id(email)
  if(id = Util::Yus.get_preference(email, :ydim_id))
    id
  elsif(debitor = identify_debitor(email))
    debitor.unique_id
  else
    create_debitor(email).unique_id
  end
end

.identify_debitor(email) ⇒ Object



51
52
53
54
55
# File 'lib/oddb/util/ydim.rb', line 51

def Ydim.identify_debitor(email)
  connect { |client|
    client.search_debitors(email).first
  }
end

.inject(invoice, opts = {:payment_received => false}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/oddb/util/ydim.rb', line 56

def Ydim.inject(invoice, opts = {:payment_received => false})
  if(id = invoice.ydim_id)
    Ydim.connect { |client| client.invoice(id) }
  elsif(email = invoice.yus_name)
    ydim_inv = inject_from_items(invoice_date(invoice), email, invoice.items,
                                invoice.currency || 'EUR')
    ydim_inv.payment_received = opts[:payment_received]
    ydim_inv.save
    invoice.ydim_id = ydim_inv.unique_id
    invoice.save
    ydim_inv
  end
end

.inject_from_items(date, email, items, currency = 'EUR') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oddb/util/ydim.rb', line 69

def Ydim.inject_from_items(date, email, items, currency='EUR')
  connect { |client|
    debitor_id = debitor_id(email)
    Util::Yus.set_preference(email, :ydim_id, debitor_id)
    ydim_inv = client.create_invoice(debitor_id)
    ydim_inv.description = invoice_description(items)
    ydim_inv.date = date
    ydim_inv.currency = currency
    ydim_inv.payment_period = ODDB.config.payment_period
    item_data = sort_items(items).collect { |item| 
      if(sprintf('%1.2f', item.quantity) == "0.00")
        ydim_inv.precision = 3
      end
      item.ydim_data 
    }
    client.add_items(ydim_inv.unique_id, item_data)
    ydim_inv
  }
end

.invoice_date(invoice) ⇒ Object



88
89
90
# File 'lib/oddb/util/ydim.rb', line 88

def Ydim.invoice_date(invoice)
  Date.new(invoice.time.year, invoice.time.month, invoice.time.day)
end

.invoice_description(items) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/oddb/util/ydim.rb', line 91

def Ydim.invoice_description(items)
  types = []
  items.each { |item| types.push(item.type) }
  main = items.first
  if(types.include?(:poweruser))
    exp = main.expiry_time || main.time + (24 * 60 *60)
    sprintf("PowerUser %s %s - %s", ODDB.config.server_name, 
            main.time.strftime(DATE_FMT), exp.strftime(DATE_FMT))
  else
    sprintf("%s %s", ODDB.config.server_name, main.time.strftime(DATE_FMT))
  end
end

.sort_items(items) ⇒ Object



103
104
105
106
107
# File 'lib/oddb/util/ydim.rb', line 103

def Ydim.sort_items(items)
  items.sort_by { |item| 
    [item.time.to_i / (24 * 60 * 60), item.text, item.type.to_s]
  }
end