|
| 1 | + |
| 2 | +from odoo.tests.common import TransactionCase |
| 3 | +import time |
| 4 | + |
| 5 | + |
| 6 | +class TestWithholdingTax(TransactionCase): |
| 7 | + |
| 8 | + def setUp(self): |
| 9 | + super(TestWithholdingTax, self).setUp() |
| 10 | + |
| 11 | + # Accounts |
| 12 | + type_payable = self.env.ref('account.data_account_type_payable') |
| 13 | + type_receivable = self.env.ref('account.data_account_type_receivable') |
| 14 | + self.wt_account_payable = self.env['account.account'].create({ |
| 15 | + 'name': 'Debiti per ritenute da versare', |
| 16 | + 'code': 'WT_001', |
| 17 | + 'user_type_id': type_payable.id, |
| 18 | + 'reconcile': True, |
| 19 | + }) |
| 20 | + self.wt_account_receivable = self.env['account.account'].create({ |
| 21 | + 'name': 'Crediti per ritenute subite', |
| 22 | + 'code': 'WT_002', |
| 23 | + 'user_type_id': type_receivable.id, |
| 24 | + 'reconcile': True, |
| 25 | + }) |
| 26 | + |
| 27 | + # Journals |
| 28 | + self.journal_misc = self.env['account.journal'].search( |
| 29 | + [('type', '=', 'general')])[0] |
| 30 | + self.journal_bank = self.env['account.journal'].create( |
| 31 | + {'name': 'Bank', 'type': 'bank', 'code': 'BNK67'}) |
| 32 | + |
| 33 | + # Payments |
| 34 | + vals_payment = { |
| 35 | + 'name': "", |
| 36 | + 'line_ids': [(0, 0, {'value': 'balance', 'days': 15})] |
| 37 | + } |
| 38 | + self.payment_term_15 = self.env['account.payment.term'].create( |
| 39 | + vals_payment) |
| 40 | + |
| 41 | + # Withholding tax |
| 42 | + wt_vals = { |
| 43 | + 'name': 'Code 1040', |
| 44 | + 'code': '1040', |
| 45 | + 'certification': True, |
| 46 | + 'account_receivable_id': self.wt_account_receivable.id, |
| 47 | + 'account_payable_id': self.wt_account_payable.id, |
| 48 | + 'journal_id': self.journal_misc.id, |
| 49 | + 'payment_term': self.payment_term_15.id, |
| 50 | + 'rate_ids': [(0, 0, { |
| 51 | + 'tax': 20, |
| 52 | + 'base': 1, |
| 53 | + })] |
| 54 | + } |
| 55 | + self.wt1040 = self.env['withholding.tax'].create(wt_vals) |
| 56 | + |
| 57 | + # Supplier Invoice with WT |
| 58 | + invoice_vals = [ |
| 59 | + (0, 0, { |
| 60 | + 'quantity': 1.0, |
| 61 | + 'account_id': self.env['account.account'].search( |
| 62 | + [('user_type_id', '=', self.env.ref( |
| 63 | + 'account.data_account_type_expenses').id)], |
| 64 | + limit=1).id, |
| 65 | + 'name': 'Advice', |
| 66 | + 'price_unit': 1000.00, |
| 67 | + 'invoice_line_tax_wt_ids': [(6, 0, [self.wt1040.id])] |
| 68 | + })] |
| 69 | + self.invoice = self.env['account.invoice'].create({ |
| 70 | + 'name': "Test Supplier Invoice WT", |
| 71 | + 'journal_id': self.env['account.journal'].search( |
| 72 | + [('type', '=', 'purchase')])[0].id, |
| 73 | + 'partner_id': self.env.ref('base.res_partner_12').id, |
| 74 | + 'account_id': self.env['account.account'].search( |
| 75 | + [('user_type_id', '=', self.env.ref( |
| 76 | + 'account.data_account_type_receivable').id)], |
| 77 | + limit=1, order='id').id, |
| 78 | + 'invoice_line_ids': invoice_vals, |
| 79 | + }) |
| 80 | + self.invoice._onchange_invoice_line_wt_ids() |
| 81 | + self.invoice.action_invoice_open() |
| 82 | + |
| 83 | + def test_creation_withholding_tax(self): |
| 84 | + domain = [('name', '=', 'Code 1040')] |
| 85 | + wts = self.env['withholding.tax'].search(domain) |
| 86 | + self.assertEqual(len(wts), 1, msg="Withholding tax was not created") |
| 87 | + |
| 88 | + def test_invoice_values(self): |
| 89 | + self.assertEqual( |
| 90 | + self.invoice.withholding_tax_amount, 200, msg='Invoice WT amount') |
| 91 | + self.assertEqual( |
| 92 | + self.invoice.amount_net_pay, 800, msg='Invoice WT amount net pay') |
| 93 | + |
| 94 | + def test_creation_statement(self): |
| 95 | + domain = [('invoice_id', '=', self.invoice.id), |
| 96 | + ('withholding_tax_id', '=', self.wt1040.id)] |
| 97 | + wt_statement = self.env['withholding.tax.statement'].search(domain) |
| 98 | + self.assertEqual( |
| 99 | + len(wt_statement), 1, msg='WT statement was not created') |
| 100 | + self.assertEqual( |
| 101 | + wt_statement.base, 1000, msg='WT statement Base amount') |
| 102 | + self.assertEqual( |
| 103 | + wt_statement.amount, 0, msg='WT statement amount applied') |
| 104 | + self.assertEqual( |
| 105 | + wt_statement.amount_paid, 0, msg='WT statement Base paid') |
| 106 | + |
| 107 | + def test_invoice_payment(self): |
| 108 | + ctx = { |
| 109 | + 'active_model': 'account.invoice', |
| 110 | + 'active_ids': [self.invoice.id], |
| 111 | + } |
| 112 | + register_payments = self.env['account.register.payments']\ |
| 113 | + .with_context(ctx).create({ |
| 114 | + 'payment_date': time.strftime('%Y') + '-07-15', |
| 115 | + 'amount': 800, |
| 116 | + 'journal_id': self.journal_bank.id, |
| 117 | + 'payment_method_id': self.env.ref( |
| 118 | + "account.account_payment_method_manual_out").id, |
| 119 | + }) |
| 120 | + register_payments.create_payments() |
| 121 | + |
| 122 | + # WT payment generation |
| 123 | + self.assertEqual( |
| 124 | + len(self.invoice.payment_move_line_ids), 2, |
| 125 | + msg='Missing WT payment') |
| 126 | + |
| 127 | + # WT amount in account move |
| 128 | + wt_line = self.invoice.payment_move_line_ids.filtered( |
| 129 | + lambda r: r.account_id.id == self.wt_account_payable.id) |
| 130 | + self.assertEqual(wt_line.debit, 200, msg='WT amount payment') |
| 131 | + |
| 132 | + # WT aomunt applied in statement |
| 133 | + domain = [('invoice_id', '=', self.invoice.id), |
| 134 | + ('withholding_tax_id', '=', self.wt1040.id)] |
| 135 | + wt_statement = self.env['withholding.tax.statement'].search(domain) |
| 136 | + self.assertEqual(wt_statement.amount, 200) |
0 commit comments