diff --git a/project_report/report/project_revenue_custom_report3.py b/project_report/report/project_revenue_custom_report3.py index 128241c..536be08 100644 --- a/project_report/report/project_revenue_custom_report3.py +++ b/project_report/report/project_revenue_custom_report3.py @@ -73,12 +73,37 @@ with pro_data as ( LEFT JOIN custom_project_tags as tag_master ON tag_master.id = cus_pro_tag.custom_project_tags_id group by pro.id ), +sub_pro_data as ( + SELECT + PRO.parent_project as pproject_id, + PRO.date_start as pro_sdate, + PRO.date as pro_edate, + PRO.active as project_active, + PRO.project_type, + PRO.pricing_type, + PRO.is_sub_project, + PRO.id as sub_project, + (select project_id from project_subproject_rel as par where pro.id=par.id limit 1) as parent_project, + --(select PRO.id from project_subproject_rel as PAR where PRO.id=PAR.project_id limit 1) as parent_project, + STRING_AGG(distinct tag_master.name, ', ') as tag_name, + PRO.partner_id AS partner_id, + PRO.budgeted_revenue as budgeted_revenue, + PRO.budgeted_hours2 as budgeted_hours, + PRO.expenses_amt as expenses_amt, + PRO.hourly_rate AS pro_hourly_rate + FROM project_project PRO + 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 + where PRO.is_sub_project=True + group by pro.id +), cons_data1 as ( SELECT date_trunc('month', min(PRO_EMP.start_date)) AS min, date_trunc('month', coalesce(max(PRO_EMP.end_date), current_date AT TIME ZONE 'UTC')) AS max, PRO_EMP.start_date as start_date, - coalesce(PRO_EMP.end_date, current_date AT TIME ZONE 'UTC') as end_date, + PRO_EMP.end_date as end_date, + --coalesce(PRO_EMP.end_date, current_date AT TIME ZONE 'UTC') as end_date, PRO_EMP.project_id, PRO_EMP.employee_id, PRO_EMP.role, @@ -98,14 +123,6 @@ tsheet_data1 as ( FROM account_analytic_line AAL group by AAL.project_id, AAL.employee_id ), -drange_data1 as ( - SELECT - tsheet_data1.project_id, - tsheet_data1.employee_id, - 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 - FROM tsheet_data1 -), drange_data2 as ( SELECT cons_data1.project_id, @@ -259,6 +276,50 @@ SELECT pro_data.budgeted_hours, pro_data.expenses_amt ), +data2_sub_project as ( + SELECT + min(sub_pro_data.pro_sdate) as start_date, + max(sub_pro_data.pro_edate)as end_date, + sub_pro_data.parent_project as project_id, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.pricing_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + null::char as role, + sub_pro_data.tag_name, + sub_pro_data.partner_id, + null::int AS employee_id, + null::int As timesheet_id, + null::timestamp as timesheet_sdatetime, + 0 as overall_budgeted_revenue, + sub_pro_data.budgeted_revenue, + sub_pro_data.budgeted_hours, + 0 as overall_hourly_rate, + 0 as unit_amount, + 0.0 AS pro_hourly_rate, + 0 as timesheet_cost, + 0.0 AS actual_revenue, + 0 as actual_cost, + sub_pro_data.expenses_amt, + 0.0 AS profit_amt, + 0.0 AS profit_per + FROM sub_pro_data + WHERE sub_pro_data.pricing_type='employee_rate' and sub_pro_data.project_type='hours_no_limit' + and sub_pro_data.is_sub_project = TRUE + group by + sub_pro_data.parent_project, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.pricing_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + sub_pro_data.tag_name, + sub_pro_data.partner_id, + sub_pro_data.budgeted_revenue, + sub_pro_data.budgeted_hours, + sub_pro_data.expenses_amt +), data3 as ( SELECT coalesce(min(pro_data.pro_sdate), min(AAL.start_datetime::date)) as start_date, @@ -310,7 +371,6 @@ SELECT pro_data.project_type, pro_data.pricing_type, pro_data.parent_project, - --AAL.sub_project, null::int as sub_project, cons_data1.role, pro_data.tag_name, @@ -383,10 +443,11 @@ SELECT FROM pro_data left join account_analytic_line AAL on AAL.project_id=pro_data.pproject_id left join cons_data1 on cons_data1.project_id=AAL.project_id and cons_data1.employee_id=AAL.employee_id - and AAL.start_datetime --at time zone 'utc' at time zone (select tz from res_partner where id=3) - >= cons_data1.start_date::date + '00:00:00'::time AT TIME ZONE 'UTC' - and AAL.end_datetime --at time zone 'utc' at time zone (select tz from res_partner where id=3) - <= cons_data1.end_date::date + '23:59:59'::time AT TIME ZONE 'UTC' + and (AAL.start_datetime, AAL.end_datetime) OVERLAPS (cons_data1.start_date::date, cons_data1.end_date::date) + --and AAL.start_datetime --at time zone 'utc' at time zone (select tz from res_partner where id=3) + -- >= cons_data1.start_date::date + '00:00:00'::time AT TIME ZONE 'UTC' + --and AAL.end_datetime --at time zone 'utc' at time zone (select tz from res_partner where id=3) + -- <= cons_data1.end_date::date + '23:59:59'::time AT TIME ZONE 'UTC' where pro_data.pricing_type='employee_rate' and pro_data.project_type='hours_in_consultant' and COALESCE(pro_data.is_sub_project, FALSE) = False group by @@ -407,6 +468,103 @@ SELECT cons_data1.cons_timesheet_cost, cons_data1.cons_hourly_cost ), +data3_sub_project as ( +SELECT + min(sub_pro_data.pro_sdate) as start_date, + max(sub_pro_data.pro_edate) as end_date, + sub_pro_data.parent_project as project_id, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.pricing_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + null::char as role, + sub_pro_data.tag_name, + sub_pro_data.partner_id, + null::int as employee_id, + null::int As timesheet_id, + null::timestamp as timesheet_sdatetime, + sub_pro_data.budgeted_revenue AS overall_budgeted_revenue, + 0 as budgeted_revenue, + 0 as budgeted_hours, + sub_pro_data.pro_hourly_rate AS overall_hourly_rate, + 0.0 as unit_amount, + 0.0 as pro_hourly_rate, + 0.0 as timesheet_cost, + 0.0 as actual_revenue, + 0.0 as actual_cost, + sub_pro_data.expenses_amt, + 0.0 as profit_amt, + 0.0 as profit_per + FROM sub_pro_data + where sub_pro_data.pricing_type='employee_rate' and sub_pro_data.project_type='hours_in_consultant' + and sub_pro_data.is_sub_project = True + group by + sub_pro_data.parent_project, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + sub_pro_data.budgeted_revenue, + sub_pro_data.tag_name, + sub_pro_data.pricing_type, + sub_pro_data.partner_id, + sub_pro_data.pro_hourly_rate, + sub_pro_data.expenses_amt +UNION +SELECT + drange_data2.start_date, + drange_data2.end_date, + sub_pro_data.parent_project as project_id, + --drange_data2.project_id, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.pricing_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + cons_data1.role, + sub_pro_data.tag_name, + sub_pro_data.partner_id, + drange_data2.employee_id, + null::int As timesheet_id, + null::timestamp as timesheet_sdatetime, + 0 as overall_budgeted_revenue, + cons_data1.budgeted_revenue, + cons_data1.budgeted_hours, + 0 as overall_hourly_rate, + 0 as unit_amount, + 0 as pro_hourly_rate, + 0 as timesheet_cost, + 0 as actual_revenue, + 0 as actual_cost, + 0 as expenses_amt, + 0 as profit_amt, + 0 as profit_per + FROM drange_data2 + left join sub_pro_data on sub_pro_data.sub_project = drange_data2.project_id + left join cons_data1 on cons_data1.project_id = drange_data2.project_id + and cons_data1.employee_id=drange_data2.employee_id + and (drange_data2.start_date, drange_data2.end_date) OVERLAPS (cons_data1.start_date, cons_data1.end_date) + --and drange_data2.start_date >= cons_data1.start_date + --and drange_data2.end_date <= cons_data1.end_date + where sub_pro_data.pricing_type='employee_rate' and sub_pro_data.project_type='hours_in_consultant' + and sub_pro_data.is_sub_project = True + group by + drange_data2.start_date, + drange_data2.end_date, + sub_pro_data.parent_project, + drange_data2.employee_id, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + cons_data1.role, + cons_data1.budgeted_revenue, + cons_data1.budgeted_hours, + sub_pro_data.tag_name, + sub_pro_data.pricing_type, + sub_pro_data.partner_id +), invoice_data as (SELECT invoice_date.date as start_date, invoice_date.date as end_date, @@ -436,17 +594,54 @@ invoice_data as (SELECT 0.0 AS profit_per from invoice_date left join pro_data on pro_data.pproject_id = invoice_date.project_id --and pro_data.employee_id = invoice_date.employee_id - --left join drange_data1 on invoice_date.project_id = drange_data1.project_id and invoice_date.employee_id = drange_data1.employee_id - --and invoice_date.date >= drange_data1.start_date and invoice_date.date <= drange_data1.end_date + where COALESCE(pro_data.is_sub_project, FALSE) = False + ), +invoice_data_sub as (SELECT + invoice_date.date as start_date, + invoice_date.date as end_date, + --invoice_date.project_id, + sub_pro_data.parent_project as project_id, + sub_pro_data.project_active, + sub_pro_data.project_type, + sub_pro_data.pricing_type, + sub_pro_data.parent_project, + sub_pro_data.sub_project, + null::char as role, + sub_pro_data.tag_name, + sub_pro_data.partner_id, + invoice_date.employee_id, + null::int As timesheet_id, + null::timestamp as timesheet_sdatetime, + 0 as overall_budgeted_revenue, + 0 as budgeted_revenue, + 0 as budgeted_hours, + 0 as overall_hourly_rate, + 0 as unit_amount, + 0 as pro_hourly_rate, + 0 as timesheet_cost, + invoice_date.fixed_amount as actual_revenue, + 0 as actual_cost, + 0.0 AS expenses_amt, + 0.0 AS profit_amt, + 0.0 AS profit_per + from invoice_date + left join sub_pro_data on sub_pro_data.sub_project = invoice_date.project_id + where sub_pro_data.is_sub_project = True ), res as ( select * from data1 UNION select * from data2 UNION +select * from data2_sub_project +UNION select * from data3 UNION +select * from data3_sub_project +UNION select * from invoice_data +UNION +select * from invoice_data_sub ) select ROW_NUMBER() OVER() as id, diff --git a/project_report/report/project_revenue_custom_report3_views.xml b/project_report/report/project_revenue_custom_report3_views.xml index 6532631..b5660d8 100644 --- a/project_report/report/project_revenue_custom_report3_views.xml +++ b/project_report/report/project_revenue_custom_report3_views.xml @@ -69,6 +69,7 @@ +