From fe419d96b01c2528e1ffa20d75c41c17a1baec15 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 27 Aug 2022 14:39:06 +0530 Subject: [PATCH] Revenue report fixed type updated --- .../report/project_revenue_custom_report3.py | 152 ++++++++++-------- .../project_revenue_custom_report3_views.xml | 25 ++- 2 files changed, 105 insertions(+), 72 deletions(-) diff --git a/project_report/report/project_revenue_custom_report3.py b/project_report/report/project_revenue_custom_report3.py index 0d964ff..769d813 100644 --- a/project_report/report/project_revenue_custom_report3.py +++ b/project_report/report/project_revenue_custom_report3.py @@ -10,12 +10,12 @@ class ProjectRevenueCustomReport3(models.Model): #_order = 'project_id' _auto = False - start_date = fields.Datetime(string='Start Date', readonly=True) - end_date = fields.Datetime(string='End Date', readonly=True) + start_date = fields.Date(string='Start Date', readonly=True) + end_date = fields.Date(string='End Date', readonly=True) project_id = fields.Many2one('project.project', string='Project', readonly=True) parent_project = fields.Many2one('project.project', string='Parent Project', readonly=True) partner_id = fields.Many2one('res.partner', string='Client', readonly=True) - #timesheet_id = fields.Integer(string='Timesheet ID', readonly=True) + timesheet_id = fields.Integer(string='Timesheet ID', readonly=True) pricing_type = fields.Selection([ ('fixed_rate', 'Fixed rate'), ('employee_rate', 'Consultant rate') @@ -25,7 +25,7 @@ class ProjectRevenueCustomReport3(models.Model): ('hours_no_limit', 'Total hours are budgeted without division to consultant'), ], string="Project Type", readonly=True) employee_id = fields.Many2one('hr.employee', string='Consultant', readonly=True) - timesheet_sdatetime = fields.Datetime(string='Timesheet Start Time', readonly=True) + #timesheet_sdatetime = fields.Datetime(string='Timesheet Start Time', readonly=True) unit_amount = fields.Float('Timesheet Hours', digits=(16, 2)) timesheet_cost = fields.Float('Hourly Cost', digits=(16, 2)) profit_per = fields.Float(string='Profit (%)', digits=(16, 2)) @@ -39,6 +39,10 @@ class ProjectRevenueCustomReport3(models.Model): overall_budgeted_revenue = fields.Float("Overall Budgeted Rev.", digits=(16, 2), readonly=True, group_operator="sum") overall_hourly_rate = fields.Float("Overall Hourly Rate", digits=(16, 2), group_operator="sum") project_active = fields.Boolean('Active') + tag_name = fields.Char("Tag Name") + role = fields.Selection([('Manager', 'Manager'), ('Employee', 'Employee')], string="Role") + is_sub_project = fields.Boolean("Is Sub Project") + sub_project = fields.Many2one('project.project', string='Sub Project') def init(self): @@ -46,80 +50,98 @@ class ProjectRevenueCustomReport3(models.Model): tools.drop_view_if_exists(self._cr, self._table) self._cr.execute(""" CREATE OR REPLACE VIEW %s AS ( -with data1 as (SELECT +with pro_tsheet as (SELECT date_trunc('month', min(AAL.start_datetime::date)) AS min, date_trunc('month', max(AAL.end_datetime::date)) AS max, AAL.start_datetime::date as t_startdate, AAL.end_datetime::date as t_enddate, AAL.project_id as project_id, PRO.active as project_active, - (select project_id from project_subproject_rel as PAR where PRO.id=PAR.id limit 1) as parent_project, + PRO.project_type, + PRO.pricing_type, + (select PRO.id from project_subproject_rel as PAR where PRO.id=PAR.project_id limit 1) as parent_project, + PRO.is_sub_project, + AAL.sub_project as sub_project, + STRING_AGG(distinct tag_master.name, ', ') as tag_name, PRO.partner_id AS partner_id, - PRO.pricing_type, PRO.project_type, AAL.employee_id, + AAL.id as timesheet_id, + PRO.expenses_amt, + AAL.employee_id, AAL.unit_amount as unit_amount, - AAL.amount * -1 as actual_cost, - (AAL.unit_amount * PRO.hourly_rate) as actual_revenue, - PRO.expenses_amt + AAL.amount * -1 as actual_cost FROM project_project PRO LEFT JOIN account_analytic_account AA ON PRO.analytic_account_id = AA.id LEFT JOIN account_analytic_line AAL ON AAL.account_id = AA.id AND AAL.project_id = PRO.id - GROUP BY PRO.id, PRO.pricing_type, PRO.project_type, PRO.hourly_rate, PRO.expenses_amt, AAL.project_id, PRO.pricing_type, PRO.project_type, - AAL.employee_id, AAL.start_datetime::date, AAL.end_datetime::date, AAL.unit_amount, AAL.amount + LEFT JOIN custom_project_tags_project_project_rel AS cus_pro_tag ON pro.id = cus_pro_tag.project_project_id + LEFT JOIN custom_project_tags as tag_master ON tag_master.id = cus_pro_tag.custom_project_tags_id + LEFT JOIN project_subproject_rel as sub_pro ON sub_pro.project_id = PRO.id + GROUP BY PRO.id, PRO.pricing_type, PRO.project_type, AAL.project_id, PRO.pricing_type, PRO.project_type, + PRO.is_sub_project, sub_pro.id, + AAL.id, AAL.employee_id, AAL.start_datetime::date, AAL.end_datetime::date, AAL.unit_amount, AAL.amount ), input_data as (SELECT generate_series(min, max,'1 month'):: date AS start_date, - (generate_series(min, max, '1 month'):: date + '1 month' :: interval - '1 day' :: interval):: date AS end_date, - project_id, employee_id - from data1), -data3 as ( -select PRO_EMP.start_date as cons_start_date, PRO_EMP.end_date as cons_end_date, - sum(PRO_EMP.price_unit) as price_unit, PRO_EMP.project_id, PRO_EMP.employee_id, - ((AAL.amount * -1)/NULLIF(AAL.unit_amount, 0)) as timesheet_cost, - CASE WHEN PRO_EMP.employee_price is null then ((AAL.amount * -1)/NULLIF(AAL.unit_amount, 0)) else (AAL.unit_amount * PRO_EMP.employee_price) end as cons_timesheet_cost, - CASE WHEN PRO_EMP.employee_price is null then (AAL.amount * -1) else (AAL.unit_amount * PRO_EMP.employee_price) end as cons_actual_cost, - (AAL.unit_amount * COALESCE(PRO_EMP.price_unit, 0)) AS cons_actual_revenue - from project_sale_line_employee_map PRO_EMP - LEFT JOIN account_analytic_line AAL ON AAL.project_id = PRO_EMP.project_id AND AAL.employee_id = PRO_EMP.employee_id - GROUP BY PRO_EMP.start_date, PRO_EMP.end_date, PRO_EMP.project_id, PRO_EMP.employee_id, PRO_EMP.employee_price, PRO_EMP.price_unit, - AAL.amount, AAL.unit_amount -) -SELECT ROW_NUMBER() OVER() as id, start_date, end_date, -input_data.project_id, data1.project_active, data1.parent_project, data1.partner_id, input_data.employee_id, -data1.pricing_type, data1.project_type, 0 as overall_budgeted_revenue, -0 as budgeted_revenue, 0 as budgeted_hours, 0 as pro_hourly_rate, 0 as overall_hourly_rate, -data1.unit_amount, -CASE WHEN data1.pricing_type = 'fixed_rate' THEN data3.timesheet_cost - WHEN data1.pricing_type = 'employee_rate' and data1.project_type='hours_no_limit' THEN data3.timesheet_cost - WHEN data1.pricing_type = 'employee_rate' and data1.project_type='hours_in_consultant' THEN data3.cons_timesheet_cost -END as timesheet_cost, -CASE WHEN data1.pricing_type = 'fixed_rate' THEN data1.actual_cost - WHEN data1.pricing_type = 'employee_rate' and data1.project_type='hours_no_limit' THEN data1.actual_cost - WHEN data1.pricing_type = 'employee_rate' and data1.project_type='hours_in_consultant' THEN data3.cons_actual_cost -END as actual_cost, -CASE WHEN data1.pricing_type = 'fixed_rate' THEN 0 - WHEN data1.pricing_type = 'employee_rate' and data1.project_type='hours_no_limit' THEN data1.actual_revenue - WHEN data1.pricing_type = 'employee_rate' and data1.project_type='hours_in_consultant' THEN data3.cons_actual_revenue -END as actual_revenue, -0 as expenses_amt, -CASE WHEN pricing_type = 'fixed_rate' THEN 0.0 - data1.actual_cost - WHEN pricing_type = 'employee_rate' and project_type='hours_no_limit' THEN data1.actual_revenue - data1.actual_cost - WHEN pricing_type = 'employee_rate' and project_type='hours_in_consultant' THEN data3.cons_actual_revenue - data3.cons_actual_cost -END as profit_amt, -CASE WHEN pricing_type = 'fixed_rate' THEN 0.0 - WHEN pricing_type = 'employee_rate' and project_type='hours_no_limit' THEN (data1.actual_revenue - data1.actual_cost)/NULLIF(data1.actual_revenue,0) - WHEN pricing_type = 'employee_rate' and project_type='hours_in_consultant' THEN (data3.cons_actual_revenue - data3.cons_actual_cost)/NULLIF(data3.cons_actual_revenue,0) * 100 -END as profit_per -from input_data -left join data1 on data1.project_id = input_data.project_id and data1.employee_id = input_data.employee_id -and data1.t_startdate >= input_data.start_date and data1.t_enddate <= input_data.end_date -left join data3 on data3.project_id = input_data.project_id and data3.employee_id = input_data.employee_id -and data3.cons_start_date >= input_data.start_date and data3.cons_end_date <= input_data.end_date -where data1.project_id=45 -group by start_date, end_date, input_data.project_id, data1.project_active, data1.parent_project, data1.partner_id, -data1.pricing_type, data1.project_type, -t_startdate, t_enddate, input_data.employee_id, data1.unit_amount, -data3.price_unit, actual_cost, data3.cons_actual_cost, actual_revenue, cons_actual_revenue, data3.timesheet_cost, data3.cons_timesheet_cost -order by employee_id, start_date - )""" % (self._table,)) + (generate_series(min, max, '1 month'):: date + '1 month' :: interval - '1 day' :: interval):: date AS end_date, + project_id, employee_id + from pro_tsheet), +cons_data as ( +select PRO_EMP.start_date as cons_start_date, PRO_EMP.end_date as cons_end_date, + sum(PRO_EMP.price_unit) as price_unit, PRO_EMP.project_id, PRO_EMP.employee_id, + sum(PRO_EMP.employee_price) as cons_price + from project_sale_line_employee_map PRO_EMP + GROUP BY PRO_EMP.start_date, PRO_EMP.end_date, PRO_EMP.project_id, PRO_EMP.employee_id +), +fixed_rate as ( + select + ROW_NUMBER() OVER() as id, + input_data.start_date, + input_data.end_date, + input_data.project_id, + pro_tsheet.project_active, + pro_tsheet.project_type, + pro_tsheet.pricing_type, + pro_tsheet.parent_project, + pro_tsheet.is_sub_project, + pro_tsheet.sub_project, + null::char as role, + pro_tsheet.tag_name, + pro_tsheet.partner_id, + pro_tsheet.timesheet_id, + input_data.employee_id, + 0 as overall_budgeted_revenue, + 0 as budgeted_revenue, + 0 as budgeted_hours, + 0 as overall_hourly_rate, + 0 AS pro_hourly_rate, + pro_tsheet.unit_amount, + ((pro_tsheet.actual_cost)/NULLIF(pro_tsheet.unit_amount, 0)) as timesheet_cost, + 0.0 AS actual_revenue, + pro_tsheet.actual_cost, + 0.0 AS expenses_amt, + 0.0 - pro_tsheet.actual_cost AS profit_amt, + 0.0 AS profit_per + from input_data + left join pro_tsheet on pro_tsheet.project_id = input_data.project_id and pro_tsheet.employee_id = input_data.employee_id + and pro_tsheet.t_startdate >= input_data.start_date and pro_tsheet.t_enddate <= input_data.end_date + where pro_tsheet.pricing_type = 'fixed_rate' + group by + input_data.start_date, + input_data.end_date, + input_data.project_id, + pro_tsheet.project_active, + pro_tsheet.project_type, + pro_tsheet.pricing_type, + pro_tsheet.parent_project, + pro_tsheet.is_sub_project, + pro_tsheet.sub_project, + pro_tsheet.tag_name, + pro_tsheet.partner_id, + pro_tsheet.timesheet_id, + input_data.employee_id, + pro_tsheet.unit_amount, + pro_tsheet.actual_cost + ) + select * from fixed_rate +)""" % (self._table,)) @api.model def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True): diff --git a/project_report/report/project_revenue_custom_report3_views.xml b/project_report/report/project_revenue_custom_report3_views.xml index 7a36d01..f5d7fd1 100644 --- a/project_report/report/project_revenue_custom_report3_views.xml +++ b/project_report/report/project_revenue_custom_report3_views.xml @@ -8,17 +8,17 @@ + + + + + - - - - - @@ -42,6 +42,8 @@ + + @@ -66,6 +68,7 @@ + @@ -73,10 +76,18 @@ + + + + + + + +