diff --git a/account_financial_report/tests/test_general_ledger.py b/account_financial_report/tests/test_general_ledger.py index ee9f9e64..bf98c31d 100644 --- a/account_financial_report/tests/test_general_ledger.py +++ b/account_financial_report/tests/test_general_ledger.py @@ -4,6 +4,7 @@ import time +from odoo.api import Environment from odoo.tests import common from . import abstract_test_foreign_currency as a_t_f_c @@ -493,7 +494,7 @@ class TestGeneralLedgerReport(common.TransactionCase): self.assertEqual(lines['unaffected'].final_debit, 500) self.assertEqual(lines['unaffected'].final_credit, 0) self.assertEqual(lines['unaffected'].final_balance, 500) - + def test_partner_filter(self): partner_1 = self.env.ref('base.res_partner_1') partner_2 = self.env.ref('base.res_partner_2') @@ -504,9 +505,43 @@ class TestGeneralLedgerReport(common.TransactionCase): partner_3.write({'is_company': False}) expected_list = [partner_2.id, partner_3.id, partner_4.id] - context = {'active_ids': - [partner_1.id, partner_2.id, partner_3.id, partner_4.id], + context = {'active_ids': [ + partner_1.id, partner_2.id, partner_3.id, partner_4.id], 'active_model': 'res.partner'} wizard = self.env["general.ledger.report.wizard"].with_context(context) - self.assertEqual(wizard._default_partners(), expected_list) \ No newline at end of file + self.assertEqual(wizard._default_partners(), expected_list) + + def test_validate_date(self): + company_id = self.env.ref('base.main_company') + company_id.write({ + 'fiscalyear_last_day': 31, + 'fiscalyear_last_month': 12, + }) + user = self.env.ref('base.user_root').with_context( + company_id=company_id.id) + wizard = self.env["general.ledger.report.wizard"].with_context( + user=user.id + ) + self.assertEqual(wizard._init_date_from(), + time.strftime('%Y') + '-01-01') + + def test_validate_date_range(self): + data_type = self.env['date.range.type'].create({ + 'name': 'Fiscal year', + 'company_id': False, + 'allow_overlap': False + }) + + dr = self.env['date.range'].create({ + 'name': 'FS2015', + 'date_start': '2018-01-01', + 'date_end': '2018-12-31', + 'type_id': data_type.id, + }) + + wizard = self.env["general.ledger.report.wizard"].create({ + 'date_range_id': dr.id}) + wizard.onchange_date_range_id() + self.assertEqual(wizard.date_from, '2018-01-01') + self.assertEqual(wizard.date_to, '2018-12-31') diff --git a/account_financial_report/tests/test_open_items.py b/account_financial_report/tests/test_open_items.py index 570589df..78b2869f 100644 --- a/account_financial_report/tests/test_open_items.py +++ b/account_financial_report/tests/test_open_items.py @@ -39,6 +39,7 @@ class TestOpenItems(a_t_f_c.AbstractTestForeignCurrency): {'hide_account_at_0': True}, {'only_posted_moves': True, 'hide_account_at_0': True}, ] + def test_partner_filter(self): partner_1 = self.env.ref('base.res_partner_1') partner_2 = self.env.ref('base.res_partner_2') @@ -49,9 +50,9 @@ class TestOpenItems(a_t_f_c.AbstractTestForeignCurrency): partner_3.write({'is_company': False}) expected_list = [partner_2.id, partner_3.id, partner_4.id] - context = {'active_ids': - [partner_1.id, partner_2.id, partner_3.id, partner_4.id], + context = {'active_ids': [ + partner_1.id, partner_2.id, partner_3.id, partner_4.id], 'active_model': 'res.partner'} wizard = self.env["open.items.report.wizard"].with_context(context) - self.assertEqual(wizard._default_partners(), expected_list) \ No newline at end of file + self.assertEqual(wizard._default_partners(), expected_list) diff --git a/account_financial_report/wizard/general_ledger_wizard.py b/account_financial_report/wizard/general_ledger_wizard.py index be6c62cb..50f9c175 100644 --- a/account_financial_report/wizard/general_ledger_wizard.py +++ b/account_financial_report/wizard/general_ledger_wizard.py @@ -11,6 +11,7 @@ from odoo import api, fields, models, _ from odoo.tools.safe_eval import safe_eval from odoo.tools import pycompat from odoo.exceptions import ValidationError +import time class GeneralLedgerReportWizard(models.TransientModel): @@ -29,8 +30,10 @@ class GeneralLedgerReportWizard(models.TransientModel): comodel_name='date.range', string='Date range' ) - date_from = fields.Date(required=True) - date_to = fields.Date(required=True) + date_from = fields.Date(required=True, + default=lambda self: self._init_date_from()) + date_to = fields.Date(required=True, + default=fields.Date.context_today) fy_start_date = fields.Date(compute='_compute_fy_start_date') target_move = fields.Selection([('posted', 'All Posted Entries'), ('all', 'All Entries')], @@ -84,7 +87,19 @@ class GeneralLedgerReportWizard(models.TransientModel): 'will display initial and final balance in that currency.', default=lambda self: self._default_foreign_currency(), ) + + def _init_date_from(self): + """set start date to begin of current year if fiscal year running""" + today = fields.Date.context_today(self) + cur_month = fields.Date.from_string(today).month + cur_day = fields.Date.from_string(today).day + last_fsc_month = self.env.user.company_id.fiscalyear_last_month + last_fsc_day = self.env.user.company_id.fiscalyear_last_day + if cur_month < last_fsc_month \ + or cur_month == last_fsc_month and cur_day <= last_fsc_day: + return time.strftime('%Y-01-01') + def _default_foreign_currency(self): return self.env.user.has_group('base.group_multi_currency') @@ -167,8 +182,9 @@ class GeneralLedgerReportWizard(models.TransientModel): @api.onchange('date_range_id') def onchange_date_range_id(self): """Handle date range change.""" - self.date_from = self.date_range_id.date_start - self.date_to = self.date_range_id.date_end + if self.date_range_id: + self.date_from = self.date_range_id.date_start + self.date_to = self.date_range_id.date_end @api.multi @api.constrains('company_id', 'date_range_id')