diff --git a/CHANGELOG b/CHANGELOG index e54225d7f4..560320cef0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Make Opportunity employee not required on leads + Version 4.2.0 - 2016-11-28 * Bug fixes (see mercurial logs for details) * Change probability into conversion probability diff --git a/opportunity.py b/opportunity.py index d59f3afebe..4384d2aa8a 100644 --- a/opportunity.py +++ b/opportunity.py @@ -5,7 +5,7 @@ import datetime from sql import Literal, Null from sql.aggregate import Max, Count, Sum -from sql.conditionals import Case +from sql.conditionals import Case, Coalesce from sql.functions import Extract from trytond.model import ModelView, ModelSQL, Workflow, fields, \ @@ -71,9 +71,13 @@ class SaleOpportunity(Workflow, ModelSQL, ModelView): ['converted', 'lost', 'cancelled']), }, depends=['state']) - employee = fields.Many2One('company.employee', 'Employee', required=True, - states=_STATES_STOP, depends=['state', 'company'], - domain=[('company', '=', Eval('company'))]) + employee = fields.Many2One('company.employee', 'Employee', + states={ + 'readonly': _STATES_STOP['readonly'], + 'required': ~Eval('state').in_(['lead', 'lost', 'cancelled']), + }, + depends=['state', 'company'], + domain=[('company', '=', Eval('company'))]) start_date = fields.Date('Start Date', required=True, select=True, states=_STATES_START, depends=_DEPENDS_START) end_date = fields.Date('End Date', select=True, @@ -152,6 +156,9 @@ class SaleOpportunity(Workflow, ModelSQL, ModelView): table.drop_constraint('check_percentage') table.drop_column('probability') + # Migration from 4.2: make employee not required + table.not_null_action('employee', action='remove') + @classmethod def __setup__(cls): super(SaleOpportunity, cls).__setup__() @@ -609,7 +616,7 @@ class SaleOpportunityEmployee(SaleOpportunityReportMixin, ModelSQL, ModelView): query = super(SaleOpportunityEmployee, cls).table_query() opportunity, = query.from_ query.columns += ( - opportunity.employee.as_('id'), + Coalesce(opportunity.employee, 0).as_('id'), opportunity.employee, ) where = Literal(True) @@ -694,7 +701,7 @@ class SaleOpportunityEmployeeMonthly( query.columns += ( Max(Extract('MONTH', opportunity.start_date) + Extract('YEAR', opportunity.start_date) * 100 - + opportunity.employee * 1000000 + + Coalesce(opportunity.employee, 0) * 1000000 ).cast(type_id).as_('id'), year_column, month_column, diff --git a/tests/scenario_sale_opportunity.rst b/tests/scenario_sale_opportunity.rst index 203a1ea0d4..b78741f09f 100644 --- a/tests/scenario_sale_opportunity.rst +++ b/tests/scenario_sale_opportunity.rst @@ -98,7 +98,6 @@ Create an lead:: >>> config.user = sale_opportunity_user.id >>> Opportunity = Model.get('sale.opportunity') >>> opportunity = Opportunity() - >>> opportunity.employee = employee >>> opportunity.description = 'Opportunity' >>> opportunity.save() >>> opportunity.state @@ -110,6 +109,7 @@ Convert to opportunity:: >>> opportunity.address, = customer.addresses >>> opportunity.payment_term = payment_term >>> opportunity.amount = Decimal(100) + >>> opportunity.employee = employee >>> opportunity.click('opportunity') >>> opportunity.state u'opportunity'