2017-08-23 03:48:14 +02:00
|
|
|
# Copyright 2015-2017 See manifest
|
|
|
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2018-10-31 11:56:26 +01:00
|
|
|
from odoo import models, fields, api, _
|
2014-07-04 18:42:47 +02:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2017-08-23 03:48:14 +02:00
|
|
|
template_id = fields.Many2one('account.move.template', required=True)
|
2018-10-31 13:03:41 +01:00
|
|
|
company_id = fields.Many2one(
|
|
|
|
'res.company', required=True,
|
|
|
|
default=lambda self: self.env.user.company_id)
|
2017-08-23 03:48:14 +02:00
|
|
|
partner_id = fields.Many2one('res.partner', 'Partner')
|
2018-10-31 13:03:41 +01:00
|
|
|
date = fields.Date(required=True, default=fields.Date.context_today)
|
2014-12-04 15:04:41 +01:00
|
|
|
line_ids = fields.One2many(
|
2017-08-23 03:48:14 +02:00
|
|
|
'wizard.select.move.template.line', 'template_id')
|
2014-12-04 15:04:41 +01:00
|
|
|
state = fields.Selection(
|
2018-03-05 20:31:53 +01:00
|
|
|
[('template_selected', 'Template selected')])
|
2014-12-09 17:50:23 +01:00
|
|
|
|
2018-10-31 13:03:41 +01:00
|
|
|
@api.onchange('template_id', 'company_id')
|
|
|
|
def onchange_company_id(self):
|
|
|
|
template_domain = [('company_id', '=', self.company_id.id)]
|
|
|
|
return {'domain': {'template_id': template_domain}}
|
|
|
|
|
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()
|
2017-08-23 03:48:14 +02:00
|
|
|
lines = self.template_id.template_line_ids
|
|
|
|
for line in lines.filtered(lambda l: l.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,
|
2018-10-31 13:03:41 +01:00
|
|
|
'partner_id': line.partner_id.id or self.partner_id.id,
|
2017-08-23 03:48:14 +02:00
|
|
|
'move_line_type': line.move_line_type,
|
2018-10-31 13:03:41 +01:00
|
|
|
'tax_ids': [(6, 0, line.tax_ids.ids)],
|
|
|
|
'tax_line_id': line.tax_line_id.id,
|
|
|
|
'analytic_account_id': line.analytic_account_id.id,
|
|
|
|
'analytic_tag_ids': [(6, 0, line.analytic_tag_ids.ids)],
|
2017-08-23 03:48:14 +02:00
|
|
|
})
|
2014-12-09 14:33:50 +01:00
|
|
|
if not self.line_ids:
|
|
|
|
return self.load_template()
|
2014-12-09 17:50:23 +01:00
|
|
|
self.state = 'template_selected'
|
2017-08-23 03:48:14 +02:00
|
|
|
view_rec = self.env.ref('account_move_template.wizard_select_template')
|
2018-10-31 13:03:41 +01:00
|
|
|
action = self.env.ref(
|
|
|
|
'account_move_template.action_wizard_select_template_by_move')
|
|
|
|
result = action.read()[0]
|
|
|
|
result['res_id'] = self.id
|
|
|
|
result['view_id'] = [view_rec.id]
|
|
|
|
result['context'] = self.env.context
|
|
|
|
return result
|
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()
|
|
|
|
input_lines = {}
|
|
|
|
for template_line in self.line_ids:
|
|
|
|
input_lines[template_line.sequence] = template_line.amount
|
2017-08-23 03:48:14 +02:00
|
|
|
amounts = self.template_id.compute_lines(input_lines)
|
|
|
|
name = self.template_id.name
|
|
|
|
partner = self.partner_id.id
|
|
|
|
moves = self.env['account.move']
|
|
|
|
for journal in self.template_id.template_line_ids.mapped('journal_id'):
|
|
|
|
lines = []
|
2018-10-31 13:03:41 +01:00
|
|
|
move = self._create_move(name, journal.id)
|
2017-08-23 03:48:14 +02:00
|
|
|
moves = moves + move
|
|
|
|
for line in self.template_id.template_line_ids.filtered(
|
2018-03-05 20:31:53 +01:00
|
|
|
lambda l, j=journal: l.journal_id == j):
|
2017-08-23 03:48:14 +02:00
|
|
|
lines.append((0, 0,
|
|
|
|
self._prepare_line(line, amounts, partner)))
|
|
|
|
move.write({'line_ids': lines})
|
2018-10-31 13:03:41 +01:00
|
|
|
action = self.env.ref('account.action_move_journal_line')
|
|
|
|
result = action.read()[0]
|
|
|
|
result['domain'] = [('id', 'in', moves.ids)]
|
|
|
|
result['name'] = _('Entries from template: %s') % name
|
|
|
|
result['context'] = self.env.context
|
|
|
|
return result
|
2014-07-04 18:42:47 +02:00
|
|
|
|
2014-12-04 15:04:41 +01:00
|
|
|
@api.model
|
2018-10-31 13:03:41 +01:00
|
|
|
def _create_move(self, ref, journal_id):
|
2017-08-23 03:48:14 +02:00
|
|
|
return self.env['account.move'].create({
|
2014-07-04 18:42:47 +02:00
|
|
|
'ref': ref,
|
|
|
|
'journal_id': journal_id,
|
2018-10-31 13:03:41 +01:00
|
|
|
'date': self.date,
|
2014-07-04 18:42:47 +02:00
|
|
|
})
|
|
|
|
|
2014-12-06 23:20:35 +01:00
|
|
|
@api.model
|
2017-08-23 03:48:14 +02:00
|
|
|
def _prepare_line(self, line, amounts, partner_id):
|
|
|
|
debit = line.move_line_type == 'dr'
|
|
|
|
values = {
|
2014-07-04 18:42:47 +02:00
|
|
|
'name': line.name,
|
|
|
|
'journal_id': line.journal_id.id,
|
2017-08-23 03:48:14 +02:00
|
|
|
'analytic_account_id': line.analytic_account_id.id,
|
2014-07-04 18:42:47 +02:00
|
|
|
'account_id': line.account_id.id,
|
2017-08-23 03:48:14 +02:00
|
|
|
'credit': not debit and amounts[line.sequence] or 0.0,
|
|
|
|
'debit': debit and amounts[line.sequence] or 0.0,
|
2018-10-31 13:03:41 +01:00
|
|
|
'partner_id': line.partner_id.id or partner_id,
|
|
|
|
'tax_line_id': line.tax_line_id.id,
|
2014-07-04 18:42:47 +02:00
|
|
|
}
|
2018-10-31 13:03:41 +01:00
|
|
|
if line.analytic_tag_ids:
|
|
|
|
values['analytic_tag_ids'] = [(6, 0, line.analytic_tag_ids.ids)]
|
|
|
|
if line.tax_ids:
|
|
|
|
values['tax_ids'] = [(6, 0, line.tax_ids.ids)]
|
2017-08-23 03:48:14 +02:00
|
|
|
return values
|
2014-07-04 18:42:47 +02:00
|
|
|
|
|
|
|
|
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(
|
2017-08-23 03:48:14 +02:00
|
|
|
'wizard.select.move.template')
|
2018-10-31 13:03:41 +01:00
|
|
|
company_id = fields.Many2one('res.company',
|
|
|
|
related='template_id.company_id',
|
|
|
|
readonly=True)
|
|
|
|
sequence = fields.Integer('Sequence', required=True,
|
|
|
|
)
|
|
|
|
name = fields.Char('Name', required=True, readonly=True,
|
|
|
|
)
|
2014-12-04 15:04:41 +01:00
|
|
|
account_id = fields.Many2one(
|
2017-08-23 03:48:14 +02:00
|
|
|
'account.account', required=True, readonly=True)
|
2018-10-31 13:03:41 +01:00
|
|
|
analytic_account_id = fields.Many2one(
|
|
|
|
'account.analytic.account',
|
|
|
|
ondelete="cascade", readonly=True,
|
|
|
|
)
|
|
|
|
analytic_tag_ids = fields.Many2many('account.analytic.tag',
|
|
|
|
string='Analytic tags',
|
|
|
|
readonly=True,)
|
|
|
|
tax_ids = fields.Many2many('account.tax', string='Taxes', readonly=True)
|
|
|
|
tax_line_id = fields.Many2one('account.tax', string='Originator tax',
|
|
|
|
ondelete='restrict', readonly=True)
|
|
|
|
partner_id = fields.Many2one('res.partner', readonly=True,
|
|
|
|
string='Partner')
|
|
|
|
move_line_type = fields.Selection([('cr', 'Credit'), ('dr', 'Debit')],
|
|
|
|
required=True, readonly=True,
|
|
|
|
string='Journal Item Type',
|
|
|
|
)
|
|
|
|
amount = fields.Float('Amount', required=True,
|
|
|
|
)
|