diff --git a/contract/models/account_analytic_account.py b/contract/models/account_analytic_account.py index 31ccf503..01f39037 100644 --- a/contract/models/account_analytic_account.py +++ b/contract/models/account_analytic_account.py @@ -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, diff --git a/contract/models/account_analytic_contract.py b/contract/models/account_analytic_contract.py index 6f04a9e5..1d67fb3b 100644 --- a/contract/models/account_analytic_contract.py +++ b/contract/models/account_analytic_contract.py @@ -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( diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index eaef5f5f..df9c0525 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -233,6 +233,12 @@ class TestContract(TestContractBase): result = self.contract.action_contract_send() self.assertEqual(result['res_model'], 'mail.compose.message') + def test_onchange_contract_type(self): + self.contract._onchange_contract_type() + self.assertEqual(self.contract.journal_id.type, 'sale') + self.assertEqual( + self.contract.journal_id.company_id, self.contract.company_id) + def test_contract_onchange_product_id_domain_blank(self): """It should return a blank UoM domain when no product.""" line = self.env['account.analytic.contract.line'].new() diff --git a/contract/views/account_analytic_account_view.xml b/contract/views/account_analytic_account_view.xml index c85a7d5c..bf9ef8d5 100644 --- a/contract/views/account_analytic_account_view.xml +++ b/contract/views/account_analytic_account_view.xml @@ -95,6 +95,54 @@ + + account.analytic.account.sale.form + account.analytic.account + + primary + + + + Customer + [('customer', '=', True)] + { + 'default_customer': True, + 'default_supplier': False + } + + + [('sale_ok', '=', True)] + + + sale + + + + + + account.analytic.account.purchase.form + account.analytic.account + + primary + + + + Supplier + [('supplier', '=', True)] + { + 'default_customer': False, + 'default_supplier': True + } + + + [('purchase_ok', '=', True)] + + + purchase + + + + Contract list @@ -154,12 +202,13 @@ - + Contracts account.analytic.account form tree,form - {'is_contract':1, 'search_default_not_finished':1, 'search_default_recurring_invoices':1, 'default_recurring_invoices': 1} + [('contract_type', '=', 'sale')] + {'is_contract':1, 'search_default_not_finished':1, 'search_default_recurring_invoices':1, 'default_recurring_invoices': 1, 'default_type': 'sale'}

@@ -168,24 +217,60 @@ - + tree - + - + form - - + + - + + + + Contracts + account.analytic.account + form + tree,form + [('contract_type', '=', 'purchase')] + {'is_contract':1, 'search_default_not_finished':1, 'search_default_recurring_invoices':1, 'default_recurring_invoices': 1, 'default_type': 'purchase'} + + +

+ Click to create a new contract. +

+
+
+ + + + tree + + + + + + + form + + + + +