Skip to content
Snippets Groups Projects
Commit a9875b03 authored by Yannick Tivisse's avatar Yannick Tivisse
Browse files

[IMP] sale: Only create analytic line on Sales Order

Before creating a account analytic line related to a sale order, check that the sale order is in 'sale' state, i.e. not a quotation, not in done or canceled state.

The issue is that when validating an expense (for example, but it's the same for a timesheet), the analytic account line is created even if related to a canceled sale order which is obviously wrong. This was leading to confusions about what's displayed, what is existing and not displayed and whatever.

This was the behavior before this commit 48ea59d4
parent eb440baa
Branches
Tags
No related merge requests found
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo import api, fields, models, _
from odoo.exceptions import UserError
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
......@@ -55,6 +55,7 @@ class AccountAnalyticLine(models.Model):
return price_unit
def _get_sale_order_line_vals(self, order, price):
last_so_line = self.env['sale.order.line'].search([('order_id', '=', order.id)], order='sequence desc', limit=1)
last_sequence = last_so_line.sequence + 1 if last_so_line else 100
......@@ -78,6 +79,12 @@ class AccountAnalyticLine(models.Model):
result = dict(vals or {})
so_line = result.get('so_line', False) or self.so_line
if not so_line and self.account_id and self.product_id and (self.product_id.expense_policy != 'no'):
order = self.env['sale.order'].search([('project_id', '=', self.account_id.id)], limit=1)
if not order:
return False
if order.state != 'sale':
raise UserError(_('The Sale Order %s linked to the Analytic Account must be validated before registering expenses.') % order.name)
order = self.env['sale.order'].search([('project_id', '=', self.account_id.id), ('state', '=', 'sale')], limit=1)
if not order:
return result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment