From 44a5536200bdaf189c496751b1679c7479b7b926 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 25 Jan 2023 13:58:42 +0700 Subject: add crm leads monitoring --- indoteknik_custom/__manifest__.py | 1 + indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/leads_monitoring.py | 70 ++++++++++++++++++++ indoteknik_custom/security/ir.model.access.csv | 3 +- indoteknik_custom/views/leads_monitoring.xml | 88 ++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 indoteknik_custom/models/leads_monitoring.py create mode 100644 indoteknik_custom/views/leads_monitoring.xml diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 66184081..1643d9d1 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -58,6 +58,7 @@ 'views/website_content.xml', 'views/custom_mail_marketing.xml', 'views/website_ads.xml', + 'views/leads_monitoring.xml', 'report/report.xml', 'report/report_banner_banner.xml', 'report/report_banner_banner2.xml', diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 5d5ad0ec..c6fb7d4f 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -45,3 +45,4 @@ from . import x_manufactures from . import x_partner_purchase_order from . import x_product_tags from . import website_ads +from . import leads_monitoring diff --git a/indoteknik_custom/models/leads_monitoring.py b/indoteknik_custom/models/leads_monitoring.py new file mode 100644 index 00000000..0fe63a3d --- /dev/null +++ b/indoteknik_custom/models/leads_monitoring.py @@ -0,0 +1,70 @@ +from odoo import fields, models, api, tools +import logging + +_logger = logging.getLogger(__name__) + + +class LeadsMonitoring(models.Model): + _name = 'leads.monitoring' + _auto = False + _rec_name = 'lead_id' + + id = fields.Integer() + lead_id = fields.Many2one('crm.lead', string='Leads') + create_date = fields.Datetime(string='Created') + lead_name = fields.Char(string='Lead') + contact_name = fields.Char(string='Contact Name') + partner_name = fields.Char(string='Partner Name') + email = fields.Char(string='Email') + phone = fields.Char(string='Phone') + mobile = fields.Char(string='Mobile') + salesperson_id = fields.Many2one('res.partner', string='Salesperson') + salesperson = fields.Char(string='Sales Name') + type = fields.Char(string='Type') + priority = fields.Char(string='Priority') + date_last_stage_update = fields.Datetime(string='Last Stage Updated') + stage_status = fields.Char(string='Status') + total_quotation = fields.Float(string='Quotation') + total_sales = fields.Float(string='Sales') + + def init(self): + self._drop_view() + self.env.cr.execute("SELECT matviewname from pg_matviews where schemaname = 'public' and matviewname = '%s'" % self._table) + materialized_view = self.env.cr.fetchone() + if materialized_view is None: + self._init_materialized_view() + + def action_refresh(self): + _logger.info("Refresh %s Materialized View..." % self._table) + self.env.cr.execute("Refresh Materialized View %s" % self._table) + _logger.info('Refresh %s View Success' % self._table) + + def _drop_view(self): + self.env.cr.execute("SELECT viewname from pg_views where schemaname = 'public' and viewname = '%s'" % self._table) + standard_view = self.env.cr.fetchone() + if standard_view is not None: + self.env.cr.execute("DROP VIEW %s CASCADE" % self._table) + + def _init_materialized_view(self): + self.env.cr.execute(""" + CREATE MATERIALIZED VIEW %s AS ( + select cl.id, cl.id as lead_id, cl.create_date,cl.email_from as email, cl.phone, cl.mobile, cl.name as lead_name, + u.partner_id as salesperson_id, p.name as salesperson, cl.type, cl.priority, cl.date_last_stage_update, + cl.contact_name, cl.partner_name, + case + when cl.stage_id = 2 then '3-Proses Quotation' + when cl.stage_id = 1 then '1-Lead / Potensi Baru' + when cl.stage_id = 4 then '5-Proses Berhasil' + when cl.stage_id = 3 then '2-Proses Lain, Visit, Etc' + when cl.stage_id = 5 then '4-Proses Negosiasi' + else '6-Undefined' + end as stage_status, + (select sum(so.amount_total) from sale_order so where so.opportunity_id = cl.id and so.state in ('draft', 'sent')) as total_quotation, + (select sum(so.amount_total) from sale_order so where so.opportunity_id = cl.id and so.state in ('sale')) as total_sales + from crm_lead cl + left join res_users u on u.id = cl.user_id + left join res_partner p on p.id = u.partner_id + where cl.active = 'true' and cl.stage_id not in(4,7) + order by cl.create_date desc + ) + """ % self._table) diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 9349a566..d469aa1e 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -26,4 +26,5 @@ access_website_content_channel,access.website.content.channel,model_website_cont access_website_content,access.website.content,model_website_content,,1,1,1,1 access_invoice_reklas,access.invoice.reklas,model_invoice_reklas,,1,1,1,1 access_custom_mail_marketing,access.custom.mail.marketing,model_custom_mail_marketing,,1,1,1,1 -access_website_ads,access.website.ads,model_website_ads,,1,1,1,1 \ No newline at end of file +access_website_ads,access.website.ads,model_website_ads,,1,1,1,1 +access_leads_monitoring,access.leads.monitoring,model_leads_monitoring,,1,1,1,1 \ No newline at end of file diff --git a/indoteknik_custom/views/leads_monitoring.xml b/indoteknik_custom/views/leads_monitoring.xml new file mode 100644 index 00000000..3b4327cd --- /dev/null +++ b/indoteknik_custom/views/leads_monitoring.xml @@ -0,0 +1,88 @@ + + + + leads_monitoring.tree + leads.monitoring + + +
+
+ + + + + + + + + + + + + + +
+
+
+ + + leads.monitoring.form + leads.monitoring + +
+ + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + + Leads Monitoring + ir.actions.act_window + leads.monitoring + tree,form + + + + +
\ No newline at end of file -- cgit v1.2.3