Class: ODDB::DownloadInvoicer

Inherits:
Invoicer show all
Defined in:
src/plugin/download_invoicer.rb

Constant Summary

Constants inherited from Invoicer

RECIPIENTS

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Method Summary (collapse)

Methods inherited from Invoicer

#create_invoice, #ensure_yus_user, #resend_invoice, #rp2fr, #send_invoice

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) filter_paid(items)



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

def filter_paid(items)
  items = items.sort_by { |item| item.time }

  range = items.first.time..items.last.time

  ## Vorgeschlagener Algorithmus
  # 1. alle invoices von app
  # 2. davon alle items die den typ :csv_export haben und im
  #    Zeitraum range liegen
  # 3. Annahme: item.time ist Eineindeutig
  times = []
  @app.invoices.each_value { |invoice|
    invoice.items.each_value { |item|
      if(item.type == :csv_export && range.include?(item.time))
        times.push(item.time)
      end
    }
  }
  
  # 5. Duplikate löschen
  result = []
  items.delete_if { |item| 
    times.include?(item.time)
  }
  items
end

- (Object) group_by_user(items)



48
49
50
51
52
53
# File 'src/plugin/download_invoicer.rb', line 48

def group_by_user(items)
  items.inject({}) { |groups, item|
    (groups[item.yus_name] ||= []).push(item)
    groups
  }
end

- (Object) recent_items(date)



54
55
56
57
58
59
60
61
62
63
64
# File 'src/plugin/download_invoicer.rb', line 54

def recent_items(date)
  slate = @app.slate(:download)
  all_items = slate.items.values
  time_start = Time.local(date.year, date.month)
  date_end = date >> 1
  time_end = Time.local(date_end.year, date_end.month)
  range = time_start...time_end
  all_items.select { |item|
    range.include?(item.time)
  }
end

- (Object) run(month = (@@today - 1))



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'src/plugin/download_invoicer.rb', line 8

def run(month = (@@today - 1))
  items = recent_items(month)
  unless(items.empty?)
    payable_items = filter_paid(items)
    groups = group_by_user(payable_items)
    groups.each { |, items|
      ## first send the invoice 
      ydim_id = send_invoice(month, , items) 
      ## then store it in the database
      create_invoice(, items, ydim_id)
    }
  end
  nil
end