You are on page 1of 36

CREATE OR REPLACE package body DGDEV.

ssi_product_right_otc
as
/******************************************************************************
NAME: SSI_PRODUCT_RIGHT_OTC
PURPOSE:

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

procedure insertinout_swap( i_rightheader_id in number,


i_rightline_id in number,
i_product_id in number,
i_qty in number,
i_price in number,
i_ad_user_id in number,
i_doctype_id in number,
v_outid out varchar2,
v_outmsg out varchar2)
as

cursor c_qtysub_team_org(p_product_id number,


p_team_id number,
p_sub_id number,
p_org_id number
) is
SELECT SUM (qtyonhand) qtyonhand, mlc.m_locator_id
FROM m_locator MLC
JOIN m_storageonhand MSR ON (MSR.m_locator_id =
MLC.m_locator_id)
JOIN m_product MPR ON (MPR.m_product_id = MSR.m_product_id)
JOIN m_warehouse WH ON (WH.m_warehouse_id =
MLC.m_warehouse_id)
WHERE MPR.m_product_id = p_product_id
AND WH.c_salesregion_id = p_team_id
AND MSR.s_subaccount_id = p_sub_id
AND WH.ad_org_id = p_org_id
having SUM (qtyonhand)>0
group by mlc.m_locator_id;

v_inoutid number;
v_inoutlineid number;
v_locator_id number;
v_documentno varchar2(1000);
v_result_docno pls_integer;
v_msg_docno varchar2(100);
v_value_org varchar2(100);
v_value_doctype varchar2(100);
v_teamid number;
v_warehouse_id number;
v_subid number;
v_orgid number;
v_datetrx date;
v_qty number;
v_amount number;
v_bpartner_location number;
v_movementtype char(2 byte);
v_currentcost number;
v_totalloss number; -- tong lo
v_totalprofit number; -- tong lai
v_profitloss number; -- lo
cur_right c_productrightheader%rowtype;
cur_rightline c_productrightline%rowtype;
cur_product m_product%rowtype;
cur_product_cate m_product_category%rowtype;
l_description varchar2(500);
l_descriptionline varchar2(500);
o_inoutid varchar2(100);
begin
select * into cur_right
from c_productrightheader
where c_productrightheader_id = i_rightheader_id;

select * into cur_rightline


from c_productrightline
where c_productrightline_id = i_rightline_id;

select mpc.* into cur_product_cate


from m_product_category mpc, m_product mp
where mp.m_product_id = cur_right.m_product_id
and mpc.m_product_category_id = mp.m_product_category_id;

select c_bpartner_location_id
into v_bpartner_location
from c_bpartner_location
where rownum=1
and c_bpartner_id = c_bpartner_vsd;

select * into cur_product


from m_product
where m_product_id = i_product_id;

v_inoutid := m_inout_sq.nextval;
o_inoutid := v_inoutid;
v_datetrx := cur_rightline.datetrx;
v_teamid := cur_rightline.c_salesregion_id;
v_orgid := cur_rightline.ad_org_id;
v_subid := cur_rightline.s_subaccount_id;
xxff_moving_inventory_pkg.get_warehouse_id(
v_teamid ,
v_orgid ,
v_warehouse_id );

v_movementtype := 'C-';
v_totalloss := 0;
v_totalprofit := 0;
-- Tao header
insert into m_inout(
m_inout_id , --1
ad_client_id , --2
ad_org_id , --3
isactive , --4
created , --5
createdby , --6
updated , --7
updatedby , --8
issotrx , --9
documentno , --10
docaction , --11
docstatus ,--12
posted ,--13
processed ,--14
c_doctype_id ,--15
description ,--16
movementtype ,--17
movementdate ,--18
dateacct ,--19
c_bpartner_id ,--20
c_bpartner_location_id ,--21
m_warehouse_id ,--22
ad_user_id ,--23
c_salesregion_id ,--24
exchange_rate ,--25
exchange_rate_type ,--26
exchange_date ,--27
s_subaccount_id ,--28
listed ,--29
self_trading_type ,--30
deliveryrule ,
deliveryviarule ,
freightcostrule ,
priorityrule ,
c_productrightline_id,
m_product_category_id,
asset_type
)values(
v_inoutid ,--1
c_ad_client_id ,--2
v_orgid ,--3
'Y' ,--4
sysdate ,--5
i_ad_user_id ,--6
sysdate ,--7
i_ad_user_id ,--8
'Y' ,--9
to_char(v_inoutid) ,--10
'CO' ,--11
'DR' ,--12
'N' ,--13
'N' ,--14
i_doctype_id ,--15
null ,--16
v_movementtype ,--17
v_datetrx ,--18
v_datetrx ,--19
c_bpartner_vsd ,--20
v_bpartner_location ,--21
v_warehouse_id ,--22
i_ad_user_id ,--23
v_teamid ,--24
0 ,--25
'U' ,--26
null ,--27
v_subid ,--28
cur_product_cate.listed ,--29
cur_product_cate.self_trading_type, --30
'A' ,--31
'P' ,--32
'I' ,--33
'5' , --34
i_rightline_id ,
cur_product.m_product_category_id,
cur_product.asset_type
);

begin
select COSTPRICE
into v_currentcost
from S_PRODUCT_COST
where M_product_id = i_product_id
and ad_org_id = v_orgid
and S_SUBACCOUNT_ID = v_subid
and C_Salesregion_id = v_teamid;
exception
when others then
v_currentcost := 0;
end;

for c_locator in c_qtysub_team_org(i_product_id,v_teamid,v_subid,v_orgid)


loop
v_locator_id := c_locator.m_locator_id;
v_qty := c_locator.qtyonhand;
v_inoutlineid := m_inoutline_sq.nextval;
-- insert line
insert into m_inoutline(
m_inoutline_id ,
ad_client_id ,
ad_org_id ,
isactive ,
created ,
createdby ,
updated ,
updatedby ,
line ,
description ,
m_inout_id ,
m_locator_id ,
m_product_id ,
movementqty ,
m_attributesetinstance_id ,
processed ,
qtyentered ,
lineamount ,
unitprice ,
asset_type ,
ifrs_asset_type ,
name ,
upc ,
c_salesregion_id ,
s_subaccount_id ,
m_product_category_id ,
c_uom_id ,
current_cost ,
differencecost
)values(
v_inoutlineid ,
c_ad_client_id ,
v_orgid ,
'Y' ,
sysdate ,
i_ad_user_id ,
sysdate ,
i_ad_user_id ,
10 ,
null ,
v_inoutid ,
v_locator_id ,
i_product_id ,
v_qty ,
0 ,
'N' ,
v_qty ,
round(nvl(v_qty * v_currentcost,0),0) ,
v_currentcost ,
cur_product.asset_type ,
cur_product.ifrs_asset_type,
cur_product.name ,
cur_product.upc ,
v_teamid ,
v_subid ,
cur_product.m_product_category_id,
100 ,
v_currentcost ,
round(nvl(v_qty * v_currentcost,0),0) -- differenceCost
);
-- Tinh lai lo
v_profitloss := round(nvl(v_qty * i_price,0),0) -
round((nvl(v_currentcost,0) * v_qty ),0);
if v_profitloss < 0 then
v_totalloss := v_totalloss + v_profitloss;
else
v_totalprofit := v_totalprofit + v_profitloss;
end if;
l_descriptionline := ssi_description.getdesinoutline(v_inoutlineid);
update m_inoutline set description = l_descriptionline,
profit_loss = round(v_profitloss,0)
where m_inoutline_id = v_inoutlineid;
commit;
end loop;

ssi_common_api_pkg.get_documentnodb(c_adtable_inout,v_inoutid,i_doctype_id
,c_sequence_Inout,

'N','N',v_documentno,v_result_docno,v_msg_docno);

l_description := ssi_description.getdesinout(v_inoutid);
update m_inout set description = l_description,
documentno = v_documentno,
total_profit = round(v_totalprofit,0),
total_loss = round(abs(v_totalloss),0)
where m_inout_id = v_inoutid;
commit;
v_outmsg := 0;
v_outid := o_inoutid;
exception
when others then
v_outid := 0;
v_outmsg := sqlcode||':'||substrb(sqlerrm, 1, 150);
xxff_logs_api.insert_trace_issues('Product_Right_OTC', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), 0);

end insertinout_swap;

procedure process_productright( i_datetrx in date,


i_ad_user_id in number,
v_out_inout_ID out varchar2,
v_out_rightCash OUT varchar2,
v_out_paymentid OUT varchar2,
v_outmsg out varchar2)
as
-- cursor product right header
cursor c_righth(p_datetrx date) is
select c_productrightheader_id
from c_productrightheader cpr, m_product mp,
m_product_category mpc
where ( ex_date = p_datetrx
or record_date = p_datetrx
or last_registration_date = p_datetrx
or expected_exec_date = p_datetrx
or ex_swaps_date = p_datetrx)
and is_process = 'N'
and mp.m_product_id = cpr.m_product_id
and mpc.m_product_category_id = mp.m_product_category_id
and mpc.listed = 'CNY'
and (c_productright_type in ('01','02','03','04','05','06','07')
and docstatus = 'CO')
order by cpr.m_product_id asc;
-- cursor onhand Team, SubAccount, AD_Org_ID
cursor c_qtysub_team_org(p_product_id number) is
select sum (movementqty) qtyonhand,
wh.c_salesregion_id,
mlc.ad_org_id,
msr.s_subaccount_id
from m_locator mlc
join m_transaction msr on (msr.m_locator_id = mlc.m_locator_id)
join m_product mpr on (mpr.m_product_id = msr.m_product_id)
join m_warehouse wh on (wh.m_warehouse_id = mlc.m_warehouse_id)
where mpr.m_product_id = p_product_id
and msr.movementdate <= i_datetrx
having sum (movementqty)>0
group by wh.c_salesregion_id, mlc.ad_org_id, msr.s_subaccount_id ;

cur_right c_productrightheader%rowtype;
cur_line c_productrightline%rowtype;
cur_product m_product%rowtype;
v_raito varchar2(100);
v_cashratio number;
v_team number;
v_sub number;
v_org number;
v_qtyonhand number;
v_qtyonhandall number;
v_qtyonhand_swap number; -- sl hoan doi cpB
v_qtyonhand_swap_all number; -- sl hoan doi cpA
v_qtyrate number;
v_qty_trx number;
v_qty_odd number;
v_qty_odd_cash number;
v_sumqty number;
v_ratio1 number;
v_ratio2 number;
v_amount number;
v_amountodd number;
v_amountpaymentodd number;
v_docstatus varchar2(2) := 'DR';
v_processed char(1) := 'N';
v_idline number;
v_docno varchar2(1000);
v_result_docno pls_integer;
v_msg_docno varchar2(100);
v_cost number;
v_unitprice number;
v_desline varchar(500);
v_id_payment number;
v_qtyinout number;
v_inout_Out varchar(100);
v_count number;
v_date_trx date;
l_datechar varchar2(500);

begin
-- select TO_TIMESTAMP(i_datetrx,'YYYY-MM-DD HH24:MI:SS')
-- into v_date_trx
---- from dual;
-- l_datechar := '2021/10/19 ' || to_char(sysdate,'hh24:mi:ss');
-- select to_date('2021/10/19 ' || to_char(sysdate,'hh24:mi:ss'),'yyyy/mm/dd
hh24:mi:ss')
-- into v_date_trx
-- from dual;
--
--
--
-- Dbms_output.put_line('v_date_trx123: '||to_date(l_datechar,'yyyy/mm/dd
hh24:mi:ss'));
-- Dbms_output.put_line('v_date_trx1233: '||
to_char(i_datetrx,'yyyy/mm/dd') );
for cs_right in c_righth(trunc(i_datetrx)) loop
v_amountodd := 0;
v_sumqty := 0;
v_amountpaymentodd := 0;
v_qty_odd := 0;
v_qty_odd_cash :=0;
v_docstatus := 'DR';
v_processed := 'N';
v_qtyonhand_swap := 0;

-- Check void pending


select count(*)
into v_count
from r_request
where ad_table_id = c_adtable_header
and record_id = cs_right.c_productrightheader_id
and docstatus in ('DR','IP');
if v_count > 0 then
continue;
end if;

-- Product Right Header


select * into cur_right
from c_productrightheader
where c_productrightheader_id = cs_right.c_productrightheader_id;

-- ONHAND ALL
select sum(movementqty)
into v_qtyonhandall
from m_transaction
where m_product_id = cur_right.m_product_id
and movementdate <= i_datetrx;

-- SAN PHAM
select * into cur_product
from m_product
where m_product_id = cur_right.m_product_id;

-- Ty le Quyen
if cur_right.c_productright_type = c_productbuy_type
or cur_right.c_productright_type = c_productbond_type then
-- QUYEN MUA CP - TP
v_raito:= cur_right.convert_ratio;
v_docstatus := 'DR';
v_processed := 'N';
elsif cur_right.c_productright_type = c_productmerge_type
or cur_right.c_productright_type = c_productsplit_type then
-- QUYEN GOP - TACH
v_raito:= cur_right.split_ratio;
v_docstatus := 'CO';
v_processed := 'Y';
elsif cur_right.c_productright_type = c_productmoney_type then
-- QUYEN CO TUC BANG TIEN
v_cashratio:= cur_right.cash_percent;
v_out_rightCash := v_out_rightCash||
cur_right.c_productrightheader_id||',';
-- v_DocStatus := 'CO';
-- v_processed := 'Y';
elsif cur_right.c_productright_type = c_productstock_type then
-- QUYEN CO TUC BANG CO PHIEU
v_raito:= cur_right.stock_ratio;
v_docstatus := 'CO';
v_processed := 'Y';
elsif cur_right.c_productright_type = c_productswap_type then
-- QUYEN HOAN DOI
v_raito:= cur_right.swaps_ratio;
v_docstatus := 'CO';
v_processed := 'Y';
-- ONHAND ALL SWAP
select sum(movementqty)
into v_qtyonhand_swap_all
from m_transaction
where m_product_id = cur_right.m_product_id;

update c_productrightheader
set swaps_qty = v_qtyonhand_swap_all
where c_productrightheader_id = cs_right.c_productrightheader_id;
commit;

if v_qtyonhand_swap_all is null or v_qtyonhand_swap_all = 0 then


continue;
end if;
end if;

-- TAO LINE THEO TEAM TIEU KHOAN TRONG TON KHO


for cur_sub in c_qtysub_team_org(cur_right.m_product_id) loop
v_team := cur_sub.c_salesregion_id;
v_sub := cur_sub.s_subaccount_id;
v_org := cur_sub.ad_org_id;
v_qtyonhand := cur_sub.qtyonhand;
v_amount := 0;
v_unitprice := 0;
v_cost := 0;
-- v_qty_Odd := 0;
-- raito
v_ratio1 := regexp_substr( v_raito ,'[^ : ]+',1,1);
v_ratio2 := regexp_substr( v_raito ,'[^ : ]+',1,2);

if cur_right.c_productright_type = c_productswap_type then


-- gia von
begin
select nvl(costprice,0)
into v_cost
from s_product_cost
where m_product_id = cur_right.m_product_id
and ad_org_id = v_org
and c_salesregion_id = v_team
and s_subaccount_id = v_sub;
exception
when others then
v_cost := 0;
end;
-- raito
v_ratio1 := regexp_substr( v_raito ,'[^ : ]+',1,1);
v_ratio2 := regexp_substr( v_raito ,'[^ : ]+',1,2);
if cur_right.rounding_type = c_rounding then
v_qtyonhandall := round(v_qtyonhandall,0);
v_qtyonhand_swap := round(v_qtyonhand_swap,0);
v_qtyrate := round(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := round((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);

elsif cur_right.rounding_type = c_not_rounding then


v_qtyonhandall := trunc(v_qtyonhandall);
v_qtyonhand_swap := trunc(v_qtyonhand_swap);
v_qtyrate := trunc(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
else
v_qtyrate := (v_qtyonhandall / v_ratio1 * v_ratio2);
v_qty_odd_cash := v_qtyrate - trunc(v_qtyrate);
v_qtyrate := trunc(v_qtyrate);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
end if;
v_amount := v_cost * cur_right.swaps_qty;
v_unitprice := v_amount / v_qtyrate;
v_amount := v_unitprice * v_qty_trx;

elsif cur_right.c_productright_type in ( c_productbuy_type


,c_productbond_type) then
-- QUYEN MUA CP - TP
v_qtyonhandall := trunc(v_qtyonhandall);
v_qtyrate := trunc(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);

update c_productrightheader
set RightQty = v_qtyrate
where c_productrightheader_id =
cs_right.c_productrightheader_id;
commit;

elsif cur_right.c_productright_type = c_productmoney_type then


-- TIEN
v_qty_trx := v_qtyonhand * v_cashratio / 100;
v_amount := v_qty_trx * cur_product.coststandard;
v_amountodd := v_amountodd + (abs(v_amount) -
floor(abs(v_amount)));
v_amount := v_amount - (abs(v_amount) -
floor(abs(v_amount)));
v_qty_trx := 0;
else
-- QUYEN GOP - TACH - CON LAI
if cur_right.rounding_type = c_rounding then
v_qtyonhandall := round(v_qtyonhandall,0);
v_qtyrate := round(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := round((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);

elsif cur_right.rounding_type = c_not_rounding then


v_qtyonhandall := trunc(v_qtyonhandall);
v_qtyrate := trunc(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
else
v_qtyrate := (v_qtyonhandall / v_ratio1 * v_ratio2);
v_qty_odd_cash := v_qtyrate - trunc(v_qtyrate);
v_qtyrate := trunc(v_qtyrate);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
end if;

end if;

-- INSERT PRODUCTRIGHTLINE
v_sumqty := v_sumqty + v_qty_trx;
v_idline := c_productrightline_sq.nextval;

-- v_docno := 'DB'||
get_documentno(i_datetrx,c_sequence_rightline);
-- dbms_output.put_line('v_docno: '||v_docno );
insert into c_productrightline (
c_productrightline_id , --1
ad_user_id ,--2
ad_client_id ,--3
ad_org_id ,--4
isactive ,--5
created ,--6
createdby ,--7
updated ,--8
updatedby ,--9
docstatus ,--10
docaction ,--11
processed ,--12
right_quantity ,--13
right_unit_price ,--14
right_amount ,--15
c_productrightheader_id ,--16
description ,--17
documentno ,--18
datetrx ,--19
c_doctype_id ,--10
c_salesregion_id ,--21
s_subaccount_id ,--22
m_product_id ,--23
qtyonhand -- 24
) values (
v_idline ,--1
i_ad_user_id ,--2
c_ad_client_id ,--3
v_org ,--4
'Y' ,--5
sysdate ,--6
i_ad_user_id , -- createdby
sysdate , -- updatedby
i_ad_user_id ,--9
v_docstatus ,--10
'CO' ,--11
v_processed ,--12
v_qty_trx ,--13
v_unitprice ,--14
v_amount ,--15
cur_right.c_productrightheader_id ,--16
null ,--17
to_char(v_idline) , -- SO chung tu 18
i_datetrx + 1 ,--19
c_doctype_right , -- DocType
v_team ,
v_sub ,
cur_right.m_product_id ,
v_qtyonhand
);
-- update description

ssi_common_api_pkg.get_documentnodb(c_adtable_line,v_idline,c_DocType_Right,c_seque
nce_RightLine,

'N','N',v_docno,v_result_docno,v_msg_docno);
v_desline :=
ssi_description.get_productrightline_description(v_idline,cur_right.c_productright_
type);
update c_productrightline
set description = v_desline,
documentno = v_docno
where c_productrightline_id = v_idline;
commit;

end loop; -- END TEAM, SUB, ORG


-- UPDATE Quy tac lam tron quy tien mat
if cur_right.c_productright_type in ( c_productstock_type
,c_productmerge_type
, c_productsplit_type,c_productswap_type) then
-- so tien le
v_qty_odd := v_qtyrate - v_sumqty;
if (v_qty_odd_cash > 0 or v_qty_odd >0) and cur_right.rounding_type
= c_for_cash then
select *
into cur_line
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id
and right_quantity =
(select max (right_quantity)
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id)
order by c_productrightline_id
fetch first 1 rows only ;
-- UPDATE KHO NAM GIU NHIEU NHAT
v_qty_odd := v_qty_odd + v_qty_odd_cash;
v_amountpaymentodd := (abs(v_qty_odd) -
floor(abs(v_qty_odd))) * cur_product.coststandard;
v_qty_odd := v_qty_odd - (abs(v_qty_odd) -
floor(abs(v_qty_odd)));

-- update odd
update c_productrightline
set amount_change = round(v_amountpaymentodd,0),
right_quantity = right_quantity + v_qty_odd
where c_productrightline_id = cur_line.c_productrightline_id;
commit;
-- Phieu thu tien le
if v_amountpaymentodd>0 then
insertpayment(cur_right.c_productrightheader_id,
v_amountpaymentodd, i_ad_user_id,
cur_line.ad_org_id,i_datetrx ,v_id_payment, v_outmsg );
-- payment id out
v_out_paymentid := v_out_paymentid||v_id_payment||',';
end if;
-- update header
update c_productrightheader
set c_payment_id = v_id_payment
where c_productrightheader_id =
cur_right.c_productrightheader_id;
commit;
elsif v_qty_odd > 0 and cur_right.rounding_type
in( c_not_rounding,c_rounding )then
select *
into cur_line
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id
and right_quantity =
(select max (right_quantity)
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id)
order by c_productrightline_id
fetch first 1 rows only ;
-- update odd
if cur_right.rounding_type = c_rounding then
v_qty_odd := round(v_qty_odd,0);
else
v_qty_odd := v_qty_odd - (abs(v_qty_odd) -
floor(abs(v_qty_odd)));
end if;

update c_productrightline
set right_quantity = right_quantity + v_qty_odd
where c_productrightline_id = cur_line.c_productrightline_id;
commit;
end if;

end if;

-- INSERT PHIEU NHAP CHO QUYEN CT, GOP, TACH


if cur_right.c_productright_type in ( c_productstock_type ,
c_productmerge_type,c_productsplit_type,c_productswap_type) then
for cur_rightinout in (select * from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id) loop
dbms_output.put_line('ssss: '||
cur_rightinout.c_productrightline_id);
v_idline := cur_rightinout.c_productrightline_id;
if cur_right.c_productright_type = c_productstock_type then
v_qtyinout := cur_rightinout.right_quantity;
-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
cur_rightinout.c_productrightline_id,
cur_right.m_product_id,
v_qtyinout ,
cur_rightinout.right_unit_price,
i_ad_user_id,
c_doctype_receip,
'N' ,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';

elsif cur_right.c_productright_type = c_productsplit_type then


v_qtyinout := cur_rightinout.right_quantity
-cur_rightinout.qtyonhand ;
-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
cur_rightinout.c_productrightline_id,
cur_right.m_product_id,
v_qtyinout ,
cur_rightinout.right_unit_price,
i_ad_user_id,
c_doctype_receip,
'N' ,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';
elsif cur_right.c_productright_type = c_productmerge_type then
v_qtyinout := cur_rightinout.qtyonhand -
cur_rightinout.right_quantity;
-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
cur_rightinout.c_productrightline_id,
cur_right.m_product_id,
v_qtyinout ,
cur_rightinout.right_unit_price,
i_ad_user_id,
c_doctype_shipment,
'Y',
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';

elsif cur_right.c_productright_type = c_productswap_type then


-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
v_idline ,
cur_right.m_product2_id,
cur_rightinout.right_quantity ,
cur_rightinout.right_unit_price,
i_ad_user_id,
c_doctype_receip,
'N' ,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';
-- PHIEU Xuat
insertinout_swap(cur_right.c_productrightheader_id,
v_idline ,
cur_right.m_product_id,
v_qtyonhand ,
v_cost,
i_ad_user_id,
c_doctype_shipment,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';
end if;
end loop;
end if;

-- UPDATE TIEN LE QUYEN CO TUC BANG TIEN MAT


if cur_right.c_productright_type = c_productmoney_type then
--
-- so tien le
if v_amountodd >0 then
select *
into cur_line
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id
and right_amount =
(select max (right_amount)
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id)
order by c_productrightline_id
fetch first 1 rows only ;

-- update odd
update c_productrightline
set right_amount = right_amount + v_amountodd
where c_productrightline_id = cur_line.c_productrightline_id;
commit;
end if;
end if; -- END QUYEN TIEN MAT

-- update processed
update c_productrightheader
set is_process = 'Y'
where c_productrightheader_id = cur_right.c_productrightheader_id;
commit;
end loop;
v_out_inout_ID := v_out_inout_ID;
v_out_rightCash := v_out_rightCash;
v_out_paymentid := v_out_paymentid;
v_outmsg := 0;
exception
when others then
v_out_inout_ID := v_out_inout_ID||'null,';
v_out_rightCash := v_out_rightCash||'null,';
v_out_paymentid := v_out_paymentid||'null,';
v_outmsg := sqlcode||':'||substrb(sqlerrm, 1, 150);
xxff_logs_api.insert_trace_issues('Product_Right_Stock', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), 0);

end process_productright;

-- TAO PHIEU NHAP


procedure insertinout( i_rightheader_id in number,
i_rightline_id in number,
i_product_id in number,
i_qty in number,
i_price in number,
i_ad_user_id in number,
i_doctype_id in number,
i_issotrx in char,
v_outid out varchar2,
v_outmsg out varchar2)
as
v_inoutid number;
v_inoutlineid number;
v_locator_id number;
v_documentno varchar2(1000);
v_result_docno pls_integer;
v_msg_docno varchar2(100);
v_value_org varchar2(100);
v_value_doctype varchar2(100);
v_teamid number;
v_warehouse_id number;
v_subid number;
v_orgid number;
v_datetrx date;
v_qty number;
v_amount number;
v_bpartner_location number;
v_movementtype char(2 byte);
v_currentcost number;
v_assettype char(1 byte);
v_ifrs_assettype varchar2(100);
cur_right c_productrightheader%rowtype;
cur_rightline c_productrightline%rowtype;
cur_product m_product%rowtype;
cur_product_cate m_product_category%rowtype;
l_description varchar2(500);
l_descriptionline varchar2(500);
o_inoutid varchar2(100);
begin
select * into cur_right
from c_productrightheader
where c_productrightheader_id = i_rightheader_id;

select * into cur_rightline


from c_productrightline
where c_productrightline_id = i_rightline_id;

select mpc.* into cur_product_cate


from m_product_category mpc, m_product mp
where mp.m_product_id = cur_right.m_product_id
and mpc.m_product_category_id = mp.m_product_category_id;

select c_bpartner_location_id
into v_bpartner_location
from c_bpartner_location
where rownum=1
and c_bpartner_id = c_bpartner_vsd;

select * into cur_product


from m_product
where m_product_id = i_product_id;

v_inoutid := m_inout_sq.nextval;
o_inoutid := v_inoutid;
v_datetrx := cur_rightline.datetrx;
v_documentno := get_documentno(v_datetrx,c_sequence_inout);
v_teamid := cur_rightline.c_salesregion_id;
v_orgid := cur_rightline.ad_org_id;

if cur_right.c_productright_type = c_productswap_type then


if cur_product.asset_type is null then
select asset_type ,ifrs_asset_type
into v_assettype, v_ifrs_assettype
from m_product
where m_product_id = cur_right.m_product_id;
else
v_assettype := cur_product.asset_type;
v_ifrs_assettype := cur_product.ifrs_asset_type;
end if;
else
v_assettype := cur_product.asset_type;
v_ifrs_assettype := cur_product.ifrs_asset_type;
end if;

select ad.short_name into v_value_org


from ad_org ad
where ad.ad_org_id = v_orgid;
select cd.value into v_value_doctype
from c_doctype cd
where cd.c_doctype_id = c_doctype_receip;
-- v_documentno := 'DB/'||v_value_org||'/'||v_value_doctype||'/'||
v_documentno;

xxff_moving_inventory_pkg.get_warehouse_id(
v_teamid ,
v_orgid ,
v_warehouse_id );
v_subid := cur_rightline.s_subaccount_id;

if cur_right.date_authority_trx is not null and


cur_right.date_authority_trx > v_datetrx then
v_locator_id :=
get_locatorid(v_orgid,v_warehouse_id,i_ad_user_id,'LIMITED','NULL','NULL');
else
v_locator_id :=
get_locatorid(v_orgid,v_warehouse_id,i_ad_user_id,'DEFAULT','NULL','NULL');
end if;

if i_doctype_id = c_Doctype_shipment then


v_locator_id :=
get_locatorid(v_orgid,v_warehouse_id,i_ad_user_id,'DEFAULT','NULL','NULL');
end if;

if i_issotrx = 'Y' then


v_movementtype := 'C-';
else
v_movementtype := 'V+';
end if;

-- Tao header
insert into m_inout(
m_inout_id , --1
ad_client_id , --2
ad_org_id , --3
isactive , --4
created , --5
createdby , --6
updated , --7
updatedby , --8
issotrx , --9
documentno , --10
docaction , --11
docstatus ,--12
posted ,--13
processed ,--14
c_doctype_id ,--15
description ,--16
movementtype ,--17
movementdate ,--18
dateacct ,--19
c_bpartner_id ,--20
c_bpartner_location_id ,--21
m_warehouse_id ,--22
ad_user_id ,--23
c_salesregion_id ,--24
exchange_rate ,--25
exchange_rate_type ,--26
exchange_date ,--27
s_subaccount_id ,--28
listed ,--29
self_trading_type ,--30
deliveryrule ,
deliveryviarule ,
freightcostrule ,
priorityrule ,
c_productrightline_id,
m_product_category_id,
asset_type
)values(
v_inoutid ,--1
c_ad_client_id ,--2
v_orgid ,--3
'Y' ,--4
sysdate ,--5
i_ad_user_id ,--6
sysdate ,--7
i_ad_user_id ,--8
i_issotrx ,--9
to_char(v_inoutid) ,--10
'CO' ,--11
'DR' ,--12
'N' ,--13
'N' ,--14
i_doctype_id ,--15
null ,--16
v_movementtype ,--17
v_datetrx ,--18
v_datetrx ,--19
c_bpartner_vsd ,--20
v_bpartner_location ,--21
v_warehouse_id ,--22
i_ad_user_id ,--23
v_teamid ,--24
0 ,--25
'U' ,--26
null ,--27
v_subid ,--28
cur_product_cate.listed ,--29
cur_product_cate.self_trading_type, --30
'A' ,--31
'P' ,--32
'I' ,--33
'5' , --34
i_rightline_id ,
cur_product.m_product_category_id,
v_assettype
);

v_inoutlineid := m_inoutline_sq.nextval;
begin
select COSTPRICE
into v_currentcost
from S_PRODUCT_COST
where M_product_id = i_product_id
and ad_org_id = v_orgid
and S_SUBACCOUNT_ID = v_subid
and C_Salesregion_id = v_teamid;
exception
when others then
v_currentcost := 0;
end;

-- insert line
insert into m_inoutline(
m_inoutline_id ,
ad_client_id ,
ad_org_id ,
isactive ,
created ,
createdby ,
updated ,
updatedby ,
line ,
description ,
m_inout_id ,
m_locator_id ,
m_product_id ,
movementqty ,
m_attributesetinstance_id ,
processed ,
qtyentered ,
lineamount ,
unitprice ,
asset_type ,
ifrs_asset_type ,
name ,
upc ,
c_salesregion_id ,
s_subaccount_id ,
m_product_category_id ,
c_uom_id ,
current_cost ,
differenceCost
)values(
v_inoutlineid ,
c_ad_client_id ,
v_orgid ,
'Y' ,
sysdate ,
i_ad_user_id ,
sysdate ,
i_ad_user_id ,
10 ,
null ,
v_inoutid ,
v_locator_id ,
i_product_id ,
i_qty ,
0 ,
'N' ,
i_qty ,
round(nvl(i_qty * i_price,0),0 ) ,
i_price ,
v_assettype ,
v_ifrs_assettype ,
cur_product.name ,
cur_product.upc ,
v_teamid ,
v_subid ,
cur_product.m_product_category_id,
100 ,
v_currentcost ,
round(nvl(v_qty * v_currentcost,0),0) -- differenceCost
);
ssi_common_api_pkg.get_documentnodb(c_adtable_inout,v_inoutid,i_doctype_id
,c_sequence_Inout,

'N','N',v_documentno,v_result_docno,v_msg_docno);

l_description := ssi_description.getdesinout(v_inoutid);
update m_inout set description = l_description,
documentno = v_documentno
where m_inout_id = v_inoutid;
commit;
l_descriptionline := ssi_description.getdesinoutline(v_inoutlineid);
update m_inoutline set description = l_descriptionline
where m_inoutline_id = v_inoutlineid;
commit;
v_outmsg := 0;
v_outid := o_inoutid;
exception
when others then
v_outid := 0;
v_outmsg := sqlcode||':'||substrb(sqlerrm, 1, 150);
xxff_logs_api.insert_trace_issues('Product_Right_OTC', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), 0);

end insertinout;

-- TAO PHIEU THU


procedure insertpayment_team(i_rightheader_id in number,
i_ad_user_id in number,
i_datetrx in date,
o_payment_id out varchar2,
v_outmsg out varchar2)
as
v_documentno varchar(500);
v_amount number;
v_org number;
v_payment_id number;
v_product_category_id number;
v_bankaccount_id number;
v_bank_id number;
v_result_docno pls_integer;
v_msg_docno varchar2(100);
cur_right c_productrightheader%rowtype;
begin

select * into cur_right


from c_productrightheader
where c_productrightheader_id = i_rightheader_id;

select m_product_category_id
into v_product_category_id
from m_product
where m_product_id = cur_right.m_product_id;

for cur_org in (select ad_org_id from c_productrightline


where c_productrightheader_id = i_rightheader_id
group by ad_org_id) loop
v_org := cur_org.ad_org_id;

select max(c_bankaccount_id),c_bank_id
into v_bankaccount_id, v_bank_id
from c_bankaccount
where account_type = '01'
and ad_org_id = v_org
group by c_bank_id
fetch first 1 row only;

select sum(right_amount)
into v_amount
from c_productrightline
where ad_org_id = v_org
and docstatus != 'VO'
and c_productrightheader_id = i_rightheader_id;

-- v_documentno :=
ssi_common_api_pkg.get_documentno(xxff_global_api.c_acct_trx_seq_id, 'Y');
select c_payment_sq.nextval into v_payment_id from dual;

insert into c_payment(


c_payment_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
documentno,
datetrx,
c_doctype_id,
docstatus,
docaction,
m_product_id,
m_product_category_id,
c_bpartner_id,
c_currency_id,
c_bank_id,
payamt,
dateacct,
c_bankaccount_id,
c_productrightheader_id ,
tendertype ,
trading_type ,
module
)
values(
v_payment_id,
c_ad_client_id,
v_org,
'Y',
sysdate,
i_ad_user_id,
sysdate,
i_ad_user_id,
to_char(v_payment_id), -- So chung tu
i_datetrx, -- Ngay giao dich
c_ar_receipt, -- Phieu thu
'DR', -- Trang thai
'CO', -- Xu ly
cur_right.m_product_id, -- San pham
v_product_category_id,
c_bpartner_vsd, -- Doi tac
c_vnd, -- Loai tien
v_bank_id, -- Ngan hang
v_amount, -- So tien
i_datetrx,
v_bankaccount_id, -- bank account
cur_right.c_productrightheader_id,
'K' ,
trading_type_right, -- Quyen
module_trading
);
-- line
insert into c_payment_dtl(
c_payment_dtl_id ,
c_payment_id ,
ad_client_id ,
ad_org_id ,
isactive ,
created ,
createdby ,
updated ,
updatedby ,
description ,
payamtline ,
c_bpartner_id ,
c_currency_id ,
ad_table_id ,
record_id ,
trading_type
)values(
c_payment_dtl_sq.nextval,
v_payment_id ,
c_ad_client_id ,
v_org ,
'Y' ,
sysdate ,
i_ad_user_id ,
sysdate ,
i_ad_user_id ,
cur_right.description ,
round(v_amount,0) ,
cur_right.c_bpartner_id ,
c_vnd ,
c_adtable_header ,
i_rightheader_id ,
trading_type_right
);

ssi_common_api_pkg.get_documentnodb(c_adtable_payment,v_payment_id,c_ar_receipt
,c_sequence_payment,

'N','N',v_documentno,v_result_docno,v_msg_docno);
update c_payment
set documentno = v_documentno
where c_payment_id = v_payment_id;
commit;
o_payment_id := o_payment_id ||v_payment_id||',';
end loop;
o_payment_id := o_payment_id;
v_outmsg := 0;
exception
when others then
o_payment_id := o_payment_id||'null,';
v_outmsg := sqlcode||':'||substrb(sqlerrm, 1, 150);
xxff_logs_api.insert_trace_issues('Product_Right_Stock', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), 0);

end;

-- TAO PHIEU THU


procedure insertpayment(i_rightheader_id in number,
i_amount_id in number,
i_ad_user_id in number,
i_ad_org_id in number,
i_datetrx in date,
o_payment_id out number,
v_outmsg out varchar2)
as
v_documentno varchar(500);
v_payment_id number;
v_bankaccount_id number;
v_bank_id number;
v_product_category_id number;
v_result_docno pls_integer;
v_msg_docno varchar2(100);
cur_right c_productrightheader%rowtype;
begin

select * into cur_right


from c_productrightheader
where c_productrightheader_id = i_rightheader_id;

select m_product_category_id
into v_product_category_id
from m_product
where m_product_id = cur_right.m_product_id;

select max(c_bankaccount_id),c_bank_id
into v_bankaccount_id, v_bank_id
from c_bankaccount
where account_type = '01'
and ad_org_id = i_ad_org_id
group by c_bank_id
fetch first 1 row only;

select c_payment_sq.nextval into v_payment_id from dual;

insert into c_payment(


c_payment_id,
ad_client_id,
ad_org_id,
isactive,
created,
createdby,
updated,
updatedby,
documentno,
datetrx,
c_doctype_id,
docstatus,
docaction,
m_product_id,
m_product_category_id,
c_bpartner_id,
c_currency_id,
c_bank_id,
payamt,
dateacct,
c_bankaccount_id,
c_productrightheader_id ,
tendertype ,
trading_type ,
module
)
values(
v_payment_id,
c_ad_client_id,
i_ad_org_id,
'Y',
sysdate,
i_ad_user_id,
sysdate,
i_ad_user_id,
to_char(v_payment_id), -- So chung tu
i_datetrx, -- Ngay giao dich
c_ar_receipt, -- Phieu thu
'DR', -- Trang thai
'CO', -- Xu ly
cur_right.m_product_id, -- San pham
v_product_category_id,
c_bpartner_vsd, -- Doi tac
c_vnd, -- Loai tien
v_bank_id, -- Ngan hang
round(i_amount_id,0), -- So tien
i_datetrx,
v_bankaccount_id, -- Bank account
cur_right.c_productrightheader_id,
'K',
trading_type_cash_odd , -- tien le,
module_trading -- module
);

insert into c_payment_dtl(


c_payment_dtl_id ,
c_payment_id ,
ad_client_id ,
ad_org_id ,
isactive ,
created ,
createdby ,
updated ,
updatedby ,
description ,
payamtline ,
c_bpartner_id ,
c_currency_id ,
ad_table_id ,
record_id ,
trading_type
)values(
c_payment_dtl_sq.nextval,
v_payment_id ,
c_ad_client_id ,
i_ad_org_id ,
'Y' ,
sysdate ,
i_ad_user_id ,
sysdate ,
i_ad_user_id ,
cur_right.description ,
round(i_amount_id,0) ,
cur_right.c_bpartner_id ,
c_vnd ,
c_adtable_header ,
i_rightheader_id ,
trading_type_cash_odd
);

ssi_common_api_pkg.get_documentnodb(c_adtable_payment,v_payment_id,c_ar_receipt
,c_sequence_payment,
'N','N',v_documentno,v_result_docno,v_msg_docno);
update c_payment
set documentno = v_documentno
where c_payment_id = v_payment_id;
commit;
o_payment_id := v_payment_id;
v_outmsg := 0;
exception
when others then
o_payment_id := 0;
v_outmsg := sqlcode||':'||substrb(sqlerrm, 1, 150);
xxff_logs_api.insert_trace_issues('Product_Right_Stock', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), 0);

end;

-- LOCATOR
function get_locatorid( i_orgid in number,
i_warehouseid in number,
i_ad_user_id in number,
i_x in varchar2,
i_y in varchar2,
i_z in varchar2
) return number
as
v_locatorid number;
v_default char;
begin
v_locatorid := 0;
v_default := 'N';
begin
select m_locator_id
into v_locatorid
from m_locator
where isactive = 'Y'
and m_warehouse_id = i_warehouseid and x = i_x and y = i_y and z = i_z;
exception
when others then
v_locatorid := 0;
end;
if v_locatorid >0 then
return v_locatorid;
else
if i_x = 'DEFAULT' then
v_default := 'Y';
end if;

v_locatorid := m_locator_sq.nextval;
insert into m_locator(
m_locator_id ,
ad_client_id ,
ad_org_id ,
isactive ,
created ,
createdby ,
updated ,
updatedby ,
value ,
locator_type ,
m_warehouse_id ,
priorityno ,
isdefault ,
x ,
y ,
z
) values(
v_locatorid ,
c_ad_client_id ,
i_orgid ,
'Y' ,
sysdate ,
i_ad_user_id ,
sysdate ,
i_ad_user_id ,
initcap(i_x) ,
i_x ,
i_warehouseid ,
50 ,
v_default ,
i_x ,
i_y ,
i_z
);
return v_locatorid;
end if;
return v_locatorid;
end;

function get_documentno(
i_datetrx in date,
i_ad_sequence_id in number
) return varchar2
is
l_documentno number;
v_outdoc varchar2(50);
begin
select currentnext
into l_documentno
from ad_sequence
where ad_sequence_id = i_ad_sequence_id
for update of currentnext;
--
update ad_sequence
set currentnext = currentnext + incrementno
where ad_sequence_id = i_ad_sequence_id;
commit;

v_outdoc := to_char(i_datetrx,'YYYY')||'/'||lpad(l_documentno,6,0);
return v_outdoc;
exception
when others then
xxff_logs_api.insert_trace_issues('DOCUMENTNO', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), i_ad_sequence_id);
return null;
end;

-- NIEM YET
procedure process_right_listed( i_datetrx in date,
i_ad_user_id in number,
v_out_inout_ID OUT varchar2,
v_out_paymentid out varchar2,
v_outmsg out varchar2)
as
-- cursor product right header
cursor c_righth(p_datetrx date) is
select c_productrightheader_id
from c_productrightheader cpr, m_product mp,
m_product_category mpc
where ( ex_date = p_datetrx
or record_date = p_datetrx
or last_registration_date = p_datetrx
or expected_exec_date = p_datetrx
or ex_swaps_date = p_datetrx)
and is_process = 'N'
and mp.m_product_id = cpr.m_product_id
and mpc.m_product_category_id = mp.m_product_category_id
and mpc.listed = 'NY'
and (c_productright_type in ('02','03','05','06','07') and
docstatus = 'CO')
order by cpr.m_product_id asc;
-- cursor onhand Team, SubAccount, AD_Org_ID
cursor c_qtysub_team_org(p_product_id number) is
select sum (qtyonhand) qtyonhand,
wh.c_salesregion_id,
mlc.ad_org_id,
msr.s_subaccount_id
from m_locator mlc
join m_storageonhand msr on (msr.m_locator_id = mlc.m_locator_id)
join m_product mpr on (mpr.m_product_id = msr.m_product_id)
join m_warehouse wh on (wh.m_warehouse_id = mlc.m_warehouse_id)
where mpr.m_product_id = p_product_id
and msr.datematerialpolicy <= i_datetrx
group by wh.c_salesregion_id, mlc.ad_org_id, msr.s_subaccount_id ;

cur_right c_productrightheader%rowtype;
cur_line c_productrightline%rowtype;
cur_product m_product%rowtype;
v_raito varchar2(100);
v_cashratio number;
v_team number;
v_sub number;
v_org number;
v_qtyonhand number;
v_qtyonhandall number;
v_qtyonhand_swap number; -- sl hoan doi cpB
v_qtyonhand_swap_all number;
v_qtyrate number;
v_qty_trx number;
v_qty_odd number;
v_qty_odd_cash number;
v_sumqty number;
v_ratio1 number;
v_ratio2 number;
v_amount number;
v_amountodd number;
v_amountpaymentodd number;
v_docstatus varchar2(2) := 'DR';
v_processed char(1) := 'N';
v_idline number;
v_docno varchar2(1000);
v_result_docno pls_integer;
v_msg_docno varchar2(100);
v_cost number;
v_unitprice number;
v_desline varchar(500);
v_id_payment number;
v_qtyinout number;
v_inout_Out varchar2(100);
v_count number;

begin

for cs_right in c_righth(i_datetrx) loop


v_amountodd := 0;
v_sumqty := 0;
v_amountpaymentodd := 0;
v_qty_odd := 0;
v_qty_odd_cash :=0;
v_docstatus := 'DR';
v_processed := 'N';
v_qtyonhand_swap := 0;

-- Check void pending


select count(*)
into v_count
from r_request
where ad_table_id = c_adtable_header
and record_id = cs_right.c_productrightheader_id
and docstatus in ('DR','IP');
if v_count > 0 then
continue;
end if;

-- Product Right Header


select * into cur_right
from c_productrightheader
where c_productrightheader_id = cs_right.c_productrightheader_id;

-- ONHAND ALL
select sum(movementqty)
into v_qtyonhandall
from m_transaction
where m_product_id = cur_right.m_product_id
and movementdate <= i_datetrx;

-- SAN PHAM
select * into cur_product
from m_product
where m_product_id = cur_right.m_product_id;
-- Ty le Quyen
if cur_right.c_productright_type = c_productbuy_type
or cur_right.c_productright_type = c_productbond_type then
-- QUYEN MUA CP - TP
v_raito:= cur_right.convert_ratio;
v_docstatus := 'DR';
v_processed := 'N';
elsif cur_right.c_productright_type = c_productmerge_type
or cur_right.c_productright_type = c_productsplit_type then
-- QUYEN GOP - TACH
v_raito:= cur_right.split_ratio;
v_docstatus := 'CO';
v_processed := 'Y';
elsif cur_right.c_productright_type = c_productswap_type then
-- QUYEN HOAN DOI
v_raito:= cur_right.swaps_ratio;
v_docstatus := 'CO';
v_processed := 'Y';
-- ONHAND ALL SWAP
select sum(movementqty)
into v_qtyonhand_swap_all
from m_transaction
where m_product_id = cur_right.m_product_id;

update c_productrightheader
set swaps_qty = v_qtyonhand_swap_all
where c_productrightheader_id = cs_right.c_productrightheader_id;
commit;

if v_qtyonhand_swap_all is null or v_qtyonhand_swap_all = 0 then


continue;
end if;
end if;

-- TAO LINE THEO TEAM TIEU KHOAN TRONG TON KHO


for cur_sub in c_qtysub_team_org(cur_right.m_product_id) loop
v_team := cur_sub.c_salesregion_id;
v_sub := cur_sub.s_subaccount_id;
v_org := cur_sub.ad_org_id;
v_qtyonhand := cur_sub.qtyonhand;
v_amount := 0;
v_unitprice := 0;
v_cost := 0;
-- v_qty_Odd := 0;
-- raito
v_ratio1 := regexp_substr( v_raito ,'[^ : ]+',1,1);
v_ratio2 := regexp_substr( v_raito ,'[^ : ]+',1,2);

if cur_right.c_productright_type = c_productswap_type then


-- gia von
begin
select nvl(costprice,0)
into v_cost
from s_product_cost
where m_product_id = cur_right.m_product_id
and ad_org_id = v_org
and c_salesregion_id = v_team
and s_subaccount_id = v_sub;
exception
when others then
v_cost := 0;
end;
-- raito
v_ratio1 := regexp_substr( v_raito ,'[^ : ]+',1,1);
v_ratio2 := regexp_substr( v_raito ,'[^ : ]+',1,2);
if cur_right.rounding_type = c_rounding then
v_qtyonhandall := round(v_qtyonhandall,0);
v_qtyonhand_swap := round(v_qtyonhand_swap,0);
v_qtyrate := round(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := round((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);

elsif cur_right.rounding_type = c_not_rounding then


v_qtyonhandall := trunc(v_qtyonhandall);
v_qtyonhand_swap := trunc(v_qtyonhand_swap);
v_qtyrate := trunc(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
else
v_qtyrate := (v_qtyonhandall / v_ratio1 * v_ratio2);
v_qty_odd_cash := v_qtyrate - trunc(v_qtyrate);
v_qtyrate := trunc(v_qtyrate);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
end if;
v_amount := v_cost * cur_right.swaps_qty;
v_unitprice := v_amount / v_qtyrate;
v_amount := v_unitprice * v_qty_trx;
elsif cur_right.c_productright_type in ( c_productbuy_type
,c_productbond_type) then
-- QUYEN MUA CP - TP
v_qtyonhandall := trunc(v_qtyonhandall);
v_qtyrate := trunc(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);

update c_productrightheader
set RightQty = v_qtyonhandall
where c_productrightheader_id =
cs_right.c_productrightheader_id;
commit;

else
-- QUYEN GOP - TACH - CON LAI
if cur_right.rounding_type = c_rounding then
v_qtyonhandall := round(v_qtyonhandall,0);
v_qtyrate := round(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := round((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);

elsif cur_right.rounding_type = c_not_rounding then


v_qtyonhandall := trunc(v_qtyonhandall);
v_qtyrate := trunc(v_qtyonhandall/ v_ratio1 * v_ratio2);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
else
v_qtyrate := (v_qtyonhandall / v_ratio1 * v_ratio2);
v_qty_odd_cash := v_qtyrate - trunc(v_qtyrate);
v_qtyrate := trunc(v_qtyrate);
v_qty_trx := trunc((v_qtyrate * v_qtyonhand)/
v_qtyonhandall);
end if;

end if;

-- INSERT PRODUCTRIGHTLINE
v_sumqty := v_sumqty + v_qty_trx;
v_idline := c_productrightline_sq.nextval;

-- v_docno := 'DB'||
get_documentno(i_datetrx,c_sequence_rightline);
insert into c_productrightline (
c_productrightline_id , --1
ad_user_id ,--2
ad_client_id ,--3
ad_org_id ,--4
isactive ,--5
created ,--6
createdby ,--7
updated ,--8
updatedby ,--9
docstatus ,--10
docaction ,--11
processed ,--12
right_quantity ,--13
right_unit_price ,--14
right_amount ,--15
c_productrightheader_id ,--16
description ,--17
documentno ,--18
datetrx ,--19
c_doctype_id ,--10
c_salesregion_id ,--21
s_subaccount_id ,--22
m_product_id ,--23
qtyonhand -- 24
) values (
v_idline ,--1
i_ad_user_id ,--2
c_ad_client_id ,--3
v_org ,--4
'Y' ,--5
sysdate ,--6
i_ad_user_id , -- createdby
sysdate , -- updatedby
i_ad_user_id ,--9
v_docstatus ,--10
'CO' ,--11
v_processed ,--12
v_qty_trx ,--13
v_unitprice ,--14
v_amount ,--15
cur_right.c_productrightheader_id ,--16
null ,--17
to_char(v_idline) , -- SO chung tu 18
i_datetrx + 1 ,--19
c_doctype_right , -- DocType
v_team ,
v_sub ,
cur_right.m_product_id ,
v_qtyonhand
);
-- update description

ssi_common_api_pkg.get_documentnodb(c_adtable_line,v_idline,c_DocType_Right,c_seque
nce_RightLine,

'N','N',v_docno,v_result_docno,v_msg_docno);
v_desline :=
ssi_description.get_productrightline_description(v_idline,cur_right.c_productright_
type);
update c_productrightline
set description = v_desline,
documentno = v_docno
where c_productrightline_id = v_idline;
commit;

end loop; -- END TEAM, SUB, ORG


-- UPDATE Quy tac lam tron quy tien mat
if cur_right.c_productright_type in ( c_productmerge_type ,
c_productsplit_type,c_productswap_type) then
-- so tien le
v_qty_odd := v_qtyrate - v_sumqty;
if (v_qty_odd_cash > 0 or v_qty_odd >0) and cur_right.rounding_type
= c_for_cash then
select *
into cur_line
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id
and right_quantity =
(select max (right_quantity)
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id)
order by c_productrightline_id
fetch first 1 rows only ;
-- UPDATE KHO NAM GIU NHIEU NHAT
v_qty_odd := v_qty_odd + v_qty_odd_cash;
v_amountpaymentodd := (abs(v_qty_odd) -
floor(abs(v_qty_odd))) * cur_product.coststandard;
v_qty_odd := v_qty_odd - (abs(v_qty_odd) -
floor(abs(v_qty_odd)));

-- update odd
update c_productrightline
set amount_change = round(v_amountpaymentodd,0),
right_quantity = right_quantity + v_qty_odd
where c_productrightline_id = cur_line.c_productrightline_id;
commit;
-- Phieu thu tien le
if v_amountpaymentodd>0 then
insertpayment(cur_right.c_productrightheader_id,
v_amountpaymentodd, i_ad_user_id,
cur_line.ad_org_id,i_datetrx ,v_id_payment, v_outmsg );
v_out_paymentid := v_out_paymentid||v_id_payment||',';
end if;
-- update header
update c_productrightheader
set c_payment_id = v_id_payment
where c_productrightheader_id =
cur_right.c_productrightheader_id;
commit;
elsif v_qty_odd > 0 and cur_right.rounding_type
in( c_not_rounding,c_rounding )then
select *
into cur_line
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id
and right_quantity =
(select max (right_quantity)
from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id)
order by c_productrightline_id
fetch first 1 rows only ;
-- update odd
if cur_right.rounding_type = c_rounding then
v_qty_odd := round(v_qty_odd,0);
else
v_qty_odd := v_qty_odd - (abs(v_qty_odd) -
floor(abs(v_qty_odd)));
end if;

update c_productrightline
set right_quantity = right_quantity + v_qty_odd
where c_productrightline_id = cur_line.c_productrightline_id;
commit;
end if;

end if;

-- INSERT PHIEU NHAP CHO QUYEN CT, GOP, TACH


if cur_right.c_productright_type in
( c_productmerge_type,c_productsplit_type,c_productswap_type) then
for cur_rightinout in (select * from c_productrightline
where c_productrightheader_id =
cur_right.c_productrightheader_id) loop

if cur_right.c_productright_type = c_productsplit_type then


v_qtyinout := cur_rightinout.right_quantity
-cur_rightinout.qtyonhand ;
-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
cur_rightinout.c_productrightline_id,
cur_right.m_product_id,
v_qtyinout ,
cur_rightinout.right_unit_price,
i_ad_user_id,
c_doctype_receip,
'N' ,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';
elsif cur_right.c_productright_type = c_productmerge_type then
v_qtyinout := cur_rightinout.qtyonhand -
cur_rightinout.right_quantity;
-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
cur_rightinout.c_productrightline_id,
cur_right.m_product_id,
v_qtyinout ,
cur_rightinout.right_unit_price,
i_ad_user_id,
c_doctype_shipment,
'Y' ,
v_inout_Out,
v_outmsg );

v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';
elsif cur_right.c_productright_type = c_productswap_type then
-- PHIEU NHAP
insertinout(cur_right.c_productrightheader_id,
v_idline ,
cur_right.m_product2_id,
v_qty_trx ,
v_unitprice,
i_ad_user_id,
c_doctype_receip,
'N' ,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';

-- PHIEU Xuat
insertinout_swap(cur_right.c_productrightheader_id,
v_idline ,
cur_right.m_product_id,
v_qtyonhand ,
v_cost,
i_ad_user_id,
c_doctype_shipment,
v_inout_Out,
v_outmsg );
v_out_inout_ID := v_out_inout_ID||v_inout_Out||',';

end if;
end loop;
end if;
-- update processed
update c_productrightheader
set is_process = 'Y'
where c_productrightheader_id = cur_right.c_productrightheader_id;
commit;
end loop;
v_out_inout_ID := v_out_inout_ID;
v_out_paymentid := v_out_paymentid;
v_outmsg := 0;
exception
when others then
v_out_inout_ID := v_out_inout_ID||'null,';
v_out_paymentid := v_out_paymentid||'null,';
v_outmsg := sqlcode||':'||substrb(sqlerrm, 1, 150);
xxff_logs_api.insert_trace_issues('Product_Right_Listed', $$plsql_line,
dbms_utility.format_error_backtrace || ': ' || sqlcode||':'||substrb(sqlerrm, 1,
150), 0);

end process_right_listed;

end ssi_product_right_otc;
/

You might also like