You are on page 1of 19

CREATE OR REPLACE package body DGDEV.

ssi_description
as
/******************************************************************************
NAME: SSI_DESCRIPTION
PURPOSE:

REVISIONS:
Ver Date Author Description
--------- ---------- --------------- ------------------------------------
1.0 4/10/2021 lamnl 1. Created this package body.
******************************************************************************/

/*lay ra ngay lam viec cua 1 ngay nao do*/


function get_wkday(
i_date in date,
i_type in varchar2
)
return date
is
l_date date;
begin
/*ngay dung*/
if i_type = '01' then
l_date := i_date;
elsif i_type = '02' then /*ngay lam viec ngay truoc*/
select max(a.date_key) into l_date
from date_dimension a
where a.date_key <= i_date
and not exists (select 'x' from C_NonBusinessDay where
date1=a.date_key);
else /*ngay lam viec ngay sau*/
select min(a.date_key) into l_date
from date_dimension a
where a.date_key >= i_date
and not exists (select 'x' from C_NonBusinessDay where
date1=a.date_key);
end if;
dbms_output.put_line('working day='||l_date);
return(l_date);
end get_wkday;

-- dien giai phieu lenh, hop dong line


function getdesorderline (i_order_id in number)
return varchar2
is
cursor cur_orderline(m_order_id number) is
select mp.value, col.qtyordered from c_orderline col, m_product
mp
where col.m_product_id = mp.m_product_id
and col.c_order_id = m_order_id;

v_des varchar2(1000);
v_outdes varchar2(1000);
v_outmsg varchar2(1000);
v_count number;
begin
select dt.name
into v_outmsg
from c_doctype dt, c_order co
where co.c_order_id = i_order_id
and dt.c_doctype_id = co.c_doctype_id ;

v_outmsg := v_outmsg || ' ( ';


