"""Factories for testing models.""" from decimal import Decimal import factory from abacus_common_logic.test_utils.factories import ( BaseModelFactory, BaseModelFactoryWithTimestamps, ) from ledger.constants.constants import ( CORRECTION_TYPES, DISTRIBUTION_TYPES, VAT_CATEGORIES, ) from ledger.models.ledger_account import LedgerAccount from ledger.models.ledger_account_contract import LedgerAccountContract from ledger.models.ledger_account_contract_current_balance import ( LedgerAccountContractCurrentBalance, ) from ledger.models.ledger_account_current_balance import LedgerAccountCurrentBalance from ledger.models.ledger_accounting_run_balance import LedgerAccountingRunBalance from ledger.models.ledger_accounting_run_vat import LedgerAccountingRunVat from ledger.models.ledger_adjustment import LedgerAdjustment from ledger.models.ledger_adjustment_adjustment_detail import ( LedgerAdjustmentAdjustmentDetail, ) from ledger.models.ledger_adjustment_applied import LedgerAdjustmentApplied from ledger.models.ledger_adjustment_detail import LedgerAdjustmentDetail from ledger.models.ledger_adjustment_detail_applied import LedgerAdjustmentDetailApplied from ledger.models.ledger_contract_advance_applied import LedgerContractAdvanceApplied from ledger.models.ledger_contract_flowthrough import LedgerContractFlowthrough from ledger.models.ledger_contract_flowthrough_current_balance import ( LedgerContractFlowthroughCurrentBalance, ) from ledger.models.ledger_correction import LedgerCorrection from ledger.models.ledger_deposit import LedgerDeposit from ledger.models.ledger_reserve_release import LedgerReserveRelease from ledger.models.ledger_reserve_release_schedule import LedgerReserveReleaseSchedule from ledger.models.ledger_reserve_taken import LedgerReserveTaken from ledger.models.ledger_vat_summary import LedgerVatSummary from ledger.models.reference_adjustment_type import ReferenceAdjustmentType from ledger.models.reference_vat_rate import ReferenceVatRate class ReferenceAdjustmentTypeFactory(BaseModelFactory): """Factory to create ReferenceAdjustmentType instances for testing.""" class Meta: """Factory meta definition.""" model = ReferenceAdjustmentType reference_adjustment_type_id = factory.Sequence(lambda n: n + 1) type_name = 'Adjustment Type' oa_category_name = 'Adjustment Type Name' class LedgerAccountingRunBalanceFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerAccountingRunBalance instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAccountingRunBalance accounting_run_id = 1 abacus_event_id = 1 contract_id = 1 currency_code = 'USD' total_gross_revenue_amount = factory.Sequence(lambda n: '%.2f' % (200 * (n + 1))) total_net_revenue_amount = factory.Sequence(lambda n: '%.2f' % (150 * (n + 1))) mechanical_deduction_total = factory.Sequence(lambda n: '%.2f' % (40 * (n + 1))) mechanical_deduction_admin_fee_total = factory.Sequence( lambda n: '%.2f' % (10 * (n + 1)) ) @factory.lazy_attribute def adjusted_net_revenue(self): """Compute adjusted net revenue.""" mech_deduction = ( Decimal(self.mechanical_deduction_total) if self.mechanical_deduction_total else 0 ) mech_deduction_fee = ( Decimal(self.mechanical_deduction_admin_fee_total) if self.mechanical_deduction_admin_fee_total else 0 ) net_revenue = Decimal(self.total_net_revenue_amount) adjusted_net_revenue = net_revenue - mech_deduction - mech_deduction_fee return adjusted_net_revenue @factory.lazy_attribute def distribution_fee(self): """Compute distribution fee.""" gross_revenue = Decimal(self.total_gross_revenue_amount) net_revenue = Decimal(self.total_net_revenue_amount) return gross_revenue - net_revenue class LedgerAccountFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerAccount instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAccount currency_code = 'USD' currency_amount = factory.Sequence(lambda n: '123.%02d' % 0) current_balance = factory.Sequence(lambda n: '225123.%02d' % 0) abacus_event_id = 1 note = None account_id = 1 contract_id = 1 previous_balance = factory.Sequence(lambda n: '225000.%02d' % 0) class LedgerAccountCurrentBalanceFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerAccountCurrentBalance instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAccountCurrentBalance ledger_account = factory.SubFactory(LedgerAccountFactory) account_id = 1 class LedgerAccountContractFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerAccountContract instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAccountContract currency_code = 'USD' currency_amount = factory.Sequence(lambda n: '123.%02d' % 0) current_balance = factory.Sequence(lambda n: '225123.%02d' % 0) abacus_event_id = 1 note = None account_id = 1 contract_id = 1 previous_balance = factory.Sequence(lambda n: '225000.%02d' % 0) class LedgerAccountContractCurrentBalanceFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerAccountContractCurrentBalance instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAccountContractCurrentBalance ledger_account = factory.SubFactory(LedgerAccountContractFactory) account_id = 1 contract_id = 1 class ReferenceVatRateFactory(BaseModelFactoryWithTimestamps): """Factory to create ReferenceVatRate instances for testing.""" class Meta: """Factory meta definition.""" model = ReferenceVatRate vat_type = 'supplier' country_of_tax_residence = 'GBR' country_of_tax_policy = 'GBR' is_sba_signed = 1 is_tax_applicable = 1 is_vat_registered_in_country_of_tax_policy = 1 transaction_type = None tax_rate = 20.00 effective_date = '2002-05-10' class LedgerAccountingRunVatFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerAccountingRunVat instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAccountingRunVat accounting_run_id = 1 abacus_event_id = 1 contract_id = 1 currency_code = 'GBP' country_of_tax_residence = 'GBR' gross_revenue = factory.Sequence(lambda n: '%.2f' % (200 * (n + 1))) net_revenue = factory.Sequence(lambda n: '%.2f' % (180 * (n + 1))) distribution_fee = factory.Sequence(lambda n: '%.2f' % (2 * (n + 1))) gross_vat_rate = 20 distribution_vat_rate = 20 gross_vat = factory.Sequence(lambda n: '%.2f' % (20 * (n + 1))) distribution_vat = factory.Sequence(lambda n: '%.2f' % (0.2 * (n + 1))) adjusted_net_revenue = factory.Sequence(lambda n: '%.2f' % (175 * (n + 1))) exempt_reason = None class LedgerAdjustmentFactory(BaseModelFactory): """Factory to create LedgerAdjustment instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAdjustment abacus_event_id = 1 account_id = 1 contract_id = 1 activity_statement_period_id = 1 apply_to_statement_period_id = 2 reference_adjustment_type_id = 1 adjustment_amount = Decimal('125.42') adjustment_currency_code = 'USD' note = 'informative message' class LedgerAdjustmentAppliedFactory(BaseModelFactory): """Factory to create LedgerAdjustmentApplied instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAdjustmentApplied abacus_event_id = 1 account_id = 1 contract_id = 1 statement_period_id = 1 adjustment_currency_code = 'USD' adjustment_amount = Decimal('1500.00') adjustment_payee_currency_code = 'GPB' adjustment_amount_payee_currency = Decimal('1395.20') worksheet_adjustment_id = 1 ledger_adjustment = factory.SubFactory(LedgerAdjustmentFactory) class LedgerAdjustmentDetailFactory(BaseModelFactory): """Factory to create LedgerAdjustmentDetail instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAdjustmentDetail account_id = 1 contract_id = 1 activity_statement_period_id = 1 apply_to_statement_period_id = 2 reference_adjustment_type = factory.SubFactory(ReferenceAdjustmentTypeFactory) currency_code = 'USD' amount = Decimal('1500.00') upc = '555444333222111' distribution_type = DISTRIBUTION_TYPES.DIGITAL note = 'informative message' class LedgerAdjustmentDetailAppliedFactory(BaseModelFactory): """Factory to create LedgerAdjustmentDetailApplied instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAdjustmentDetailApplied ledger_adjustment_detail = factory.SubFactory(LedgerAdjustmentDetailFactory) adjustment_amount = Decimal('1500.00') adjustment_currency_code = 'USD' adjustment_amount_payee_currency = Decimal('1600.00') adjustment_payee_currency_code = 'GBP' worksheet_adjustment_id = 1 worksheet_adjustment_detail_id = 1 class LedgerAdjustmentAdjustmentDetailFactory(BaseModelFactory): """Factory to create LedgerAdjustmentAdjustmentDetail instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerAdjustmentAdjustmentDetail detail_count = 5 ledger_adjustment = factory.SubFactory(LedgerAdjustmentFactory) ledger_adjustment_detail = factory.SubFactory(LedgerAdjustmentDetailFactory) class LedgerDepositFactory(BaseModelFactory): """Factory to create LedgerDeposit instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerDeposit account_id = 1 abacus_event_id = 1 contract_id = 1 currency_code = 'USD' rounded_amount = '225123.44' remaining_amount = '0.0044' class LedgerReserveTakenFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerReserveTaken instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerReserveTaken abacus_event_id = 1 contract_reserve_id = 1 accounting_run_id = 1 statement_period_id = 1 currency_code = 'USD' gross_sales = '150.00' gross_returns = '-50.00' net_revenue = '104.00' reserve_amount = '-60.00' net_revenue_after_reserve = '44.00' class LedgerReserveReleaseScheduleFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerReserveReleaseSchedule instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerReserveReleaseSchedule abacus_event_id = 1 account_id = 1 taken_statement_period_id = 1 release_statement_period_id = 2 ledger_reserve_taken = factory.SubFactory(LedgerReserveTakenFactory) currency_code = ('USD',) installment_amount = '-1500.11' outstanding_amount_after_installment = '-2100.01' early_release_date = None class LedgerReserveReleaseFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerReserveRelease instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerReserveRelease abacus_event_id = 1 statement_period_id = 1 ledger_reserve_release_schedule = factory.SubFactory( LedgerReserveReleaseScheduleFactory ) installment_amount = '1500.11' class LedgerContractAdvanceAppliedFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerContractAdvanceApplied instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerContractAdvanceApplied abacus_event_id = 7 account_id = 1 contract_id = 1 contract_advance_id = 1 statement_period_id = 1 worksheet_payment_contract_advance_id = 1 advance_amount = Decimal('-150.00') advance_currency_code = 'USD' advance_amount_payee_currency = Decimal('-150.00') advance_payee_currency_code = 'USD' vat_amount = Decimal('-20.00') withholding_tax_amount = Decimal('10.00') amount_after_withholding_and_vat = Decimal('-140.00') vat_amount_payee_currency = Decimal('-20.00') withholding_tax_amount_payee_currency = Decimal('10.00') amount_after_withholding_and_vat_payee_currency = Decimal('-140.00') us_source_income_rate = Decimal('90.00') class LedgerCorrectionFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerCorrection instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerCorrection abacus_event_id = 10 worksheet_correction_id = 1 account_id = 1 contract_id = 1 statement_period_id = 1 correction_statement_period_id = 2 correction_type = CORRECTION_TYPES.ROYALTY_REVERSAL currency_code = 'USD' gross_revenue = '200.00' distribution_fee = '-50.00' net_revenue = '150.00' class LedgerVatSummaryFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerVatSummary instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerVatSummary statement_period_id = 1 activity_statement_period_id = 1 abacus_event_id = 13 account_id = 1 contract_id = 1 vat_category = VAT_CATEGORIES.CLOSING_BALANCE payee_currency_code = 'USD' vat_currency_code = 'GBP' base_amount_payee_currency = '100.00' vat_rate = '20.00' vat_amount_payee_currency = '80.00' vat_amount_vat_currency = '63.71' net_amount_payee_currency = '180.00' wht_rate = '10.00' wht_amount_payee_currency = '70.00' wht_amount_vat_currency = '53.71' abacus_exempt_reason = None description = 'Test description' class LedgerContractFlowthroughFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerContractFlowthrough instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerContractFlowthrough currency_code = 'USD' currency_amount = factory.Sequence(lambda n: '123.%02d' % 0) current_balance = factory.Sequence(lambda n: '225123.%02d' % 0) abacus_event_id = 1 note = None account_id = 1 contract_id = 1 previous_balance = factory.Sequence(lambda n: '225000.%02d' % 0) class LedgerContractFlowthroughCurrentBalanceFactory(BaseModelFactoryWithTimestamps): """Factory to create LedgerContractFlowthroughCurrentBalanceFactory instances for testing.""" class Meta: """Factory meta definition.""" model = LedgerContractFlowthroughCurrentBalance ledger_account = factory.SubFactory(LedgerContractFlowthroughFactory) account_id = 1 contract_id = 1