summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-01-24 11:41:31 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-01-24 11:41:31 +0700
commit96a00bc1ac5f114777b2f5f0f0117c1e67141caf (patch)
tree7200fbc52eda409d9bd45fb42a635dd0c76bf7b2
parent9b026c1ce5683b42dfdd15da74cf6da0ef7bf9d5 (diff)
refresh materialize view
-rw-r--r--indoteknik_api/models/x_manufactures.py1
-rwxr-xr-xindoteknik_custom/models/sale_monitoring.py58
-rwxr-xr-xindoteknik_custom/models/sale_monitoring_detail.py101
-rwxr-xr-xindoteknik_custom/views/sale_monitoring.xml5
-rwxr-xr-xindoteknik_custom/views/sale_monitoring_detail.xml6
5 files changed, 97 insertions, 74 deletions
diff --git a/indoteknik_api/models/x_manufactures.py b/indoteknik_api/models/x_manufactures.py
index 988d7a45..b85f6d27 100644
--- a/indoteknik_api/models/x_manufactures.py
+++ b/indoteknik_api/models/x_manufactures.py
@@ -5,7 +5,6 @@ class Manufactures(models.Model):
_inherit = 'x_manufactures'
def api_single_response(self, manufacture, with_detail=False):
- base_url = self.env['ir.config_parameter'].get_param('web.base.url')
data = {
'id': manufacture.id,
'logo': self.env['ir.attachment'].api_image('x_manufactures', 'x_logo_manufacture', manufacture.id),
diff --git a/indoteknik_custom/models/sale_monitoring.py b/indoteknik_custom/models/sale_monitoring.py
index a837a6bc..a3265a8b 100755
--- a/indoteknik_custom/models/sale_monitoring.py
+++ b/indoteknik_custom/models/sale_monitoring.py
@@ -1,5 +1,7 @@
from odoo import fields, models, api, tools
+import logging
+_logger = logging.getLogger(__name__)
class SaleMonitoring(models.Model):
_name = 'sale.monitoring'
@@ -22,29 +24,35 @@ class SaleMonitoring(models.Model):
def init(self):
tools.drop_view_if_exists(self.env.cr, self._table)
self.env.cr.execute("""
- CREATE OR REPLACE VIEW %s AS (
- SELECT
- smd.sale_order_id AS id,
- smd.date_order,
- smd.sale_order_id,
- smd.partner_id,
- smd.user_id,
- SUM(smd.qty_so) AS qty_so,
- SUM(smd.qty_po) AS qty_po,
- SUM(smd.qty_po_received) AS qty_po_received,
- SUM(smd.qty_so_delivered) AS qty_so_delivered,
- SUM(smd.qty_so_invoiced) AS qty_so_invoiced,
- CASE
- WHEN SUM(qty_po) < SUM(qty_so) AND SUM(qty_po) <= 0 THEN 'Belum PO sama sekali'
- WHEN SUM(qty_po) < SUM(qty_so) THEN 'Belum PO full'
- WHEN SUM(qty_po_received) < SUM(qty_so) AND SUM(qty_po_received) <= 0 THEN 'Belum Received sama sekali'
- WHEN SUM(qty_po_received) < SUM(qty_so) THEN 'Belum Received full'
- WHEN SUM(qty_to_delivered) = SUM(qty_so) THEN 'SIAP KIRIM'
- WHEN SUM(qty_to_delivered) < SUM(qty_so) and SUM(qty_to_delivered) > 0 THEN 'KIRIM SISANYA'
- ELSE 'Belum Invoiced'
- END AS status,
- get_po_number(smd.sale_order_id) as po_number
- FROM sale_monitoring_detail smd
- GROUP BY smd.date_order, smd.sale_order_id, smd.partner_id, smd.user_id
- )
+ CREATE OR REPLACE VIEW %s AS (
+ SELECT
+ smd.sale_order_id AS id,
+ smd.date_order,
+ smd.sale_order_id,
+ smd.partner_id,
+ smd.user_id,
+ SUM(smd.qty_so) AS qty_so,
+ SUM(smd.qty_po) AS qty_po,
+ SUM(smd.qty_po_received) AS qty_po_received,
+ SUM(smd.qty_so_delivered) AS qty_so_delivered,
+ SUM(smd.qty_so_invoiced) AS qty_so_invoiced,
+ CASE
+ WHEN SUM(qty_po) < SUM(qty_so) AND SUM(qty_po) <= 0 THEN 'Belum PO sama sekali'
+ WHEN SUM(qty_po) < SUM(qty_so) THEN 'Belum PO full'
+ WHEN SUM(qty_po_received) < SUM(qty_so) AND SUM(qty_po_received) <= 0 THEN 'Belum Received sama sekali'
+ WHEN SUM(qty_po_received) < SUM(qty_so) THEN 'Belum Received full'
+ WHEN SUM(qty_to_delivered) = SUM(qty_so) THEN 'SIAP KIRIM'
+ WHEN SUM(qty_to_delivered) < SUM(qty_so) and SUM(qty_to_delivered) > 0 THEN 'KIRIM SISANYA'
+ ELSE 'Belum Invoiced'
+ END AS status,
+ get_po_number(smd.sale_order_id) as po_number
+ FROM sale_monitoring_detail smd
+ GROUP BY smd.date_order, smd.sale_order_id, smd.partner_id, smd.user_id
+ )
""" % self._table)
+
+ def action_refresh(self):
+ _logger.info("Refresh %s View Starting..." % self._table)
+ self.env.cr.execute("REFRESH MATERIALIZED VIEW sale_monitoring_detail")
+ _logger.info("Refresh %s View Success" % self._table)
+ \ No newline at end of file
diff --git a/indoteknik_custom/models/sale_monitoring_detail.py b/indoteknik_custom/models/sale_monitoring_detail.py
index 405b43ef..553ec21f 100755
--- a/indoteknik_custom/models/sale_monitoring_detail.py
+++ b/indoteknik_custom/models/sale_monitoring_detail.py
@@ -22,52 +22,63 @@ class SaleMonitoringDetail(models.Model):
date_order = fields.Datetime(string="Date Order")
status = fields.Char(string="Status")
- def refresh_mv(self):
- _logger.info('Refresh Materialized View Starting...')
- self.env.cr.execute("REFRESH MATERIALIZED VIEW sale_monitoring_detail")
- _logger.info("Refresh Materialized View berhasil")
-
def init(self):
- # tools.drop_view_if_exists(self.env.cr, self._table)
- self.env.cr.execute("DROP MATERIALIZED VIEW sale_monitoring_detail CASCADE")
+ 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 View Starting..." % 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
- *,
- CASE
- WHEN qty_po < qty_so AND qty_po <= 0 THEN 'Belum PO sama sekali'
- WHEN qty_po < qty_so THEN 'Belum PO full'
- WHEN qty_po_received < qty_so and qty_po_received <= 0 THEN 'Belum Received sama sekali'
- WHEN qty_po_received < qty_so THEN 'Belum Received full'
- WHEN qty_to_delivered = qty_so THEN 'SIAP KIRIM'
- WHEN qty_to_delivered < qty_so and qty_to_delivered > 0 THEN 'KIRIM SISANYA'
- ELSE 'Belum Invoiced'
- END AS status
- FROM
- (
+ CREATE MATERIALIZED VIEW %s AS (
SELECT
- sol.id AS id,
- so.id AS sale_order_id,
- so.partner_id as partner_id,
- so.user_id,
- p.id AS product_id,
- sol.product_uom_qty AS qty_so,
- sol.qty_delivered AS qty_so_delivered,
- get_qty_to_delivered(sol.id) as qty_to_delivered,
- sol.qty_invoiced AS qty_so_invoiced,
- so.date_order AS date_order,
- get_qty_po(so.id, sol.product_id) AS qty_po,
- get_qty_received(so.id, sol.product_id) AS qty_po_received
- FROM sale_order so
- JOIN sale_order_line sol ON sol.order_id = so.id
- JOIN product_product p ON p.id = sol.product_id
- JOIN product_template pt ON pt.id = p.product_tmpl_id
- WHERE pt.type IN ('consu','product')
- AND so.state IN ('sale','done')
- AND so.create_date >= '2022-08-10'
- ) a
- WHERE
- a.qty_so_delivered > a.qty_so_invoiced
- or a.qty_to_delivered > 0
- )
+ *,
+ CASE
+ WHEN qty_po < qty_so AND qty_po <= 0 THEN 'Belum PO sama sekali'
+ WHEN qty_po < qty_so THEN 'Belum PO full'
+ WHEN qty_po_received < qty_so and qty_po_received <= 0 THEN 'Belum Received sama sekali'
+ WHEN qty_po_received < qty_so THEN 'Belum Received full'
+ WHEN qty_to_delivered = qty_so THEN 'SIAP KIRIM'
+ WHEN qty_to_delivered < qty_so and qty_to_delivered > 0 THEN 'KIRIM SISANYA'
+ ELSE 'Belum Invoiced'
+ END AS status
+ FROM
+ (
+ SELECT
+ sol.id AS id,
+ so.id AS sale_order_id,
+ so.partner_id as partner_id,
+ so.user_id,
+ p.id AS product_id,
+ sol.product_uom_qty AS qty_so,
+ sol.qty_delivered AS qty_so_delivered,
+ get_qty_to_delivered(sol.id) as qty_to_delivered,
+ sol.qty_invoiced AS qty_so_invoiced,
+ so.date_order AS date_order,
+ get_qty_po(so.id, sol.product_id) AS qty_po,
+ get_qty_received(so.id, sol.product_id) AS qty_po_received
+ FROM sale_order so
+ JOIN sale_order_line sol ON sol.order_id = so.id
+ JOIN product_product p ON p.id = sol.product_id
+ JOIN product_template pt ON pt.id = p.product_tmpl_id
+ WHERE pt.type IN ('consu','product')
+ AND so.state IN ('sale','done')
+ AND so.create_date >= '2022-08-10'
+ ) a
+ WHERE
+ a.qty_so_delivered > a.qty_so_invoiced
+ or a.qty_to_delivered > 0
+ )
""" % self._table)
diff --git a/indoteknik_custom/views/sale_monitoring.xml b/indoteknik_custom/views/sale_monitoring.xml
index 025e5d26..b3b186e9 100755
--- a/indoteknik_custom/views/sale_monitoring.xml
+++ b/indoteknik_custom/views/sale_monitoring.xml
@@ -4,7 +4,10 @@
<field name="name">sale.monitoring.tree</field>
<field name="model">sale.monitoring</field>
<field name="arch" type="xml">
- <tree create="false">
+ <tree create="false" multi_edit="1">
+ <header>
+ <button name="action_refresh" string="Refresh" class="oe_highlight" type="object" />
+ </header>
<field name="date_order"/>
<field name="sale_order_id"/>
<field name="partner_id"/>
diff --git a/indoteknik_custom/views/sale_monitoring_detail.xml b/indoteknik_custom/views/sale_monitoring_detail.xml
index cd87bc1a..d22f0a13 100755
--- a/indoteknik_custom/views/sale_monitoring_detail.xml
+++ b/indoteknik_custom/views/sale_monitoring_detail.xml
@@ -4,7 +4,10 @@
<field name="name">sale.monitoring.detail.tree</field>
<field name="model">sale.monitoring.detail</field>
<field name="arch" type="xml">
- <tree create="false">
+ <tree create="false" multi_edit="1">
+ <header>
+ <button name="action_refresh" string="Refresh" class="oe_highlight" type="object" />
+ </header>
<field name="date_order"/>
<field name="sale_order_id"/>
<field name="partner_id"/>
@@ -22,7 +25,6 @@
decoration-success="status == 'SIAP KIRIM' or status == 'KIRIM SISANYA'"
decoration-info="status == 'Belum Invoiced'"
/>
- <button name="refresh_mv" string="Refresh" class="oe_highlight" type="object"/>
</tree>
</field>
</record>