<commit_before># Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# type: ignore[1]
"""Tax Processing Functions"""

from decimal import Decimal
from typing import List

from consts import FIRESTORE_COLLECTION, FIRESTORE_PROJECT_ID
from firestore_utils import get_all_data_from_firestore_collection

_FORM_1099DIV = "FORM_1099DIV"
_FORM_1099INT = "FORM_1099INT"
_FORM_1099MISC = "FORM_1099MISC"
_FORM_1099NEC = "FORM_1099NEC"
_FORM_W2 = "FORM_W2"

# Standard Deduction for Single Filing in 2020
_STANDARD_DEDUCTION = 12400


def get_stored_data() -> dict:
    """
    Get Previously Extracted and stored Data from Firestore
    Format:
    {
        document_type: {
            field_name: field_value
        }
    }
    """
    document_data = get_all_data_from_firestore_collection(
        project_id=FIRESTORE_PROJECT_ID, collection=FIRESTORE_COLLECTION
    )

    return document_data


def run_tax_pipeline() -> List[List]:
    """
    Get Document Data from Firestore and calculate tax values
    """

    document_data = get_stored_data()

    if not document_data:
        return []

    tax_values = calculate_tax_values(document_data)
    return tax_values


def calculate_tax_values(data: dict) -> List[List]:
    # pylint: disable=too-many-locals
    """
    Calculate tax values
    """

    form_1099div = data.get(_FORM_1099DIV)
    form_1099int = data.get(_FORM_1099INT)
    form_1099misc = data.get(_FORM_1099MISC)
    form_1099nec = data.get(_FORM_1099NEC)
    form_w2 = data.get(_FORM_W2)

    # Don't continue if no data provided
    if not any([form_1099div, form_1099int, form_1099misc, form_1099nec, form_w2]):
        return []

    # The W2 Parser wasn't giving the full name
    full_name = form_1099div.get("RecipientName", "")
    ssn = form_w2.get("SSN", "")
    address = form_w2.get("Address", "")

    # Wages, salaries, tips, etc
    line_1 = get_numerical_form_value(form_w2, "WagesTipsOtherCompensation")
    # Taxable interest
    line_2b = get_numerical_form_value(form_1099int, "InterestIncome")
    # Qualified dividends
    line_3a = get_numerical_form_value(form_1099div, "QualifiedDividends")
    # Ordinary dividends
    line_3b = get_numerical_form_value(form_1099div, "TotalOrdinaryDividends")

    # Not used for this demo<commit_msg>Fixed issue where Home Address wasn't showing in final tax calulation<commit_after># Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# type: ignore[1]
"""Tax Processing Functions"""

from decimal import Decimal
from typing import List

from consts import FIRESTORE_COLLECTION, FIRESTORE_PROJECT_ID
from firestore_utils import get_all_data_from_firestore_collection

_FORM_1099DIV = "FORM_1099DIV"
_FORM_1099INT = "FORM_1099INT"
_FORM_1099MISC = "FORM_1099MISC"
_FORM_1099NEC = "FORM_1099NEC"
_FORM_W2 = "FORM_W2"

# Standard Deduction for Single Filing in 2020
_STANDARD_DEDUCTION = 12400


def get_stored_data() -> dict:
    """
    Get Previously Extracted and stored Data from Firestore
    Format:
    {
        document_type: {
            field_name: field_value
        }
    }
    """
    document_data = get_all_data_from_firestore_collection(
        project_id=FIRESTORE_PROJECT_ID, collection=FIRESTORE_COLLECTION
    )

    return document_data


def run_tax_pipeline() -> List[List]:
    """
    Get Document Data from Firestore and calculate tax values
    """

    document_data = get_stored_data()

    if not document_data:
        return []

    tax_values = calculate_tax_values(document_data)
    return tax_values


def calculate_tax_values(data: dict) -> List[List]:
    # pylint: disable=too-many-locals
    """
    Calculate tax values
    """

    form_1099div = data.get(_FORM_1099DIV)
    form_1099int = data.get(_FORM_1099INT)
    form_1099misc = data.get(_FORM_1099MISC)
    form_1099nec = data.get(_FORM_1099NEC)
    form_w2 = data.get(_FORM_W2)

    # Don't continue if no data provided
    if not any([form_1099div, form_1099int, form_1099misc, form_1099nec, form_w2]):
        return []

    # The W2 Parser wasn't giving the full name
    full_name = form_1099div.get("RecipientName", "")
    ssn = form_w2.get("SSN", "")
    address = form_w2.get("EmployeeAddress", "")

    # Wages, salaries, tips, etc
    line_1 = get_numerical_form_value(form_w2, "WagesTipsOtherCompensation")
    # Taxable interest
    line_2b = get_numerical_form_value(form_1099int, "InterestIncome")
    # Qualified dividends
    line_3a = get_numerical_form_value(form_1099div, "QualifiedDividends")
    # Ordinary dividends
    line_3b = get_numerical_form_value(form_1099div, "TotalOrdinaryDividends")

    # Not used for this demo
{'fraction_non_alphanumeric': 0.08013384994716449, 'fraction_numerical': 0.04473406128918633, 'mean_word_length': 3.9859525899912205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5135835', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1823, 'n_words': 491}