v_count := 0;
for cur_order in cur_orderline(i_order_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_order.value||'_'||cur_order.qtyordered;
end loop;
v_outdes := v_outmsg ||' ) ';

select bp.name || ' ngày '|| to_char(co.dateordered,'DD/MM/YYYY')


into v_des
from c_bpartner bp, c_order co
where co.c_order_id = i_order_id
and bp.c_bpartner_id = co.c_bpartner_id;

v_outdes := v_outdes || v_des;

return v_outdes;
end ;

-- so luong hop dong


function getdesorderlineqty (c_order_id in number) return varchar2
is
cursor cur_orderline(i_order_id number) is
select mp.value, col.qtyordered, col.linenetamt from
c_orderline col, m_product mp
where col.m_product_id = mp.m_product_id
and col.c_order_id = i_order_id;

v_outmsg varchar2(1000);
v_count number;
begin
v_outmsg := '( ';
v_count := 0;
for cur_order in cur_orderline(c_order_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_order.value ||'_'||
cur_order.qtyordered||'_'
||to_char(cur_order.linenetamt,'fm999g999g999g999g999');
end loop;
v_outmsg := v_outmsg ||' )';
return v_outmsg;
end ;

-- Phieu Nhap Xuat


function getdesinout (i_inout_id in number) return varchar2
is
cursor cur_inoutline(i_inout_id number) is
select mp.value, col.qtyentered
from m_inoutline col, m_product mp
where col.m_product_id = mp.m_product_id
and col.m_inout_id = i_inout_id;

v_outdes varchar2(1000);
v_outmsg varchar2(1000);
v_msg varchar2(1000);
v_count number;
v_qty number;
curinout m_inout%rowtype;
begin

select *
into curinout
from m_inout
where m_inout_id = i_inout_id;

-- Phan loai lai


if curinout.c_doctype_id = c_doctype_reclass then
select cd.name ||' ngày '|| to_char(mi.movementdate,'DD/MM/YYYY')
into v_outdes
from c_doctype cd, m_inout mi
where mi.c_doctype_id = cd.c_doctype_id
and mi.m_inout_id = i_inout_id;
elsif curinout.c_doctype_id = c_doctype_adjcost then
-- DIEU CHINH GIA VON
select cd.name
into v_outdes
from c_doctype cd, m_inout mi
where mi.c_doctype_id = cd.c_doctype_id
and mi.m_inout_id = i_inout_id;

v_outmsg := v_outmsg || ' ( ';


v_count := 0;
for cur_inout in cur_inoutline(i_inout_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_inout.value ||'_'||
cur_inout.qtyentered;
end loop;
v_outdes := v_outdes || v_outmsg ||' ) ';
elsif curinout.c_doctype_id = c_doctype_receip then
if curinout.islotodd = 'Y' then
-- Mua lo le
v_qty :=0;
v_outmsg := 'Mua lô lẻ theo bảng kê ';
v_outmsg := v_outmsg || ' ( ';
v_count := 0;
for cur_inout in cur_inoutline(i_inout_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_inout.value ||'_'||
cur_inout.qtyentered;
end loop;

select to_char(movementdate,'DD/MM/YYYY')
into v_msg
from m_inout
where m_inout_id = i_inout_id;

v_outdes := v_outmsg ||' ) '||v_msg;


else
select cd.name
into v_outmsg
from m_inout mi, c_doctype cd
where mi.c_doctype_id = cd.c_doctype_id
and mi.m_inout_id = i_inout_id;

v_outmsg := v_outmsg || ' ( ';


v_count := 0;
for cur_inout in cur_inoutline(i_inout_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_inout.value ||'_'||
cur_inout.qtyentered;
end loop;
v_outdes := v_outmsg ||' ) ';

select sb.name
into v_msg
from m_inout mi, s_subaccount sb
where mi.s_subaccount_id = sb.s_subaccount_id
and mi.m_inout_id = i_inout_id;

v_outdes := v_outdes || v_msg;

end if;
elsif curinout.c_doctype_id = c_doctype_sellfixes then
-- Ban sua loi
v_qty := 0;
v_msg := ' ';
select cd.name
into v_outmsg
from m_inout mi, c_doctype cd
where mi.c_doctype_id = cd.c_doctype_id;

for cur_inout in cur_inoutline(i_inout_id) loop


v_qty := v_qty + cur_inout.qtyentered;
v_msg := v_msg || cur_inout.value ;
end loop;

v_outdes := v_outmsg ||' '|| v_qty ||v_msg;


select to_char(movementdate,'DD/MM/YYYY')
into v_msg
from m_inout
where m_inout_id = i_inout_id;
v_outdes := v_outdes ||' '||v_msg;
elsif curinout.c_doctype_id = c_doctype_shipment then
select cd.name
into v_outmsg
from m_inout mi, c_doctype cd
where mi.c_doctype_id = cd.c_doctype_id
and mi.m_inout_id = i_inout_id;
v_outmsg := v_outmsg || ' ( ';
v_count := 0;
for cur_inout in cur_inoutline(i_inout_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_inout.value ||'_'||
cur_inout.qtyentered;
end loop;
v_outdes := v_outmsg ||' ) ';

select sb.name
into v_msg
from m_inout mi, s_subaccount sb
where mi.s_subaccount_id = sb.s_subaccount_id
and mi.m_inout_id = i_inout_id;

v_outdes := v_outdes || v_msg;


else
dbms_output.put_line('des');
select cd.name
into v_outmsg
from m_inout mi, c_doctype cd
where mi.c_doctype_id = cd.c_doctype_id
and mi.m_inout_id = i_inout_id;

v_outmsg := v_outmsg || ' ( ';


v_count := 0;
for cur_inout in cur_inoutline(i_inout_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_inout.value ||'_'||
cur_inout.qtyentered;
end loop;
v_outdes := v_outmsg ||' ) ';

select sb.name
into v_msg
from m_inout mi, s_subaccount sb
where mi.s_subaccount_id = sb.s_subaccount_id
and mi.m_inout_id = i_inout_id;

v_outdes := v_outdes || v_msg;


end if;
return v_outdes;
end ;

-- dien giai phieu nhap xuat line


function getdesinoutline ( i_inoutline_id in number)
return varchar2
is
v_outdes varchar2(1000);
v_outmsg varchar2(1000);
v_msg varchar2(1000);
v_trading_type varchar2(1000);
v_count number;
v_qty number;
curinoutline m_inoutline%rowtype;
curinout m_inout%rowtype;
cur_product m_product%rowtype;
begin
select *
into curinoutline
from m_inoutline
where m_inoutline_id = i_inoutline_id;

select *
into cur_product
from m_product
where m_product_id = curinoutline.m_product_id;

select *
into curinout
from m_inout
where curinoutline.m_inout_id = m_inout_id;

select name
into v_trading_type
from ad_ref_list_trl
where ad_ref_list_id =(select ad_ref_list_id
from ad_ref_list
where ad_reference_id = c_reference_trading_type
and value = cur_product.self_trading_type);

if curinout.c_order_id >0 then


select cd.name|| ' - '|| mil.qtyentered|| ' '||lower(v_trading_type)
||' '
|| mp.value|| ' - '|| cs.value
|| ' theo hợp đồng số '|| co.documentno
into v_outdes
from m_inout mi,
m_inoutline mil,
c_doctype cd,
m_product mp,
c_order co,
c_salesregion cs
where mi.m_inout_id = mil.m_inout_id
and mi.c_order_id = co.c_order_id
and mi.c_doctype_id = cd.c_doctype_id
and mil.m_product_id = mp.m_product_id
and mil.m_inoutline_id = i_inoutline_id
and cs.c_salesregion_id = mil.c_salesregion_id;

else
select cd.name|| ' '|| mil.qtyentered|| ' '||lower(v_trading_type)
||' '|| mp.value
|| ' '|| sb.name|| ' '|| to_char
(mi.movementdate, 'DD/MM/YYYY')
into v_outdes
from m_inout mi,
m_inoutline mil,
c_doctype cd,
m_product mp,
s_subaccount sb
where mi.m_inout_id = mil.m_inout_id
and mi.c_doctype_id = cd.c_doctype_id
and mil.m_product_id = mp.m_product_id
and mil.m_inoutline_id = i_inoutline_id
and mil.s_subaccount_id = sb.s_subaccount_id;
end if;
return v_outdes;
end;

-- dien giai chuyen kho header


function getdesmovement (i_movement_id in number)
return varchar2
is
cursor cur_movementline(p_movement_id number) is
select mp.name
from m_movementline mml, m_product mp
where mml.m_product_id = mp.m_product_id
and mml.m_movement_id = p_movement_id;
v_outdes varchar2(1000);
v_outmsg varchar2(1000);
v_count number;
begin
select cd.name||' '||to_char(mm.movementdate,'DD/MM/YYYY')
into v_outmsg
from c_doctype cd, m_movement mm
where mm.c_doctype_id = cd.c_doctype_id
and mm.m_movement_id = i_movement_id;
v_outmsg := v_outmsg || ' ( ';
v_count := 0;
for cur_line in cur_movementline(i_movement_id) loop
v_count := v_count +1;
if v_count > 1 then
v_outmsg := v_outmsg || ' - ';
end if;
v_outmsg := v_outmsg || cur_line.name ;
end loop;
v_outdes := v_outmsg ||' ) ';
return v_outdes;
end;
-- dien giai chuyen kho line
function getdesmovementline (i_movementline_id in number)
return varchar2
is
v_outdes varchar2(1000);
v_outdesh varchar2(1000);
v_name varchar2(100);
o_des varchar2(1000);
cur_movementline m_movementline%rowtype;
cur_movement m_movement%rowtype;
begin
v_name := '';
select *
into cur_movementline
from m_movementline
where m_movementline_id = i_movementline_id;

select * into cur_movement


from m_movement
where m_movement_id = cur_movementline.m_movement_id;
if cur_movement.c_doctype_id = c_doctype_movement then
-- CHUYEN KHO
select mp.value
into v_name
from m_movementline mml,m_product mp
where mp.m_product_id = mml.m_product_id
and mml.m_movementline_id = i_movementline_id;

select cd.name||' '||v_name||' ( '||cs.name||', '||sb.name||') -> '


into v_outdesh
from c_doctype cd, m_movement mm, c_salesregion cs,s_subaccount sb
where mm.m_movement_id = cur_movementline.m_movement_id
and mm.c_doctype_id = cd.c_doctype_id
and sb.s_subaccount_id = mm.s_subaccount_id
and mm.c_salesregion_to = cs.c_salesregion_id;

select '( '||cs.name||', '||sb.name||')'


into v_outdes
from m_movementline mml ,c_salesregion cs,s_subaccount sb
where mml.m_movementline_id = i_movementline_id
and sb.s_subaccount_id = mml.s_subaccount_id
and mml.c_salesregion_to_id = cs.c_salesregion_id;

o_des := v_outdesh||v_outdes;

elsif cur_movement.c_doctype_id = c_doctype_status_change then


-- CHUYEN TRANG THAI
select cd.name||' '||mp.name
into o_des
from c_doctype cd, m_movement mm, m_movementline mml,m_product mp
where mm.m_movement_id = mml.m_movement_id
and mm.c_doctype_id = cd.c_doctype_id
and mp.m_product_id = mml.m_product_id
and mml.m_movementline_id = cur_movementline.m_movementline_id;

end if;
return o_des;
exception
when others then
o_des := '';
return o_des;
end;

-- dien giai dieu chinh gia, phan loai lai, danh gia lai header

function get_inventory_header_description (i_inventory_id in number,


i_asset_type in number)
return varchar2
is
cursor cur_get_inventoryline (i_m_inventory_id in number)
is
select mp.name, mil.qty_change, mil.newcostprice
from m_product mp, m_inventoryline mil, m_inventory mi
where mp.m_product_id = mil.m_product_id
and mil.m_inventory_id = mi.m_inventory_id
and mil.m_inventory_id = i_inventory_id;
cur_inventory m_inventory%rowtype;
o_des varchar2 (1000);
v_asset_type varchar2 (100);
v_count number;
begin
select *
into cur_inventory
from m_inventory
where m_inventory_id = i_inventory_id;

if cur_inventory.c_doctype_id != c_doctype_cost_adjustment
then
select name
into v_asset_type
from ad_ref_list_trl, m_inventory mi
where ad_ref_list_id =
(select ad_ref_list_id
from ad_ref_list
where ad_reference_id = i_asset_type
and value = mi.asset_type
and mi.m_inventory_id = i_inventory_id);
end if;

if cur_inventory.c_doctype_id = c_doctype_cost_adjustment
then
o_des := ' ( ';
v_count := 0;

for get_inventoryline in cur_get_inventoryline (i_inventory_id)


loop
v_count := v_count + 1;

if v_count > 1
then
o_des := o_des || ' - ';
end if;

o_des :=
o_des
|| get_inventoryline.name
|| '_'
|| get_inventoryline.qty_change
|| '_'
|| get_inventoryline.newcostprice;
end loop;

o_des := o_des || ' ) ';


elsif cur_inventory.c_doctype_id = c_doctype_revaluation
then
select ss.name
|| '_'
|| v_asset_type
|| '_'
|| mpc.name
|| '_'
|| to_char (mi.movementdate, 'dd/mm/yyyy')
into o_des
from m_inventory mi, m_product_category mpc, s_subaccount ss
where mi.s_subaccount_id = ss.s_subaccount_id
and mi.m_product_category_id = mpc.m_product_category_id
and mi.m_inventory_id = i_inventory_id;
elsif cur_inventory.c_doctype_id = c_doctype_reclassification
then
select to_char (mi.movementdate, 'dd/mm/yyyy')
into o_des
from m_inventory mi
where mi.m_inventory_id = i_inventory_id;
end if;

return o_des;
end;

-- dien giai dieu chinh gia, danh gia lai, phan loai lai line
function get_inventory_line_description (i_inventoryline_id in number,
i_asset_type in number)
return varchar2
is
cursor cur_get_inventoryline (i_inventoryline_id in number)
is
select mil.qty_change,
mp.name as product,
cs.name as team,
ss.name as subaccount
from m_inventory mi,
m_inventoryline mil,
m_product mp,
s_subaccount ss,
c_salesregion cs
where mil.m_product_id = mp.m_product_id
and mi.c_salesregion_id = cs.c_salesregion_id
and mi.s_subaccount_id = ss.s_subaccount_id
and mi.m_inventory_id = mil.m_inventory_id
and mil.m_inventoryline_id = i_inventoryline_id;

cur_inventory m_inventory%rowtype;
cur_inventoryline m_inventoryline%rowtype;
o_des varchar (1000);
v_asset_type varchar (100);
v_doctype_id number;
begin
select mi.c_doctype_id
into v_doctype_id
from m_inventoryline mil, m_inventory mi
where mi.m_inventory_id = mil.m_inventory_id
and mil.m_inventoryline_id = i_inventoryline_id;

if v_doctype_id != c_doctype_cost_adjustment
then
select arlt.name
into v_asset_type
from ad_ref_list_trl arlt, m_inventory mi, m_inventoryline mil
where ad_ref_list_id =
(select ad_ref_list_id
from ad_ref_list
where ad_reference_id = i_asset_type
and mil.m_inventory_id = mi.m_inventory_id
and value = mi.asset_type
and mil.m_inventoryline_id = i_inventoryline_id);
end if;

if v_doctype_id = c_doctype_cost_adjustment
then
for get_inventoryline in cur_get_inventoryline (i_inventoryline_id)
loop
o_des :=
o_des
|| get_inventoryline.qty_change
|| ' - '
|| get_inventoryline.product
|| ' - '
|| get_inventoryline.team
|| ' - '
|| get_inventoryline.subaccount;
end loop;

return o_des;
elsif v_doctype_id = c_doctype_revaluation
then
select ss.name
|| ' - '
|| v_asset_type
|| ' - '
|| mp.name
|| ' - '
|| to_char (mi.movementdate, 'dd/mm/yyyy')
into o_des
from m_inventoryline mil,
m_inventory mi,
m_product mp,
s_subaccount ss
where mil.s_subaccount_id = ss.s_subaccount_id
and mil.m_product_id = mp.m_product_id
and mil.m_inventory_id = mi.m_inventory_id
and mil.m_inventoryline_id = i_inventoryline_id;
elsif v_doctype_id = c_doctype_reclassification
then
select mp.name || ' - ' || to_char (mi.movementdate, 'dd/mm/yyyy')
into o_des
from m_inventory mi, m_inventoryline mil, m_product mp
where mil.m_inventory_id = mi.m_inventory_id
and mil.m_product_id = mp.m_product_id
and mil.m_inventoryline_id = i_inventoryline_id;
end if;

return o_des;
end;

-- dien giai quyen cp header


function get_productrightheader_description (
i_productrightheader_id in number,
i_type_id in varchar2
) return varchar2
is
o_des varchar2(1000);
begin
select mp.value || ' - ' ||
case
when pr.c_productright_type = f_productstock_type then
pr.stock_ratio ||' ngày '||
to_char(pr.ex_date, 'dd/mm/yyyy')
when pr.c_productright_type = f_productbuy_type then
pr.convert_ratio ||' ngày '||
to_char(pr.last_registration_date,'dd/mm/yyyy')
when pr.c_productright_type = f_productmoney_type then
to_char(pr.cash_amount,'fm999g999g999g999g999d00') || ' - ' ||
to_char(pr.cash_percent,'fm999g999g999g999g999d0') ||' ngày '||
to_char(pr.record_date, 'dd/mm/yyyy')
when pr.c_productright_type = f_productmerge_type or
pr.c_productright_type = f_productsplit_type then
pr.split_ratio ||' ngày '||
to_char(pr.expected_exec_date,'dd/mm/yyyy')
when pr.c_productright_type = f_productswap_type then
pr.swaps_ratio || ' ngày ' ||
to_char(pr.ex_swaps_date, 'dd/mm/yyyy')
when pr.c_productright_type = f_productbond_type then
pr.convert_ratio ||' ngày '||
to_char(pr.last_registration_date, 'dd/mm/yyyy')
end
into o_des
from c_productrightheader pr,
m_product mp
where ((mp.m_product_id = pr.m_product_id and c_productright_type !=
'03')
or ( pr.m_product1_id = mp.m_product_id and c_productright_type = '03'))
and pr.c_productrightheader_id = i_productrightheader_id;
return o_des;
exception
when others then
o_des := null;
return o_des;
end;

-- dien giai quyen cp line


function get_productrightline_description (
i_productrightline_id in number,
i_type_id in varchar2
) return varchar2
is
o_des varchar2(1000);

begin
select mp.value || ' - ' ||
(case
when pr.c_productright_type = f_productstock_type then
pr.stock_ratio ||' ngày '||
to_char(prl.datetrx, 'dd/mm/yyyy')
when pr.c_productright_type = f_productbuy_type then
pr.convert_ratio ||' ngày '||
to_char(prl.datetrx,'dd/mm/yyyy')
when pr.c_productright_type = f_productmoney_type then
to_char(cs.name) ||' ngày '||
to_char(prl.datetrx,'dd/mm/yyyy')
when pr.c_productright_type = f_productmerge_type
or pr.c_productright_type = f_productsplit_type then
pr.split_ratio ||' ngày '||
to_char(prl.datetrx, 'dd/mm/yyyy')
when pr.c_productright_type = f_productswap_type then
pr.swaps_ratio || ' ' ||
sa.name ||' ngày '||
to_char(prl.datetrx, 'dd/mm/yyyy')
when pr.c_productright_type = f_productbond_type then
pr.convert_ratio ||' ngày '||
to_char(prl.datetrx, 'dd/mm/yyyy')
end)
into o_des
from c_productrightheader pr,
c_productrightline prl,
m_product mp,
s_subaccount sa,
c_salesregion cs
where ((mp.m_product_id = pr.m_product_id and pr.c_productright_type !=
'03')
or ( pr.m_product1_id = mp.m_product_id and pr.c_productright_type =
'03'))
and prl.s_subaccount_id = sa.s_subaccount_id
and prl.c_salesregion_id = cs.c_salesregion_id
and pr.c_productrightheader_id = prl.c_productrightheader_id
and prl.c_productrightline_id = i_productrightline_id;

return o_des;
exception
when others then
o_des := null;
return o_des;
end;

-- lamnl hoa don header


function get_invoice_des(i_invoice_id in number)
return varchar2
is
o_des varchar2(1000);
o_type varchar2(500);
v_desinout varchar2(500);
cur_invoice c_invoice%rowtype;
begin
select * into cur_invoice
from c_invoice
where c_invoice_id = i_invoice_id;

select cd.name
into o_des
from c_invoice ci, c_doctype cd
where ci.c_doctype_id = cd.c_doctype_id
and ci.c_invoice_id = i_invoice_id;

if cur_invoice.trading_type is not null then

if cur_invoice.m_inout_id > 0 then


if cur_invoice.trading_type = 'PCI' then
select lower(name) into o_type
from ad_ref_list_trl where ad_ref_list_id =
(select ad_ref_list_id from ad_ref_list
where ad_reference_id = c_reference_tradingtype
and value = cur_invoice.trading_type);
select description
into v_desinout
from m_inout
where m_inout_id = cur_invoice.m_inout_id;
o_des:= o_des ||' '||o_type||' '||v_desinout;

else
select description
into v_desinout
from m_inout
where m_inout_id = cur_invoice.m_inout_id;
o_des:= o_des ||' '||v_desinout;
end if;

else
select lower(name) into o_type
from ad_ref_list_trl where ad_ref_list_id =
(select ad_ref_list_id from ad_ref_list
where ad_reference_id = c_reference_tradingtype
and value = cur_invoice.trading_type);

o_des:= o_des||' '|| 'giao dịch ' ||o_type ||' '||


to_char(cur_invoice.grandtotal,'fm999g999g999g999g999d00');
end if;
end if;

return o_des;
end;

-- lamnl hoa don


function get_invoiceline_des (
i_invoiceline_id in number
) return varchar2
is
o_des varchar2(1000);
o_type varchar2(200);
v_desline varchar2(200);
v_desinout varchar2(200);
cur_invoice c_invoice%rowtype;
cur_invoiceline c_invoiceline%rowtype;
cur_inoutline m_inoutline%rowtype;
begin

select * into cur_invoiceline


from c_invoiceline
where c_invoiceline_id = i_invoiceline_id;

select * into cur_invoice


from c_invoice
where c_invoice_id = cur_invoiceline.c_invoice_id;

select cd.name
into o_des
from c_invoice ci, c_doctype cd
where ci.c_doctype_id = cd.c_doctype_id
and ci.c_invoice_id = cur_invoice.c_invoice_id;
if cur_invoice.trading_type is not null then
if cur_invoiceline.m_inoutline_id > 0 then
select description
into v_desinout
from m_inoutline
where m_inoutline_id = cur_invoiceline.m_inoutline_id;
o_des:= o_des ||' '||v_desinout;

else
select lower(name) into o_type
from ad_ref_list_trl where ad_ref_list_id =
(select ad_ref_list_id from ad_ref_list
where ad_reference_id = c_reference_tradingtype
and value = cur_invoice.trading_type);

o_des:=o_des||' '||'giao dịch ' ||o_type ;


select to_char(cil.linetotalamt,'fm999g999g999g999g999d00' )
into v_desline
from c_invoiceline cil
where c_invoiceline_id = i_invoiceline_id;
o_des := o_des||' '|| v_desline;
end if;
end if;
return o_des;
end;

-- niem yet chung khoan


function get_listed_product_des (i_movement_listed_header_id in number)
return varchar2
as
o_des varchar2(1000);
cur_movement c_movement_listed_header%rowtype;
begin
select * into cur_movement
from c_movement_listed_header
where c_movement_listed_header_id = i_movement_listed_header_id;

select cd.name
into o_des
from c_doctype cd
where cd.c_doctype_id = cur_movement.c_doctype_id;

o_des := o_des || ' ' ||cur_movement.upc;


return o_des;
exception
when others then
o_des := '';

return o_des;
end;

-- dau gia chung khoan


function get_offering_des (i_offering_id in number)
return varchar2
as
o_des varchar2(1000);
cur_offering s_offering%rowtype;
begin
select cd.name|| ' '||so.documentno ||' '|| cp.value ||' - '||mp.name
||'( '||so.ipo_qty||', '||so.lineamount||', '||
to_char(so.grandtotal,'fm999g999g999g999g999d00')||')'
into o_des
from m_product mp, s_offering so, c_bpartner cp, c_doctype cd
where mp.m_product_id = so.m_product_id
and cp.c_bpartner_id = so.c_bpartner_id
and so.c_doctype_id = cd.c_doctype_id
and so.s_offering_id =i_offering_id;

return o_des;
exception
when others then
o_des := '';

return o_des;
end;

-- thuc hien dau gia chung khoan


function get_offeringtrx_des (i_offeringtrx_id in number)
return varchar2
as
o_des varchar2(1000);
cur_offeringtrx s_offering_trx%rowtype;
begin
select cd.name|| ' '||so.documentno ||' - '|| cp.value ||' - '||mp.name
||'( '||so.accepted_quantity||', '||so.accepted_unit_price||', '||
to_char(so.accepted_amt,'fm999g999g999g999g999d00')||' )'
into o_des
from m_product mp, s_offering_trx so, c_bpartner cp ,c_doctype cd
where mp.m_product_id = so.m_product_id
and cp.c_bpartner_id = so.c_bpartner_id
and so.c_doctype_id = cd.c_doctype_id
and so.s_offering_trx_id = i_offeringtrx_id;

return o_des;
exception
when others then
o_des := '';

return o_des;
end;

-- phat hop dong - lai ung truoc


function get_overdue_des (i_overdue_id in number)
return varchar2
as
o_des varchar2(1000);
cur_overdue c_order_overdue%rowtype;
cur_order c_order%rowtype;
begin
select * into cur_overdue
from c_order_overdue
where c_order_overdue_id = i_overdue_id;

select * into cur_order


from c_order
where c_order_id = cur_overdue.c_order_id;
if cur_overdue.due_type = '01' then
if cur_overdue.c_method_type = 'R' then
o_des := 'Lãi ứng trước của hợp đồng ' ||cur_order.documentno
||' từ '||to_char(cur_overdue.date_from,'dd/mm/yyyy') ||' đến '||
to_char(cur_overdue.date_to,'dd/mm/yyyy');
else
o_des := 'Lãi ứng trước của hợp đồng ' ||cur_order.documentno
||' ngày '||to_char(cur_overdue.datetrx,'dd/mm/yyyy');

end if;
else
o_des := 'Phạt trễ hạn hợp đồng ' ||cur_order.documentno
||' ngày '||to_char(cur_order.dateordered,'dd/mm/yyyy');
end if;
return o_des;
exception
when others then
o_des := '';
return o_des;
end;

-- De nghi thanh toan M_Contract_Act


function get_contract_act_des (i_contract_act_id in number)
return varchar2
as
o_des varchar2(1000);
o_des_orderline varchar2(1000);
o_type varchar2(100);
cur_contract_act c_contract_act%rowtype;
begin

select * into cur_contract_act


from c_contract_act
where c_contract_act_id = i_contract_act_id;

select name into o_type


from ad_ref_list_trl where ad_ref_list_id =
(select ad_ref_list_id from ad_ref_list
where ad_reference_id = c_reference_paymenttype
and value = cur_contract_act.payment_type);

if cur_contract_act.c_order_id > 0 then


-- Hop dong mua ban

select lower(cd.name) || ' ' || co.documentno


into o_des
from c_doctype cd, c_order co
where co.c_doctype_id = cd.c_doctype_id
and co.c_order_id = cur_contract_act.c_order_id;

o_des_orderline := getdesorderlineqty(cur_contract_act.c_order_id);

o_des := o_type ||' '|| o_des ||' '||o_des_orderline;

-- if cur_contract_act.isreceip = 'Y' then


-- select lower(cd.name) || ' ' || lower(mpc.name)
-- into o_des
-- from c_doctype cd, c_order co, m_product_category mpc
-- where co.c_doctype_id = cd.c_doctype_id
-- and co.m_product_category_id = mpc.m_product_category_id
-- and co.c_order_id = cur_contract_act.c_order_id;
--
-- o_des:= o_type ||' '|| o_des ;
--
-- end if;
elsif cur_contract_act.c_fw_order_id > 0 then
-- Hop dong dat mua
select contractno
into o_des
from c_fw_order
where c_fw_order_id = cur_contract_act.c_fw_order_id;

o_des:= o_type ||' '|| o_des ;

elsif cur_contract_act.c_productrightheader_id > 0 then


-- Quyen co phieu
select mp.name || ' ' || cp.documentno
into o_des
from m_product mp, c_productrightheader cp
where mp.m_product_id = cp.m_product_id
and cp.c_productrightheader_id
=cur_contract_act.c_productrightheader_id;

o_des:= o_type ||' '|| o_des ;


elsif cur_contract_act.s_offering_id > 0 then
-- Dau gia chung khoan
select so.documentno || ' ' || lower(mpc.name) || ' ' || mp.name
into o_des
from m_product mp, s_offering so, m_product_category mpc
where so.m_product_id = mp.m_product_id
and mpc.m_product_category_id = mp.m_product_category_id
and so.s_offering_id = cur_contract_act.s_offering_id;

o_des:= o_type ||' '|| o_des ;


elsif cur_contract_act.s_offering_trx_id > 0 then
-- Thuc hien dau gia
select so.documentno || ' ' || lower(mpc.name) || ' ' || mp.name
into o_des
from m_product mp, s_offering_trx so, m_product_category mpc
where so.m_product_id = mp.m_product_id
and mpc.m_product_category_id = mp.m_product_category_id
and so.s_offering_trx_id = cur_contract_act.s_offering_trx_id;

o_des:= o_type ||' '|| o_des ;


elsif cur_contract_act.s_etf_dealslip_header_id > 0 then
-- ETF
select so.documentno
into o_des
from s_etf_dealslip_header so
where so.s_etf_dealslip_header_id =
cur_contract_act.s_etf_dealslip_header_id;

o_des:= o_type ||' '|| o_des ;


end if;
return o_des;
exception
when others then
o_des := '';
return o_des;
end;

-- C_Payment
function get_payment_des (i_payment_id in number)
return varchar2
as
o_des varchar2(1000);
begin
select 'Xóa nợ hợp đồng '||co.documentno||' ' || cb.name
into o_des
from c_order co, c_bpartner cb, c_payment cp
where cp.c_payment_id = i_payment_id
and cp.c_order_id = co.c_order_id
and cp.c_bpartner_id = cb.c_bpartner_id;
return o_des;
exception
when others then
o_des := '';
return o_des;
end;

-- C_Payment_DTL
function get_payment_dtl_des (i_payment_dtl_id in number)
return varchar2
as
o_des varchar2(1000);
begin
select 'Xóa nợ hợp đồng '||co.documentno||' hóa đơn '||ci.documentno|| ' '
|| cb.name
into o_des
from c_payment_dtl cpl, c_invoice ci, c_order co, c_bpartner cb, c_payment
cp
where cpl.c_payment_id = cp.c_payment_id
and cpl.c_invoice_id = ci.c_invoice_id
and co.c_order_id = ci.c_order_id
and cp.c_bpartner_id = cb.c_bpartner_id
and cpl.c_payment_dtl_id = i_payment_dtl_id;
return o_des;
exception
when others then
o_des := '';
return o_des;
end;

end ssi_description;
/

You might also like