$PBExportHeader$getid.srf
global type getid from function_object
end type

forward prototypes
global function string getid (long argscid, string argctg, date argdate, boolean arg_ifcommit, transaction arg_transaction)
end prototypes

global function string getid (long argscid, string argctg, date argdate, boolean arg_ifcommit, transaction arg_transaction);// 函数获取一个新的业务编号
String rslt,ls_oldfirststr,ls_datestyle ,ls_typecode,ls_firststr
Long cnt,id
DateTime d1,d2
Long ll_nmbr
String ls_sccode

argctg = Upper(Trim(argctg))
d1 = DateTime(argDate)
ls_oldfirststr = Right(argctg,2)
ls_sccode = Left(argctg,Len(argctg) - 2)

//取新单号
IF argctg = 'TXM' THEN
	ls_datestyle = 'yymmdd'
	ls_firststr = argctg
elseif argctg = 'P' then 
	ls_datestyle = 'pbarcode'
	ls_firststr = argctg
ELSE
	SELECT datestyle,firststr
		INTO :ls_datestyle,:ls_firststr
		FROM u_billcode
		WHERE oldfirststr = :ls_oldfirststr
		USING arg_transaction;
		
	IF arg_transaction.SQLCode <> 0 THEN
		ROLLBACK USING arg_transaction;
		rslt = "err"
		GOTO ext
	END IF
	
	IF Trim(ls_firststr) = '' THEN
		ls_firststr = ls_oldfirststr
	END IF
	ls_firststr = ls_sccode + ls_firststr
END IF
//

rslt = String(argDate,Trim(ls_datestyle))

CHOOSE CASE ls_datestyle
	CASE 'yymmdd'
		UPDATE  u_bsnno
			SET nmbr = nmbr + 1
			WHERE opDate = :d1
			AND typecode = :ls_firststr
			AND scid = :argscid
			AND status = 0 USING arg_transaction;
			
		IF arg_transaction.SQLCode <> 0 THEN
			ROLLBACK USING arg_transaction ;
			rslt = "err"
			GOTO ext
		END IF
		
		IF arg_transaction.SQLNRows = 0 THEN
			INSERT INTO u_bsnno(scid, opdate, typecode, nmbr, status)
				Values (:argscid, :d1, :ls_firststr, 1, 0) USING arg_transaction;
				
			IF arg_transaction.SQLCode <> 0 THEN
				ROLLBACK USING arg_transaction ;
				rslt = "err"
				GOTO ext
			END IF
		END IF
		
		SELECT nmbr
			INTO :ll_nmbr
			FROM  u_bsnno
			WHERE opDate = :d1
			AND typecode = :ls_firststr
			AND scid = :argscid
			AND status = 0 USING arg_transaction;
			
		IF argctg = 'TXM' THEN
			rslt = rslt + String(ll_nmbr,"00000")
		ELSE
			rslt = ls_firststr + rslt + String(ll_nmbr,"000")
		END IF
		
	CASE 'yymm'
		d2 = DateTime(Date(String(Year(argDate)) + '-' + String(Month(argDate)) + '-01'))
		
		UPDATE  u_bsnno SET nmbr = nmbr + 1 WHERE opDate = :d2 AND typecode = :ls_firststr
			AND scid = :argscid AND status = 1 USING arg_transaction;
			
		IF arg_transaction.SQLCode <> 0 THEN
			ROLLBACK USING arg_transaction ;
			rslt = "err"
			GOTO ext
		END IF
		
		IF arg_transaction.SQLNRows = 0 THEN
			INSERT INTO u_bsnno(scid, opdate, typecode, nmbr, status)
				Values (:argscid, :d2, :ls_firststr, 1, 1) USING arg_transaction;
				
			IF arg_transaction.SQLCode <> 0 THEN
				ROLLBACK USING arg_transaction ;
				rslt = "err"
				GOTO ext
			END IF
		END IF
		
		SELECT nmbr
			INTO :ll_nmbr
			FROM  u_bsnno
			WHERE opDate = :d2
			AND typecode = :ls_firststr
			AND scid = :argscid
			AND status = 1 USING arg_transaction;
			
		rslt = ls_firststr + rslt + String(ll_nmbr,"00000")
	CASE 'yy'
		d2 = DateTime(Date(String(Year(argDate)) + '-01-01'))
		
		UPDATE  u_bsnno SET nmbr = nmbr + 1 WHERE opDate = :d2 AND typecode = :ls_firststr
			AND scid = :argscid AND status = 2 USING arg_transaction;
			
		IF arg_transaction.SQLCode <> 0 THEN
			ROLLBACK USING arg_transaction ;
			rslt = "err"
			GOTO ext
		END IF
		
		IF arg_transaction.SQLNRows = 0 THEN
			INSERT INTO u_bsnno(scid, opdate, typecode, nmbr, status)
				Values (:argscid, :d2, :ls_firststr, 1, 2) USING arg_transaction;
				
			IF arg_transaction.SQLCode <> 0 THEN
				ROLLBACK USING arg_transaction ;
				rslt = "err"
				GOTO ext
			END IF
		END IF
		
		SELECT nmbr
			INTO :ll_nmbr FROM  u_bsnno
			WHERE opDate = :d2
			AND typecode = :ls_firststr
			AND scid = :argscid
			AND status = 2 USING arg_transaction;
			
		rslt = ls_firststr + rslt + String(ll_nmbr,"0000000")
	case 'pbarcode' 
		UPDATE  u_bsnno
			SET nmbr = nmbr + 1
			WHERE typecode = :ls_firststr
			AND scid = :argscid
			AND status = 0 USING arg_transaction;
			
		IF arg_transaction.SQLCode <> 0 THEN
			ROLLBACK USING arg_transaction ;
			rslt = "err"
			GOTO ext
		END IF
		
		IF arg_transaction.SQLNRows = 0 THEN
			INSERT INTO u_bsnno(scid, typecode, nmbr, status)
				Values (:argscid,  :ls_firststr, 1, 0) USING arg_transaction;
				
			IF arg_transaction.SQLCode <> 0 THEN
				ROLLBACK USING arg_transaction ;
				rslt = "err"
				GOTO ext
			END IF
		END IF
		
		SELECT nmbr
			INTO :ll_nmbr
			FROM  u_bsnno
			WHERE typecode = :ls_firststr
			AND scid = :argscid USING arg_transaction;
			
			rslt = ls_firststr + String(ll_nmbr,"000000000")
END CHOOSE

IF rslt = '' THEN rslt = 'err'


IF arg_ifcommit AND rslt <> "err" THEN
	COMMIT USING arg_transaction;
END IF

ext:
RETURN(rslt)










end function

