create or replace package body n_pk_doccontrol as function of_getnewdocno (avc_coopid varchar2, avc_doccode varchar2) return varchar2 as begin return of_getnewdocno (avc_coopid, avc_doccode, ''); end of_getnewdocno; function of_getnewdocno (avc_coopid varchar2, avc_doccode varchar2, avc_prefixcustom varchar2) return varchar2 as begin declare as_coopid varchar2(500); as_doccode varchar2(500); as_prefixcustom varchar2(500); ls_docprefix varchar2(500); ls_docformat varchar2(500); ls_docyear varchar2(500); ls_newdocno varchar2(500); ls_fillzero varchar2(500); ll_lastdocno number; li_doclength number; li_lenrunning number; li_lenlastdocno number; ln_return number; lvc_tempchar varchar2(100); begin as_coopid := avc_coopid; as_doccode := avc_doccode; as_prefixcustom := avc_prefixcustom; as_doccode := trim(as_doccode); ln_return := of_new(as_coopid, as_doccode, ll_lastdocno, li_doclength, ls_docprefix, ls_docformat, ls_docyear); -- ถ้าไม่มีการกำหนดรูปแบบเลขที่เอกสาร if ls_docformat is null or ls_docformat = '' then --ถ้าความยาวของเลขที่เอกสารไม่ได้กำหนดหรือกำหนดต่ำกว่า 0 if li_doclength <= 0 then li_doclength := 10; end if; --จำนวนหลักของ Running Number li_lenrunning := li_doclength - fpb.len(ls_docprefix); -- ความยาวของ Running number ล่าสุด lvc_tempchar := to_char(ll_lastdocno); li_lenlastdocno := fpb.len(lvc_tempchar); if li_lenlastdocno > li_lenrunning then ls_fillzero := ''; else ls_fillzero := fpb.fill('0', li_lenrunning - li_lenlastdocno); end if; ls_newdocno := ls_docprefix || ls_fillzero || to_char(ll_lastdocno); else -- Y ปีเอกสาร, R เลขที running, B สาขาที่ส่งเข้ามา, P คำนำหน้าที่กำหนดไว้ที่ doccontrol, C คำนำหน้าที่กำหนดเองโดยคนเรียก Srv declare li_count number; li_index number; li_num number; ls_value ntb_vc999; ls_char ntb_vc30; ls_tempt varchar(999); ls_tempv varchar(999); begin --ประกาศ array ls_char := ntb_vc30('Y', 'R', 'P', 'C', 'B'); --ประกาศ array ls_value := ntb_vc999(); ls_newdocno := trim(ls_docformat); ls_value.extend; ls_value(1) := trim(ls_docyear); ls_value.extend; ls_value(2) := to_char(ll_lastdocno); ls_value.extend; ls_value(3) := trim(ls_docprefix); ls_value.extend; ls_value(4) := trim(as_prefixcustom); ls_value.extend; ls_value(5) := trim(as_coopid); li_count := ls_char.last; -- upperbound(ls_char[]) for li_index in 1 .. li_count loop li_num := n_pk_string.of_countoccurrences(ls_docformat, ls_char(li_index)); if li_num = 0 then continue; end if; ls_tempt := fpb.fill( ls_char(li_index), li_num ); ls_tempv := fpb.right('0000000000' || ls_value(li_index), li_num); ls_newdocno := n_pk_string.of_globalreplace(ls_newdocno, ls_tempt, ls_tempv); end loop; end; end if; return ls_newdocno; end; end of_getnewdocno; function of_new(avc_coopid varchar2, avc_doccode varchar2, an_lastdocno in out number, an_doclength in out number, avc_docprefix in out varchar2, avc_docformat in out varchar2, avc_docyear in out varchar2) return number as begin declare ln_return number; begin ln_return := of_open( avc_coopid, avc_doccode, an_lastdocno, an_doclength, avc_docprefix, avc_docformat, avc_docyear, true); an_lastdocno := an_lastdocno + 1; ln_return := of_update(avc_coopid, avc_doccode, an_lastdocno); return ln_return; end; end of_new; function of_open(avc_coopid varchar2, avc_doccode varchar2, an_lastdocno in out number, an_doclength in out number, avc_docprefix in out varchar2, avc_docformat in out varchar2, avc_docyear in out varchar2) return number as begin return of_open(avc_coopid, avc_doccode, an_lastdocno, an_doclength, avc_docprefix, avc_docformat, avc_docyear, false); end of_open; function of_open(avc_coopid varchar2, avc_doccode varchar2, an_lastdocno in out number, an_doclength in out number, avc_docprefix in out varchar2, avc_docformat in out varchar2, avc_docyear in out varchar2, ab_forupdate boolean) return number as begin declare begin if avc_doccode is null or length(avc_doccode) = 0 then raise_application_error(-10, 'ไม่มีรหัสเอกสาร (เป็นค่าว่าง) กรุณาตรวจสอบ'); end if; if avc_coopid is null or avc_coopid = '' then raise_application_error(-10, 'ไม่มีรหัสสาขา (เป็นค่าว่าง) กรุณาตรวจสอบ'); end if; -- SELECT ข้อมูลพร้อมดัก exception begin if ab_forupdate then select last_documentno, document_length, document_prefix, document_format, document_year into an_lastdocno, an_doclength, avc_docprefix, avc_docformat, avc_docyear from cmdocumentcontrol where document_code = avc_doccode and coop_id = avc_coopid; --for update of last_documentno else select last_documentno, document_length, document_prefix, document_format, document_year into an_lastdocno, an_doclength, avc_docprefix, avc_docformat, avc_docyear from cmdocumentcontrol where document_code = avc_doccode and coop_id = avc_coopid; end if; exception when others then raise_application_error(-10, 'ไม่สามารถดึงเลขที่เอกสาร รหัส ' || avc_doccode || ' สาขา ' || avc_coopid || ' ได้ ' || sqlerrm); end; if avc_docprefix is null then avc_docprefix := ''; end if; if avc_docformat is null then avc_docformat := ''; end if; if an_lastdocno is null then an_lastdocno := 0; end if; if an_doclength is null then an_doclength := 0; end if; avc_docprefix := trim(avc_docprefix); avc_docformat := trim(avc_docformat); return 1; end; end of_open; function of_update(avc_coopid varchar2, avc_doccode varchar2, an_lastdocno in out number) return number as begin declare begin -- parameter check if avc_doccode is null or avc_doccode = '' then --ITHW_EXCEPTION.TEXT += "ไม่พบเลขที่เอกสารที่จะทำการปรับปรุงเลขที่ล่าสุดได้ " raise_application_error(-10, 'ไม่พบเลขที่เอกสารที่จะทำการปรับปรุงเลขที่ล่าสุดได้ '); end if; if avc_coopid is null or avc_coopid = '' then --ITHW_EXCEPTION.TEXT += "ไม่พบสาขาของเลขที่เอกสารที่จะทำการปรับปรุงเลขที่ล่าสุดได้" raise_application_error(-10, 'ไม่พบสาขาของเลขที่เอกสารที่จะทำการปรับปรุงเลขที่ล่าสุดได้'); end if; -- update ข้อมูลพร้อมดัก exception begin update cmdocumentcontrol set last_documentno = an_lastdocno where document_code = avc_doccode and coop_id = avc_coopid; exception when others then raise_application_error(-10, 'ไม่สามารถปรับปรุงเลขที่เอกสารล่าสุดได้' || sqlerrm ); end; return 1; end; end of_update; end n_pk_doccontrol;