2014-07-04 18:42:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
2014-12-03 17:57:52 +01:00
|
|
|
# Copyright (C) 2011 - 2014 Agile Business Group sagl
|
|
|
|
# (<http://www.agilebg.com>)
|
2014-07-04 18:42:47 +02:00
|
|
|
# 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-12-17 14:14:35 +01:00
|
|
|
from openerp.tools.safe_eval import safe_eval as eval
|
2014-12-17 11:56:21 +01:00
|
|
|
from functools import partial
|
2014-07-04 18:42:47 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
2014-12-04 15:01:38 +01:00
|
|
|
class AccountDocumentTemplate(models.Model):
|
2014-07-04 18:42:47 +02:00
|
|
|
_name = 'account.document.template'
|
|
|
|
|
2014-12-04 15:01:38 +01:00
|
|
|
name = fields.Char(required=True)
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 17:50:23 +01:00
|
|
|
@api.multi
|
|
|
|
def _input_lines(self):
|
2014-07-04 18:42:47 +02:00
|
|
|
count = 0
|
2014-12-09 17:50:23 +01:00
|
|
|
for line in self.template_line_ids:
|
2014-07-04 18:42:47 +02:00
|
|
|
if line.type == 'input':
|
|
|
|
count += 1
|
|
|
|
return count
|
|
|
|
|
2014-12-09 14:24:43 +01:00
|
|
|
@api.multi
|
2014-12-09 17:50:23 +01:00
|
|
|
def _get_template_line(self, line_number):
|
|
|
|
for line in self.template_line_ids:
|
2014-07-04 18:42:47 +02:00
|
|
|
if line.sequence == line_number:
|
|
|
|
return line
|
|
|
|
return False
|
|
|
|
|
2014-12-09 14:24:43 +01:00
|
|
|
@api.multi
|
2014-12-09 17:50:23 +01:00
|
|
|
def _generate_empty_lines(self):
|
2014-07-04 18:42:47 +02:00
|
|
|
lines = {}
|
2014-12-09 17:50:23 +01:00
|
|
|
for line in self.template_line_ids:
|
2014-12-04 15:01:38 +01:00
|
|
|
lines[line.sequence] = None
|
2014-07-04 18:42:47 +02:00
|
|
|
return lines
|
|
|
|
|
2014-12-17 11:56:21 +01:00
|
|
|
@api.multi
|
|
|
|
def lines(self, line_number, computed_lines=None):
|
|
|
|
if computed_lines is None:
|
|
|
|
computed_lines = {}
|
|
|
|
if computed_lines[line_number] is not None:
|
|
|
|
return computed_lines[line_number]
|
2014-12-09 17:50:23 +01:00
|
|
|
line = self._get_template_line(line_number)
|
2014-11-18 21:07:06 +01:00
|
|
|
if re.match(r'L\( *' + str(line_number) + r' *\)', line.python_code):
|
2014-12-04 15:01:38 +01:00
|
|
|
raise exceptions.Warning(
|
2014-07-04 18:42:47 +02:00
|
|
|
_('Line %s can\'t refer to itself') % str(line_number)
|
|
|
|
)
|
|
|
|
try:
|
2014-12-17 11:56:21 +01:00
|
|
|
recurse_lines = partial(self.lines, computed_lines=computed_lines)
|
|
|
|
computed_lines[line_number] = eval(
|
2014-12-17 14:14:35 +01:00
|
|
|
line.python_code.replace('L', 'recurse_lines'),
|
|
|
|
locals_dict={'recurse_lines': recurse_lines}
|
2014-08-20 15:46:23 +02:00
|
|
|
)
|
2014-07-04 18:42:47 +02:00
|
|
|
except KeyError:
|
2014-12-04 15:01:38 +01:00
|
|
|
raise exceptions.Warning(
|
2014-07-04 18:42:47 +02:00
|
|
|
_('Code "%s" refers to non existing line') % line.python_code)
|
2014-12-17 11:56:21 +01:00
|
|
|
return computed_lines[line_number]
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-09 17:50:23 +01:00
|
|
|
@api.multi
|
|
|
|
def compute_lines(self, input_lines):
|
2014-07-04 18:42:47 +02:00
|
|
|
# input_lines: dictionary in the form {line_number: line_amount}
|
2014-08-20 15:46:23 +02:00
|
|
|
# returns all the lines (included input lines)
|
|
|
|
# in the form {line_number: line_amount}
|
2014-12-09 17:50:23 +01:00
|
|
|
if len(input_lines) != self._input_lines():
|
2014-12-04 15:01:38 +01:00
|
|
|
raise exceptions.Warning(
|
2014-08-20 15:46:23 +02:00
|
|
|
_('Inconsistency between input lines and '
|
2014-12-09 17:50:23 +01:00
|
|
|
'filled lines for template %s') % self.name
|
2014-07-04 18:42:47 +02:00
|
|
|
)
|
2014-12-17 11:56:21 +01:00
|
|
|
computed_lines = self._generate_empty_lines()
|
|
|
|
computed_lines.update(input_lines)
|
|
|
|
for line_number in computed_lines:
|
|
|
|
computed_lines[line_number] = self.lines(
|
|
|
|
line_number, computed_lines)
|
|
|
|
return computed_lines
|
2014-07-04 18:42:47 +02:00
|
|
|
|
|
|
|
|
2014-12-04 15:01:38 +01:00
|
|
|
class AccountDocumentTemplateLine(models.Model):
|
2014-07-04 18:42:47 +02:00
|
|
|
_name = 'account.document.template.line'
|
|
|
|
|
2014-12-04 15:01:38 +01:00
|
|
|
name = fields.Char(required=True)
|
|
|
|
sequence = fields.Integer(string='Sequence', required=True)
|
|
|
|
type = fields.Selection(
|
|
|
|
[('computed', 'Computed'), ('input', 'User input')],
|
|
|
|
string='Type',
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
python_code = fields.Text(string='Python Code')
|