Class: PapierkramApi::V1::Endpoints::Expense::Vouchers

Inherits:
Base
  • Object
show all
Defined in:
lib/papierkram_api/v1/endpoints/expense/vouchers.rb

Overview

This class is responsible for all the API calls related to expense vouchers.

Constant Summary collapse

ALLOWED_DIFFERENCE_REASONS =
%w[sonstige mahnung teilzahlung skonto sonstige schmaelerung].freeze

Instance Attribute Summary

Attributes inherited from Base

#client, #url_api_path

Instance Method Summary collapse

Methods inherited from Base

#http_delete, #http_get, #http_patch, #http_post, #http_put, #initialize, #remaining_quota

Constructor Details

This class inherits a constructor from PapierkramApi::V1::Endpoints::Base

Instance Method Details

#add_document_by(id:, path_to_file:, file_type:) ⇒ Object



119
120
121
122
123
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 119

def add_document_by(id:, path_to_file:, file_type:)
  doc = Faraday::Multipart::FilePart.new(path_to_file, file_type)

  http_post("#{@url_api_path}/expense/vouchers/#{id}/documents", { file: doc })
end

#all(page: 1, page_size: 100, order_by: nil, order_direction: nil, creditor_id: nil, project_id: nil, document_date_range_start: nil, document_date_range_end: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 19

def all(page: 1, # rubocop:disable Metrics/CyclomaticComplexity
        page_size: 100,
        order_by: nil,
        order_direction: nil,
        creditor_id: nil,
        project_id: nil,
        document_date_range_start: nil,
        document_date_range_end: nil)
  query = {
    page: page,
    page_size: page_size
  }
  query[:order_by] = order_by if order_by
  query[:order_direction] = order_direction if order_direction
  query[:creditor_id] = creditor_id if creditor_id
  query[:project_id] = project_id if project_id
  query[:document_date_range_start] = document_date_range_start if document_date_range_start
  if document_date_range_end && document_date_range_start
    query[:document_date_range_end] =
      document_date_range_end
  end

  http_get("#{@url_api_path}/expense/vouchers", query)
end

#archive_by(id:) ⇒ Object



133
134
135
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 133

def archive_by(id:)
  http_post("#{@url_api_path}/expense/vouchers/#{id}/archive")
end

#cancel_by(id:) ⇒ Object



141
142
143
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 141

def cancel_by(id:)
  http_post("#{@url_api_path}/expense/vouchers/#{id}/cancel")
end

#cancel_with_reverse_entry_by(id:) ⇒ Object



145
146
147
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 145

def cancel_with_reverse_entry_by(id:)
  http_post("#{@url_api_path}/expense/vouchers/#{id}/cancel_with_reverse_entry")
end

#create(name:, provenance:, line_items:, due_date: nil, document_date: nil, description: nil, entertainment_reason: nil, entertainment_persons: nil, flagged: nil, creditor_id: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength



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
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 44

def create( # rubocop:disable Metrics/ParameterLists,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
  name:,
  provenance:,
  line_items:,
  due_date: nil,
  document_date: nil,
  description: nil,
  entertainment_reason: nil,
  entertainment_persons: nil,
  flagged: nil,
  creditor_id: nil
)
  body = {}
  body[:name] = name
  body[:line_items] = line_items
  body[:provenance] = provenance

  body[:due_date] = due_date if due_date
  body[:document_date] = document_date if document_date
  body[:description] = description if description
  body[:entertainment_reason] = entertainment_reason if entertainment_reason
  body[:entertainment_persons] = entertainment_persons if entertainment_persons
  body[:flagged] = flagged if flagged
  body[:creditor] = { id: creditor_id } if creditor_id

  if entertainment_persons
    raise ArgumentError, 'entertainment_persons must be an array' unless entertainment_persons.is_a?(Array)

    unless entertainment_persons.all?(String)
      raise ArgumentError,
            'entertainment_persons must be an array of strings'
    end
  end

  if line_items
    raise ArgumentError, 'line_items must be an array' unless line_items.is_a?(Array)
    raise ArgumentError, 'line_items must be an array of hashes' unless line_items.all?(Hash)
  end

  http_post("#{@url_api_path}/expense/vouchers", body)
end

#delete_by(id:) ⇒ Object



129
130
131
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 129

def delete_by(id:)
  http_delete("#{@url_api_path}/expense/vouchers/#{id}")
end

#delete_document_by(id:, document_id:) ⇒ Object



125
126
127
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 125

def delete_document_by(id:, document_id:)
  http_delete("#{@url_api_path}/expense/vouchers/#{id}/documents/#{document_id}")
end

#find_by(id:, pdf: false) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 11

def find_by(id:, pdf: false)
  if pdf == true
    return get("#{@url_api_path}/expense/vouchers/#{id}/pdf", nil,
               { headers: { 'Content-Type' => 'application/pdf' } })
  end
  http_get("#{@url_api_path}/expense/vouchers/#{id}")
end

#mark_as_paid_by(id:, value: nil, payment_date: nil, banking_transaction_id: nil, difference_reason: nil) ⇒ Object



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
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 149

def mark_as_paid_by(
  id:,
  value: nil,
  payment_date: nil,
  banking_transaction_id: nil,
  difference_reason: nil
)
  unless value || banking_transaction_id
    raise ArgumentError, 'either value or banking_transaction_id must be given'
  end

  body = {}

  if value
    validate_value!(value)
    body[:value] = value
  end

  if difference_reason
    validate_difference_reason!(difference_reason)
    body[:difference_reason] = difference_reason
  end

  body[:payment_date] = payment_date if payment_date
  body[:banking_transaction] = { id: banking_transaction_id } if banking_transaction_id

  http_post("#{@url_api_path}/expense/vouchers/#{id}/pay", body)
end

#unarchive_by(id:) ⇒ Object



137
138
139
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 137

def unarchive_by(id:)
  http_post("#{@url_api_path}/expense/vouchers/#{id}/unarchive")
end

#update_by(id:, name: nil, due_date: nil, document_date: nil, description: nil, entertainment_reason: nil, entertainment_persons: nil, flagged: nil, provenance: nil, creditor_id: nil, line_items: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



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
# File 'lib/papierkram_api/v1/endpoints/expense/vouchers.rb', line 86

def update_by( # rubocop:disable Metrics/ParameterLists,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  id:,
  name: nil,
  due_date: nil,
  document_date: nil,
  description: nil,
  entertainment_reason: nil,
  entertainment_persons: nil,
  flagged: nil,
  provenance: nil,
  creditor_id: nil,
  line_items: nil
)
  body = {}
  body[:name] = name if name
  body[:due_date] = due_date if due_date
  body[:document_date] = document_date if document_date
  body[:description] = description if description
  body[:entertainment_reason] = entertainment_reason if entertainment_reason
  body[:entertainment_persons] = entertainment_persons if entertainment_persons
  body[:flagged] = flagged if flagged
  body[:provenance] = provenance if provenance
  body[:creditor] = { id: creditor_id } if creditor_id
  body[:line_items] = line_items if line_items

  if line_items
    raise ArgumentError, 'line_items must be an array' unless line_items.is_a?(Array)
    raise ArgumentError, 'line_items must be an array of hashes' unless line_items.all?(Hash)
  end

  http_put("#{@url_api_path}/expense/vouchers/#{id}", body)
end