SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE view [dbo].[vw_acc_invoices_risks_situations_totals]
as
	select	invrist_invoice_id = irv.invrisv_invoice_id,
			invrist_risk_situation_id = irv.invrisv_risk_situation_id,
			invrist_variable_type_code = nv.nmpv_type_code,
			invrist_tf = sum(irv.invrisv_tf),
			invrist_taxes = sum(irv.invrisv_taxes),
			invrist_ti = sum(irv.invrisv_ti),
			invrist_commission_ti = sum(irv.invrisv_commission_ti),
			invrist_commission_tf = sum(irv.invrisv_commission_tf),
			invrist_commission_taxes = sum(irv.invrisv_commission_taxes)
	from	acc_invoices_risks_variables irv
			inner join pr_products_variables pv on pv.prdv_variable_id = irv.invrisv_variable_id 
			inner join nm_products_variables nv on nv.nmpv_variable_code = pv.prdv_variable_code 
			inner join acc_invoices ai on ai.inv_invoice_id = irv.invrisv_invoice_id and ai.inv_operation_code__last <> 'AN'
	group by irv.invrisv_invoice_id, irv.invrisv_risk_situation_id, nv.nmpv_type_code 
			
			


GO
