SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE view [dbo].[vw_acc_invoices_policies_situations_totals]
as
	select	invpolt_invoice_id = ipv.invpolv_invoice_id,
			invpolt_policy_situation_id = ipv.invpolv_policy_situation_id,
			invpolt_variable_type_code = nv.nmpv_type_code,
			invpolt_tf = sum(ipv.invpolv_tf),
			invpolt_taxes = sum(ipv.invpolv_taxes),
			invpolt_ti = sum(ipv.invpolv_ti),
			invpolt_commission_ti = sum(ipv.invpolv_commission_ti),
			invpolt_commission_tf = sum(ipv.invpolv_commission_tf),
			invpolt_commission_taxes = sum(ipv.invpolv_commission_taxes)
	from	acc_invoices_policies_variables ipv
			inner join pr_products_variables pv on pv.prdv_variable_id = ipv.invpolv_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 = ipv.invpolv_invoice_id and ai.inv_operation_code__last <> 'AN'
	group by ipv.invpolv_invoice_id, ipv.invpolv_policy_situation_id, nv.nmpv_type_code 


GO
