[IMP] Add type to analytic account / contract

This commit is contained in:
Stefan Becker
2017-09-06 13:18:00 +02:00
committed by Pedro M. Baeza
parent aa19a15aea
commit 3cc3c00452
4 changed files with 128 additions and 19 deletions

View File

@@ -199,28 +199,33 @@ class AccountAnalyticAccount(models.Model):
return invoice_line_vals
@api.multi
def _prepare_invoice(self):
def _prepare_invoice(self, journal=None):
self.ensure_one()
if not self.partner_id:
raise ValidationError(
_("You must first select a Customer for Contract %s!") %
self.name)
journal = self.journal_id or self.env['account.journal'].search(
[('type', '=', 'sale'),
('company_id', '=', self.company_id.id)],
limit=1)
if not journal:
journal = self.journal_id or self.env['account.journal'].search([
('type', '=', self.contract_type),
('company_id', '=', self.company_id.id)
], limit=1)
if not journal:
raise ValidationError(
_("Please define a sale journal for the company '%s'.") %
(self.company_id.name or '',))
_("Please define a %s journal for the company '%s'.") %
(self.contract_type, self.company_id.name or '')
)
currency = (
self.pricelist_id.currency_id or
self.partner_id.property_product_pricelist.currency_id or
self.company_id.currency_id
)
invoice_type = 'out_invoice'
if self.contract_type == 'purchase':
invoice_type = 'in_invoice'
invoice = self.env['account.invoice'].new({
'reference': self.code,
'type': 'out_invoice',
'type': invoice_type,
'partner_id': self.partner_id.address_get(
['invoice'])['invoice'],
'currency_id': currency.id,

View File

@@ -6,7 +6,7 @@
# Copyright 2015-2017 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
from odoo import api, fields, models, _
class AccountAnalyticContract(models.Model):
@@ -26,6 +26,12 @@ class AccountAnalyticContract(models.Model):
comodel_name="res.partner",
string="Partner (always False)",
)
contract_type = fields.Selection(
selection=[
('sale', _('Sale')),
('purchase', _('Purchase')),
], default='sale'
)
pricelist_id = fields.Many2one(
comodel_name='product.pricelist',
string='Pricelist',
@@ -64,7 +70,7 @@ class AccountAnalyticContract(models.Model):
'account.journal',
string='Journal',
default=lambda s: s._default_journal(),
domain="[('type', '=', 'sale'),('company_id', '=', company_id)]",
domain="[('company_id', '=', company_id)]",
)
company_id = fields.Many2one(
'res.company',
@@ -73,6 +79,13 @@ class AccountAnalyticContract(models.Model):
default=lambda self: self.env.user.company_id,
)
@api.onchange('contract_type')
def _onchange_contract_type(self):
self.journal_id = self.env['account.journal'].search([
('type', '=', self.contract_type),
('company_id', '=', self.company_id.id)
], limit=1)
@api.model
def _default_journal(self):
company_id = self.env.context.get(