|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../common_fields' |
| 4 | +require_relative '../base' |
| 5 | +require_relative 'receipt_v5_line_item' |
| 6 | + |
| 7 | +module Mindee |
| 8 | + module Prediction |
| 9 | + # Expense Receipt v5 prediction results. |
| 10 | + class ReceiptV5 < Prediction |
| 11 | + # The locale identifier in BCP 47 (RFC 5646) format: ISO language code, '-', ISO country code. |
| 12 | + # @return [Mindee::Locale] |
| 13 | + attr_reader :locale |
| 14 | + # The receipt category among predefined classes. |
| 15 | + # @return [Mindee::TextField] |
| 16 | + attr_reader :category |
| 17 | + # The receipt sub category among predefined classes for transport and food. |
| 18 | + # @return [Mindee::TextField] |
| 19 | + attr_reader :subcategory |
| 20 | + # Whether the document is an expense receipt or a credit card receipt. |
| 21 | + # @return [Mindee::TextField] |
| 22 | + attr_reader :document_type |
| 23 | + # The date the purchase was made. |
| 24 | + # @return [Mindee::DateField] |
| 25 | + attr_reader :date |
| 26 | + # Time of purchase with 24 hours formatting (HH:MM). |
| 27 | + # @return [Mindee::TextField] |
| 28 | + attr_reader :time |
| 29 | + # The total amount paid including taxes, discounts, fees, tips, and gratuity. |
| 30 | + # @return [Mindee::AmountField] |
| 31 | + attr_reader :total_amount |
| 32 | + # The total amount excluding taxes. |
| 33 | + # @return [Mindee::AmountField] |
| 34 | + attr_reader :total_net |
| 35 | + # The total amount of taxes. |
| 36 | + # @return [Mindee::AmountField] |
| 37 | + attr_reader :total_tax |
| 38 | + # The total amount of tip and gratuity. |
| 39 | + # @return [Mindee::AmountField] |
| 40 | + attr_reader :tip |
| 41 | + # List of tax lines information including: Amount, tax rate, tax base amount and tax code. |
| 42 | + # @return [Array<Mindee::TaxField>] |
| 43 | + attr_reader :taxes |
| 44 | + # The name of the supplier or merchant. |
| 45 | + # @return [Mindee::TextField] |
| 46 | + attr_reader :supplier_name |
| 47 | + # List of supplier company registrations or identifiers. |
| 48 | + # @return [Array<Mindee::CompanyRegistration>] |
| 49 | + attr_reader :supplier_company_registrations |
| 50 | + # The address of the supplier or merchant returned as a single string. |
| 51 | + # @return [Mindee::TextField] |
| 52 | + attr_reader :supplier_address |
| 53 | + # The Phone number of the supplier or merchant returned as a single string. |
| 54 | + # @return [Mindee::TextField] |
| 55 | + attr_reader :supplier_phone_number |
| 56 | + # Full extraction of lines, including: description, quantity, unit price and total. |
| 57 | + # @return [Array<Mindee::ReceiptV5LineItem>] |
| 58 | + attr_reader :line_items |
| 59 | + |
| 60 | + # @param prediction [Hash] |
| 61 | + # @param page_id [Integer, nil] |
| 62 | + def initialize(prediction, page_id) |
| 63 | + super |
| 64 | + @locale = Locale.new(prediction['locale'], page_id) |
| 65 | + @category = TextField.new(prediction['category'], page_id) |
| 66 | + @subcategory = TextField.new(prediction['subcategory'], page_id) |
| 67 | + @document_type = TextField.new(prediction['document_type'], page_id) |
| 68 | + @date = DateField.new(prediction['date'], page_id) |
| 69 | + @time = TextField.new(prediction['time'], page_id) |
| 70 | + @total_amount = AmountField.new(prediction['total_amount'], page_id) |
| 71 | + @total_net = AmountField.new(prediction['total_net'], page_id) |
| 72 | + @total_tax = AmountField.new(prediction['total_tax'], page_id) |
| 73 | + @tip = AmountField.new(prediction['tip'], page_id) |
| 74 | + @taxes = [] |
| 75 | + prediction['taxes'].each do |item| |
| 76 | + @taxes.push(TaxField.new(item, page_id)) |
| 77 | + end |
| 78 | + @supplier_name = TextField.new(prediction['supplier_name'], page_id) |
| 79 | + @supplier_company_registrations = [] |
| 80 | + prediction['supplier_company_registrations'].each do |item| |
| 81 | + @supplier_company_registrations.push(CompanyRegistration.new(item, page_id)) |
| 82 | + end |
| 83 | + @supplier_address = TextField.new(prediction['supplier_address'], page_id) |
| 84 | + @supplier_phone_number = TextField.new(prediction['supplier_phone_number'], page_id) |
| 85 | + @line_items = [] |
| 86 | + prediction['line_items'].each do |item| |
| 87 | + @line_items.push(ReceiptV5LineItem.new(item, page_id)) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + # @return String |
| 92 | + def to_s |
| 93 | + taxes = @taxes.join("\n #{' ' * 7}") |
| 94 | + supplier_company_registrations = @supplier_company_registrations.join("\n #{' ' * 32}") |
| 95 | + line_items = line_items_to_s |
| 96 | + out_str = String.new |
| 97 | + out_str << "\n:Expense Locale: #{@locale}".rstrip |
| 98 | + out_str << "\n:Expense Category: #{@category}".rstrip |
| 99 | + out_str << "\n:Expense Sub Category: #{@subcategory}".rstrip |
| 100 | + out_str << "\n:Document Type: #{@document_type}".rstrip |
| 101 | + out_str << "\n:Purchase Date: #{@date}".rstrip |
| 102 | + out_str << "\n:Purchase Time: #{@time}".rstrip |
| 103 | + out_str << "\n:Total Amount: #{@total_amount}".rstrip |
| 104 | + out_str << "\n:Total Excluding Taxes: #{@total_net}".rstrip |
| 105 | + out_str << "\n:Total Tax: #{@total_tax}".rstrip |
| 106 | + out_str << "\n:Tip and Gratuity: #{@tip}".rstrip |
| 107 | + out_str << "\n:Taxes: #{taxes}".rstrip |
| 108 | + out_str << "\n:Supplier Name: #{@supplier_name}".rstrip |
| 109 | + out_str << "\n:Supplier Company Registrations: #{supplier_company_registrations}".rstrip |
| 110 | + out_str << "\n:Supplier Address: #{@supplier_address}".rstrip |
| 111 | + out_str << "\n:Supplier Phone Number: #{@supplier_phone_number}".rstrip |
| 112 | + out_str << "\n:Line Items:" |
| 113 | + out_str << line_items |
| 114 | + out_str[1..].to_s |
| 115 | + end |
| 116 | + |
| 117 | + private |
| 118 | + |
| 119 | + def line_items_separator(char) |
| 120 | + " +#{char * 38}+#{char * 10}+#{char * 14}+#{char * 12}+" |
| 121 | + end |
| 122 | + |
| 123 | + def line_items_to_s |
| 124 | + return '' if @line_items.empty? |
| 125 | + |
| 126 | + line_items = @line_items.map(&:to_s).join("\n#{line_items_separator('-')}\n ") |
| 127 | + out_str = String.new |
| 128 | + out_str << "\n#{line_items_separator('-')}" |
| 129 | + out_str << "\n | Description #{' ' * 25}| Quantity | Total Amount | Unit Price |" |
| 130 | + out_str << "\n#{line_items_separator('=')}" |
| 131 | + out_str << "\n #{line_items}" |
| 132 | + out_str << "\n#{line_items_separator('-')}" |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments