"""Factories for testing model.""" from decimal import Decimal from abacus_common_logic.test_utils.factories import ( BaseModelFactory, BaseModelFactoryWithTimestamps, ) from abacus_common_logic.utils.dates import current_timestamp import factory from payment.config import Config from payment.constants.constants import ( PAYEE_TYPES, PAYMENT_ALLOCATION_LEDGER_STATUSES, PAYMENT_ALLOCATION_STATUSES, PAYMENT_ALLOCATION_TYPES, REPORT_TYPE, REVENUE_TRANSACTION_TYPES, ) from payment.models.exchange_rate import ExchangeRate from payment.models.payment_allocation import PaymentAllocationFlowthrough from payment.models.payment_group import PaymentGroup from payment.models.payment_group_payment import PaymentGroupPayment from payment.models.payment_group_payment_account import PaymentGroupPaymentAccount from payment.models.payment_group_payment_account_detail import ( PaymentDebitCreditDataEntry, PaymentGroupPaymentAccountDetail, ) from payment.models.payment_group_payment_batch import PaymentGroupPaymentBatch from payment.models.payment_group_payment_batch_account import ( PaymentGroupPaymentBatchAccount, ) from payment.models.payment_minimum import PaymentMinimum from payment.models.reference_payment_type import ReferencePaymentType from payment.models.reference_tax_withholding import ReferenceTaxWithholding from payment.models.report_payment import ReportPayment from payment.models.report_payment_group_payment import ReportPaymentGroupPayment from payment.models.worksheet_account_contract_closing_balance import ( WorksheetAccountContractClosingBalance, ) from payment.models.worksheet_account_contract_payable_details import ( WorksheetAccountContractPayableDetails, ) from payment.models.worksheet_account_contract_taxable_revenue import ( WorksheetAccountContractTaxableRevenue, ) from payment.models.worksheet_payable_balance_after_tax import ( WorksheetPayableBalanceAfterTax, ) from payment.models.worksheet_payment_contract_advance import ( WorksheetPaymentContractAdvance, ) from payment.models.worksheet_payment_custom import WorksheetPaymentCustom from payment.models.worksheet_tax_correction import WorksheetTaxCorrection from payment.models.worksheet_tax_correction_vat import WorksheetTaxCorrectionVAT class PaymentGroupFactory(BaseModelFactoryWithTimestamps): """Factory to create Payment Groups for testing.""" class Meta: """Meta.""" model = PaymentGroup deleted_at = None deleted_by = None group_criteria = {} group_name = factory.Sequence(lambda n: f'Payment Group {n + 1}') is_reusable = False class PaymentGroupPaymentFactory(BaseModelFactoryWithTimestamps): """Factory to create Payment Group Payments for testing.""" class Meta: """Meta.""" model = PaymentGroupPayment deleted_at = None deleted_by = None payment_name = 'July 2020' payment_group = factory.SubFactory(PaymentGroupFactory) statement_period_id = 1 created_at = current_timestamp() class PaymentGroupPaymentAccountFactory(BaseModelFactoryWithTimestamps): """Factory to create payment_group_payment_account instances for testing.""" class Meta: """Meta.""" model = PaymentGroupPaymentAccount account_id = 1 contracts_payable = [ {'contract_id': 1, 'current_balance': '200.00', 'currency_code': 'USD'} ] currency_code = 'USD' current_balance = factory.sequence(lambda n: '200.%02d' % 0) deleted_at = None deleted_by = None tax_withholding = None balance_after_tax = factory.sequence(lambda n: '200.%02d' % 0) last_payment = factory.sequence(lambda n: '400.%02d' % 0) note = None payment_group_payment = factory.SubFactory(PaymentGroupPaymentFactory) payoneer_program_id = 100158970 prior_payment_group_payment_id = None current_statement_period_id = 1 last_statement_period_id = None class PaymentMinimumFactory(BaseModelFactoryWithTimestamps): """Factory to create payment_minimum models for testing.""" class Meta: """Meta definition for the factory.""" model = PaymentMinimum currency_code = 'USD' check_amount = factory.Sequence(lambda n: '35.%02d' % 0) wire_transfer_amount = factory.Sequence(lambda n: '35.%02d' % 0) western_union_amount = factory.Sequence(lambda n: '100.%02d' % 0) class PaymentGroupPaymentBatchFactory(BaseModelFactoryWithTimestamps): """Factory to create PaymentGroupPaymentBatch instances for testing.""" class Meta: """Factory meta definition.""" model = PaymentGroupPaymentBatch batch_num = 1 payment_group_payment = factory.SubFactory(PaymentGroupPaymentFactory) payoneer_program_id = 1001 class PaymentGroupPaymentBatchAccountFactory(BaseModelFactoryWithTimestamps): """Factory to create PaymentGroupPaymentBatchAccount instances for testing.""" class Meta: """Factory meta definition.""" model = PaymentGroupPaymentBatchAccount payment_group_payment_account = factory.SubFactory( PaymentGroupPaymentAccountFactory ) payment_group_payment_batch = factory.SubFactory(PaymentGroupPaymentBatchFactory) class ReferenceTaxWithholdingFactory(BaseModelFactoryWithTimestamps): """Factory to create ReferenceTaxWithholding instances for testing.""" class Meta: """Factory meta definition.""" model = ReferenceTaxWithholding country_of_withholding = 'GBR' country_of_tax_residence = 'IND' tax_rate = 15.00 is_resource_provisioned = False class ReportPaymentGroupPaymentFactory(BaseModelFactoryWithTimestamps): """Factory to create ReportPaymentGroupPayment instances for testing.""" class Meta: """Factory meta definition.""" model = ReportPaymentGroupPayment payment_group_payment = factory.SubFactory(PaymentGroupPaymentFactory) report_export_url = ( f's3://{Config.S3_PAYMENTS_BUCKET_NAME}/123-payment-name/summary/summary.tsv' ) report_type = REPORT_TYPE.SUMMARY class ReportPaymentFactory(BaseModelFactoryWithTimestamps): """Factory to create ReportPayment instances for testing.""" class Meta: """Factory meta definition.""" model = ReportPayment target_type = 'worksheet_payment_custom' target_id = 1 report_export_url = f's3://{Config.S3_PAYMENTS_BUCKET_NAME}/worksheet_payment_custom/approval/approval.pdf' report_type = REPORT_TYPE.APPROVAL created_at = current_timestamp() created_by = 'test_user' last_modified = current_timestamp() last_modified_by = 'test_user' class WorksheetPaymentContractAdvanceFactory(BaseModelFactoryWithTimestamps): """Factory to create WorksheetPaymentContractAdvance instances for testing.""" class Meta: """Factory meta definition.""" model = WorksheetPaymentContractAdvance contract_advance_id = 1 statement_period_id = 1 exchange_rate_statement_period_id = 1 payment_name = 'test payment' amount = Decimal('100.00') currency_code = 'USD' amount_payee_currency = Decimal('100.00') payee_currency_code = 'USD' exchange_rate = Decimal('0.79') withholding_tax_amount = Decimal('-10.00') vat_amount = Decimal('20.00') amount_after_withholding_and_vat = Decimal('110.00') withholding_tax_amount_payee_currency = Decimal('-10.00') vat_amount_payee_currency = Decimal('20.00') amount_after_withholding_and_vat_payee_currency = Decimal('110.00') us_source_income_rate = Decimal('50.00') salesforce_id = 'test_id' is_internal = True class ExchangeRateFactory(BaseModelFactoryWithTimestamps): """Factory to create ExchangeRate instances for testing.""" class Meta: """Factory meta definition.""" model = ExchangeRate statement_period_id = 1 from_currency_code = 'USD' to_currency_code = 'GBP' rate = Decimal('0.79') class WorksheetAccountContractClosingBalanceFactory(BaseModelFactoryWithTimestamps): """Factory to create WorksheetAccountContractClosingB... instances for testing.""" class Meta: """Factory meta definition.""" model = WorksheetAccountContractClosingBalance account_id = 1 contract_id = 1 reference_payment_entity_id = 1 abacus_event_id = 1 ledger_account_contract_id = 1 currency_code = 'USD' statement_period_id = 3 amount = Decimal('100.00') includes_tax_adjustments = False class WorksheetAccountContractTaxableRevenueFactory(BaseModelFactoryWithTimestamps): """Factory to create WorksheetAccountContractTaxableR... instances for testing.""" class Meta: """Factory meta definition.""" model = WorksheetAccountContractTaxableRevenue worksheet_account_contract_closing_balance_id = 1 account_id = 1 contract_id = 1 reference_payment_entity_id = 1 statement_period_id = 3 abacus_event_id = 1 currency_code = 'USD' amount = Decimal('100.00') is_us_revenue = False revenue_transaction_type = REVENUE_TRANSACTION_TYPES.CLOSING_BALANCE class WorksheetPayableBalanceAfterTaxFactory(BaseModelFactoryWithTimestamps): """Factory to create WorksheetPayableBalanceAfterTax instances for testing.""" class Meta: """Factory meta definition.""" model = WorksheetPayableBalanceAfterTax worksheet_account_contract_closing_balance_id = 1 contract_id = 1 account_id = 1 statement_period_id = 1 abacus_event_id = 1 payable_amount_pre_tax = Decimal('100.00') tax_withholding_amount = Decimal('10.00') vat_amount = Decimal('11.01') payable_amount_post_tax = Decimal('90.00') currency_code = 'USD' country_of_tax_residence = 'IND' country_of_tax_policy = 'USA' created_at = current_timestamp() created_by = 'test_user' last_modified = current_timestamp() last_modified_by = 'test_user' deleted_at = None deleted_by = None class PaymentGroupPaymentAccountDetailFactory(BaseModelFactoryWithTimestamps): """Factory to create payment_group_payment_account_detail instances for testing.""" class Meta: """Meta.""" model = PaymentGroupPaymentAccountDetail payment_group_payment_account = factory.SubFactory( PaymentGroupPaymentAccountFactory ) worksheet_account_contract_payable_after_tax_id = 1 account_id = 1 contract_id = 1 payable_amount_pre_tax = Decimal('100.01') tax_withholding_amount = Decimal('10.01') payable_amount_post_tax = Decimal('90.00') currency_code = 'USD' vat_amount = Decimal('11.01') class WorksheetAccountContractPayableDetailsFactory(BaseModelFactoryWithTimestamps): """Factory to create worksheet_account_contract_payable_details records.""" class Meta: """Meta.""" model = WorksheetAccountContractPayableDetails worksheet_account_contract_payable_after_tax = factory.SubFactory( WorksheetPayableBalanceAfterTaxFactory ) contract_id = 1 account_id = 1 statement_period_id = 1 target_table = 'test_table' target_id = 1 reference_target_table = 'test_table_2' reference_target_id = 1 payable_detail_type_id = 1 amount_payable = Decimal('10.00') currency = 'USD' notes = 'test note' created_by = 'test' last_modified_by = 'test' class PaymentDebitCreditDataEntryFactory(factory.Factory): """Factory for PaymentDebitCreditDataEntry.""" class Meta: """Meta config.""" model = PaymentDebitCreditDataEntry account_id = 1 contract_id = 1 currency_code = 'USD' amount = Decimal('10.01') worksheet_account_contract_payable_after_tax_id = 1 class WorksheetTaxCorrectionFactory(BaseModelFactoryWithTimestamps): """Factory for WorksheetTaxCorrection.""" class Meta: """Meta config.""" model = WorksheetTaxCorrection contract_id = 1 account_id = 1 correction_statement_period_id = 1 payable_detail_type_id = 1 amount = Decimal('20.00') currency_code = 'USD' note = 'test note' class WorksheetTaxCorrectionVATFactory(BaseModelFactoryWithTimestamps): """Factory for WorksheetTaxCorrectionVAT.""" class Meta: """Meta config.""" model = WorksheetTaxCorrectionVAT payable_detail_type_id = 1 contract_id = 1 account_id = 1 correction_statement_period_id = 1 base_amount_payee_currency = Decimal('20.00') payee_currency_code = 'USD' vat_category = 'closing_balance' vat_currency_code = 'USD' vat_rate = Decimal('20.00') vat_amount_payee_currency = Decimal('20.00') vat_amount_vat_currency = Decimal('20.00') net_amount_payee_currency = Decimal('20.00') note = 'test note' class ReferencePaymentTypeFactory(BaseModelFactory): """Factory to create ReferencePaymentType instances for testing.""" class Meta: """Factory meta definition.""" model = ReferencePaymentType payment_type = 'advance' payment_service = 'service' is_internal = False notes = None class WorksheetPaymentCustomFactory(BaseModelFactoryWithTimestamps): """Factory to create WorksheetPaymentCustom instances for testing.""" class Meta: """Factory meta definition.""" model = WorksheetPaymentCustom account_id = 1 contract_id = 1 currency_code = 'USD' amount = Decimal('100.00') withholding_tax_amount = Decimal('-10.00') withholding_tax_rate = Decimal('5.00') vat_amount = Decimal('20.00') vat_rate = Decimal('10.00') amount_after_withholding_and_vat = Decimal('110.00') activity_statement_period_id = 1 statement_period_id = 1 payment_name = 'test custom payment' created_by = 'test_user' last_modified_by = 'test_user' class PaymentSearchItemFactory(factory.DictFactory): """Factory to create payment search result item dicts for testing.""" _id = factory.Sequence(lambda n: str(n + 1)) account_id = factory.Sequence(lambda n: 10000 + n) account_name = factory.Sequence(lambda n: f'Example Inc {n}') contract_id = factory.Sequence(lambda n: 300000 + n) contract_name = factory.Sequence(lambda n: f'Contract {n}') currency = 'USD' error_message = None payment_created_date = None payment_id = factory.Sequence(lambda n: 1000 + n) payment_name = factory.Sequence(lambda n: f'payment_name_{n}') payment_release_date = None payment_status = 'successful' payment_type = 'balance_payment' post_tax_amount = '2256.27' pre_tax_amount = '3256.25' statement_period_id = factory.Sequence(lambda n: 2000 + n) statement_period_name = 'October 2025' sent_statement_period_id = factory.Sequence(lambda n: 3000 + n) sent_statement_period_name = 'November 2025' class PaymentAllocationFlowthroughFactory(BaseModelFactoryWithTimestamps): """Factory to create PaymentAllocationFlowthrough instances for testing.""" class Meta: """Factory meta definition.""" model = PaymentAllocationFlowthrough contract_id = 1 payee_type = PAYEE_TYPES.ACCOUNT_PAYEE payee_id = 1 statement_period_id = 1 payment_allocation_type = PAYMENT_ALLOCATION_TYPES.FLOWTHROUGH amount_to_payment = Decimal('1000.00') payment_status = PAYMENT_ALLOCATION_STATUSES.INIT payment_status_modified = factory.Faker('past_date') amount_to_ledger = Decimal('1000.00') ledger_status = PAYMENT_ALLOCATION_LEDGER_STATUSES.DEBITED ledger_status_modified = factory.Faker('past_date') currency_code = 'USD' description = 'Test flowthrough allocation'