|
1 | 1 | # Copyright 2015 Agile Business Group <http://www.agilebg.com> |
| 2 | +# Copyright 2022 Simone Rubino - TAKOBI |
2 | 3 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
3 | 4 |
|
4 | | -from odoo.tests.common import TransactionCase |
| 5 | +from odoo import fields |
| 6 | +from odoo.tests.common import TransactionCase, Form |
5 | 7 | from datetime import datetime, date |
6 | 8 | from dateutil.rrule import MONTHLY |
7 | 9 |
|
@@ -227,3 +229,135 @@ def test_vat_statement(self): |
227 | 229 | self.assertTrue(self.vat_statement.move_id) |
228 | 230 | self.assertEqual(self.vat_statement.move_id.amount, 122) |
229 | 231 | # TODO payment |
| 232 | + |
| 233 | + def _create_vendor_bill(self, partner, date_invoice, price_unit, tax): |
| 234 | + """ |
| 235 | + Create an open Vendor Bill for `partner` having date `date_invoice`. |
| 236 | + The Bill will also have a Line having Price `price_unit` and Tax `tax`. |
| 237 | + """ |
| 238 | + bill_model = self.invoice_model.with_context( |
| 239 | + type='in_invoice', |
| 240 | + ) |
| 241 | + bill_form = Form(bill_model) |
| 242 | + bill_form.partner_id = partner |
| 243 | + bill_form.date_invoice = date_invoice |
| 244 | + with bill_form.invoice_line_ids.new() as line: |
| 245 | + line.invoice_line_tax_ids.clear() |
| 246 | + line.invoice_line_tax_ids.add(tax) |
| 247 | + line.name = "Test Invoice Line" |
| 248 | + line.price_unit = price_unit |
| 249 | + bill = bill_form.save() |
| 250 | + bill.action_invoice_open() |
| 251 | + return bill |
| 252 | + |
| 253 | + def _get_statement(self, period, statement_date, accounts): |
| 254 | + """ |
| 255 | + Create a VAT Statement in date `statement_date` |
| 256 | + for Period `period` and Accounts `accounts`. |
| 257 | + """ |
| 258 | + # Create statement |
| 259 | + statement_form = Form(self.vat_statement_model) |
| 260 | + statement_form.journal_id = self.general_journal |
| 261 | + statement_form.authority_vat_account_id = self.vat_authority |
| 262 | + statement_form.payment_term_id = self.account_payment_term |
| 263 | + statement_form.date = statement_date |
| 264 | + statement_form.account_ids.clear() |
| 265 | + for account in accounts: |
| 266 | + statement_form.account_ids.add(account) |
| 267 | + statement = statement_form.save() |
| 268 | + |
| 269 | + # Add period |
| 270 | + add_period_model = self.env['add.period.to.vat.statement'] |
| 271 | + add_period_model = add_period_model \ |
| 272 | + .with_context( |
| 273 | + active_id=statement.id, |
| 274 | + active_model=statement._name, |
| 275 | + ) |
| 276 | + add_period_form = Form(add_period_model) |
| 277 | + add_period_form.period_id = period |
| 278 | + add_period = add_period_form.save() |
| 279 | + add_period.add_period() |
| 280 | + return statement |
| 281 | + |
| 282 | + def test_different_previous_vat_statements(self): |
| 283 | + """ |
| 284 | + Statements for different Accounts |
| 285 | + only count previous Amounts from Statements with the same Accounts. |
| 286 | + """ |
| 287 | + # Arrange: Create two different VAT Statements |
| 288 | + # selecting two different Accounts |
| 289 | + partner = self.env.ref('base.res_partner_4') |
| 290 | + tax = self.account_tax_22_credit |
| 291 | + tax_statement_account = tax.vat_statement_account_id |
| 292 | + last_year_bill = self._create_vendor_bill( |
| 293 | + partner, self.last_year_recent_date, |
| 294 | + 10, tax, |
| 295 | + ) |
| 296 | + self.assertEqual(last_year_bill.state, 'open') |
| 297 | + last_year_period = self.last_year_period |
| 298 | + last_year_statement = self._get_statement( |
| 299 | + last_year_period, |
| 300 | + self.last_year_date, |
| 301 | + tax_statement_account, |
| 302 | + ) |
| 303 | + self.assertTrue(last_year_statement) |
| 304 | + |
| 305 | + # Create another Bill and Statement |
| 306 | + other_tax_statement_account = tax_statement_account.copy() |
| 307 | + other_tax = tax.copy( |
| 308 | + default={ |
| 309 | + 'vat_statement_account_id': other_tax_statement_account.id, |
| 310 | + }, |
| 311 | + ) |
| 312 | + other_last_year_bill = self._create_vendor_bill( |
| 313 | + partner, self.last_year_recent_date, |
| 314 | + 20, other_tax, |
| 315 | + ) |
| 316 | + self.assertEqual(other_last_year_bill.state, 'open') |
| 317 | + last_year_period.type_id.allow_overlap = True |
| 318 | + other_last_year_period = last_year_period.copy( |
| 319 | + default={ |
| 320 | + 'name': "Test Other last Year Period", |
| 321 | + 'vat_statement_id': False, |
| 322 | + }, |
| 323 | + ) |
| 324 | + other_last_year_statement = self._get_statement( |
| 325 | + other_last_year_period, |
| 326 | + self.last_year_date, |
| 327 | + other_tax_statement_account, |
| 328 | + ) |
| 329 | + self.assertTrue(other_last_year_statement) |
| 330 | + |
| 331 | + # Act: Create this Year's Statements, |
| 332 | + # one for each Account used in previous Statements |
| 333 | + today = fields.Date.today() |
| 334 | + current_period = self.current_period |
| 335 | + statement = self._get_statement( |
| 336 | + current_period, |
| 337 | + today, |
| 338 | + tax_statement_account, |
| 339 | + ) |
| 340 | + |
| 341 | + current_period.type_id.allow_overlap = True |
| 342 | + other_current_period = current_period.copy( |
| 343 | + default={ |
| 344 | + 'name': "Test Other current Period", |
| 345 | + 'vat_statement_id': False, |
| 346 | + }, |
| 347 | + ) |
| 348 | + other_statement = self._get_statement( |
| 349 | + other_current_period, |
| 350 | + today, |
| 351 | + other_tax_statement_account, |
| 352 | + ) |
| 353 | + |
| 354 | + # Assert: Each one of this Year's Statements counts as previous Amount |
| 355 | + # only the corresponding last Year's Statement |
| 356 | + self.assertEqual( |
| 357 | + statement.previous_credit_vat_amount, |
| 358 | + -last_year_statement.authority_vat_amount, |
| 359 | + ) |
| 360 | + self.assertEqual( |
| 361 | + other_statement.previous_credit_vat_amount, |
| 362 | + -other_last_year_statement.authority_vat_amount, |
| 363 | + ) |
0 commit comments