lunes, 12 de marzo de 2012
Inventario Inicial Contabilizado
-- VISTA QUE CONTIENE LA CONSULTA DEL REPORTE
-- select ic.* from vw_inventario_contabilizado ic;
-- drop view vw_inventario_contabilizado;
create view vw_inventario_contabilizado as
(
select
i.m_inventory_id cod_inventario,
i.documentno no_inventario,
i.description descripcion,
cast('C$' as character varying) moneda,
o.ad_org_id cod_organizacion,
o.name organizacion,
ev.value cod_cuenta,
evt.name cuenta_contable,
f.amtacctdr debito,
f.amtacctcr credito,
coalesce(p.m_product_id, 0) id_producto,
coalesce(p.value, '') cod_producto,
coalesce(pt.name, '') producto,
f.dateacct fecha_contable,
pp.c_period_id cod_periodo,
pp.name periodo,
cast('Actual' as character varying) tipo_aplicacion -- ad_ref_list
from fact_acct f
join ad_org o
on f.ad_org_id = o.ad_org_id
join c_elementvalue ev
on f.account_id = ev.c_elementvalue_id
join c_elementvalue_trl evt
on (ev.c_elementvalue_id = evt.c_elementvalue_id
and evt.ad_language = 'es_CO')
left outer join m_product p
on f.m_product_id = p.m_product_id
left outer join m_product_trl pt
on (p.m_product_id = pt.m_product_id
and pt.ad_language = 'es_CO')
join c_period pp
on f.c_period_id = pp.c_period_id
join m_inventory i
on f.record_id = i.m_inventory_id
where f.ad_client_id = 1000000
and f.postingtype = 'A'
and f.c_acctschema_id = 1000000
and f.ad_table_id = 321 -- Physical Inventory
--and f.record_id = 1000061
and f.fact_acct_id not in
(select
pa.record_id
from ad_private_access pa
where pa.ad_table_id = 270 -- GLJournal
and pa.ad_user_id <> 0
and pa.isactive = 'Y')
and exists
(
select 1 from rv_transaction t
where t.m_inventory_id = i.m_inventory_id
and t.movementtype = 'I+'
)
and i.docstatus = 'CO'
and i.isactive = 'Y'
order by
f.fact_acct_id desc
);
-- FUNCION QUE CONTIENE LA VISTA DEL REPORTE
/*
drop type inventario_contabilizado;
create type inventario_contabilizado as
(
cod_inventario numeric(10,0),
no_inventario character varying,
descripcion character varying,
moneda character varying,
cod_organizacion numeric(10,0),
organizacion character varying(60),
cod_cuenta character varying(40),
cuenta_contable character varying(60),
debito numeric,
credito numeric,
id_producto numeric,
cod_producto character varying,
producto character varying,
fecha_contable timestamp without time zone,
cod_periodo numeric(10,0),
periodo character varying(60),
tipo_aplicacion character varying
);
*/
-- drop function rep_inventario_contabilizado(finicial date, ffinal date, num_inventario character varying)
-- select * from rep_inventario_contabilizado(0, 0);
-- select * from rep_inventario_contabilizado('2011-11-01', '2011-12-30', '4968');
create or replace function rep_inventario_contabilizado(finicial date, ffinal date, num_inventario character varying)
returns setof inventario_contabilizado as $$
declare
resultado inventario_contabilizado%rowtype;
begin
drop table if exists tmp_inventario_contabilizado;
create table tmp_inventario_contabilizado as
select
ic.*
from vw_inventario_contabilizado ic
where ic.fecha_contable between finicial and ffinal
and ic.no_inventario = case when num_inventario = '0' then ic.no_inventario else num_inventario end;
for resultado in execute
'select
t.*
from tmp_inventario_contabilizado t;'
loop
return next resultado;
end loop;
end;
$$ language plpgsql;
-- VISTA QUE SE UTILIZA EN EL TERCER PARAMETRO DEL REPORTE
-- drop view param_num_inventario;
create view param_num_inventario as
(
select distinct
i.documentno codigo,
i.description nombre,
i.ad_client_id,
i.ad_org_id,
i.created,
i.createdby,
i.updated,
i.updatedby,
i.isactive
from m_inventory i
join m_inventoryline il
on i.m_inventory_id = il.m_inventory_id
where i.ad_client_id = 1000000
and il.inventorytype = 'D'
and exists
(
select 1 from rv_transaction t
where t.m_inventory_id = i.m_inventory_id
and t.movementtype = 'I+'
)
);
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario