from decimal import Decimal import unittest from trytond.exceptions import UserError from trytond.modules.account_debit_note.invoice import Invoice from trytond.tests.test_tryton import ModuleTestCase class MockInvoice: untaxed_amount = Decimal('100.00') total_amount = Decimal('118.00') class MockLine: type = 'line' def __init__(self, quantity, unit_price): self.quantity = Decimal(quantity) self.unit_price = Decimal(unit_price) class MockTax: def __init__(self, base, amount): self.base = Decimal(base) self.amount = Decimal(amount) class MockDebitNote: def __init__(self, lines=(), taxes=()): self.lines = list(lines) self.taxes = list(taxes) class AccountDebitNoteTestCase(ModuleTestCase): 'Test Account Debit Note module' module = 'account_debit_note' class AccountDebitNoteLogicTestCase(unittest.TestCase): def test_full_factor(self): self.assertEqual( Invoice._debit_compute_factor( MockInvoice(), 'full', None, None, 'total'), Decimal('1')) def test_percent_factor_supports_user_and_internal_values(self): self.assertEqual( Invoice._debit_compute_factor( MockInvoice(), 'percent', Decimal('20'), None, 'total'), Decimal('0.2')) self.assertEqual( Invoice._debit_compute_factor( MockInvoice(), 'percent', Decimal('0.20'), None, 'total'), Decimal('0.20')) def test_amount_factor_uses_selected_basis(self): self.assertEqual( Invoice._debit_compute_factor( MockInvoice(), 'amount', None, Decimal('59'), 'total'), Decimal('0.5')) self.assertEqual( Invoice._debit_compute_factor( MockInvoice(), 'amount', None, Decimal('25'), 'untaxed'), Decimal('0.25')) def test_invalid_percent_is_rejected(self): with self.assertRaises(UserError): Invoice._debit_compute_factor( MockInvoice(), 'percent', Decimal('101'), None, 'total') def test_fixed_amount_rejects_multiple_invoices(self): with self.assertRaises(UserError): Invoice.debit( [MockInvoice(), MockInvoice()], debit_mode='amount', debit_amount=Decimal('10')) def test_debit_restores_negative_adjustment_line_sign(self): # Core _credit() changed the original quantity 2 to -2 while keeping # the original negative adjustment price. line = MockLine('-2', '-15') debit = MockDebitNote([line]) Invoice._debit_force_positive_sign(debit) self.assertEqual(line.quantity, Decimal('2')) self.assertEqual(line.unit_price, Decimal('-15')) def test_debit_restores_manual_tax_sign_without_abs(self): # Core _credit() negated the original base 100 and amount -18. tax = MockTax('-100', '18') debit = MockDebitNote(taxes=[tax]) Invoice._debit_force_positive_sign(debit) self.assertEqual(tax.base, Decimal('100')) self.assertEqual(tax.amount, Decimal('-18')) def test_partial_debit_preserves_relative_line_signs(self): positive = MockLine('2', '50') adjustment = MockLine('1', '-20') debit = MockDebitNote([positive, adjustment]) origin = MockInvoice() origin.untaxed_amount = Decimal('80') Invoice._debit_apply_factor(origin, debit, Decimal('0.5')) self.assertEqual(positive.quantity, Decimal('2')) self.assertEqual(positive.unit_price, Decimal('25.00000')) self.assertEqual(adjustment.quantity, Decimal('1')) self.assertEqual(adjustment.unit_price, Decimal('-10.00000')) del ModuleTestCase def suite(): from trytond.tests.test_tryton import suite return suite()