mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
[IMP] Make sure it works for sale and purchase contracts
This commit is contained in:
committed by
Pedro M. Baeza
parent
3cc3c00452
commit
669249ad35
@@ -202,14 +202,19 @@ class AccountAnalyticAccount(models.Model):
|
||||
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)
|
||||
if self.contract_type == 'purchase':
|
||||
raise ValidationError(
|
||||
_("You must first select a Supplier for Contract %s!") %
|
||||
self.name)
|
||||
else:
|
||||
raise ValidationError(
|
||||
_("You must first select a Customer for Contract %s!") %
|
||||
self.name)
|
||||
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)
|
||||
('type', '=', self.contract_type),
|
||||
('company_id', '=', self.company_id.id)
|
||||
], limit=1)
|
||||
if not journal:
|
||||
raise ValidationError(
|
||||
_("Please define a %s journal for the company '%s'.") %
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2004-2010 OpenERP SA
|
||||
# Copyright 2014 Angel Moya <angel.moya@domatix.com>
|
||||
# Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
@@ -6,7 +5,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):
|
||||
@@ -28,9 +27,9 @@ class AccountAnalyticContract(models.Model):
|
||||
)
|
||||
contract_type = fields.Selection(
|
||||
selection=[
|
||||
('sale', _('Sale')),
|
||||
('purchase', _('Purchase')),
|
||||
], default='sale'
|
||||
('sale', 'Customer'),
|
||||
('purchase', 'Supplier'),
|
||||
], default='sale',
|
||||
)
|
||||
pricelist_id = fields.Many2one(
|
||||
comodel_name='product.pricelist',
|
||||
@@ -70,7 +69,8 @@ class AccountAnalyticContract(models.Model):
|
||||
'account.journal',
|
||||
string='Journal',
|
||||
default=lambda s: s._default_journal(),
|
||||
domain="[('company_id', '=', company_id)]",
|
||||
domain="[('type', '=', contract_type),"
|
||||
"('company_id', '=', company_id)]",
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
'res.company',
|
||||
@@ -81,6 +81,9 @@ class AccountAnalyticContract(models.Model):
|
||||
|
||||
@api.onchange('contract_type')
|
||||
def _onchange_contract_type(self):
|
||||
if self.contract_type == 'purchase':
|
||||
self.recurring_invoice_line_ids.filtered('automatic_price').update(
|
||||
{'automatic_price': False})
|
||||
self.journal_id = self.env['account.journal'].search([
|
||||
('type', '=', self.contract_type),
|
||||
('company_id', '=', self.company_id.id)
|
||||
@@ -91,6 +94,6 @@ class AccountAnalyticContract(models.Model):
|
||||
company_id = self.env.context.get(
|
||||
'company_id', self.env.user.company_id.id)
|
||||
domain = [
|
||||
('type', '=', 'sale'),
|
||||
('type', '=', self.contract_type),
|
||||
('company_id', '=', company_id)]
|
||||
return self.env['account.journal'].search(domain, limit=1)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2004-2010 OpenERP SA
|
||||
# Copyright 2014 Angel Moya <angel.moya@domatix.com>
|
||||
# Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
|
||||
|
||||
@@ -9,7 +9,7 @@ class AccountAnalyticInvoiceLine(models.Model):
|
||||
_inherit = 'account.analytic.contract.line'
|
||||
|
||||
analytic_account_id = fields.Many2one(
|
||||
'account.analytic.account',
|
||||
comodel_name='account.analytic.account',
|
||||
string='Analytic Account',
|
||||
required=True,
|
||||
ondelete='cascade',
|
||||
|
||||
@@ -7,30 +7,45 @@ from odoo import fields, models
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
contract_count = fields.Integer(
|
||||
string='Contracts',
|
||||
sale_contract_count = fields.Integer(
|
||||
string='Sale Contracts',
|
||||
compute='_compute_contract_count',
|
||||
)
|
||||
purchase_contract_count = fields.Integer(
|
||||
string='Purchase Contracts',
|
||||
compute='_compute_contract_count',
|
||||
)
|
||||
|
||||
def _compute_contract_count(self):
|
||||
Contract = self.env['account.analytic.account']
|
||||
contract_model = self.env['account.analytic.account']
|
||||
today = fields.Date.today()
|
||||
fetch_data = contract_model.read_group([
|
||||
('recurring_invoices', '=', True),
|
||||
('partner_id', 'child_of', self.ids),
|
||||
'|',
|
||||
('date_end', '=', False),
|
||||
('date_end', '>=', today)],
|
||||
['partner_id', 'contract_type'], ['partner_id', 'contract_type'],
|
||||
lazy=False)
|
||||
result = [[data['partner_id'][0], data['contract_type'],
|
||||
data['__count']] for data in fetch_data]
|
||||
for partner in self:
|
||||
partner.contract_count = Contract.search_count([
|
||||
('recurring_invoices', '=', True),
|
||||
('partner_id', 'child_of', partner.ids),
|
||||
'|',
|
||||
('date_end', '=', False),
|
||||
('date_end', '>=', today),
|
||||
])
|
||||
partner_child_ids = partner.child_ids.ids + partner.ids
|
||||
partner.sale_contract_count = sum([
|
||||
r[2] for r in result
|
||||
if r[0] in partner_child_ids and r[1] == 'sale'])
|
||||
partner.purchase_contract_count = sum([
|
||||
r[2] for r in result
|
||||
if r[0] in partner_child_ids and r[1] == 'purchase'])
|
||||
|
||||
def act_show_contract(self):
|
||||
""" This opens contract view
|
||||
@return: the contract view
|
||||
"""
|
||||
self.ensure_one()
|
||||
res = self.env['ir.actions.act_window'].for_xml_id(
|
||||
'contract', 'action_account_analytic_overdue_all')
|
||||
contract_type = self._context.get('contract_type')
|
||||
|
||||
res = self._get_act_window_contract_xml(contract_type)
|
||||
res.update(
|
||||
context=dict(
|
||||
self.env.context,
|
||||
@@ -43,3 +58,11 @@ class ResPartner(models.Model):
|
||||
),
|
||||
)
|
||||
return res
|
||||
|
||||
def _get_act_window_contract_xml(self, contract_type):
|
||||
if contract_type == 'purchase':
|
||||
return self.env['ir.actions.act_window'].for_xml_id(
|
||||
'contract', 'action_account_analytic_purchase_overdue_all')
|
||||
else:
|
||||
return self.env['ir.actions.act_window'].for_xml_id(
|
||||
'contract', 'action_account_analytic_sale_overdue_all')
|
||||
|
||||
Reference in New Issue
Block a user