mindee 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +2 -0
  4. data/.yardopts +1 -0
  5. data/CHANGELOG.md +36 -0
  6. data/README.md +29 -16
  7. data/bin/mindee.rb +23 -26
  8. data/docs/code_samples/bank_account_details_v1.txt +10 -5
  9. data/docs/code_samples/bank_account_details_v2.txt +19 -0
  10. data/docs/code_samples/bank_check_v1.txt +10 -5
  11. data/docs/code_samples/carte_vitale_v1.txt +10 -5
  12. data/docs/code_samples/custom_v1.txt +19 -10
  13. data/docs/code_samples/default.txt +10 -2
  14. data/docs/code_samples/expense_receipts_v4.txt +10 -5
  15. data/docs/code_samples/expense_receipts_v5.txt +11 -6
  16. data/docs/code_samples/financial_document_v1.txt +10 -5
  17. data/docs/code_samples/idcard_fr_v1.txt +10 -5
  18. data/docs/code_samples/invoice_splitter_v1_async.txt +66 -0
  19. data/docs/code_samples/invoices_v4.txt +10 -5
  20. data/docs/code_samples/license_plates_v1.txt +10 -5
  21. data/docs/code_samples/passport_v1.txt +10 -5
  22. data/docs/code_samples/proof_of_address_v1.txt +10 -5
  23. data/docs/ruby-api-builder.md +30 -31
  24. data/docs/ruby-getting-started.md +64 -23
  25. data/docs/ruby-invoice-ocr.md +70 -59
  26. data/docs/ruby-passport-ocr.md +49 -40
  27. data/docs/ruby-receipt-ocr.md +45 -32
  28. data/lib/mindee/client.rb +150 -148
  29. data/lib/mindee/geometry/min_max.rb +23 -0
  30. data/lib/mindee/geometry/point.rb +35 -0
  31. data/lib/mindee/geometry/polygon.rb +23 -0
  32. data/lib/mindee/geometry/quadrilateral.rb +45 -0
  33. data/lib/mindee/geometry/utils.rb +81 -0
  34. data/lib/mindee/geometry.rb +5 -116
  35. data/lib/mindee/http/endpoint.rb +123 -16
  36. data/lib/mindee/http.rb +3 -0
  37. data/lib/mindee/input/sources.rb +87 -73
  38. data/lib/mindee/parsing/common/api_response.rb +109 -0
  39. data/lib/mindee/parsing/common/document.rb +48 -0
  40. data/lib/mindee/parsing/common/error.rb +24 -0
  41. data/lib/mindee/parsing/common/inference.rb +43 -0
  42. data/lib/mindee/parsing/common/ocr/mvision_v1.rb +34 -0
  43. data/lib/mindee/parsing/common/ocr/ocr.rb +169 -0
  44. data/lib/mindee/parsing/common/ocr.rb +3 -0
  45. data/lib/mindee/parsing/common/orientation.rb +26 -0
  46. data/lib/mindee/parsing/common/page.rb +40 -0
  47. data/lib/mindee/parsing/common/prediction.rb +15 -0
  48. data/lib/mindee/parsing/common/product.rb +19 -0
  49. data/lib/mindee/parsing/common.rb +10 -0
  50. data/lib/mindee/parsing/custom/classification_field.rb +28 -0
  51. data/lib/mindee/parsing/custom/list_field.rb +76 -0
  52. data/lib/mindee/parsing/custom.rb +4 -0
  53. data/lib/mindee/parsing/standard/amount_field.rb +26 -0
  54. data/lib/mindee/parsing/standard/base_field.rb +104 -0
  55. data/lib/mindee/parsing/standard/classification_field.rb +16 -0
  56. data/lib/mindee/parsing/standard/company_registration_field.rb +21 -0
  57. data/lib/mindee/parsing/standard/date_field.rb +34 -0
  58. data/lib/mindee/parsing/standard/locale_field.rb +50 -0
  59. data/lib/mindee/parsing/standard/payment_details_field.rb +42 -0
  60. data/lib/mindee/parsing/standard/position_field.rb +44 -0
  61. data/lib/mindee/parsing/standard/tax_field.rb +108 -0
  62. data/lib/mindee/parsing/standard/text_field.rb +16 -0
  63. data/lib/mindee/parsing/standard.rb +12 -0
  64. data/lib/mindee/parsing.rb +3 -2
  65. data/lib/mindee/{input → pdf}/pdf_processing.rb +4 -32
  66. data/lib/mindee/pdf/pdf_tools.rb +34 -0
  67. data/lib/mindee/pdf.rb +3 -0
  68. data/lib/mindee/product/.rubocop.yml +5 -0
  69. data/lib/mindee/product/custom/custom_v1.rb +35 -0
  70. data/lib/mindee/product/custom/custom_v1_document.rb +60 -0
  71. data/lib/mindee/product/custom/custom_v1_page.rb +32 -0
  72. data/lib/mindee/product/eu/license_plate/license_plate_v1.rb +38 -0
  73. data/lib/mindee/product/eu/license_plate/license_plate_v1_document.rb +37 -0
  74. data/lib/mindee/product/eu/license_plate/license_plate_v1_page.rb +34 -0
  75. data/lib/mindee/product/financial_document/financial_document_v1.rb +36 -0
  76. data/lib/mindee/product/financial_document/financial_document_v1_document.rb +188 -0
  77. data/lib/mindee/product/financial_document/financial_document_v1_line_item.rb +90 -0
  78. data/lib/mindee/product/financial_document/financial_document_v1_page.rb +32 -0
  79. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v1.rb +38 -0
  80. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v1_document.rb +43 -0
  81. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v1_page.rb +34 -0
  82. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2.rb +38 -0
  83. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2_bban.rb +71 -0
  84. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2_document.rb +58 -0
  85. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2_page.rb +34 -0
  86. data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1.rb +38 -0
  87. data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1_document.rb +52 -0
  88. data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1_page.rb +34 -0
  89. data/lib/mindee/product/fr/id_card/id_card_v1.rb +38 -0
  90. data/lib/mindee/product/fr/id_card/id_card_v1_document.rb +82 -0
  91. data/lib/mindee/product/fr/id_card/id_card_v1_page.rb +48 -0
  92. data/lib/mindee/product/invoice/invoice_v4.rb +37 -0
  93. data/lib/mindee/product/invoice/invoice_v4_document.rb +212 -0
  94. data/lib/mindee/product/invoice/invoice_v4_line_item.rb +66 -0
  95. data/lib/mindee/product/invoice/invoice_v4_page.rb +32 -0
  96. data/lib/mindee/product/invoice_splitter/invoice_splitter_v1.rb +36 -0
  97. data/lib/mindee/product/invoice_splitter/invoice_splitter_v1_document.rb +65 -0
  98. data/lib/mindee/product/invoice_splitter/invoice_splitter_v1_page.rb +32 -0
  99. data/lib/mindee/product/passport/passport_v1.rb +36 -0
  100. data/lib/mindee/{parsing/prediction/fr/id_card/id_card_v1.rb → product/passport/passport_v1_document.rb} +45 -45
  101. data/lib/mindee/product/passport/passport_v1_page.rb +32 -0
  102. data/lib/mindee/product/proof_of_address/proof_of_address_v1.rb +36 -0
  103. data/lib/mindee/product/proof_of_address/proof_of_address_v1_document.rb +83 -0
  104. data/lib/mindee/product/proof_of_address/proof_of_address_v1_page.rb +32 -0
  105. data/lib/mindee/product/receipt/receipt_v4.rb +36 -0
  106. data/lib/mindee/product/receipt/receipt_v4_document.rb +86 -0
  107. data/lib/mindee/product/receipt/receipt_v4_page.rb +32 -0
  108. data/lib/mindee/product/receipt/receipt_v5.rb +36 -0
  109. data/lib/mindee/product/receipt/receipt_v5_document.rb +138 -0
  110. data/lib/mindee/product/receipt/receipt_v5_line_item.rb +69 -0
  111. data/lib/mindee/product/receipt/receipt_v5_page.rb +32 -0
  112. data/lib/mindee/product/us/bank_check/bank_check_v1.rb +38 -0
  113. data/lib/mindee/product/us/bank_check/bank_check_v1_document.rb +73 -0
  114. data/lib/mindee/product/us/bank_check/bank_check_v1_page.rb +34 -0
  115. data/lib/mindee/product.rb +16 -0
  116. data/lib/mindee/version.rb +2 -1
  117. data/lib/mindee.rb +3 -1
  118. metadata +87 -38
  119. data/docs/code_samples/shipping_containers_v1.txt +0 -14
  120. data/lib/mindee/document_config.rb +0 -60
  121. data/lib/mindee/parsing/document.rb +0 -31
  122. data/lib/mindee/parsing/error.rb +0 -22
  123. data/lib/mindee/parsing/inference.rb +0 -53
  124. data/lib/mindee/parsing/page.rb +0 -46
  125. data/lib/mindee/parsing/prediction/base.rb +0 -30
  126. data/lib/mindee/parsing/prediction/common_fields/amount.rb +0 -21
  127. data/lib/mindee/parsing/prediction/common_fields/base.rb +0 -72
  128. data/lib/mindee/parsing/prediction/common_fields/company_registration.rb +0 -17
  129. data/lib/mindee/parsing/prediction/common_fields/date.rb +0 -30
  130. data/lib/mindee/parsing/prediction/common_fields/locale.rb +0 -45
  131. data/lib/mindee/parsing/prediction/common_fields/payment_details.rb +0 -33
  132. data/lib/mindee/parsing/prediction/common_fields/position.rb +0 -39
  133. data/lib/mindee/parsing/prediction/common_fields/tax.rb +0 -40
  134. data/lib/mindee/parsing/prediction/common_fields/text.rb +0 -12
  135. data/lib/mindee/parsing/prediction/common_fields.rb +0 -11
  136. data/lib/mindee/parsing/prediction/custom/custom_v1.rb +0 -58
  137. data/lib/mindee/parsing/prediction/custom/fields.rb +0 -91
  138. data/lib/mindee/parsing/prediction/eu/license_plate/license_plate_v1.rb +0 -34
  139. data/lib/mindee/parsing/prediction/financial_document/financial_document_v1.rb +0 -237
  140. data/lib/mindee/parsing/prediction/financial_document/financial_document_v1_line_item.rb +0 -58
  141. data/lib/mindee/parsing/prediction/fr/bank_account_details/bank_account_details_v1.rb +0 -40
  142. data/lib/mindee/parsing/prediction/fr/carte_vitale/carte_vitale_v1.rb +0 -49
  143. data/lib/mindee/parsing/prediction/invoice/invoice_v4.rb +0 -212
  144. data/lib/mindee/parsing/prediction/invoice/invoice_v4_line_item.rb +0 -58
  145. data/lib/mindee/parsing/prediction/passport/passport_v1.rb +0 -121
  146. data/lib/mindee/parsing/prediction/proof_of_address/proof_of_address_v1.rb +0 -80
  147. data/lib/mindee/parsing/prediction/receipt/receipt_v4.rb +0 -87
  148. data/lib/mindee/parsing/prediction/receipt/receipt_v5.rb +0 -136
  149. data/lib/mindee/parsing/prediction/receipt/receipt_v5_line_item.rb +0 -37
  150. data/lib/mindee/parsing/prediction/shipping_container/shipping_container_v1.rb +0 -38
  151. data/lib/mindee/parsing/prediction/us/bank_check/bank_check_v1.rb +0 -70
  152. data/lib/mindee/parsing/prediction.rb +0 -15
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../parsing'
4
+
5
+ module Mindee
6
+ module Product
7
+ module FR
8
+ module CarteVitale
9
+ # Carte Vitale V1 document prediction.
10
+ class CarteVitaleV1Document < Mindee::Parsing::Common::Prediction
11
+ include Mindee::Parsing::Standard
12
+ # The given name(s) of the card holder.
13
+ # @return [Array<Mindee::Parsing::Standard::TextField>]
14
+ attr_reader :given_names
15
+ # The date the card was issued.
16
+ # @return [Mindee::Parsing::Standard::DateField]
17
+ attr_reader :issuance_date
18
+ # The Social Security Number (Numéro de Sécurité Sociale) of the card holder
19
+ # @return [Mindee::Parsing::Standard::TextField]
20
+ attr_reader :social_security
21
+ # The surname of the card holder.
22
+ # @return [Mindee::Parsing::Standard::TextField]
23
+ attr_reader :surname
24
+
25
+ # @param prediction [Hash]
26
+ # @param page_id [Integer, nil]
27
+ def initialize(prediction, page_id)
28
+ super()
29
+ @given_names = []
30
+ prediction['given_names'].each do |item|
31
+ @given_names.push(TextField.new(item, page_id))
32
+ end
33
+ @issuance_date = DateField.new(prediction['issuance_date'], page_id)
34
+ @social_security = TextField.new(prediction['social_security'], page_id)
35
+ @surname = TextField.new(prediction['surname'], page_id)
36
+ end
37
+
38
+ # @return [String]
39
+ def to_s
40
+ given_names = @given_names.join("\n #{' ' * 15}")
41
+ out_str = String.new
42
+ out_str << "\n:Given Name(s): #{given_names}".rstrip
43
+ out_str << "\n:Surname: #{@surname}".rstrip
44
+ out_str << "\n:Social Security Number: #{@social_security}".rstrip
45
+ out_str << "\n:Issuance Date: #{@issuance_date}".rstrip
46
+ out_str[1..].to_s
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../parsing'
4
+ require_relative 'carte_vitale_v1_document'
5
+
6
+ module Mindee
7
+ module Product
8
+ module FR
9
+ module CarteVitale
10
+ # Carte Vitale V1 page.
11
+ class CarteVitaleV1Page < Mindee::Parsing::Common::Page
12
+ # @param prediction [Hash]
13
+ def initialize(prediction)
14
+ super(prediction)
15
+ @prediction = CarteVitaleV1PagePrediction.new(
16
+ prediction['prediction'],
17
+ prediction['id']
18
+ )
19
+ end
20
+ end
21
+
22
+ # Carte Vitale V1 page prediction.
23
+ class CarteVitaleV1PagePrediction < CarteVitaleV1Document
24
+ # @return [String]
25
+ def to_s
26
+ out_str = String.new
27
+ out_str << "\n#{super}"
28
+ out_str
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../parsing'
4
+ require_relative 'id_card_v1_document'
5
+ require_relative 'id_card_v1_page'
6
+
7
+ module Mindee
8
+ module Product
9
+ module FR
10
+ module IdCard
11
+ # Carte Nationale d'Identité V1 prediction inference.
12
+ class IdCardV1 < Mindee::Parsing::Common::Inference
13
+ @endpoint_name = 'idcard_fr'
14
+ @endpoint_version = '1'
15
+
16
+ # @param prediction [Hash]
17
+ def initialize(prediction)
18
+ super
19
+ @prediction = IdCardV1Document.new(prediction['prediction'], nil)
20
+ @pages = []
21
+ prediction['pages'].each do |page|
22
+ @pages.push(IdCardV1Page.new(page))
23
+ end
24
+ end
25
+
26
+ class << self
27
+ # Name of the endpoint for this product.
28
+ # @return [String]
29
+ attr_reader :endpoint_name
30
+ # Version for this product.
31
+ # @return [String]
32
+ attr_reader :endpoint_version
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../parsing'
4
+
5
+ module Mindee
6
+ module Product
7
+ module FR
8
+ module IdCard
9
+ # Carte Nationale d'Identité V1 document prediction.
10
+ class IdCardV1Document < Mindee::Parsing::Common::Prediction
11
+ include Mindee::Parsing::Standard
12
+ # The name of the issuing authority.
13
+ # @return [Mindee::Parsing::Standard::TextField]
14
+ attr_reader :authority
15
+ # The date of birth of the card holder.
16
+ # @return [Mindee::Parsing::Standard::DateField]
17
+ attr_reader :birth_date
18
+ # The place of birth of the card holder.
19
+ # @return [Mindee::Parsing::Standard::TextField]
20
+ attr_reader :birth_place
21
+ # The expiry date of the identification card.
22
+ # @return [Mindee::Parsing::Standard::DateField]
23
+ attr_reader :expiry_date
24
+ # The gender of the card holder.
25
+ # @return [Mindee::Parsing::Standard::TextField]
26
+ attr_reader :gender
27
+ # The given name(s) of the card holder.
28
+ # @return [Array<Mindee::Parsing::Standard::TextField>]
29
+ attr_reader :given_names
30
+ # The identification card number.
31
+ # @return [Mindee::Parsing::Standard::TextField]
32
+ attr_reader :id_number
33
+ # Machine Readable Zone, first line
34
+ # @return [Mindee::Parsing::Standard::TextField]
35
+ attr_reader :mrz1
36
+ # Machine Readable Zone, second line
37
+ # @return [Mindee::Parsing::Standard::TextField]
38
+ attr_reader :mrz2
39
+ # The surname of the card holder.
40
+ # @return [Mindee::Parsing::Standard::TextField]
41
+ attr_reader :surname
42
+
43
+ # @param prediction [Hash]
44
+ # @param page_id [Integer, nil]
45
+ def initialize(prediction, page_id)
46
+ super()
47
+ @authority = TextField.new(prediction['authority'], page_id)
48
+ @birth_date = DateField.new(prediction['birth_date'], page_id)
49
+ @birth_place = TextField.new(prediction['birth_place'], page_id)
50
+ @expiry_date = DateField.new(prediction['expiry_date'], page_id)
51
+ @gender = TextField.new(prediction['gender'], page_id)
52
+ @given_names = []
53
+ prediction['given_names'].each do |item|
54
+ @given_names.push(TextField.new(item, page_id))
55
+ end
56
+ @id_number = TextField.new(prediction['id_number'], page_id)
57
+ @mrz1 = TextField.new(prediction['mrz1'], page_id)
58
+ @mrz2 = TextField.new(prediction['mrz2'], page_id)
59
+ @surname = TextField.new(prediction['surname'], page_id)
60
+ end
61
+
62
+ # @return [String]
63
+ def to_s
64
+ given_names = @given_names.join("\n #{' ' * 15}")
65
+ out_str = String.new
66
+ out_str << "\n:Identity Number: #{@id_number}".rstrip
67
+ out_str << "\n:Given Name(s): #{given_names}".rstrip
68
+ out_str << "\n:Surname: #{@surname}".rstrip
69
+ out_str << "\n:Date of Birth: #{@birth_date}".rstrip
70
+ out_str << "\n:Place of Birth: #{@birth_place}".rstrip
71
+ out_str << "\n:Expiry Date: #{@expiry_date}".rstrip
72
+ out_str << "\n:Issuing Authority: #{@authority}".rstrip
73
+ out_str << "\n:Gender: #{@gender}".rstrip
74
+ out_str << "\n:MRZ Line 1: #{@mrz1}".rstrip
75
+ out_str << "\n:MRZ Line 2: #{@mrz2}".rstrip
76
+ out_str[1..].to_s
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../parsing'
4
+ require_relative 'id_card_v1_document'
5
+
6
+ module Mindee
7
+ module Product
8
+ module FR
9
+ module IdCard
10
+ # Carte Nationale d'Identité V1 page.
11
+ class IdCardV1Page < Mindee::Parsing::Common::Page
12
+ # @param prediction [Hash]
13
+ def initialize(prediction)
14
+ super(prediction)
15
+ @prediction = IdCardV1PagePrediction.new(
16
+ prediction['prediction'],
17
+ prediction['id']
18
+ )
19
+ end
20
+ end
21
+
22
+ # Carte Nationale d'Identité V1 page prediction.
23
+ class IdCardV1PagePrediction < IdCardV1Document
24
+ include Mindee::Parsing::Standard
25
+
26
+ # The side of the document which is visible.
27
+ # @return [Mindee::Parsing::Standard::ClassificationField]
28
+ attr_reader :document_side
29
+
30
+ # @param prediction [Hash]
31
+ # @param page_id [Integer, nil]
32
+ def initialize(prediction, page_id)
33
+ @document_side = ClassificationField.new(prediction['document_side'], page_id)
34
+ super(prediction, page_id)
35
+ end
36
+
37
+ # @return [String]
38
+ def to_s
39
+ out_str = String.new
40
+ out_str << "\n:Document Side: #{@document_side}".rstrip
41
+ out_str << "\n#{super}"
42
+ out_str
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../parsing'
4
+ require_relative 'invoice_v4_document'
5
+ require_relative 'invoice_v4_page'
6
+
7
+ module Mindee
8
+ module Product
9
+ module Invoice
10
+ # Invoice document.
11
+ class InvoiceV4 < Mindee::Parsing::Common::Inference
12
+ include Mindee::Parsing::Common
13
+ @endpoint_name = 'invoices'
14
+ @endpoint_version = '4'
15
+
16
+ # @param prediction [Hash]
17
+ def initialize(prediction)
18
+ super
19
+ @prediction = InvoiceV4Document.new(prediction['prediction'], nil)
20
+ @pages = []
21
+ prediction['pages'].each do |page|
22
+ @pages.push(InvoiceV4Page.new(page))
23
+ end
24
+ end
25
+
26
+ class << self
27
+ # Name of the endpoint for this product.
28
+ # @return [String]
29
+ attr_reader :endpoint_name
30
+ # Version for this product.
31
+ # @return [String]
32
+ attr_reader :endpoint_version
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,212 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../parsing'
4
+ require_relative 'invoice_v4_line_item'
5
+
6
+ module Mindee
7
+ module Product
8
+ module Invoice
9
+ # Invoice V4 document prediction.
10
+ class InvoiceV4Document < Mindee::Parsing::Common::Prediction
11
+ include Mindee::Parsing::Standard
12
+
13
+ # Locale information.
14
+ # @return [Mindee::Parsing::Standard::Locale]
15
+ attr_reader :locale
16
+ # The nature of the invoice.
17
+ # @return [Mindee::Parsing::Standard::ClassificationField]
18
+ attr_reader :document_type
19
+ # The total amount with tax included.
20
+ # @return [Mindee::Parsing::Standard::AmountField]
21
+ attr_reader :total_amount
22
+ # The total amount without the tax value.
23
+ # @return [Mindee::Parsing::Standard::AmountField]
24
+ attr_reader :total_net
25
+ # The total tax.
26
+ # @return [Mindee::Parsing::Standard::AmountField]
27
+ attr_reader :total_tax
28
+ # The creation date of the invoice.
29
+ # @return [Mindee::Parsing::Standard::DateField]
30
+ attr_reader :date
31
+ # The invoice number.
32
+ # @return [Mindee::Parsing::Standard::TextField]
33
+ attr_reader :invoice_number
34
+ # List of Reference numbers including PO number.
35
+ # @return [Mindee::Parsing::Standard::TextField]
36
+ attr_reader :reference_numbers
37
+ # The due date of the invoice.
38
+ # @return [Mindee::Parsing::Standard::DateField]
39
+ attr_reader :due_date
40
+ # The list of taxes.
41
+ # @return [Mindee::Parsing::Standard::Taxes]
42
+ attr_reader :taxes
43
+ # The name of the customer.
44
+ # @return [Mindee::Parsing::Standard::TextField]
45
+ attr_reader :customer_name
46
+ # The address of the customer.
47
+ # @return [Mindee::Parsing::Standard::TextField]
48
+ attr_reader :customer_address
49
+ # The company registration information for the customer.
50
+ # @return [Array<Mindee::Parsing::Standard::CompanyRegistration>]
51
+ attr_reader :customer_company_registrations
52
+ # The supplier's name.
53
+ # @return [Mindee::Parsing::Standard::TextField]
54
+ attr_reader :supplier_name
55
+ # The supplier's address.
56
+ # @return [Mindee::Parsing::Standard::TextField]
57
+ attr_reader :supplier_address
58
+ # The payment information.
59
+ # @return [Array<Mindee::Parsing::Standard::PaymentDetails>]
60
+ attr_reader :supplier_payment_details
61
+ # The supplier's company registration information.
62
+ # @return [Array<Mindee::Parsing::Standard::CompanyRegistration>]
63
+ attr_reader :supplier_company_registrations
64
+ # Line items details.
65
+ # @return [Array<Mindee::InvoiceV4LineItem>]
66
+ attr_reader :line_items
67
+
68
+ # @param prediction [Hash]
69
+ # @param page_id [Integer, nil]
70
+ def initialize(prediction, page_id)
71
+ super()
72
+ @locale = Locale.new(prediction['locale'])
73
+ @document_type = ClassificationField.new(prediction['document_type'], page_id)
74
+ @total_amount = AmountField.new(prediction['total_amount'], page_id)
75
+ @total_net = AmountField.new(prediction['total_net'], page_id)
76
+ @customer_address = TextField.new(prediction['customer_address'], page_id)
77
+ @customer_name = TextField.new(prediction['customer_name'], page_id)
78
+ @date = DateField.new(prediction['date'], page_id)
79
+ @due_date = DateField.new(prediction['due_date'], page_id)
80
+ @invoice_number = TextField.new(prediction['invoice_number'], page_id)
81
+ @supplier_name = TextField.new(prediction['supplier_name'], page_id)
82
+ @supplier_address = TextField.new(prediction['supplier_address'], page_id)
83
+ @reference_numbers = []
84
+ prediction['reference_numbers'].each do |item|
85
+ @reference_numbers.push(TextField.new(item, page_id))
86
+ end
87
+ @customer_company_registrations = []
88
+ prediction['customer_company_registrations'].each do |item|
89
+ @customer_company_registrations.push(CompanyRegistration.new(item, page_id))
90
+ end
91
+ @taxes = Taxes.new(prediction['taxes'], page_id)
92
+ @supplier_payment_details = []
93
+ prediction['supplier_payment_details'].each do |item|
94
+ @supplier_payment_details.push(PaymentDetails.new(item, page_id))
95
+ end
96
+ @supplier_company_registrations = []
97
+ prediction['supplier_company_registrations'].each do |item|
98
+ @supplier_company_registrations.push(CompanyRegistration.new(item, page_id))
99
+ end
100
+ @total_tax = AmountField.new(
101
+ { value: nil, confidence: 0.0 }, page_id
102
+ )
103
+ @line_items = []
104
+ prediction['line_items'].each do |item|
105
+ @line_items.push(InvoiceV4LineItem.new(item, page_id))
106
+ end
107
+ reconstruct(page_id)
108
+ end
109
+
110
+ # @return [String]
111
+ def to_s
112
+ customer_company_registrations = @customer_company_registrations.map(&:value).join('; ')
113
+ supplier_payment_details = @supplier_payment_details.map(&:to_s).join("\n ")
114
+ supplier_company_registrations = @supplier_company_registrations.map(&:to_s).join('; ')
115
+ reference_numbers = @reference_numbers.map(&:to_s).join(', ')
116
+ out_str = String.new
117
+ out_str << "\n:Locale: #{@locale}".rstrip
118
+ out_str << "\n:Document type: #{@document_type}".rstrip
119
+ out_str << "\n:Invoice number: #{@invoice_number}".rstrip
120
+ out_str << "\n:Reference numbers: #{reference_numbers}".rstrip
121
+ out_str << "\n:Invoice date: #{@date}".rstrip
122
+ out_str << "\n:Invoice due date: #{@due_date}".rstrip
123
+ out_str << "\n:Supplier name: #{@supplier_name}".rstrip
124
+ out_str << "\n:Supplier address: #{@supplier_address}".rstrip
125
+ out_str << "\n:Supplier company registrations: #{supplier_company_registrations}".rstrip
126
+ out_str << "\n:Supplier payment details: #{supplier_payment_details}".rstrip
127
+ out_str << "\n:Customer name: #{@customer_name}".rstrip
128
+ out_str << "\n:Customer address: #{@customer_address}".rstrip
129
+ out_str << "\n:Customer company registrations: #{customer_company_registrations}".rstrip
130
+ out_str << "\n:Taxes:#{@taxes}".rstrip
131
+ out_str << "\n:Total net: #{@total_net}".rstrip
132
+ out_str << "\n:Total tax: #{@total_tax}".rstrip
133
+ out_str << "\n:Total amount: #{@total_amount}".rstrip
134
+ out_str << "\n:Line Items:"
135
+ out_str << line_items_to_s
136
+ out_str[1..].to_s
137
+ end
138
+
139
+ private
140
+
141
+ def line_item_separator(char)
142
+ " +#{char * 22}+#{char * 9}+#{char * 9}+#{char * 10}+#{char * 18}+#{char * 38}+"
143
+ end
144
+
145
+ def line_items_to_s
146
+ return '' if @line_items.empty?
147
+
148
+ line_items = @line_items.map(&:to_s).join("\n#{line_item_separator('-')}\n ")
149
+ out_str = String.new
150
+ out_str << "\n#{line_item_separator('-')}"
151
+ out_str << "\n | Code#{' ' * 17}| QTY | Price | Amount | Tax (Rate) | Description #{' ' * 25}|"
152
+ out_str << "\n#{line_item_separator('=')}"
153
+ out_str << "\n #{line_items}"
154
+ out_str << "\n#{line_item_separator('-')}"
155
+ end
156
+
157
+ def reconstruct(page_id)
158
+ construct_total_tax_from_taxes(page_id)
159
+ return unless page_id.nil?
160
+
161
+ construct_total_excl_from_tcc_and_taxes(page_id)
162
+ construct_total_incl_from_taxes_plus_excl(page_id)
163
+ construct_total_tax_from_totals(page_id)
164
+ end
165
+
166
+ def construct_total_excl_from_tcc_and_taxes(page_id)
167
+ return if @total_amount.value.nil? || taxes.empty? || !@total_net.value.nil?
168
+
169
+ total_excl = {
170
+ 'value' => @total_amount.value - @taxes.map(&:value).sum,
171
+ 'confidence' => TextField.array_confidence(@taxes) * @total_amount.confidence,
172
+ }
173
+ @total_net = AmountField.new(total_excl, page_id, reconstructed: true)
174
+ end
175
+
176
+ def construct_total_incl_from_taxes_plus_excl(page_id)
177
+ return if @total_net.value.nil? || @taxes.empty? || !@total_amount.value.nil?
178
+
179
+ total_incl = {
180
+ 'value' => @taxes.map(&:value).sum + @total_net.value,
181
+ 'confidence' => TextField.array_confidence(@taxes) * @total_net.confidence,
182
+ }
183
+ @total_amount = AmountField.new(total_incl, page_id, reconstructed: true)
184
+ end
185
+
186
+ def construct_total_tax_from_taxes(page_id)
187
+ return if @taxes.empty?
188
+
189
+ total_tax = {
190
+ 'value' => @taxes.map(&:value).sum,
191
+ 'confidence' => TextField.array_confidence(@taxes),
192
+ }
193
+ return unless total_tax['value'].positive?
194
+
195
+ @total_tax = AmountField.new(total_tax, page_id, reconstructed: true)
196
+ end
197
+
198
+ def construct_total_tax_from_totals(page_id)
199
+ return if !@total_tax.value.nil? || @total_amount.value.nil? || @total_net.value.nil?
200
+
201
+ total_tax = {
202
+ 'value' => @total_amount.value - @total_net.value,
203
+ 'confidence' => TextField.array_confidence(@taxes),
204
+ }
205
+ return unless total_tax['value'] >= 0
206
+
207
+ @total_tax = AmountField.new(total_tax, page_id, reconstructed: true)
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../parsing'
4
+
5
+ module Mindee
6
+ module Product
7
+ module Invoice
8
+ # Line items for Invoice V4
9
+ class InvoiceV4LineItem
10
+ include Mindee::Parsing::Common
11
+ include Mindee::Parsing::Standard
12
+ # @return [String] The product code referring to the item.
13
+ attr_reader :product_code
14
+ # @return [String]
15
+ attr_reader :description
16
+ # @return [Float]
17
+ attr_reader :quantity
18
+ # @return [Float]
19
+ attr_reader :unit_price
20
+ # @return [Float]
21
+ attr_reader :total_amount
22
+ # @return [Float] The item tax rate percentage.
23
+ attr_reader :tax_rate
24
+ # @return [Float]
25
+ attr_reader :tax_amount
26
+ # @return [Float]
27
+ attr_reader :confidence
28
+ # @return [Integer]
29
+ attr_reader :page_id
30
+ # @return [Mindee::Geometry::Quadrilateral]
31
+ attr_reader :bounding_box
32
+ # @return [Array<Mindee::Geometry::Polygon>]
33
+ attr_reader :polygon
34
+
35
+ # @param prediction [Hash]
36
+ # @param page_id [Integer, nil]
37
+ def initialize(prediction, page_id)
38
+ @product_code = prediction['product_code']
39
+ @quantity = prediction['quantity']
40
+ @unit_price = prediction['unit_price']
41
+ @total_amount = prediction['total_amount']
42
+ @tax_amount = prediction['tax_amount']
43
+ @tax_rate = prediction['tax_rate']
44
+ @description = prediction['description']
45
+ @page_id = page_id
46
+ end
47
+
48
+ # @return [String]
49
+ def to_s
50
+ tax = Field.float_to_string(@tax_amount)
51
+ tax << " (#{Field.float_to_string(@tax_rate)}%)" unless @tax_rate.nil?
52
+ description = @description.nil? ? '' : @description
53
+ description = "#{description[0..32]}..." if description.size > 35
54
+ out_str = String.new
55
+ out_str << format('| %- 20s', @product_code)
56
+ out_str << " #{format('| %- 7s', Field.float_to_string(@quantity))}"
57
+ out_str << " #{format('| %- 7s', Field.float_to_string(@unit_price))}"
58
+ out_str << " #{format('| %- 8s', Field.float_to_string(@total_amount))}"
59
+ out_str << " #{format('| %- 16s', tax)}"
60
+ out_str << " #{format('| %- 37s', description)}"
61
+ out_str << '|'
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../parsing'
4
+ require_relative 'invoice_v4_document'
5
+
6
+ module Mindee
7
+ module Product
8
+ module Invoice
9
+ # Invoice V4 page.
10
+ class InvoiceV4Page < Mindee::Parsing::Common::Page
11
+ # @param prediction [Hash]
12
+ def initialize(prediction)
13
+ super(prediction)
14
+ @prediction = InvoiceV4PagePrediction.new(
15
+ prediction['prediction'],
16
+ prediction['id']
17
+ )
18
+ end
19
+ end
20
+
21
+ # Invoice V4 page prediction.
22
+ class InvoiceV4PagePrediction < InvoiceV4Document
23
+ # @return [String]
24
+ def to_s
25
+ out_str = String.new
26
+ out_str << "\n#{super}"
27
+ out_str
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../parsing'
4
+ require_relative 'invoice_splitter_v1_document'
5
+ require_relative 'invoice_splitter_v1_page'
6
+
7
+ module Mindee
8
+ module Product
9
+ module InvoiceSplitter
10
+ # Invoice Splitter V1 prediction inference.
11
+ class InvoiceSplitterV1 < Mindee::Parsing::Common::Inference
12
+ @endpoint_name = 'invoice_splitter'
13
+ @endpoint_version = '1'
14
+
15
+ # @param prediction [Hash]
16
+ def initialize(prediction)
17
+ super
18
+ @prediction = InvoiceSplitterV1Document.new(prediction['prediction'], nil)
19
+ @pages = []
20
+ prediction['pages'].each do |page|
21
+ @pages.push(InvoiceSplitterV1Page.new(page))
22
+ end
23
+ end
24
+
25
+ class << self
26
+ # Name of the endpoint for this product.
27
+ # @return [String]
28
+ attr_reader :endpoint_name
29
+ # Version for this product.
30
+ # @return [String]
31
+ attr_reader :endpoint_version
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end