Files
tryton/modules/sale_opportunity/opportunity.py
T

676 lines
23 KiB
Python
Raw Normal View History

2014-12-02 12:55:56 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2010-07-05 23:16:33 +02:00
"Sales extension for managing leads and opportunities"
import datetime
from itertools import groupby
from sql.conditionals import Coalesce
from sql.functions import CharLength
2013-09-06 22:34:41 +02:00
2018-12-29 14:20:29 +01:00
from trytond.i18n import gettext
from trytond.ir.attachment import AttachmentCopyMixin
from trytond.ir.note import NoteCopyMixin
2021-12-11 17:58:44 +01:00
from trytond.model import (
2025-12-02 16:01:39 +01:00
ChatMixin, Index, ModelSQL, ModelView, Workflow, fields, sequence_ordered)
2021-12-11 17:58:44 +01:00
from trytond.model.exceptions import AccessError
2020-04-10 18:53:44 +02:00
from trytond.modules.company.model import employee_field, set_employee
2021-08-21 09:09:56 +02:00
from trytond.modules.currency.fields import Monetary
2021-12-11 17:58:44 +01:00
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, Get, If, In
from trytond.tools import firstline
2021-12-11 17:58:44 +01:00
from trytond.transaction import Transaction
2020-04-10 18:53:44 +02:00
2010-09-07 20:43:03 +02:00
class SaleOpportunity(
Workflow, ModelSQL, ModelView,
2025-12-02 16:01:39 +01:00
AttachmentCopyMixin, NoteCopyMixin, ChatMixin):
2012-09-24 22:18:14 +02:00
__name__ = "sale.opportunity"
2010-07-05 23:16:33 +02:00
_history = True
_rec_name = 'number'
_states_start = {
'readonly': Eval('state') != 'lead',
}
_states_stop = {
'readonly': Eval('state').in_(
['converted', 'won', 'lost', 'cancelled']),
}
number = fields.Char("Number", readonly=True, required=True)
reference = fields.Char("Reference")
party = fields.Many2One(
'party.party', "Party",
2014-01-26 14:29:33 +01:00
states={
2013-10-05 12:17:22 +02:00
'readonly': Eval('state').in_(['converted', 'lost', 'cancelled']),
'required': ~Eval('state').in_(['lead', 'lost', 'cancelled']),
},
context={
'company': Eval('company', -1),
},
depends={'company'})
2020-03-28 14:25:57 +01:00
contact = fields.Many2One(
'party.contact_mechanism', "Contact",
context={
'company': Eval('company', -1),
},
2020-03-28 14:25:57 +01:00
search_context={
'related_party': Eval('party'),
},
depends=['party', 'company'])
address = fields.Many2One(
'party.address', "Address", states=_states_stop,
domain=[('party', '=', Eval('party', -1))],
help="The default address for the invoice and shipment.\n"
"Leave empty to use the default values.")
company = fields.Many2One(
'company.company', "Company", required=True,
states={
'readonly': _states_stop['readonly'] | Eval('party', True),
},
domain=[
2011-08-04 16:57:40 +02:00
('id', If(In('company', Eval('context', {})), '=', '!='),
Get(Eval('context', {}), 'company', 0)),
])
2022-09-11 01:10:29 +02:00
currency = fields.Many2One(
'currency.currency', "Currency", required=True, states=_states_start)
2021-08-21 09:09:56 +02:00
amount = Monetary(
"Amount", currency='currency', digits='currency',
states=_states_stop,
2019-09-30 09:39:42 +02:00
help='Estimated revenue amount.')
2023-02-21 12:55:12 +01:00
payment_term = fields.Many2One(
'account.invoice.payment_term', "Payment Term", ondelete='RESTRICT',
states={
2011-08-04 16:57:40 +02:00
'readonly': In(Eval('state'),
['converted', 'lost', 'cancelled']),
})
2017-01-18 09:57:00 +01:00
employee = fields.Many2One('company.employee', 'Employee',
states={
'readonly': _states_stop['readonly'],
2017-01-18 09:57:00 +01:00
'required': ~Eval('state').in_(['lead', 'lost', 'cancelled']),
},
domain=[('company', '=', Eval('company', -1))])
start_date = fields.Date("Start Date", required=True, states=_states_start)
end_date = fields.Date("End Date", states=_states_stop)
description = fields.Char('Description', states=_states_stop)
comment = fields.Text('Comment', states=_states_stop)
2010-07-05 23:16:33 +02:00
lines = fields.One2Many('sale.opportunity.line', 'opportunity', 'Lines',
states=_states_stop)
conversion_probability = fields.Float('Conversion Probability',
digits=(1, 4), required=True,
domain=[
('conversion_probability', '>=', 0),
('conversion_probability', '<=', 1),
],
2015-03-02 23:13:04 +01:00
states={
'readonly': ~Eval('state').in_(
['opportunity', 'lead', 'converted']),
},
help="Percentage between 0 and 100.")
2010-07-05 23:16:33 +02:00
lost_reason = fields.Text('Reason for loss', states={
2011-08-21 11:20:52 +02:00
'invisible': Eval('state') != 'lost',
})
2015-03-02 23:13:04 +01:00
sales = fields.One2Many('sale.sale', 'origin', 'Sales')
2010-07-05 23:16:33 +02:00
2020-04-10 18:53:44 +02:00
converted_by = employee_field(
"Converted By", states=['converted', 'won', 'lost', 'cancelled'])
state = fields.Selection([
('lead', "Lead"),
('opportunity', "Opportunity"),
('converted', "Converted"),
('won', "Won"),
('lost', "Lost"),
2022-02-15 00:08:47 +01:00
('cancelled', "Cancelled"),
], "State", required=True, sort=False, readonly=True)
2020-04-10 18:53:44 +02:00
del _states_start
del _states_stop
2013-07-16 16:44:41 +02:00
@classmethod
def __register__(cls, module_name):
2015-03-02 23:13:04 +01:00
pool = Pool()
2022-09-11 01:10:29 +02:00
Company = pool.get('company.company')
transaction = Transaction()
cursor = transaction.connection.cursor()
2013-09-06 22:34:41 +02:00
sql_table = cls.__table__()
2022-09-11 01:10:29 +02:00
company = Company.__table__()
2013-09-06 22:34:41 +02:00
2018-07-24 22:01:08 +02:00
table = cls.__table_handler__(module_name)
2022-09-11 01:10:29 +02:00
currency_exists = table.column_exist('currency')
2025-04-04 00:45:18 +02:00
super().__register__(module_name)
2017-01-18 09:57:00 +01:00
2022-09-11 01:10:29 +02:00
# Migration from 6.4: store currency
if not currency_exists:
value = company.select(
company.currency,
where=(sql_table.company == company.id))
cursor.execute(*sql_table.update(
[sql_table.currency],
[value]))
2012-09-24 22:18:14 +02:00
@classmethod
def __setup__(cls):
cls.number.search_unaccented = False
cls.reference.search_unaccented = False
2025-04-04 00:45:18 +02:00
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(t, (Coalesce(t.reference, ''), Index.Similarity())),
2024-07-27 18:36:14 +02:00
Index(t, (t.party, Index.Range())),
Index(
t,
(t.start_date, Index.Range(order='DESC')),
(t.end_date, Index.Range(order='DESC'))),
Index(
2024-07-27 18:36:14 +02:00
t, (t.state, Index.Equality(cardinality='low')),
where=t.state.in_(['lead', 'opportunity'])),
})
2012-09-24 22:18:14 +02:00
cls._order.insert(0, ('start_date', 'DESC'))
cls._transitions |= set((
2012-03-25 00:18:14 +01:00
('lead', 'opportunity'),
('lead', 'lost'),
('lead', 'cancelled'),
2015-03-10 23:57:35 +01:00
('lead', 'converted'),
2012-03-25 00:18:14 +01:00
('opportunity', 'converted'),
('opportunity', 'lead'),
('opportunity', 'lost'),
('opportunity', 'cancelled'),
2015-03-02 23:13:04 +01:00
('converted', 'won'),
('converted', 'lost'),
('won', 'converted'),
('lost', 'converted'),
2012-03-25 00:18:14 +01:00
('lost', 'lead'),
('cancelled', 'lead'),
))
2012-09-24 22:18:14 +02:00
cls._buttons.update({
2012-03-25 00:18:14 +01:00
'lead': {
'invisible': ~Eval('state').in_(
['cancelled', 'lost', 'opportunity']),
'icon': If(Eval('state').in_(['cancelled', 'lost']),
2018-08-19 20:02:58 +02:00
'tryton-undo', 'tryton-back'),
2018-02-19 11:40:49 +01:00
'depends': ['state'],
2012-03-25 00:18:14 +01:00
},
'opportunity': {
2022-01-13 12:48:17 +01:00
'pre_validate': [
If(~Eval('party'),
('party', '!=', None),
()),
If(~Eval('employee'),
('employee', '!=', None),
()),
],
2012-03-25 00:18:14 +01:00
'invisible': ~Eval('state').in_(['lead']),
2018-02-19 11:40:49 +01:00
'depends': ['state'],
2012-03-25 00:18:14 +01:00
},
'convert': {
'invisible': ~Eval('state').in_(['opportunity']),
2018-02-19 11:40:49 +01:00
'depends': ['state'],
2012-03-25 00:18:14 +01:00
},
'lost': {
'invisible': ~Eval('state').in_(['lead', 'opportunity']),
2018-02-19 11:40:49 +01:00
'depends': ['state'],
2012-03-25 00:18:14 +01:00
},
'cancel': {
'invisible': ~Eval('state').in_(['lead', 'opportunity']),
2018-02-19 11:40:49 +01:00
'depends': ['state'],
2012-03-25 00:18:14 +01:00
},
})
2010-07-05 23:16:33 +02:00
@classmethod
def order_number(cls, tables):
table, _ = tables[None]
return [CharLength(table.number), table.number]
2012-09-24 22:18:14 +02:00
@staticmethod
def default_state():
2010-07-05 23:16:33 +02:00
return 'lead'
2012-09-24 22:18:14 +02:00
@staticmethod
def default_start_date():
Date = Pool().get('ir.date')
return Date.today()
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
@staticmethod
def default_conversion_probability():
return 0.5
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
@staticmethod
def default_company():
2012-03-27 10:43:59 +02:00
return Transaction().context.get('company')
2010-07-05 23:16:33 +02:00
2022-09-11 01:27:48 +02:00
@fields.depends('amount', 'company')
2022-09-11 01:21:17 +02:00
def on_change_company(self):
self.payment_term = self.default_payment_term(
company=self.company.id if self.company else None)
2022-09-11 01:27:48 +02:00
if not self.amount:
self.currency = self.default_currency(
company=self.company.id if self.company else None)
2022-09-11 01:21:17 +02:00
2022-09-11 01:10:29 +02:00
@classmethod
def default_currency(cls, **pattern):
pool = Pool()
Company = pool.get('company.company')
company = pattern.get('company')
if not company:
company = cls.default_company()
if company is not None and company >= 0:
2022-09-11 01:10:29 +02:00
return Company(company).currency.id
2012-09-24 22:18:14 +02:00
@staticmethod
def default_employee():
2016-09-22 12:25:16 +02:00
return Transaction().context.get('employee')
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
@classmethod
2022-09-11 01:21:17 +02:00
def default_payment_term(cls, **pattern):
pool = Pool()
Configuration = pool.get('account.configuration')
config = Configuration(1)
payment_term = config.get_multivalue(
'default_customer_payment_term', **pattern)
return payment_term.id if payment_term else None
2010-07-05 23:16:33 +02:00
def get_rec_name(self, name):
items = [self.number]
if self.reference:
items.append(f'[{self.reference}]')
return ' '.join(items)
@classmethod
def search_rec_name(cls, name, clause):
_, operator, value = clause
if operator.startswith('!') or operator.startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
domain = [bool_op,
('number', operator, value),
('reference', operator, value),
]
return domain
2019-09-20 11:47:15 +02:00
@classmethod
def view_attributes(cls):
2021-04-09 10:52:03 +02:00
return super().view_attributes() + [
2019-09-20 11:47:15 +02:00
('/tree', 'visual', If(Eval('state') == 'cancelled', 'muted', '')),
]
2025-12-02 16:01:39 +01:00
def chat_language(self, audience='internal'):
language = super().chat_language(audience=audience)
if audience == 'public':
language = self.party.lang.code if self.party.lang else None
return language
@classmethod
def get_resources_to_copy(cls, name):
return {
'sale.sale',
}
2013-07-16 16:44:41 +02:00
@classmethod
def preprocess_values(cls, mode, values):
2013-07-16 16:44:41 +02:00
pool = Pool()
Configuration = pool.get('sale.configuration')
values = super().preprocess_values(mode, values)
if mode == 'create' and not values.get('number'):
company_id = values.get('company', cls.default_company())
if company_id is not None:
configuration = Configuration(1)
if sequence := configuration.get_multivalue(
'sale_opportunity_sequence', company=company_id):
values['number'] = sequence.get()
return values
2013-07-16 16:44:41 +02:00
@classmethod
def check_modification(
cls, mode, opportunities, values=None, external=False):
super().check_modification(
mode, opportunities, values=values, external=external)
if mode == 'delete':
for opportunity in opportunities:
if opportunity.state not in {'cancelled', 'draft'}:
raise AccessError(gettext(
'sale_opportunity.msg_opportunity_delete_cancel',
opportunity=opportunity.rec_name))
2013-07-16 16:44:41 +02:00
@classmethod
def copy(cls, opportunities, default=None):
if default is None:
default = {}
2018-09-01 01:02:04 +02:00
else:
default = default.copy()
default.setdefault('number', None)
2025-08-01 00:10:49 +02:00
default.setdefault('reference')
2015-03-02 23:13:04 +01:00
default.setdefault('sales', None)
2021-04-05 16:24:13 +02:00
default.setdefault('converted_by')
2025-04-04 00:45:18 +02:00
return super().copy(opportunities, default=default)
2013-07-16 16:44:41 +02:00
2022-09-11 01:10:29 +02:00
@fields.depends('party', 'amount', 'company')
2012-09-24 22:18:14 +02:00
def on_change_party(self):
2022-09-11 01:21:17 +02:00
self.payment_term = self.default_payment_term(
company=self.company.id if self.company else None)
2022-09-11 01:10:29 +02:00
if self.party:
2022-09-11 01:21:17 +02:00
if self.party.customer_payment_term:
self.payment_term = self.party.customer_payment_term
2022-09-11 01:10:29 +02:00
if not self.amount:
if self.party.customer_currency:
self.currency = self.party.customer_currency
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
def _get_sale_opportunity(self):
2010-07-05 23:16:33 +02:00
'''
2012-09-24 22:18:14 +02:00
Return sale for an opportunity
2010-07-05 23:16:33 +02:00
'''
pool = Pool()
Sale = pool.get('sale.sale')
sale = Sale(
2014-09-17 21:45:52 +02:00
description=self.description,
party=self.party,
2020-03-28 14:25:57 +01:00
contact=self.contact,
2014-09-17 21:45:52 +02:00
company=self.company,
comment=self.comment,
sale_date=None,
2015-03-02 23:13:04 +01:00
origin=self,
warehouse=Sale.default_warehouse(),
2014-09-17 21:45:52 +02:00
)
sale.on_change_party()
if self.address:
sale.invoice_address = sale.shipment_address = self.address
if self.payment_term:
sale.payment_term = self.payment_term
sale.currency = self.currency
return sale
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
def create_sale(self):
2010-07-05 23:16:33 +02:00
'''
2012-09-24 22:18:14 +02:00
Create a sale for the opportunity and return the sale
2010-07-05 23:16:33 +02:00
'''
2012-09-24 22:18:14 +02:00
sale = self._get_sale_opportunity()
2015-03-02 23:13:04 +01:00
sale_lines = []
for line in self.lines:
sale_lines.append(line.get_sale_line(sale))
sale.lines = sale_lines
2012-09-24 22:18:14 +02:00
return sale
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
@classmethod
2012-03-25 00:18:14 +01:00
@ModelView.button
@Workflow.transition('lead')
2012-09-24 22:18:14 +02:00
def lead(cls, opportunities):
2012-03-25 00:18:14 +01:00
pass
2012-09-24 22:18:14 +02:00
@classmethod
2012-03-25 00:18:14 +01:00
@ModelView.button
@Workflow.transition('opportunity')
2012-09-24 22:18:14 +02:00
def opportunity(cls, opportunities):
2012-03-25 00:18:14 +01:00
pass
2012-09-24 22:18:14 +02:00
@classmethod
2023-11-05 00:02:18 +01:00
@ModelView.button_action('sale.act_sale_form')
2012-03-25 00:18:14 +01:00
@Workflow.transition('converted')
2020-04-10 18:53:44 +02:00
@set_employee('converted_by')
2012-09-24 22:18:14 +02:00
def convert(cls, opportunities):
2015-03-02 23:13:04 +01:00
pool = Pool()
Sale = pool.get('sale.sale')
sales = [o.create_sale() for o in opportunities if not o.sales]
Sale.save(sales)
for sale in sales:
sale.origin.copy_resources_to(sale)
2023-11-05 00:02:18 +01:00
return {
'res_id': [s.id for s in sales],
}
2015-03-02 23:13:04 +01:00
2015-03-10 23:53:36 +01:00
@property
def is_forecast(self):
pool = Pool()
Date = pool.get('ir.date')
with Transaction().set_context(company=self.company.id):
today = Date.today()
2015-03-10 23:53:36 +01:00
return self.end_date or datetime.date.max > today
2015-03-02 23:13:04 +01:00
@classmethod
@Workflow.transition('won')
def won(cls, opportunities):
pool = Pool()
Date = pool.get('ir.date')
for company, c_opportunities in groupby(
opportunities, key=lambda o: o.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([o for o in c_opportunities if o.is_forecast], {
'end_date': today,
'state': 'won',
})
2012-09-24 22:18:14 +02:00
@classmethod
2012-03-25 00:18:14 +01:00
@ModelView.button
@Workflow.transition('lost')
2012-09-24 22:18:14 +02:00
def lost(cls, opportunities):
pool = Pool()
Date = pool.get('ir.date')
for company, c_opportunities in groupby(
opportunities, key=lambda o: o.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([o for o in c_opportunities if o.is_forecast], {
'end_date': today,
'state': 'lost',
})
2012-09-24 22:18:14 +02:00
@classmethod
2012-03-25 00:18:14 +01:00
@ModelView.button
@Workflow.transition('cancelled')
2012-09-24 22:18:14 +02:00
def cancel(cls, opportunities):
pool = Pool()
Date = pool.get('ir.date')
for company, c_opportunities in groupby(
opportunities, key=lambda o: o.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([o for o in c_opportunities if o.is_forecast], {
'end_date': today,
'state': 'cancelled',
})
2015-03-02 23:13:04 +01:00
@staticmethod
def _sale_won_states():
return ['confirmed', 'processing', 'done']
@staticmethod
def _sale_lost_states():
2020-06-06 10:04:28 +01:00
return ['cancelled']
2015-03-02 23:13:04 +01:00
def is_won(self):
sale_won_states = self._sale_won_states()
sale_lost_states = self._sale_lost_states()
end_states = sale_won_states + sale_lost_states
return (self.sales
and all(s.state in end_states for s in self.sales)
and any(s.state in sale_won_states for s in self.sales))
def is_lost(self):
sale_lost_states = self._sale_lost_states()
return (self.sales
and all(s.state in sale_lost_states for s in self.sales))
@property
def sale_amount(self):
pool = Pool()
Currency = pool.get('currency.currency')
if not self.sales:
return
sale_lost_states = self._sale_lost_states()
amount = 0
for sale in self.sales:
if sale.state not in sale_lost_states:
amount += Currency.compute(sale.currency, sale.untaxed_amount,
self.currency)
return amount
@classmethod
def process(cls, opportunities):
won = []
lost = []
converted = []
for opportunity in opportunities:
2015-06-28 22:59:02 +02:00
sale_amount = opportunity.sale_amount
if opportunity.amount != sale_amount:
opportunity.amount = sale_amount
2015-03-02 23:13:04 +01:00
if opportunity.is_won():
won.append(opportunity)
elif opportunity.is_lost():
lost.append(opportunity)
2015-03-10 23:57:35 +01:00
elif (opportunity.state != 'converted'
2015-03-02 23:13:04 +01:00
and opportunity.sales):
converted.append(opportunity)
cls.save(opportunities)
if won:
cls.won(won)
if lost:
cls.lost(lost)
if converted:
cls.convert(converted)
2010-07-05 23:16:33 +02:00
2016-09-13 14:09:46 +02:00
class SaleOpportunityLine(sequence_ordered(), ModelSQL, ModelView):
2012-09-24 22:18:14 +02:00
__name__ = "sale.opportunity.line"
2010-07-05 23:16:33 +02:00
_history = True
2016-06-30 18:53:13 +02:00
_states = {
'readonly': Eval('opportunity_state').in_(
['converted', 'won', 'lost', 'cancelled']),
}
opportunity = fields.Many2One(
'sale.opportunity', "Opportunity", ondelete='CASCADE', required=True,
2016-06-30 18:53:13 +02:00
states={
'readonly': _states['readonly'] & Bool(Eval('opportunity')),
})
2016-06-30 18:53:13 +02:00
opportunity_state = fields.Function(
fields.Selection('get_opportunity_states', "Opportunity State"),
2016-06-30 18:53:13 +02:00
'on_change_with_opportunity_state')
2024-03-23 22:52:15 +01:00
product = fields.Many2One(
'product.product', "Product",
domain=[
If(Eval('opportunity_state').in_(['lead', 'opportunity'])
& ~(Eval('quantity', 0) < 0),
('salable', '=', True),
()),
],
states=_states,
context={
'company': Eval('company', None),
},
depends=['company'])
product_uom_category = fields.Function(
fields.Many2One(
'product.uom.category', "Product UoM Category"),
'on_change_with_product_uom_category')
2021-08-30 00:27:13 +02:00
quantity = fields.Float(
"Quantity", digits='unit', required=True, states=_states)
2024-03-23 22:52:15 +01:00
unit = fields.Many2One(
'product.uom', "Unit",
domain=[
If(Eval('product_uom_category'),
('category', '=', Eval('product_uom_category', -1)),
('category', '=', -1)),
],
2024-03-23 22:52:15 +01:00
states={
'required': Bool(Eval('product')),
'readonly': _states['readonly'],
})
description = fields.Text("Description", states=_states)
2024-07-18 10:12:04 +02:00
summary = fields.Function(
fields.Char("Summary"), 'on_change_with_summary',
searcher='search_summary')
note = fields.Text("Note")
2010-07-05 23:16:33 +02:00
company = fields.Function(
fields.Many2One('company.company', "Company"),
'on_change_with_company')
del _states
2016-06-30 18:53:13 +02:00
2021-03-07 11:13:53 +01:00
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('opportunity')
2024-03-23 22:52:15 +01:00
@classmethod
def __register__(cls, module):
table_h = cls.__table_handler__(module)
super().__register__(module)
# Migration from 7.0: remove required on product and unit
table_h.not_null_action('product', 'remove')
table_h.not_null_action('unit', 'remove')
@classmethod
def get_opportunity_states(cls):
pool = Pool()
Opportunity = pool.get('sale.opportunity')
return Opportunity.fields_get(['state'])['state']['selection']
2016-06-30 18:53:13 +02:00
@fields.depends('opportunity', '_parent_opportunity.state')
def on_change_with_opportunity_state(self, name=None):
if self.opportunity:
return self.opportunity.state
2014-01-26 14:29:33 +01:00
@fields.depends('product', 'unit')
2012-09-24 22:18:14 +02:00
def on_change_product(self):
if not self.product:
2014-10-29 20:40:17 +01:00
return
2010-07-05 23:16:33 +02:00
2012-09-24 22:18:14 +02:00
category = self.product.sale_uom.category
if not self.unit or self.unit.category != category:
2014-10-29 20:40:17 +01:00
self.unit = self.product.sale_uom
2010-07-05 23:16:33 +02:00
@fields.depends('product')
def on_change_with_product_uom_category(self, name=None):
return self.product.default_uom_category if self.product else None
@fields.depends('description')
def on_change_with_summary(self, name=None):
return firstline(self.description or '')
2024-07-18 10:12:04 +02:00
@classmethod
def search_summary(cls, name, clause):
return [('description', *clause[1:])]
@fields.depends('opportunity', '_parent_opportunity.company')
def on_change_with_company(self, name=None):
return self.opportunity.company if self.opportunity else None
2012-09-24 22:18:14 +02:00
def get_sale_line(self, sale):
2010-07-05 23:16:33 +02:00
'''
2012-09-24 22:18:14 +02:00
Return sale line for opportunity line
2010-07-05 23:16:33 +02:00
'''
2012-09-24 22:18:14 +02:00
SaleLine = Pool().get('sale.line')
2014-09-17 21:45:52 +02:00
sale_line = SaleLine(
type='line',
product=self.product,
sale=sale,
description=self.description,
2014-09-17 21:45:52 +02:00
)
2015-03-02 23:13:04 +01:00
sale_line.on_change_product()
self._set_sale_line_quantity(sale_line)
sale_line.on_change_quantity()
2012-09-24 22:18:14 +02:00
return sale_line
2010-07-05 23:16:33 +02:00
def _set_sale_line_quantity(self, sale_line):
sale_line.quantity = self.quantity
sale_line.unit = self.unit
2017-07-26 23:43:26 +02:00
def get_rec_name(self, name):
2019-11-26 22:20:24 +01:00
pool = Pool()
Lang = pool.get('ir.lang')
lang = Lang.get()
2024-03-23 22:52:15 +01:00
if self.product:
return (lang.format_number_symbol(
self.quantity or 0, self.unit, digits=self.unit.digits)
+ ' %s @ %s' % (
self.product.rec_name, self.opportunity.rec_name))
else:
return self.opportunity.rec_name
2017-07-26 23:43:26 +02:00
@classmethod
def search_rec_name(cls, name, clause):
2017-09-27 18:37:32 +02:00
return [('product.rec_name',) + tuple(clause[1:])]