Revenue report fixed type updated

This commit is contained in:
root 2022-08-27 14:39:06 +05:30
parent 79a3e34f90
commit fe419d96b0
2 changed files with 105 additions and 72 deletions

View File

@ -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):

View File

@ -8,17 +8,17 @@
<pivot string="Revenue Analysis" disable_linking="True" sample="1">
<field name="project_id" type="row"/>
<field name="overall_budgeted_revenue" type="measure"/>
<field name="budgeted_hours" type="measure"/>
<field name="budgeted_revenue" type="measure"/>
<field name="overall_hourly_rate" type="measure"/>
<field name="timesheet_cost" type="measure"/>
<field name="pro_hourly_rate" type="measure"/>
<field name="unit_amount" widget="float_time" type="measure"/>
<field name="actual_revenue" type="measure"/>
<field name="actual_cost" type="measure"/>
<field name="budgeted_hours" type="measure"/>
<field name="overall_hourly_rate" type="measure"/>
<field name="pro_hourly_rate" type="measure"/>
<field name="expenses_amt" type="measure"/>
<field name="profit_amt" type="measure"/>
<field name="profit_per" type="measure"/>
<field name="unit_amount" widget="float_time" type="measure"/>
<field name="timesheet_cost" type="measure"/>
</pivot>
</field>
</record>
@ -42,6 +42,8 @@
<field name="arch" type="xml">
<tree string="Revenue Analysis" create="false" edit="false" delete="false">
<field name="project_id"/>
<field name="parent_project"/>
<field name="sub_project"/>
<field name="employee_id"/>
<field name="budgeted_revenue"/>
<field name="actual_revenue"/>
@ -66,6 +68,7 @@
<search string="Revenue Analysis">
<field name="project_id"/>
<field name="employee_id"/>
<field name="tag_name"/>
<filter string="Fixed rate" name="fixed" domain="[('pricing_type','=','fixed_rate')]"/>
<filter string="Hours are budgeted according to a consultant" name="cons" domain="[('pricing_type','=','employee_rate'),('project_type','=','hours_in_consultant')]"/>
<filter string="Total hours are budgeted without division to consultant" name="limit" domain="[('pricing_type','=','employee_rate'),('project_type','=','hours_no_limit')]"/>
@ -73,10 +76,18 @@
<filter string="End Date" name="filter_end_date" date="end_date"/>
<filter string="Active" name="active_project" domain="[('project_active','=',True)]"/>
<filter string="Archived" name="active_project" domain="[('project_active','=',False)]"/>
<filter string="Is Sub Project" name="subproject" domain="[('is_sub_project','=',True)]"/>
<filter string="Is Parent Project" name="subproject" domain="[('sub_project','=',False)]"/>
<filter string="Is Sub Project" name="issubproject" domain="[('sub_project','!=',False)]"/>
<filter string="Manager Role" name="manager_role" domain="[('role','=','Manager')]"/>
<filter string="Employee Role" name="employee_role" domain="[('role','=','Employee')]"/>
<group expand="1" string="Group By">
<filter string="Project" name="group_project" context="{'group_by':'project_id'}"/>
<filter string="Parent Project" name="group_parent_project" context="{'group_by':'parent_project'}"/>
<filter string="Sub Project" name="group_sub_project" context="{'group_by':'sub_project'}"/>
<filter string="Consultant" name="group_employee" context="{'group_by':'employee_id'}"/>
<filter string="End Date" name="group_enddate" domain="[]" context="{'group_by':'end_date:month'}"/>
<filter string="Role" name="group_role" domain="[]" context="{'group_by':'role'}"/>
<!--<filter string="Budgeted Hours" name="budgethrs"
domain="[('timesheet_sdatetime','=',False)]"/>
<filter string="Current Year" name="currentyear"
@ -88,7 +99,7 @@
</record>
<record id="project_revenue_custom_report3_action" model="ir.actions.act_window">
<field name="name">Projects Revenue (updated)</field>
<field name="name">Projects Revenue</field>
<field name="res_model">project.revenue.custom.report3</field>
<field name="view_mode">pivot,tree,graph</field>
<field name="search_view_id" ref="project_revenue_custom_report3_view_search"/>
@ -99,7 +110,7 @@
<menuitem id="menu_project_revenue_custom_report3"
parent="project.menu_project_report"
action="project_revenue_custom_report3_action"
name="Projects Revenue (upd)"
name="Projects Revenue (Updated)"
sequence="50"/>
</odoo>