Skip to content

Commit

Permalink
Merge 667bd0b into 3b85068
Browse files Browse the repository at this point in the history
  • Loading branch information
feketemihai committed Feb 25, 2018
2 parents 3b85068 + 667bd0b commit 9bc59b8
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 0 deletions.
2 changes: 2 additions & 0 deletions l10n_ro_account_period_close/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizards
21 changes: 21 additions & 0 deletions l10n_ro_account_period_close/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2018 Forest and Biomass Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
'name': 'Romania - Account Period Closing',
'summary': 'Romania - Account Period Closing',
'version': '11.0.1.0.0',
'category': 'Localization',
'author': 'Forest and Biomass Romania, '
'Odoo Community Association (OCA)',
'website': 'https://www.forbiom.eu',
'license': 'AGPL-3',
'installable': True,
'depends': ['account'],
'data': [
'views/account_period_close_view.xml',
'wizards/wizard_account_period_closing_view.xml',
'security/account_security.xml',
'security/ir.model.access.csv',
],
}
2 changes: 2 additions & 0 deletions l10n_ro_account_period_close/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import account
from . import account_period_close
22 changes: 22 additions & 0 deletions l10n_ro_account_period_close/models/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2018 Forest and Biomass Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class Account(models.Model):
_inherit = 'account.account'

close_check = fields.Boolean(
'Bypass Closing Side Check',
help='By checking this when you close a period, it will not respect '
'the side of closing, meaning: expenses closed on credit side, '
'incomed closed on debit side. \n You should check the 711xxx '
'accounts.')


class AccountMove(models.Model):
_inherit = 'account.move'

close_id = fields.Many2one(
'account.period.closing', 'Closed Account Period')
229 changes: 229 additions & 0 deletions l10n_ro_account_period_close/models/account_period_close.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
# Copyright 2018 Forest and Biomass Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models


class AccountPeriodClosing(models.Model):
_name = 'account.period.closing'
_description = 'Account Period Closing'

name = fields.Char('Name', required=True)
company_id = fields.Many2one(
'res.company', string='Company', required=True,
default=lambda self: self.env.user.company_id)
type = fields.Selection(
[
('income', 'Incomes'),
('expense', 'Expenses'),
('selected', 'Selected')
], string='Type', required=True)
close_result = fields.Boolean('Close debit and credit accounts')
journal_id = fields.Many2one(
'account.journal', string='Journal', required=True)
account_ids = fields.Many2many(
'account.account', string='Accounts to close')
debit_account_id = fields.Many2one(
'account.account',
'Closing account, debit',
required=True,
domain="[('company_id', '=', company_id)]"
)
credit_account_id = fields.Many2one(
'account.account',
'Closing account, credit',
required=True,
domain="[('company_id', '=', company_id)]"
)
move_ids = fields.One2many('account.move', 'close_id', 'Closing Moves')

@api.onchange('company_id', 'type')
def _onchange_type(self):
acc_type = False
accounts = self.env['account.account']
if self.type == 'income':
acc_type = self.env.ref(
'account.data_account_type_revenue').id
elif self.type == 'expense':
acc_type = self.env.ref(
'account.data_account_type_expenses').id
if acc_type:
accounts = self.env['account.account'].search([
('user_type_id', '=', acc_type),
('company_id', '=', self.company_id.id)
])
self.account_ids = accounts

def _get_accounts(self, accounts, display_account):
""" compute the balance, debit and credit for the provided accounts
:Arguments:
`accounts`: list of accounts record,
`display_account`: it's used to display either all accounts or
those accounts which balance is > 0
:Returns a list of dict of Accounts with following key and value
`name`: Account name,
`code`: Account code,
`credit`: total amount of credit,
`debit`: total amount of debit,
`balance`: total amount of balance,
"""

