From 2b00e5651847e2672dba71d8fa57a4a154ff810a Mon Sep 17 00:00:00 2001 From: Iryna Vyshnevska Date: Sun, 13 Jan 2019 15:34:42 +0200 Subject: [PATCH] [ADD] initialize wizard by current fiscal year --- .../tests/test_general_ledger.py | 42 +++++++++++++++++-- .../tests/test_open_items.py | 7 ++-- .../wizard/general_ledger_wizard.py | 25 +++++++++-- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/account_financial_report_qweb/tests/test_general_ledger.py b/account_financial_report_qweb/tests/test_general_ledger.py index 8538a7a2..91a3d204 100644 --- a/account_financial_report_qweb/tests/test_general_ledger.py +++ b/account_financial_report_qweb/tests/test_general_ledger.py @@ -4,6 +4,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import time +from odoo.api import Environment from odoo.tests import common from . import abstract_test_foreign_currency as a_t_f_c @@ -510,9 +511,44 @@ 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], - 'active_model': 'res.partner'} + 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) + + def test_validate_date(self): + company_id = self.env.ref('base.main_company') + company_id.write({ + 'fiscalyear_last_day': 31, + 'fiscalyear_last_month': 12, + }) + cr = self.registry.cursor() + user = self.env.ref('base.user_root').with_context({ + 'company_id': company_id.id}) + env = Environment(cr, user.id, {}) + wizard = env["general.ledger.report.wizard"] + self.assertEqual(wizard._init_date_from(), + time.strftime('%Y') + '-01-01') + + def test_validate_date_range(self): + 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': 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_qweb/tests/test_open_items.py b/account_financial_report_qweb/tests/test_open_items.py index 97b98b72..e6636b90 100644 --- a/account_financial_report_qweb/tests/test_open_items.py +++ b/account_financial_report_qweb/tests/test_open_items.py @@ -51,9 +51,10 @@ 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], - 'active_model': 'res.partner'} + 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) diff --git a/account_financial_report_qweb/wizard/general_ledger_wizard.py b/account_financial_report_qweb/wizard/general_ledger_wizard.py index 940b9873..c6f00858 100644 --- a/account_financial_report_qweb/wizard/general_ledger_wizard.py +++ b/account_financial_report_qweb/wizard/general_ledger_wizard.py @@ -9,6 +9,8 @@ from odoo import api, fields, models, _ from odoo.tools.safe_eval import safe_eval from odoo.exceptions import ValidationError +import time + class GeneralLedgerReportWizard(models.TransientModel): """General ledger report wizard.""" @@ -26,8 +28,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')], @@ -100,6 +104,18 @@ class GeneralLedgerReportWizard(models.TransientModel): return list(partner_ids) + 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 = int(fields.Date.from_string(today).strftime('%m')) + cur_day = int(fields.Date.from_string(today).strftime('%d')) + 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') + @api.depends('date_from') def _compute_fy_start_date(self): for wiz in self.filtered('date_from'): @@ -164,8 +180,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')