Skip to content

Commit

Permalink
Merge 7468b4c into cb8d77a
Browse files Browse the repository at this point in the history
  • Loading branch information
funbaker committed Apr 2, 2018
2 parents cb8d77a + 7468b4c commit 030245e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
33 changes: 25 additions & 8 deletions contract/models/contract.py
Expand Up @@ -102,6 +102,11 @@ def _default_journal(self):
('company_id', '=', company_id)]
return self.env['account.journal'].search(domain, limit=1)

contract_type = fields.Selection(
selection=[
('sale', _('Sale'))
], default='sale'
)
pricelist_id = fields.Many2one(
comodel_name='product.pricelist',
string='Pricelist')
Expand Down Expand Up @@ -142,14 +147,22 @@ def _default_journal(self):
'account.journal',
string='Journal',
default=_default_journal,
domain="[('type', '=', 'sale'),('company_id', '=', company_id)]")
domain="[('company_id', '=', company_id)]")
user_id = fields.Many2one(
comodel_name='res.users',
string='Responsible',
index=True,
default=lambda self: self.env.user,
)

@api.onchange('contract_type')
def _onchange_contract_type(self):
if self.contract_type == 'sale':
self.journal_id = self.env['account.journal'].search([
('type', '=', 'sale'),
('company_id', '=', self.company_id.id)
], limit=1)

@api.onchange('partner_id')
def _onchange_partner_id(self):
self.pricelist_id = self.partner_id.property_product_pricelist.id
Expand Down Expand Up @@ -220,7 +233,7 @@ def _prepare_invoice_line(self, line, invoice_id):
return invoice_line_vals

@api.multi
def _prepare_invoice(self):
def _prepare_invoice(self, journal=None):
"""Prepare the values for the invoice creation from the contract(s)
given. It's possible to provide several contracts. Only one invoice
will be created and most of the values will be taken from first
Expand All @@ -236,14 +249,18 @@ def _prepare_invoice(self):
raise ValidationError(
_("You must first select a Customer for Contract %s!") %
contract.name)
journal = contract.journal_id or self.env['account.journal'].search(
[('type', '=', 'sale'),
('company_id', '=', contract.company_id.id)],
limit=1)

if not journal:
journal = contract.journal_id or self.env[
'account.journal'].search([
('type', '=', contract.contract_type),
('company_id', '=', contract.company_id.id)
], limit=1)

if not journal:
raise ValidationError(
_("Please define a sale journal for the company '%s'.") %
(contract.company_id.name or '',))
_("Please define a %s journal for the company '%s'.") %
(contract.contract_type, contract.company_id.name or '',))
currency = (
contract.pricelist_id.currency_id or
contract.partner_id.property_product_pricelist.currency_id or
Expand Down
6 changes: 6 additions & 0 deletions contract/tests/test_contract.py
Expand Up @@ -144,3 +144,9 @@ def test_check_journal(self):
def test_send_mail_contract(self):
result = self.contract.action_contract_send()
self.assertEqual(result['name'], 'Compose Email')

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)
14 changes: 12 additions & 2 deletions contract/views/contract.xml
Expand Up @@ -26,6 +26,16 @@
<button name="action_contract_send" type="object" string="Send by Email" groups="base.group_user"/>
</header>
</xpath>
<xpath expr='//field[@name="code"]' position='before'>
<field name="contract_type"/>
</xpath>
<field name="partner_id" position="attributes">
<attribute name="domain">[('customer', '=', True)]</attribute>
<attribute name="context">{
'default_customer': True,
'default_supplier': False
}</attribute>
</field>
<notebook position="before">
<separator string="Recurring Invoices" attrs="{'invisible': [('recurring_invoices','!=',True)]}"/>
<div>
Expand Down Expand Up @@ -55,7 +65,7 @@
<div attrs="{'invisible': [('recurring_invoices','=',False)]}">
<field name="recurring_invoice_line_ids">
<tree string="Account Analytic Lines" editable="bottom">
<field name="product_id"/>
<field name="product_id" domain="[('sale_ok', '=', True)]"/>
<field name="name"/>
<field name="quantity"/>
<field name="uom_id"/>
Expand Down Expand Up @@ -113,7 +123,7 @@
<field name="res_model">account.analytic.account</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="domain"/> <!-- This is for resetting the field when migrating from v8 -->
<field name="domain">[('contract_type', '=', 'sale')]</field>
<field name="context">{'search_default_active':1, 'search_default_recurring_invoices':1}</field>
<field name="search_view_id" ref="analytic.view_account_analytic_account_search"/>
<field name="help" type="html">
Expand Down

0 comments on commit 030245e

Please sign in to comment.