account_result = {}
# Prepare sql query base on selected parameters from wizard
tables, where_clause, where_params = self.env[
'account.move.line']._query_get()
tables = tables.replace('"', '')
if not tables:
tables = 'account_move_line'
wheres = [""]
if where_clause.strip():
wheres.append(where_clause.strip())
filters = " AND ".join(wheres)
# compute the balance, debit and credit for the provided accounts
request = ("SELECT account_id AS id, "
"SUM(debit) AS debit, "
"SUM(credit) AS credit, "
"(SUM(debit) - SUM(credit)) AS balance" +
" FROM " + tables +
" WHERE account_id IN %s " + filters +
" GROUP BY account_id")
params = (tuple(accounts.ids),) + tuple(where_params)
self.env.cr.execute(request, params)
for row in self.env.cr.dictfetchall():
account_result[row.pop('id')] = row

account_res = []
for account in accounts:
res = dict((fn, 0.0) for fn in ['credit', 'debit', 'balance'])
currency = account.currency_id if account.currency_id else \
account.company_id.currency_id
res['id'] = account.id
res['code'] = account.code
res['name'] = account.name
if account.id in account_result:
res['debit'] = account_result[account.id].get('debit')
res['credit'] = account_result[account.id].get('credit')
res['balance'] = account_result[account.id].get('balance')
if display_account == 'all':
account_res.append(res)
if display_account == 'not_zero' and \
not currency.is_zero(res['balance']):
account_res.append(res)
if display_account == 'movement' and \
(not currency.is_zero(res['debit']) or
not currency.is_zero(res['credit'])):
account_res.append(res)
return account_res

@api.multi
def close(self, date_from=None, date_to=None):
""" This method will create the closing move for the
date interval selected."""
account_obj = self.env['account.account']
journal_id = self.journal_id.id
for closing in self:
ctx = self.env.context.copy()
ctx['strict_range'] = True
ctx['date_from'] = date_from
ctx['date_to'] = date_to
account_res = self.with_context(ctx)._get_accounts(
closing.account_ids, 'not_zero')
move = self.env['account.move'].create({
'date': date_to,
'journal_id': journal_id,
'close_id': closing.id,
'company_id': closing.company_id.id
})
amount = 0.0
for account in account_res:
if account['balance'] != 0.0:
balance = account['balance']
check = account_obj.browse(account['id']).close_check
if closing.type == 'expense' and not check:
val = {
'name': 'Closing ' + closing.name,
'move_id': move.id,
'account_id': account['id'],
'credit': balance or 0.0,
'debit': 0.0,
}
elif closing.type == 'income' and not check:
val = {
'name': 'Closing ' + closing.name,
'move_id': move.id,
'account_id': account['id'],
'credit': 0.0,
'debit': (-1 * balance) or 0.0,
}
else:
val = {
'name': 'Closing ' + closing.name,
'move_id': move.id,
'account_id': account['id'],
'credit': balance if balance > 0.0 else 0.0,
'debit': -balance if balance < 0.0 else 0.0,
}
amount += balance
self.env['account.move.line'].with_context(
check_move_validity=False).create(val)

diff_line = {
'name': 'Closing ' + closing.name,
'move_id': move.id,
'account_id':
closing.debit_account_id.id if
amount >= 0 else closing.credit_account_id.id,
'credit': -amount if amount <= 0.0 else 0.0,
'debit': amount if amount >= 0.0 else 0.0,
}
self.env['account.move.line'].with_context(
check_move_validity=False).create(diff_line)
if self.close_result and amount != 0.0:
debit_acc = closing.debit_account_id
credit_acc = closing.credit_account_id
debit = credit = new_amount = 0.0
ctx1 = dict(self._context)
ctx1.update({'date_from': False, 'date_to': date_to})
accounts = account_obj.browse(
[closing.debit_account_id.id,
closing.credit_account_id.id])
account_res = self.with_context(ctx1)._get_accounts(
accounts, 'all')
for acc in account_res:
if acc['id'] == closing.debit_account_id.id:
debit = acc['balance']
if acc['id'] == closing.credit_account_id.id:
credit = acc['balance']
old_balance = debit - (-1 * credit)
if credit and debit:
if old_balance > 0:
debit_acc = closing.credit_account_id
credit_acc = closing.debit_account_id
elif old_balance < 0:
debit_acc = closing.debit_account_id
credit_acc = closing.credit_account_id
if abs(debit) > abs(credit):
new_amount = -1 * credit
else:
new_amount = debit
diff_line = {
'name': 'Closing ' + closing.name +
' ' + str(debit_acc.code),
'move_id': move.id,
'account_id': debit_acc.id,
'credit': 0.0,
'debit': new_amount,
}
self.env['account.move.line'].with_context(
check_move_validity=False).create(diff_line)
diff_line = {
'name': 'Closing ' + closing.name +
' ' + str(credit_acc.code),
'move_id': move.id,
'account_id': credit_acc.id,
'credit': new_amount,
'debit': 0.0,
}
self.env['account.move.line'].with_context(
check_move_validity=False).create(diff_line)
move.post()
11 changes: 11 additions & 0 deletions l10n_ro_account_period_close/security/account_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="account_period_closing_comp_rule" model="ir.rule">
<field name="name">Account Period Closing</field>
<field name="model_id" ref="model_account_period_closing"/>
<field name="global" eval="True"/>
<field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
</record>
</data>
</odoo>
3 changes: 3 additions & 0 deletions l10n_ro_account_period_close/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_account_period_closing_user,account.period.closing,model_account_period_closing,account.group_account_user,1,1,1,0
access_account_period_closing_manager,account.period.closing,model_account_period_closing,account.group_account_manager,1,1,1,1
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions l10n_ro_account_period_close/views/account_period_close_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_account_form" model="ir.ui.view">
<field name="name">account.account.form</field>
<field name="model">account.account</field>
<field name="type">form</field>
<field name="inherit_id" ref="account.view_account_form"/>
<field name="arch" type="xml">
<field position="after" name="reconcile">
<field name="close_check"/>
</field>
</field>
</record>

<record id="view_account_period_closing_tree" model="ir.ui.view">
<field name="name">account.period.closing.tree</field>
<field name="model">account.period.closing</field>
<field name="type">form</field>
<field name="arch" type="xml">
<tree string="Closing">
<field name="name"/>
<field name="type"/>
<field name="debit_account_id"/>
<field name="credit_account_id"/>
</tree>
</field>
</record>

<record id="view_account_period_closing_form" model="ir.ui.view">
<field name="name">account.period.closing.form</field>
<field name="model">account.period.closing</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Closing">
<div class="oe_editonly">
<label for="name"/>
<h1><field name="name"/></h1>
</div>
<notebook>
<page string="Config">
<group name="main_info">
<field name="company_id" options="{'no_create': True}" groups="base.group_multi_company"/>
<field name="journal_id" options="{'no_create': True}" domain="[('company_id','=',company_id)]"/>
<field name="type"/>
<field name="close_result"/>
</group>
<group name="accounts">
<field name="debit_account_id" options="{'no_create': True}"/>
<field name="credit_account_id" options="{'no_create': True}"/>
</group>
<field colspan="4" name="account_ids"/>
</page>
<page string="Moves">
<field name="move_ids" />
</page>
</notebook>
</form>
</field>
</record>

<record id="action_account_period_closing" model="ir.actions.act_window">
<field name="name">Account Period Closing</field>
<field name="res_model">account.period.closing</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>

<menuitem
action="action_account_period_closing"
id="menu_action_account_period_closing"
parent="account.menu_finance_entries_actions"/>

<act_window
id="act_account_period_closing_wizard"
name="Close Period"
res_model="account.period.closing.wizard"
view_mode="form" target="new" multi="False"
context="{'search_default_closing_id':[active_id], 'default_closing_id': active_id}"
src_model="account.period.closing"/>

</odoo>
1 change: 1 addition & 0 deletions l10n_ro_account_period_close/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import wizard_account_period_closing

0 comments on commit 9bc59b8

Please sign in to comment.