2014-07-04 18:42:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
|
|
|
|
# Copyright (C) 2011 Domsense srl (<http://www.domsense.com>)
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2014-12-09 11:43:50 +01:00
|
|
|
from openerp import models, fields, api, exceptions, _
|
2014-07-04 18:42:47 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
class WizardSelectMoveTemplate(models.TransientModel):
|
2014-07-04 18:42:47 +02:00
|
|
|
_name = "wizard.select.move.template"
|
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
template_id = fields.Many2one(
|
|
|
|
comodel_name='account.move.template',
|
|
|
|
string='Move Template',
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
partner_id = fields.Many2one(
|
|
|
|
comodel_name='res.partner',
|
|
|
|
string='Partner'
|
|
|
|
)
|
|
|
|
line_ids = fields.One2many(
|
|
|
|
comodel_name='wizard.select.move.template.line',
|
|
|
|
inverse_name='template_id',
|
|
|
|
string='Lines'
|
|
|
|
)
|
|
|
|
state = fields.Selection(
|
|
|
|
[('template_selected', 'Template selected')],
|
|
|
|
string='State'
|
|
|
|
)
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 17:50:23 +01:00
|
|
|
@api.multi
|
|
|
|
def check_zero_lines(self):
|
|
|
|
if not self.line_ids:
|
|
|
|
return True
|
|
|
|
for template_line in self.line_ids:
|
|
|
|
if template_line.amount:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
@api.multi
|
|
|
|
def load_lines(self):
|
2014-12-09 14:33:50 +01:00
|
|
|
self.ensure_one()
|
2014-12-09 17:50:23 +01:00
|
|
|
template = self.template_id
|
2014-12-09 14:33:50 +01:00
|
|
|
for line in template.template_line_ids:
|
|
|
|
if line.type == 'input':
|
|
|
|
self.env['wizard.select.move.template.line'].create({
|
|
|
|
'template_id': self.id,
|
|
|
|
'sequence': line.sequence,
|
|
|
|
'name': line.name,
|
|
|
|
'amount': 0.0,
|
|
|
|
'account_id': line.account_id.id,
|
|
|
|
'move_line_type': line.move_line_type,
|
|
|
|
})
|
|
|
|
if not self.line_ids:
|
|
|
|
return self.load_template()
|
2014-12-09 17:50:23 +01:00
|
|
|
self.state = 'template_selected'
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 14:33:50 +01:00
|
|
|
view_rec = self.env['ir.model.data'].get_object_reference(
|
|
|
|
'account_move_template', 'wizard_select_template')
|
|
|
|
view_id = view_rec and view_rec[1] or False
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 14:33:50 +01:00
|
|
|
return {
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_id': [view_id],
|
|
|
|
'view_mode': 'form',
|
|
|
|
'res_model': 'wizard.select.move.template',
|
|
|
|
'res_id': self.id,
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'target': 'new',
|
|
|
|
'context': self.env.context,
|
|
|
|
}
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
@api.multi
|
|
|
|
def load_template(self):
|
2014-12-09 14:33:50 +01:00
|
|
|
self.ensure_one()
|
|
|
|
account_period_model = self.env['account.period']
|
2014-12-09 17:50:23 +01:00
|
|
|
if not self.check_zero_lines():
|
2014-12-09 14:33:50 +01:00
|
|
|
raise exceptions.Warning(
|
|
|
|
_('At least one amount has to be non-zero!')
|
|
|
|
)
|
|
|
|
input_lines = {}
|
|
|
|
for template_line in self.line_ids:
|
|
|
|
input_lines[template_line.sequence] = template_line.amount
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 14:33:50 +01:00
|
|
|
period = account_period_model.find()
|
|
|
|
if not period:
|
2014-12-09 17:50:23 +01:00
|
|
|
raise exceptions.Warning(_('Unable to find a valid period !'))
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 17:50:23 +01:00
|
|
|
computed_lines = self.template_id.compute_lines(input_lines)
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 14:33:50 +01:00
|
|
|
moves = {}
|
|
|
|
for line in self.template_id.template_line_ids:
|
|
|
|
if line.journal_id.id not in moves:
|
|
|
|
moves[line.journal_id.id] = self._make_move(
|
|
|
|
self.template_id.name,
|
|
|
|
period.id,
|
|
|
|
line.journal_id.id,
|
|
|
|
self.partner_id.id
|
|
|
|
)
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 14:33:50 +01:00
|
|
|
self._make_move_line(
|
|
|
|
line,
|
|
|
|
computed_lines,
|
|
|
|
moves[line.journal_id.id],
|
|
|
|
period.id,
|
|
|
|
self.partner_id.id
|
|
|
|
)
|
|
|
|
if self.template_id.cross_journals:
|
|
|
|
trans_account_id = self.template_id.transitory_acc_id.id
|
|
|
|
self._make_transitory_move_line(
|
2014-07-04 18:42:47 +02:00
|
|
|
line,
|
|
|
|
computed_lines,
|
|
|
|
moves[line.journal_id.id],
|
2014-12-06 23:20:35 +01:00
|
|
|
period.id,
|
2014-12-09 14:33:50 +01:00
|
|
|
trans_account_id,
|
|
|
|
self.partner_id.id
|
2014-07-04 18:42:47 +02:00
|
|
|
)
|
|
|
|
|
2014-12-09 14:33:50 +01:00
|
|
|
return {
|
|
|
|
'domain': "[('id','in', " + str(moves.values()) + ")]",
|
|
|
|
'name': 'Entries',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
'res_model': 'account.move',
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'target': 'current',
|
|
|
|
}
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
@api.model
|
|
|
|
def _make_move(self, ref, period_id, journal_id, partner_id):
|
2014-12-06 23:20:35 +01:00
|
|
|
move = self.env['account.move'].create({
|
2014-07-04 18:42:47 +02:00
|
|
|
'ref': ref,
|
|
|
|
'period_id': period_id,
|
|
|
|
'journal_id': journal_id,
|
|
|
|
'partner_id': partner_id,
|
|
|
|
})
|
2014-12-06 23:20:35 +01:00
|
|
|
return move.id
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-06 23:20:35 +01:00
|
|
|
@api.model
|
2014-12-04 15:04:41 +01:00
|
|
|
def _make_move_line(self, line, computed_lines,
|
2014-08-20 15:46:23 +02:00
|
|
|
move_id, period_id, partner_id):
|
2014-12-04 15:04:41 +01:00
|
|
|
account_move_line_model = self.env['account.move.line']
|
2014-07-04 18:42:47 +02:00
|
|
|
analytic_account_id = False
|
|
|
|
if line.analytic_account_id:
|
|
|
|
if not line.journal_id.analytic_journal_id:
|
2014-12-04 15:04:41 +01:00
|
|
|
raise exceptions.Warning(
|
2014-08-20 15:46:23 +02:00
|
|
|
_("You have to define an analytic "
|
|
|
|
"journal on the '%s' journal!")
|
2014-07-04 18:42:47 +02:00
|
|
|
% (line.journal_id.name,)
|
|
|
|
)
|
|
|
|
|
|
|
|
analytic_account_id = line.analytic_account_id.id
|
2014-12-04 15:04:41 +01:00
|
|
|
vals = {
|
2014-07-04 18:42:47 +02:00
|
|
|
'name': line.name,
|
|
|
|
'move_id': move_id,
|
|
|
|
'journal_id': line.journal_id.id,
|
|
|
|
'period_id': period_id,
|
|
|
|
'analytic_account_id': analytic_account_id,
|
|
|
|
'account_id': line.account_id.id,
|
|
|
|
'date': time.strftime('%Y-%m-%d'),
|
|
|
|
'account_tax_id': line.account_tax_id.id,
|
|
|
|
'credit': 0.0,
|
|
|
|
'debit': 0.0,
|
|
|
|
'partner_id': partner_id,
|
|
|
|
}
|
|
|
|
if line.move_line_type == 'cr':
|
2014-12-04 15:04:41 +01:00
|
|
|
vals['credit'] = computed_lines[line.sequence]
|
2014-07-04 18:42:47 +02:00
|
|
|
if line.move_line_type == 'dr':
|
2014-12-04 15:04:41 +01:00
|
|
|
vals['debit'] = computed_lines[line.sequence]
|
|
|
|
id_line = account_move_line_model.create(vals)
|
2014-07-04 18:42:47 +02:00
|
|
|
return id_line
|
|
|
|
|
2014-12-06 23:20:35 +01:00
|
|
|
@api.model
|
2014-12-04 15:04:41 +01:00
|
|
|
def _make_transitory_move_line(self, line,
|
2014-07-04 18:42:47 +02:00
|
|
|
computed_lines, move_id, period_id,
|
|
|
|
trans_account_id, partner_id):
|
2014-12-04 15:04:41 +01:00
|
|
|
account_move_line_model = self.env['account.move.line']
|
2014-07-04 18:42:47 +02:00
|
|
|
analytic_account_id = False
|
|
|
|
if line.analytic_account_id:
|
|
|
|
if not line.journal_id.analytic_journal_id:
|
2014-12-04 15:04:41 +01:00
|
|
|
raise exceptions.Warning(
|
2014-07-04 18:42:47 +02:00
|
|
|
_('No Analytic Journal !'),
|
2014-08-20 15:46:23 +02:00
|
|
|
_("You have to define an analytic journal "
|
|
|
|
"on the '%s' journal!")
|
2014-07-04 18:42:47 +02:00
|
|
|
% (line.template_id.journal_id.name,)
|
|
|
|
)
|
|
|
|
analytic_account_id = line.analytic_account_id.id
|
2014-12-04 15:04:41 +01:00
|
|
|
vals = {
|
2014-07-04 18:42:47 +02:00
|
|
|
'name': 'transitory',
|
|
|
|
'move_id': move_id,
|
|
|
|
'journal_id': line.journal_id.id,
|
|
|
|
'period_id': period_id,
|
|
|
|
'analytic_account_id': analytic_account_id,
|
|
|
|
'account_id': trans_account_id,
|
|
|
|
'date': time.strftime('%Y-%m-%d'),
|
|
|
|
'partner_id': partner_id,
|
|
|
|
}
|
|
|
|
if line.move_line_type != 'cr':
|
2014-12-04 15:04:41 +01:00
|
|
|
vals['credit'] = computed_lines[line.sequence]
|
2014-07-04 18:42:47 +02:00
|
|
|
if line.move_line_type != 'dr':
|
2014-12-04 15:04:41 +01:00
|
|
|
vals['debit'] = computed_lines[line.sequence]
|
|
|
|
id_line = account_move_line_model.create(vals)
|
2014-07-04 18:42:47 +02:00
|
|
|
return id_line
|
|
|
|
|
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
class WizardSelectMoveTemplateLine(models.TransientModel):
|
2014-07-04 18:42:47 +02:00
|
|
|
_description = 'Template Lines'
|
|
|
|
_name = "wizard.select.move.template.line"
|
2014-12-04 15:04:41 +01:00
|
|
|
|
|
|
|
template_id = fields.Many2one(
|
|
|
|
comodel_name='wizard.select.move.template',
|
|
|
|
string='Template'
|
|
|
|
)
|
|
|
|
sequence = fields.Integer(string='Number', required=True)
|
2014-12-04 15:06:50 +01:00
|
|
|
name = fields.Char(required=True, readonly=True)
|
2014-12-04 15:04:41 +01:00
|
|
|
account_id = fields.Many2one(
|
|
|
|
comodel_name='account.account',
|
|
|
|
string='Account',
|
|
|
|
required=True,
|
|
|
|
readonly=True
|
|
|
|
)
|
|
|
|
move_line_type = fields.Selection(
|
|
|
|
[('cr', 'Credit'), ('dr', 'Debit')],
|
|
|
|
string='Move Line Type',
|
|
|
|
required=True,
|
|
|
|
readonly=True
|
|
|
|
)
|
|
|
|
amount = fields.Float(required=True)
|