Browse Source

[IMP] Add type to analytic account / contract

pull/202/head
Stefan Becker 7 years ago
committed by Pedro M. Baeza
parent
commit
3cc3c00452
  1. 21
      contract/models/account_analytic_account.py
  2. 17
      contract/models/account_analytic_contract.py
  3. 6
      contract/tests/test_contract.py
  4. 103
      contract/views/account_analytic_account_view.xml

21
contract/models/account_analytic_account.py

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

17
contract/models/account_analytic_contract.py

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

6
contract/tests/test_contract.py

@ -233,6 +233,12 @@ class TestContract(TestContractBase):
result = self.contract.action_contract_send() result = self.contract.action_contract_send()
self.assertEqual(result['res_model'], 'mail.compose.message') 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): def test_contract_onchange_product_id_domain_blank(self):
"""It should return a blank UoM domain when no product.""" """It should return a blank UoM domain when no product."""
line = self.env['account.analytic.contract.line'].new() line = self.env['account.analytic.contract.line'].new()

103
contract/views/account_analytic_account_view.xml

@ -95,6 +95,54 @@
</field> </field>
</record> </record>
<record id="account_analytic_account_sale_form" model="ir.ui.view">
<field name="name">account.analytic.account.sale.form</field>
<field name="model">account.analytic.account</field>
<field name="inherit_id" ref="contract.account_analytic_account_recurring_form_form"/>
<field name="mode">primary</field>
<field name="priority" eval="20"/>
<field name="arch" type="xml">
<field name="partner_id" position="attributes">
<attribute name="string">Customer</attribute>
<attribute name="domain">[('customer', '=', True)]</attribute>
<attribute name="context">{
'default_customer': True,
'default_supplier': False
}</attribute>
</field>
<field name="product_id" position="attributes">
<attribute name="domain">[('sale_ok', '=', True)]</attribute>
</field>
<xpath expr='//field[@name="code"]' position='before'>
<field name="contract_type">sale</field>
</xpath>
</field>
</record>
<record id="account_analytic_account_purchase_form" model="ir.ui.view">
<field name="name">account.analytic.account.purchase.form</field>
<field name="model">account.analytic.account</field>
<field name="inherit_id" ref="contract.account_analytic_account_recurring_form_form"/>
<field name="mode">primary</field>
<field name="priority" eval="20"/>
<field name="arch" type="xml">
<field name="partner_id" position="attributes">
<attribute name="string">Supplier</attribute>
<attribute name="domain">[('supplier', '=', True)]</attribute>
<attribute name="context">{
'default_customer': False,
'default_supplier': True
}</attribute>
</field>
<field name="product_id" position="attributes">
<attribute name="domain">[('purchase_ok', '=', True)]</attribute>
</field>
<xpath expr='//field[@name="code"]' position='before'>
<field name="contract_type">purchase</field>
</xpath>
</field>
</record>
<!-- Inherited Analytic Account list for contracts --> <!-- Inherited Analytic Account list for contracts -->
<record id="view_account_analytic_account_journal_tree" model="ir.ui.view"> <record id="view_account_analytic_account_journal_tree" model="ir.ui.view">
<field name="name">Contract list</field> <field name="name">Contract list</field>
@ -154,12 +202,13 @@
</record> </record>
<!-- Action Sales/Sales/Contracts --> <!-- Action Sales/Sales/Contracts -->
<record id="action_account_analytic_overdue_all" model="ir.actions.act_window">
<record id="action_account_analytic_sale_overdue_all" model="ir.actions.act_window">
<field name="name">Contracts</field> <field name="name">Contracts</field>
<field name="res_model">account.analytic.account</field> <field name="res_model">account.analytic.account</field>
<field name="view_type">form</field> <field name="view_type">form</field>
<field name="view_mode">tree,form</field> <field name="view_mode">tree,form</field>
<field name="context">{'is_contract':1, 'search_default_not_finished':1, 'search_default_recurring_invoices':1, 'default_recurring_invoices': 1}</field>
<field name="domain">[('contract_type', '=', 'sale')]</field>
<field name="context">{'is_contract':1, 'search_default_not_finished':1, 'search_default_recurring_invoices':1, 'default_recurring_invoices': 1, 'default_type': 'sale'}</field>
<field name="search_view_id" ref="analytic.view_account_analytic_account_search"/> <field name="search_view_id" ref="analytic.view_account_analytic_account_search"/>
<field name="help" type="html"> <field name="help" type="html">
<p class="oe_view_nocontent_create"> <p class="oe_view_nocontent_create">
@ -168,23 +217,59 @@
</field> </field>
</record> </record>
<record id="action_account_analytic_overdue_all_tree" model="ir.actions.act_window.view">
<record id="action_account_analytic_sale_overdue_all_tree" model="ir.actions.act_window.view">
<field name="sequence" eval="1"/> <field name="sequence" eval="1"/>
<field name="view_mode">tree</field> <field name="view_mode">tree</field>
<field name="view_id" ref="view_account_analytic_account_journal_tree"/> <field name="view_id" ref="view_account_analytic_account_journal_tree"/>
<field name="act_window_id" ref="action_account_analytic_overdue_all"/>
<field name="act_window_id" ref="action_account_analytic_sale_overdue_all"/>
</record> </record>
<record id="action_account_analytic_overdue_all_form" model="ir.actions.act_window.view">
<record id="action_account_analytic_sale_overdue_all_form" model="ir.actions.act_window.view">
<field name="sequence" eval="2"/> <field name="sequence" eval="2"/>
<field name="view_mode">form</field> <field name="view_mode">form</field>
<field name="view_id" ref="account_analytic_account_recurring_form_form"/>
<field name="act_window_id" ref="action_account_analytic_overdue_all"/>
<field name="view_id" ref="account_analytic_account_sale_form"/>
<field name="act_window_id" ref="action_account_analytic_sale_overdue_all"/>
</record> </record>
<menuitem id="menu_action_account_analytic_overdue_all"
<menuitem id="menu_action_account_analytic_sale_overdue_all"
parent="account.menu_finance_receivables_documents" parent="account.menu_finance_receivables_documents"
action="action_account_analytic_overdue_all"
action="action_account_analytic_sale_overdue_all"
sequence="99"
/>
<!-- Action Purchases/Purchases/Contracts -->
<record id="action_account_analytic_purchase_overdue_all" model="ir.actions.act_window">
<field name="name">Contracts</field>
<field name="res_model">account.analytic.account</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('contract_type', '=', 'purchase')]</field>
<field name="context">{'is_contract':1, 'search_default_not_finished':1, 'search_default_recurring_invoices':1, 'default_recurring_invoices': 1, 'default_type': 'purchase'}</field>
<field name="search_view_id" ref="analytic.view_account_analytic_account_search"/>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to create a new contract.
</p>
</field>
</record>
<record id="action_account_analytic_purchase_overdue_all_tree" model="ir.actions.act_window.view">
<field name="sequence" eval="1"/>
<field name="view_mode">tree</field>
<field name="view_id" ref="view_account_analytic_account_journal_tree"/>
<field name="act_window_id" ref="action_account_analytic_purchase_overdue_all"/>
</record>
<record id="action_account_analytic_purchase_overdue_all_form" model="ir.actions.act_window.view">
<field name="sequence" eval="2"/>
<field name="view_mode">form</field>
<field name="view_id" ref="account_analytic_account_purchase_form"/>
<field name="act_window_id" ref="action_account_analytic_purchase_overdue_all"/>
</record>
<menuitem id="menu_action_account_analytic_purchase_overdue_all"
parent="account.menu_finance_payables_documents"
action="action_account_analytic_purchase_overdue_all"
sequence="99" sequence="99"
/> />

Loading…
Cancel
Save