Package B
Package B
Package B
as
-- package header
/**********************************************************************************
*****
name: ssi_sptc_rpt_pkg
purpose: this package used for process data from tabels of module tdcp
for
report.
revisions:
ver date author description
--------- ---------- ---------------
------------------------------------
1.0 01/12/2021 tampv 1.
***********************************************************************************
*****/
-- SO LUONG DAU KI (sum lai theo ma chung khoan, team, tieu khoan)
function get_quantity_bg(
p_from_date date,
p_to_date date,
p_m_product_id number,
p_c_salesregion_id number,
p_s_subaccount_id number
) return number as
v_return number := 0;
begin
select sum(v.movementqty)
into v_return
from ssg_transaction_v v
where v.m_product_id = p_m_product_id
and v.c_salesregion_id = p_c_salesregion_id
and v.s_subaccount_id = p_s_subaccount_id
and ((v.movementdate between p_from_date and p_to_date) or (p_to_date is null
and p_from_date is null))
group by v.m_product_id, v.c_salesregion_id, v.s_subaccount_id;
return v_return;
exception
when others then
return v_return;
end get_quantity_bg;
-- DON GIA DAU KI (sum lai theo ma chung khoan, team, tieu khoan)
function get_costprice_bg(
p_from_date date,
p_to_date date,
p_m_product_id number,
p_c_salesregion_id number,
p_s_subaccount_id number
) return number as
v_return number := 0;
begin
select spcd.costprice
into v_return
from s_product_cost_detail spcd join s_product_cost spc
on (spcd.s_product_cost_id = spc.s_product_cost_id
and spc.m_product_id = p_m_product_id
and spc.s_subaccount_id = p_s_subaccount_id
and spc.c_salesregion_id = p_c_salesregion_id
and ((spcd.created between p_from_date and p_to_date) or (p_to_date is null
and p_from_date is null)))
order by spcd.created desc
fetch first 1 row only;
return v_return;
exception
when others then
return v_return;
end get_costprice_bg;
-- SO LUONG, GIA TRI MUA (sum lai theo ma chung khoan, team, tieu khoan) | lay
phieu nhap != quyen mua
function get_qtyentered_or_lineamount_b(
p_return varchar2,
p_from_date date,
p_to_date date,
p_m_product_id number,
p_c_salesregion_id number,
p_s_subaccount_id number
) return number as
v_return number := 0;
begin
select
case
when p_return = 'qtyentered' then sum(mil.qtyentered)
when p_return = 'lineamount' then sum(mil.lineamount)
end
into v_return
from m_inoutline mil
join m_inout mi
on (mil.m_inout_id = mi.m_inout_id
and mi.listed = 'CNY' and mi.c_doctype_id = 1000014 -- phieu nhap
and mil.m_product_id = p_m_product_id
and mil.s_subaccount_id = p_s_subaccount_id
and mil.c_salesregion_id = p_c_salesregion_id
and (extract(year from mi.movementdate) between extract(year from
p_from_date) and extract(year from p_to_date) or (p_from_date is null and p_to_date
is null)))
join (select cpl.c_productrightline_id from c_productrightline cpl join
c_productrightheader cp on (cpl.c_productrightheader_id =
cp.c_productrightheader_id and cp.c_productright_type != '02')) pr
on (pr.c_productrightline_id = mi.c_productrightline_id)
group by mil.m_product_id, mil.s_subaccount_id, mil.c_salesregion_id;
return v_return;
exception
when others then
return v_return;
end get_qtyentered_or_lineamount_b;
-- SO LUONG, GIA TRI BAN (sum lai theo ma chung khoan, team, tieu khoan) | lay
phieu nhap != quyen mua
function get_qtyentered_or_lineamount_s(
p_return varchar2,
p_from_date date,
p_to_date date,
p_m_product_id number,
p_c_salesregion_id number,
p_s_subaccount_id number
) return number as
v_return number := 0;
begin
select
case
when p_return = 'qtyentered' then sum(mil.qtyentered)
when p_return = 'lineamount' then sum(mil.lineamount)
end
into v_return
from m_inoutline mil
join m_inout mi
on (mil.m_inout_id = mi.m_inout_id
and mi.listed = 'CNY' and mi.c_doctype_id = 1000011
and mil.m_product_id = p_m_product_id
and mil.s_subaccount_id = p_s_subaccount_id
and mil.c_salesregion_id = p_c_salesregion_id
and (extract(year from mi.movementdate) between extract(year from
p_from_date) and extract(year from p_to_date) or (p_from_date is null and p_to_date
is null)))
group by mil.m_product_id, mil.s_subaccount_id, mil.c_salesregion_id;
return v_return;
exception
when others then
return v_return;
end get_qtyentered_or_lineamount_s;
-- SO LUONG, GIA TRI CP THUONG/MUA THEO QUYEN (sum lai theo ma chung khoan, team,
tieu khoan) | lay phieu nhap = quyen mua
function get_qtyentered_or_lineamount_prob(
p_return varchar2,
p_from_date date,
p_to_date date,
p_m_product_id number,
p_c_salesregion_id number,
p_s_subaccount_id number
) return number as
v_return number := 0;
begin
select
case
when p_return = 'qtyentered' then sum(mil.qtyentered)
when p_return = 'lineamount' then sum(mil.lineamount)
end
into v_return
from m_inoutline mil
join m_inout mi
on (mil.m_inout_id = mi.m_inout_id
and mi.listed = 'CNY' and mi.c_doctype_id = 1000014
and mil.m_product_id = p_m_product_id
and mil.s_subaccount_id = p_s_subaccount_id
and mil.c_salesregion_id = p_c_salesregion_id
and (extract(year from mi.movementdate) between extract(year from
p_from_date) and extract(year from p_to_date) or (p_from_date is null and p_to_date
is null)))
join (select cpl.c_productrightline_id from c_productrightline cpl join
c_productrightheader cp on (cpl.c_productrightheader_id =
cp.c_productrightheader_id and cp.c_productright_type = '02')) pr on
(pr.c_productrightline_id = mi.c_productrightline_id)
group by mil.m_product_id, mil.s_subaccount_id, mil.c_salesregion_id;
return v_return;
exception
when others then
return v_return;
end get_qtyentered_or_lineamount_prob;
-- SO LUONG, GIA TRI CO TUC/TRAI TUC, LAI TUC GIAM GIA VON (sum lai theo ma chung
khoan, team, tieu khoan) | lay phieu nhap = quyen mua
function get_qtyentered_or_lineamount_prostock(
p_return varchar2,
p_from_date date,
p_to_date date,
p_m_product_id number,
p_c_salesregion_id number,
p_s_subaccount_id number
) return number as
v_return number := 0;
begin
select
case
when p_return = 'qtyentered' then sum(mil.qtyentered)
when p_return = 'lineamount' then sum(mil.lineamount)
end
into v_return
from m_inoutline mil
join m_inout mi
on (mil.m_inout_id = mi.m_inout_id
and mi.listed = 'CNY' and mi.c_doctype_id = 1000014
and mil.m_product_id = p_m_product_id
and mil.s_subaccount_id = p_s_subaccount_id
and mil.c_salesregion_id = p_c_salesregion_id
and (extract(year from mi.movementdate) between extract(year from
p_from_date) and extract(year from p_to_date) or (p_from_date is null and p_to_date
is null)))
join (select cpl.c_productrightline_id from c_productrightline cpl join
c_productrightheader cp on (cpl.c_productrightheader_id =
cp.c_productrightheader_id and cp.c_productright_type = '01')) pr on
(pr.c_productrightline_id = mi.c_productrightline_id)
group by mil.m_product_id, mil.s_subaccount_id, mil.c_salesregion_id;
return v_return;
exception
when others then
return v_return;
end get_qtyentered_or_lineamount_prostock;
-- Parameter tu ngay
select para.p_date
into p_from_date
from ad_pinstance_para para
where ad_pinstance_id = p_ad_pinstance_id
and para.parametername = 'From_Date';
-- Parameter don vi
select para.p_number
into p_ad_org_id
from ad_pinstance_para para
where para.ad_pinstance_id = p_ad_pinstance_id
and para.parametername = 'AD_Org_ID';
-- Cap nhat cac line tinh toan tu tham so cua bang tam
for i in (select * from tdcp_rpt_003_temp) loop
-- gia tri dau ki
if (i.quantity_bg != 0 and i.unitprice_bg != 0) then
-- SL (12) * Đơn giá (13)
l_value_bg := i.quantity_bg * i.unitprice_bg;
end if;
-- lai lo ban/ ck
-- (26) - (18) - (27)
l_interest_sck := nvl(i.total_s, 0) - nvl(i.value_s, 0) - nvl(i.reduced_s,
0);
end ssi_tdcp_rpt_pkg;