Advertisement
ankdroid

Untitled

Jan 14th, 2025
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 109.20 KB | None | 0 0
  1. from odoo import fields, models, api, _
  2. from datetime import datetime, date, time, timedelta
  3. from dateutil.relativedelta import relativedelta
  4. import logging
  5. import pytz
  6. import psycopg2
  7. from psycopg2 import extras
  8. from odoo.exceptions import ValidationError
  9.  
  10. _logger = logging.getLogger(__name__)
  11.  
  12.  
  13. class MergingMartData(models.Model):
  14.     _name = "merging.svd.mart"
  15.     _description = "Model Tampungan untuk SVD Mart Data"
  16.  
  17.     date = fields.Date(string="Date")
  18.     # branch_id = fields.Many2one('res.branch',string="Branch")
  19.     branch_code = fields.Char(string="Branch Code")
  20.     # product_id = fields.Many2one('product.product',string="Product")
  21.     product_code = fields.Char(string="Product Code")
  22.     product_name = fields.Char(string="Product Name")
  23.     product_category = fields.Char(string="Product Category")
  24.     sub_category_name = fields.Char(string="Sub Category Name")
  25.     avg_7 = fields.Float(string="Average 7")
  26.     avg_14 = fields.Float(string="Average 14")
  27.     avg_30 = fields.Float(string="Average 30")
  28.     avg_60 = fields.Float(string="Average 60")
  29.     avg_90 = fields.Float(string="Average 90")
  30.     avg_180 = fields.Float(string="Average 180")
  31.     # average_sales_price = fields.Float(string="Average Sales Price")
  32.     total_sales_qty_7 = fields.Float(string="Total Sales Qty 7 Days")
  33.     total_sales_qty_14 = fields.Float(string="Total Sales Qty 14 Days")
  34.     total_sales_qty_30 = fields.Float(string="Total Sales Qty 30 Days")
  35.     total_sales_qty_60 = fields.Float(string="Total Sales Qty 60 Days")
  36.     total_sales_qty_90 = fields.Float(string="Total Sales Qty 90 Days")
  37.     total_sales_qty_180 = fields.Float(string="Total Sales Qty 180 Days")
  38.     days_stock_available_7 = fields.Float(string="Stock Available 7 Days")
  39.     days_stock_available_14 = fields.Float(string="Stock Available 14 Days")
  40.     days_stock_available_30 = fields.Float(string="Stock Available 30 Days")
  41.     days_stock_available_60 = fields.Float(string="Stock Available 60 Days")
  42.     days_stock_available_90 = fields.Float(string="Stock Available 90 Days")
  43.     days_stock_available_180 = fields.Float(string="Stock Available 180 Days")
  44.     type_data_svd = fields.Selection([
  45.         ("abu", "DWH ABU"),
  46.         ("pnj", "DWH PNJ"),
  47.         ("abu_new", "ABU 2024")], string='Data SVD')
  48.  
  49.  
  50. class SbbSnapshot(models.Model):
  51.     _name = "sbb.snapshot"
  52.     _description = "Daily SVD Spot"
  53.  
  54.     date = fields.Date(string="Date")
  55.     # branch_id = fields.Many2one('res.branch',string="Branch")
  56.     branch_code = fields.Char(string="Branch Code")
  57.     # product_id = fields.Many2one('product.product',string="Product")
  58.     product_code = fields.Char(string="Product Code")
  59.     product_name = fields.Char(string="Product Name")
  60.     product_category = fields.Char(string="Product Category")
  61.     sub_category_name = fields.Char(string="Sub Category Name")
  62.     avg_7 = fields.Float(string="Average 7")
  63.     avg_14 = fields.Float(string="Average 14")
  64.     avg_30 = fields.Float(string="Average 30")
  65.     avg_60 = fields.Float(string="Average 60")
  66.     avg_90 = fields.Float(string="Average 90")
  67.     avg_180 = fields.Float(string="Average 180")
  68.     # average_sales_price = fields.Float(string="Average Sales Price")
  69.     total_sales_qty_7 = fields.Float(string="Total Sales Qty 7 Days")
  70.     total_sales_qty_14 = fields.Float(string="Total Sales Qty 14 Days")
  71.     total_sales_qty_30 = fields.Float(string="Total Sales Qty 30 Days")
  72.     total_sales_qty_60 = fields.Float(string="Total Sales Qty 60 Days")
  73.     total_sales_qty_90 = fields.Float(string="Total Sales Qty 90 Days")
  74.     total_sales_qty_180 = fields.Float(string="Total Sales Qty 180 Days")
  75.     days_stock_available_7 = fields.Float(string="Stock Available 7 Days")
  76.     days_stock_available_14 = fields.Float(string="Stock Available 14 Days")
  77.     days_stock_available_30 = fields.Float(string="Stock Available 30 Days")
  78.     days_stock_available_60 = fields.Float(string="Stock Available 60 Days")
  79.     days_stock_available_90 = fields.Float(string="Stock Available 90 Days")
  80.     days_stock_available_180 = fields.Float(string="Stock Available 180 Days")
  81.     type_data_svd = fields.Selection([
  82.         ("abu", "DWH ABU"),
  83.         ("pnj", "DWH PNJ"),
  84.         ("abu_new", "ABU 2024")], string='Data SVD')
  85.  
  86.     def query_pnj(self):
  87.         server = self.env['integration.external.db'].sudo().search([('server', '=', 'pnj_replication')], limit=1)
  88.         if not server:
  89.             raise ValidationError(
  90.                 "Server Database PNJ Belum di setting, silahkan di setting external DB nya terlebih dahulu!")
  91.  
  92.         done_process = 0
  93.         if server:
  94.             conn = server.connection_db()
  95.             cur = conn.cursor()
  96.             cur.execute(
  97.                 """
  98.                    select a.product_code as product_code,a.product_name,a.branch_name as branch_name,a.branch_code as branch_code,a.category_name as category_name,a.sub_category_name as sub_category_name, a.product_id as product_id,
  99.                    coalesce (max(sales7.qty),0) as qty_sales_7,
  100.                    max(soh7.av_days) as av_days_7,
  101.                    coalesce(max(sales7.qty),0) / coalesce(max(soh7.av_days),0) as svd_7,
  102.                    coalesce(max(sales14.qty),0) as qty_sales_14,
  103.                    max(soh14.av_days) as av_days_14,
  104.                    coalesce(max(sales14.qty),0) / coalesce(max(soh14.av_days),0) as svd_14,
  105.                    coalesce(max(sales30.qty),0) as qty_sales_30,
  106.                    max(soh30.av_days) as av_days_30,
  107.                    coalesce(max(sales30.qty),0) / coalesce(max(soh30.av_days),0) as svd_30,
  108.                    coalesce(max(sales60.qty),0) as qty_sales_60,
  109.                    max(soh60.av_days) as av_days_60,
  110.                    coalesce(max(sales60.qty),0) / coalesce(max(soh60.av_days),0) as svd_60,
  111.                    coalesce(max(sales90.qty),0) as qty_sales_90,
  112.                    max(soh90.av_days) as av_days_90,
  113.                    coalesce(max(sales90.qty),0) / coalesce(max(soh90.av_days),0) as svd_90,
  114.                    coalesce(max(sales180.qty),0) as qty_sales_180,
  115.                    max(soh180.av_days) as av_days_180,
  116.                    coalesce(max(sales180.qty),0) / coalesce(max(soh180.av_days),0) as svd_180
  117.                    from
  118.                        (select
  119.                            pp.id as product_id,
  120.                            rb.id as branch_id,
  121.                            pp.default_code as product_code,
  122.                            pt1."name" as product_name,
  123.                            rb.code as branch_code,
  124.                            pc1."name" as category_name,
  125.                            apc1."name" as sub_category_name,
  126.                            rb."name" as branch_name
  127.                        from product_product pp
  128.                        cross join res_branch rb
  129.                        left join product_template pt1 on pp.product_tmpl_id = pt1.id
  130.                        left join product_category pc1 on pt1.categ_id = pc1.id
  131.                        left join attribute_product_category apc1 on pt1.attribute_categ_id = apc1.id  
  132.                        where
  133.                        pt1.sale_ok = true
  134.                        ) a
  135.                    left join (
  136.                        select
  137.                            ail2.product_id,
  138.                            ai2.branch_id,
  139.                            sum(ail2.quantity) as qty
  140.                        from account_invoice_line ail2
  141.                        left join account_invoice ai2 on ail2.invoice_id = ai2.id
  142.                        where
  143.                            ai2.state not in ('draft')
  144.                            and ai2.type = 'out_invoice'
  145.                            and ai2."date" BETWEEN current_date - interval '7 days' AND now()
  146.                        group by 1,2
  147.                    ) sales7 on a.product_id = sales7.product_id and sales7.branch_id = a.branch_id
  148.                    left join (
  149.                        select product_code,branch, count(product_code) as av_days from (
  150.                            select
  151.                                product_code,
  152.                                branch,
  153.                                date(date_soh) as date_soh,
  154.                                sum(soh) ttl_qty
  155.                            from
  156.                                soh_snapshoot ss
  157.                            where "date_soh" BETWEEN current_date - interval '7 days' AND now()
  158.                            group by 1,2,3
  159.                        ) raw_soh_7
  160.                        where raw_soh_7.ttl_qty > 20
  161.                        group by 1,2
  162.                    ) soh7 on a.product_code = soh7.product_code and a.branch_name = soh7.branch
  163.                    left join (
  164.                        select
  165.                            ail2.product_id,
  166.                            ai2.branch_id,
  167.                            sum(ail2.quantity) as qty
  168.                        from account_invoice_line ail2
  169.                        left join account_invoice ai2 on ail2.invoice_id = ai2.id
  170.                        where
  171.                            ai2.state not in ('draft')
  172.                            and ai2.type = 'out_invoice'
  173.                            and ai2."date" BETWEEN current_date - interval '14 days' AND now()
  174.                        group by 1,2
  175.                    ) sales14 on a.product_id = sales14.product_id and sales14.branch_id = a.branch_id
  176.                    left join (
  177.                        select product_code,branch, count(product_code) as av_days from (
  178.                            select
  179.                                product_code,
  180.                                branch,
  181.                                date(date_soh) as date_soh,
  182.                                sum(soh) ttl_qty
  183.                            from
  184.                                soh_snapshoot ss
  185.                            where "date_soh" BETWEEN current_date - interval '14 days' AND now()
  186.                            group by 1,2,3
  187.                        ) raw_soh_7
  188.                        where raw_soh_7.ttl_qty > 20
  189.                        group by 1,2
  190.                    ) soh14 on a.product_code = soh14.product_code and a.branch_name = soh14.branch
  191.                    left join (
  192.                        select
  193.                            ail2.product_id,
  194.                            ai2.branch_id,
  195.                            sum(ail2.quantity) as qty
  196.                        from account_invoice_line ail2
  197.                        left join account_invoice ai2 on ail2.invoice_id = ai2.id
  198.                        where
  199.                            ai2.state not in ('draft')
  200.                            and ai2.type = 'out_invoice'
  201.                            and ai2."date" BETWEEN current_date - interval '30 days' AND now()
  202.                        group by 1,2
  203.                    ) sales30 on a.product_id = sales30.product_id and sales30.branch_id = a.branch_id
  204.                    left join (
  205.                        select product_code,branch, count(product_code) as av_days from (
  206.                            select
  207.                                product_code,
  208.                                branch,
  209.                                date(date_soh) as date_soh,
  210.                                sum(soh) ttl_qty
  211.                            from
  212.                                soh_snapshoot ss
  213.                            where "date_soh" BETWEEN current_date - interval '30 days' AND now()
  214.                            group by 1,2,3
  215.                        ) raw_soh_7
  216.                        where raw_soh_7.ttl_qty > 20
  217.                        group by 1,2
  218.                    ) soh30 on a.product_code = soh30.product_code and a.branch_name = soh30.branch
  219.                    left join (
  220.                        select
  221.                            ail2.product_id,
  222.                            ai2.branch_id,
  223.                            sum(ail2.quantity) as qty
  224.                        from account_invoice_line ail2
  225.                        left join account_invoice ai2 on ail2.invoice_id = ai2.id
  226.                        where
  227.                            ai2.state not in ('draft')
  228.                            and ai2.type = 'out_invoice'
  229.                            and ai2."date" BETWEEN current_date - interval '60 days' AND now()
  230.                        group by 1,2
  231.                    ) sales60 on a.product_id = sales60.product_id and sales60.branch_id = a.branch_id
  232.                    left join (
  233.                        select product_code,branch, count(product_code) as av_days from (
  234.                            select
  235.                                product_code,
  236.                                branch,
  237.                                date(date_soh) as date_soh,
  238.                                sum(soh) ttl_qty
  239.                            from
  240.                                soh_snapshoot ss
  241.                            where "date_soh" BETWEEN current_date - interval '60 days' AND now()
  242.                            group by 1,2,3
  243.                        ) raw_soh_7
  244.                        where raw_soh_7.ttl_qty > 20
  245.                        group by 1,2
  246.                    ) soh60 on a.product_code = soh60.product_code and a.branch_name = soh60.branch
  247.                    left join (
  248.                        select
  249.                            ail2.product_id,
  250.                            ai2.branch_id,
  251.                            sum(ail2.quantity) as qty
  252.                        from account_invoice_line ail2
  253.                        left join account_invoice ai2 on ail2.invoice_id = ai2.id
  254.                        where
  255.                            ai2.state not in ('draft')
  256.                            and ai2.type = 'out_invoice'
  257.                            and ai2."date" BETWEEN current_date - interval '90 days' AND now()
  258.                        group by 1,2
  259.                    ) sales90 on a.product_id = sales90.product_id and sales90.branch_id = a.branch_id
  260.                    left join (
  261.                        select product_code,branch, count(product_code) as av_days from (
  262.                            select
  263.                                product_code,
  264.                                branch,
  265.                                date(date_soh) as date_soh,
  266.                                sum(soh) ttl_qty
  267.                            from
  268.                                soh_snapshoot ss
  269.                            where "date_soh" BETWEEN current_date - interval '90 days' AND now()
  270.                            group by 1,2,3
  271.                        ) raw_soh_7
  272.                        where raw_soh_7.ttl_qty > 20
  273.                        group by 1,2
  274.                    ) soh90 on a.product_code = soh90.product_code and a.branch_name = soh90.branch
  275.                    left join (
  276.                        select
  277.                            ail2.product_id,
  278.                            ai2.branch_id,
  279.                            sum(ail2.quantity) as qty
  280.                        from account_invoice_line ail2
  281.                        left join account_invoice ai2 on ail2.invoice_id = ai2.id
  282.                        where
  283.                            ai2.state not in ('draft')
  284.                            and ai2.type = 'out_invoice'
  285.                            and ai2."date" BETWEEN current_date - interval '180 days' AND now()
  286.                        group by 1,2
  287.                    ) sales180 on a.product_id = sales180.product_id and sales180.branch_id = a.branch_id
  288.                    left join (
  289.                        select product_code,branch, count(product_code) as av_days from (
  290.                            select
  291.                                product_code,
  292.                                branch,
  293.                                date(date_soh) as date_soh,
  294.                                sum(soh) ttl_qty
  295.                            from
  296.                                soh_snapshoot ss
  297.                            where "date_soh" BETWEEN current_date - interval '180 days' AND now()
  298.                            group by 1,2,3
  299.                        ) raw_soh_7
  300.                        where raw_soh_7.ttl_qty > 20
  301.                        group by 1,2
  302.                    ) soh180 on a.product_code = soh180.product_code and a.branch_name = soh180.branch
  303.                    group by 1,2,3,4,5,6,7
  304.                    having
  305.                        count(soh7.av_days) > 0
  306.                        and count(soh14.av_days) > 0
  307.                        and count(soh30.av_days) > 0
  308.                        and count(soh60.av_days) > 0
  309.                        and count(soh90.av_days) > 0
  310.                        and count(soh180.av_days) > 0
  311.                """
  312.             )
  313.             query_database_dwh_pnj = cur.fetchall()
  314.  
  315.             if query_database_dwh_pnj:
  316.                 for data_query_dwh_pnj in query_database_dwh_pnj:
  317.                     # _logger.info("------------------------ DATA QUERY DWH PNJ %s"%query_database_dwh_pnj)
  318.  
  319.                     product_code_pnj = data_query_dwh_pnj[0]
  320.                     product_name_pnj = data_query_dwh_pnj[1]
  321.                     branch_name_pnj = data_query_dwh_pnj[2]
  322.                     branch_code_pnj = data_query_dwh_pnj[3]
  323.                     category_name_pnj = data_query_dwh_pnj[4]
  324.                     sub_category_name_pnj = data_query_dwh_pnj[5]
  325.                     product_id_pnj = data_query_dwh_pnj[6]
  326.                     qty_sales_7_pnj = data_query_dwh_pnj[7]
  327.                     av_days_7_pnj = data_query_dwh_pnj[8]
  328.                     svd_7_pnj = data_query_dwh_pnj[9]
  329.                     qty_sales_14_pnj = data_query_dwh_pnj[10]
  330.                     av_days_14_pnj = data_query_dwh_pnj[11]
  331.                     svd_14_pnj = data_query_dwh_pnj[12]
  332.                     qty_sales_30_pnj = data_query_dwh_pnj[13]
  333.                     av_days_30_pnj = data_query_dwh_pnj[14]
  334.                     svd_30_pnj = data_query_dwh_pnj[15]
  335.                     qty_sales_60_pnj = data_query_dwh_pnj[16]
  336.                     av_days_60_pnj = data_query_dwh_pnj[17]
  337.                     svd_60_pnj = data_query_dwh_pnj[18]
  338.                     qty_sales_90_pnj = data_query_dwh_pnj[19]
  339.                     av_days_90_pnj = data_query_dwh_pnj[20]
  340.                     svd_90_pnj = data_query_dwh_pnj[21]
  341.                     qty_sales_180_pnj = data_query_dwh_pnj[22]
  342.                     av_days_180_pnj = data_query_dwh_pnj[23]
  343.                     svd_180_pnj = data_query_dwh_pnj[24]
  344.  
  345.                     convert_branch_pnj_to_branch_local_abu = self.env['branch.code'].sudo().search(
  346.                         [('code', '=', branch_code_pnj)])
  347.                     for branch_local in convert_branch_pnj_to_branch_local_abu:
  348.                         pnj_branch_local_abu = branch_local.branch_id.code
  349.  
  350.                     query_create_svd_from_dwh_pnj = """
  351.                                            INSERT INTO sbb_snapshot
  352.                                            ("product_code","product_name","product_category","sub_category_name","branch_code","avg_7","avg_14","avg_30","avg_60","avg_90","avg_180","total_sales_qty_7","total_sales_qty_14","total_sales_qty_30","total_sales_qty_60","total_sales_qty_90","total_sales_qty_180","days_stock_available_7","days_stock_available_14","days_stock_available_30","days_stock_available_60","days_stock_available_90","days_stock_available_180","date","type_data_svd")
  353.                                            VALUES (%(product_code)s,%(product_name)s,%(product_category)s,%(sub_category_name)s,%(branch_code)s,%(avg_7)s,%(avg_14)s,%(avg_30)s,%(avg_60)s,%(avg_90)s,%(avg_180)s,%(total_sales_qty_7)s,%(total_sales_qty_14)s,%(total_sales_qty_30)s,%(total_sales_qty_60)s,%(total_sales_qty_90)s,%(total_sales_qty_180)s,%(days_stock_available_7)s,%(days_stock_available_14)s,%(days_stock_available_30)s,%(days_stock_available_60)s,%(days_stock_available_90)s,%(days_stock_available_180)s,current_date,'pnj')
  354.                                        """
  355.  
  356.                     execute_create_svd_snapshot_pnj = self.env.cr.execute(query_create_svd_from_dwh_pnj, {
  357.                         "product_code": product_code_pnj,
  358.                         "product_name": product_name_pnj,
  359.                         "product_category": category_name_pnj,
  360.                         "sub_category_name": sub_category_name_pnj,
  361.                         "branch_code": pnj_branch_local_abu,
  362.                         "avg_7": svd_7_pnj,
  363.                         "avg_14": svd_14_pnj,
  364.                         "avg_30": svd_30_pnj,
  365.                         "avg_60": svd_60_pnj,
  366.                         "avg_90": svd_90_pnj,
  367.                         "avg_180": svd_180_pnj,
  368.                         "total_sales_qty_7": qty_sales_7_pnj,
  369.                         "total_sales_qty_14": qty_sales_14_pnj,
  370.                         "total_sales_qty_30": qty_sales_30_pnj,
  371.                         "total_sales_qty_60": qty_sales_60_pnj,
  372.                         "total_sales_qty_90": qty_sales_90_pnj,
  373.                         "total_sales_qty_180": qty_sales_180_pnj,
  374.                         "days_stock_available_7": av_days_7_pnj,
  375.                         "days_stock_available_14": av_days_14_pnj,
  376.                         "days_stock_available_30": av_days_30_pnj,
  377.                         "days_stock_available_60": av_days_60_pnj,
  378.                         "days_stock_available_90": av_days_90_pnj,
  379.                         "days_stock_available_180": av_days_180_pnj,
  380.                     })
  381.                     # _logger.info("------------- DATA YANG DI CREATE PUNYA PNJ %s"%execute_create_svd_snapshot_pnj)
  382.  
  383.     def query_split_dwh_abu(self):
  384.         svd_7_start_abu = []
  385.         svd_7_end_abu = []
  386.         svd_7_start_dwh = []
  387.         svd_7_end_dwh = []
  388.         svd_14_start_abu = []
  389.         svd_14_end_abu = []
  390.         svd_14_start_dwh = []
  391.         svd_14_end_dwh = []
  392.         svd_30_start_abu = []
  393.         svd_30_end_abu = []
  394.         svd_30_start_dwh = []
  395.         svd_30_end_dwh = []
  396.         svd_60_start_abu = []
  397.         svd_60_end_abu = []
  398.         svd_60_start_dwh = []
  399.         svd_60_end_dwh = []
  400.         svd_90_start_abu = []
  401.         svd_90_end_abu = []
  402.         svd_90_start_dwh = []
  403.         svd_90_end_dwh = []
  404.         svd_180_start_abu = []
  405.         svd_180_end_abu = []
  406.         svd_180_start_dwh = []
  407.         svd_180_end_dwh = []
  408.         today = date.today()
  409.  
  410.         pnj_branch_local_abu = []
  411.  
  412.         abu_lama_branch_local_abu = []
  413.  
  414.         tanggal_golive = self.env['ir.config_parameter'].sudo().get_param('golive_abu_date')
  415.         tanggal_golive_abu = datetime.strptime(tanggal_golive, '%Y-%m-%d').date()
  416.  
  417.         tanggal_svd_7_dwh = tanggal_golive_abu - today
  418.  
  419.         validasi_tanggal_golive_7 = today - timedelta(days=7)  if (today - timedelta(days=7) ) > tanggal_golive_abu else tanggal_golive_abu
  420.         validasi_tanggal_golive_14 = today - timedelta(days=14) if (today - timedelta(days=14)) > tanggal_golive_abu else tanggal_golive_abu
  421.         validasi_tanggal_golive_30 = today - timedelta(days=30) if (today - timedelta(days=30)) > tanggal_golive_abu else tanggal_golive_abu
  422.         validasi_tanggal_golive_60 = today - timedelta(days=60) if (today - timedelta(days=60)) > tanggal_golive_abu else tanggal_golive_abu
  423.         validasi_tanggal_golive_90 = today - timedelta(days=90) if (today - timedelta(days=90)) > tanggal_golive_abu else tanggal_golive_abu
  424.         validasi_tanggal_golive_180 = today - timedelta(days=180) if (today - timedelta(days=180)) > tanggal_golive_abu else tanggal_golive_abu
  425.  
  426.         # 7
  427.         if validasi_tanggal_golive_7 > tanggal_golive_abu:
  428.             _logger.info("-------------- KONDISI 7 YANG PERTAMA -----------------------")
  429.             tanggal_svd_7_abu = (today - tanggal_golive_abu).days  # 22
  430.             svd_7_start_abu = validasi_tanggal_golive_7
  431.             svd_7_end_abu = today
  432.             _logger.info("%s---------- start abu 7" % svd_7_start_abu)
  433.             _logger.info("%s---------- end abu 7" % svd_7_end_abu)
  434.             svd_7_start_dwh = svd_7_start_abu + timedelta(days=180)
  435.             svd_7_end_dwh = svd_7_end_abu + timedelta(days=180)
  436.             _logger.info("%s---------- start dwh 7" % svd_7_start_dwh)
  437.             _logger.info("%s---------- end dwh7" % svd_7_end_dwh)
  438.         else:
  439.             _logger.info("-------------- KONDISI 7 YANG KEDUA -----------------------")
  440.             tanggal_svd_7_abu = (today - tanggal_golive_abu).days
  441.             svd_7_start_abu = validasi_tanggal_golive_7
  442.             svd_7_end_abu = today
  443.             _logger.info("%s---------- start abu 7" % svd_7_start_abu)
  444.             _logger.info("%s---------- end abu 7" % svd_7_end_abu)
  445.             svd_7_start_dwh = tanggal_golive_abu - timedelta(days=7 - tanggal_svd_7_abu)
  446.             svd_7_end_dwh = tanggal_golive_abu + timedelta(days=1)
  447.             _logger.info("%s---------- start dwh 7" % svd_7_start_dwh)
  448.             _logger.info("%s---------- end dwh7" % svd_7_end_dwh)
  449.         # 14
  450.         if validasi_tanggal_golive_14 > tanggal_golive_abu:
  451.             _logger.info("-------------- KONDISI 14 YANG PERTAMA -----------------------")
  452.             tanggal_svd_14_abu = (today - tanggal_golive_abu).days  # 22
  453.             svd_14_start_abu = validasi_tanggal_golive_14
  454.             svd_14_end_abu = today
  455.             _logger.info("%s---------- start abu 14" % svd_14_start_abu)
  456.             _logger.info("%s---------- end abu 14" % svd_14_end_abu)
  457.             svd_14_start_dwh = svd_14_start_abu + timedelta(days=180)
  458.             svd_14_end_dwh = svd_14_end_abu + timedelta(days=180)
  459.             _logger.info("%s---------- start dwh 14" % svd_14_start_dwh)
  460.             _logger.info("%s---------- end dwh14" % svd_14_end_dwh)
  461.         else:
  462.             _logger.info("-------------- KONDISI 14 YANG KEDUA -----------------------")
  463.             tanggal_svd_14_abu = (today - tanggal_golive_abu).days
  464.             _logger.info("tanggal_svd_14_abu ------------ %s" % tanggal_svd_14_abu)
  465.             svd_14_start_abu = validasi_tanggal_golive_14
  466.             svd_14_end_abu = today
  467.             _logger.info("%s---------- start abu 14" % svd_14_start_abu)
  468.             _logger.info("%s---------- end abu 14" % svd_14_end_abu)
  469.             svd_14_start_dwh = tanggal_golive_abu - timedelta(days=14 - tanggal_svd_14_abu)
  470.             svd_14_end_dwh = tanggal_golive_abu + timedelta(days=1)
  471.             _logger.info("%s---------- start dwh 14" % svd_14_start_dwh)
  472.             _logger.info("%s---------- end dwh14" % svd_14_end_dwh)
  473.         # 30
  474.         if validasi_tanggal_golive_30 > tanggal_golive_abu:
  475.             _logger.info("-------------- KONDISI 30 YANG PERTAMA -----------------------")
  476.             tanggal_svd_30_abu = (today - tanggal_golive_abu).days  # 22
  477.             svd_30_start_abu = validasi_tanggal_golive_30
  478.             svd_30_end_abu = today
  479.             _logger.info("%s---------- start abu 30" % svd_30_start_abu)
  480.             _logger.info("%s---------- end abu 30" % svd_30_end_abu)
  481.             svd_30_start_dwh = svd_30_start_abu + timedelta(days=180)
  482.             svd_30_end_dwh = svd_30_end_abu + timedelta(days=180)
  483.             _logger.info("%s---------- start dwh 30" % svd_30_start_dwh)
  484.             _logger.info("%s---------- end dwh30" % svd_30_end_dwh)
  485.         else:
  486.             _logger.info("-------------- KONDISI 30 YANG KEDUA -----------------------")
  487.             tanggal_svd_30_abu = (today - tanggal_golive_abu).days
  488.             svd_30_start_abu = validasi_tanggal_golive_30
  489.             svd_30_end_abu = today
  490.             _logger.info("%s---------- start abu 30" % svd_30_start_abu)
  491.             _logger.info("%s---------- end abu 30" % svd_30_end_abu)
  492.             svd_30_start_dwh = tanggal_golive_abu - timedelta(days=30 - tanggal_svd_30_abu)
  493.             svd_30_end_dwh = tanggal_golive_abu + timedelta(days=1)
  494.             _logger.info("%s---------- start dwh 30" % svd_30_start_dwh)
  495.             _logger.info("%s---------- end dwh30" % svd_30_end_dwh)
  496.         # 60
  497.         if validasi_tanggal_golive_60 > tanggal_golive_abu:
  498.             _logger.info("-------------- KONDISI 60 YANG PERTAMA -----------------------")
  499.             tanggal_svd_60_abu = (today - tanggal_golive_abu).days  # 22
  500.             svd_60_start_abu = validasi_tanggal_golive_60
  501.             svd_60_end_abu = today
  502.             _logger.info("%s---------- start abu 60" % svd_60_start_abu)
  503.             _logger.info("%s---------- end abu 60" % svd_60_end_abu)
  504.             svd_60_start_dwh = svd_60_start_abu + timedelta(days=180)
  505.             svd_60_end_dwh = svd_60_end_abu+ timedelta(days=180)
  506.             _logger.info("%s---------- start dwh 60" % svd_60_start_dwh)
  507.             _logger.info("%s---------- end dwh60" % svd_60_end_dwh)
  508.         else:
  509.             _logger.info("-------------- KONDISI 60 YANG KEDUA -----------------------")
  510.             tanggal_svd_60_abu = (today - tanggal_golive_abu).days
  511.             svd_60_start_abu = validasi_tanggal_golive_60
  512.             svd_60_end_abu = today
  513.             _logger.info("%s---------- start abu 60" % svd_60_start_abu)
  514.             _logger.info("%s---------- end abu 60" % svd_60_end_abu)
  515.             svd_60_start_dwh = tanggal_golive_abu - timedelta(days=60 - tanggal_svd_60_abu)
  516.             svd_60_end_dwh = tanggal_golive_abu + timedelta(days=1)
  517.             _logger.info("%s---------- start dwh 60" % svd_60_start_dwh)
  518.             _logger.info("%s---------- end dwh60" % svd_60_end_dwh)
  519.         # 90
  520.         if validasi_tanggal_golive_90 > tanggal_golive_abu:
  521.             _logger.info("-------------- KONDISI 90 YANG PERTAMA -----------------------")
  522.             tanggal_svd_90_abu = (today - tanggal_golive_abu).days  # 22
  523.             svd_90_start_abu = validasi_tanggal_golive_90
  524.             svd_90_end_abu = today
  525.             _logger.info("%s---------- start abu 90" % svd_90_start_abu)
  526.             _logger.info("%s---------- end abu 90" % svd_90_end_abu)
  527.             svd_90_start_dwh = svd_90_start_abu + timedelta(days=180)
  528.             svd_90_end_dwh = svd_90_end_abu + timedelta(days=180)
  529.             _logger.info("%s---------- start dwh 90" % svd_90_start_dwh)
  530.             _logger.info("%s---------- end dwh90" % svd_90_end_dwh)
  531.         else:
  532.             _logger.info("-------------- KONDISI 90 YANG KEDUA -----------------------")
  533.             tanggal_svd_90_abu = (today - tanggal_golive_abu).days
  534.             svd_90_start_abu = validasi_tanggal_golive_90
  535.             svd_90_end_abu = today
  536.             _logger.info("%s---------- start abu 90" % svd_90_start_abu)
  537.             _logger.info("%s---------- end abu 90" % svd_90_end_abu)
  538.             svd_90_start_dwh = tanggal_golive_abu - timedelta(days=90 - tanggal_svd_90_abu)
  539.             svd_90_end_dwh = tanggal_golive_abu + timedelta(days=1)
  540.             _logger.info("%s---------- start dwh 90" % svd_90_start_dwh)
  541.             _logger.info("%s---------- end dwh90" % svd_90_end_dwh)
  542.         # 180
  543.         if validasi_tanggal_golive_180 > tanggal_golive_abu:
  544.             _logger.info("-------------- KONDISI 180 YANG PERTAMA -----------------------")
  545.             tanggal_svd_180_abu = (today - tanggal_golive_abu).days  # 22
  546.             svd_180_start_abu = validasi_tanggal_golive_180
  547.             svd_180_end_abu = today
  548.             _logger.info("%s---------- start abu 180" % svd_180_start_abu)
  549.             _logger.info("%s---------- end abu 180" % svd_180_end_abu)
  550.             svd_180_start_dwh = svd_180_start_abu + timedelta(days=180)
  551.             svd_180_end_dwh = svd_180_end_abu + timedelta(days=180)
  552.             _logger.info("%s---------- start dwh 180" % svd_180_start_dwh)
  553.             _logger.info("%s---------- end dwh180" % svd_180_end_dwh)
  554.         else:
  555.             tanggal_svd_180_abu = (today - tanggal_golive_abu).days
  556.             _logger.info("-------------- KONDISI 180 YANG KEDUA -----------------------")
  557.             svd_180_start_abu = validasi_tanggal_golive_180
  558.             svd_180_end_abu = today
  559.             _logger.info("%s---------- start abu 180" % svd_180_start_abu)
  560.             _logger.info("%s---------- end abu 180" % svd_180_end_abu)
  561.             svd_180_start_dwh = tanggal_golive_abu - timedelta(days=180 - tanggal_svd_180_abu)
  562.             svd_180_end_dwh = tanggal_golive_abu + timedelta(days=1)
  563.             _logger.info("%s---------- start dwh 180" % svd_180_start_dwh)
  564.             _logger.info("%s---------- end dwh180" % svd_180_end_dwh)
  565.  
  566.         server = self.env['integration.external.db'].sudo().search([('server', '=', 'DWH')], limit=1)
  567.  
  568.         done_process = 0
  569.         if server:
  570.             conn = server.connection_db()
  571.             cur = conn.cursor()
  572.             _logger.info("-------------------- MASUK KONDISI YANG ABU")
  573.             conn = server.connection_db()
  574.             cur = conn.cursor()
  575.             sql = """
  576.                
  577.            """
  578.             cur.execute(
  579.                 """
  580.                    WITH abu_sales AS (
  581.    SELECT
  582.        '' AS MAINACCOUNTID,
  583.        '' AS ACCOUNT_NAME,
  584.        A."INVOICEID",
  585.        date_part('year', cast(A."INVOICEDATE" as DATE)) AS INV_YEAR,
  586.        date_part('month', cast(A."INVOICEDATE" as DATE)) AS INV_MONTH,
  587.        A."SALESID",
  588.        A."INVOICEACCOUNT",
  589.        LOWER(A."INVOICINGNAME") AS INVOICINGNAME,
  590.        cast(A."INVOICEDATE" as DATE) AS INVOICEDATE,
  591.        A."INVENTLOCATIONID",
  592.        B."ITEMID",
  593.        B."QTY",
  594.        B."SALESPRICE",
  595.        B."LINEAMOUNTTAX",    
  596.        B."LINEAMOUNT",
  597.        LOWER(B."NAME") AS PRODUCTNAME,
  598.        F."INVENTSITEID",
  599.        E."ITEMGROUPID",
  600.        F."SEGMENTID",
  601.        F."PAYMTERMID",
  602.        LOWER(G."ABUITEMGROUPID") AS ABUITEMGROUPID,
  603.        LOWER(H."DESCRIPTION") AS SEGMENT_NAME,
  604.        B."LINENUM",
  605.        A."PRINTMGMTSITEID" AS SITE,
  606.        I."JOBTITLE" AS "Jobtitle ABU"
  607.    FROM raw_axsub."CUSTINVOICEJOUR" AS A
  608.    INNER JOIN raw_axsub."CUSTINVOICETRANS" AS B ON B."INVOICEID" = A."INVOICEID"
  609.    INNER JOIN raw_axsub."INVENTITEMGROUPITEM" AS E ON E."ITEMID" = B."ITEMID"
  610.    INNER JOIN raw_axsub."CUSTTABLE" AS F ON F."ACCOUNTNUM" = A."INVOICEACCOUNT"
  611.    INNER JOIN raw_axsub."ABUITEMGROUP" AS G ON G."ITEMID" = B."ITEMID"
  612.    INNER JOIN raw_axsub."SMMBUSRELSEGMENTGROUP" AS H ON H."SEGMENTID" = F."SEGMENTID"
  613.    LEFT JOIN raw_axsub."SALESTABLE" AS I ON B."SALESID" = I."SALESID"
  614.    WHERE F."SEGMENTID" <> 'SC23'
  615.    AND A."INVOICEDATE" BETWEEN NOW() - INTERVAL '365 days' AND NOW()
  616. ),
  617. aggregated_sales AS (
  618.    SELECT
  619.        "ITEMID",
  620.        "INVENTSITEID",
  621.        SUM(CASE WHEN INVOICEDATE BETWEEN %(svd_7_start_dwh)s and %(svd_7_end_dwh)s THEN "QTY" ELSE 0 END) AS qty_sales_7,
  622.        SUM(CASE WHEN INVOICEDATE BETWEEN %(svd_14_start_dwh)s and %(svd_14_end_dwh)s THEN "QTY" ELSE 0 END) AS qty_sales_14,
  623.        SUM(CASE WHEN INVOICEDATE BETWEEN %(svd_30_start_dwh)s and %(svd_30_end_dwh)s THEN "QTY" ELSE 0 END) AS qty_sales_30,
  624.        SUM(CASE WHEN INVOICEDATE BETWEEN %(svd_60_start_dwh)s and %(svd_60_end_dwh)s THEN "QTY" ELSE 0 END) AS qty_sales_60,
  625.        SUM(CASE WHEN INVOICEDATE BETWEEN %(svd_90_start_dwh)s and %(svd_90_end_dwh)s THEN "QTY" ELSE 0 END) AS qty_sales_90,
  626.        SUM(CASE WHEN INVOICEDATE BETWEEN %(svd_180_start_dwh)s and %(svd_180_end_dwh)s THEN "QTY" ELSE 0 END) AS qty_sales_180
  627.    FROM abu_sales
  628.    GROUP BY "ITEMID", "INVENTSITEID"
  629. ),
  630. available_days AS (
  631.    SELECT
  632.        "ITEMID" AS product_code,
  633.        "SITEID" AS branch_code,
  634.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN %(svd_7_start_dwh)s and %(svd_7_end_dwh)s THEN "DATED" ELSE NULL END) AS ttl_avday_7,
  635.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN %(svd_14_start_dwh)s and %(svd_14_end_dwh)s THEN "DATED" ELSE NULL END) AS ttl_avday_14,
  636.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN %(svd_30_start_dwh)s and %(svd_30_end_dwh)s THEN "DATED" ELSE NULL END) AS ttl_avday_30,
  637.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN %(svd_60_start_dwh)s and %(svd_60_end_dwh)s THEN "DATED" ELSE NULL END) AS ttl_avday_60,
  638.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN %(svd_90_start_dwh)s and %(svd_90_end_dwh)s THEN "DATED" ELSE NULL END) AS ttl_avday_90,
  639.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN %(svd_180_start_dwh)s and %(svd_180_end_dwh)s THEN "DATED" ELSE NULL END) AS ttl_avday_180
  640.    FROM raw_dx."ABUSNAPSHOT"
  641.    WHERE "QTY" > 20
  642.    GROUP BY "ITEMID", "SITEID"
  643. )
  644. SELECT
  645.    a."ITEMID",
  646.    TRIM(a."SITEID") AS SITEID,
  647.    '',
  648.    '',
  649.    '',
  650.    COALESCE(max(sls.qty_sales_7), 0) AS qty_sales_7,
  651.    COALESCE(max(avl.ttl_avday_7), 0) AS av_days_7,
  652.    COALESCE(COALESCE(max(sls.qty_sales_7), 0) / NULLIF(COALESCE(max(avl.ttl_avday_7), 0),0),0) AS svd_7,
  653.    COALESCE(max(sls.qty_sales_14), 0) AS qty_sales_14,
  654.    COALESCE(max(avl.ttl_avday_14), 0) AS av_days_14,
  655.    COALESCE(COALESCE(max(sls.qty_sales_14), 0) / NULLIF(COALESCE(max(avl.ttl_avday_14), 0),0),0) AS svd_14,
  656.    COALESCE(max(sls.qty_sales_30), 0) AS qty_sales_30,
  657.    COALESCE(max(avl.ttl_avday_30), 0) AS av_days_30,
  658.    COALESCE(COALESCE(max(sls.qty_sales_30), 0) / NULLIF(COALESCE(max(avl.ttl_avday_30), 0),0),0) AS svd_30,
  659.    COALESCE(max(sls.qty_sales_60), 0) AS qty_sales_60,
  660.    COALESCE(max(avl.ttl_avday_60), 0) AS av_days_60,
  661.    COALESCE(COALESCE(max(sls.qty_sales_60), 0) / NULLIF(COALESCE(max(avl.ttl_avday_60), 0),0),0) AS svd_60,
  662.    COALESCE(max(sls.qty_sales_90), 0) AS qty_sales_90,
  663.    COALESCE(max(avl.ttl_avday_90), 0) AS av_days_90,
  664.    COALESCE(COALESCE(max(sls.qty_sales_90), 0) / NULLIF(COALESCE(max(avl.ttl_avday_90), 0),0),0) AS svd_90,
  665.    COALESCE(max(sls.qty_sales_180), 0) AS qty_sales_180,
  666.    COALESCE(max(avl.ttl_avday_180), 0) AS av_days_180,
  667.    COALESCE(COALESCE(max(sls.qty_sales_180), 0) / NULLIF(COALESCE(max(avl.ttl_avday_180), 0),0),0) AS svd_180
  668. FROM (
  669.    SELECT
  670.        b."ITEMID",
  671.        b."SITEID"
  672.    FROM raw_dx."ABUSNAPSHOT" b
  673.    GROUP BY b."ITEMID", b."SITEID"
  674. ) a
  675. LEFT JOIN aggregated_sales sls ON TRIM(sls."ITEMID") = TRIM(a."ITEMID") AND TRIM(sls."INVENTSITEID") = TRIM(a."SITEID")
  676. LEFT JOIN available_days avl ON TRIM(avl.product_code) = TRIM(a."ITEMID") AND TRIM(avl.branch_code) = TRIM(a."SITEID")
  677. GROUP BY a."ITEMID", a."SITEID"
  678. HAVING
  679.    COUNT(avl.ttl_avday_7) > 0
  680.    AND COUNT(avl.ttl_avday_14) > 0
  681.    AND COUNT(avl.ttl_avday_30) > 0
  682.    AND COUNT(avl.ttl_avday_60) > 0
  683.    AND COUNT(avl.ttl_avday_90) > 0
  684.    AND COUNT(avl.ttl_avday_180) > 0;
  685.                """, {
  686.                     "svd_7_start_dwh": svd_7_start_dwh,
  687.                     "svd_7_end_dwh": svd_7_end_dwh,
  688.                     "svd_14_start_dwh": svd_14_start_dwh,
  689.                     "svd_14_end_dwh": svd_14_end_dwh,
  690.                     "svd_30_start_dwh": svd_30_start_dwh,
  691.                     "svd_30_end_dwh": svd_30_end_dwh,
  692.                     "svd_60_start_dwh": svd_60_start_dwh,
  693.                     "svd_60_end_dwh": svd_60_end_dwh,
  694.                     "svd_90_start_dwh": svd_90_start_dwh,
  695.                     "svd_90_end_dwh": svd_90_end_dwh,
  696.                     "svd_180_start_dwh": svd_180_start_dwh,
  697.                     "svd_180_end_dwh": svd_180_end_dwh,
  698.  
  699.                 }
  700.             )
  701.             query_database_dwh_abu = cur.fetchall()
  702.             if query_database_dwh_abu:
  703.                 for data_query_dwh_abu in query_database_dwh_abu:
  704.                     # _logger.info("%s ---------------- masuk kondisi kalo ada ngab -------------"%query_database_dwh_abu)
  705.                     product_code = data_query_dwh_abu[0]
  706.                     branch_code = data_query_dwh_abu[1]
  707.                     product_name = data_query_dwh_abu[2]
  708.                     product_category = data_query_dwh_abu[3]
  709.                     sub_category_name = data_query_dwh_abu[4]
  710.                     total_sales_qty_7 = data_query_dwh_abu[5]
  711.                     days_stock_available_7 = data_query_dwh_abu[6]
  712.                     svd_7 = data_query_dwh_abu[7]
  713.                     total_sales_qty_14 = data_query_dwh_abu[8]
  714.                     days_stock_available_14 = data_query_dwh_abu[9]
  715.                     svd_14 = data_query_dwh_abu[10]
  716.                     total_sales_qty_30 = data_query_dwh_abu[11]
  717.                     days_stock_available_30 = data_query_dwh_abu[12]
  718.                     svd_30 = data_query_dwh_abu[13]
  719.                     total_sales_qty_60 = data_query_dwh_abu[14]
  720.                     days_stock_available_60 = data_query_dwh_abu[15]
  721.                     svd_60 = data_query_dwh_abu[16]
  722.                     total_sales_qty_90 = data_query_dwh_abu[17]
  723.                     days_stock_available_90 = data_query_dwh_abu[18]
  724.                     svd_90 = data_query_dwh_abu[19]
  725.                     total_sales_qty_180 = data_query_dwh_abu[20]
  726.                     days_stock_available_180 = data_query_dwh_abu[21]
  727.                     svd_180 = data_query_dwh_abu[22]
  728.  
  729.                     convert_branch_abu_lama_to_local_abu = self.env['branch.code'].sudo().search([('code','=',branch_code)])
  730.                     for branch_abu_lama in convert_branch_abu_lama_to_local_abu:
  731.                         abu_lama_branch_local_abu = branch_abu_lama.branch_id.code
  732.  
  733.  
  734.                     query_create_svd_from_dwh_abu = """
  735.                                                        INSERT INTO sbb_snapshot
  736.                                                        ("product_code",
  737.                                                        "product_name",
  738.                                                        "product_category",
  739.                                                        "sub_category_name",
  740.                                                        "branch_code",
  741.                                                        "avg_7",
  742.                                                        "avg_14",
  743.                                                        "avg_30",
  744.                                                        "avg_60",
  745.                                                        "avg_90",
  746.                                                        "avg_180",
  747.                                                        "total_sales_qty_7",
  748.                                                        "total_sales_qty_14",
  749.                                                        "total_sales_qty_30",
  750.                                                        "total_sales_qty_60",
  751.                                                        "total_sales_qty_90",
  752.                                                        "total_sales_qty_180",
  753.                                                        "days_stock_available_7",
  754.                                                        "days_stock_available_14",
  755.                                                        "days_stock_available_30",
  756.                                                        "days_stock_available_60",
  757.                                                        "days_stock_available_90",
  758.                                                        "days_stock_available_180",
  759.                                                        "date",
  760.                                                        "type_data_svd")
  761.                                                        VALUES (
  762.                                                            %(product_code)s,
  763.                                                            %(product_name)s,
  764.                                                            %(product_category)s,
  765.                                                            %(sub_category_name)s,
  766.                                                            %(branch_code)s,
  767.                                                            %(avg_7)s,
  768.                                                            %(avg_14)s,
  769.                                                            %(avg_30)s,
  770.                                                            %(avg_60)s,
  771.                                                            %(avg_90)s,
  772.                                                            %(avg_180)s,
  773.                                                            %(total_sales_qty_7)s,
  774.                                                            %(total_sales_qty_14)s,
  775.                                                            %(total_sales_qty_30)s,
  776.                                                            %(total_sales_qty_60)s,
  777.                                                            %(total_sales_qty_90)s,
  778.                                                            %(total_sales_qty_180)s,
  779.                                                            %(days_stock_available_7)s,
  780.                                                            %(days_stock_available_14)s,
  781.                                                            %(days_stock_available_30)s,
  782.                                                            %(days_stock_available_60)s,
  783.                                                            %(days_stock_available_90)s,
  784.                                                            %(days_stock_available_180)s,
  785.                                                            current_date,
  786.                                                            'abu')
  787.                                                    """
  788.                     execute_create_svd_snapshot = self.env.cr.execute(query_create_svd_from_dwh_abu, {
  789.                         "product_code": product_code,
  790.                         "product_name": product_name,
  791.                         "product_category": product_category,
  792.                         "sub_category_name": sub_category_name,
  793.                         "branch_code": abu_lama_branch_local_abu,
  794.                         "avg_7": svd_7,
  795.                         "avg_14": svd_14,
  796.                         "avg_30": svd_30,
  797.                         "avg_60": svd_60,
  798.                         "avg_90": svd_90,
  799.                         "avg_180": svd_180,
  800.                         "total_sales_qty_7": total_sales_qty_7 if total_sales_qty_7 else 0,
  801.                         "total_sales_qty_14": total_sales_qty_14  if total_sales_qty_7 else 0,
  802.                         "total_sales_qty_30": total_sales_qty_30  if total_sales_qty_30 else 0,
  803.                         "total_sales_qty_60": total_sales_qty_60  if total_sales_qty_60 else 0,
  804.                         "total_sales_qty_90": total_sales_qty_90  if total_sales_qty_90 else 0,
  805.                         "total_sales_qty_180": total_sales_qty_180  if total_sales_qty_180 else 0,
  806.                         "days_stock_available_7": days_stock_available_7  if days_stock_available_7 else 0,
  807.                         "days_stock_available_14": days_stock_available_14  if days_stock_available_14 else 0,
  808.                         "days_stock_available_30": days_stock_available_30  if days_stock_available_30 else 0,
  809.                         "days_stock_available_60": days_stock_available_60  if days_stock_available_60 else 0,
  810.                         "days_stock_available_90": days_stock_available_90  if days_stock_available_90 else 0,
  811.                         "days_stock_available_180": days_stock_available_180  if days_stock_available_180 else 0
  812.                     })
  813.                     self.env.cr.commit()
  814.                     # _logger.info("----------- DATA SNAPSHOT YANG DI CREATE %s"%execute_create_svd_snapshot)
  815.                     query = """
  816.                    select a.product_id as product_id,
  817.                    a.product_code as product_code,
  818.                    a.product_name as product_name,
  819.                    a.branch_id as branch_id,
  820.                    a.branch_name as branch_name,
  821.                    a.branch_code as branch_code,
  822.                    a.category_name as category_name,
  823.                    a.sub_category_name as sub_category_name,
  824.                    coalesce(max(sales7.qty),0) as qty_sales_7,
  825.                    max(soh7.av_days) av_days_7,
  826.                    nullif(max(sales7.qty),0) / nullif(max(soh7.av_days),0) as svd_7,
  827.                    coalesce(max(sales14.qty),0) as qty_sales_14,
  828.                    max(soh14.av_days) av_days_14,
  829.                    nullif(max(sales14.qty),0) / nullif(max(soh14.av_days),0) as svd_14,
  830.                    coalesce(max(sales30.qty),0) as qty_sales_30,
  831.                    max(soh30.av_days) av_days_30,
  832.                    nullif(max(sales30.qty),0) / nullif(max(soh30.av_days),0) as svd_30,
  833.                    coalesce(max(sales60.qty),0) as qty_sales_60,
  834.                    max(soh60.av_days) av_days_60,
  835.                    nullif(max(sales60.qty),0) / nullif(max(soh60.av_days),0) as svd_60,
  836.                    coalesce(max(sales90.qty),0) as qty_sales_90,
  837.                    max(soh90.av_days) av_days_90,
  838.                    nullif(max(sales90.qty),0) / nullif(max(soh90.av_days),0) as svd_90,
  839.                    coalesce(max(sales180.qty),0) as qty_sales_180,
  840.                    max(soh180.av_days) av_days_180,
  841.                    nullif(max(sales180.qty),0) / nullif(max(soh180.av_days),0) as svd_180
  842.                    from (
  843.                            select pp1.id as product_id,
  844.                            pt1."name" as product_name,
  845.                            pp1.default_code as product_code,
  846.                            pc1."name" as category_name,
  847.                            apc1."name" as sub_category_name,
  848.                            rb1.id as branch_id,
  849.                            rb1."name" as branch_name,
  850.                            rb1.code as branch_code
  851.                            from account_move_line aml1
  852.                            left join account_move am1 on am1.id = aml1.move_id
  853.                            left join account_account aa1 on aml1.account_id = aa1.id
  854.                            left join product_product pp1 on aml1.product_id = pp1.id
  855.                            left join product_template pt1 on pp1.product_tmpl_id = pt1.id
  856.                            left join product_category pc1 on pt1.categ_id = pc1.id
  857.                            left join attribute_product_category apc1 on pt1.attribute_categ_id = apc1.id  
  858.                            left join res_branch rb1 on aml1.branch_id = rb1.id
  859.                            where pt1.item_type = 'commercial'
  860.                            group by 1,2,3,4,5,6,7,8) a
  861.                    left join (
  862.                            select
  863.                                aml2.product_id as product_id,
  864.                                am2.branch_id as branch_id,
  865.                                sum(aml2.quantity) as qty
  866.                                from account_move_line aml2
  867.                                left join account_move am2 on am2.id = aml2.move_id
  868.                                left join account_account aa2 on aml2.account_id = aa2.id
  869.                                left join res_branch rb2 on aml2.branch_id = rb2.id
  870.                                where am2.move_type in ('out_invoice','out_refund')
  871.                                and aa2.account_type = 'income'
  872.                                and coalesce(am2.is_contract, false) != true
  873.                                and am2.date BETWEEN %(svd_7_start_abu)s AND %(svd_7_end_abu)s
  874.                                and aml2.partner_id not in (
  875.                                    select
  876.                                        partner_id
  877.                                    from mapping_virtual_customer_branch mvcb
  878.                                )
  879.                                group by 1,2
  880.                    ) sales7 on a.product_id = sales7.product_id and sales7.branch_id = a.branch_id
  881.                    left join (
  882.                            select product_id,product_code, branch_id, count(product_id) as av_days from (
  883.                                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  884.                                    from soh_snapshot ss
  885.                                    left join product_product pp on ss.product = pp.default_code
  886.                                    left join res_branch rb on ss.branch = rb.code
  887.                                    where date between %(svd_7_start_abu)s AND %(svd_7_end_abu)s
  888.                                    group by 1,2,3,4
  889.                        ) raw_soh_7
  890.                        where raw_soh_7.ttl_qty > 20
  891.                        group by 1,2,3
  892.                    ) soh7 on a.product_id = soh7.product_id and a.branch_id = soh7.branch_id
  893.                    left join (
  894.                            select
  895.                                aml2.product_id as product_id,
  896.                                am2.branch_id as branch_id,
  897.                                sum(aml2.quantity) as qty
  898.                                from account_move_line aml2
  899.                                left join account_move am2 on am2.id = aml2.move_id
  900.                                left join account_account aa2 on aml2.account_id = aa2.id
  901.                                left join res_branch rb2 on aml2.branch_id = rb2.id
  902.                                where am2.move_type in ('out_invoice','out_refund')
  903.                                and aa2.account_type = 'income'
  904.                                and coalesce(am2.is_contract, false) != true
  905.                                and am2.date BETWEEN %(svd_14_start_abu)s AND %(svd_14_end_abu)s
  906.                                and aml2.partner_id not in (
  907.                                    select
  908.                                        partner_id
  909.                                    from mapping_virtual_customer_branch mvcb
  910.                                )
  911.                                group by 1,2
  912.                    ) sales14 on a.product_id = sales14.product_id and sales14.branch_id = a.branch_id
  913.                    left join (
  914.                            select product_id,product_code, branch_id, count(product_id) as av_days from (
  915.                                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  916.                                    from soh_snapshot ss
  917.                                    left join product_product pp on ss.product = pp.default_code
  918.                                    left join res_branch rb on ss.branch = rb.code
  919.                                    where date between %(svd_14_start_abu)s AND %(svd_14_end_abu)s
  920.                                    group by 1,2,3,4
  921.                        ) raw_soh_7
  922.                        where raw_soh_7.ttl_qty > 20
  923.                        group by 1,2,3
  924.                    ) soh14 on a.product_id = soh14.product_id and a.branch_id = soh14.branch_id
  925.                    left join (
  926.                            select
  927.                                aml2.product_id as product_id,
  928.                                am2.branch_id as branch_id,
  929.                                sum(aml2.quantity) as qty
  930.                                from account_move_line aml2
  931.                                left join account_move am2 on am2.id = aml2.move_id
  932.                                left join account_account aa2 on aml2.account_id = aa2.id
  933.                                left join res_branch rb2 on aml2.branch_id = rb2.id
  934.                                where am2.move_type in ('out_invoice','out_refund')
  935.                                and aa2.account_type = 'income'
  936.                                and coalesce(am2.is_contract, false) != true
  937.                                and am2.date BETWEEN %(svd_30_start_abu)s AND %(svd_30_end_abu)s
  938.                                and aml2.partner_id not in (
  939.                                    select
  940.                                        partner_id
  941.                                    from mapping_virtual_customer_branch mvcb
  942.                                )
  943.                                group by 1,2
  944.                    ) sales30 on a.product_id = sales30.product_id and sales30.branch_id = a.branch_id
  945.                    left join (
  946.                        select product_id,product_code, branch_id, count(product_id) as av_days from (
  947.                            select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  948.                                    from soh_snapshot ss
  949.                                    left join product_product pp on ss.product = pp.default_code
  950.                                    left join res_branch rb on ss.branch = rb.code
  951.                                    where date between %(svd_30_start_abu)s AND %(svd_30_end_abu)s
  952.                                    group by 1,2,3,4
  953.                        ) raw_soh_7
  954.                        where raw_soh_7.ttl_qty > 20
  955.                        group by 1,2,3
  956.                    ) soh30 on a.product_id = soh30.product_id and a.branch_id = soh30.branch_id
  957.                    left join (
  958.                            select
  959.                                aml2.product_id as product_id,
  960.                                am2.branch_id as branch_id,
  961.                                sum(aml2.quantity) as qty
  962.                                from account_move_line aml2
  963.                                left join account_move am2 on am2.id = aml2.move_id
  964.                                left join account_account aa2 on aml2.account_id = aa2.id
  965.                                left join res_branch rb2 on aml2.branch_id = rb2.id
  966.                                where am2.move_type in ('out_invoice','out_refund')
  967.                                and aa2.account_type = 'income'
  968.                                and coalesce(am2.is_contract, false) != true
  969.                                and am2.date BETWEEN %(svd_60_start_abu)s AND %(svd_60_end_abu)s
  970.                                and aml2.partner_id not in (
  971.                                    select
  972.                                        partner_id
  973.                                    from mapping_virtual_customer_branch mvcb
  974.                                )
  975.                                group by 1,2
  976.                    ) sales60 on a.product_id = sales60.product_id and sales60.branch_id = a.branch_id
  977.                    left join (
  978.                            select product_id,product_code, branch_id, count(product_id) as av_days from (
  979.                                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  980.                                    from soh_snapshot ss
  981.                                    left join product_product pp on ss.product = pp.default_code
  982.                                    left join res_branch rb on ss.branch = rb.code
  983.                                    where date between %(svd_60_start_abu)s AND %(svd_60_end_abu)s
  984.                                    group by 1,2,3,4
  985.                        ) raw_soh_7
  986.                        where raw_soh_7.ttl_qty > 20
  987.                        group by 1,2,3
  988.                    ) soh60 on a.product_id = soh60.product_id and a.branch_id = soh60.branch_id
  989.                    left join (
  990.                            select
  991.                                aml2.product_id as product_id,
  992.                                am2.branch_id as branch_id,
  993.                                sum(aml2.quantity) as qty
  994.                                from account_move_line aml2
  995.                                left join account_move am2 on am2.id = aml2.move_id
  996.                                left join account_account aa2 on aml2.account_id = aa2.id
  997.                                left join res_branch rb2 on aml2.branch_id = rb2.id
  998.                                where am2.move_type in ('out_invoice','out_refund')
  999.                                and aa2.account_type = 'income'
  1000.                                and coalesce(am2.is_contract, false) != true
  1001.                                and am2.date BETWEEN %(svd_90_start_abu)s AND %(svd_90_end_abu)s
  1002.                                and aml2.partner_id not in (
  1003.                                    select
  1004.                                        partner_id
  1005.                                    from mapping_virtual_customer_branch mvcb
  1006.                                )
  1007.                                group by 1,2
  1008.                    ) sales90 on a.product_id = sales90.product_id and sales90.branch_id = a.branch_id
  1009.                    left join (
  1010.                            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1011.                                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1012.                                    from soh_snapshot ss
  1013.                                    left join product_product pp on ss.product = pp.default_code
  1014.                                    left join res_branch rb on ss.branch = rb.code
  1015.                                    where date between %(svd_90_start_abu)s AND %(svd_90_end_abu)s
  1016.                                    group by 1,2,3,4
  1017.                        ) raw_soh_7
  1018.                        where raw_soh_7.ttl_qty > 20
  1019.                        group by 1,2,3
  1020.                    ) soh90 on a.product_id = soh90.product_id and a.branch_id = soh90.branch_id
  1021.                    left join (
  1022.                            select
  1023.                                aml2.product_id as product_id,
  1024.                                am2.branch_id as branch_id,
  1025.                                sum(aml2.quantity) as qty
  1026.                                from account_move_line aml2
  1027.                                left join account_move am2 on am2.id = aml2.move_id
  1028.                                left join account_account aa2 on aml2.account_id = aa2.id
  1029.                                left join res_branch rb2 on aml2.branch_id = rb2.id
  1030.                                where am2.move_type in ('out_invoice','out_refund')
  1031.                                and aa2.account_type = 'income'
  1032.                                and coalesce(am2.is_contract, false) != true
  1033.                                and am2.date BETWEEN %(svd_180_start_abu)s AND %(svd_180_end_abu)s
  1034.                                and aml2.partner_id not in (
  1035.                                    select
  1036.                                        partner_id
  1037.                                    from mapping_virtual_customer_branch mvcb
  1038.                                )
  1039.                                group by 1,2
  1040.                    ) sales180 on a.product_id = sales180.product_id and sales180.branch_id = a.branch_id
  1041.                    left join (
  1042.                            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1043.                                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1044.                                    from soh_snapshot ss
  1045.                                    left join product_product pp on ss.product = pp.default_code
  1046.                                    left join res_branch rb on ss.branch = rb.code
  1047.                                    where date between %(svd_180_start_abu)s AND %(svd_180_end_abu)s
  1048.                                    group by 1,2,3,4
  1049.                        ) raw_soh_7
  1050.                        where raw_soh_7.ttl_qty > 20
  1051.                        group by 1,2,3
  1052.                    ) soh180 on a.product_id = soh180.product_id and a.branch_id = soh180.branch_id
  1053.                    group by 1,2,3,4,5,6,7,8
  1054.                """
  1055.         self.env.cr.execute(query,{
  1056.                         "svd_7_start_abu": svd_7_start_abu,
  1057.                         "svd_7_end_abu": svd_7_end_abu,
  1058.                         "svd_14_start_abu": svd_14_start_abu,
  1059.                         "svd_14_end_abu": svd_14_end_abu,
  1060.                         "svd_30_start_abu": svd_30_start_abu,
  1061.                         "svd_30_end_abu": svd_30_end_abu,
  1062.                         "svd_60_start_abu": svd_60_start_abu,
  1063.                         "svd_60_end_abu": svd_60_end_abu,
  1064.                         "svd_90_start_abu": svd_90_start_abu,
  1065.                         "svd_90_end_abu": svd_90_end_abu,
  1066.                         "svd_180_start_abu": svd_180_start_abu,
  1067.                         "svd_180_end_abu": svd_180_end_abu,
  1068.  
  1069.                     })
  1070.         query_svd_mart_abu_2024 = self.env.cr.dictfetchall()
  1071.         if query_svd_mart_abu_2024:
  1072.             for data_query_abu in query_svd_mart_abu_2024:
  1073.                 print(data_query_abu)
  1074.                 _logger.info("%s-------------------- MART ABU 2024" % data_query_abu)
  1075.                 merging_id = self.env['sbb.snapshot'].sudo().search([('product_code','=',data_query_abu['product_code']),('branch_code','=', data_query_abu['branch_code'])],limit=1)
  1076.                 print('**************************',data_query_abu)
  1077.                 product_id = data_query_abu['product_id']
  1078.                 product_code = data_query_abu['product_code']
  1079.                 product_name = data_query_abu['product_name']['en_US']
  1080.                 branch_id = data_query_abu['branch_id']
  1081.                 branch_name = data_query_abu['branch_name']
  1082.                 branch_code = data_query_abu['branch_code']
  1083.                 category_name = data_query_abu['category_name']
  1084.                 sub_category_name = data_query_abu['sub_category_name']
  1085.                 qty_sales_7 = data_query_abu['qty_sales_7']
  1086.                 av_days_7 = data_query_abu['av_days_7']
  1087.                 svd_7 = data_query_abu['svd_7']
  1088.                 qty_sales_14 = data_query_abu['qty_sales_14']
  1089.                 av_days_14 = data_query_abu['av_days_14']
  1090.                 svd_14 = data_query_abu['svd_14']
  1091.                 qty_sales_30 = data_query_abu['qty_sales_30']
  1092.                 av_days_30 = data_query_abu['av_days_30']
  1093.                 svd_30 = data_query_abu['svd_30']
  1094.                 qty_sales_60 = data_query_abu['qty_sales_60']
  1095.                 av_days_60 = data_query_abu['av_days_60']
  1096.                 svd_60 = data_query_abu['svd_60']
  1097.                 qty_sales_90 = data_query_abu['qty_sales_90']
  1098.                 av_days_90 = data_query_abu['av_days_90']
  1099.                 svd_90 = data_query_abu['svd_90']
  1100.                 qty_sales_180 = data_query_abu['qty_sales_180']
  1101.                 av_days_180 = data_query_abu['av_days_180']
  1102.                 svd_180 = data_query_abu['svd_180']
  1103.                 if not merging_id:
  1104.                     query_create_svd_from_abu = """
  1105.                                                        insert into sbb_snapshot
  1106.                                                        ("product_code","product_name","branch_code","avg_7","avg_14","avg_30","avg_60","avg_90","avg_180","total_sales_qty_7","total_sales_qty_14","total_sales_qty_30","total_sales_qty_60","total_sales_qty_90","total_sales_qty_180","days_stock_available_7","days_stock_available_14","days_stock_available_30","days_stock_available_60","days_stock_available_90","days_stock_available_180","date","type_data_svd")
  1107.                                                        VALUES (%(product_code)s,%(product_name)s,%(branch_code)s,%(avg_7)s,%(avg_14)s,%(avg_30)s,%(avg_60)s,%(avg_90)s,%(avg_180)s,%(total_sales_qty_7)s,%(total_sales_qty_14)s,%(total_sales_qty_30)s,%(total_sales_qty_60)s,%(total_sales_qty_90)s,%(total_sales_qty_180)s,%(days_stock_available_7)s,%(days_stock_available_14)s,%(days_stock_available_30)s,%(days_stock_available_60)s,%(days_stock_available_90)s,%(days_stock_available_180)s,current_date,'abu')
  1108.                                                    """
  1109.                     execute_create_svd_snapshot = self.env.cr.execute(query_create_svd_from_abu, {
  1110.                         "product_code": product_code,
  1111.                         "product_name": product_name,
  1112.                         "branch_code": branch_code,
  1113.                         "avg_7": svd_7,
  1114.                         "avg_14": svd_14,
  1115.                         "avg_30": svd_30,
  1116.                         "avg_60": svd_60,
  1117.                         "avg_90": svd_90,
  1118.                         "avg_180": svd_180,
  1119.                         "total_sales_qty_7": qty_sales_7,
  1120.                         "total_sales_qty_14": qty_sales_14,
  1121.                         "total_sales_qty_30": qty_sales_30,
  1122.                         "total_sales_qty_60": qty_sales_60,
  1123.                         "total_sales_qty_90": qty_sales_90,
  1124.                         "total_sales_qty_180": qty_sales_180,
  1125.                         "days_stock_available_7": av_days_7,
  1126.                         "days_stock_available_14": av_days_14,
  1127.                         "days_stock_available_30": av_days_30,
  1128.                         "days_stock_available_60": av_days_60,
  1129.                         "days_stock_available_90": av_days_90,
  1130.                         "days_stock_available_180": av_days_180,
  1131.                     })
  1132.                 else:
  1133.                     days_stock_available_7 = (float(av_days_7) if av_days_7 else 0) + (float(merging_id.days_stock_available_7) if merging_id.days_stock_available_7 else 0)
  1134.                     if float(days_stock_available_7) > 7:
  1135.                         days_stock_available_7 = 7
  1136.                     days_stock_available_14 = (float(av_days_14) if av_days_14 else 0) + (float(merging_id.days_stock_available_14) if merging_id.days_stock_available_14 else 0)
  1137.                     if float(days_stock_available_14) > 14:
  1138.                         days_stock_available_14 = 14
  1139.                     days_stock_available_30 = (float(av_days_30) if av_days_30 else 0) + (float(merging_id.days_stock_available_30) if merging_id.days_stock_available_30 else 0)
  1140.                     if float(days_stock_available_30) > 30:
  1141.                         days_stock_available_30 = 30
  1142.                     days_stock_available_60 = (float(av_days_60) if av_days_60 else 0) + (float(merging_id.days_stock_available_60) if merging_id.days_stock_available_60 else 0)
  1143.                     if float(days_stock_available_60) > 60:
  1144.                         days_stock_available_60 = 60
  1145.                     days_stock_available_90 = (float(av_days_90) if av_days_90 else 0) + (float(merging_id.days_stock_available_90) if merging_id.days_stock_available_90 else 0)
  1146.                     if float(days_stock_available_90) > 90:
  1147.                         days_stock_available_90 = 90
  1148.                     days_stock_available_180 = (float(av_days_180) if av_days_180 else 0) + (float(merging_id.days_stock_available_180) if merging_id.days_stock_available_180 else 0)
  1149.                     if float(days_stock_available_180) > 180:
  1150.                         days_stock_available_180 = 180
  1151.                     merging_id.sudo().write({
  1152.                         "avg_7": (float(svd_7) if svd_7 else 0) + (float(merging_id.avg_7) if merging_id.avg_7 else 0),
  1153.                         "avg_14": (float(svd_14) if svd_14 else 0) + (
  1154.                             float(merging_id.avg_14) if merging_id.avg_14 else 0),
  1155.                         "avg_30": (float(svd_30) if svd_30 else 0) + (
  1156.                             float(merging_id.avg_30) if merging_id.avg_30 else 0),
  1157.                         "avg_60": (float(svd_60) if svd_60 else 0) + (
  1158.                             float(merging_id.avg_60) if merging_id.avg_60 else 0),
  1159.                         "avg_90": (float(svd_90) if svd_90 else 0) + (
  1160.                             float(merging_id.avg_90) if merging_id.avg_90 else 0),
  1161.                         "avg_180": (float(svd_180) if svd_180 else 0) + (
  1162.                             float(merging_id.avg_180) if merging_id.avg_180 else 0),
  1163.                         "total_sales_qty_7": (float(qty_sales_7) if qty_sales_7 else 0) + (
  1164.                             float(merging_id.total_sales_qty_7) if merging_id.total_sales_qty_7 else 0),
  1165.                         "total_sales_qty_14": (float(qty_sales_14) if qty_sales_14 else 0) + (
  1166.                             float(merging_id.total_sales_qty_14) if merging_id.total_sales_qty_14 else 0),
  1167.                         "total_sales_qty_30": (float(qty_sales_30) if qty_sales_30 else 0) + (
  1168.                             float(merging_id.total_sales_qty_30) if merging_id.total_sales_qty_30 else 0),
  1169.                         "total_sales_qty_60": (float(qty_sales_60) if qty_sales_60 else 0) + (
  1170.                             float(merging_id.total_sales_qty_60) if merging_id.total_sales_qty_60 else 0),
  1171.                         "total_sales_qty_90": (float(qty_sales_90) if qty_sales_90 else 0) + (
  1172.                             float(merging_id.total_sales_qty_90) if merging_id.total_sales_qty_90 else 0),
  1173.                         "total_sales_qty_180": (float(qty_sales_180) if qty_sales_180 else 0) + (
  1174.                             float(merging_id.total_sales_qty_180) if merging_id.total_sales_qty_180 else 0),
  1175.                         "days_stock_available_7": days_stock_available_7,
  1176.                         "days_stock_available_14": days_stock_available_14,
  1177.                         "days_stock_available_30": days_stock_available_30,
  1178.                         "days_stock_available_60": days_stock_available_60,
  1179.                         "days_stock_available_90": days_stock_available_90,
  1180.                         "days_stock_available_180": days_stock_available_180,
  1181.                     })
  1182.                     # query_create_svd_from_abu = """
  1183.                     #                                                         INSERT INTO sbb_snapshot
  1184.                     #                                                         ("product_code",
  1185.                     #                                                         "product_name",
  1186.                     #                                                         "branch_code",
  1187.                     #                                                         "avg_7",
  1188.                     #                                                         "avg_14",
  1189.                     #                                                         "avg_30",
  1190.                     #                                                         "avg_60",
  1191.                     #                                                         "avg_90",
  1192.                     #                                                         "avg_180",
  1193.                     #                                                         "total_sales_qty_7",
  1194.                     #                                                         "total_sales_qty_14",
  1195.                     #                                                         "total_sales_qty_30",
  1196.                     #                                                         "total_sales_qty_60",
  1197.                     #                                                         "total_sales_qty_90",
  1198.                     #                                                         "total_sales_qty_180",
  1199.                     #                                                         "days_stock_available_7",
  1200.                     #                                                         "days_stock_available_14",
  1201.                     #                                                         "days_stock_available_30",
  1202.                     #                                                         "days_stock_available_60",
  1203.                     #                                                         "days_stock_available_90",
  1204.                     #                                                         "days_stock_available_180",
  1205.                     #                                                         "date","type_data_svd")
  1206.                     #                                                         VALUES (%(product_code)s,%(product_name)s,%(branch_code)s,%(avg_7)s,%(avg_14)s,%(avg_30)s,%(avg_60)s,%(avg_90)s,%(avg_180)s,%(total_sales_qty_7)s,%(total_sales_qty_14)s,%(total_sales_qty_30)s,%(total_sales_qty_60)s,%(total_sales_qty_90)s,%(total_sales_qty_180)s,%(days_stock_available_7)s,%(days_stock_available_14)s,%(days_stock_available_30)s,%(days_stock_available_60)s,%(days_stock_available_90)s,%(days_stock_available_180)s,current_date,'abu')
  1207.                     #                                                     """
  1208.                     # execute_create_svd_snapshot = self.env.cr.execute(query_create_svd_from_abu, {
  1209.                     #     "product_code": product_code,
  1210.                     #     "product_name": product_name,
  1211.                     #     "branch_code": branch_code,
  1212.                     #     "avg_7": float(svd_7) if svd_7 else 0 + float(merging_id.avg_7) if merging_id.avg_7 else 0 ,
  1213.                     #     "avg_14": float(svd_14) if svd_14 else 0 + float(merging_id.avg_14) if merging_id.avg_14 else 0 ,
  1214.                     #     "avg_30": float(svd_30) if svd_30 else 0 + float(merging_id.avg_30) if merging_id.avg_30 else 0 ,
  1215.                     #     "avg_60": float(svd_60) if svd_60 else 0 + float(merging_id.avg_60) if merging_id.avg_60 else 0 ,
  1216.                     #     "avg_90": float(svd_90) if svd_90 else 0 + float(merging_id.avg_90) if merging_id.avg_90 else 0 ,
  1217.                     #     "avg_180": float(svd_180) if svd_180 else 0 + float(merging_id.avg_180) if merging_id.avg_180 else 0 ,
  1218.                     #     "total_sales_qty_7": float(qty_sales_7) if qty_sales_7 else 0 + float(merging_id.total_sales_qty_7) if merging_id.total_sales_qty_7 else 0 ,
  1219.                     #     "total_sales_qty_14": float(qty_sales_14) if qty_sales_14 else 0 + float(merging_id.total_sales_qty_14) if merging_id.total_sales_qty_14 else 0 ,
  1220.                     #     "total_sales_qty_30": float(qty_sales_30) if qty_sales_30 else 0 + float(merging_id.total_sales_qty_30) if merging_id.total_sales_qty_30 else 0 ,
  1221.                     #     "total_sales_qty_60": float(qty_sales_60) if qty_sales_60 else 0 + float(merging_id.total_sales_qty_60) if merging_id.total_sales_qty_60 else 0 ,
  1222.                     #     "total_sales_qty_90": float(qty_sales_90) if qty_sales_90 else 0 + float(merging_id.total_sales_qty_90) if merging_id.total_sales_qty_90 else 0 ,
  1223.                     #     "total_sales_qty_180": float(qty_sales_180) if qty_sales_180 else 0 + float(merging_id.total_sales_qty_180) if merging_id.total_sales_qty_180 else 0 ,
  1224.                     #     "days_stock_available_7": float(av_days_7) if av_days_7 else 0 + float(merging_id.total_sales_qty_7) if merging_id.total_sales_qty_7 else 0 ,
  1225.                     #     "days_stock_available_14": float(av_days_14) if av_days_14 else 0 + float(merging_id.total_sales_qty_14) if merging_id.total_sales_qty_14 else 0 ,
  1226.                     #     "days_stock_available_30": float(av_days_30) if av_days_30 else 0 + float(merging_id.total_sales_qty_30) if merging_id.total_sales_qty_30 else 0 ,
  1227.                     #     "days_stock_available_60": float(av_days_60) if av_days_60 else 0 + float(merging_id.total_sales_qty_60) if merging_id.total_sales_qty_60 else 0 ,
  1228.                     #     "days_stock_available_90": float(av_days_90) if av_days_90 else 0 + float(merging_id.total_sales_qty_90) if merging_id.total_sales_qty_90 else 0 ,
  1229.                     #     "days_stock_available_180": float(av_days_180) if av_days_180 else 0 + float(merging_id.total_sales_qty_180) if merging_id.total_sales_qty_180 else 0 ,
  1230.                     # })
  1231.  
  1232.     def query_before_golive_abu(self):
  1233.         server = self.env['integration.external.db'].sudo().search([('server', '=', 'DWH')], limit=1)
  1234.         today = date.today()
  1235.         tampungan_branch_local_abu_dari_abu_lama = []
  1236.  
  1237.         tanggal_golive = self.env['ir.config_parameter'].sudo().get_param('golive_abu_date')
  1238.         tanggal_golive_abu = datetime.strptime(tanggal_golive, '%Y-%m-%d').date()
  1239.  
  1240.         if not server:
  1241.             raise ValidationError(
  1242.                 "Server Database DWH Belum di setting, silahkan di setting external DB nya terlebih dahulu!")
  1243.  
  1244.         if server:
  1245.             conn = server.connection_db()
  1246.             cur = conn.cursor()
  1247.             cur.execute(
  1248.                 """
  1249.                    WITH abu_sales AS (
  1250.    SELECT
  1251.        '' AS MAINACCOUNTID,
  1252.        '' AS ACCOUNT_NAME,
  1253.        A."INVOICEID",
  1254.        date_part('year', cast(A."INVOICEDATE" as DATE)) AS INV_YEAR,
  1255.        date_part('month', cast(A."INVOICEDATE" as DATE)) AS INV_MONTH,
  1256.        A."SALESID",
  1257.        A."INVOICEACCOUNT",
  1258.        LOWER(A."INVOICINGNAME") AS INVOICINGNAME,
  1259.        cast(A."INVOICEDATE" as DATE) AS INVOICEDATE,
  1260.        A."INVENTLOCATIONID",
  1261.        B."ITEMID",
  1262.        B."QTY",
  1263.        B."SALESPRICE",
  1264.        B."LINEAMOUNTTAX",    
  1265.        B."LINEAMOUNT",
  1266.        LOWER(B."NAME") AS PRODUCTNAME,
  1267.        F."INVENTSITEID",
  1268.        E."ITEMGROUPID",
  1269.        F."SEGMENTID",
  1270.        F."PAYMTERMID",
  1271.        LOWER(G."ABUITEMGROUPID") AS ABUITEMGROUPID,
  1272.        LOWER(H."DESCRIPTION") AS SEGMENT_NAME,
  1273.        B."LINENUM",
  1274.        A."PRINTMGMTSITEID" AS SITE,
  1275.        I."JOBTITLE" AS "Jobtitle ABU"
  1276.    FROM raw_axsub."CUSTINVOICEJOUR" AS A
  1277.    INNER JOIN raw_axsub."CUSTINVOICETRANS" AS B ON B."INVOICEID" = A."INVOICEID"
  1278.    INNER JOIN raw_axsub."INVENTITEMGROUPITEM" AS E ON E."ITEMID" = B."ITEMID"
  1279.    INNER JOIN raw_axsub."CUSTTABLE" AS F ON F."ACCOUNTNUM" = A."INVOICEACCOUNT"
  1280.    INNER JOIN raw_axsub."ABUITEMGROUP" AS G ON G."ITEMID" = B."ITEMID"
  1281.    INNER JOIN raw_axsub."SMMBUSRELSEGMENTGROUP" AS H ON H."SEGMENTID" = F."SEGMENTID"
  1282.    LEFT JOIN raw_axsub."SALESTABLE" AS I ON B."SALESID" = I."SALESID"
  1283.    WHERE F."SEGMENTID" <> 'SC23'
  1284.    AND A."INVOICEDATE" BETWEEN NOW() - INTERVAL '365 days' AND NOW()
  1285. ),
  1286. aggregated_sales AS (
  1287.    SELECT
  1288.        "ITEMID",
  1289.        "INVENTSITEID",
  1290.        SUM(CASE WHEN INVOICEDATE BETWEEN NOW() - INTERVAL '7 days' AND NOW() THEN "QTY" ELSE 0 END) AS qty_sales_7,
  1291.        SUM(CASE WHEN INVOICEDATE BETWEEN NOW() - INTERVAL '14 days' AND NOW() THEN "QTY" ELSE 0 END) AS qty_sales_14,
  1292.        SUM(CASE WHEN INVOICEDATE BETWEEN NOW() - INTERVAL '30 days' AND NOW() THEN "QTY" ELSE 0 END) AS qty_sales_30,
  1293.        SUM(CASE WHEN INVOICEDATE BETWEEN NOW() - INTERVAL '60 days' AND NOW() THEN "QTY" ELSE 0 END) AS qty_sales_60,
  1294.        SUM(CASE WHEN INVOICEDATE BETWEEN NOW() - INTERVAL '90 days' AND NOW() THEN "QTY" ELSE 0 END) AS qty_sales_90,
  1295.        SUM(CASE WHEN INVOICEDATE BETWEEN NOW() - INTERVAL '180 days' AND NOW() THEN "QTY" ELSE 0 END) AS qty_sales_180
  1296.    FROM abu_sales
  1297.    GROUP BY "ITEMID", "INVENTSITEID"
  1298. ),
  1299. available_days AS (
  1300.    SELECT
  1301.        "ITEMID" AS product_code,
  1302.        "SITEID" AS branch_code,
  1303.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN NOW() - INTERVAL '7 days' AND NOW() THEN "DATED" ELSE NULL END) AS ttl_avday_7,
  1304.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN NOW() - INTERVAL '14 days' AND NOW() THEN "DATED" ELSE NULL END) AS ttl_avday_14,
  1305.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN NOW() - INTERVAL '30 days' AND NOW() THEN "DATED" ELSE NULL END) AS ttl_avday_30,
  1306.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN NOW() - INTERVAL '60 days' AND NOW() THEN "DATED" ELSE NULL END) AS ttl_avday_60,
  1307.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN NOW() - INTERVAL '90 days' AND NOW() THEN "DATED" ELSE NULL END) AS ttl_avday_90,
  1308.        COUNT(DISTINCT CASE WHEN "DATED" BETWEEN NOW() - INTERVAL '180 days' AND NOW() THEN "DATED" ELSE NULL END) AS ttl_avday_180
  1309.    FROM raw_dx."ABUSNAPSHOT"
  1310.    WHERE "QTY" > 20
  1311.    GROUP BY "ITEMID", "SITEID"
  1312. )
  1313. SELECT
  1314.    a."ITEMID",
  1315.    TRIM(a."SITEID") AS SITEID,
  1316.    COALESCE(max(sls.qty_sales_7), 0) AS qty_sales_7,
  1317.    COALESCE(max(avl.ttl_avday_7), 0) AS av_days_7,
  1318.    COALESCE(COALESCE(max(sls.qty_sales_7), 0) / NULLIF(COALESCE(max(avl.ttl_avday_7), 0),0),0) AS svd_7,
  1319.    COALESCE(max(sls.qty_sales_14), 0) AS qty_sales_14,
  1320.    COALESCE(max(avl.ttl_avday_14), 0) AS av_days_14,
  1321.    COALESCE(COALESCE(max(sls.qty_sales_14), 0) / NULLIF(COALESCE(max(avl.ttl_avday_14), 0),0),0) AS svd_14,
  1322.    COALESCE(max(sls.qty_sales_30), 0) AS qty_sales_30,
  1323.    COALESCE(max(avl.ttl_avday_30), 0) AS av_days_30,
  1324.    COALESCE(COALESCE(max(sls.qty_sales_30), 0) / NULLIF(COALESCE(max(avl.ttl_avday_30), 0),0),0) AS svd_30,
  1325.    COALESCE(max(sls.qty_sales_60), 0) AS qty_sales_60,
  1326.    COALESCE(max(avl.ttl_avday_60), 0) AS av_days_60,
  1327.    COALESCE(COALESCE(max(sls.qty_sales_60), 0) / NULLIF(COALESCE(max(avl.ttl_avday_60), 0),0),0) AS svd_60,
  1328.    COALESCE(max(sls.qty_sales_90), 0) AS qty_sales_90,
  1329.    COALESCE(max(avl.ttl_avday_90), 0) AS av_days_90,
  1330.    COALESCE(COALESCE(max(sls.qty_sales_90), 0) / NULLIF(COALESCE(max(avl.ttl_avday_90), 0),0),0) AS svd_90,
  1331.    COALESCE(max(sls.qty_sales_180), 0) AS qty_sales_180,
  1332.    COALESCE(max(avl.ttl_avday_180), 0) AS av_days_180,
  1333.    COALESCE(COALESCE(max(sls.qty_sales_180), 0) / NULLIF(COALESCE(max(avl.ttl_avday_180), 0),0),0) AS svd_180
  1334. FROM (
  1335.    SELECT
  1336.        b."ITEMID",
  1337.        b."SITEID"
  1338.    FROM raw_dx."ABUSNAPSHOT" b
  1339.    GROUP BY b."ITEMID", b."SITEID"
  1340. ) a
  1341. LEFT JOIN aggregated_sales sls ON TRIM(sls."ITEMID") = TRIM(a."ITEMID") AND TRIM(sls."INVENTSITEID") = TRIM(a."SITEID")
  1342. LEFT JOIN available_days avl ON TRIM(avl.product_code) = TRIM(a."ITEMID") AND TRIM(avl.branch_code) = TRIM(a."SITEID")
  1343. GROUP BY a."ITEMID", a."SITEID"
  1344. HAVING
  1345.    COUNT(avl.ttl_avday_7) > 0
  1346.    AND COUNT(avl.ttl_avday_14) > 0
  1347.    AND COUNT(avl.ttl_avday_30) > 0
  1348.    AND COUNT(avl.ttl_avday_60) > 0
  1349.    AND COUNT(avl.ttl_avday_90) > 0
  1350.    AND COUNT(avl.ttl_avday_180) > 0;
  1351.  
  1352.  
  1353.                """)
  1354.             query_database_dwh_abu = cur.fetchall()
  1355.             if query_database_dwh_abu:
  1356.                 for data_query_dwh_abu in query_database_dwh_abu:
  1357.                     print(data_query_dwh_abu)
  1358.                     # _logger.info("%s ---------------- masuk kondisi kalo ada ngab -------------" %(data_query_dwh_abu))
  1359.                     product_code = data_query_dwh_abu[0]
  1360.                     branch_code = data_query_dwh_abu[1]
  1361.                     # get product template
  1362.                     product_id = self.env['product.product'].search([('default_code','=', product_code)],limit=1)
  1363.                     product_name = product_id.name
  1364.                     product_category = product_id.categ_id.name
  1365.                     sub_category_name = product_id.attribute_categ_id.name
  1366.                     total_sales_qty_7 = data_query_dwh_abu[2]
  1367.                     days_stock_available_7 = data_query_dwh_abu[3]
  1368.                     svd_7 = data_query_dwh_abu[4]
  1369.                     total_sales_qty_14 = data_query_dwh_abu[5]
  1370.                     days_stock_available_14 = data_query_dwh_abu[6]
  1371.                     svd_14 = data_query_dwh_abu[7]
  1372.                     total_sales_qty_30 = data_query_dwh_abu[8]
  1373.                     days_stock_available_30 = data_query_dwh_abu[9]
  1374.                     svd_30 = data_query_dwh_abu[10]
  1375.                     total_sales_qty_60 = data_query_dwh_abu[11]
  1376.                     days_stock_available_60 = data_query_dwh_abu[12]
  1377.                     svd_60 = data_query_dwh_abu[13]
  1378.                     total_sales_qty_90 = data_query_dwh_abu[14]
  1379.                     days_stock_available_90 = data_query_dwh_abu[15]
  1380.                     svd_90 = data_query_dwh_abu[16]
  1381.                     total_sales_qty_180 = data_query_dwh_abu[17]
  1382.                     days_stock_available_180 = data_query_dwh_abu[18]
  1383.                     svd_180 = data_query_dwh_abu[19]
  1384.  
  1385.                     convert_branch_abu_lama_to_local_abu = self.env['branch.code'].sudo().search([('code','=',branch_code)],limit=1)
  1386.                     # for branch_local_abu in convert_branch_abu_lama_to_local_abu:
  1387.                     #     tampungan_branch_local_abu_dari_abu_lama = branch_local_abu.branch_id.code
  1388.                     #     _logger.info("%s ----------------- tampungan_branch_local_abu_dari_abu_lama"%tampungan_branch_local_abu_dari_abu_lama)
  1389.                     if convert_branch_abu_lama_to_local_abu:
  1390.                         tampungan_branch_local_abu_dari_abu_lama = convert_branch_abu_lama_to_local_abu.branch_id.code
  1391.                     else:
  1392.                         tampungan_branch_local_abu_dari_abu_lama = branch_code
  1393.  
  1394.                     query_create_svd_from_dwh_abu = """
  1395.                                                        INSERT INTO sbb_snapshot
  1396.                                                        ("product_code","product_name","product_category","sub_category_name","branch_code","avg_7","avg_14","avg_30","avg_60","avg_90","avg_180","total_sales_qty_7","total_sales_qty_14","total_sales_qty_30","total_sales_qty_60","total_sales_qty_90","total_sales_qty_180","days_stock_available_7","days_stock_available_14","days_stock_available_30","days_stock_available_60","days_stock_available_90","days_stock_available_180","date","type_data_svd")
  1397.                                                        VALUES (%(product_code)s,%(product_name)s,%(product_category)s,%(sub_category_name)s,%(branch_code)s,%(avg_7)s,%(avg_14)s,%(avg_30)s,%(avg_60)s,%(avg_90)s,%(avg_180)s,%(total_sales_qty_7)s,%(total_sales_qty_14)s,%(total_sales_qty_30)s,%(total_sales_qty_60)s,%(total_sales_qty_90)s,%(total_sales_qty_180)s,%(days_stock_available_7)s,%(days_stock_available_14)s,%(days_stock_available_30)s,%(days_stock_available_60)s,%(days_stock_available_90)s,%(days_stock_available_180)s,current_date,'abu')
  1398.                                                    """
  1399.                     execute_create_svd_snapshot = self.env.cr.execute(query_create_svd_from_dwh_abu, {
  1400.                         "product_code": product_code,
  1401.                         "product_name": product_name,
  1402.                         "product_category": product_category,
  1403.                         "sub_category_name": sub_category_name,
  1404.                         "branch_code": tampungan_branch_local_abu_dari_abu_lama,
  1405.                         "avg_7": svd_7,
  1406.                         "avg_14": svd_14,
  1407.                         "avg_30": svd_30,
  1408.                         "avg_60": svd_60,
  1409.                         "avg_90": svd_90,
  1410.                         "avg_180": svd_180,
  1411.                         "total_sales_qty_7": total_sales_qty_7,
  1412.                         "total_sales_qty_14": total_sales_qty_14,
  1413.                         "total_sales_qty_30": total_sales_qty_30,
  1414.                         "total_sales_qty_60": total_sales_qty_60,
  1415.                         "total_sales_qty_90": total_sales_qty_90,
  1416.                         "total_sales_qty_180": total_sales_qty_180,
  1417.                         "days_stock_available_7": days_stock_available_7,
  1418.                         "days_stock_available_14": days_stock_available_14,
  1419.                         "days_stock_available_30": days_stock_available_30,
  1420.                         "days_stock_available_60": days_stock_available_60,
  1421.                         "days_stock_available_90": days_stock_available_90,
  1422.                         "days_stock_available_180": days_stock_available_180,
  1423.  
  1424.                     })
  1425.                     _logger.info("----------- DATA SNAPSHOT YANG DI CREATE %s" % execute_create_svd_snapshot)
  1426.  
  1427.     def query_after_golive_abu(self):
  1428.  
  1429.         today = date.today()
  1430.  
  1431.         minimum_soh = self.env['ir.config_parameter'].sudo().get_param('minimum_soh')
  1432.         minimum_soh = float(minimum_soh)
  1433.         today = date.today()
  1434.         one_week = today - timedelta(days=7)
  1435.         two_week = today - timedelta(weeks=2)
  1436.         one_month = today - timedelta(days=30)  # Approxi­mate number of days in one jmonth
  1437.         two_month = today - timedelta(days=60)  # Approxi­mate number of days in two months
  1438.         three_month = today - timedelta(days=90)  # Approxi­mate number of days in three months
  1439.         six_month = today - timedelta(days=180)
  1440.  
  1441.         _logger.info("-----------------------FUNCTION AFTER GOLIVE DIPANGGIL!")
  1442.         query = """
  1443.                    select
  1444.     product_branch.product_id as product_id,
  1445.    product_branch.product_code as product_code,
  1446.    (product_branch.product_name->>$$en_US$$)::text as product_name,
  1447.    product_branch.branch_id as branch_id,
  1448.    product_branch.branch_name as branch_name,
  1449.    product_branch.branch_code as branch_code,
  1450.    product_branch.category_name as category_name,
  1451.    product_branch.sub_category_name as sub_category_name,
  1452.    coalesce(max(sales7.qty),0) as qty_sales_7,
  1453.    coalesce(max(soh7.av_days),0) av_days_7,
  1454.    coalesce(nullif(max(sales7.qty),0) / nullif(max(soh7.av_days),0),0) as svd_7,
  1455.    coalesce(max(sales14.qty),0) as qty_sales_14,
  1456.    coalesce(max(soh14.av_days),0) av_days_14,
  1457.    coalesce(nullif(max(sales14.qty),0) / nullif(max(soh14.av_days),0),0) as svd_14,
  1458.    coalesce(max(sales30.qty),0) as qty_sales_30,
  1459.    coalesce(max(soh30.av_days),0) av_days_30,
  1460.    coalesce(nullif(max(sales30.qty),0) / nullif(max(soh30.av_days),0),0) as svd_30,
  1461.    coalesce(max(sales60.qty),0) as qty_sales_60,
  1462.    coalesce(max(soh60.av_days),0) av_days_60,
  1463.    coalesce(nullif(max(sales60.qty),0) / nullif(max(soh60.av_days),0),0) as svd_60,
  1464.    coalesce(max(sales90.qty),0) as qty_sales_90,
  1465.    coalesce(max(soh90.av_days),0) av_days_90,
  1466.    coalesce(nullif(max(sales90.qty),0) / nullif(max(soh90.av_days),0),0) as svd_90,
  1467.    coalesce(max(sales180.qty),0) as qty_sales_180,
  1468.    coalesce(max(soh180.av_days),0) av_days_180,
  1469.    coalesce(nullif(max(sales180.qty),0) / nullif(max(soh180.av_days),0),0) as svd_180
  1470.    from (
  1471.         select
  1472.             pp.id as product_id,
  1473.             rb.id as branch_id,
  1474.             pp.default_code as product_code,
  1475.            pt1."name" as product_name,
  1476.             rb.code as branch_code,
  1477.            pc1."name" as category_name,
  1478.            apc1."name" as sub_category_name,
  1479.            rb."name" as branch_name
  1480.         from product_product pp
  1481.         cross join res_branch rb
  1482.        left join product_template pt1 on pp.product_tmpl_id = pt1.id
  1483.        left join product_category pc1 on pt1.categ_id = pc1.id
  1484.        left join attribute_product_category apc1 on pt1.attribute_categ_id = apc1.id  
  1485.        where
  1486.            pt.item_type = 'commercial' and
  1487.            rb.virtual_branch <> true and
  1488.            rb.code <> 'HOF'
  1489.    ) product_branch
  1490.    left join (
  1491.            select pp1.id as product_id,
  1492.            pt1."name" as product_name,
  1493.            pp1.default_code as product_code,
  1494.            pc1."name" as category_name,
  1495.            apc1."name" as sub_category_name,
  1496.            rb1.id as branch_id,
  1497.            rb1."name" as branch_name,
  1498.            rb1.code as branch_code
  1499.            from account_move_line aml1
  1500.            left join account_move am1 on am1.id = aml1.move_id
  1501.            left join account_account aa1 on aml1.account_id = aa1.id
  1502.            left join product_product pp1 on aml1.product_id = pp1.id
  1503.            left join product_template pt1 on pp1.product_tmpl_id = pt1.id
  1504.            left join product_category pc1 on pt1.categ_id = pc1.id
  1505.            left join attribute_product_category apc1 on pt1.attribute_categ_id = apc1.id  
  1506.            left join res_branch rb1 on aml1.branch_id = rb1.id
  1507.            where pt1.item_type = 'commercial'
  1508.                and am1.is_contract != true
  1509.            group by 1,2,3,4,5,6,7,8
  1510.    ) a on product_branch.product_id = a.product_id and product_branch.branch_id = a.branch_id
  1511.    left join (
  1512.            select
  1513.                aml2.product_id as product_id,
  1514.                am2.branch_id as branch_id,
  1515.                sum(aml2.quantity) as qty
  1516.                from account_move_line aml2
  1517.                left join account_move am2 on am2.id = aml2.move_id
  1518.                left join account_account aa2 on aml2.account_id = aa2.id
  1519.                left join res_branch rb2 on aml2.branch_id = rb2.id
  1520.                where am2.move_type in ('out_invoice','out_refund')
  1521.                and aa2.account_type = 'income'
  1522.                and am2.date BETWEEN current_date - interval '7 days' AND now()
  1523.                and am2.is_contract != true
  1524.                and aml2.partner_id not in (
  1525.                    select
  1526.                        partner_id
  1527.                    from mapping_virtual_customer_branch mvcb
  1528.                )
  1529.                group by 1,2
  1530.    ) sales7 on product_branch.product_id = sales7.product_id and sales7.branch_id = product_branch.branch_id
  1531.    left join (
  1532.            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1533.                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1534.                    from soh_snapshot ss
  1535.                    left join product_product pp on ss.product = pp.default_code
  1536.                    left join res_branch rb on ss.branch = rb.code
  1537.                where date between now() - interval '7 days' and now()
  1538.                group by 1,2,3,4
  1539.        ) raw_soh_7
  1540.        where raw_soh_7.ttl_qty > 20
  1541.        group by 1,2,3
  1542.    ) soh7 on product_branch.product_id = soh7.product_id and product_branch.branch_id = soh7.branch_id
  1543.    left join (
  1544.            select
  1545.                aml2.product_id as product_id,
  1546.                am2.branch_id as branch_id,
  1547.                sum(aml2.quantity) as qty
  1548.                from account_move_line aml2
  1549.                left join account_move am2 on am2.id = aml2.move_id
  1550.                left join account_account aa2 on aml2.account_id = aa2.id
  1551.                left join res_branch rb2 on aml2.branch_id = rb2.id
  1552.                where am2.move_type in ('out_invoice','out_refund')
  1553.                and aa2.account_type = 'income'
  1554.                and am2.date BETWEEN current_date - interval '14 days' AND now()
  1555.                and aml2.partner_id not in (
  1556.                    select
  1557.                        partner_id
  1558.                    from mapping_virtual_customer_branch mvcb
  1559.                )
  1560.                group by 1,2
  1561.    ) sales14 on product_branch.product_id = sales14.product_id and sales14.branch_id = product_branch.branch_id
  1562.    left join (
  1563.            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1564.                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1565.                    from soh_snapshot ss
  1566.                    left join product_product pp on ss.product = pp.default_code
  1567.                    left join res_branch rb on ss.branch = rb.code
  1568.                where date between now() - interval '14 days' and now()
  1569.                group by 1,2,3,4
  1570.        ) raw_soh_14
  1571.        where raw_soh_14.ttl_qty > 20
  1572.        group by 1,2,3
  1573.    ) soh14 on product_branch.product_id = soh14.product_id and product_branch.branch_id = soh14.branch_id
  1574.    left join (
  1575.            select
  1576.                aml2.product_id as product_id,
  1577.                am2.branch_id as branch_id,
  1578.                sum(aml2.quantity) as qty
  1579.                from account_move_line aml2
  1580.                left join account_move am2 on am2.id = aml2.move_id
  1581.                left join account_account aa2 on aml2.account_id = aa2.id
  1582.                left join res_branch rb2 on aml2.branch_id = rb2.id
  1583.                where am2.move_type in ('out_invoice','out_refund')
  1584.                and aa2.account_type = 'income'
  1585.                and am2.date BETWEEN current_date - interval '1 month' AND now()
  1586.                and aml2.partner_id not in (
  1587.                    select
  1588.                        partner_id
  1589.                    from mapping_virtual_customer_branch mvcb
  1590.                )
  1591.                group by 1,2
  1592.    ) sales30 on product_branch.product_id = sales30.product_id and sales30.branch_id = product_branch.branch_id
  1593.    left join (
  1594.            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1595.                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1596.                    from soh_snapshot ss
  1597.                    left join product_product pp on ss.product = pp.default_code
  1598.                    left join res_branch rb on ss.branch = rb.code
  1599.                where date between now() - interval '1 month' and now()
  1600.                group by 1,2,3,4
  1601.        ) raw_soh_7
  1602.        where raw_soh_7.ttl_qty > 20
  1603.        group by 1,2,3
  1604.    ) soh30 on product_branch.product_id = soh30.product_id and product_branch.branch_id = soh30.branch_id
  1605.    left join (
  1606.            select
  1607.                aml2.product_id as product_id,
  1608.                am2.branch_id as branch_id,
  1609.                sum(aml2.quantity) as qty
  1610.                from account_move_line aml2
  1611.                left join account_move am2 on am2.id = aml2.move_id
  1612.                left join account_account aa2 on aml2.account_id = aa2.id
  1613.                left join res_branch rb2 on aml2.branch_id = rb2.id
  1614.                where am2.move_type in ('out_invoice','out_refund')
  1615.                and aa2.account_type = 'income'
  1616.                and am2.date BETWEEN current_date - interval '2 month' AND now()
  1617.                and aml2.partner_id not in (
  1618.                    select
  1619.                        partner_id
  1620.                    from mapping_virtual_customer_branch mvcb
  1621.                )
  1622.                group by 1,2
  1623.    ) sales60 on product_branch.product_id = sales60.product_id and sales60.branch_id = product_branch.branch_id
  1624.    left join (
  1625.            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1626.                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1627.                    from soh_snapshot ss
  1628.                    left join product_product pp on ss.product = pp.default_code
  1629.                    left join res_branch rb on ss.branch = rb.code
  1630.                where date between now() - interval '2 month' and now()
  1631.                group by 1,2,3,4
  1632.        ) raw_soh_7
  1633.        where raw_soh_7.ttl_qty > 20
  1634.        group by 1,2,3
  1635.    ) soh60 on product_branch.product_id = soh60.product_id and product_branch.branch_id = soh60.branch_id
  1636.    left join (
  1637.            select
  1638.                aml2.product_id as product_id,
  1639.                am2.branch_id as branch_id,
  1640.                sum(aml2.quantity) as qty
  1641.                from account_move_line aml2
  1642.                left join account_move am2 on am2.id = aml2.move_id
  1643.                left join account_account aa2 on aml2.account_id = aa2.id
  1644.                left join res_branch rb2 on aml2.branch_id = rb2.id
  1645.                where am2.move_type in ('out_invoice','out_refund')
  1646.                and aa2.account_type = 'income'
  1647.                and am2.date BETWEEN current_date - interval '3 month' AND now()
  1648.                and aml2.partner_id not in (
  1649.                    select
  1650.                        partner_id
  1651.                    from mapping_virtual_customer_branch mvcb
  1652.                )
  1653.                group by 1,2
  1654.    ) sales90 on product_branch.product_id = sales90.product_id and sales90.branch_id = product_branch.branch_id
  1655.    left join (
  1656.            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1657.                select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1658.                    from soh_snapshot ss
  1659.                    left join product_product pp on ss.product = pp.default_code
  1660.                    left join res_branch rb on ss.branch = rb.code
  1661.                    where date between now() - interval '3 month' and now()
  1662.                    group by 1,2,3,4
  1663.        ) raw_soh_7
  1664.        where raw_soh_7.ttl_qty > 20
  1665.        group by 1,2,3
  1666.    ) soh90 on product_branch.product_id = soh90.product_id and product_branch.branch_id = soh90.branch_id
  1667.    left join (
  1668.            select
  1669.                aml2.product_id as product_id,
  1670.                am2.branch_id as branch_id,
  1671.                sum(aml2.quantity) as qty
  1672.                from account_move_line aml2
  1673.                left join account_move am2 on am2.id = aml2.move_id
  1674.                left join account_account aa2 on aml2.account_id = aa2.id
  1675.                left join res_branch rb2 on aml2.branch_id = rb2.id
  1676.                where am2.move_type in ('out_invoice','out_refund')
  1677.                and aa2.account_type = 'income'
  1678.                and am2.date BETWEEN current_date - interval '6 month' AND now()
  1679.                and aml2.partner_id not in (
  1680.                    select
  1681.                        partner_id
  1682.                    from mapping_virtual_customer_branch mvcb
  1683.                )
  1684.                group by 1,2
  1685.    ) sales180 on product_branch.product_id = sales180.product_id and sales180.branch_id = product_branch.branch_id
  1686.    left join (
  1687.            select product_id,product_code, branch_id, count(product_id) as av_days from (
  1688.                    select pp.id as product_id , product as product_code, rb.id as branch_id , "date" as date_soh,sum(ss.available_spot) as ttl_qty
  1689.                    from soh_snapshot ss
  1690.                    left join product_product pp on ss.product = pp.default_code
  1691.                    left join res_branch rb on ss.branch = rb.code
  1692.                    where date between now() - interval '6 month' and now()
  1693.                    group by 1,2,3,4
  1694.        ) raw_soh_7
  1695.        where raw_soh_7.ttl_qty > 20
  1696.        group by 1,2,3
  1697.    ) soh180 on product_branch.product_id = soh180.product_id and product_branch.branch_id = soh180.branch_id
  1698.    group by 1,2,3,4,5,6,7,8
  1699.                """
  1700.         self.env.cr.execute(query)
  1701.         query_svd_mart_abu_2024 = self.env.cr.dictfetchall()
  1702.         if query_svd_mart_abu_2024:
  1703.             for data_query_abu in query_svd_mart_abu_2024:
  1704.                 _logger.info("%s-------------------- MART ABU 2024" % data_query_abu)
  1705.                 product_id = data_query_abu[0]
  1706.                 product_code = data_query_abu[1]
  1707.                 product_name = data_query_abu[2]
  1708.                 branch_id = data_query_abu[3]
  1709.                 branch_name = data_query_abu[4]
  1710.                 branch_code = data_query_abu[5]
  1711.                 category_name = data_query_abu[6]
  1712.                 sub_category_name = data_query_abu[7]
  1713.                 qty_sales_7 = data_query_abu[8]
  1714.                 av_days_7 = [9]
  1715.                 svd_7 = [10]
  1716.                 qty_sales_14 = data_query_abu[11]
  1717.                 av_days_14 = [12]
  1718.                 svd_14 = [13]
  1719.                 qty_sales_30 = data_query_abu[14]
  1720.                 av_days_30 = [15]
  1721.                 svd_30 = [16]
  1722.                 qty_sales_60 = data_query_abu[17]
  1723.                 av_days_60 = [18]
  1724.                 svd_60 = [19]
  1725.                 qty_sales_90 = data_query_abu[20]
  1726.                 av_days_90 = [21]
  1727.                 svd_90 = [22]
  1728.                 qty_sales_180 = data_query_abu[23]
  1729.                 av_days_180 = [24]
  1730.                 svd_180 = [25]
  1731.                 query_create_svd_from_abu = """
  1732.                                                    INSERT INTO sbb_snapshot
  1733.                                                    ("product_code","product_name","branch_code","avg_7","avg_14","avg_30","avg_60","avg_90","avg_180","total_sales_qty_7","total_sales_qty_14","total_sales_qty_30","total_sales_qty_60","total_sales_qty_90","total_sales_qty_180","days_stock_available_7","days_stock_available_14","days_stock_available_30","days_stock_available_60","days_stock_available_90","days_stock_available_180","date","type_data_svd")
  1734.                                                    VALUES (%(product_code)s,%(product_name)s,%(branch_code)s,%(avg_7)s,%(avg_14)s,%(avg_30)s,%(avg_60)s,%(avg_90)s,%(avg_180)s,%(total_sales_qty_7)s,%(total_sales_qty_14)s,%(total_sales_qty_30)s,%(total_sales_qty_60)s,%(total_sales_qty_90)s,%(total_sales_qty_180)s,%(days_stock_available_7)s,%(days_stock_available_14)s,%(days_stock_available_30)s,%(days_stock_available_60)s,%(days_stock_available_90)s,%(days_stock_available_180)s,current_date,'abu')
  1735.                                                """
  1736.                 execute_create_svd_snapshot = self.env.cr.execute(query_create_svd_from_abu, {
  1737.                     "product_code": product_code,
  1738.                     "product_name": product_name,
  1739.                     "branch_code": branch_code,
  1740.                     "avg_7": svd_7,
  1741.                     "avg_14": svd_14,
  1742.                     "avg_30": svd_30,
  1743.                     "avg_60": svd_60,
  1744.                     "avg_90": svd_90,
  1745.                     "avg_180": svd_180,
  1746.                     "total_sales_qty_7": qty_sales_7,
  1747.                     "total_sales_qty_14": qty_sales_14,
  1748.                     "total_sales_qty_30": qty_sales_30,
  1749.                     "total_sales_qty_60": qty_sales_60,
  1750.                     "total_sales_qty_90": qty_sales_90,
  1751.                     "total_sales_qty_180": qty_sales_180,
  1752.                     "days_stock_available_7": av_days_7,
  1753.                     "days_stock_available_14": av_days_14,
  1754.                     "days_stock_available_30": av_days_30,
  1755.                     "days_stock_available_60": av_days_60,
  1756.                     "days_stock_available_90": av_days_90,
  1757.                     "days_stock_available_180": av_days_180,
  1758.                 })
  1759.                 _logger.info("----------- DATA SNAPSHOT YANG DI CREATE %s" % execute_create_svd_snapshot)
  1760.  
  1761.     def cron_sbb_snapshot(self):
  1762.         today = date.today()
  1763.         self.env.cr.execute("Truncate sbb_snapshot")
  1764.         minimum_soh = self.env['ir.config_parameter'].sudo().get_param('minimum_soh')
  1765.         minimum_soh = float(minimum_soh)
  1766.         tanggal_golive_abu = []
  1767.         today = date.today()
  1768.         tanggal_golive = self.env['ir.config_parameter'].sudo().get_param('golive_abu_date')
  1769.         # self.query_pnj()
  1770.         if tanggal_golive:
  1771.             tanggal_golive_abu = datetime.strptime(tanggal_golive, '%Y-%m-%d').date()
  1772.  
  1773.         if not tanggal_golive_abu or (today - timedelta(days=180) < tanggal_golive_abu and today < tanggal_golive_abu):
  1774.             _logger.info("---------------- MASUK KONDISI PERTAMA")
  1775.             self.query_before_golive_abu()
  1776.         elif today - timedelta(days=180) > tanggal_golive_abu:
  1777.             _logger.info("---------------- MASUK KONDISI KEDUA")
  1778.             self.query_after_golive_abu()
  1779.         else:
  1780.             _logger.info("---------------- MASUK KONDISI KETIGA")
  1781.             self.query_split_dwh_abu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement