Skip to content
Snippets Groups Projects
Commit f4945258e5ef authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

Add a period & dates onto acc docs

parent 0db1618b15a4
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,7 @@
'data': [
'security/ir.model.access.csv',
'views/account_move.xml',
'views/account_period.xml',
'views/account_fiscalyear.xml',
],
......
# flake8: noqa
from . import account_move
from . import account_period
from . import account_fiscalyear
......@@ -36,6 +36,14 @@
required=True,
)
company_id = fields.Many2one(
comodel_name='res.company',
string='Company',
default=lambda rec: rec.env.user.company_id,
help='The company the fiscal year is in.',
required=True,
)
name = fields.Char(
string='Name',
help='Displayed name of this fiscal year.',
......
##############################################################################
#
# Accounting periods, for Odoo
# Copyright (C) 2018 XCG Consulting <http://odoo.consulting>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from collections import defaultdict
from datetime import date
from odoo import _
from odoo import api
from odoo import exceptions
from odoo import fields
from odoo import models
class AccountMove(models.Model):
"""Add a period & dates onto accounting documents.
"""
_inherit = 'account.move'
period_id = fields.Many2one(
comodel_name='account.period',
string='Period',
help='The period this accounting document is in.',
ondelete='restrict',
required=True,
states={'posted': [('readonly', True)]},
)
transaction_date = fields.Date(
string='Transaction date',
help=(
'Invoicing date when provided; otherwise this is the accounting '
'date.'
),
states={'posted': [('readonly', True)]},
)
@api.model
def create(self, vals):
"""Override to set a transaction date from invoices.
"""
invoice = self.env.context.get('invoice')
if invoice and isinstance(invoice, models.BaseModel):
if not isinstance(vals, dict):
vals = {}
vals['transaction_date'] = invoice.date_invoice
return super(AccountMove, self).create(vals)
@api.model
def fields_get(self, allfields=None, attributes=None):
"""Override to tweak the default "date" field.
"""
ret = super(AccountMove, self).fields_get(
allfields=allfields, attributes=attributes,
)
date_field = ret.get('date')
if date_field:
date_field.update({
'string': _('Accounting date'),
'help': _('Validation date of the accounting document.'),
'readonly': True,
'states': {},
})
return ret
@api.multi
def post(self):
"""Override accounting document validation to:
- Set accounting dates (default "date" field).
- Also set the transaction date ("transaction_date" field) when empty.
"""
self.fill_accounting_dates()
return super(AccountMove, self).post()
@api.multi
def fill_accounting_dates(self):
"""- Set accounting dates (default "date" field).
- Also set the transaction date ("transaction_date" field) when empty.
- Force the period to always be around the current date.
"""
today = date.today()
# Group accounting documents by company.
accdocs_per_company = defaultdict(lambda: self.env['account.move'])
for accdoc in self:
accdocs_per_company[accdoc.company_id] |= accdoc
for company, accdocs in accdocs_per_company.iteritems():
period = self.env['account.period'].search([
('company_id', '=', company.id),
('date_start', '<=', today),
('date_stop', '>=', today),
])
if not period:
raise exceptions.Warning(_(
'No period found around %s in the "%s" company.'
) % (today, company.sudo().name))
accdocs_with_transaction_date = self.env['account.move']
accdocs_without_transaction_date = self.env['account.move']
for accdoc in accdocs:
if accdoc.transaction_date:
accdocs_with_transaction_date |= accdoc
else:
accdocs_without_transaction_date |= accdoc
accdocs_with_transaction_date.write({
'date': today,
'period_id': period.id,
})
accdocs_without_transaction_date.write({
'date': today,
'period_id': period.id,
'transaction_date': today,
})
return True
......@@ -39,6 +39,15 @@
required=True,
)
company_id = fields.Many2one(
related=('fiscalyear_id', 'company_id'),
comodel_name='res.company',
string='Company',
help='The company the fiscal year is in.',
readonly=True,
store=True,
)
date_cutoff = fields.Date(
string='Cut-off date',
help=(
......
......@@ -22,6 +22,7 @@
<tree>
<field name="name" />
<field name="code" />
<field name="company_id" />
<field name="period_ids" />
</tree>
......@@ -42,6 +43,7 @@
<group col="4">
<field name="name" />
<field name="code" />
<field name="company_id" />
</group>
<group>
......
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Add a period & dates onto accounting documents. -->
<!-- Inherit from the acc doc form view defined in "account". -->
<record id="account_period_account_move_form" model="ir.ui.view">
<field name="name">account_period_account_move_form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<!-- Add fields. -->
<xpath expr="//field[@name='date']" position="before">
<field name="period_id" options="{'no_create': 1}" />
</xpath>
<xpath expr="//field[@name='date']" position="after">
<field name="transaction_date" />
</xpath>
</field>
</record>
</odoo>
......@@ -22,6 +22,7 @@
<field name="fiscalyear_id" />
<field name="name" />
<field name="code" />
<field name="company_id" />
<field name="date_start" />
<field name="date_stop" />
<field name="date_cutoff" />
......@@ -45,6 +46,7 @@
<group col="4">
<field name="name" />
<field name="code" />
<field name="company_id" />
<field name="date_start" />
<field name="fiscalyear_id" />
<field name="date_stop" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment