summaryrefslogtreecommitdiff
path: root/addons/sale_crm/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/sale_crm/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/sale_crm/models')
-rw-r--r--addons/sale_crm/models/__init__.py7
-rw-r--r--addons/sale_crm/models/crm_lead.py85
-rw-r--r--addons/sale_crm/models/crm_team.py44
-rw-r--r--addons/sale_crm/models/res_users.py10
-rw-r--r--addons/sale_crm/models/sale_order.py15
5 files changed, 161 insertions, 0 deletions
diff --git a/addons/sale_crm/models/__init__.py b/addons/sale_crm/models/__init__.py
new file mode 100644
index 00000000..3ae8003c
--- /dev/null
+++ b/addons/sale_crm/models/__init__.py
@@ -0,0 +1,7 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import crm_lead
+from . import crm_team
+from . import res_users
+from . import sale_order
diff --git a/addons/sale_crm/models/crm_lead.py b/addons/sale_crm/models/crm_lead.py
new file mode 100644
index 00000000..59439949
--- /dev/null
+++ b/addons/sale_crm/models/crm_lead.py
@@ -0,0 +1,85 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from dateutil.relativedelta import relativedelta
+
+from odoo import api, fields, models
+
+
+class CrmLead(models.Model):
+ _inherit = 'crm.lead'
+
+ sale_amount_total = fields.Monetary(compute='_compute_sale_data', string="Sum of Orders", help="Untaxed Total of Confirmed Orders", currency_field='company_currency')
+ quotation_count = fields.Integer(compute='_compute_sale_data', string="Number of Quotations")
+ sale_order_count = fields.Integer(compute='_compute_sale_data', string="Number of Sale Orders")
+ order_ids = fields.One2many('sale.order', 'opportunity_id', string='Orders')
+
+ @api.depends('order_ids.state', 'order_ids.currency_id', 'order_ids.amount_untaxed', 'order_ids.date_order', 'order_ids.company_id')
+ def _compute_sale_data(self):
+ for lead in self:
+ total = 0.0
+ quotation_cnt = 0
+ sale_order_cnt = 0
+ company_currency = lead.company_currency or self.env.company.currency_id
+ for order in lead.order_ids:
+ if order.state in ('draft', 'sent'):
+ quotation_cnt += 1
+ if order.state not in ('draft', 'sent', 'cancel'):
+ sale_order_cnt += 1
+ total += order.currency_id._convert(
+ order.amount_untaxed, company_currency, order.company_id, order.date_order or fields.Date.today())
+ lead.sale_amount_total = total
+ lead.quotation_count = quotation_cnt
+ lead.sale_order_count = sale_order_cnt
+
+ def action_sale_quotations_new(self):
+ if not self.partner_id:
+ return self.env["ir.actions.actions"]._for_xml_id("sale_crm.crm_quotation_partner_action")
+ else:
+ return self.action_new_quotation()
+
+ def action_new_quotation(self):
+ action = self.env["ir.actions.actions"]._for_xml_id("sale_crm.sale_action_quotations_new")
+ action['context'] = {
+ 'search_default_opportunity_id': self.id,
+ 'default_opportunity_id': self.id,
+ 'search_default_partner_id': self.partner_id.id,
+ 'default_partner_id': self.partner_id.id,
+ 'default_team_id': self.team_id.id,
+ 'default_campaign_id': self.campaign_id.id,
+ 'default_medium_id': self.medium_id.id,
+ 'default_origin': self.name,
+ 'default_source_id': self.source_id.id,
+ 'default_company_id': self.company_id.id or self.env.company.id,
+ 'default_tag_ids': [(6, 0, self.tag_ids.ids)]
+ }
+ return action
+
+ def action_view_sale_quotation(self):
+ action = self.env["ir.actions.actions"]._for_xml_id("sale.action_quotations_with_onboarding")
+ action['context'] = {
+ 'search_default_draft': 1,
+ 'search_default_partner_id': self.partner_id.id,
+ 'default_partner_id': self.partner_id.id,
+ 'default_opportunity_id': self.id
+ }
+ action['domain'] = [('opportunity_id', '=', self.id), ('state', 'in', ['draft', 'sent'])]
+ quotations = self.mapped('order_ids').filtered(lambda l: l.state in ('draft', 'sent'))
+ if len(quotations) == 1:
+ action['views'] = [(self.env.ref('sale.view_order_form').id, 'form')]
+ action['res_id'] = quotations.id
+ return action
+
+ def action_view_sale_order(self):
+ action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders")
+ action['context'] = {
+ 'search_default_partner_id': self.partner_id.id,
+ 'default_partner_id': self.partner_id.id,
+ 'default_opportunity_id': self.id,
+ }
+ action['domain'] = [('opportunity_id', '=', self.id), ('state', 'not in', ('draft', 'sent', 'cancel'))]
+ orders = self.mapped('order_ids').filtered(lambda l: l.state not in ('draft', 'sent', 'cancel'))
+ if len(orders) == 1:
+ action['views'] = [(self.env.ref('sale.view_order_form').id, 'form')]
+ action['res_id'] = orders.id
+ return action
diff --git a/addons/sale_crm/models/crm_team.py b/addons/sale_crm/models/crm_team.py
new file mode 100644
index 00000000..438d9f08
--- /dev/null
+++ b/addons/sale_crm/models/crm_team.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models,fields, api, _
+
+
+class CrmTeam(models.Model):
+ _inherit = 'crm.team'
+
+ def _compute_dashboard_button_name(self):
+ super(CrmTeam, self)._compute_dashboard_button_name()
+ teams_with_opp = self.filtered(lambda team: team.use_opportunities)
+ if self._context.get('in_sales_app'):
+ teams_with_opp.update({'dashboard_button_name': _("Sales Analysis")})
+
+ def action_primary_channel_button(self):
+ if self._context.get('in_sales_app') and self.use_opportunities:
+ return self.env["ir.actions.actions"]._for_xml_id("sale.action_order_report_so_salesteam")
+ return super(CrmTeam,self).action_primary_channel_button()
+
+ def _graph_get_model(self):
+ if self.use_opportunities and self._context.get('in_sales_app') :
+ return 'sale.report'
+ return super(CrmTeam,self)._graph_get_model()
+
+ def _graph_date_column(self):
+ if self.use_opportunities and self._context.get('in_sales_app'):
+ return 'date'
+ return super(CrmTeam,self)._graph_date_column()
+
+ def _graph_y_query(self):
+ if self.use_opportunities and self._context.get('in_sales_app'):
+ return 'SUM(price_subtotal)'
+ return super(CrmTeam,self)._graph_y_query()
+
+ def _graph_title_and_key(self):
+ if self.use_opportunities and self._context.get('in_sales_app'):
+ return ['', _('Sales: Untaxed Total')]
+ return super(CrmTeam,self)._graph_title_and_key()
+
+ def _extra_sql_conditions(self):
+ if self.use_opportunities and self._context.get('in_sales_app'):
+ return "AND state in ('sale', 'done', 'pos_done')"
+ return super(CrmTeam,self)._extra_sql_conditions()
diff --git a/addons/sale_crm/models/res_users.py b/addons/sale_crm/models/res_users.py
new file mode 100644
index 00000000..c567bfc8
--- /dev/null
+++ b/addons/sale_crm/models/res_users.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models
+
+
+class ResUsers(models.Model):
+ _inherit = 'res.users'
+
+ target_sales_invoiced = fields.Integer('Invoiced in Sales Orders Target')
diff --git a/addons/sale_crm/models/sale_order.py b/addons/sale_crm/models/sale_order.py
new file mode 100644
index 00000000..50a97d19
--- /dev/null
+++ b/addons/sale_crm/models/sale_order.py
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models
+
+
+class SaleOrder(models.Model):
+ _inherit = 'sale.order'
+
+ opportunity_id = fields.Many2one(
+ 'crm.lead', string='Opportunity', check_company=True,
+ domain="[('type', '=', 'opportunity'), '|', ('company_id', '=', False), ('company_id', '=', company_id)]")
+
+ def action_confirm(self):
+ return super(SaleOrder, self.with_context({k:v for k,v in self._context.items() if k != 'default_tag_ids'})).action_confirm()