lunes, 5 de agosto de 2013
Asientos Contables de las Facturas por Pagar
-- select cf.* from vw_asientos_factura cf;
drop view if exists vw_asientos_factura;
create view vw_asientos_factura as
with cte_asientos_factura as
(
select
f.c_invoice_id as id_factura,
f.documentno as cod_factura,
f.description as desc_factura,
f.dateacct as fecha_contable,
bp.c_bpartner_id as id_proveedor,
bp.value as cod_proveedor,
bp.name as proveedor,
o.ad_org_id as id_centro,
o.value as cod_centro,
o.name as centro_costo,
coalesce(ev.c_elementvalue_id, ev2.c_elementvalue_id) as id_cuenta_mov,
coalesce(ev.value, ev2.value) as cod_cuenta_mov,
coalesce(ev.name, ev2.name) as cuenta_contable_mov,
sum(df.linetotalamt) as monto,
ev3.c_elementvalue_id as id_cta_proveedor,
ev3.value as cod_cta_proveedor,
ev3.name as cta_contable_proveedor
from c_invoice f
join ad_org o
on f.ad_org_id = o.ad_org_id
join c_invoiceline df
on f.c_invoice_id = df.c_invoice_id
left outer join m_product p
on df.m_product_id = p.m_product_id
left outer join m_product_acct pc
on p.m_product_id = pc.m_product_id
left outer join c_validcombination vc
on pc.p_asset_acct = vc.c_validcombination_id
left outer join c_elementvalue ev
on vc.account_id = ev.c_elementvalue_id
left outer join c_charge c
on df.c_charge_id = c.c_charge_id
left outer join c_charge_acct ca
on c.c_charge_id = ca.c_charge_id
left outer join c_validcombination vc2
on ca.ch_expense_acct = vc2.c_validcombination_id
left outer join c_elementvalue ev2
on vc2.account_id = ev2.c_elementvalue_id
join c_bpartner bp
on f.c_bpartner_id = bp.c_bpartner_id
join c_bp_vendor_acct va
on bp.c_bpartner_id = va.c_bpartner_id
join c_bp_employee_acct ea
on bp.c_bpartner_id = ea.c_bpartner_id
join c_validcombination vc3
on vc3.c_validcombination_id =
case
when bp.isvendor = 'Y' then
va.v_liability_acct
when bp.isemployee = 'Y' then
ea.e_expense_acct
end
join c_elementvalue ev3
on vc3.account_id = ev3.c_elementvalue_id
where f.ad_client_id = 1000002
and f.docstatus = 'CO'
group by
f.c_invoice_id,
f.documentno,
f.description,
f.dateacct,
bp.c_bpartner_id,
bp.value,
bp.name,
o.ad_org_id,
o.value,
o.name,
ev.c_elementvalue_id,
ev.value,
ev.name,
ev2.c_elementvalue_id,
ev2.value,
ev2.name,
ev3.c_elementvalue_id,
ev3.value,
ev3.name
)
select
t.id_factura,
t.cod_factura,
t.id_centro,
t.cod_centro,
t.centro_costo,
t.cod_cuenta_mov,
t.cuenta_contable_mov,
t.fecha_contable,
t.monto,
'D' as tipo_mov,
t.desc_factura,
t.id_proveedor,
t.cod_proveedor,
t.proveedor
from cte_asientos_factura t
union all
select
t.id_factura,
t.cod_factura,
t.id_centro,
t.cod_centro,
t.centro_costo,
t.cod_cta_proveedor,
t.cta_contable_proveedor,
t.fecha_contable,
t.monto,
'C' as tipo_mov,
t.desc_factura,
t.id_proveedor,
t.cod_proveedor,
t.proveedor
from cte_asientos_factura t
order by
8 desc, -- fecha_contable
1; -- id_factura
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario