--LAST UPDATE SCRIPT CONTAINING ONLY VIEWS THAT HAVE CHANGED SINCE PREVIOUS RELEASE

CREATE OR REPLACE FUNCTION V8_FNC_CCM_RATEVALUE(PERHOUR in integer, RATE in number, STARTTIME in date, ENDTIME in date)
return number
as
begin
if perhour = 0 then return rate;
else return (endtime - starttime) * 24 * rate;
end IF; 
end;
/


CREATE OR REPLACE FUNCTION V8_FNC_YEBS_YFSC_REP_INFOS
(
  YEVMID IN NUMBER,
  INCL_IN_YPKD in NUMBER,     -- 1 incl package 0  for not incl package,  3 for all
  FSOPTION in NUMBER          -- 0 original function space; 1 - alternatives  
) return varchar2 as
  ALL_YFSC varchar2(500);

  cursor YEBS_W_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
           yebs.YEBS_ALTERNATIVE,
           v8_fnc_ccm_alternatives('YEBS', yebs.YEBS_ID, 0, 1) Alternatives,
       yevm.YEVM_ALTERNATIVE
     FROM
       yebs,yfsc,yevm
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID
       and nvl(yebs.YEBS_PKGINCL,0)=1
           and yevm.yevm_id=yebs.yebs_yevm_id
       and yevm.YEVM_ID=YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YFSC.YFSC_LONGDESC;


  cursor YEBS_WO_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
           yebs.YEBS_ALTERNATIVE,
           v8_fnc_ccm_alternatives('YEBS', yebs.YEBS_ID, 0, 1) Alternatives,
       yevm.YEVM_ALTERNATIVE
     FROM
       yebs,yfsc,yevm
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID
       and nvl(yebs.YEBS_PKGINCL,0)=0
           and yevm.yevm_id=yebs.yebs_yevm_id
       and yevm.YEVM_ID=YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YFSC.YFSC_LONGDESC;


   cursor YEBS_ALL_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
           yebs.YEBS_ALTERNATIVE,
           v8_fnc_ccm_alternatives('YEBS', yebs.YEBS_ID, 0, 1) Alternatives,
       yevm.YEVM_ALTERNATIVE
     FROM
       yebs,yfsc,yevm
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID
       and nvl(yebs.YEBS_PKGINCL,0) in (0,1)
           and yevm.yevm_id=yebs.yebs_yevm_id
       and yevm.YEVM_ID=YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YFSC.YFSC_LONGDESC;

       
       
  YEBS_W_P_VALUE YEBS_W_P_CURSOR%rowtype;

  YEBS_WO_P_VALUE YEBS_WO_P_CURSOR%rowtype;
  
  YEBS_ALL_VALUE YEBS_ALL_CURSOR%rowtype;

begin
    ALL_YFSC := '';
    IF INCL_IN_YPKD=1 THEN
    begin
        open YEBS_W_P_CURSOR;
        loop
              fetch YEBS_W_P_CURSOR
            into YEBS_W_P_VALUE;
        exit when YEBS_W_P_CURSOR%notfound;
            
            if FSOPTION=0 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_W_P_VALUE.YFSC_LONGDESC;
            end if;    

            if (YEBS_W_P_VALUE.yevm_alternative=0) and (YEBS_W_P_VALUE.yebs_alternative=1) then
                if FSOPTION=0 then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                if YEBS_W_P_VALUE.alternatives IS NULL then
                    ALL_YFSC := ALL_YFSC||'(alternative)';
                else
                    ALL_YFSC := ALL_YFSC||'Alternative of'||' '||YEBS_W_P_VALUE.alternatives;
                end if;
            end if;

        end loop;
               exception
                when no_data_found then null;
                when others then null;
        close YEBS_W_P_CURSOR;
    end;
    END IF;

    IF INCL_IN_YPKD=0 THEN
    begin
        open YEBS_WO_P_CURSOR;
        loop
              fetch YEBS_WO_P_CURSOR
            into YEBS_WO_P_VALUE;
        exit when YEBS_WO_P_CURSOR%notfound;
            
            if FSOPTION=0 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_WO_P_VALUE.YFSC_LONGDESC;
            end if;
            
            if (YEBS_WO_P_VALUE.yevm_alternative=0) and (YEBS_WO_P_VALUE.yebs_alternative=1) then
                if FSOPTION=0 then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                if YEBS_WO_P_VALUE.alternatives IS NULL then
                    ALL_YFSC := ALL_YFSC||'(alternative)';
                else
                    ALL_YFSC := ALL_YFSC||'Alternative of'||' '||YEBS_WO_P_VALUE.alternatives;
                end if;
            end if;

        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_WO_P_CURSOR;
    end;
    END IF;
      
    IF INCL_IN_YPKD=3 THEN
    begin
        open YEBS_ALL_CURSOR;
        loop
              fetch YEBS_ALL_CURSOR
            into YEBS_ALL_VALUE;
        exit when YEBS_ALL_CURSOR%notfound;

            if FSOPTION=0 then
                if ALL_YFSC is not NULL then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                ALL_YFSC := ALL_YFSC||YEBS_ALL_VALUE.YFSC_LONGDESC;
            end if;


            if (YEBS_ALL_VALUE.yevm_alternative=0) and (YEBS_ALL_VALUE.yebs_alternative=1) then
                if FSOPTION=0 then
                    ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10);
                end if;
                if YEBS_ALL_VALUE.alternatives IS NULL then
                    ALL_YFSC := ALL_YFSC||'(alternative)';
                else
                    ALL_YFSC := ALL_YFSC||'Alternative of'||' '||YEBS_ALL_VALUE.alternatives;
                end if;
            end if;

        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_ALL_CURSOR;
    end;
    END IF;

     RETURN ALL_YFSC;

end V8_FNC_YEBS_YFSC_REP_INFOS;
/


CREATE OR REPLACE FUNCTION V8_FNC_YEBS_YSSC_REP_INFOS
(
  YEVMID IN NUMBER,
  INCL_IN_YPKD in NUMBER     -- 1 incl package 0  for not incl package,  3 for all
) return varchar2 as
  ALL_YFSC varchar2(200);

  cursor YEBS_W_P_CURSOR is
    SELECT
	   yebs.YEBS_ID,
	   yebs.YEBS_YEVM_ID,
	   yebs.YEBS_YFSC_ID,
	   yssc.YSSC_LONGDESC
	 FROM 
       YEBS 
          LEFT OUTER JOIN YSSL 
          ON YEBS.YEBS_YSSL_ID = YSSL.YSSL_ID 
          LEFT OUTER JOIN YSSC 
          ON YSSL.YSSL_YSSC_ID = YSSC.YSSC_ID, 
       YFSC
    WHERE 
       YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID
       AND NVL(YEBS.YEBS_PKGINCL,0)=1
       AND YEBS.YEBS_YEVM_ID = YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YEBS.YEBS_ID;


  cursor YEBS_WO_P_CURSOR is
     SELECT
	   yebs.YEBS_ID,
	   yebs.YEBS_YEVM_ID,
	   yebs.YEBS_YFSC_ID,
	   yssc.YSSC_LONGDESC
     FROM 
        YEBS 
           LEFT OUTER JOIN YSSL 
           ON YEBS.YEBS_YSSL_ID = YSSL.YSSL_ID 
           LEFT OUTER JOIN YSSC 
           ON YSSL.YSSL_YSSC_ID = YSSC.YSSC_ID, 
        YFSC
    WHERE 
        YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID
		AND NVL(YEBS.YEBS_PKGINCL,0)=1
		AND YEBS.YEBS_YEVM_ID = YEVMID
        ORDER BY YEBS.YEBS_DISPLORDER,YEBS.YEBS_ID;
	   
  cursor YEBS_ALL_CURSOR is
    SELECT
	   yebs.YEBS_ID,
	   yebs.YEBS_YEVM_ID,
	   yebs.YEBS_YFSC_ID,
	   yssc.YSSC_LONGDESC
     FROM 
        YEBS 
           LEFT OUTER JOIN YSSL 
           ON YEBS.YEBS_YSSL_ID = YSSL.YSSL_ID 
           LEFT OUTER JOIN YSSC 
           ON YSSL.YSSL_YSSC_ID = YSSC.YSSC_ID, 
        YFSC
	WHERE 
        YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID
		AND NVL(YEBS.YEBS_PKGINCL,0) IN (0,1)
		AND YEBS.YEBS_YEVM_ID = YEVMID
        ORDER BY YEBS.YEBS_DISPLORDER,YEBS.YEBS_ID;
	   


  YEBS_W_P_VALUE YEBS_W_P_CURSOR%rowtype;

  YEBS_WO_P_VALUE YEBS_WO_P_CURSOR%rowtype;
  
  YEBS_ALL_VALUE YEBS_ALL_CURSOR%rowtype;  

begin

  	 IF INCL_IN_YPKD=1 THEN
	 	begin
		open YEBS_W_P_CURSOR;
		loop
  			fetch YEBS_W_P_CURSOR
			into YEBS_W_P_VALUE;
		exit when YEBS_W_P_CURSOR%notfound;
			if ALL_YFSC is NULL then
			ALL_YFSC := YEBS_W_P_VALUE.YSSC_LONGDESC;
			else
			ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10)||YEBS_W_P_VALUE.YSSC_LONGDESC;
			end if;
		end loop;
	 	 	 exception
      	 	 when no_data_found then null;
      	 	 when others then null;
	 	close YEBS_W_P_CURSOR;
		end;
	  END IF;

  	 IF INCL_IN_YPKD=0 THEN
	 	begin
		open YEBS_WO_P_CURSOR;
		loop
  			fetch YEBS_WO_P_CURSOR
			into YEBS_WO_P_VALUE;
		exit when YEBS_WO_P_CURSOR%notfound;
			if ALL_YFSC is NULL then
			ALL_YFSC := YEBS_WO_P_VALUE.YSSC_LONGDESC;
			else
			ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10)||YEBS_WO_P_VALUE.YSSC_LONGDESC;
			end if;
		end loop;
	 	 	 exception
      	 	 when no_data_found then null;
      	 	 when others then null;
	 	close YEBS_WO_P_CURSOR;
		end;
	  END IF;
	  
	  IF INCL_IN_YPKD=3 THEN
	 	begin
		open YEBS_ALL_CURSOR;
		loop
  			fetch YEBS_ALL_CURSOR
			into YEBS_ALL_VALUE;
		exit when YEBS_ALL_CURSOR%notfound;
			if ALL_YFSC is NULL then
			ALL_YFSC := YEBS_ALL_VALUE.YSSC_LONGDESC;
			else
			ALL_YFSC := ALL_YFSC||CHR(13)||CHR(10)||YEBS_ALL_VALUE.YSSC_LONGDESC;
			end if;
		end loop;
	 	 	 exception
      	 	 when no_data_found then null;
      	 	 when others then null;
	 	close YEBS_ALL_CURSOR;
		end;
	  END IF;
	  

	 RETURN ALL_YFSC;

end V8_FNC_YEBS_YSSC_REP_INFOS;
/


CREATE OR REPLACE FUNCTION V8_FNC_YEBS_YFSR_REP_INFOS
(
 /* LS 02/12/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99 */
  YEVMID IN NUMBER,
  -- 1 incl package 0  for not incl package,  3 for all
  INCL_IN_YPKD in NUMBER, 
    /*   
     0  FCRate: Returns Function Space Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
     1  FCOrg_Rate: Returns Function Space Original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
     2  Rebate_%: Returns Rebate percentage.
     3  FCRebate_Flat: Returns Rebate flat amount.
     4  FCRate_Net: Returns Function Space Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description  
     5  FCRate_Vat: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
     6  FRate: Returns Function Space Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
     7  FOrg_Rate: Returns Function Space Original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
     8  FRebate_Flat: Returns Rebate flat amount;
     9  FRate_Net: Returns Function Space Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description
    10  FRate_Vat: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
    11  Rate: Returns Function Space Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
    12  Org_Rate: Returns Function Space Original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
    12  Rebate_Flat: Returns Rebate flat amount.
    14  Rate_Net: Returns Function Space Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description
    15  Rate_Vat: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
    16  FCRATE||' '||YEBS_WO_P_VALUE.RATE_DESC: Returns FCRATE + Rate Description.
     */    
  VALUEOPTION IN NUMBER         
  ) return varchar2 as
  ALL_YFSR varchar2(200);

  cursor YEBS_W_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE,       
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))) FORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) ORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'',V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE_VAT,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'', V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE_VAT,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'', V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE_VAT,
       ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE_DESC,
       yebs.yebs_rebate_perc,
       yebs.yebs_rebate_flat,
       V8_FNC_NUM_TO_CHAR_FORMATING(yebs.yebs_rebate_flat) Fyebs_rebate_flat,
       V8_FNC_VALCURR_FORMATING(yebs.yebs_rebate_flat, zcur.ZCUR_SHORTDESC) FCyebs_rebate_flat,
       yebs.YEBS_SPECIALRATE RATENOFORMAT
     FROM 
         YEBS ,YFSR,YFSC, YFRD, YEVM, YBOM, ZCUR
    WHERE 
       YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID
       AND YEBS.YEBS_YFSR_ID = YFSR.YFSR_ID(+)  
       AND YFSR.YFSR_YFRD_ID = YFRD.YFRD_ID(+)
       AND YEBS.YEBS_YEVM_ID = YEVM.YEVM_ID
       AND YEVM.YEVM_YBOM_ID = YBOM.YBOM_ID
       AND YBOM.YBOM_BASE_ZCUR_ID = ZCUR.ZCUR_ID
       AND NVL(YEBS.YEBS_PKGINCL,0)=1
       AND YEBS.YEBS_YEVM_ID = YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YEBS.YEBS_ID;



  cursor YEBS_WO_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE,       
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))) FORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) ORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'',V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE_VAT,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'',V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE_VAT,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'',V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE_VAT,
       ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE_DESC,
       yebs.yebs_rebate_perc,
       yebs.yebs_rebate_flat,
       V8_FNC_NUM_TO_CHAR_FORMATING(yebs.yebs_rebate_flat) Fyebs_rebate_flat,
       V8_FNC_VALCURR_FORMATING(yebs.yebs_rebate_flat, zcur.ZCUR_SHORTDESC) FCyebs_rebate_flat,
       yebs.YEBS_SPECIALRATE RATENOFORMAT
    FROM 
         YEBS ,YFSR,YFSC, YFRD, YEVM, YBOM, ZCUR
    WHERE 
       YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID
       AND YEBS.YEBS_YFSR_ID = YFSR.YFSR_ID(+)  
       AND YFSR.YFSR_YFRD_ID = YFRD.YFRD_ID(+)
       AND YEBS.YEBS_YEVM_ID = YEVM.YEVM_ID
       AND YEVM.YEVM_YBOM_ID = YBOM.YBOM_ID
       AND YBOM.YBOM_BASE_ZCUR_ID = ZCUR.ZCUR_ID
       AND NVL(YEBS.YEBS_PKGINCL,0)=0
       AND YEBS.YEBS_YEVM_ID = YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YEBS.YEBS_ID;



   cursor YEBS_ALL_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE,       
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))) FORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_ORG_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) ORG_RATE,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'Inklusive',decode(nvl(YEBS_SPECIALRATE,0),0,yfrd.YFRD_LONGDESC, V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE_NET,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'',V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime))))) RATE_VAT,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'', V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime)))))) FRATE_VAT,
       ltrim((decode(yebs.YEBS_PKGINCL,1,'',decode(nvl(YEBS_SPECIALRATE,0),0,'', V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))) FCRATE_VAT,
       ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE_DESC,
       yebs.yebs_rebate_perc,
       yebs.yebs_rebate_flat,
       V8_FNC_NUM_TO_CHAR_FORMATING(yebs.yebs_rebate_flat) Fyebs_rebate_flat,
       V8_FNC_VALCURR_FORMATING(yebs.yebs_rebate_flat, zcur.ZCUR_SHORTDESC) FCyebs_rebate_flat,
       yebs.YEBS_SPECIALRATE RATENOFORMAT
      FROM 
         YEBS ,YFSR,YFSC, YFRD, YEVM, YBOM, ZCUR
    WHERE 
       YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID
       AND YEBS.YEBS_YFSR_ID = YFSR.YFSR_ID(+)  
       AND YFSR.YFSR_YFRD_ID = YFRD.YFRD_ID(+)
       AND YEBS.YEBS_YEVM_ID = YEVM.YEVM_ID
       AND YEVM.YEVM_YBOM_ID = YBOM.YBOM_ID
       AND YBOM.YBOM_BASE_ZCUR_ID = ZCUR.ZCUR_ID
       AND NVL(YEBS.YEBS_PKGINCL,0) IN (0,1)
       AND YEBS.YEBS_YEVM_ID = YEVMID
       ORDER BY YEBS.YEBS_DISPLORDER,YEBS.YEBS_ID;     
       
          
  YEBS_W_P_VALUE YEBS_W_P_CURSOR%rowtype;

  YEBS_WO_P_VALUE YEBS_WO_P_CURSOR%rowtype;

  YEBS_ALL_VALUE YEBS_ALL_CURSOR%rowtype;

begin
  ALL_YFSR :=' ';
       IF INCL_IN_YPKD=1 THEN
         begin
        open YEBS_W_P_CURSOR;
        loop
              fetch YEBS_W_P_CURSOR
            into YEBS_W_P_VALUE;
        exit when YEBS_W_P_CURSOR%notfound;
            
            if ALL_YFSR<>' ' then 
              ALL_YFSR := ALL_YFSR||CHR(10);
            end if;
              
            if valueoption=0 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FCRATE;
            end if;
            if valueoption=1 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FCORG_RATE;
            end if;
            if valueoption=2 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.yebs_rebate_perc;
            end if;
            if valueoption=3 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FCyebs_rebate_flat;
            end if;
            if valueoption=4 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FCRATE_NET;
            end if;
            if valueoption=5 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FCRATE_VAT;
            end if;
            if valueoption=6 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FRATE;
            end if;
            if valueoption=7 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FORG_RATE;
            end if;
            if valueoption=8 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.Fyebs_rebate_flat;
            end if;
            if valueoption=9 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FRATE_NET;
            end if;
            if valueoption=10then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FRATE_VAT;
            end if;
            if valueoption=11 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.RATE;
            end if;
            if valueoption=12 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.ORG_RATE;
            end if;
            if valueoption=13 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.yebs_rebate_flat;
            end if;
            if valueoption=14 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.RATE_NET;
            end if;
            if valueoption=15 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.RATE_VAT;
            end if;
            if valueoption=16 then ALL_YFSR := ALL_YFSR||YEBS_W_P_VALUE.FCRATE||' '||YEBS_W_P_VALUE.RATE_DESC;
            end if;
            
            
            
            
            
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_W_P_CURSOR;
        end;
      END IF;

       IF INCL_IN_YPKD=0 THEN
         begin
        open YEBS_WO_P_CURSOR;
        loop
              fetch YEBS_WO_P_CURSOR
            into YEBS_WO_P_VALUE;
        exit when YEBS_WO_P_CURSOR%notfound;

            if ALL_YFSR<>' ' then 
              ALL_YFSR := ALL_YFSR||CHR(10);
            end if;
              
            if valueoption=0 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FCRATE;
            end if;
            if valueoption=1 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FCORG_RATE;
            end if;
            if valueoption=2 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.yebs_rebate_perc;
            end if;
            if valueoption=3 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FCyebs_rebate_flat;
            end if;
            if valueoption=4 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FCRATE_NET;
            end if;
            if valueoption=5 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FCRATE_VAT;
            end if;
            if valueoption=6 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FRATE;
            end if;
            if valueoption=7 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FORG_RATE;
            end if;
            if valueoption=8 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.Fyebs_rebate_flat;
            end if;
            if valueoption=9 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FRATE_NET;
            end if;
            if valueoption=10then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FRATE_VAT;
            end if;
            if valueoption=11 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.RATE;
            end if;
            if valueoption=12 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.ORG_RATE;
            end if;
            if valueoption=13 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.yebs_rebate_flat;
            end if;
            if valueoption=14 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.RATE_NET;
            end if;
            if valueoption=15 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.RATE_VAT;
            end if;
            if valueoption=16 then ALL_YFSR := ALL_YFSR||YEBS_WO_P_VALUE.FCRATE||' '||YEBS_WO_P_VALUE.RATE_DESC;
            end if;
           
        
    
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_WO_P_CURSOR;
        end;
      END IF;

        IF INCL_IN_YPKD=3 THEN
         begin
        open YEBS_ALL_CURSOR;
        loop
              fetch YEBS_ALL_CURSOR
            into YEBS_ALL_VALUE;
        exit when YEBS_ALL_CURSOR%notfound;
        
        
          if ALL_YFSR<>' ' then 
              ALL_YFSR := ALL_YFSR||CHR(10);
            end if;
              

            if valueoption=0 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FCRATE;
            end if;
            if valueoption=1 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FCORG_RATE;
            end if;
            if valueoption=2 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.yebs_rebate_perc;
            end if;
            if valueoption=3 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FCyebs_rebate_flat;
            end if;
            if valueoption=4 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FCRATE_NET;
            end if;
            if valueoption=5 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FCRATE_VAT;
            end if;
            if valueoption=6 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FRATE;
            end if;
            if valueoption=7 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FORG_RATE;
            end if;
            if valueoption=8 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.Fyebs_rebate_flat;
            end if;
            if valueoption=9 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FRATE_NET;
            end if;
            if valueoption=10then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FRATE_VAT;
            end if;
            if valueoption=11 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.RATE;
            end if;
            if valueoption=12 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.ORG_RATE;
            end if;
            if valueoption=13 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.yebs_rebate_flat;
            end if;
            if valueoption=14 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.RATE_NET;
            end if;
            if valueoption=15 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.RATE_VAT;
            end if;
            if valueoption=16 then ALL_YFSR := ALL_YFSR||YEBS_ALL_VALUE.FCRATE||' '||YEBS_ALL_VALUE.RATE_DESC;
            end if;
            
            

        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_ALL_CURSOR;
        end;
      END IF;
      

     RETURN ALL_YFSR;

end V8_FNC_YEBS_YFSR_REP_INFOS;
/


-- another way to get Function Space Rates

CREATE OR REPLACE FUNCTION V8_FNC_YEBS_YFSR_RD_REP_INFOS
(
  YEVMID IN NUMBER,
  INCL_IN_YPKD in NUMBER     -- 1 incl package 0  for not incl package,  3 for all
) return varchar2 as
  ALL_YFSR varchar2(200);

  cursor YEBS_W_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
          ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC)))  RATE
      FROM
       yebs,yfsc,yfsr,yfrd,yevm,ybom,zcur
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID(+)
       and yebs.YEBS_YFSR_ID=yfsr.YFSR_ID(+)
          and yfsr.YFSR_YFRD_ID=yfrd.YFRD_ID(+)
       and yebs.YEBS_YEVM_ID=yevm.YEVM_ID
       and yevm.YEVM_YBOM_ID=ybom.YBOM_ID
       and ybom.YBOM_BASE_ZCUR_ID=zcur.ZCUR_ID
       and nvl(yebs.YEBS_PKGINCL,0)=1
       and yebs.YEBS_YEVM_ID=YEVMID
       ORDER BY yebs.YEBS_ID;



  cursor YEBS_WO_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
          ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE
      FROM
       yebs,yfsc,yfsr,yfrd,yevm,ybom,zcur
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID(+)
       and yebs.YEBS_YFSR_ID=yfsr.YFSR_ID(+)
          and yfsr.YFSR_YFRD_ID=yfrd.YFRD_ID(+)
       and yebs.YEBS_YEVM_ID=yevm.YEVM_ID
       and yevm.YEVM_YBOM_ID=ybom.YBOM_ID
       and ybom.YBOM_BASE_ZCUR_ID=zcur.ZCUR_ID
       and nvl(yebs.YEBS_PKGINCL,0)=0
       and yebs.YEBS_YEVM_ID=YEVMID
       ORDER BY yebs.YEBS_ID;



   cursor YEBS_ALL_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
          ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE
      FROM
       yebs,yfsc,yfsr,yfrd,yevm,ybom,zcur
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID(+)
       and yebs.YEBS_YFSR_ID=yfsr.YFSR_ID(+)
       and yfsr.YFSR_YFRD_ID=yfrd.YFRD_ID(+)
       and yebs.YEBS_YEVM_ID=yevm.YEVM_ID
       and yevm.YEVM_YBOM_ID=ybom.YBOM_ID
       and ybom.YBOM_BASE_ZCUR_ID=zcur.ZCUR_ID
       and nvl(yebs.YEBS_PKGINCL,0) in (0,1)
       and yebs.YEBS_YEVM_ID=YEVMID
       ORDER BY yebs.YEBS_ID;


  YEBS_W_P_VALUE YEBS_W_P_CURSOR%rowtype;

  YEBS_WO_P_VALUE YEBS_WO_P_CURSOR%rowtype;

  YEBS_ALL_VALUE YEBS_ALL_CURSOR%rowtype;

begin

       IF INCL_IN_YPKD=1 THEN
         begin
        open YEBS_W_P_CURSOR;
        loop
              fetch YEBS_W_P_CURSOR
            into YEBS_W_P_VALUE;
        exit when YEBS_W_P_CURSOR%notfound;
            if ALL_YFSR is NULL then
            ALL_YFSR := YEBS_W_P_VALUE.RATE;
            else
            ALL_YFSR := ALL_YFSR||CHR(10)||YEBS_W_P_VALUE.RATE;
            end if;
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_W_P_CURSOR;
        end;
      END IF;

       IF INCL_IN_YPKD=0 THEN
         begin
        open YEBS_WO_P_CURSOR;
        loop
              fetch YEBS_WO_P_CURSOR
            into YEBS_WO_P_VALUE;
        exit when YEBS_WO_P_CURSOR%notfound;
            if ALL_YFSR is NULL then
            ALL_YFSR := YEBS_WO_P_VALUE.RATE;
            else
            ALL_YFSR := ALL_YFSR||CHR(10)||YEBS_WO_P_VALUE.RATE;
            end if;
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_WO_P_CURSOR;
        end;
      END IF;

        IF INCL_IN_YPKD=3 THEN
         begin
        open YEBS_ALL_CURSOR;
        loop
              fetch YEBS_ALL_CURSOR
            into YEBS_ALL_VALUE;
        exit when YEBS_ALL_CURSOR%notfound;
            if ALL_YFSR is NULL then
            ALL_YFSR := YEBS_ALL_VALUE.RATE;
            else
            ALL_YFSR := ALL_YFSR||CHR(10)||YEBS_ALL_VALUE.RATE;
            end if;
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_ALL_CURSOR;
        end;
      END IF;

     RETURN ALL_YFSR;

end V8_FNC_YEBS_YFSR_RD_REP_INFOS;
/


CREATE OR REPLACE FUNCTION V8_FNC_YEBS_YFSR_RD_REP_INFOS
(
  YEVMID IN NUMBER,
  INCL_IN_YPKD in NUMBER     -- 1 incl package 0  for not incl package,  3 for all
) return varchar2 as
  ALL_YFSR varchar2(200);

  cursor YEBS_W_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
          ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC)))  RATE
      FROM
       yebs,yfsc,yfsr,yfrd,yevm,ybom,zcur
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID(+)
       and yebs.YEBS_YFSR_ID=yfsr.YFSR_ID(+)
          and yfsr.YFSR_YFRD_ID=yfrd.YFRD_ID(+)
       and yebs.YEBS_YEVM_ID=yevm.YEVM_ID
       and yevm.YEVM_YBOM_ID=ybom.YBOM_ID
       and ybom.YBOM_BASE_ZCUR_ID=zcur.ZCUR_ID
       and nvl(yebs.YEBS_PKGINCL,0)=1
       and yebs.YEBS_YEVM_ID=YEVMID
       ORDER BY yebs.YEBS_ID;



  cursor YEBS_WO_P_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
          ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE
      FROM
       yebs,yfsc,yfsr,yfrd,yevm,ybom,zcur
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID(+)
       and yebs.YEBS_YFSR_ID=yfsr.YFSR_ID(+)
          and yfsr.YFSR_YFRD_ID=yfrd.YFRD_ID(+)
       and yebs.YEBS_YEVM_ID=yevm.YEVM_ID
       and yevm.YEVM_YBOM_ID=ybom.YBOM_ID
       and ybom.YBOM_BASE_ZCUR_ID=zcur.ZCUR_ID
       and nvl(yebs.YEBS_PKGINCL,0)=0
       and yebs.YEBS_YEVM_ID=YEVMID
       ORDER BY yebs.YEBS_ID;



   cursor YEBS_ALL_CURSOR is
     SELECT
       yebs.YEBS_ID,
       yebs.YEBS_YEVM_ID,
       yebs.YEBS_YFSC_ID,
       yfsc.YFSC_LONGDESC,
       yfsr.YFSR_VALUE,
          ltrim(decode(yebs.YEBS_PKGINCL,1,to_char(NULL),decode(nvl(YEBS_SPECIALRATE,0),0,to_char(NULL),yfrd.YFRD_LONGDESC))) RATE
      FROM
       yebs,yfsc,yfsr,yfrd,yevm,ybom,zcur
     WHERE
       yebs.YEBS_YFSC_ID=yfsc.YFSC_ID(+)
       and yebs.YEBS_YFSR_ID=yfsr.YFSR_ID(+)
       and yfsr.YFSR_YFRD_ID=yfrd.YFRD_ID(+)
       and yebs.YEBS_YEVM_ID=yevm.YEVM_ID
       and yevm.YEVM_YBOM_ID=ybom.YBOM_ID
       and ybom.YBOM_BASE_ZCUR_ID=zcur.ZCUR_ID
       and nvl(yebs.YEBS_PKGINCL,0) in (0,1)
       and yebs.YEBS_YEVM_ID=YEVMID
       ORDER BY yebs.YEBS_ID;


  YEBS_W_P_VALUE YEBS_W_P_CURSOR%rowtype;

  YEBS_WO_P_VALUE YEBS_WO_P_CURSOR%rowtype;

  YEBS_ALL_VALUE YEBS_ALL_CURSOR%rowtype;

begin

       IF INCL_IN_YPKD=1 THEN
         begin
        open YEBS_W_P_CURSOR;
        loop
              fetch YEBS_W_P_CURSOR
            into YEBS_W_P_VALUE;
        exit when YEBS_W_P_CURSOR%notfound;
            if ALL_YFSR is NULL then
            ALL_YFSR := YEBS_W_P_VALUE.RATE;
            else
            ALL_YFSR := ALL_YFSR||CHR(10)||YEBS_W_P_VALUE.RATE;
            end if;
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_W_P_CURSOR;
        end;
      END IF;

       IF INCL_IN_YPKD=0 THEN
         begin
        open YEBS_WO_P_CURSOR;
        loop
              fetch YEBS_WO_P_CURSOR
            into YEBS_WO_P_VALUE;
        exit when YEBS_WO_P_CURSOR%notfound;
            if ALL_YFSR is NULL then
            ALL_YFSR := YEBS_WO_P_VALUE.RATE;
            else
            ALL_YFSR := ALL_YFSR||CHR(10)||YEBS_WO_P_VALUE.RATE;
            end if;
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_WO_P_CURSOR;
        end;
      END IF;

        IF INCL_IN_YPKD=3 THEN
         begin
        open YEBS_ALL_CURSOR;
        loop
              fetch YEBS_ALL_CURSOR
            into YEBS_ALL_VALUE;
        exit when YEBS_ALL_CURSOR%notfound;
            if ALL_YFSR is NULL then
            ALL_YFSR := YEBS_ALL_VALUE.RATE;
            else
            ALL_YFSR := ALL_YFSR||CHR(10)||YEBS_ALL_VALUE.RATE;
            end if;
        end loop;
               exception
                when no_data_found then null;
                when others then null;
         close YEBS_ALL_CURSOR;
        end;
      END IF;

     RETURN ALL_YFSR;

end V8_FNC_YEBS_YFSR_RD_REP_INFOS;
/


CREATE OR REPLACE FUNCTION V8_FNC_YEBS_REP_INFOS_TR (
   YEVMID              IN NUMBER,
   /* LS 28/11/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99

   All output of function is translated according to provided wlanid, if translation available.

    1 YFSC_LONGDESC_TR: Returns Function Space Name
    2 YFSC_GENERIC_NAME_TR: Returns Function Space Generic Name
    3 FCRATE: Returns Function Space Rate. If FS included in package returns"Included in package". If no rate amount or amount is 0 returns ''.
    4 RATEDES3RIPTION: Returns Function Space Rate Description
    5 RATEDESCRIPTIONIFRATEAMOUNT: Returns Function Space Rate Description if rate is present. If no rate amount or amount is 0 return 0.
    6 FCRATE_DESCRIPTION: Returns Function Space Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
    7 SEATING: Returns the seating
    8 FCORG_RATE: Returns Function Space original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns ''.
    9 FCORG_RATE_DESCRIPTION: Returns Function Space original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
   10 YEBS_REBATE_PERC: Returns Rebate percentage. 
   11 FCYEBS_REBATE_FLAT: Returns Rebate flat amount 
   12 FCYEBS_REBATE: Returns rebate flat amount or percentage with % sign.
   13 FCYEBS_REBATE_TOTAL: Returns total amount of rebate (flat or % calculation). 
   14 FCYEBS_TOTAL_NOREBATE: Returns Function Space original Rate - Total rebates. In fact this is exaclty the same as 3.FCRATE with the exception that it will return
       0.00 Euro instead of ("Included in package" if FS is included, or '' if rate amount is 0). For me this one is obsolete. Left it as some installs might used it.
       I did not add a YEBS_TOTAL_NOREBATE nor FYEBS_TOTAL_NOREBATE for this field.
   15 FCYEBS_REBATE_TR: Returns Rebate flat amount + "incl"(translated) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' (translated) if rebate is %.
   16 FCYEBS_COSTPRICE: Returns Function Space Cost Price
   17
   18 RATE: Returns Function Space Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns ''.
   19 FRATE: Returns Function Space Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns ''.
   20 RATE_NET: Returns Function Space Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns ''.
   21 FRATE_NET: Returns Function Space Net Rate. If FS included in package returns"Included in package". If no rate amount or amount is 0 returns ''.
   22 FCRATE_NET: Returns Function Space Net Rate. If FS included in package returns"Included in package". If no rate amount or amount is 0 returns ''.
   23 RATE_VAT: Returns Function Space Rate VAT. If FS included in package returns '' . If no rate amount or amount is 0 returns ''.
   24 FRATE_VAT: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
   25 FCRATE_VAT: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
   26 RATE_DESCRIPTION: Returns Function Space Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description
   27 FRATE_DESCRIPTION: Returns Function Space Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description
   28 RATE_NET_DESCRIPTION: Returns Function Space Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description 
   29 FRATE_NET_DESCRIPTION: Returns Function Space Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description
   30 FCRATE_NET_DESCRIPTION: Returns Function Space  Net Rate. If FS included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description
   31 RATE_VAT_DESCRIPTION: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
   32 FRATE_VAT_DESCRIPTION: Returns Function Space Rate VAT. If FS included in package returns ''. If no rate amount or amount is 0 returns ''.
   33 FCRATE_VAT_DESCRIPTION: Returns Function Space Rate VAT. If FS included in package returns ''.If no rate amount or amount is 0 returns ''.  
   34 ORG_RATE : Returns Function Space original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns ''.  
   35 FORG_RATE : Returns Function Space original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns ''.
   36 ORG_RATE_DESCRIPTION: Returns Function Space original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description
   37 FORG_RATE_DESCRIPTION: Returns Function Space original Rate. If FS included in package return "Included in package". If no rate amount or amount is 0 returns Rate Description.
   38 YEBS_REBATE_FLAT: Returns Rebate flat amount 
   39 FYEBS_REBATE_FLAT: Returns Rebate flat amount 
   40 FYEBS_REBATE: Returns rebate flat amount or percentage with % sign.
   41 YEBS_REBATE_TOTAL : Returns total amount of rebate (flat or % calculation).
   42 FYEBS_REBATE_TOTAL : Returns total amount of rebate (flat or % calculation).
   43 YEBS_REBATE_TR: Returns Rebate flat amount + "incl"(translated) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' (translated) if rebate is %.
   44 FCYEBS_REBATE_TR: Returns Rebate flat amount + "incl"(translated) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' (translated) if rebate is %.   
   45 YEBS_COSTPRICE: Returns Function Space Cost Price.
   46 FYEBS_COSTPRICE: Returns Function Space Cost Price.
    */
   FIELDOPTIONNUMBER   IN NUMBER,
   WLANID              IN NUMBER)
   RETURN VARCHAR2
AS
   ALL_YFSR   VARCHAR2 (200);

   CURSOR YEBS_ALL_CURSOR
   IS
      SELECT YEBS.YEBS_ID,
             YEBS.YEBS_YEVM_ID,
             YEBS.YEBS_YFSC_ID,
             Wtrl_Translate ('YFSC',
                             YFSC.YFSC_ID,
                             'YFSC_LONGDESC',
                             WLANID)
                YFSC_LONGDESC_TR,
             Wtrl_Translate ('YFSC',
                             YFSC.YFSC_ID,
                             'YFSC_GENERIC_NAME',
                             WLANID)
                YFSC_GENERIC_NAME_TR,
             YFSR.YFSR_VALUE,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),0,'',V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))) RATE, 
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),0, '',V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) FRATE,             
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),0, '',V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime),zcur.ZCUR_SHORTDESC)))) FCRATE,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),0, '', V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime)))) RATE_NET,             
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),0, '',V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime))))) FRATE_NET,             
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),0, '',V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC)))) FCRATE_NET,
trim(decode(yebs.YEBS_PKGINCL,1,'',
      decode(nvl(YEBS_SPECIALRATE,0),0, '', V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime)))) RATE_VAT,             
trim(decode(yebs.YEBS_PKGINCL,1,'',
      decode(nvl(YEBS_SPECIALRATE,0),0, '',V8_FNC_NUM_TO_CHAR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime))))) FRATE_VAT,             
trim(decode(yebs.YEBS_PKGINCL,1, '',
      decode(nvl(YEBS_SPECIALRATE,0),0, '',V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC)))) FCRATE_VAT,    
trim(Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID) ) RATEDESCRIPTION,
trim(decode(nvl(YEBS_SPECIALRATE,0),0, '',Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID) )) RATEDESCRIPTIONIFRATEAMOUNT,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',        
          V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))RATE_DESCRIPTION,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_NUM_TO_CHAR_FORMATING(
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))))FRATE_DESCRIPTION,            
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_VALCURR_FORMATING(
          V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))FCRATE_DESCRIPTION,   
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
          V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime))))RATE_NET_DESCRIPTION,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_NUM_TO_CHAR_FORMATING(
          V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime)))))FRATE_NET_DESCRIPTION,            
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_VALCURR_FORMATING(
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_NET, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))FCRATE_NET_DESCRIPTION,            
trim(decode(yebs.YEBS_PKGINCL,
      1,'',
      decode(nvl(YEBS_SPECIALRATE,0),
        0,'',
          V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime))))RATE_VAT_DESCRIPTION,
trim(decode(yebs.YEBS_PKGINCL,
      1,'',
      decode(nvl(YEBS_SPECIALRATE,0),
        0,'',
        V8_FNC_NUM_TO_CHAR_FORMATING(
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime)))))FRATE_VAT_DESCRIPTION,            
trim(decode(yebs.YEBS_PKGINCL,
       1,'',
      decode(nvl(YEBS_SPECIALRATE,0),
        0,'',
        V8_FNC_VALCURR_FORMATING(
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_PRICE_VAT, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC))))FCRATE_VAT_DESCRIPTION,            
WTRL_Translate('YSSC', (select yssc_id from yssl, yssc where yssl_yssc_id=yssc_id and yssl_id=yebs_yssl_id and rownum=1), 'YSSC_LONGDESC', WLANID ) SEATING,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_ORG_SPECIALRATE,0),
        0, '',        
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))) ORG_RATE,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_ORG_SPECIALRATE,0),
        0, '',
        V8_FNC_NUM_TO_CHAR_FORMATING(
          V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) FORG_RATE,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_ORG_SPECIALRATE,0),
        0, '',
        V8_FNC_VALCURR_FORMATING(
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC)))) FCORG_RATE, 
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_ORG_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)))) ORG_RATE_DESCRIPTION,
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_ORG_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_NUM_TO_CHAR_FORMATING(
       V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))))) FORG_RATE_DESCRIPTION,                                   
trim(decode(yebs.YEBS_PKGINCL,
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBS_ORG_SPECIALRATE,0),
        0, Wtrl_Translate('YFRD',YFRD.YFRD_ID,'YFRD_LONGDESC',WLANID)||' ',
        V8_FNC_VALCURR_FORMATING(
         V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime), zcur.ZCUR_SHORTDESC)))) FCORG_RATE_DESCRIPTION,
yebs.yebs_rebate_perc,
yebs_rebate_flat YEBS_REBATE_FLAT,
V8_FNC_NUM_TO_CHAR_FORMATING(yebs_rebate_flat) FYEBS_REBATE_FLAT,
V8_FNC_VALCURR_FORMATING(yebs_rebate_flat, zcur.ZCUR_SHORTDESC) FCYEBS_REBATE_FLAT,
decode(yebs.yebs_rebate_perc, 0, V8_FNC_NUM_TO_CHAR_FORMATING(yebs_rebate_flat/decode(yebs_perhour, 0, 1, 1, (yebs.yebs_endtime-yebs.yebs_starttime)*24)), yebs.yebs_rebate_perc||'%') FYEBS_REBATE,
decode(yebs.yebs_rebate_perc, 0, V8_FNC_VALCURR_FORMATING(yebs_rebate_flat/decode(yebs_perhour, 0, 1, 1, (yebs.yebs_endtime-yebs.yebs_starttime)*24), zcur.ZCUR_SHORTDESC), yebs.yebs_rebate_perc||'%') FCYEBS_REBATE,        
decode(nvl(yebs_pkgincl, 0)+decode(nvl(yebs_org_specialrate, 0), 0, 1, 0)+decode(nvl(yebs_rebate_perc, 0)+nvl(yebs_rebate_flat, 0), 0,1,0), 0,
    decode(yebs.yebs_rebate_perc, 0,(yebs_rebate_flat/decode(yebs_perhour, 0, 1, 1, (yebs.yebs_endtime-yebs.yebs_starttime)*24))||' '||V8_FNC_WRTR_Translate('incl', WLANID), 
        yebs.yebs_rebate_perc||'%'||' '||V8_FNC_WRTR_Translate('Rebate', WLANID))) Yebs_Rebate_Tr,
decode(nvl(yebs_pkgincl, 0)+decode(nvl(yebs_org_specialrate, 0), 0, 1, 0)+decode(nvl(yebs_rebate_perc, 0)+nvl(yebs_rebate_flat, 0), 0,1,0), 0,
    decode(yebs.yebs_rebate_perc, 0,V8_FNC_NUM_TO_CHAR_FORMATING(yebs_rebate_flat/decode(yebs_perhour, 0, 1, 1, (yebs.yebs_endtime-yebs.yebs_starttime)*24))||' '||V8_FNC_WRTR_Translate('incl', WLANID), 
        yebs.yebs_rebate_perc||'%'||' '||V8_FNC_WRTR_Translate('Rebate', WLANID))) FYebs_Rebate_Tr,
decode(nvl(yebs_pkgincl, 0)+decode(nvl(yebs_org_specialrate, 0), 0, 1, 0)+decode(nvl(yebs_rebate_perc, 0)+nvl(yebs_rebate_flat, 0), 0,1,0), 0,
    decode(yebs.yebs_rebate_perc, 0,V8_FNC_VALCURR_FORMATING(yebs_rebate_flat/decode(yebs_perhour, 0, 1, 1, (yebs.yebs_endtime-yebs.yebs_starttime)*24), zcur.ZCUR_SHORTDESC)||' '||V8_FNC_WRTR_Translate('incl', WLANID), 
        yebs.yebs_rebate_perc||'%'||' '||V8_FNC_WRTR_Translate('Rebate', WLANID))) FCYebs_Rebate_Tr,                
decode(yebs.yebs_rebate_perc, 0, yebs_rebate_flat, 
      (yebs_rebate_perc* V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))/100) Yebs_Rebate_Total,
V8_FNC_NUM_TO_CHAR_FORMATING(decode(yebs.yebs_rebate_perc, 0, yebs_rebate_flat, 
      (yebs_rebate_perc* V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))/100)) FYebs_Rebate_Total,              
V8_FNC_VALCURR_FORMATING(decode(yebs.yebs_rebate_perc, 0, yebs_rebate_flat, 
      (yebs_rebate_perc* V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))/100), zcur.ZCUR_SHORTDESC) FCYebs_Rebate_Total,
V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime)-
    decode(yebs.yebs_rebate_perc, 0, yebs_rebate_flat, 
      (yebs_rebate_perc*V8_FNC_CCM_RATEVALUE(yebs.yebs_perhour, yebs.YEBS_ORG_SPECIALRATE, yebs.yebs_starttime,yebs.yebs_endtime))/100), zcur.ZCUR_SHORTDESC) FCYebs_Total_NORebate,
 YEBS.yebs_costprice YEBS_COSTPRICE,
V8_FNC_NUM_TO_CHAR_FORMATING(yebs.yebs_costprice) FYEBS_COSTPRICE,
V8_FNC_VALCURR_FORMATING(yebs.yebs_costprice, zcur.ZCUR_SHORTDESC) FCYEBS_COSTPRICE,
yevm.yevm_alternative,
yebs.yebs_alternative,
v8_fnc_ccm_alternatives ('YEBS',yebs.YEBS_ID,0,WLANID)Alternatives
FROM
    YEBS,
    YFSR,            
    YFRD,                
    YFSC,
    YEVM,
    YBOM,
    ZCUR
WHERE
    YEBS.YEBS_YFSC_ID = YFSC.YFSC_ID AND 
    YEBS.YEBS_YFSR_ID = YFSR.YFSR_ID(+) AND
    YFSR.YFSR_YFRD_ID = YFRD.YFRD_ID(+) AND
    YEBS.YEBS_YEVM_ID = YEVM.YEVM_ID AND
    YEVM.YEVM_YBOM_ID = YBOM.YBOM_ID AND 
    YBOM.YBOM_BASE_ZCUR_ID = ZCUR.ZCUR_ID AND
    YEBS.YEBS_YEVM_ID = YEVMID
ORDER BY YEBS.YEBS_DISPLORDER, YFSC.YFSC_LONGDESC;


   YEBS_ALL_VALUE   YEBS_ALL_CURSOR%ROWTYPE;
BEGIN
   BEGIN
      OPEN YEBS_ALL_CURSOR;

      LOOP
         FETCH YEBS_ALL_CURSOR INTO YEBS_ALL_VALUE;

         EXIT WHEN YEBS_ALL_CURSOR%NOTFOUND;

         IF ALL_YFSR IS NOT NULL
         THEN
            ALL_YFSR := ALL_YFSR || CHR (10);
         END IF;

         IF FIELDOPTIONNUMBER = 1
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.YFSC_LONGDESC_TR);

            IF     (YEBS_ALL_VALUE.yevm_alternative = 0)
               AND (YEBS_ALL_VALUE.yebs_alternative = 1)
            THEN
               IF YEBS_ALL_VALUE.alternatives IS NULL
               THEN
                  ALL_YFSR :=
                        ALL_YFSR
                     || CHR (13)
                     || CHR (10)
                     || V8_FNC_WRTR_Translate ('(alternative)', WLANID);
               ELSE
                  ALL_YFSR :=
                        ALL_YFSR
                     || CHR (13)
                     || CHR (10)
                     || V8_FNC_WRTR_Translate ('Alternative of', WLANID)
                     || ' '
                     || YEBS_ALL_VALUE.alternatives;
               END IF;
            END IF;
         END IF;

         IF FIELDOPTIONNUMBER = 2
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.YFSC_GENERIC_NAME_TR);
         END IF;

         IF FIELDOPTIONNUMBER = 3
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCRATE);
         END IF;

         IF FIELDOPTIONNUMBER = 4
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATEDESCRIPTION);
         END IF;

         IF FIELDOPTIONNUMBER = 5
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATEDESCRIPTIONIFRATEAMOUNT);
         END IF;

         IF FIELDOPTIONNUMBER = 6
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCRATE_DESCRIPTION);
         END IF;

         IF FIELDOPTIONNUMBER = 7
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.SEATING);
         END IF;

         IF FIELDOPTIONNUMBER = 8
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCORG_RATE);
         END IF;

         IF FIELDOPTIONNUMBER = 9
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCORG_RATE_DESCRIPTION);
         END IF;

         IF FIELDOPTIONNUMBER = 10
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.YEBS_REBATE_PERC);
         END IF;

         IF FIELDOPTIONNUMBER = 11
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCYEBS_REBATE_FLAT);
         END IF;
         
         IF FIELDOPTIONNUMBER = 12
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCYEBS_REBATE);
         END IF;
         
         IF FIELDOPTIONNUMBER = 13
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCYEBS_REBATE_TOTAL);
         END IF;

         IF FIELDOPTIONNUMBER = 14
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCYEBS_TOTAL_NOREBATE);
         END IF;

         IF FIELDOPTIONNUMBER = 15
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCYEBS_REBATE_TR);
         END IF;

         IF FIELDOPTIONNUMBER = 16
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCYEBS_COSTPRICE);
         END IF;

         IF FIELDOPTIONNUMBER = 17
         THEN
            IF     (YEBS_ALL_VALUE.yevm_alternative = 0)
               AND (YEBS_ALL_VALUE.yebs_alternative = 1)
            THEN
               IF YEBS_ALL_VALUE.alternatives IS NULL
               THEN
                  ALL_YFSR :=
                        ALL_YFSR
                     || V8_FNC_WRTR_Translate ('(alternative)', WLANID);
               ELSE
                  ALL_YFSR :=
                        ALL_YFSR
                     || V8_FNC_WRTR_Translate ('Alternative of', WLANID)
                     || ' '
                     || YEBS_ALL_VALUE.alternatives;
               END IF;
            END IF;
         END IF;

         IF FIELDOPTIONNUMBER = 18
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATE);
         END IF;

         IF FIELDOPTIONNUMBER = 19
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.FRATE);
         END IF;

         IF FIELDOPTIONNUMBER = 20
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATE_NET);
         END IF;

         IF FIELDOPTIONNUMBER = 21
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FRATE_NET);
         END IF;
         IF FIELDOPTIONNUMBER = 22
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCRATE_NET);
         END IF;

         IF FIELDOPTIONNUMBER = 23
         THEN
            ALL_YFSR := ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATE_VAT);
         END IF;

         IF FIELDOPTIONNUMBER = 24
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FRATE_VAT);
         END IF;
         IF FIELDOPTIONNUMBER = 25
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCRATE_VAT);
         END IF;
         IF FIELDOPTIONNUMBER = 26
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATE_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 27
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FRATE_DESCRIPTION);
         END IF;         
         IF FIELDOPTIONNUMBER = 28
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATE_NET_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER =29
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FRATE_NET_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 30
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCRATE_NET_DESCRIPTION);
         END IF;
          IF FIELDOPTIONNUMBER =31
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.RATE_VAT_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 32
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FRATE_VAT_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 33
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FCRATE_VAT_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 34
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.ORG_RATE);
         END IF;
         IF FIELDOPTIONNUMBER = 35
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FORG_RATE);
         END IF;
         IF FIELDOPTIONNUMBER = 36
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.ORG_RATE_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 37
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FORG_RATE_DESCRIPTION);
         END IF;
         IF FIELDOPTIONNUMBER = 38
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.YEBS_REBATE_FLAT);
         END IF;
         
         IF FIELDOPTIONNUMBER = 39
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FYEBS_REBATE_FLAT);
         END IF;
                          
         IF FIELDOPTIONNUMBER = 40
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FYEBS_REBATE);
         END IF;
         
          IF FIELDOPTIONNUMBER = 41
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.YEBS_REBATE_TOTAL);
         END IF;
                          
         IF FIELDOPTIONNUMBER = 42
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FYEBS_REBATE_TOTAL);
         END IF;
         
         IF FIELDOPTIONNUMBER = 43
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.YEBS_REBATE_TR);
         END IF;
         
          IF FIELDOPTIONNUMBER = 44
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FYEBS_REBATE_TR);
         END IF;
                
         
          IF FIELDOPTIONNUMBER = 45
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.YEBS_COSTPRICE);
         END IF;
                   
          IF FIELDOPTIONNUMBER = 46
         THEN
            ALL_YFSR :=
               ALL_YFSR || TRIM (YEBS_ALL_VALUE.FYEBS_COSTPRICE);
         END IF;
         
      END LOOP;

      ALL_YFSR := ALL_YFSR || CHR (1);
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         NULL;
      WHEN OTHERS
      THEN
         NULL;

         CLOSE YEBS_ALL_CURSOR;
   END;

   RETURN ALL_YFSR;
END V8_FNC_YEBS_REP_INFOS_TR;
/


CREATE OR REPLACE FUNCTION V8_FNC_CCM_MENU_RATES
(
  /* LS 28/11/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99 
 Attention FCYEMI_REBATE_TR: Function missing param wlan_id. Translation is hardcoded to wlan_id=1. To be fixed in following releases.
   
 VALUEOPTION IN NUMBER:
 
0   FCRATE: Returns Menu Item Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
1   FCORGRATE: Returns Menu Item Original Rate. If Item included in menu or no original rate amount or amount is 0 returns ''. 
2   FYEMI_REBATE_PERC: Returns Rebate percentage + '%'.
3   FCYEMI_REBATE_FLAT: Returns Total Rebate flat amount;
4   FCYEMI_REBATE_UNITPRICE: Returns Rebate Unit price.
5   FCYEMI_REBATE_TR: Returns Rebate Unit price + "incl"( not translated!) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' ( not translated !) if rebate is %.
6   FCYEMI_COSTPRICE: Returns  Menu Item cost price.   
7   RATE: Returns Menu Item Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
8   FRATE: Returns Menu Item Rate. If Item included in menu or no rate amount or amount is 0 returns ''.
9   RATE_NET: Returns Menu Item Net Rate. If Item included in menu or no rate amount or amount is 0 returns ''.  
10  FRATE_NET: Returns Menu Item Net Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
11  FCRATE_NET: Returns Menu Item Net Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
12  RATE_VAT: Returns Menu Item Rate Vat. If Item included in menu or no rate amount or amount is 0 returns ''.  
13  FRATE_VAT: Returns Menu Item Rate Vat. If Item included in menu or no rate amount or amount is 0 returns ''. 
14  FCRATE_VAT: Returns Menu Item Rate Vat. If Item included in menu or no rate amount or amount is 0 returns ''. 
15  ORGRATE: Returns Menu Item Original Rate. If Item included in menu or no original rate amount or amount is 0 returns ''. 
16  FORGRATE: Returns Menu Item Original. If Item included in menu or no original rate amount or amount is 0 returns ''. 
17  YEMI_REBATE_FLAT: Returns Total Rebate flat amount;
18  FYEMI_REBATE_FLAT: Returns Total Rebate flat amount;
19  YEMI_REBATE_UNITPRICE: Returns Rebate Unit price.
20  FYEMI_REBATE_UNITPRICE: Returns Rebate Unit price.
21  YEMI_COSTPRICE: Returns  Menu Item cost price.
22  FYEMI_COSTPRICE: Returns  Menu Item cost price.
   */


  TABLE_ID IN NUMBER,            -- YEVM_ID or YEMN_ID
  YEVM_OR_YEMN IN NUMBER,        -- here you set 1 for YEVM_ID or 2 for YEMN_ID
  INCLUDEDINTHEMENU IN NUMBER,   --0 not included, 1 inluded, 2 both
  FOODORBEVERAGE IN NUMBER,      --0 Beverage, 1 Food, 2 Both
  VALUEOPTION IN NUMBER          -- See above. 
)
  return varchar2 as
  ALL_MENU varchar2(2000);

  cursor YEMI_ALL_CURSOR is 
  
  
   SELECT yemi.YEMI_ID YEMI_ID,
          yemi.YEMI_YEVM_ID YEMI_YEVM_ID,
          yemi.YEMI_YEMN_ID YEMI_YEMN_ID,
          DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                    yemi.YEMI_PRICE))
             RATE,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_PRICE))))
             FRATE,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_PRICE,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCRATE,            
              DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                    yemi.YEMI_PRICE_NET))
             RATE_NET,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_PRICE_NET))))
             FRATE_NET,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_PRICE_NET,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
               FCRATE_NET,             
              DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                    yemi.YEMI_PRICE_VAT))
             RATE_VAT,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_PRICE_VAT))))
             FRATE_VAT,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_PRICE_VAT,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCRATE_VAT,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',yemi.YEMI_ORG_PRICE)))
             ORGRATE,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',
                 V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_ORG_PRICE))))
             FORGRATE,                     
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_ORG_PRICE,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCORGRATE,                         
          yemi.yemi_rebate_perc,
          V8_FNC_VALCURR_FORMATING (yemi.yemi_rebate_perc, '%')
             fyemi_rebate_perc,
          yemi.yemi_rebate_flat,
           V8_FNC_NUM_TO_CHAR_FORMATING(yemi.yemi_rebate_flat)fyemi_rebate_flat, 
           V8_FNC_VALCURR_FORMATING(yemi.yemi_rebate_flat,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1))
             fcyemi_rebate_flat,          
          yemi.yemi_rebate_unitprice,
          V8_FNC_NUM_TO_CHAR_FORMATING(yemi.yemi_rebate_unitprice) fyemi_rebate_unitprice,
          V8_FNC_VALCURR_FORMATING (yemi.yemi_rebate_unitprice,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1)) fcyemi_rebate_unitprice,
          DECODE (
               NVL (yemn_pkgincl, 0)
             + DECODE (NVL (yemi_org_price, 0), 0, 1, 0)
             + DECODE (NVL (yemi_rebate_perc, 0) + NVL (yemi_rebate_flat, 0),
                       0, 1,
                       0),
             0, DECODE (
                   yemi_rebate_perc,
                   0,    V8_FNC_VALCURR_FORMATING (
                            yemi_rebate_unitprice,
                            (SELECT zcur.ZCUR_SHORTDESC
                               FROM yevm, ybom, zcur
                              WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                                    AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                                    AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                                    AND ROWNUM = 1))
                      || ' '
                      || V8_FNC_WRTR_Translate ('incl', 1),
                      yemi_rebate_perc
                   || '%'
                   || ' '
                   || V8_FNC_WRTR_Translate ('Rebate', 1)))
             FCYemi_Rebate_Tr,
          yemi.yemi_costprice,
          V8_FNC_NUM_TO_CHAR_FORMATING(yemi.yemi_costprice) Fyemi_costprice,   
          V8_FNC_VALCURR_FORMATING (
             yemi.yemi_costprice,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1))
             FCyemi_costprice,
             NVL2 (yemi.YEMI_LONGDESC2, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_LONGDESC3, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_LONGDESC4, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_ORIGIN, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_ORIGIN2, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_ORIGIN3, CHR (10), TO_CHAR (NULL))
             SPACING,
          YEMI.YEMI_COURSE             
     FROM
         YEMI,          
         YEMN
    WHERE yemi_yemn_id = yemn_id
    and  ((YEVM_OR_YEMN=1) and YEMI.YEMI_YEVM_ID=TABLE_ID or (YEVM_OR_YEMN=2) and YEMI.YEMI_YEMN_ID=TABLE_ID) 
    and  ((INCLUDEDINTHEMENU=2) or (INCLUDEDINTHEMENU<>2) and (YEMI.YEMI_INCLUDEDINTHEMENU=INCLUDEDINTHEMENU))
    and  ((FOODORBEVERAGE=2) or (FOODORBEVERAGE<>2) and (YEMI.YEMI_FOODORBEVERAGE=FOODORBEVERAGE))
    ORDER BY yemi.YEMI_COURSE,
            yemi.YEMI_COURSEORDER,
            yemi.YEMI_FOODORBEVERAGE;  
 
  
  YEMI_ALL_VALUE YEMI_ALL_CURSOR%rowtype;

  courseflag number;
begin

  begin
    courseflag := NULL;
 open YEMI_ALL_CURSOR;
 loop
 fetch YEMI_ALL_CURSOR
 into YEMI_ALL_VALUE;
 exit when YEMI_ALL_CURSOR%notfound;
 
 if ALL_MENU is NOT NULL then
   ALL_MENU := ALL_MENU||CHR(10);
 end if;

 if courseflag is NULL then
   courseflag := YEMI_ALL_VALUE.YEMI_COURSE;
 else
   if courseflag <> YEMI_ALL_VALUE.YEMI_COURSE then
     ALL_MENU := ALL_MENU||CHR(10);
  courseflag := YEMI_ALL_VALUE.YEMI_COURSE;
   end if; 
 end if;

  if VALUEOPTION=0 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCRATE;
  end if;
  if VALUEOPTION=1 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCORGRATE;
  end if;
  if VALUEOPTION=2 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_REBATE_PERC;
  end if;
  if VALUEOPTION=3 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_REBATE_FLAT;
  end if;
  if VALUEOPTION=4 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_REBATE_UNITPRICE;
  end if;
  if VALUEOPTION=5 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_REBATE_TR;
  end if;
  if VALUEOPTION=6 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_COSTPRICE;
  end if;
  if VALUEOPTION=7 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.RATE;
  end if;
  if VALUEOPTION=8 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FRATE;
  end if;
  if VALUEOPTION=9 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.RATE_NET;
  end if;
  if VALUEOPTION=10 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FRATE_NET;
  end if;
  if VALUEOPTION=11 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCRATE_NET;
  end if;
  if VALUEOPTION=12 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.RATE_VAT;
  end if;
  if VALUEOPTION=13 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FRATE_VAT;
  end if;
  if VALUEOPTION=14 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCRATE_VAT;
  end if;
  if VALUEOPTION=15 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.ORGRATE;
  end if;
  if VALUEOPTION=16 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FORGRATE;
  end if;
  if VALUEOPTION=17 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.YEMI_REBATE_FLAT;
  end if;
  if VALUEOPTION=18 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_REBATE_FLAT;
  end if;
  if VALUEOPTION=19 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.YEMI_REBATE_UNITPRICE;
  end if;
  if VALUEOPTION=20 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_REBATE_UNITPRICE;
  end if;
  if VALUEOPTION=21 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.YEMI_COSTPRICE;
  end if;
  if VALUEOPTION=22 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_COSTPRICE;
  end if;
  
 ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.SPACING;
   
   if ALL_MENU is NULL then
     ALL_MENU := ' ';
   end if;   

 end loop;
 ALL_MENU := ALL_MENU||CHR(1);
 exception
      when no_data_found then null;
      when others then null;
 close YEMI_ALL_CURSOR;
  end;
  RETURN ALL_MENU;
end V8_FNC_CCM_MENU_RATES;
/


CREATE OR REPLACE FUNCTION V8_FNC_CCM_MENU_TOT_RATES
(

/* LS 04/12/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99 
  
 VALUEOPTION IN NUMBER:
 
0   FCTOT_RATE: Returns Total Menu Item Rate (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''. 
1   FCTOT_ORGRATE: Returns Total Menu Item Original Rate (Amount * Quantity). If Item included in menu or no original rate amount or amount is 0 returns ''. 
2   TOT_RATE: Returns Total Menu Item Rate (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''. 
3   FTOT_RATE: Returns Total Menu Item Rate (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''.
4   RATE_NET: Returns Total Menu Item Net Rate (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''.  
5   FRATE_NET: Returns Total Menu Item Net Rate (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''. 
6   FCRATE_NET: Returns Total Menu Item Net Rate (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''. 
7   RATE_VAT: Returns  Total Menu Item Rate Vat (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''.  
8   FRATE_VAT: Returns  Total Menu Item Rate Vat (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''. 
9   FCRATE_VAT: Returns  Total Menu Item Rate Vat (Amount * Quantity). If Item included in menu or no rate amount or amount is 0 returns ''. 
10  ORGRATE: Returns Total Menu Item Original Rate (Amount * Quantity). If Item included in menu or no original rate amount or amount is 0 returns ''. 
11  FORGRATE: Returns  Total Menu Item Original Rate (Amount * Quantity). If Item included in menu or no original rate amount or amount is 0 returns ''. 
   */

  TABLE_ID IN NUMBER,            -- YEVM_ID or YEMN_ID
  YEVM_OR_YEMN IN NUMBER,        -- here you set 1 for YEVM_ID or 2 for YEMN_ID
  INCLUDEDINTHEMENU IN NUMBER,      --0 not included, 1 inluded, 2 both
  FOODORBEVERAGE IN NUMBER,       --0 Beverage, 1 Food, 2 Both
  VALUEOPTION IN NUMBER           -- see above
)
  return varchar2 as
  ALL_MENU varchar2(2000);

  cursor YEMI_ALL_CURSOR is
  
  
   SELECT yemi.YEMI_ID YEMI_ID,
          yemi.YEMI_YEVM_ID YEMI_YEVM_ID,
          yemi.YEMI_YEMN_ID YEMI_YEMN_ID,
          DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                   yemi.YEMI_QUANTITY * yemi.YEMI_PRICE))
             TOT_RATE,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                   yemi.YEMI_QUANTITY * yemi.YEMI_PRICE))))
             FTOT_RATE,         
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_QUANTITY * yemi.YEMI_PRICE,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCTOT_RATE,            
              DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                 yemi.YEMI_QUANTITY * yemi.YEMI_PRICE_NET))
             TOT_RATE_NET,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                  yemi.YEMI_QUANTITY * yemi.YEMI_PRICE_NET))))
             FTOT_RATE_NET,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                  yemi.YEMI_QUANTITY *  yemi.YEMI_PRICE_NET,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
               FCTOT_RATE_NET,             
              DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                   yemi.YEMI_QUANTITY * yemi.YEMI_PRICE_VAT))
             TOT_RATE_VAT,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                 yemi.YEMI_QUANTITY * yemi.YEMI_PRICE_VAT))))
             FTOT_RATE_VAT,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                   yemi.YEMI_QUANTITY * yemi.YEMI_PRICE_VAT,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCTOT_RATE_VAT,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '', yemi.YEMI_QUANTITY * yemi.YEMI_ORG_PRICE)))
             TOT_ORGRATE,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',
                 V8_FNC_NUM_TO_CHAR_FORMATING (
                 yemi.YEMI_QUANTITY * yemi.YEMI_ORG_PRICE))))
             FTOT_ORGRATE,                     
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                  yemi.YEMI_QUANTITY * yemi.YEMI_ORG_PRICE,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCTOT_ORGRATE,                         
             NVL2 (yemi.YEMI_LONGDESC2, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_LONGDESC3, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_LONGDESC4, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_ORIGIN, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_ORIGIN2, CHR (10), TO_CHAR (NULL))
          || NVL2 (yemi.YEMI_ORIGIN3, CHR (10), TO_CHAR (NULL))
             SPACING,
          YEMI.YEMI_COURSE             
     FROM
         YEMI,          
         YEMN
    WHERE yemi_yemn_id = yemn_id
    and  ((YEVM_OR_YEMN=1) and YEMI.YEMI_YEVM_ID=TABLE_ID or (YEVM_OR_YEMN=2) and YEMI.YEMI_YEMN_ID=TABLE_ID) 
    and  ((INCLUDEDINTHEMENU=2) or (INCLUDEDINTHEMENU<>2) and (YEMI.YEMI_INCLUDEDINTHEMENU=INCLUDEDINTHEMENU))
    and  ((FOODORBEVERAGE=2) or (FOODORBEVERAGE<>2) and (YEMI.YEMI_FOODORBEVERAGE=FOODORBEVERAGE))
    ORDER BY yemi.YEMI_COURSE,
            yemi.YEMI_COURSEORDER,
            yemi.YEMI_FOODORBEVERAGE;  

  YEMI_ALL_VALUE YEMI_ALL_CURSOR%rowtype;

  courseflag number;
begin

  begin
    courseflag := NULL;
 open YEMI_ALL_CURSOR;
 loop
 fetch YEMI_ALL_CURSOR
 into YEMI_ALL_VALUE;
 exit when YEMI_ALL_CURSOR%notfound;

 if ALL_MENU is NOT NULL then
   ALL_MENU := ALL_MENU||CHR(10);
 end if;

 if courseflag is NULL then
   courseflag := YEMI_ALL_VALUE.YEMI_COURSE;
 else
   if courseflag <> YEMI_ALL_VALUE.YEMI_COURSE then
     ALL_MENU := ALL_MENU||CHR(10);
  courseflag := YEMI_ALL_VALUE.YEMI_COURSE;
   end if;
 end if;


  if VALUEOPTION=0 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCTOT_RATE;
  end if;
  if VALUEOPTION=1 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCTOT_ORGRATE;
  end if;
  if VALUEOPTION=2 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.TOT_RATE;
  end if;
  if VALUEOPTION=3 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FTOT_RATE;
  end if;
  if VALUEOPTION=4 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.TOT_RATE_NET;
  end if;
  if VALUEOPTION=5 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FTOT_RATE_NET;
  end if;
  if VALUEOPTION=6 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCTOT_RATE_NET;
  end if;
  if VALUEOPTION=7 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.TOT_RATE_VAT;
  end if;
  if VALUEOPTION=8 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FTOT_RATE_VAT;
  end if;
  if VALUEOPTION=9 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCTOT_RATE_VAT;
  end if;
  if VALUEOPTION=10 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.TOT_ORGRATE;
  end if;
  if VALUEOPTION=11 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FTOT_ORGRATE;
  end if;


ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.SPACING;

 end loop;
 ALL_MENU := ALL_MENU||CHR(1);
 exception
      when no_data_found then null;
      when others then null;
 close YEMI_ALL_CURSOR;
  end;
  RETURN ALL_MENU;
end V8_FNC_CCM_MENU_TOT_RATES;
/


CREATE OR REPLACE FUNCTION V8_FNC_CCM_MENUITEM_RATES
(
/* LS 28/11/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99 
   Attention FCYEMI_REBATE_TR: Function V8_FNC_CCM_MENUITEM_RATES missing param wlan_id. Translation is hardcoded to wlan_id=1. To be fixed in following releases.
   
VALUEOPTION IN NUMBER:
 
0   FCRATE: Returns Menu Item Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
1   FCORGRATE: Returns Menu Item Original Rate. If Item included in menu or no original rate amount or amount is 0 returns ''. 
2   FYEMI_REBATE_PERC: Returns Rebate percentage + '%'.
3   FCYEMI_REBATE_FLAT: Returns Total Rebate flat amount;
4   FCYEMI_REBATE_UNITPRICE: Returns Rebate Unit price.
5   FCYEMI_REBATE_TR: Returns Rebate Unit price + "incl"( not translated!) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' ( not translated !) if rebate is %.
6   FCYEMI_COSTPRICE: Returns  Menu Item cost price.   
7   RATE: Returns Menu Item Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
8   FRATE: Returns Menu Item Rate. If Item included in menu or no rate amount or amount is 0 returns ''.
9   RATE_NET: Returns Menu Item Net Rate. If Item included in menu or no rate amount or amount is 0 returns ''.  
10  FRATE_NET: Returns Menu Item Net Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
11  FCRATE_NET: Returns Menu Item Net Rate. If Item included in menu or no rate amount or amount is 0 returns ''. 
12  RATE_VAT: Returns Menu Item Rate Vat. If Item included in menu or no rate amount or amount is 0 returns ''.  
13  FRATE_VAT: Returns Menu Item Rate Vat. If Item included in menu or no rate amount or amount is 0 returns ''. 
14  FCRATE_VAT: Returns Menu Item Rate Vat. If Item included in menu or no rate amount or amount is 0 returns ''. 
15  ORGRATE: Returns Menu Item Original Rate. If Item included in menu or no original rate amount or amount is 0 returns ''. 
16  FORGRATE: Returns Menu Item Original Rate. If Item included in menu or no original rate amount or amount is 0 returns ''. 
17  YEMI_REBATE_FLAT: Returns Total Rebate flat amount;
18  FYEMI_REBATE_FLAT: Returns Total Rebate flat amount;
19  YEMI_REBATE_UNITPRICE: Returns Rebate Unit price.
20  FYEMI_REBATE_UNITPRICE: Returns Rebate Unit price.
21  YEMI_COSTPRICE: Returns  Menu Item cost price.
22  FYEMI_COSTPRICE: Returns  Menu Item cost price.
   */

  TABLE_ID IN NUMBER,         -- YEMI_ID
  VALUEOPTION IN NUMBER       
)
  return varchar2 as
  ALL_MENU varchar2(2000);

  cursor YEMI_ALL_CURSOR is 
   SELECT yemi.YEMI_ID YEMI_ID,
          yemi.YEMI_YEVM_ID YEMI_YEVM_ID,
          yemi.YEMI_YEMN_ID YEMI_YEMN_ID,
          DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                    yemi.YEMI_PRICE))
             RATE,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_PRICE))))
             FRATE,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_PRICE,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCRATE,            
              DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                    yemi.YEMI_PRICE_NET))
             RATE_NET,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_PRICE_NET))))
             FRATE_NET,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_PRICE_NET,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
               FCRATE_NET,             
              DECODE (yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (NVL (yemi.YEMI_PRICE, 0),0, '',             
                    yemi.YEMI_PRICE_VAT))
             RATE_VAT,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_PRICE_VAT))))
             FRATE_VAT,            
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_PRICE_VAT,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCRATE_VAT,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_NUMBER (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',yemi.YEMI_ORG_PRICE)))
             ORGRATE,
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',
                 V8_FNC_NUM_TO_CHAR_FORMATING (
                    yemi.YEMI_ORG_PRICE))))
             FORGRATE,                     
          (DECODE (
              yemi.YEMI_INCLUDEDINTHEMENU,
              1, TO_CHAR (NULL),
              DECODE (
                 NVL (yemi.YEMI_ORG_PRICE, 0),
                 0, '',
                 V8_FNC_VALCURR_FORMATING (
                    yemi.YEMI_ORG_PRICE,
                    (SELECT zcur.ZCUR_SHORTDESC
                       FROM yevm, ybom, zcur
                      WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                            AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                            AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                            AND ROWNUM = 1)))))
             FCORGRATE,                         
          yemi.yemi_rebate_perc,
          V8_FNC_VALCURR_FORMATING (yemi.yemi_rebate_perc, '%')
             fyemi_rebate_perc,
          yemi.yemi_rebate_flat,
           V8_FNC_NUM_TO_CHAR_FORMATING(yemi.yemi_rebate_flat)fyemi_rebate_flat, 
           V8_FNC_VALCURR_FORMATING(yemi.yemi_rebate_flat,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1))
             fcyemi_rebate_flat,          
          yemi.yemi_rebate_unitprice,
          V8_FNC_NUM_TO_CHAR_FORMATING(yemi.yemi_rebate_unitprice) fyemi_rebate_unitprice,
          V8_FNC_VALCURR_FORMATING (yemi.yemi_rebate_unitprice,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1)) fcyemi_rebate_unitprice,
          DECODE (
               NVL (yemn_pkgincl, 0)
             + DECODE (NVL (yemi_org_price, 0), 0, 1, 0)
             + DECODE (NVL (yemi_rebate_perc, 0) + NVL (yemi_rebate_flat, 0),
                       0, 1,
                       0),
             0, DECODE (
                   yemi_rebate_perc,
                   0,    V8_FNC_VALCURR_FORMATING (
                            yemi_rebate_unitprice,
                            (SELECT zcur.ZCUR_SHORTDESC
                               FROM yevm, ybom, zcur
                              WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                                    AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                                    AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                                    AND ROWNUM = 1))
                      || ' '
                      || V8_FNC_WRTR_Translate ('incl', 1),
                      yemi_rebate_perc
                   || '%'
                   || ' '
                   || V8_FNC_WRTR_Translate ('Rebate', 1)))
             FCYemi_Rebate_Tr,
          yemi.yemi_costprice,
          V8_FNC_NUM_TO_CHAR_FORMATING(yemi.yemi_costprice) Fyemi_costprice,   
          V8_FNC_VALCURR_FORMATING (
             yemi.yemi_costprice,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yemi.YEMI_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1))
             FCyemi_costprice        
     FROM    YEMI,         
             YEMN
    WHERE yemi_yemn_id = yemn_id
    and YEMI.YEMI_ID =TABLE_ID
    ORDER BY yemi.YEMI_COURSE,
            yemi.YEMI_COURSEORDER,
            yemi.YEMI_FOODORBEVERAGE;

  
  YEMI_ALL_VALUE YEMI_ALL_CURSOR%rowtype;
  
begin
    begin
        open YEMI_ALL_CURSOR;
        loop
        fetch YEMI_ALL_CURSOR
        into YEMI_ALL_VALUE;
        exit when YEMI_ALL_CURSOR%notfound;
 
        if VALUEOPTION=0 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCRATE;
        end if;
        if VALUEOPTION=1 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCORGRATE;
        end if;
        if VALUEOPTION=2 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_REBATE_PERC;
        end if;
        if VALUEOPTION=3 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_REBATE_FLAT;
        end if;
        if VALUEOPTION=4 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_REBATE_UNITPRICE;
        end if;
        if VALUEOPTION=5 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_REBATE_TR;
        end if;
        if VALUEOPTION=6 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCYEMI_COSTPRICE;
        end if;
        if VALUEOPTION=7 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.RATE;
        end if;
        if VALUEOPTION=8 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FRATE;
        end if;
        if VALUEOPTION=9 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.RATE_NET;
        end if;
        if VALUEOPTION=10 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FRATE_NET;
        end if;
        if VALUEOPTION=11 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCRATE_NET;
        end if;
        if VALUEOPTION=12 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.RATE_VAT;
        end if;
        if VALUEOPTION=13 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FRATE_VAT;
        end if;
        if VALUEOPTION=14 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FCRATE_VAT;
        end if;
        if VALUEOPTION=15 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.ORGRATE;
        end if;
        if VALUEOPTION=16 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FORGRATE;
        end if;
        if VALUEOPTION=17 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.YEMI_REBATE_FLAT;
        end if;
        if VALUEOPTION=18 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_REBATE_FLAT;
        end if;
        if VALUEOPTION=19 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.YEMI_REBATE_UNITPRICE;
        end if;
        if VALUEOPTION=20 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_REBATE_UNITPRICE;
        end if;
        if VALUEOPTION=21 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.YEMI_COSTPRICE;
        end if;
        if VALUEOPTION=22 then ALL_MENU := ALL_MENU||YEMI_ALL_VALUE.FYEMI_COSTPRICE;
        end if;
        
        end loop;
        exception
            when no_data_found then null;
            when others then null;
        close YEMI_ALL_CURSOR;
    end;
    RETURN ALL_MENU;
end V8_FNC_CCM_MENUITEM_RATES;
/


CREATE OR REPLACE function V8_FNC_CCM_STD_MENU
(
/* LS 28/11/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99
   All output of function is translated according to provided wlanid, if translation available.
STDMENUINFONR NUMBER:
1   STDMENUNAME: Returns Standard Menu Name.
2   FCRATE: Returns Standard  Menu Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
3   FCTOT_RATE: Returns Total Standard  Menu Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
4   FCORG_RATE: Returns Standard Menu Original Rate.
5   FCTOT_ORG_RATE: Returns Total Standard Menu Original Rate.
6   FCYEMN_COSTPRICE: Returns Standard Menu Cost Price.
7   RATE: Returns Standard  Menu Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
8   FRATE: Returns Standard  Menu Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
9   RATE_NET: Returns Standard Menu Net Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".   
10  FRATE_NET: Returns Standard  Menu Net Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
11  FCRATE_NET: Returns Standard  Menu Net Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
12  RATE_VAT: Returns Standard Menu Rate Vat. If Menu included in package or if On Consumption or no rate amount or rate amount is 0 returns "".   
13  FRATE_VAT: Returns Standard  Menu Rate Vat. If Menu included in package or if On Consumption or no rate amount or rate amount is 0 returns "".
14  FCRATE_VAT: Returns Standard  Menu Rate Vat. If Menu included in package or if On Consumption or no rate amount or rate amount is 0 returns "".
15  TOT_RATE: Returns Total Standard Menu Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
16  FTOT_RATE: Returns Total Standard Menu Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
17  TOT_RATE_NET: Returns Total Standard Menu Net Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
18  FTOT_RATE_NET: Returns Total Standard Menu Net Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
19  FCTOT_RATE_NET: Returns Total Standard  Menu Net Rate. If Menu included in package returns "Incl. in package".  If On Consumption returns "on consumption" If no rate amount or amount is 0 returns "".
20  TOT_RATE_VAT: Returns Total Standard Menu Rate Vat. If Menu included in package or if On Consumption or no rate amount or rate amount is 0 returns "".
21  FTOT_RATE_VAT: Returns Total Standard Menu Rate Vat. If Menu included in package or if On Consumption or no rate amount or rate amount is 0 returns "".
22  FCTOT_RATE_VAT: Returns Total Standard Menu Rate Vat. If Menu included in package or if On Consumption or no rate amount or rate amount is 0 returns "".
23  ORG_RATE: Returns Standard Menu Original Rate.
24  FORG_RATE: Returns Standard Menu Original Rate.
25  TOT_ORG_RATE: Returns Total Standard Menu Original Rate.
26  FTOT_ORG_RATE: Returns Total Standard Menu Original Rate.
27  YEMN_COSTPRICE: Returns Standard Menu Cost Price.
28  FYEMN_COSTPRICE: Returns Standard Menu Cost Price.
*/
  YEMNID IN NUMBER,    
  STDMENUINFONR NUMBER,              
  WLANID IN NUMBER:=null                  
)
  return varchar2 as
  STDMENUINFO varchar2(2000);
  
  CURSOR STDMENUINFOCURSOR is
  select decode(yemn_menuname, ysme_longdesc, WTRL_Translate('YSME', ysme_id, 'YSME_LONGDESC', WLANID), WTRL_Translate('YEMN', yemn_id, 'YEMN_MENUNAME', WLANID)) STDMENUNAME,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),yemn.YEMN_MENUPRICE))),16),17,' ') RATE,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_NUM_TO_CHAR_FORMATING(yemn.YEMN_MENUPRICE)))),16),17,' ') FRATE,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_VALCURR_FORMATING(yemn.YEMN_MENUPRICE, (select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1))))),16),17,' ') FCRATE,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),(select sum(ymrd.ymrd_price_net) from ymrd where ymrd_yemn_id=yemn.yemn_id)))),16),17,' ') RATE_NET,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_NUM_TO_CHAR_FORMATING((select sum(ymrd.ymrd_price_net) from ymrd where ymrd_yemn_id=yemn.yemn_id))))),16),17,' ') FRATE_NET,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_VALCURR_FORMATING((select sum(ymrd.ymrd_price_net) from ymrd where ymrd_yemn_id=yemn.yemn_id),(select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1))))),16),17,' ') FCRATE_NET,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,'',decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,'', decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),(select sum(ymrd.ymrd_price_vat) from ymrd where ymrd_yemn_id=yemn.yemn_id)))),16),17,' ') RATE_VAT,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,'',decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,'', decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_NUM_TO_CHAR_FORMATING((select sum(ymrd.ymrd_price_vat) from ymrd where ymrd_yemn_id=yemn.yemn_id))))),16),17,' ') FRATE_VAT,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,'',decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,'', decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_VALCURR_FORMATING((select sum(ymrd.ymrd_price_vat) from ymrd where ymrd_yemn_id=yemn.yemn_id),(select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1))))),16),17,' ') FCRATE_VAT,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),yemn.YEMN_QUANTITY*yemn.YEMN_MENUPRICE))),16),17,' ') TOT_RATE,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_NUM_TO_CHAR_FORMATING(yemn.YEMN_QUANTITY*yemn.YEMN_MENUPRICE)))),16),17,' ') FTOT_RATE,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_VALCURR_FORMATING(yemn.YEMN_QUANTITY*yemn.YEMN_MENUPRICE, (select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1))))),16),17,' ') FCTOT_RATE,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),yemn.yemn_quantity*(select sum(ymrd.ymrd_price_net) from ymrd where ymrd_yemn_id=yemn.yemn_id)))),16),17,' ') TOT_RATE_NET,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_NUM_TO_CHAR_FORMATING(yemn.yemn_quantity*(select sum(ymrd.ymrd_price_net) from ymrd where ymrd_yemn_id=yemn.yemn_id))))),16),17,' ') FTOT_RATE_NET,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,V8_FNC_WRTR_Translate('incl. in package', WLANID),decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,V8_FNC_WRTR_Translate('on consumption', WLANID), decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_VALCURR_FORMATING(yemn.yemn_quantity*(select sum(ymrd.ymrd_price_net) from ymrd where ymrd_yemn_id=yemn.yemn_id),(select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1))))),16),17,' ') FCTOT_RATE_NET,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,'',decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,'', decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),yemn.yemn_quantity*(select sum(ymrd.ymrd_price_vat) from ymrd where ymrd_yemn_id=yemn.yemn_id)))),16),17,' ') TOT_RATE_VAT,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,'',decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,'', decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_NUM_TO_CHAR_FORMATING(yemn.yemn_quantity*(select sum(ymrd.ymrd_price_vat) from ymrd where ymrd_yemn_id=yemn.yemn_id))))),16),17,' ') FTOT_RATE_VAT,
  lpad(rtrim(decode(yemn.YEMN_PKGINCL,1,'',decode(yemn.YEMN_CHARGEDONCONSUMPTION,1,'', decode(nvl(yemn.YEMN_MENUPRICE,0),0,to_char(null),V8_FNC_VALCURR_FORMATING(yemn.yemn_quantity*(select sum(ymrd.ymrd_price_vat) from ymrd where ymrd_yemn_id=yemn.yemn_id),(select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1))))),16),17,' ') FCTOT_RATE_VAT, 
  (select sum(ymrd_org_amount) from ymrd where ymrd_yemn_id=yemn.yemn_id) ORG_RATE,
  V8_FNC_NUM_TO_CHAR_FORMATING((select sum(ymrd_org_amount) from ymrd where ymrd_yemn_id=yemn.yemn_id)) FORG_RATE,
  V8_FNC_VALCURR_FORMATING( 
    (select sum(ymrd_org_amount) from ymrd where ymrd_yemn_id=yemn.yemn_id), 
    (select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1
     )  ) FCORG_RATE,
    yemn.yemn_quantity*(select sum(ymrd_org_amount) from ymrd where ymrd_yemn_id=yemn.yemn_id) TOT_ORG_RATE,
 V8_FNC_NUM_TO_CHAR_FORMATING( 
    yemn.yemn_quantity*(select sum(ymrd_org_amount) from ymrd where ymrd_yemn_id=yemn.yemn_id)) FTOT_ORG_RATE,
  V8_FNC_VALCURR_FORMATING( 
    yemn.yemn_quantity*(select sum(ymrd_org_amount) from ymrd where ymrd_yemn_id=yemn.yemn_id), 
    (select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1
     )  ) FCTOT_ORG_RATE,
  yemn.yemn_costprice YEMN_COSTPRICE,
  V8_FNC_NUM_TO_CHAR_FORMATING(yemn.yemn_costprice) FYEMN_COSTPRICE,
  V8_FNC_VALCURR_FORMATING(yemn.yemn_costprice, (select zcur.ZCUR_SHORTDESC from yevm,ybom,zcur where yevm.YEVM_ID=yemn.YEMN_YEVM_ID and yevm.YEVM_YBOM_ID=ybom.YBOM_ID and zcur.ZCUR_ID=ybom.YBOM_BASE_ZCUR_ID and rownum=1)) FCYEMN_COSTPRICE   
  from yemn, ysme  
  where yemn_ysme_id=ysme_id and yemn_id=YEMNID;
  
  STDMENUINFOVALUE STDMENUINFOCURSOR%rowtype;
begin
  begin
	open STDMENUINFOCURSOR;
	loop
	fetch STDMENUINFOCURSOR
	into STDMENUINFOVALUE;
	exit when STDMENUINFOCURSOR%notfound;
	
	if STDMENUINFONR = 1 then
	  STDMENUINFO := STDMENUINFOVALUE.STDMENUNAME;
	end if;
	if STDMENUINFONR = 2 then
	  STDMENUINFO := STDMENUINFOVALUE.FCRATE;
	end if;
	if STDMENUINFONR = 3 then
	  STDMENUINFO := STDMENUINFOVALUE.FCTOT_RATE;
	end if;
	if STDMENUINFONR = 4 then
	  STDMENUINFO := STDMENUINFOVALUE.FCORG_RATE;
	end if;
	if STDMENUINFONR = 5 then
	  STDMENUINFO := STDMENUINFOVALUE.FCTOT_ORG_RATE;
	end if;
	if STDMENUINFONR = 6 then
	  STDMENUINFO := STDMENUINFOVALUE.FCYEMN_COSTPRICE;
	end if;
    if STDMENUINFONR = 7 then
      STDMENUINFO := STDMENUINFOVALUE.RATE;
    end if;
    if STDMENUINFONR = 8 then
      STDMENUINFO := STDMENUINFOVALUE.FRATE;
    end if;
        if STDMENUINFONR = 9 then
      STDMENUINFO := STDMENUINFOVALUE.RATE_NET;
    end if;
    if STDMENUINFONR = 10 then
      STDMENUINFO := STDMENUINFOVALUE.FRATE_NET;
    end if;
    if STDMENUINFONR = 11 then
      STDMENUINFO := STDMENUINFOVALUE.FCRATE_NET;
    end if;
    if STDMENUINFONR = 12 then
      STDMENUINFO := STDMENUINFOVALUE.RATE_VAT;
    end if;
    if STDMENUINFONR = 13 then
      STDMENUINFO := STDMENUINFOVALUE.FRATE_VAT;
    end if;
    if STDMENUINFONR = 14 then
      STDMENUINFO := STDMENUINFOVALUE.FCRATE_VAT;
    end if;
    if STDMENUINFONR = 15 then
      STDMENUINFO := STDMENUINFOVALUE.TOT_RATE;
    end if;
	if STDMENUINFONR = 16 then
      STDMENUINFO := STDMENUINFOVALUE.FTOT_RATE;
    end if;
    if STDMENUINFONR = 17 then
      STDMENUINFO := STDMENUINFOVALUE.TOT_RATE_NET;
    end if;
    if STDMENUINFONR = 18 then
      STDMENUINFO := STDMENUINFOVALUE.FTOT_RATE_NET;
    end if;
    if STDMENUINFONR = 19 then
      STDMENUINFO := STDMENUINFOVALUE.FCTOT_RATE_NET;
    end if;
    if STDMENUINFONR = 20 then
      STDMENUINFO := STDMENUINFOVALUE.TOT_RATE_VAT;
    end if;
    if STDMENUINFONR = 21 then
      STDMENUINFO := STDMENUINFOVALUE.FTOT_RATE_VAT;
    end if;
    if STDMENUINFONR = 22 then
      STDMENUINFO := STDMENUINFOVALUE.FCTOT_RATE_VAT;
    end if;
    if STDMENUINFONR = 23 then
      STDMENUINFO := STDMENUINFOVALUE.ORG_RATE;
    end if;
    if STDMENUINFONR = 24 then
      STDMENUINFO := STDMENUINFOVALUE.FCORG_RATE;
    end if;
    if STDMENUINFONR = 25 then
      STDMENUINFO := STDMENUINFOVALUE.TOT_ORG_RATE;
    end if;
    if STDMENUINFONR = 26 then
      STDMENUINFO := STDMENUINFOVALUE.FTOT_ORG_RATE;
    end if;
     if STDMENUINFONR = 27 then
      STDMENUINFO := STDMENUINFOVALUE.YEMN_COSTPRICE;
    end if;
    if STDMENUINFONR = 28 then
      STDMENUINFO := STDMENUINFOVALUE.FYEMN_COSTPRICE;
    end if;
    
	end loop;
	exception
      when no_data_found then null;
      when others then null;
	close STDMENUINFOCURSOR;
  end;
  
  return STDMENUINFO;
end V8_FNC_CCM_STD_MENU;
/


CREATE OR REPLACE FUNCTION V8_FNC_YEBR_INFOS
(
 /* LS 28/11/2012
   FC = Format with currency. Currency of main booking. Position of currency specified in global settings. eg 9,999.99 Euro
   F  = Format without currency as specified in global settings. eg. 9,999.99
   All output of function is translated according to provided wlanid, if translation available.
   
1   TR_YEBR_NAME: Returns Item Name.
2   TR_YCIA_LONGDESC: Returns attribute of item.
3   RATEDESCRIPTIONIFRATEAMOUNT: Returns Item Rate Description if rate is present. If no rate amount or amount is 0 returns ''. 
4   FCRATE_DESCRIPTION: Returns Item Rate. If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
5   FCTOT_RATE_DESCRIPTION: Returns Total Item Rate (Item quantity * Rate). If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
6   FCORG_RATE_DESCRIPTION: Returns Item Original Rate. If Item included in package returns "Included in package". If no org. rate amount or org. amount is 0 returns Rate Description.
7   FCTOT_ORG_RATE_DESCRIPTION: Returns Total Item Original Rate (Item quantity * Org.Rate). If Item included in package returns "Included in package". If no org. rate amount or org. amount is 0 returns Rate Description.
8   YEBR_REBATE_PERC: Returns Rebate percentage. 
9   FCYEBR_REBATE_FLAT: Returns Total Rebate flat amount (rebate_unitprice * quantity).
10  FCYEBR_REBATE_UNITPRICE: Returns Rebate Unit price.
11  FCYEBR_REBATE: Returns rebate Unit price or percentage with % sign.
12  FCYEBR_REBATE_TOTAL: Returns total amount of rebate (flat or % calculation).
13  FCYEBR_TOTAL_NOREBATE : Returns Items original Rate - Total rebates. In fact this is the same as 4 FCRATE_DESCRIPTION with the exception that it will return
    0.00 Euro instead of ("Included in package" if FS is included, or '' if rate amount is 0). For me this one is obsolete. Left it as some installs might used it.
    I did not add a YEBR_TOTAL_NOREBATE nor FYEBR_TOTAL_NOREBATE for this field.
14  FCYEBR_REBATE_TR: Returns Rebate Unit price + "incl"(translated) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' (translated) if rebate is %.
15  FCYEBR_COSTPRICE: Returns Item cost price.
16  RATE_DESCRIPTION: Returns Item  Rate. If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
17  FRATE_DESCRIPTION: Returns Item  Rate. If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
18  RATE_NET_DESCRIPTION: Returns Item Net Rate. If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
19  FRATE_NET_DESCRIPTION: Returns Item Net Rate. If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
20  FCRATE_NET_DESCRIPTION: Returns Item Net Rate. If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
21  RATE_VAT_DESCRIPTION: Returns Item Rate Vat. If item included in package returns ''. If no rate amount or amount is 0 returns ''.
22  FRATE_VAT_DESCRIPTION: Returns Item Rate Vat. If item included in package returns ''. If no rate amount or amount is 0 returns ''.
23  FCRATE_VAT_DESCRIPTION: Returns Item Rate Vat. If item included in package returns ''. If no rate amount or amount is 0 returns ''.
24  TOT_RATE_DESCRIPTION: Returns Total Item Rate (Item quantity * Rate). If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
25  FTOT_RATE_DESCRIPTION: Returns Total Item Rate (Item quantity * Rate). If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
26  TOT_RATE_NET_DESCRIPTION: Returns Total Item Net Rate (Item quantity *  Net Rate). If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
27  FTOT_RATE_NET_DESCRIPTION: Returns Total Item Net Rate (Item quantity *  Net Rate). If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
28  FCTOT_RATE_NET_DESCRIPTION: Returns Total Item Net Rate (Item quantity * Net Rate). If Item included in package returns "Included in package". If no rate amount or amount is 0 returns Rate Description.
29  TOT_RATE_VAT_DESCRIPTION: Returns Total Item Rate Vat (Item quantity * Rate Vat).  If item included in package returns ''. If no rate amount or amount is 0 returns ''.
30  FTOT_RATE_VAT_DESCRIPTION: Returns Total Item Rate Vat (Item quantity * Rate Vat). If item included in package returns ''. If no rate amount or amount is 0 returns ''.
31  FCTOT_RATE_VAT_DESCRIPTION: Returns Total Item Rate Vat(Item quantity * Rate Vat). If item included in package returns ''. If no rate amount or amount is 0 returns ''.
32  ORG_RATE_DESCRIPTION: Returns Item Original Rate. If Item included in package returns "Included in package". If no org. rate amount or org. amount is 0 returns Rate Description.
33  FORG_RATE_DESCRIPTION: Returns Item Original Rate. If Item included in package returns "Included in package". If no org. rate amount or org. amount is 0 returns Rate Description.
34  TOT_ORG_RATE_DESCRIPTION: Returns Total Item Original Rate (Item quantity * Org.Rate). If Item included in package returns "Included in package". If no org. rate amount or org. amount is 0 returns Rate Description.
35  FTOT_ORG_RATE_DESCRIPTION: Returns Total Item Original Rate (Item quantity * Org.Rate). If Item included in package returns "Included in package". If no org. rate amount or org. amount is 0 returns Rate Description.
36  YEBR_REBATE_FLAT: Returns Total Rebate flat amount (rebate_unitprice * quantity).
37  FYEBR_REBATE_FLAT: Returns Total Rebate flat amount (rebate_unitprice * quantity).
38  YEBR_REBATE_UNITPRICE: Returns Rebate Unit price.
39  FYEBR_REBATE_UNITPRICE: Returns Rebate Unit price.
40  FYEBR_REBATE: Returns rebate Unit price or percentage with % sign.
41  YEBR_REBATE_TOTAL: Returns total amount of rebate (flat or % calculation).
42  FYEBR_REBATE_TOTAL: Returns total amount of rebate (flat or % calculation).
43  YEBR_REBATE_TR: Returns Rebate Unit price + "incl"(translated) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' (translated) if rebate is %.
44  FYEBR_REBATE_TR: Returns Rebate Unit price + "incl"(translated) if rebate is flat amount. Returns Rebate percentage + '%' + 'Rebate' (translated) if rebate is %.
45  YEBR_COSTPRICE: Returns Item cost price.
46  FYEBR_COSTPRICE: Returns Item cost price.
   */
   
  YEBRID IN NUMBER,
  FIELDOPTIONNUMBER in number,
  WLANID IN NUMBER :=null
) RETURN VARCHAR2 AS
  ALL_YEBR VARCHAR2(200);

  CURSOR YEBR_CURSOR IS
  SELECT     
    YEBR.YEBR_ID,
    YEBR.YEBR_NAME,
    decode(YEBR_NAME, YCIT_LONGDESC, WTRL_Translate('YCIT', YCIT_ID, 'YCIT_LONGDESC', WLANID), WTRL_Translate('YEBR', YEBR_ID, 'YEBR_NAME', WLANID)) TR_YEBR_NAME,
  WTRL_Translate('YCIA', YEBR_YCIA_ID, 'YCIA_LONGDESC', WLANID) TR_YCIA_LONGDESC,
    trim(decode(nvl(YEBR_MANUALPRICE,0), 
        0, '', 
        Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID) )) RATEDESCRIPTIONIFRATEAMOUNT,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        yebr.YEBR_MANUALPRICE))) RATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_MANUALPRICE)))) FRATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_MANUALPRICE, zcur.ZCUR_SHORTDESC)))) FCRATE_DESCRIPTION,          
     trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        yebr.YEBR_PRICE_NET))) RATE_NET_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_PRICE_NET)))) FRATE_NET_DESCRIPTION,  
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_VALCURR_FORMATING(yebr.yebr_price_net, zcur.ZCUR_SHORTDESC)))) FCRATE_NET_DESCRIPTION, 
    trim(decode(yebr.YEBR_PKGINCL, 
        1, '',decode(nvl(YEBR_MANUALPRICE,0),0, '', yebr.YEBR_PRICE_VAT))) RATE_VAT_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL,1, '',decode(nvl(YEBR_MANUALPRICE,0),0, '', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_PRICE_VAT)))) FRATE_VAT_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL,1, '',decode(nvl(YEBR_MANUALPRICE,0),0, '', 
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_PRICE_VAT, zcur.ZCUR_SHORTDESC)))) FCRATE_VAT_DESCRIPTION, 
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_manualprice, yebr.yebr_starttime,yebr.yebr_endtime)))) TOT_RATE_DESCRIPTION,        
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_manualprice, yebr.yebr_starttime,yebr.yebr_endtime))))) FTOT_RATE_DESCRIPTION,        
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_manualprice, yebr.yebr_starttime,yebr.yebr_endtime), zcur.ZCUR_SHORTDESC)))) FCTOT_RATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_price_net, yebr.yebr_starttime,yebr.yebr_endtime)))) TOT_RATE_NET_DESCRIPTION,        
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_price_net, yebr.yebr_starttime,yebr.yebr_endtime))))) FTOT_RATE_NET_DESCRIPTION,        
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_price_net, yebr.yebr_starttime,yebr.yebr_endtime), zcur.ZCUR_SHORTDESC)))) FCTOT_RATE_NET_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL,1,'',decode(nvl(YEBR_MANUALPRICE,0),0,'', 
        yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_price_vat, yebr.yebr_starttime,yebr.yebr_endtime)))) TOT_RATE_VAT_DESCRIPTION,        
    trim(decode(yebr.YEBR_PKGINCL,1,'',decode(nvl(YEBR_MANUALPRICE,0),0,'',
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_price_vat, yebr.yebr_starttime,yebr.yebr_endtime))))) FTOT_RATE_VAT_DESCRIPTION,        
    trim(decode(yebr.YEBR_PKGINCL,1,'',decode(nvl(YEBR_MANUALPRICE,0),0,'',
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_price_vat,yebr.yebr_starttime,yebr.yebr_endtime), zcur.ZCUR_SHORTDESC)))) FCTOT_RATE_VAT_DESCRIPTION,       
    V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.yebr_manualprice, yebr.yebr_starttime,yebr.yebr_endtime)) TOT_AMOUNT,    
     trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_ORG_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ',yebr.YEBR_ORG_MANUALPRICE))) ORG_RATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_ORG_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_ORG_MANUALPRICE)))) FORG_RATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_ORG_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_ORG_MANUALPRICE, zcur.ZCUR_SHORTDESC)))) FCORG_RATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_ORG_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
       yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime)))) TOT_ORG_RATE_DESCRIPTION,
    trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_ORG_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_NUM_TO_CHAR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime))))) FTOT_ORG_RATE_DESCRIPTION,
     trim(decode(yebr.YEBR_PKGINCL, 
      1, V8_FNC_WRTR_Translate('incl. in package', WLANID),
      decode(nvl(YEBR_ORG_MANUALPRICE,0), 
        0, Wtrl_Translate('YCRD',YCIR.YCIR_YCRD_ID,'YCRD_LONGDESC',WLANID)||' ', 
        V8_FNC_VALCURR_FORMATING(yebr.YEBR_QUANTITY*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime), zcur.ZCUR_SHORTDESC)))) FCTOT_ORG_RATE_DESCRIPTION,                
    yebr.yebr_rebate_perc,
    yebr.yebr_rebate_flat yebr_rebate_flat,
    V8_FNC_NUM_TO_CHAR_FORMATING(yebr.yebr_rebate_flat) fyebr_rebate_flat,
    V8_FNC_VALCURR_FORMATING(yebr.yebr_rebate_flat, zcur.ZCUR_SHORTDESC) fcyebr_rebate_flat, 
    yebr.yebr_rebate_unitprice yebr_rebate_unitprice,
    V8_FNC_NUM_TO_CHAR_FORMATING(yebr.yebr_rebate_unitprice) fyebr_rebate_unitprice,  
    V8_FNC_VALCURR_FORMATING(yebr.yebr_rebate_unitprice, zcur.ZCUR_SHORTDESC) fcyebr_rebate_unitprice,
    decode(yebr_rebate_perc, 0, V8_FNC_NUM_TO_CHAR_FORMATING(yebr_rebate_unitprice), yebr_rebate_perc||'%') FYebr_Rebate,
    decode(yebr_rebate_perc, 0, V8_FNC_VALCURR_FORMATING(yebr_rebate_unitprice, zcur_shortdesc), yebr_rebate_perc||'%') FCYebr_Rebate, 
    decode(nvl(yebr_pkgincl, 0)+decode(nvl(yebr_org_manualprice, 0), 0, 1, 0)+decode(nvl(yebr_rebate_perc, 0)+nvl(yebr_rebate_flat, 0), 0,1,0), 0,
      decode(yebr_rebate_perc, 0, 
       yebr_rebate_unitprice||' '||V8_FNC_WRTR_Translate('incl', WLANID), 
       yebr_rebate_perc||'%'||' '||V8_FNC_WRTR_Translate('Rebate', WLANID))) Yebr_Rebate_Tr,  
    decode(nvl(yebr_pkgincl, 0)+decode(nvl(yebr_org_manualprice, 0), 0, 1, 0)+decode(nvl(yebr_rebate_perc, 0)+nvl(yebr_rebate_flat, 0), 0,1,0), 0,
      decode(yebr_rebate_perc, 0, 
       V8_FNC_NUM_TO_CHAR_FORMATING(yebr_rebate_unitprice)||' '||V8_FNC_WRTR_Translate('incl', WLANID), 
       yebr_rebate_perc||'%'||' '||V8_FNC_WRTR_Translate('Rebate', WLANID))) FYebr_Rebate_Tr,           
    decode(nvl(yebr_pkgincl, 0)+decode(nvl(yebr_org_manualprice, 0), 0, 1, 0)+decode(nvl(yebr_rebate_perc, 0)+nvl(yebr_rebate_flat, 0), 0,1,0), 0,
      decode(yebr_rebate_perc, 0, 
       V8_FNC_VALCURR_FORMATING(yebr_rebate_unitprice, zcur_shortdesc)||' '||V8_FNC_WRTR_Translate('incl', WLANID), 
       yebr_rebate_perc||'%'||' '||V8_FNC_WRTR_Translate('Rebate', WLANID))) FCYebr_Rebate_Tr, 
     decode(yebr_rebate_perc, 0, yebr_rebate_flat, 
      (yebr_rebate_perc* V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime))/100
     ) Yebr_Rebate_Total,
     V8_FNC_NUM_TO_CHAR_FORMATING(decode(yebr_rebate_perc, 0, yebr_rebate_flat, 
      (yebr_rebate_perc* V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime))/100
     )) FYebr_Rebate_Total,              
    V8_FNC_VALCURR_FORMATING(decode(yebr_rebate_perc, 0, yebr_rebate_flat, 
      (yebr_rebate_perc* V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime))/100
     ), zcur_shortdesc) FCYebr_Rebate_Total,
    V8_FNC_VALCURR_FORMATING(V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime)-
    decode(yebr_rebate_perc, 0, yebr_rebate_unitprice, 
      (yebr_rebate_perc*V8_FNC_CCM_RATEVALUE(yebr.yebr_perhour, yebr.YEBR_ORG_MANUALPRICE, yebr.yebr_starttime,yebr.yebr_endtime))/100
     ), zcur.ZCUR_SHORTDESC) FCYebr_Total_NoRebate,
   yebr_costprice YEBR_COSTPRICE,
     V8_FNC_NUM_TO_CHAR_FORMATING(yebr_costprice)FYEBR_COSTPRICE,
     V8_FNC_VALCURR_FORMATING(yebr_costprice, zcur_shortdesc) FCYEBR_COSTPRICE        
   FROM
    YEBR, YCIT, YCIR, YEVM,YBOM,ZCUR
  WHERE
    YEBR.YEBR_ID=YEBRID
 AND YEBR.YEBR_YCIT_ID=YCIT.YCIT_ID
 AND YEBR.YEBR_YCIR_ID=YCIR.YCIR_ID(+)
    AND YEBR.YEBR_YEVM_ID=YEVM.YEVM_ID
    AND YEVM.YEVM_YBOM_ID=YBOM.YBOM_ID
    AND YBOM.YBOM_BASE_ZCUR_ID=ZCUR.ZCUR_ID;

  YEBR_VALUE YEBR_CURSOR%ROWTYPE;

BEGIN
  BEGIN
    OPEN YEBR_CURSOR;
    LOOP
       FETCH YEBR_CURSOR
       INTO YEBr_VALUE;
    EXIT WHEN YEBR_CURSOR%NOTFOUND;
      IF ALL_YEBR IS NOT NULL THEN
        ALL_YEBR := ALL_YEBR||CHR(10); 
      END IF;
    IF FIELDOPTIONNUMBER=1 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.TR_YEBR_NAME);
   END IF;
    IF FIELDOPTIONNUMBER=2 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.TR_YCIA_LONGDESC);
    END IF;
    IF FIELDOPTIONNUMBER=3 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.RATEDESCRIPTIONIFRATEAMOUNT);
    END IF;
    IF FIELDOPTIONNUMBER=4 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCRATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=5 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCTOT_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=6 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCORG_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=7 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCTOT_ORG_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=8 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.yebr_rebate_perc);
    END IF;
    IF FIELDOPTIONNUMBER=9 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCyebr_rebate_flat);
    END IF;
    IF FIELDOPTIONNUMBER=10 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fcyebr_rebate_unitprice);
    END IF;
    IF FIELDOPTIONNUMBER=11 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fcyebr_rebate);
    END IF;
    IF FIELDOPTIONNUMBER=12 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fcyebr_rebate_total);
    END IF;
    IF FIELDOPTIONNUMBER=13 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fcyebr_total_norebate);
    END IF;
    IF FIELDOPTIONNUMBER=14 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fcyebr_rebate_tr);
    END IF;
    IF FIELDOPTIONNUMBER=15 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fcyebr_costprice);
    END IF;
    IF FIELDOPTIONNUMBER=16 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=17 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FRATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=18 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.RATE_NET_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=19 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FRATE_NET_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=20 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCRATE_NET_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=21 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.RATE_VAT_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=22 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FRATE_VAT_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=23 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCRATE_VAT_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=24 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.TOT_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=25 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FTOT_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=26 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.TOT_RATE_NET_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=27 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FTOT_RATE_NET_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=28 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCTOT_RATE_NET_DESCRIPTION);
    END IF;    
    IF FIELDOPTIONNUMBER=29 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.TOT_RATE_VAT_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=30 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FTOT_RATE_VAT_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=31 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FCTOT_RATE_VAT_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=32 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.ORG_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=33 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FORG_RATE_DESCRIPTION);
    END IF;    
    IF FIELDOPTIONNUMBER=34 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.TOT_ORG_RATE_DESCRIPTION);
    END IF;
    IF FIELDOPTIONNUMBER=35 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FTOT_ORG_RATE_DESCRIPTION);
    END IF;  
    IF FIELDOPTIONNUMBER=36 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.YEBR_REBATE_FLAT);
    END IF;
    IF FIELDOPTIONNUMBER=37 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.FYEBR_REBATE_FLAT);
    END IF; 
    IF FIELDOPTIONNUMBER=38 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.yebr_rebate_unitprice);
    END IF;
    IF FIELDOPTIONNUMBER=39 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fyebr_rebate_unitprice);
    END IF;
    IF FIELDOPTIONNUMBER=40 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fyebr_rebate);
    END IF;
    IF FIELDOPTIONNUMBER=41 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.yebr_rebate_total);
    END IF;
    IF FIELDOPTIONNUMBER=42 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fyebr_rebate_total);
    END IF;
    IF FIELDOPTIONNUMBER=43 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.yebr_rebate_tr);
    END IF;
    IF FIELDOPTIONNUMBER=44 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fyebr_rebate_tr);
    END IF;
    IF FIELDOPTIONNUMBER=45 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.yebr_costprice);
    END IF; 
    IF FIELDOPTIONNUMBER=46 then
      ALL_YEBR := ALL_YEBR||trim(YEBR_VALUE.fyebr_costprice);
    END IF;
   

    END LOOP;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN NULL;
      WHEN OTHERS THEN NULL;
    CLOSE YEBR_CURSOR;
  END;
  RETURN ALL_YEBR;
END V8_FNC_YEBR_INFOS;
/


create or replace view v8_sys_ydet_occ_vs as
  select
    ydet_suite_ycat_id
   ,ydet_date
   ,sum(defnoofrooms) defnoofrooms
   ,sum(tentnoofrooms) tentnoofrooms
   ,sum(defnoofroomsDayUse) defnoofroomsDayUse
   ,sum(tentnoofroomsDayUse) tentnoofroomsDayUse
   ,sum(defnoofroomsNotAssigned) defnoofroomsNotAssigned
   ,sum(tentnoofroomsNotAssigned) tentnoofroomsNotAssigned
   ,sum(defnoofroomsDayUseNotAssigned) defnoofroomsDayUseNotAssigned
   ,sum(tentnoofroomsDayUseNotAssigned) tentnoofroomsDayUseNotAssigned
from (
  select
    ydet_suite_ycat_id
   ,ydet_date
   ,sum(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) defnoofrooms
   ,sum(decode(yres_resstatus,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentnoofrooms
   ,sum(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) defnoofroomsDayUse
   ,sum(decode(yres_resstatus,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentnoofroomsDayUse
   ,sum(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0),0)) defnoofroomsNotAssigned
   ,sum(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentnoofroomsNotAssigned
   ,sum(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0),0)) defnoofroomsDayUseNotAssigned
   ,sum(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentnoofroomsDayUseNotAssigned
 from ydet,yres
 where
     ydet_yres_id=yres_id(+)
 and yres_resstatus<>3
 --and yres_exparrtime<>yres_expdeptime
 and nvl(ydet_sharenum,0)=0
 and ydet_suite_ycat_id is not null
 --and ydet_suite_yrms_id is null
 group by ydet_suite_ycat_id,ydet_date
union all
  select
    ydet_suite_ycat_id
   ,ydet_date
   ,max(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0)) defnoofrooms
   ,max(decode(yres_resstatus,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end)) tentnoofrooms
   ,max(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0)) defnoofroomsDayUse
   ,max(decode(yres_resstatus,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end)) tentnoofroomsDayUse
   ,max(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end,0),0)) defnoofroomsNotAssigned
   ,max(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,0,case when yres_exparrtime<>yres_expdeptime then ydet_noofrooms else 0 end),0)) tentnoofroomsNotAssigned
   ,max(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end,0),0)) defnoofroomsDayUseNotAssigned
   ,max(decode(ydet_suite_yrms_id,null,decode(yres_resstatus,1,0,case when yres_exparrtime=yres_expdeptime then ydet_noofrooms else 0 end),0)) tentnoofroomsDayUseNotAssigned
 from ydet,yres
 where
     ydet_yres_id=yres_id(+)
 and yres_resstatus<>3
 --and yres_exparrtime<>yres_expdeptime
 and ydet_sharenum<>0
 and ydet_suite_ycat_id is not null
 --and ydet_suite_yrms_id is null
 group by ydet_suite_ycat_id,ydet_date,ydet_sharenum
)
group by ydet_suite_ycat_id,ydet_date;


create or replace view v8_sys_vs_rms_occ as
  select ydet_Date day,yrss_suite_yrms_id yrms_id,yrms_ycat_id ycat_id
   ,max(decode(yres_resstatus,1,case when yres_exparrtime<>yres_expdeptime then 1 else 0 end,0)) defOcc
   ,max(decode(yres_resstatus,1,0,case when yres_exparrtime<>yres_expdeptime then 1 else 0 end)) tentOcc
   ,max(decode(yres_resstatus,1,case when yres_exparrtime=yres_expdeptime then 1 else 0 end,0)) defOccDayUse
   ,max(decode(yres_resstatus,1,0,case when yres_exparrtime=yres_expdeptime then 1 else 0 end)) tentOccDayUse
  from ydet,yres,yrss,yrms
  where ydet_yres_id=yres_id
  and yres_resstatus<>3
  and ydet_yrms_id=yrss_room_yrms_id
  and yrms_id=yrss_suite_yrms_id 
  group by ydet_Date,yrss_suite_yrms_id,yrms_ycat_id;

create or replace view v8_sys_vs_rms_occ_prt as
select day,ycat_id,sum(defOcc) defOcc,sum(tentOcc) tentOcc,sum(defOccDayUse) defOccDayUse,sum(tentOccDayUse) tentOccDayUse
from v8_sys_vs_rms_occ
group by day,ycat_id;


create or replace view v8_sys_oooForSuiteAvl as
select  yrms_id
       ,decode(nvl(yrsc_affectailability,1),1,0,1) oos
       ,decode(yrsc_affectailability,1,1,0) ooo
       ,yrst_fromtime fromtime
       ,trunc(yrst_untiltime) untiltime
from yrms,yrst,yrsc,ycat
where yrst_yrms_id=yrms_id
  and yrst_yrsc_id=yrsc_id
  and ycat_id=yrms_ycat_id
  and ycat_notinavl=0;

  
create or replace view v8_sys_nooftotalrooms_all_prt as
select wdat_date,yrms_ycat_id
   ,decode(ycat_max
          ,null,count(yrms_id)
          ,0,count(yrms_id)
            ,least(count(yrms_id),ycat_max)
          ) noofrooms
from yrms,wdat,ycat
where
      yrms_validfrom<= wdat_date
  and yrms_validuntil>= wdat_date
  --and yrms_suite=0
  and ycat_id=yrms_ycat_id
  and ycat_notinavl=0
group by wdat_date,yrms_ycat_id,ycat_max;


CREATE OR REPLACE FORCE VIEW V8_REP_QUEST_REP_SUM
AS 
SELECT
         ROUND(SUM(PercAnswerValue) / COUNT(1),2) AVGReached,
         ROUND(SUM(MaxAnswerValue),2) MaxAnswerSum,
         ROUND(SUM(AnswerValue),2) AnswerSum,
         qnrf_id,
         QNRF_GUEST_XCMS_ID XCMS_ID,
         QNRF_YRES_ID YRES_ID
FROM (
SELECT  qnrf_id,
        WQAN.WQAN_VALUE AnswerValue,
        (SELECT MAX(WQAN.WQAN_VALUE) FROM wqan WHERE WQAN.WQAN_WQUE_ID = wque_id) MaxAnswerValue,
        ((WQAN.WQAN_VALUE * 100) / decode((SELECT max(WQAN.WQAN_VALUE) FROM wqan WHERE WQAN.WQAN_WQUE_ID = wque_id),0,1,(SELECT max(WQAN.WQAN_VALUE) FROM wqan WHERE WQAN.WQAN_WQUE_ID = wque_id))) PercAnswerValue,
         QNRF.QNRF_ENTERED EnteredDate,
         QNRF.QNRF_GUEST_XCMS_ID,
         QNRF.QNRF_YRES_ID
  --,qnra.*, wqan.*, wque.*, wqnl.*, wqug.*, wqnr.*, qnrf.*
  FROM   qnra,
         qnrf,
         wqan,
         wque,
         wqnl,
         wqug,
         wqnr
 WHERE       WQAN.WQAN_ID = QNRA.QNRA_WQAN_ID
         AND WQUE.WQUE_ID = WQAN.WQAN_WQUE_ID
         AND WQNL.WQNL_ID = WQUE.WQUE_ID
         AND WQUG.WQUG_ID = WQNL.WQNL_WQUG_ID
         AND WQNR.WQNR_ID = WQNL.WQNL_WQNR_ID
         AND QNRF.QNRF_ID = QNRA.QNRA_QNRF_ID
    UNION
SELECT   qnrf_id,
             0 AnswerValue,
             0 MaxAnswerValue,
             0 PercAnswerValue,
             QNRF_ENTERED EnteredDate,
             QNRF_GUEST_XCMS_ID,
             QNRF_YRES_ID FROM qnrf qnrf_1
    WHERE NOT EXISTS (SELECT 1 FROM qnra WHERE qnra_qnrf_id = qnrf_1.qnrf_id)
         )
         GROUP BY qnrf_id,QNRF_GUEST_XCMS_ID,QNRF_YRES_ID;


CREATE OR REPLACE VIEW V8_AR_AINV_DETAILED1
AS 
select ainv.AINV_ID,
       AINV_POSTDATE, 
       AINV_PCDATE, 
       AINV_TRANSFER_DATE, 
       AINV_CLOSEDTIME, 
       ainv_db DebitAmount, 
       ainv_cr CreditAmount, 
       ainv_db-ainv_cr OpenBalance, 
       AINV_ADJUST_AINV_ID, 
       AINV_REALPOSTDATE, 
       AINV_MANUALPOST, 
       AINV_DESCRIPT, 
       AINV_TRANSFER_INFO, 
       AACC_ID, 
       AACC_NUMBER, 
       AACC_ATYP_ID, 
       AACC_OBSOLETE, 
       (select Decode(xcms1.XCMS_TYPE, 
                      1, xcms1.XCMS_NAME1||' '||xcms1.XCMS_NAME2||' '||xcms1.XCMS_NAME3, 
                      Trim( 
                           Decode(nvl(xcms1.XCMS_NAME1,''),'','',xcms1.XCMS_NAME1||Decode(nvl(xcms1.XCMS_NAME3,''),'','',',')||' ')|| 
                           Decode(nvl(xcms1.XCMS_NAME3,''),'','',xcms1.XCMS_NAME3||' ')|| 
                           xcms1.XCMS_NAME2 
                          ) 
                     ) 
          from XCMS xcms1 
         where xcms1.XCMS_ID (+) = AACC_XCMS_ID) AccountName ,
         nvl((select count(ACPL.ACPL_ACMP_ID) from acpl where ACPL.ACPL_AINV_ID=AINV.AINV_ID),0) COUNT_ACMP,
         nvl((select max(ACPL.ACPL_ACMP_ID) from acpl where ACPL.ACPL_AINV_ID=AINV.AINV_ID),0) MAX_ACMP_ID,
         nvl((select sum(ainv_s.ainv_amount) from ainv ainv_s, acpl where ainv_s.ainv_id=acpl.acpl_ainv_id and acpl_acmp_id in (select ACPL.ACPL_ACMP_ID from acpl where ACPL.ACPL_AINV_ID=AINV.AINV_ID)),1) ACMP_AMOUNT,
         nvl((select sum(ainv_s.ainv_amount) from ainv ainv_s where ainv_s.ainv_adjust_ainv_id=ainv.ainv_id),0) AINV_ADJUST_AMOUNT
  from AACC,
       ainv 
 where AACC_ID (+) = AINV_AACC_ID
 and (nvl(ainv_db,0)-nvl(ainv_cr,0)<>0
      or not exists (select 1 from aipl where aipl_credit_ainv_id=ainv.ainv_id)
     );


CREATE OR REPLACE VIEW V8_REP_HSK_INFOS_ALLDAY
AS 
SELECT 
'FREE ROOM' STATUS, 
1 V_STATUS, 
-1 YDET_ID, 
-1 YRES_ID, 
-1 YRES_CALCSTATUS, 
WDAT.WDAT_DATE, 
V8_sys_fideliodate FIDELIO_DATE, 
YRMS.YRMS_ID, 
YRMS.YRMS_YCAT_ID, 
YRMS.YRMS_SHORTDESC, 
YRMS.YRMS_LONGDESC, 
YRMS.YRMS_DISPLORDER, 
YRMS.YRMS_VALIDFROM, 
YRMS.YRMS_VALIDUNTIL, 
YRMS.YRMS_YHKS_ID, 
(SELECT YHKS.YHKS_SHORTDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_SHORTDESC, 
(SELECT YHKS.YHKS_LONGDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_LONGDESC, 
(SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_CLEAN, 
DECODE(NVL((SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID),0),0,1) IS_DIRTY,
DECODE((SELECT COUNT(YRST_ID) FROM YRST WHERE YRMS.YRMS_ID = YRST.YRST_YRMS_ID AND WDAT.WDAT_DATE >= YRST.YRST_FROMTIME AND WDAT.WDAT_DATE < YRST.YRST_UNTILTIME),1,1,0) IS_OOO,  
YRMS.YRMS_NOOFPERSONS, 
YRMS.YRMS_FLOOR, 
YRMS.YRMS_HSK_OCCUPIED, 
YRMS.YRMS_MAX_BEDS, 
YRMS.YRMS_FREETEXT, 
YRMS.YRMS_L_STATUS, 
YRMS.YRMS_HSK_NOOFPERSONS, 
YRMS.YRMS_DEFAULTCREDITS, 
(SELECT YCAT.YCAT_SHORTDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SHORTDESC, 
(SELECT YCAT.YCAT_LONGDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_LONGDESC, 
(SELECT YCAT.YCAT_DISPLORDER FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_DISPLORDER, 
(SELECT YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAD_ID,
(SELECT YCAT.YCAT_SUITE FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SUITE,
TO_CHAR(NULL) NAME, 
TO_DATE(NULL) YRES_EXPARRTIME, 
TO_DATE(NULL) YRES_EXPDEPTIME, 
TO_CHAR(NULL) YRES_EXPARRHOUR, 
TO_CHAR(NULL) YRES_EXPDEPHOUR, 
to_number(null) CHECKED_OUT,
TO_NUMBER(NULL) YRES_RESSTATUS, 
TO_CHAR(NULL) YRES_FREETEXT1, 
TO_CHAR(NULL) YRES_FREETEXT2, 
0 YDET_NOOFROOMS, 
0 YDET_ADULTNO, 
0 TOTALKIDS, 
0 TOTALKIDS_G1, 
0 TOTALKIDS_G2, 
0 TOTALKIDS_G3, 
TO_NUMBER(NULL) YBLH_ID, 
TO_CHAR(NULL) YBLH_SHORTDESC, 
TO_NUMBER(NULL) CONF_RATE, 
TO_NUMBER(NULL) CALC_PRICE, 
TO_NUMBER(NULL) YRCH_ID, 
TO_CHAR(NULL) YRCH_SHORTDESC, 
TO_NUMBER(NULL) YRCH_COMPSTATUS, 
TO_CHAR(NULL) ALL_YCLN_NAMES, 
TO_NUMBER(NULL) YDET_XCMA_ID, 
TO_CHAR(NULL) XCMA_SHORTDESC, 
TO_NUMBER(NULL) IS_RES_SPECIALS, 
TO_CHAR(NULL) RES_SPECIALS_SHORT, 
TO_CHAR(NULL) RES_SPECIALS_LONG, 
TO_NUMBER(NULL) IS_XCMS_SPECIAL, 
TO_CHAR(NULL) XCMS_SPECIALS_SHORT, 
TO_CHAR(NULL) XCMS_SPECIALS_LONG, 
TO_CHAR(NULL) PACKAGES_SHORT, 
TO_CHAR(NULL) PACKAGES_LONG, 
TO_CHAR(NULL) ALL_PACKAGES_SHORT, 
TO_CHAR(NULL) ALL_PACKAGES_LONG, 
TO_NUMBER(NULL) IS_YRES_NOTES_NON_VABLE, 
TO_CHAR(NULL) YRES_NOTES_NON_VABLE_SHORT, 
TO_CHAR(NULL) YRES_NOTES_NON_VABLE_LONG, 
TO_NUMBER(NULL) IS_YRES_NOTES_VABLE, 
TO_CHAR(NULL) YRES_NOTES_VABLE_SHORT, 
TO_CHAR(NULL) YRES_NOTES_VABLE_LONG, 
TO_NUMBER(NULL) IS_XCMS_NOTES_NON_VABLE, 
TO_CHAR(NULL) XCMS_NOTES_NON_VABLE_SHORT, 
TO_CHAR(NULL) XCMS_NOTES_NON_VABLE_LONG, 
TO_NUMBER(NULL) IS_XCMS_NOTES_VABLE, 
TO_CHAR(NULL) XCMS_NOTES_VABLE_SHORT, 
TO_CHAR(NULL) XCMS_NOTES_VABLE_LONG, 
TO_CHAR(NULL) SHARER_NAME, 
TO_CHAR(NULL) MULTI_GUEST_NAMES, 
TO_CHAR(NULL) ALL_AMENITIES, 
TO_CHAR(NULL) AMENITIES, 
TO_CHAR(NULL) YDET_AMENITIES_SHORT, 
TO_CHAR(NULL) YDET_AMENITIES_LONG, 
TO_CHAR(NULL) CHILDREN_SHORT, 
TO_CHAR(NULL) CHILDREN_LONG,
TO_CHAR(NULL) VIP_SHORTDESC,
to_date(NULL) ARRIVAL_ROOM
FROM 
WDAT,YRMS--,YDET 
WHERE WDAT.WDAT_DATE BETWEEN YRMS.YRMS_VALIDFROM AND YRMS.YRMS_VALIDUNTIL 
--AND WDAT.WDAT_DATE BETWEEN V8_sys_fideliodate AND V8_sys_fideliodate 
AND NOT EXISTS (SELECT 1 FROM YDET YDET_S1,YRES WHERE YDET_S1.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (0,1,2,3,4,5,6,7,8,9) AND YDET_S1.YDET_YRMS_ID=YRMS.YRMS_ID AND YDET_S1.YDET_DATE=WDAT.WDAT_DATE)
and not exists (select 1 from Ydet,yres where yres.yres_expdeptime=ydet.ydet_date+1 and ydet.ydet_date=wdat.wdat_date and ydet.ydet_yrms_id=yrms.yrms_id and yres.yres_calcstatus in (0,1,2,3,4,5,6,7,8,9))
and not exists (select 1 from yres,ydet where yres.yres_expdeptime=wdat.wdat_date and ydet.ydet_date+1=yres.yres_expdeptime and yres.yres_resstatus<>3 and ydet.ydet_yrms_id=yrms.yrms_id and ydet.ydet_yres_id=yres.yres_id and yres.yres_calcstatus in (0,1,2,3,4,5,6,7,8,9))
UNION ALL 
SELECT 
'OCC ROOM' STATUS, 
2 V_STATUS, 
YDET.YDET_ID, 
YDET.YDET_YRES_ID YRES_ID, 
RES.YRES_CALCSTATUS YRES_CALCSTATUS, 
WDAT.WDAT_DATE, 
V8_sys_fideliodate FIDELIO_DATE, 
YRMS.YRMS_ID, 
YRMS.YRMS_YCAT_ID, 
YRMS.YRMS_SHORTDESC, 
YRMS.YRMS_LONGDESC, 
YRMS.YRMS_DISPLORDER, 
YRMS.YRMS_VALIDFROM, 
YRMS.YRMS_VALIDUNTIL, 
YRMS.YRMS_YHKS_ID, 
(SELECT YHKS.YHKS_SHORTDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_SHORTDESC, 
(SELECT YHKS.YHKS_LONGDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_LONGDESC, 
(SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_CLEAN, 
DECODE(NVL((SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID),0),0,1) IS_DIRTY,
DECODE((SELECT COUNT(YRST_ID) FROM YRST WHERE YRMS.YRMS_ID = YRST.YRST_YRMS_ID AND WDAT.WDAT_DATE >= YRST.YRST_FROMTIME AND WDAT.WDAT_DATE < YRST.YRST_UNTILTIME),1,1,0) IS_OOO,  
YRMS.YRMS_NOOFPERSONS, 
YRMS.YRMS_FLOOR, 
YRMS.YRMS_HSK_OCCUPIED, 
YRMS.YRMS_MAX_BEDS, 
YRMS.YRMS_FREETEXT, 
YRMS.YRMS_L_STATUS, 
YRMS.YRMS_HSK_NOOFPERSONS, 
YRMS.YRMS_DEFAULTCREDITS, 
(SELECT YCAT.YCAT_SHORTDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SHORTDESC, 
(SELECT YCAT.YCAT_LONGDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_LONGDESC, 
(SELECT YCAT.YCAT_DISPLORDER FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_DISPLORDER, 
(SELECT YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAD_ID,
(SELECT YCAT.YCAT_SUITE FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SUITE,
RES.NAME, 
RES.YRES_EXPARRTIME, 
RES.YRES_EXPDEPTIME, 
RES.YRES_EXPARRHOUR, 
RES.YRES_EXPDEPHOUR, 
nvl2(res.yres_actdeptime,1,0) CHECKED_OUT,
RES.YRES_RESSTATUS, 
RES.YRES_FREETEXT1, 
RES.YRES_FREETEXT2, 
RES.YDET_NOOFROOMS, 
RES.YDET_ADULTNO, 
RES.TOTALKIDS, 
RES.TOTALKIDS_G1, 
RES.TOTALKIDS_G2, 
RES.TOTALKIDS_G3, 
RES.YBLH_ID, 
RES.YBLH_SHORTDESC, 
RES.CONF_RATE, 
RES.CALC_PRICE, 
RES.YRCH_ID, 
RES.YRCH_SHORTDESC, 
RES.YRCH_COMPSTATUS, 
RES.ALL_YCLN_NAMES, 
RES.YDET_XCMA_ID, 
RES.XCMA_SHORTDESC, 
RES.IS_RES_SPECIALS, 
RES.RES_SPECIALS_SHORT, 
RES.RES_SPECIALS_LONG, 
RES.IS_XCMS_SPECIAL, 
RES.XCMS_SPECIALS_SHORT, 
RES.XCMS_SPECIALS_LONG, 
RES.PACKAGES_SHORT, 
RES.PACKAGES_LONG, 
RES.ALL_PACKAGES_SHORT, 
RES.ALL_PACKAGES_LONG, 
RES.IS_YRES_NOTES_NON_VABLE, 
RES.YRES_NOTES_NON_VABLE_SHORT, 
RES.YRES_NOTES_NON_VABLE_LONG, 
RES.IS_YRES_NOTES_VABLE, 
RES.YRES_NOTES_VABLE_SHORT, 
RES.YRES_NOTES_VABLE_LONG, 
RES.IS_XCMS_NOTES_NON_VABLE, 
RES.XCMS_NOTES_NON_VABLE_SHORT, 
RES.XCMS_NOTES_NON_VABLE_LONG, 
RES.IS_XCMS_NOTES_VABLE, 
RES.XCMS_NOTES_VABLE_SHORT, 
RES.XCMS_NOTES_VABLE_LONG, 
RES.SHARER_NAME, 
RES.MULTI_GUEST_NAMES, 
RES.ALL_AMENITIES, 
RES.AMENITIES, 
RES.YDET_AMENITIES_SHORT, 
RES.YDET_AMENITIES_LONG, 
RES.CHILDREN_SHORT, 
RES.CHILDREN_LONG,
RES.VIP_SHORTDESC ,
(select min(ydet_s.ydet_date) from ydet ydet_s where ydet_s.ydet_yres_id=Res.yres_id and ydet_s.ydet_yrms_id=yrms.yrms_id) ARRIVAL_ROOM
FROM 
WDAT,YRMS,YDET,V8_REP_YRES_INFOS RES 
WHERE WDAT.WDAT_DATE BETWEEN YRMS.YRMS_VALIDFROM AND YRMS.YRMS_VALIDUNTIL 
AND WDAT.WDAT_DATE=YDET.YDET_DATE 
AND YDET.YDET_YRES_ID=RES.YRES_ID 
--AND WDAT.WDAT_DATE BETWEEN V8_sys_fideliodate AND V8_sys_fideliodate 
AND YRMS.YRMS_ID=YDET.YDET_YRMS_ID 
and ydet.ydet_date<>RES.YRES_EXPARRTIME
AND (SELECT YRES.YRES_RESSTATUS FROM YRES WHERE YRES.YRES_ID=YDET.YDET_YRES_ID)<>3 
and EXISTS (SELECT 1 FROM YDET YDET_2,YRES WHERE YDET_2.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (0,1,2,3) AND YDET_2.YDET_ID=YDET.YDET_ID)
UNION ALL 
SELECT 
'DEP EXP' STATUS, 
4 V_STATUS, 
YDET.YDET_ID, 
YDET.YDET_YRES_ID YRES_ID, 
RES.YRES_CALCSTATUS YRES_CALCSTATUS, 
WDAT.WDAT_DATE, 
V8_sys_fideliodate FIDELIO_DATE, 
YRMS.YRMS_ID, 
YRMS.YRMS_YCAT_ID,
YRMS.YRMS_SHORTDESC, 
YRMS.YRMS_LONGDESC, 
YRMS.YRMS_DISPLORDER, 
YRMS.YRMS_VALIDFROM, 
YRMS.YRMS_VALIDUNTIL, 
YRMS.YRMS_YHKS_ID, 
(SELECT YHKS.YHKS_SHORTDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_SHORTDESC, 
(SELECT YHKS.YHKS_LONGDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_LONGDESC, 
(SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_CLEAN, 
DECODE(NVL((SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID),0),0,1) IS_DIRTY, 
DECODE((SELECT COUNT(YRST_ID) FROM YRST WHERE YRMS.YRMS_ID = YRST.YRST_YRMS_ID AND WDAT.WDAT_DATE >= YRST.YRST_FROMTIME AND WDAT.WDAT_DATE < YRST.YRST_UNTILTIME),1,1,0) IS_OOO, 
YRMS.YRMS_NOOFPERSONS, 
YRMS.YRMS_FLOOR, 
YRMS.YRMS_HSK_OCCUPIED, 
YRMS.YRMS_MAX_BEDS, 
YRMS.YRMS_FREETEXT, 
YRMS.YRMS_L_STATUS, 
YRMS.YRMS_HSK_NOOFPERSONS, 
YRMS.YRMS_DEFAULTCREDITS, 
(SELECT YCAT.YCAT_SHORTDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SHORTDESC, 
(SELECT YCAT.YCAT_LONGDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_LONGDESC, 
(SELECT YCAT.YCAT_DISPLORDER FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_DISPLORDER, 
(SELECT YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAD_ID,
(SELECT YCAT.YCAT_SUITE FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SUITE,
RES.NAME, 
RES.YRES_EXPARRTIME, 
RES.YRES_EXPDEPTIME, 
RES.YRES_EXPARRHOUR, 
RES.YRES_EXPDEPHOUR, 
nvl2(res.yres_actdeptime,1,0) CHECKED_OUT,
RES.YRES_RESSTATUS, 
RES.YRES_FREETEXT1, 
RES.YRES_FREETEXT2, 
RES.YDET_NOOFROOMS, 
RES.YDET_ADULTNO, 
RES.TOTALKIDS, 
RES.TOTALKIDS_G1, 
RES.TOTALKIDS_G2, 
RES.TOTALKIDS_G3, 
RES.YBLH_ID, 
RES.YBLH_SHORTDESC, 
RES.CONF_RATE, 
RES.CALC_PRICE, 
RES.YRCH_ID, 
RES.YRCH_SHORTDESC, 
RES.YRCH_COMPSTATUS, 
RES.ALL_YCLN_NAMES, 
RES.YDET_XCMA_ID, 
RES.XCMA_SHORTDESC, 
RES.IS_RES_SPECIALS, 
RES.RES_SPECIALS_SHORT, 
RES.RES_SPECIALS_LONG, 
RES.IS_XCMS_SPECIAL, 
RES.XCMS_SPECIALS_SHORT, 
RES.XCMS_SPECIALS_LONG, 
RES.PACKAGES_SHORT, 
RES.PACKAGES_LONG, 
RES.ALL_PACKAGES_SHORT, 
RES.ALL_PACKAGES_LONG, 
RES.IS_YRES_NOTES_NON_VABLE, 
RES.YRES_NOTES_NON_VABLE_SHORT, 
RES.YRES_NOTES_NON_VABLE_LONG, 
RES.IS_YRES_NOTES_VABLE, 
RES.YRES_NOTES_VABLE_SHORT, 
RES.YRES_NOTES_VABLE_LONG, 
RES.IS_XCMS_NOTES_NON_VABLE, 
RES.XCMS_NOTES_NON_VABLE_SHORT, 
RES.XCMS_NOTES_NON_VABLE_LONG, 
RES.IS_XCMS_NOTES_VABLE, 
RES.XCMS_NOTES_VABLE_SHORT, 
RES.XCMS_NOTES_VABLE_LONG, 
RES.SHARER_NAME, 
RES.MULTI_GUEST_NAMES, 
RES.ALL_AMENITIES, 
RES.AMENITIES, 
RES.YDET_AMENITIES_SHORT, 
RES.YDET_AMENITIES_LONG, 
RES.CHILDREN_SHORT, 
RES.CHILDREN_LONG,
RES.VIP_SHORTDESC ,
(select min(ydet_s.ydet_date) from ydet ydet_s where ydet_s.ydet_yres_id=Res.yres_id and ydet_s.ydet_yrms_id=yrms.yrms_id) ARRIVAL_ROOM
FROM 
WDAT,YRMS,YDET,V8_REP_YRES_INFOS RES 
WHERE WDAT.WDAT_DATE BETWEEN YRMS.YRMS_VALIDFROM AND YRMS.YRMS_VALIDUNTIL 
AND WDAT.WDAT_DATE=YDET.YDET_DATE+1 
AND YDET.YDET_YRES_ID=RES.YRES_ID 
AND (RES.YRES_EXPDEPTIME)=WDAT.WDAT_DATE 
--AND WDAT.WDAT_DATE BETWEEN V8_sys_fideliodate AND V8_sys_fideliodate 
AND YRMS.YRMS_ID=YDET.YDET_YRMS_ID 
AND (SELECT YRES.YRES_RESSTATUS FROM YRES WHERE YRES.YRES_ID=YDET.YDET_YRES_ID)<>3 
AND EXISTS (SELECT 1 FROM YDET YDET_2,YRES WHERE YDET_2.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (0,1,2,3,4,5,6) AND YDET_2.YDET_ID=YDET.YDET_ID) 
UNION ALL
SELECT 
'ARR EXP' STATUS, 
5 V_STATUS, 
YDET.YDET_ID, 
YDET.YDET_YRES_ID YRES_ID, 
RES.YRES_CALCSTATUS YRES_CALCSTATUS, 
WDAT.WDAT_DATE, 
V8_sys_fideliodate FIDELIO_DATE, 
YRMS.YRMS_ID, 
YRMS.YRMS_YCAT_ID, 
YRMS.YRMS_SHORTDESC, 
YRMS.YRMS_LONGDESC, 
YRMS.YRMS_DISPLORDER, 
YRMS.YRMS_VALIDFROM, 
YRMS.YRMS_VALIDUNTIL, 
YRMS.YRMS_YHKS_ID, 
(SELECT YHKS.YHKS_SHORTDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_SHORTDESC, 
(SELECT YHKS.YHKS_LONGDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_LONGDESC, 
(SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_CLEAN, 
DECODE(NVL((SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID),0),0,1) IS_DIRTY, 
DECODE((SELECT COUNT(YRST_ID) FROM YRST WHERE YRMS.YRMS_ID = YRST.YRST_YRMS_ID AND WDAT.WDAT_DATE >= YRST.YRST_FROMTIME AND WDAT.WDAT_DATE < YRST.YRST_UNTILTIME),1,1,0) IS_OOO, 
YRMS.YRMS_NOOFPERSONS, 
YRMS.YRMS_FLOOR, 
YRMS.YRMS_HSK_OCCUPIED, 
YRMS.YRMS_MAX_BEDS, 
YRMS.YRMS_FREETEXT, 
YRMS.YRMS_L_STATUS, 
YRMS.YRMS_HSK_NOOFPERSONS, 
YRMS.YRMS_DEFAULTCREDITS, 
(SELECT YCAT.YCAT_SHORTDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SHORTDESC, 
(SELECT YCAT.YCAT_LONGDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_LONGDESC, 
(SELECT YCAT.YCAT_DISPLORDER FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_DISPLORDER, 
(SELECT YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAD_ID,
(SELECT YCAT.YCAT_SUITE FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SUITE,
RES.NAME, 
RES.YRES_EXPARRTIME, 
RES.YRES_EXPDEPTIME, 
RES.YRES_EXPARRHOUR, 
RES.YRES_EXPDEPHOUR, 
nvl2(res.yres_actdeptime,1,0) CHECKED_OUT,
RES.YRES_RESSTATUS, 
RES.YRES_FREETEXT1, 
RES.YRES_FREETEXT2, 
RES.YDET_NOOFROOMS, 
RES.YDET_ADULTNO, 
RES.TOTALKIDS, 
RES.TOTALKIDS_G1, 
RES.TOTALKIDS_G2, 
RES.TOTALKIDS_G3, 
RES.YBLH_ID, 
RES.YBLH_SHORTDESC, 
RES.CONF_RATE, 
RES.CALC_PRICE, 
RES.YRCH_ID, 
RES.YRCH_SHORTDESC, 
RES.YRCH_COMPSTATUS, 
RES.ALL_YCLN_NAMES, 
RES.YDET_XCMA_ID, 
RES.XCMA_SHORTDESC, 
RES.IS_RES_SPECIALS, 
RES.RES_SPECIALS_SHORT, 
RES.RES_SPECIALS_LONG, 
RES.IS_XCMS_SPECIAL, 
RES.XCMS_SPECIALS_SHORT, 
RES.XCMS_SPECIALS_LONG, 
RES.PACKAGES_SHORT, 
RES.PACKAGES_LONG, 
RES.ALL_PACKAGES_SHORT, 
RES.ALL_PACKAGES_LONG, 
RES.IS_YRES_NOTES_NON_VABLE, 
RES.YRES_NOTES_NON_VABLE_SHORT, 
RES.YRES_NOTES_NON_VABLE_LONG, 
RES.IS_YRES_NOTES_VABLE, 
RES.YRES_NOTES_VABLE_SHORT, 
RES.YRES_NOTES_VABLE_LONG, 
RES.IS_XCMS_NOTES_NON_VABLE, 
RES.XCMS_NOTES_NON_VABLE_SHORT, 
RES.XCMS_NOTES_NON_VABLE_LONG, 
RES.IS_XCMS_NOTES_VABLE, 
RES.XCMS_NOTES_VABLE_SHORT, 
RES.XCMS_NOTES_VABLE_LONG, 
RES.SHARER_NAME, 
RES.MULTI_GUEST_NAMES, 
RES.ALL_AMENITIES, 
RES.AMENITIES, 
RES.YDET_AMENITIES_SHORT, 
RES.YDET_AMENITIES_LONG, 
RES.CHILDREN_SHORT, 
RES.CHILDREN_LONG,
RES.VIP_SHORTDESC ,
(select min(ydet_s.ydet_date) from ydet ydet_s where ydet_s.ydet_yres_id=Res.yres_id and ydet_s.ydet_yrms_id=yrms.yrms_id) ARRIVAL_ROOM
FROM 
WDAT,YRMS,YDET,V8_REP_YRES_INFOS RES 
WHERE WDAT.WDAT_DATE BETWEEN YRMS.YRMS_VALIDFROM AND YRMS.YRMS_VALIDUNTIL 
AND WDAT.WDAT_DATE=YDET.YDET_DATE
and wdat.wdat_date=RES.YRES_EXPARRTIME
AND YDET.YDET_YRES_ID=RES.YRES_ID 
--AND WDAT.WDAT_DATE BETWEEN V8_sys_fideliodate AND V8_sys_fideliodate 
AND YRMS.YRMS_ID=YDET.YDET_YRMS_ID 
AND (SELECT YRES.YRES_RESSTATUS FROM YRES WHERE YRES.YRES_ID=YDET.YDET_YRES_ID)<>3 
AND EXISTS (SELECT 1 FROM YDET YDET_2,YRES WHERE YDET_2.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (0,1,2) AND YDET.YDET_ID=YDET_2.YDET_ID)
AND NOT EXISTS (SELECT 1 FROM YDET YDET_2,YRES WHERE YDET_2.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (7,8,9) AND YDET_2.YDET_ID=YDET.YDET_ID)
UNION ALL
SELECT 
'DAY USE' STATUS, 
7 V_STATUS, 
YDET.YDET_ID, 
YDET.YDET_YRES_ID YRES_ID, 
RES.YRES_CALCSTATUS YRES_CALCSTATUS, 
WDAT.WDAT_DATE, 
V8_sys_fideliodate FIDELIO_DATE, 
YRMS.YRMS_ID, 
YRMS.YRMS_YCAT_ID, 
YRMS.YRMS_SHORTDESC, 
YRMS.YRMS_LONGDESC, 
YRMS.YRMS_DISPLORDER, 
YRMS.YRMS_VALIDFROM, 
YRMS.YRMS_VALIDUNTIL, 
YRMS.YRMS_YHKS_ID, 
(SELECT YHKS.YHKS_SHORTDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_SHORTDESC, 
(SELECT YHKS.YHKS_LONGDESC FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_LONGDESC, 
(SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID) YHKS_CLEAN, 
DECODE(NVL((SELECT YHKS.YHKS_CLEAN FROM YHKS WHERE YHKS.YHKS_ID=YRMS.YRMS_YHKS_ID),0),0,1) IS_DIRTY, 
DECODE((SELECT COUNT(YRST_ID) FROM YRST WHERE YRMS.YRMS_ID = YRST.YRST_YRMS_ID AND WDAT.WDAT_DATE >= YRST.YRST_FROMTIME AND WDAT.WDAT_DATE < YRST.YRST_UNTILTIME),1,1,0) IS_OOO, 
YRMS.YRMS_NOOFPERSONS, 
YRMS.YRMS_FLOOR, 
YRMS.YRMS_HSK_OCCUPIED, 
YRMS.YRMS_MAX_BEDS, 
YRMS.YRMS_FREETEXT, 
YRMS.YRMS_L_STATUS, 
YRMS.YRMS_HSK_NOOFPERSONS, 
YRMS.YRMS_DEFAULTCREDITS, 
(SELECT YCAT.YCAT_SHORTDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SHORTDESC, 
(SELECT YCAT.YCAT_LONGDESC FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_LONGDESC, 
(SELECT YCAT.YCAT_DISPLORDER FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_DISPLORDER, 
(SELECT YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAD_ID,
(SELECT YCAT.YCAT_SUITE FROM YCAT WHERE YCAT.YCAT_ID=YRMS.YRMS_YCAT_ID) YCAT_SUITE,
RES.NAME, 
RES.YRES_EXPARRTIME, 
RES.YRES_EXPDEPTIME, 
RES.YRES_EXPARRHOUR, 
RES.YRES_EXPDEPHOUR, 
nvl2(res.yres_actdeptime,1,0) CHECKED_OUT,
RES.YRES_RESSTATUS, 
RES.YRES_FREETEXT1, 
RES.YRES_FREETEXT2, 
RES.YDET_NOOFROOMS, 
RES.YDET_ADULTNO, 
RES.TOTALKIDS, 
RES.TOTALKIDS_G1, 
RES.TOTALKIDS_G2, 
RES.TOTALKIDS_G3, 
RES.YBLH_ID, 
RES.YBLH_SHORTDESC, 
RES.CONF_RATE, 
RES.CALC_PRICE, 
RES.YRCH_ID, 
RES.YRCH_SHORTDESC, 
RES.YRCH_COMPSTATUS, 
RES.ALL_YCLN_NAMES, 
RES.YDET_XCMA_ID, 
RES.XCMA_SHORTDESC, 
RES.IS_RES_SPECIALS, 
RES.RES_SPECIALS_SHORT, 
RES.RES_SPECIALS_LONG, 
RES.IS_XCMS_SPECIAL, 
RES.XCMS_SPECIALS_SHORT, 
RES.XCMS_SPECIALS_LONG, 
RES.PACKAGES_SHORT, 
RES.PACKAGES_LONG, 
RES.ALL_PACKAGES_SHORT, 
RES.ALL_PACKAGES_LONG, 
RES.IS_YRES_NOTES_NON_VABLE, 
RES.YRES_NOTES_NON_VABLE_SHORT, 
RES.YRES_NOTES_NON_VABLE_LONG, 
RES.IS_YRES_NOTES_VABLE, 
RES.YRES_NOTES_VABLE_SHORT, 
RES.YRES_NOTES_VABLE_LONG, 
RES.IS_XCMS_NOTES_NON_VABLE, 
RES.XCMS_NOTES_NON_VABLE_SHORT, 
RES.XCMS_NOTES_NON_VABLE_LONG, 
RES.IS_XCMS_NOTES_VABLE, 
RES.XCMS_NOTES_VABLE_SHORT, 
RES.XCMS_NOTES_VABLE_LONG, 
RES.SHARER_NAME, 
RES.MULTI_GUEST_NAMES, 
RES.ALL_AMENITIES, 
RES.AMENITIES, 
RES.YDET_AMENITIES_SHORT, 
RES.YDET_AMENITIES_LONG, 
RES.CHILDREN_SHORT, 
RES.CHILDREN_LONG,
RES.VIP_SHORTDESC ,
(select min(ydet_s.ydet_date) from ydet ydet_s where ydet_s.ydet_yres_id=Res.yres_id and ydet_s.ydet_yrms_id=yrms.yrms_id) ARRIVAL_ROOM
FROM 
WDAT,YRMS,YDET,V8_REP_YRES_INFOS RES 
WHERE WDAT.WDAT_DATE BETWEEN YRMS.YRMS_VALIDFROM AND YRMS.YRMS_VALIDUNTIL 
AND WDAT.WDAT_DATE=YDET.YDET_DATE
AND YDET.YDET_YRES_ID=RES.YRES_ID 
--AND WDAT.WDAT_DATE BETWEEN V8_sys_fideliodate AND V8_sys_fideliodate 
AND YRMS.YRMS_ID=YDET.YDET_YRMS_ID 
AND (SELECT YRES.YRES_RESSTATUS FROM YRES WHERE YRES.YRES_ID=YDET.YDET_YRES_ID)<>3 
AND NOT EXISTS (SELECT 1 FROM YDET YDET_2,YRES WHERE YDET_2.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (1,2) AND YDET_2.YDET_ID=YDET.YDET_ID)
AND EXISTS (SELECT 1 FROM YDET YDET_2,YRES WHERE YDET_2.YDET_YRES_ID=YRES.YRES_ID AND YRES.YRES_RESSTATUS<>3 AND YRES.YRES_CALCSTATUS IN (7,8,9) AND YDET_2.YDET_ID=YDET.YDET_ID);


CREATE OR REPLACE VIEW V8_REP_HSK_RESERVATIONS
AS 
SELECT
(select yrms.YRMS_ID from yrms,ydet where ydet.YDET_YRMS_ID=yrms.YRMS_ID and ydet.ydet_id=yres.yres_current_ydet_id) YRMS_ID,
YRES.YRES_ID,
xcms.XCMS_ID GUESTNUMBER,
decode(xcms_type,
      1,xcms_name1||' '||xcms_name2||' '||xcms_name3
        ,trim(
         decode(nvl(xcms_name1,''),'','',xcms_name1||decode(nvl(xcms_name3,''),'','',',')||' ')||
         decode(nvl(xcms_name3,''),'','',xcms_name3||' ')||
         xcms_name2
          )
       ) GuestName,
xcms.XCMS_NAME1 LASTNAME,
xcms.XCMS_NAME3 FIRSTNAME,
xcid.XCID_ADDRGREET TITLE,
xpda.xpda_shortdesc VIP,
(select yrms.YRMS_SHORTDESC from yrms,ydet where ydet.YDET_YRMS_ID=yrms.YRMS_ID and ydet.ydet_id=yres.yres_current_ydet_id) ROOMNUMBER,
(select ycat.YCAT_SHORTDESC from yrms,ydet,ycat where ydet.YDET_YRMS_ID=yrms.YRMS_ID and ydet.ydet_id=yres.yres_current_ydet_id and yrms.YRMS_YCAT_ID=ycat.YCAT_ID) ROOMTYPE,
yres.YRES_EXPARRTIME ARRIVALDATE,
yres.YRES_EXPDEPTIME DEPARTUREDATE,
(select ydet.YDET_SHARENUM from ydet where ydet.ydet_id=yres.yres_current_ydet_id) SHARENUMBER,
yres.YRES_NOPOST NOPOSTFLAG,
(select ydet.YDET_ADULTNO from ydet where ydet.ydet_id=yres.yres_current_ydet_id) ADULTS,
nvl((Select sum(yrgl_number) from yrgl,ydet where yrgl.YRGL_YDET_ID=ydet.YDET_ID and ydet.YDET_ID=yres.YRES_CURRENT_YDET_ID ),0) TOTALKIDS,
decode(yres.YRES_ACTARRTIME,Null  ,0  ,decode(yres.YRES_ACTDEPTIME,Null,1,0)) GUESTRESSTATUS,
decode(yres_actdeptime
       ,null,decode(yres_actarrtime
                     ,null,decode(YRES_EXPARRTIME-d.WGBS_DATEVALUE
                                       ,0,'exparr'
                                         ,'future')
                         ,decode(YRES_EXPDEPTIME-d.WGBS_DATEVALUE
                                 ,0,'expdep'
                                   ,'inhouse')
                         )
        ,'departed') occupiedstatus,
decode(yres_actdeptime
       ,null,decode(yres_actarrtime
                     ,null,decode(YRES_EXPARRTIME-d.WGBS_DATEVALUE
                                       ,0,'0'
                                         ,'1')
                         ,decode(YRES_EXPDEPTIME-d.WGBS_DATEVALUE
                                 ,0,'2'
                                   ,'3')
                         )
        ,'4') occupiedstatusnum,
V8_sys_fideliodate Today,
(select min(ydet_date) from ydet where ydet.ydet_yres_id=yres.yres_id and ydet.ydet_yrms_id=(select yrms.YRMS_ID from yrms,ydet where ydet.YDET_YRMS_ID=yrms.YRMS_ID and ydet.ydet_id=yres.yres_current_ydet_id)) ARRIVAL_ROOM
FROM
yres,
xcms,
xpal,
xpda,
xcid,
wlan,
fideliodate d
WHERE
yres.YRES_XCMS_ID=xcms.XCMS_ID and
xpal.XPAL_XPAC_ID(+) = 11 and
xpal.XPAL_XCMS_ID(+)=xcms.XCMS_ID and
xpda.xpda_id(+) = xpal.XPAL_XPDA_ID and
xcms.XCMS_ID=xcid.XCID_ID(+) and
xcms.XCMS_WLAN_ID=wlan.WLAN_ID(+) and
(yres.YRES_ACTDEPTIMENULL = 1 or trunc(yres.yres_actdeptime)=d.WGBS_DATEVALUE)
and (YRES.YRES_EXPARRTIME)<=d.WGBS_DATEVALUE
and yres.YRES_RESSTATUS<>3
and (YRES.YRES_EXPDEPTIME)>(select * from HistoryFidelioDate );


CREATE OR REPLACE VIEW V8_REP_DATES
AS 
SELECT 
  WDAT.WDAT_DATE FIDELIODATE, 
  LAST_DAY(ADD_MONTHS(WDAT.WDAT_DATE,-1))+1 FIRST_OF_MONTH, 
  TO_DATE((TO_CHAR(('1.01.'||TO_CHAR(WDAT.WDAT_DATE,'YYYY')))),'DD.MM.YYYY') FIRST_OF_YEAR, 
  LAST_DAY(WDAT.WDAT_DATE) LAST_OF_MONTH,
  (CASE WHEN (TO_DATE(TO_CHAR(((TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartDay')))||'.'||(TO_CHAR(WDAT.WDAT_DATE,'MM'))||'.'||TO_CHAR(WDAT.WDAT_DATE,'YYYY'))),'DD.MM.YYYY')<=WDAT.WDAT_DATE) THEN (TO_DATE(TO_CHAR(((TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartDay')))||'.'||(TO_CHAR(WDAT.WDAT_DATE,'MM'))||'.'||TO_CHAR(WDAT.WDAT_DATE,'YYYY'))),'DD.MM.YYYY')) ELSE (TO_DATE(TO_CHAR(((TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartDay')))||'.'||(TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-1),'MM'))||'.'||TO_CHAR(WDAT.WDAT_DATE,'YYYY'))),'DD.MM.YYYY')) END) FI_FIRST_OF_MONTH, 
  (CASE WHEN (TO_DATE(TO_CHAR(((TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartDay')))||'.'||(TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartMonth')))||'.'||TO_CHAR(WDAT.WDAT_DATE,'YYYY'))),'DD.MM.YYYY')<=WDAT.WDAT_DATE) THEN (TO_DATE(TO_CHAR(((TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartDay')))||'.'||(TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartMonth')))||'.'||TO_CHAR(WDAT.WDAT_DATE,'YYYY'))),'DD.MM.YYYY')) ELSE (TO_DATE(TO_CHAR(((TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartDay')))||'.'||(TO_CHAR((SELECT wuss_value FROM WUSS WHERE wuss_name= 'FiscalYearStartMonth')))||'.'||TO_CHAR(ADD_MONTHS(WDAT.WDAT_DATE,-12),'YYYY'))),'DD.MM.YYYY')) END) FI_FIRST_OF_YEAR 
  FROM 
  WDAT
;


CREATE OR REPLACE VIEW V8_REP_YRES_INFOS
AS 
SELECT
  YRES.YRES_ID,
  '*'||YRES.YRES_ID||'*' BARCODE39_YRES_ID,
  YRES.yres_calcstatus,
  nvl((select sum(nvl(decode(zpos_cdt,5,-zpos_unitprice*zpos_quantity,zpos_grossunitprice*zpos_quantity),0)) from zpos where zpos.ZPOS_YRES_ID = yres.YRES_ID),0) Balance,  
  YRES.YRES_CURRENT_YDET_ID YDET_ID,
  YRES.YRES_CRSRESNR YRES_CRSRESNR, 
  V8_sys_fideliodate FIDELIODATUM,
  (SELECT MIN(WDAT.WDAT_DATE) FROM WDAT) MIN_WDAT_DATE,
  (SELECT MAX(WDAT.WDAT_DATE) FROM WDAT) MAX_WDAT_DATE,
  YRES.YRES_EXPARRTIME YRES_EXPARRTIME,
  YRES.YRES_ACTARRTIME YRES_ACTARRTIME,
  YRES.YRES_EXPDEPTIME YRES_EXPDEPTIME,
  YRES.YRES_ACTDEPTIME YRES_ACTDEPTIME,
  YRES.YRES_ORGDEPTIME YRES_ORGDEPTIME,
  TRUNC(YRES.YRES_ORGDEPTIME) YRES_ORGDEPTIME_DATE,
  (CASE WHEN TRUNC(YRES.YRES_ORGDEPTIME)>TRUNC(YRES.YRES_ACTDEPTIME) THEN 1 ELSE 0 END) IS_EARLY_DEPARTURE,
  YRES.YRES_YGCO_ID YRES_YGCO_ID,
  (SELECT YGCO.YGCO_SHORTDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID) YGCO_SHORTDESC,
  (SELECT YGCO.YGCO_LONGDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID) YGCO_LONGDESC,
  (YRES.YRES_EXPDEPTIME) - (YRES.YRES_EXPARRTIME) Nights,
  (CASE WHEN YRES.YRES_CALCSTATUS in (1,2,15) THEN 1 ELSE 0 END) ARRIVAL_TODAY,
  (CASE WHEN YRES.YRES_CALCSTATUS in (2,8) THEN 1 ELSE 0 END) CHECKED_IN_TODAY,
  (CASE WHEN YRES.YRES_CALCSTATUS in (2,3,8) THEN 1 ELSE 0 END) CHECKED_IN,
  (CASE WHEN YRES.YRES_CALCSTATUS in (4,5,16) THEN 1 ELSE 0 END) DEPARTURE_TODAY,
  (CASE WHEN YRES.YRES_CALCSTATUS in (5,16) THEN 1 ELSE 0 END) CHECKED_OUT_TODAY,
  (CASE WHEN YRES.YRES_CALCSTATUS in (5,6,9,16) THEN 1 ELSE 0 END)  CHECKED_OUT,
  (CASE WHEN YRES.YRES_CALCSTATUS in (2,3,4,8) THEN 1 ELSE 0 END) INHOUSE,
  TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24:MI') YRES_EXPARRHOUR,
  TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24:MI') YRES_EXPDEPHOUR,
  DECODE(YRES.YRES_ACTARRTIME,NULL,DECODE(TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24'),'00',TO_CHAR(NULL),TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24:MI')),DECODE(TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24'),'00',TO_CHAR(NULL),TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24:MI'))) ARR_DEP_HOUR,
  NVL(DECODE(YRES.YRES_ACTARRTIME,NULL,DECODE(TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24'),'00',TO_CHAR(NULL),1),DECODE(TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24'),'00',TO_CHAR(NULL),2)),0) IS_ARR_DEP_HOUR,
  YRES.YRES_RESSTATUS YRES_RESSTATUS,
  NVL(YRES.YRES_NOAVAILREASON,0) YRES_NOAVAILREASON,
  YRES.YRES_WALKIN YRES_WALKIN,
  YRES.YRES_FREETEXT1 YRES_FREETEXT1,
  YRES.YRES_FREETEXT2 YRES_FREETEXT2,
  NVL(YRES.YRES_HASSHARE,0) YRES_HASSHARE,
  YRES.YRES_SHARETYPE YRES_SHARETYPE,
  (SELECT YDET.YDET_SHARENUM FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_SHARENUM,
  (DECODE(NVL((SELECT YDET.YDET_SHARENUM FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0),0,(SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),(CASE WHEN (YRES.YRES_HASSHARE=1 AND YRES.YRES_ID=(SELECT MIN(ydet_2.YDET_YRES_ID) FROM YDET,YDET ydet_2 WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND YDET.YDET_SHARENUM=ydet_2.YDET_SHARENUM GROUP BY ydet_2.YDET_SHARENUM)) THEN (SELECT AVG(ydet_2.YDET_NOOFROOMS) FROM YDET,YDET ydet_2 WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND YDET.YDET_SHARENUM=ydet_2.YDET_SHARENUM GROUP BY ydet_2.YDET_SHARENUM) ELSE 0 END))) YDET_NOOFROOMS,
  (SELECT YDET.YDET_ADULTNO FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_ADULTNO,
  NVL((SELECT SUM(yrgl_number) FROM YRGL,YDET WHERE YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID ),0) TOTALKIDS,
  NVL((SELECT SUM(yrgl_number) FROM YRGL,XCAC,YDET WHERE YRGL.YRGL_XCAC_ID=XCAC.XCAC_ID AND YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND NVL(XCAC.XCAC_REPORT_GROUP,1)=1 ),0) TOTALKIDS_G1,
  NVL((SELECT SUM(yrgl_number) FROM YRGL,XCAC,YDET WHERE YRGL.YRGL_XCAC_ID=XCAC.XCAC_ID AND YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND XCAC.XCAC_REPORT_GROUP=2 ),0) TOTALKIDS_G2,
  NVL((SELECT SUM(yrgl_number) FROM YRGL,XCAC,YDET WHERE YRGL.YRGL_XCAC_ID=XCAC.XCAC_ID AND YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND XCAC.XCAC_REPORT_GROUP=3 ),0) TOTALKIDS_G3,    
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_YRMS_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YRMS_SHORTDESC,
  (SELECT YDET.YDET_YCAT_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_YCAT_ID,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YCAT_SHORTDESC,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YCAT_LONGDESC,
  (SELECT YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YCAT_SUM_ROOMTYPE_YCAD_ID,
  (SELECT YCAD.YCAD_SHORTDESC FROM YCAT,YCAD,YDET WHERE YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID=YCAD.YCAD_ID AND YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YCAD_SHORTDESC,
  (SELECT YCAD.YCAD_LONGDESC FROM YCAT,YCAD,YDET WHERE YCAT.YCAT_SUM_ROOMTYPE_YCAD_ID=YCAD.YCAD_ID AND YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YCAD_LONGDESC,
  (SELECT YDET.YDET_YCAT_ID_RATE FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_YCAT_ID_RATE,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YCAT_SHORTDESC_RATE,
  (SELECT YBLH.YBLH_ID FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YBLH_ID,
  (SELECT YBLH.YBLH_SHORTDESC FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YBLH_SHORTDESC,
  (SELECT YBLH.YBLH_LONGDESC FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YBLH_LONGDESC,  
  NVL((SELECT YDET.YDET_RATEAMOUNT FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) YDET_RATEAMOUNT,
  NVL((SELECT YDET.YDET_RATEDIFF FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) YDET_RATEDIFF,
  NVL((SELECT YDET.YDET_RATEDISCOUNTPERC FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) YDET_RATEDISCOUNTPERC,
  (SELECT NVL(YDET.YDET_RATEMANUAL,0) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_RATEMANUAL,
  (SELECT (((DECODE(YDET.ydet_ratemanual,NULL,YDET.ydet_rateamount,YDET.ydet_ratemanual)+NVL(YDET.ydet_ratediff,0))*((100-NVL(YDET.ydet_ratediscountperc,0))/100))-  NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CONF_RATE,
  ((SELECT ((DECODE(YDET.ydet_ratemanual,NULL,YDET.ydet_rateamount,YDET.ydet_ratemanual)+NVL(YDET.ydet_ratediff,0)) * (100-NVL(YDET.ydet_ratediscountperc,0)))/100 FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CALC_PRICE,
  (SELECT (NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) ADD_ON_PACKAGES,
  NVL((SELECT SUM(YFXC.YFXC_AMOUNT*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) TOTAL_FIX_CHARGES,
  (SELECT YDET.YDET_YRCH_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YRCH_ID,
  (SELECT YRCH.YRCH_SHORTDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YRCH_SHORTDESC,
  NVL((SELECT YRCH.YRCH_COMPSTATUS FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) YRCH_COMPSTATUS,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN 1 ELSE 0 END) UNSPLITTED_GROUP_RES,
  YRES.YRES_XCMS_ID YRES_XCMS_ID,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=YRES.YRES_XCMS_ID) GUEST_NAME,
  (SELECT v8_rep_name.NAME3 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=YRES.YRES_XCMS_ID) GUEST_GREET_NAME,
  (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) YRES_GROUP_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=YRES.YRES_XCMS_ID) END) NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_WLAN_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=YRES.YRES_XCMS_ID) END) WLAN_ID,
  (SELECT WLAN.WLAN_SHORTDESC FROM WLAN WHERE WLAN.WLAN_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_WLAN_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=YRES.YRES_XCMS_ID) END)) WLAN_SHORTDESC,
  UPPER((CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=YRES.YRES_XCMS_ID) END)) UPPER_NAME,  
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT v8_rep_name.NAME3 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=YRES.YRES_XCMS_ID) END) NAME_GREET,
  (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_GROUP_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_ID FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_ID,
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name,YCLN,YDET WHERE v8_rep_name.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) COMPANY_NAME,
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name,YCLN,YDET WHERE v8_rep_name.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) TRAVELAGENT_NAME,
  (SELECT XCMS_ID FROM XCMS,YCLN,YDET WHERE XCMS.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) TRAVELAGENT_ID,
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name,YCLN,YDET WHERE v8_rep_name.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=3 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) SOURCE_NAME,
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name,YCLN,YDET WHERE v8_rep_name.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=4 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BOOKER_NAME,
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name,YCLN,YDET WHERE v8_rep_name.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=5 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) VENDOR_NAME,
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name,YCLN,YDET WHERE v8_rep_name.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=6 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) MPLANER_NAME,
  V8_Fnc_Ycln_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,1) ALL_YCLN_NAMES,
  (SELECT YDET.YDET_XCMA_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YDET_XCMA_ID,
  (SELECT XCMA.XCMA_SHORTDESC FROM XCMA,YDET WHERE XCMA.XCMA_ID=YDET.YDET_XCMA_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) XCMA_SHORTDESC,
  (SELECT YRAS.yras_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.yral_ydet_id = YDET.ydet_id AND YRAL.yral_yras_id = YRAS.yras_id AND YRAL.yral_yrac_id = YRAC.yrac_id AND YRAC.yrac_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1) SOURCE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.yral_ydet_id = YDET.ydet_id AND YRAL.yral_yras_id = YRAS.yras_id AND YRAL.yral_yrac_id = YRAC.yrac_id AND YRAC.yrac_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1) SOURCE_SHORTDESC,
  (SELECT YRAS.yras_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.yral_ydet_id = YDET.ydet_id AND YRAL.yral_yras_id = YRAS.yras_id AND YRAL.yral_yrac_id = YRAC.yrac_id AND YRAC.yrac_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1) CHANNEL_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.yral_ydet_id = YDET.ydet_id AND YRAL.yral_yras_id = YRAS.yras_id AND YRAL.yral_yrac_id = YRAC.yrac_id AND YRAC.yrac_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1) CHANNEL_SHORTDESC,
  (SELECT COUNT(yres_s.YRES_ID) FROM YRES yres_s WHERE yres_s.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND yres_s.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND yres_s.YRES_RESSTATUS<>3) NO_OF_PREV_STAYS,
  (SELECT COUNT(YDET.YDET_ID) FROM YDET,YRES yres_s WHERE YDET.YDET_YRES_ID=yres_s.YRES_ID AND yres_s.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND yres_s.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND yres_s.YRES_RESSTATUS<>3) NO_OF_PREV_ROOM_NIGHTS,
  (SELECT XPDA.XPDA_ID FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=YRES.YRES_XCMS_ID) VIP_ID,
  (SELECT XPDA.XPDA_SHORTDESC FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=YRES.YRES_XCMS_ID) VIP_SHORTDESC,
  (SELECT XPDA.XPDA_LONGDESC FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=YRES.YRES_XCMS_ID) VIP_LONGDESCDESC,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN 1 ELSE 0 END) EXP_ROOM_MOVE_TODAY,
  (CASE WHEN YRES.YRES_YRMS_ID=(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) AND YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) THEN 1 ELSE 0 END) ROOM_MOVE_DONE_TODAY,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate) TODAY_ROOM_MOVE_TO_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate) TODAY_ROOM_MOVE_TO,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN YRES.YRES_YRMS_ID ELSE (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ID,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN (SELECT YRMS.YRMS_SHORTDESC FROM YRMS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID) ELSE (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM,
  (SELECT MAX(TRUNC(yres_s.YRES_ACTARRTIME)) FROM YRES yres_s WHERE yres_s.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND yres_s.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND yres_s.YRES_RESSTATUS<>3) LAST_ARRIVAL,
  (SELECT MAX(TRUNC(yres_s.YRES_ACTDEPTIME)) FROM YRES yres_s WHERE yres_s.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND yres_s.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND yres_s.YRES_RESSTATUS<>3) LAST_DEPARTURE,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET,YRES yres_s2 WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID and YDET.YDET_ID=yres_s2.YRES_CURRENT_YDET_ID and yres_s2.YRES_XCMS_ID=YRES.YRES_XCMS_ID and TRUNC(yres_s2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(yres_s.YRES_ACTDEPTIME)) FROM YRES yres_s WHERE yres_s.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND yres_s.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND yres_s.YRES_RESSTATUS<>3) and rownum=1) LAST_YRMS_SHORTDESC,
  (SELECT YRCH.YRCH_SHORTDESC FROM YRCH,YDET,YRES yres_s2 WHERE YRCH.YRCH_ID =YDET.YDET_YRCH_ID and YDET.YDET_ID=yres_s2.YRES_CURRENT_YDET_ID and yres_s2.YRES_XCMS_ID=YRES.YRES_XCMS_ID and TRUNC(yres_s2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(yres_s.YRES_ACTDEPTIME)) FROM YRES yres_s WHERE yres_s.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND yres_s.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND yres_s.YRES_RESSTATUS<>3) and rownum=1) LAST_YRCH_SHORTDESC,  
  YRES.YRES_VACANT YRES_VACANT,
  YRES.YRES_OPTION YRES_OPTION,
  (SELECT MAX(ycxl_2.YCXL_ID) FROM YCXL ycxl_2 WHERE ycxl_2.YCXL_YRES_ID=YRES.YRES_ID AND ycxl_2.YCXL_CANCELORREINSTATE=1) MAX_YCXL_ID,
  (SELECT ycxl_3.YCXL_NUMBER FROM YCXL ycxl_3 WHERE ycxl_3.YCXL_ID=(SELECT MAX(ycxl_2.YCXL_ID) FROM YCXL ycxl_2 WHERE ycxl_2.YCXL_YRES_ID=YRES.YRES_ID AND ycxl_2.YCXL_CANCELORREINSTATE=1)) YCXL_NUMBER,
  (SELECT YCXL.YCXL_DATE FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(ycxl_2.YCXL_ID) FROM YCXL ycxl_2 WHERE ycxl_2.YCXL_YRES_ID=YRES.YRES_ID AND ycxl_2.YCXL_CANCELORREINSTATE=1)) YCXL_DATE,
  (SELECT YCXL.YCXL_REASON FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(ycxl_2.YCXL_ID) FROM YCXL ycxl_2 WHERE ycxl_2.YCXL_YRES_ID=YRES.YRES_ID AND ycxl_2.YCXL_CANCELORREINSTATE=1)) YCXL_REASON,
  (SELECT TRUNC(YCXL.YCXL_PCDATE) FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(ycxl_2.YCXL_ID) FROM YCXL ycxl_2 WHERE ycxl_2.YCXL_YRES_ID=YRES.YRES_ID AND ycxl_2.YCXL_CANCELORREINSTATE=1)) YCXL_PCDATE,
  (SELECT TO_CHAR(YCXL.YCXL_PCDATE,'HH24:MI') FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(ycxl_2.YCXL_ID) FROM YCXL ycxl_2 WHERE ycxl_2.YCXL_YRES_ID=YRES.YRES_ID AND ycxl_2.YCXL_CANCELORREINSTATE=1)) YCXL_PCTIME,
  (SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NULL) ACT_YWLT_ID,
  DECODE(NVL((SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NULL),0),0,0,1) IS_WAITLIST,
  (SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NOT NULL) OLD_YWLT_ID,
  DECODE(NVL((SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NOT NULL),0),0,0,1) IS_2NDTIME_WAITLIST,
  (SELECT YWLP.YWLP_LONGDESC FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(ywlt_2.YWLT_ID) FROM YWLT ywlt_2 WHERE ywlt_2.YWLT_YRES_ID=YRES.YRES_ID AND ywlt_2.YWLT_ACCEPTEDDATE IS NULL)))  ACT_YWLP_LONGDESC,
  (SELECT YWLP.YWLP_PRIORITY FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(ywlt_2.YWLT_ID) FROM YWLT ywlt_2 WHERE ywlt_2.YWLT_YRES_ID=YRES.YRES_ID AND ywlt_2.YWLT_ACCEPTEDDATE IS NULL)))  ACT_YWLP_PRIORITY,
  (SELECT XCOM.XCOM_VALUE FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(ywlt_2.YWLT_ID) FROM YWLT ywlt_2 WHERE ywlt_2.YWLT_YRES_ID=YRES.YRES_ID AND ywlt_2.YWLT_ACCEPTEDDATE IS NULL)))  ACT_YWLT_XCOM_VALUE,
  (SELECT XCMT.XCMT_LONGDESC FROM XCMT WHERE XCMT.XCMT_ID=(SELECT XCOM.XCOM_XCMT_ID FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(ywlt_2.YWLT_ID) FROM YWLT ywlt_2 WHERE ywlt_2.YWLT_YRES_ID=YRES.YRES_ID AND ywlt_2.YWLT_ACCEPTEDDATE IS NULL)))) ACT_YWLT_XCMT_LONGDESC,
  (SELECT XCMT.XCMT_TYPE FROM XCMT WHERE XCMT.XCMT_ID=(SELECT XCOM.XCOM_XCMT_ID FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(ywlt_2.YWLT_ID) FROM YWLT ywlt_2 WHERE ywlt_2.YWLT_YRES_ID=YRES.YRES_ID AND ywlt_2.YWLT_ACCEPTEDDATE IS NULL)))) ACT_YWLT_XCMT_TYPE,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END) XCOU_ID,
  (SELECT XCOU.XCOU_CUSTSHORTDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)) XCOU_CUSTSHORTDESC,
  (SELECT XCOU.XCOU_LONGDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)) XCOU_LONGDESC,
  (SELECT XCID.XCID_BIRTHTIME FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID) XCID_BIRTHTIME,
  (SELECT SUM(YDEP.YDEP_AMOUNT_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID) YDEP_DEPOSIT_REQUESTED,
  (SELECT MAX(YDEP.YDEP_DUEDATE_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID) YDEP_DEPOSIT_LIMIT,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_PAID,
  (SELECT MAX(ZPOS.ZPOS_POSTDATE) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_POSTDATE,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=2 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_TRANSFERED,
  (SELECT MAX(ZLSM.ZLSM_DATE) FROM ZLSM, ZPOS WHERE ZLSM.ZLSM_ZPOS_ID=ZPOS.ZPOS_ID AND ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=2 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_TRANSFERDATE,
  (SELECT YQUE.YQUE_ID FROM YQUE WHERE YQUE.YQUE_YRES_ID=YRES.YRES_ID) YQUE_ID,
  nvl2((SELECT YQUE.YQUE_ID FROM YQUE WHERE YQUE.YQUE_YRES_ID=YRES.YRES_ID),1,0) IS_QUEUE,
  (SELECT YQUE.YQUE_Q_DATE FROM YQUE WHERE YQUE.YQUE_YRES_ID=YRES.YRES_ID) YQUE_Q_DATE,
  V8_Fnc_Yrft_Rep_Infos(YRES.YRES_ID,1,1) YRES_YRFT_SHORT,
  V8_Fnc_Yrft_Rep_Infos(YRES.YRES_ID,1,2) YRES_YRFT_LONG,
  V8_Fnc_Yrft_Rep_Infos(YRES.YRES_XCMS_ID,2,1) XCMS_YRFT_SHORT,
  V8_Fnc_Yrft_Rep_Infos(YRES.YRES_XCMS_ID,2,2) XCMS_YRFT_LONG,
  DECODE(V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),18,1,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18)),NULL,0,1) IS_RES_SPECIALS,
  V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),18,1,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18)) RES_SPECIALS_SHORT,
  V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),18,2,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=18)) RES_SPECIALS_LONG,
  DECODE(V8_Fnc_Xpda_Rep_Infos(YRES.YRES_XCMS_ID,18,1,0),NULL,0,1) IS_XCMS_SPECIAL,
  V8_Fnc_Xpda_Rep_Infos(YRES.YRES_XCMS_ID,18,1,0) XCMS_SPECIALS_SHORT,
  V8_Fnc_Xpda_Rep_Infos(YRES.YRES_XCMS_ID,18,2,0) XCMS_SPECIALS_LONG,
  DECODE(V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=17),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),17,1,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=17)),NULL,0,1) IS_RES_NEWS,
  V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=17),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),17,1,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=17)) RES_NEWS_SHORT,
  V8_Fnc_Xpda_Rep_Infos((DECODE((SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=17),0,YRES.YRES_XCMS_ID,1,YRES.YRES_ID,2,YRES.YRES_CURRENT_YDET_ID)),17,2,(SELECT XPAC.XPAC_RESORDET FROM XPAC WHERE XPAC.XPAC_ID=17)) RES_NEWS_LONG,
  DECODE(V8_Fnc_Xpda_Rep_Infos(YRES.YRES_XCMS_ID,17,1,0),NULL,0,1) IS_XCMS_NEWS,
  V8_Fnc_Xpda_Rep_Infos(YRES.YRES_XCMS_ID,17,1,0) XCMS_NEWS_SHORT,
  V8_Fnc_Xpda_Rep_Infos(YRES.YRES_XCMS_ID,17,2,0) XCMS_NEWS_LONG,
  V8_Fnc_Ypal_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,1) PACKAGES_SHORT,
  V8_Fnc_Ypal_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,2) PACKAGES_LONG,
  V8_Fnc_Ypal_Rep_Infos2(YRES.YRES_ID,1) ALL_PACKAGES_SHORT,
  V8_Fnc_Ypal_Rep_Infos2(YRES.YRES_ID,2) ALL_PACKAGES_LONG,
  DECODE(V8_Fnc_Xcno_Rep_Infos(YRES.YRES_ID,'YRES',1,0),NULL,0,1) IS_YRES_NOTES_NON_VABLE,  
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_ID,'YRES',1,0) YRES_NOTES_NON_VABLE_SHORT,
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_ID,'YRES',2,0) YRES_NOTES_NON_VABLE_LONG,
  DECODE(V8_Fnc_Xcno_Rep_Infos(YRES.YRES_ID,'YRES',1,1),NULL,0,1) IS_YRES_NOTES_VABLE,  
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_ID,'YRES',1,1) YRES_NOTES_VABLE_SHORT,
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_ID,'YRES',2,1) YRES_NOTES_VABLE_LONG,
  DECODE(V8_Fnc_Xcno_Rep_Infos(YRES.YRES_XCMS_ID,'XCMS',1,0),NULL,0,1) IS_XCMS_NOTES_NON_VABLE,  
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_XCMS_ID,'XCMS',1,0) XCMS_NOTES_NON_VABLE_SHORT,
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_XCMS_ID,'XCMS',2,0) XCMS_NOTES_NON_VABLE_LONG,
  DECODE(V8_Fnc_Xcno_Rep_Infos(YRES.YRES_XCMS_ID,'XCMS',1,1),NULL,0,1) IS_XCMS_NOTES_VABLE,  
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_XCMS_ID,'XCMS',1,1) XCMS_NOTES_VABLE_SHORT,
  V8_Fnc_Xcno_Rep_Infos(YRES.YRES_XCMS_ID,'XCMS',2,1) XCMS_NOTES_VABLE_LONG,
  V8_Fnc_Share_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,1) SHARER_NAME,
  YRES.YRES_YBOM_ID YRES_YBOM_ID,
  nvl2((SELECT MAX(YCLN.YCLN_ID) FROM YCLN WHERE YCLN.YCLN_YDET_ID=YRES.YRES_CURRENT_YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY<>4),0,1) IS_INDI_RES,
  YRES.YRES_YPRT_ID YPRT_ID,
  (SELECT YPRT.YPRT_LONGDESC FROM YPRT WHERE YPRT.YPRT_ID=YRES.YRES_YPRT_ID) YPRT_LONGDESC,
  (SELECT YPRT.YPRT_COMMENT FROM YPRT WHERE YPRT.YPRT_ID=YRES.YRES_YPRT_ID) YPRT_COMMENT,
  (SELECT COUNT(yres_2.YRES_ID) FROM YRES yres_2 WHERE yres_2.YRES_YPRT_ID=YRES.YRES_YPRT_ID AND yres_2.YRES_RESSTATUS<>3) TOTAL_PARTY_MEMBERS,
  V8_Fnc_Yprt_Rep_Infos(YRES.YRES_ID,YRES.YRES_YPRT_ID,3) PARTY_MEMBER,
  DECODE(V8_Fnc_Multiguest_Rep_Infos(YRES.YRES_ID,1),NULL,0,1) HAS_MULTIGUEST,
  V8_Fnc_Multiguest_Rep_Infos(YRES.YRES_ID,1) MULTI_GUEST_NAMES,
  (SELECT WMLG.WMLG_TIME_CREATION FROM WMLG WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_TABLE_ID=YRES.YRES_ID) WMLG_TIME_CREATION,
  (SELECT WMLG.WMLG_TIME_UPDATE FROM WMLG WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_TABLE_ID=YRES.YRES_ID) WMLG_TIME_UPDATE,  
  TO_CHAR((SELECT WLOG.WLOG_TIME FROM WLOG WHERE UPPER(WLOG.WLOG_TABLENAME)='YRES' AND WLOG.WLOG_TYPE=0 AND WLOG.WLOG_TABLE_ID=YRES.YRES_ID),'DD.MM.YYYY') CREATE_DATE, 
  TO_CHAR((SELECT WLOG.WLOG_TIME FROM WLOG WHERE UPPER(WLOG.WLOG_TABLENAME)='YRES' AND WLOG.WLOG_TYPE=0 AND WLOG.WLOG_TABLE_ID=YRES.YRES_ID),'HH24:MI') CREATE_TIME, 
  (SELECT WLOG.WLOG_XCED_ID FROM WLOG WHERE UPPER(WLOG.WLOG_TABLENAME)='YRES' AND WLOG.WLOG_TYPE=0 AND WLOG.WLOG_TABLE_ID=YRES.YRES_ID) CREATE_USER_ID, 
  (SELECT v8_rep_name.NAME2 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=(SELECT WLOG.WLOG_XCED_ID FROM WLOG WHERE UPPER(WLOG.WLOG_TABLENAME)='YRES' AND WLOG.WLOG_TYPE=0 AND WLOG.WLOG_TABLE_ID=YRES.YRES_ID)) CREATE_USER_NAME, 
  (SELECT XCED.XCED_LOGINNAME FROM XCED WHERE XCED.XCED_ID=(SELECT WLOG.WLOG_XCED_ID FROM WLOG WHERE UPPER(WLOG.WLOG_TABLENAME)='YRES' AND WLOG.WLOG_TYPE=0 AND WLOG.WLOG_TABLE_ID=YRES.YRES_ID)) CREATE_USER_LOGIN,
  V8_Fnc_Yebr_Ydet_Rep_Infos(YRES.YRES_ID,1) ALL_BOOKED_ITEMS, 
  DECODE(V8_Fnc_Yebr_Ydet_Rep_Infos(YRES.YRES_ID,1),NULL,0,1) IS_ALL_BOOKED_ITEMS,
  V8_Fnc_Yebr_Ydet_Rep_Infos(YRES.YRES_ID,2) BOOKED_ITEMS,
  DECODE(V8_Fnc_Yebr_Ydet_Rep_Infos(YRES.YRES_ID,2),NULL,0,1) IS_BOOKED_ITEMS,
  V8_Fnc_Amenities_Rep_Infos(YRES.YRES_ID,1) ALL_AMENITIES,
  DECODE(V8_Fnc_Amenities_Rep_Infos(YRES.YRES_ID,1),NULL,0,1) IS_ALL_AMENITIES,
  V8_Fnc_Amenities_Rep_Infos(YRES.YRES_ID,2) AMENITIES,
  DECODE(V8_Fnc_Amenities_Rep_Infos(YRES.YRES_ID,2),NULL,0,1) IS_AMENITIES,
  V8_Fnc_Amenities_Ydet_Infos(YRES.YRES_CURRENT_YDET_ID,1) YDET_AMENITIES_SHORT,
  DECODE(V8_Fnc_Amenities_Ydet_Infos(YRES.YRES_CURRENT_YDET_ID,1),NULL,0,1) IS_YDET_AMENITIES_SHORT,
  V8_Fnc_Amenities_Ydet_Infos(YRES.YRES_CURRENT_YDET_ID,2) YDET_AMENITIES_LONG,
  DECODE(V8_Fnc_Amenities_Ydet_Infos(YRES.YRES_CURRENT_YDET_ID,2),NULL,0,1) IS_YDET_AMENITIES_LONG,
  V8_Fnc_Yrgl_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,1) CHILDREN_SHORT,  
  DECODE(V8_Fnc_Yrgl_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,1),NULL,0,1) IS_CHILDREN_SHORT,  
  V8_Fnc_Yrgl_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,2) CHILDREN_LONG,
  DECODE(V8_Fnc_Yrgl_Rep_Infos(YRES.YRES_CURRENT_YDET_ID,2),NULL,0,1) IS_CHILDREN_LONG,
  (select XCOM.XCOM_VALUE from xcom,xcmt where XCMT.XCMT_ID=XCOM.XCOM_XCMT_ID and XCMT.XCMT_TYPE=1 and XCOM.XCOM_PRIMARY=1 and XCOM.XCOM_XCMS_ID=YRES.YRES_XCMS_ID and rownum=1 ) EMAIL,
  (select XCOM.XCOM_VALUE from xcom,xcmt where XCMT.XCMT_ID=XCOM.XCOM_XCMT_ID and XCMT.XCMT_TYPE=1 and XCOM.XCOM_PRIMARY=1 and XCOM.XCOM_XCMS_ID=YRES.YRES_XCMS_ID and rownum=1 ) RECIPIENT,
  (case when ((select XCOM.XCOM_VALUE from xcom,xcmt where XCMT.XCMT_ID=XCOM.XCOM_XCMT_ID and XCMT.XCMT_TYPE=1 and XCOM.XCOM_PRIMARY=1 and XCOM.XCOM_XCMS_ID=YRES.YRES_XCMS_ID and rownum=1 ) like '%@%' and (select nvl(XCMS.XCMS_NOEMAILING,0) from xcms where XCMS.XCMS_ID=YRES.YRES_XCMS_ID)<>1) then 1 else 0 end) POT_EMAIL_CANDI,
  YRES.YRES_BONUS_XMNR_ID  YRES_BONUS_XMNR_ID
FROM
YRES;


CREATE OR REPLACE VIEW V8_REP_RESERVATION_ROOMMOVE AS
select 
ydet.YDET_DATE YDET_DATE,
ydet.YDET_ID YDET_ID,
yres.YRES_ID YRES_ID,
(select v8_rep_name.NAME2 from v8_rep_name where v8_rep_name.XCMS_ID=yres.YRES_XCMS_ID) GUESTNAME, 
yres.YRES_EXPARRTIME ARRIVAL,
yres.YRES_EXPDEPTIME DEPARTURE,
(case when (ydet.YDET_DATE=V8_sys_fideliodate and yres.YRES_YRMS_ID<>(select d.YDET_YRMS_ID from ydet d where d.YDET_ID=yres.YRES_CURRENT_YDET_ID)) then yres.YRES_YRMS_ID else (select d.YDET_YRMS_ID from ydet d where d.YDET_YRES_ID=ydet.YDET_YRES_ID and d.YDET_DATE=ydet.YDET_DATE-1) end) FROM_ROOM_ID,
(case when (ydet.YDET_DATE=V8_sys_fideliodate and yres.YRES_YRMS_ID<>(select d.YDET_YRMS_ID from ydet d where d.YDET_ID=yres.YRES_CURRENT_YDET_ID)) then (select yrms.YRMS_SHORTDESC from yrms where yrms.YRMS_ID=yres.YRES_YRMS_ID) else (select yrms.YRMS_SHORTDESC from yrms,ydet d where yrms.YRMS_ID=d.YDET_YRMS_ID and d.YDET_YRES_ID=ydet.YDET_YRES_ID and d.YDET_DATE=ydet.YDET_DATE-1) end) FROM_ROOM,
ydet.YDET_YRMS_ID TO_ROOM_ID,
(select yrms.YRMS_SHORTDESC from yrms where yrms.YRMS_ID=ydet.YDET_YRMS_ID) TO_ROOM,
(case when yres.YRES_YRMS_ID=(select d.YDET_YRMS_ID from ydet d where d.YDET_ID=yres.YRES_CURRENT_YDET_ID) and yres.YRES_YRMS_ID<>(select d.YDET_YRMS_ID from ydet d where d.YDET_YRES_ID=yres.YRES_ID and d.YDET_DATE=V8_sys_fideliodate - 1 and ydet.YDET_DATE=V8_sys_fideliodate) then 1 else 0 end) ROOM_MOVE_DONE,
(select count(*) from yres y 
  where y.yres_actarrtimenull=0
  and y.yres_actdeptimenull=1
  and y.yres_resstatus<>3
  and y.YRES_YRMS_ID=ydet.ydet_yrms_id
  and y.yres_id<>yres.yres_id) TO_ROOM_OCCUPIED,
(case when yres.YRES_ACTARRTIMENULL=1 then 0 else 1 end) CHECKED_IN,
V8_sys_fideliodate FIDELIODATE,
YDET.YDET_ADULTNO YDET_ADULTNO, --ACZ new field 21.11.2012
NVL((SELECT SUM(yrgl_number) FROM YRGL,YDET WHERE YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID ),0) TOTALKIDS, --ACZ new field 21.11.2012
YDET.YDET_NOOFROOMS NOOFROOMS --ACZ new field 21.11.2012
from 
ydet,
yres
where yres.YRES_ID=ydet.YDET_YRES_ID and
((yres.YRES_YRMS_ID not like ydet.YDET_YRMS_ID and ydet.YDET_DATE=V8_sys_fideliodate) or 
(ydet.YDET_YRMS_ID<>(select d.YDET_YRMS_ID from ydet d where d.YDET_YRES_ID=ydet.YDET_YRES_ID and d.YDET_DATE+1=ydet.YDET_DATE))) and
yres.yres_resstatus<>3 and
ydet.YDET_DATE>=V8_sys_fideliodate
order by ydet.YDET_DATE;


CREATE OR REPLACE VIEW V8_REP_BLOCKS
AS 
SELECT
        YBLH.YBLH_ID YBLH_ID,
        YBLH.YBLH_SHORTDESC YBLH_SHORTDESC,
        YBLH.YBLH_LONGDESC YBLH_LONGDESC,
        YBLH.YBLH_STARTDATE YBLH_STARTDATE,
    YBLH.YBLH_ENDDATE YBLH_ENDDATE,
    YBLH.YBLH_CUTOFFDATE YBLH_CUTOFFDATE,
    YBLH.YBLH_ELASTIC YBLH_ELASTIC,
    YBLH.YBLH_BACKTOHOUSE YBLH_BACKTOHOUSE,
    trunc(YBLH.YBLH_ROOMINGLISTDATE) YBLH_ROOMINGLISTDATE,
    YBST.YBST_SHORTDESC YBST_SHORTDESC,
    YBLH.YBLH_YBOM_ID YBLH_YBOM_ID,
    YBST.YBST_RESSTATUS YBST_RESSTATUS,
     (SELECT YRCH.YRCH_SHORTDESC FROM YRCH WHERE YBLH.YBLH_YRCH_ID = YRCH_ID) YRCH_SHORTDESC,
     (SELECT XCMA.XCMA_SHORTDESC FROM XCMA WHERE YBLH.YBLH_XCMA_ID = XCMA_ID) XCMA_SHORTDESC,
         (select yras.yras_ID from ybal,YRAS, yrac  where ybal.YBAL_YRAS_ID = YRAS.YRAS_ID AND YRAS.YRAS_YRAC_ID = yrac.yrac_id
                              AND yrac.yrac_ID = 1 and rownum = 1) SOURCE_ID,
         (select yras.YRAS_SHORTDESC from ybal,YRAS, yrac  where ybal.YBAL_YRAS_ID = YRAS.YRAS_ID AND YRAS.YRAS_YRAC_ID = yrac.yrac_id
                              AND yrac.yrac_ID = 1 and rownum = 1) SOURCE,
         (select yras.yras_ID from ybal,YRAS, yrac  where ybal.YBAL_YRAS_ID = YRAS.YRAS_ID AND YRAS.YRAS_YRAC_ID = yrac.yrac_id
                              AND yrac.yrac_ID = 2 and rownum = 1) CHANNEL_ID,
         (select yras.YRAS_SHORTDESC from ybal,YRAS, yrac  where ybal.YBAL_YRAS_ID = YRAS.YRAS_ID AND YRAS.YRAS_YRAC_ID = yrac.yrac_id
                              AND yrac.yrac_ID = 2 and rownum = 1) CHANNEL,
    YBLD.YBLD_ID YBLD_ID,
     YBLD.YBLD_DATE YBLD_DATE,
     YBLD.YBLD_INITIALNOOFROOMS YBLD_INITIALNOOFROOMS,
     YBLD.YBLD_ACTUALNOOFROOMS YBLD_ACTUALNOOFROOMS,
     YBLD.YBLD_CUTOFFDATE YBLD_CUTOFFDATE,
         (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_INTERNALCATEGORY=1 and ybcl.YBCL_PRIMARY=0 and rownum = 1) COMPANY_NAME,
     (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_INTERNALCATEGORY=2 and ybcl.YBCL_PRIMARY=0 and rownum = 1) TRAVELAGENT_NAME,
     (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_INTERNALCATEGORY=3 and ybcl.YBCL_PRIMARY=0 and rownum = 1) SOURCE_NAME,
     (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_INTERNALCATEGORY=4 and ybcl.YBCL_PRIMARY=0 and rownum = 1) INDIVIDUAL_NAME,
     (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_INTERNALCATEGORY=5 and ybcl.YBCL_PRIMARY=0 and rownum = 1) VENDOR_NAME,
     (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_INTERNALCATEGORY=6 and ybcl.YBCL_PRIMARY=0 and rownum = 1) MPLANER_NAME,
     (select v8_rep_name.NAME2 from v8_rep_name,ybcl where v8_rep_name.XCMS_ID=ybcl.YBCL_XCMS_ID and ybcl.YBCL_YBLH_ID=yblh.YBLH_ID and ybcl.YBCL_PRIMARY=1 and rownum = 1) BOOKER,
        YCAT.YCAT_ID,
     YCAT.YCAT_SHORTDESC YCAT_SHORTDESC,
     YCAT.YCAT_LONGDESC YCAT_LONGDESC,
     (select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=1 and yrcv.YRCV_DEFAULT=1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1) PAX_DEF,
    V8_FNC_NUM_TO_CHAR_FORMATING(decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=1),0),0,
  (select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=1 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
    (select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=1 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID))) PAX1,
      V8_FNC_NUM_TO_CHAR_FORMATING(decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=2),0),0,
      (select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=2 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
    (select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=1 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID))) PAX2,
        V8_FNC_NUM_TO_CHAR_FORMATING(decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=3),0),0,
      (select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=3 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
    (select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=1 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID))) PAX3,
        V8_FNC_NUM_TO_CHAR_FORMATING(decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=4),0),0,
      (select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=4 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
    (select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=1 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID))) PAX4,
    (select sum(YDET.YDET_NOOFROOMS) from ydet where YBLD.YBLD_ID = YDET.YDET_YBLD_ID) YDET_NOOFROOMS ,
     (select sum(YDET.YDET_NOOFROOMS) from ydet,yres r where YBLD.YBLD_ID = YDET.YDET_YBLD_ID and ydet.ydet_yres_id=r.yres_id and r.YRES_NOAVAILREASON=3) CANCELROOM,
     (select sum(YDET.YDET_NOOFROOMS) from ydet,yres r where YBLD.YBLD_ID = YDET.YDET_YBLD_ID and ydet.ydet_yres_id=r.yres_id and r.YRES_NOAVAILREASON=4) NOSHOWROOM,
     (select sum(YDET.YDET_NOOFROOMS) from ydet,yres r where YBLD.YBLD_ID = YDET.YDET_YBLD_ID and ydet.ydet_yres_id=r.yres_id and r.YRES_ORGDEPTIME>r.YRES_ACTDEPTIME) EARLYDEPROOM,
     (SELECT 1 FROM YRCH WHERE YBLH.YBLH_YRCH_ID = YRCH_ID and yrch.YRCH_COMPSTATUS=1) COMPLIROOMS,
     (SELECT 1 FROM YRCH WHERE YBLH.YBLH_YRCH_ID = YRCH_ID and yrch.YRCH_COMPSTATUS=2) HOUSEUSEROOMS,
      decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=1),0),0,
      V8_FNC_VALCURR_FORMATING((select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=1 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
        (select zcur.zcur_shortdesc from yrcv,yrcd,yrch,zcur where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=1 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and yrch.yrch_zcur_id=zcur.zcur_id and rownum=1)),
      V8_FNC_VALCURR_FORMATING((select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=1 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where ZCUR.ZCUR_ID=YRCH.YRCH_ZCUR_ID and YRCH.YRCH_ID=YBLH.YBLH_YRCH_ID and rownum=1))) 
      FPAX1,
      decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=2),0),0,
      V8_FNC_VALCURR_FORMATING((select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=2 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
        (select zcur.zcur_shortdesc from yrcv,yrcd,yrch,zcur where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=2 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and yrch.yrch_zcur_id=zcur.zcur_id and rownum=1)),
      V8_FNC_VALCURR_FORMATING((select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=2 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where ZCUR.ZCUR_ID=YRCH.YRCH_ZCUR_ID and YRCH.YRCH_ID=YBLH.YBLH_YRCH_ID and rownum=1))) 
      FPAX2,
      decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=3),0),0,
      V8_FNC_VALCURR_FORMATING((select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=3 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
        (select zcur.zcur_shortdesc from yrcv,yrcd,yrch,zcur where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=3 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and yrch.yrch_zcur_id=zcur.zcur_id and rownum=1)),
      V8_FNC_VALCURR_FORMATING((select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=3 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where ZCUR.ZCUR_ID=YRCH.YRCH_ZCUR_ID and YRCH.YRCH_ID=YBLH.YBLH_YRCH_ID and rownum=1))) 
      FPAX3,
      decode(nvl((SELECT 1 FROM YBLR WHERE YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID and YBLR.YBLR_ADULTNO=4),0),0,
      V8_FNC_VALCURR_FORMATING((select yrcv.YRCV_VALUE from yrcv,yrcd,yrch where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=4 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and rownum=1),
        (select zcur.zcur_shortdesc from yrcv,yrcd,yrch,zcur where yrcv.YRCV_YRCD_ID = yrcd.YRCD_ID and yrcd.YRCD_YRCH_ID = yrch.YRCH_ID and YBLH_YRCH_ID = yrch.YRCH_ID and yrcv.YRCV_NOOFADULTS=4 and yrcv.YRCV_DEFAULT<>1 and (yrcd.YRCD_STARTDATE<=ybld.YBLD_DATE and yrcd.YRCD_ENDDATE>=ybld.YBLD_DATE) and yrch.yrch_zcur_id=zcur.zcur_id and rownum=1)),
      V8_FNC_VALCURR_FORMATING((select YBLR.YBLR_RATEAMOUNT from yblr where YBLR.YBLR_ADULTNO=4 and YBLR.YBLR_YBLD_ID=YBLD.YBLD_ID),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where ZCUR.ZCUR_ID=YRCH.YRCH_ZCUR_ID and YRCH.YRCH_ID=YBLH.YBLH_YRCH_ID and rownum=1))) 
      FPAX4,
     (select sum(YBOS.YBOS_QUANTITY*YBOS.YBOS_UNITPRICE) from ybos where YBOS.YBOS_YBLD_ID=ybld.ybld_id) RATE,
     V8_FNC_VALCURR_FORMATING((select sum(YBOS.YBOS_QUANTITY*YBOS.YBOS_UNITPRICE) from ybos where YBOS.YBOS_YBLD_ID=ybld.ybld_id),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where zcur.zcur_id=YRCH.YRCH_ZCUR_ID and YBLH.YBLH_YRCH_ID=YRCH.YRCH_ID and rownum=1)) FRATE,
     V8_FNC_VALCURR_FORMATING((select sum(YBOS.YBOS_UNITPRICE) from ybos where YBOS.YBOS_YBLD_ID=ybld.ybld_id),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where zcur.zcur_id=YRCH.YRCH_ZCUR_ID and YBLH.YBLH_YRCH_ID=YRCH.YRCH_ID and rownum=1)) RATE_GROSS,
     v8_fnc_valcurr_formating((select sum(YBOS.YBOS_UNITPRICE) from ybos where ybos.ybos_dt=1 and YBOS.YBOS_YBLD_ID=ybld.ybld_id),(select ZCUR.ZCUR_SHORTDESC from zcur,yrch where zcur.zcur_id=YRCH.YRCH_ZCUR_ID and YBLH.YBLH_YRCH_ID=YRCH.YRCH_ID and rownum=1)) RATE_NET     
FROM
    YBLH YBLH,
    YBLD YBLD,
    YBST YBST,
    YCAT YCAT
WHERE
    YBLH.YBLH_ID = YBLD.YBLD_YBLH_ID(+) AND
    YBLH.YBLH_YBST_ID = YBST.YBST_ID AND
    YBLD.YBLD_YCAT_ID = YCAT.YCAT_ID(+)
ORDER BY
    YBLH.YBLH_LONGDESC ASC,
    YBLD.YBLD_ID ASC;


CREATE OR REPLACE VIEW V8_REP_DEPOSIT_PAID_INFOS_DET
AS 
SELECT
  YRES.YRES_ID ID,
  zpos.zpos_quantity*zpos.zpos_unitprice Amount,
  zpos.zpos_postdate zpos_postdate,
  YRES.YRES_EXPARRTIME EXPARRTIME,
  YRES.YRES_ACTARRTIME ACTARRTIME,
  YRES.YRES_EXPDEPTIME EXPDEPTIME,
  YRES.YRES_ACTDEPTIME ACTDEPTIME,
  (YRES.YRES_EXPDEPTIME) - (YRES.YRES_EXPARRTIME) Nights,
  YRES.YRES_FREETEXT1 FREETEXT1,
  YRES.YRES_FREETEXT2 FREETEXT2,
  (DECODE(NVL((SELECT YDET.YDET_SHARENUM FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0),0,(SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),(CASE WHEN (YRES.YRES_HASSHARE=1 AND YRES.YRES_ID=(SELECT MIN(ydet_2.YDET_YRES_ID) FROM YDET,YDET ydet_2 WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND YDET.YDET_SHARENUM=ydet_2.YDET_SHARENUM GROUP BY ydet_2.YDET_SHARENUM)) THEN (SELECT AVG(ydet_2.YDET_NOOFROOMS) FROM YDET,YDET ydet_2 WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND YDET.YDET_SHARENUM=ydet_2.YDET_SHARENUM GROUP BY ydet_2.YDET_SHARENUM) ELSE 0 END))) NOOFROOMS,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) NUM_SHORTDESC,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CAT_SHORTDESC,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CAT_LONGDESC,
  (SELECT YRCH.YRCH_SHORTDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) YRCH_SHORTDESC,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=YRES.YRES_XCMS_ID) GUEST_NAME,
  (SELECT SUM(YDEP.YDEP_AMOUNT_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID) YDEP_DEPOSIT_REQUESTED,
  (SELECT MAX(YDEP.YDEP_DUEDATE_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID) YDEP_DEPOSIT_LIMIT,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_PAID,
  (SELECT MAX(ZPOS.ZPOS_POSTDATE) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_POSTDATE,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=2 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_TRANSFERED,
  (SELECT MAX(ZLSM.ZLSM_DATE) FROM ZLSM, ZPOS WHERE ZLSM.ZLSM_ZPOS_ID=ZPOS.ZPOS_ID AND ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=2 AND ZPOS.ZPOS_YRES_ID=YRES.yres_id) ZPOS_DEPOSIT_TRANSFERDATE,
  1 TYP_NUM,
  'RES' TYP
FROM
YRES,zpos
WHERE
yres.yres_id=zpos.zpos_yres_id and ZPOS.zpos_ledgerstatus=1 and zpos.zpos_cdt=5 and
 ((SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS in (1,2) and ZPOS.ZPOS_YRES_ID=YRES.yres_id)+(SELECT NVL(SUM(YDEP.YDEP_AMOUNT_RMS),0) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID))<>0
UNION ALL
SELECT 
  ZFAC.ZFAC_ID ID,
  zpos.zpos_quantity*zpos.zpos_unitprice Amount,
  zpos.zpos_postdate zpos_postdate,
  ZFAC.ZFAC_EXPVALIDFROM EXPARRTIME,
  ZFAC.ZFAC_ACTVALIDFROM ACTARRTIME,
  ZFAC.ZFAC_EXPVALIDUNTIL EXPDEPTIME,
  ZFAC.ZFAC_ACTVALIDUNTIL ACTDEPTIME,
  (ZFAC.ZFAC_EXPVALIDUNTIL) - (ZFAC.ZFAC_EXPVALIDFROM) Nights,
  ZFAC.ZFAC_FREETEXT1 FREETEXT1,
  ZFAC.ZFAC_FREETEXT2 FREETEXT2,
  1 NOOFROOMS,
  decode(nvl(zfac.ZFAC_AUTONUMBER,0),0,(select zfan.ZFAN_LONGDESC from zfan where zfan.ZFAN_ID=zfac.ZFAC_ZFAN_ID),zfac.ZFAC_AUTONUMBER) NUM_SHORTDESC,
  (SELECT zfag.ZFAG_SHORTDESC FROM zfag where zfag.ZFAG_ID=zfac.ZFAC_ZFAG_ID) CAT_SHORTDESC,
  (SELECT zfag.ZFAG_LONGDESC FROM zfag where zfag.ZFAG_ID=zfac.ZFAC_ZFAG_ID) CAT_LONGDESC,
  null YRCH_SHORTDESC,
  (SELECT v8_rep_name.NAME6 FROM v8_rep_name WHERE v8_rep_name.XCMS_ID=ZFAC.ZFAC_XCMS_ID ) GUEST_NAME,
  null YDEP_DEPOSIT_REQUESTED,
  null YDEP_DEPOSIT_LIMIT,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_ZFAC_ID=ZFAC.zfac_id) ZPOS_DEPOSIT_PAID,
  (SELECT MAX(ZPOS.ZPOS_POSTDATE) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_ZFAC_ID=ZFAC.zfac_id) ZPOS_DEPOSIT_POSTDATE,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=2 AND ZPOS.ZPOS_ZFAC_ID=ZFAC.zfac_id) ZPOS_DEPOSIT_TRANSFERED,
  (SELECT MAX(ZLSM.ZLSM_DATE) FROM ZLSM, ZPOS WHERE ZLSM.ZLSM_ZPOS_ID=ZPOS.ZPOS_ID AND ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=2 AND ZPOS.ZPOS_ZFAC_ID=ZFAC.zfac_id) ZPOS_DEPOSIT_TRANSFERDATE,
  2 TYP_NUM,
  'FA' TYP
FROM
ZFAC, zpos
WHERE
zfac.zfac_id=zpos.zpos_zfac_id and ZPOS.zpos_ledgerstatus=1 and zpos.zpos_cdt=5 and
 (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS in (1,2) and ZPOS.ZPOS_ZFAC_ID=ZFAC.zfac_id)<>0;


create or replace view V8_REP_REBATES_BARCODE as
select ZRDF.ZRDF_SHORTDESC,
       ZRDF.ZRDF_LONGDESC,
       ZRDF.ZRDF_BARCODE BARCODE,
       '*'||ZRDF.ZRDF_BARCODE||'*' BARCODE39   
 from zrdf;


 CREATE OR REPLACE VIEW V8_REP_REPORT_DETAILS
AS 
SELECT
wrpt.wrpt_id
,wrpt.wrpt_displorder
,wrpt.wrpt_longdesc
,WRPB.WRPB_ID
,WRPB.WRPB_LONGDESC
,WRPB.WRPB_FILETIMESTAMP
,WRPS.WRPS_ID
,WRPS.WRPS_MASTER_WRPS_ID
,WRPS.WRPS_DISPLORDER
,WRPS.WRPS_LONGDESC
,WRPS1.WRPS_DISPLORDER WRPS_DISPLORDER1
,WRPS1.WRPS_LONGDESC WRPS_LONGDESC1
,WRPS2.WRPS_DISPLORDER WRPS_DISPLORDER2
,WRPS2.WRPS_LONGDESC WRPS_LONGDESC2
,WRPS3.WRPS_DISPLORDER WRPS_DISPLORDER3
,WRPS3.WRPS_LONGDESC WRPS_LONGDESC3
,WRPS4.WRPS_DISPLORDER WRPS_DISPLORDER4 
,WRPS4.WRPS_LONGDESC WRPS_LONGDESC4
from
wrps
,wrps wrps1
,wrps wrps2
,wrps wrps3
,wrps wrps4
,wrpt
,wrpb
where
wrpt.wrpt_wrps_id=WRPS.WRPS_ID
and WRPS1.WRPS_ID (+) = WRPS.WRPS_MASTER_WRPS_ID 
and WRPS2.WRPS_ID(+) = WRPS1.WRPS_MASTER_WRPS_ID 
and WRPS3.WRPS_ID(+) = WRPS2.WRPS_MASTER_WRPS_ID  
and WRPS4.WRPS_ID(+) = WRPS3.WRPS_MASTER_WRPS_ID
and WRPB.WRPB_ID = wrpt.wrpt_wrpb_id(+)
order by wrpt.wrpt_displorder;


create or replace view V8_SYS_SCHEDULED_PROFILE as
select 
xcms_id         as CONF_XCMS_ID,
xcms_id         as CONF_CONTACT_XCMS_ID,
xcms_wlan_id    as CONF_WLAN_ID,
xcid_addrgreet  as CONF_ADDRGREET, 
xcid_lettgreet  as CONF_LETTGREET,
0 /*gtIndividual*/  as CONF_GREETINGTYPE,
(select xadr_id from xadr where xadr_xcms_id = xcms_id and XADR_PRIMARY = 1 and rownum=1) as CONF_XADR_ID,
(select xcom_id from xcom where xcom_xcms_id = xcms_id and xcom_PRIMARY = 1 and rownum=1) as CONF_XCOM_ID
from xcms, xcid
where 
xcms_id=xcid_id(+);
/


create or replace view V8_SYS_SCHEDULED_CONFIRM as
select 
 p.*,
 r.yres_id        as CONF_YRES_ID,
 r.yres_ygrp_id   as CONF_YGRP_ID 
from 
 V8_SYS_SCHEDULED_PROFILE p, yres r 
where
  p.CONF_XCMS_ID = r.yres_xcms_id;
/  
 

CREATE OR REPLACE FORCE VIEW V8_MAILING_CUSTOMERS
AS 
SELECT
  xcms.xcms_id,
  decode(xcms.xcms_type, 1, xcms.xcms_name1, 2, '') AS CompanyName1,
  xcil.xcil_primary,
  xcid_individual.XCID_DEPARTMENT,
  xcid_individual.XCID_POSITION,
  xcid_individual.XCID_TITLE,
  xcid_individual.XCID_SEX,
  xadr.xadr_zip zip,
  xadr.xadr_city city,
  xadr.xadr_street1 street1,
  xadr.xadr_street2 street2,
  xadr.xadr_street3 street3,
  xcou.xcou_id,
  xcou.xcou_longdesc country,
  xcou.xcou_custshortdesc,
  Decode(xcms.xcms_type, 1, xcms.xcms_name2, 2, '') AS CompanyName2,
  Decode(xcms.xcms_type, 1, xcms.xcms_name3, 2, '') AS CompanyName3,
  xsta.xsta_shortdesc,
  xsta.xsta_longdesc,
  xcms.xcms_type,
  xcms.xcms_active,
  xcms.xcms_nomailing,
  xcms.xcms_noemailing,
  DECODE(xcms.xcms_type, 1, xcid_contact.xcid_lettgreet||' '||xcms_contact.xcms_name1,
                         2, xcid_individual.xcid_lettgreet||' '||xcms.xcms_name1) lettergreeting,
  DECODE(xcms.xcms_type, 1, xcid_contact.xcid_alt_lettgreet||' '||xcms_contact.xcms_name1,
                         2, xcid_individual.xcid_alt_lettgreet||' '||xcms.xcms_name1) alt_lettergreeting,
  DECODE(xcms.xcms_type, 1, xcid_contact.xcid_addrgreet,
                         2, xcid_individual.xcid_addrgreet) addressgreetingshort,
  DECODE(xcms.xcms_type, 1, DECODE(NVL(xcid_contact.xcid_addrgreet, ''), '', '',  xcid_contact.xcid_addrgreet||' ')||DECODE(NVL(xcms_contact.xcms_name3, ''), '', '', xcms_contact.xcms_name3||' ')||DECODE(NVL(xcms_contact.xcms_name2, ''), '', '', xcms_contact.xcms_name2||' ')||DECODE(NVL(xcms_contact.xcms_name1, ''), '', '', xcms_contact.xcms_name1),
                         2, DECODE(NVL(xcid_individual.xcid_addrgreet, ''), '', '',  xcid_individual.xcid_addrgreet||' ')||DECODE(NVL(xcms.xcms_name3, ''), '', '', xcms.xcms_name3||' ')||DECODE(NVL(xcms.xcms_name2, ''), '', '', xcms.xcms_name2||' ')||DECODE(NVL(xcms.xcms_name1, ''), '', '', xcms.xcms_name1)) addressgreetingfull,
  DECODE(xcms.xcms_type, 1, DECODE(NVL(xcms_contact.xcms_name3, ''), '', '', xcms_contact.xcms_name3||' ')||DECODE(NVL(xcms_contact.xcms_name2, ''), '', '', xcms_contact.xcms_name2||' ')||DECODE(NVL(xcms_contact.xcms_name1, ''), '', '', xcms_contact.xcms_name1),
                         2, DECODE(NVL(xcms.xcms_name3, ''), '', '', xcms.xcms_name3||' ')||DECODE(NVL(xcms.xcms_name2, ''), '', '', xcms.xcms_name2||' ')||DECODE(NVL(xcms.xcms_name1, ''), '', '', xcms.xcms_name1)) addressgreeting,
  xcms.xcms_xcca_id,
  DECODE(xcms.xcms_type, 2, xcid_individual.xcid_birthtime) birthdate,
  DECODE(xcms.xcms_type, 2, xcid_individual.xcid_weddingday) xcid_weddingday,
  (SELECT TRUNC(w.wmlg_time_creation) FROM wmlg w WHERE UPPER(w.wmlg_tablename)='XCMS' AND w.wmlg_table_id=xcms.xcms_id AND ROWNUM=1) ProfileCreationTime,
(SELECT wlan_longdesc FROM wlan WHERE xcms.xcms_wlan_id=wlan_id) wlan_longdesc, 
(select v.Address from v8_rep_fulladdress_xadr v where v.xadr_id=xadr.xadr_id) Address, 
DECODE(xcms.xcms_type, 1, xcms.xcms_name1||' '||xcms.xcms_name2||' '||xcms.xcms_name3, 2, '' ) NAME,
  xcrl.xcrl_yrch_id RateCodeId,
  (SELECT yrch_shortdesc FROM yrch WHERE yrch_id=xcrl_yrch_id) yrch_shortdesc,
  (SELECT yrch_longdesc FROM yrch WHERE yrch_id=xcrl_yrch_id) yrch_longdesc,
  (SELECT wphs.WPHS_NAME FROM wphs WHERE wphs.WPHS_ID = xcms.XCMS_ORG_WPHS_ID) OrigHotel,
  xcms.XCMS_ORG_WPHS_ID, 
  DECODE(xcms.xcms_type, 1, '', 2, xcms.xcms_name1) IndividualName1,
  DECODE(xcms.xcms_type, 1, '', 2, xcms.xcms_name2) IndividualName2,
  DECODE(xcms.xcms_type, 1, '', 2, xcms.xcms_name3) IndividualName3,
  DECODE(xcms.xcms_type, 1, '', 2, xcid_individual.xcid_fam_addrgreet) famaddressgreetingshort,
  xmty_shortdesc,
  xmty_longdesc,
  xmnr.XMNR_EXPIRYDATE,
  (select xced_initials from xced where xced_id=xecl.xecl_emp_xcms_id) xced_initials,
  xmnr.xmnr_value
FROM
  xcms,
  xcca,
  (select * from xcil where nvl(xcil_primary, 0)=1) xcil,
  xcms xcms_contact,
  xcid xcid_contact,
  xcid xcid_individual,
  xsta,
  xadr,
  xcou,
  xcrl,
  xmnr, xmty, 
  xecl
  WHERE
  xcms.xcms_id=xcil.xcil_company_xcms_id(+) AND
  xcil.xcil_individual_xcms_id=xcms_contact.xcms_id(+) AND
  xcms_contact.xcms_id = xcid_contact.xcid_id(+) AND
  xcms.xcms_id = xcid_individual.xcid_id(+) AND
  xcms.xcms_xcca_id=xcca.xcca_id(+) AND
  xcms.xcms_id=xadr.xadr_xcms_id(+) AND
  xadr.xadr_xcou_id=xcou.xcou_id(+) AND
  xadr.xadr_xsta_id=xsta.xsta_id(+) AND
  xcms.xcms_id=xcrl.xcrl_xcms_id(+) AND
  xcms.xcms_id=xmnr.xmnr_xcms_id(+) AND xmnr.xmnr_xmty_id=xmty_id(+) AND
  xcms.xcms_id=xecl_cust_xcms_id(+);


create or replace view v8_mailing_shis_only_yres
as
  select * from shis
  where shis_yres_id is not null;

create or replace view v8_mailing_reservation_shis
as
  select  shis.shis_id,
    shis.shis_yres_id, shis.shis_yres_actarrtime, shis.shis_yres_actdeptime, 
    shis.shis_yres_exparrtime, shis.shis_yres_expdeptime,  shis.shis_yres_resstatus, 
    trunc(decode(nvl(shis.shis_yres_actdeptime, NULL), NULL, shis.shis_yres_expdeptime, shis.shis_yres_actdeptime)) - 
    trunc(decode(nvl(shis.shis_yres_actarrtime, NULL), NULL, shis.shis_yres_exparrtime, shis.shis_yres_actarrtime)) Nights, 0 FutureNights, 1 Stays, 0 FutureStays,
    shis.shis_xcms_id,
    shis.shis_wphs_id,
    (select trunc(min(a.shis_yres_actarrtime)) from shis a where a.shis_xcms_id=shis.shis_xcms_id) firstarrival,
    (select trunc(max(a.shis_yres_actarrtime)) from shis a where a.shis_xcms_id=shis.shis_xcms_id) lastarrival,
    (select trunc(max(a.shis_yres_actdeptime)) from shis a where a.shis_xcms_id=shis.shis_xcms_id) lastdeparture,
    (select max(v.shis_id) from v8_mailing_shis_only_yres v where v.shis_yres_id=shis.shis_yres_id and nvl(v.shis_wphs_id, 0)=nvl(shis.shis_wphs_id, 0) ) max_shis_id,
    shis.shis_ydet_xcma_id,
    shis.shis_ydet_yrch_id,
    shis.shis_source_id,
    shis.shis_ydet_ycat_id
from v8_mailing_shis_only_yres shis
where 
  (shis_yres_actarrtime is not null and shis_yres_actdeptime is not null --checked in+checked out 
          or shis_yres_actdeptime is null and shis_yres_expdeptime<(select wgbs_datevalue from wgbs) --past no show
        );  
  
CREATE OR REPLACE VIEW V8_MAILING_RESERVATION
AS 
select v.yres_id, v.yres_actarrtime, v.yres_actdeptime, v.yres_exparrtime, v.yres_expdeptime,  v.yres_resstatus, 
       v.Nights, v.FutureNights, v.Stays, v.FutureStays, v.xcms_id yres_xcms_id,
       (select SUM(zpos_grossunitprice*zpos_quantity) Payment from zpos where zpos_yres_id=v.yres_id and zpos_cdt<>5 ) Payment, 
       (select SUM((((DECODE(ydet.ydet_ratemanual,NULL,ydet.ydet_rateamount,ydet.ydet_ratemanual)+NVL(ydet.ydet_ratediff,0))*((100-NVL(ydet.ydet_ratediscountperc,0))/100)))) 
	    from ydet where ydet.ydet_yres_id=v.yres_id) rate,
       yras_id, 
       ydet_yrch_id, 
       ydet_xcma_id,
       wphs_id,
       v.firstarrival,
       v.lastarrival,
       v.lastdeparture,
       ydet_ycat_id
from	   
  (select shis.shis_yres_id yres_id, shis.shis_yres_actarrtime yres_actarrtime, shis.shis_yres_actdeptime yres_actdeptime, 
     shis.shis_yres_exparrtime yres_exparrtime, shis.shis_yres_expdeptime yres_expdeptime,  shis.shis_yres_resstatus yres_resstatus, 
     shis.Nights, shis.FutureNights, shis.Stays, shis.FutureStays,
     shisp.shis_xcms_id xcms_id,
     shis.shis_wphs_id wphs_id,
     shis.firstarrival,
     shis.lastarrival,
     shis.lastdeparture,
     shis.shis_ydet_xcma_id ydet_xcma_id,
     shis.shis_ydet_yrch_id ydet_yrch_id,
     shis.shis_source_id yras_id,
     shis.shis_ydet_ycat_id ydet_ycat_id
  from 
    v8_mailing_reservation_shis shis,
    (select shis_id, shis_xcms_id from v8_mailing_shis_only_yres 
     union select shis_id, shis_ycln_1_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_1_xcms_id, 0) > 0
     union select shis_id, shis_ycln_2_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_2_xcms_id, 0) >0
     union select shis_id, shis_ycln_3_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_3_xcms_id, 0) >0
     union select shis_id, shis_ycln_4_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_4_xcms_id, 0) >0
     union select shis_id, shis_ycln_5_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_5_xcms_id, 0) >0
     union select shis_id, shis_ycln_6_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_6_xcms_id, 0) >0
     union select shis_id, shis_ycln_7_xcms_id from v8_mailing_shis_only_yres where nvl(shis_ycln_7_xcms_id, 0) >0
      ) shisp 
  where 
      shis.shis_id=shis.max_shis_id  
      and shis.shis_id=shisp.shis_id
  union all
  SELECT v.yres_id, v.yres_actarrtime, v.yres_actdeptime, v.yres_exparrtime, v.yres_expdeptime, v.yres_resstatus, 
	     decode(nvl(v.yres_actarrtime, NULL), NULL, 0, TRUNC(decode(nvl(v.yres_actdeptime, NULL), NULL, v.yres_expdeptime, v.yres_actdeptime )) - TRUNC(v.yres_actarrtime) ) Nights, 
		 decode(nvl(v.yres_actarrtime, NULL), NULL, (V.YRES_EXPDEPTIME) - TRUNC(v.yres_exparrtime), 0) FutureNights, 
		 decode(nvl(v.yres_actarrtime, NULL), NULL, 0, 1) Stays, 
		 decode(nvl(v.yres_actarrtime, NULL), NULL, 1, 0) FutureStays,
		 pv1.xcms_id,
		 0,
         (select trunc(min(a.yres_actarrtime)) from yres a where a.yres_xcms_id=v.yres_xcms_id) firstarrival,
         (select trunc(max(a.yres_actarrtime)) from yres a where a.yres_xcms_id=v.yres_xcms_id) lastarrival,
         (select trunc(max(a.yres_actdeptime)) from yres a where a.yres_xcms_id=v.yres_xcms_id) lastdeparture,
         (select y.ydet_xcma_id from ydet y where y.ydet_id=v.yres_last_ydet_id) ydet_xcma_id,
         (select y.ydet_yrch_id from ydet y where y.ydet_id=v.yres_last_ydet_id) ydet_yrch_id,
         (select y.yral_yras_id from yres r, ydet rd, yral y where r.yres_last_ydet_id=rd.ydet_id and y.yral_ydet_id=rd.ydet_id and r.yres_id=v.yres_id and rownum=1) yras_id,
         (select y.ydet_ycat_id from ydet y where y.ydet_id=v.yres_last_ydet_id) ydet_ycat_id
  from yres v, wgbs, v8_mailing_reservation_prof pv1
  where	   	    
    (decode(nvl(v.yres_actarrtime, NULL), NULL, v.yres_exparrtime, trunc(v.yres_actarrtime))<=wgbs_datevalue and 
	decode(nvl(yres_actdeptime, NULL), NULL, yres_expdeptime, trunc(yres_actdeptime))>=wgbs_datevalue or
	YRES_EXPARRTIME>wgbs_datevalue) and
	v.yres_id=pv1.yres_id ) v;


CREATE OR REPLACE FORCE VIEW V8_EDITOR_GPU
AS 
SELECT
  YRES.YRES_ID                                                      RESERVATION_ID,
  YRES.YRES_YBOM_ID                                                                                     BOOKINGMASTER_ID,
  YRES.YRES_CURRENT_YDET_ID                                 RESERVATION_DETAIL_ID,
  YRES.YRES_EXPARRTIME                                              ARRIVAL,
  YRES.YRES_EXPDEPTIME                                         DEPARTURE,
  (YRES.YRES_EXPDEPTIME) - (YRES.YRES_EXPARRTIME) NIGHTS,
  TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24:MI')                     ARRIVALTIME,
  TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24:MI')                     DEPARTURETIME,
  (select YDET.YDET_YCAT_ID from ydet where ydet.ydet_id=YRES.YRES_CURRENT_YDET_ID) YCAT_ID,
  (SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                               NOOFROOMS,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMTYPE,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMTYPE_DESC,
  WTRL_TRANSLATE('YCAT',(SELECT YDET.YDET_YCAT_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),'YCAT_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID from XCMS where XCMS.XCMS_ID=YRES.YRES_XCMS_ID)) TRANSLATED_TYPE_DESC,
  (SELECT YGCO.YGCO_SHORTDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID)                                                        GUARANTEECODE,
  (SELECT YGCO.YGCO_LONGDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID)                                                      GUARANTEECODE_DESC,
  DECODE(YRES.YRES_RESSTATUS,0,'TENTATIVE', 1,'DEFINITE',3,'NOT')                                                                 RESSTATUS,
  DECODE(YRES.YRES_NOAVAILREASON,1,'WAITLIST',2,'OFFER',3,'CANCELED',4,'NOSHOW')                                                  NOAVAILREASON,
  YRES.YRES_WALKIN                                                                                                                  WALKIN,
  YRES.YRES_FREETEXT1                                                                                                              FREETEXT1,
  YRES.YRES_FREETEXT2                                                                                                              FREETEXT2,
  NVL(YRES.YRES_HASSHARE,0)                                                                                                      HASSHARE,
  DECODE(YRES.YRES_HASSHARE,1,(DECODE(YRES.YRES_SHARETYPE,0,'INDEPENDANT',1,'MASTER',2,'SLAVE')),' ')                             SHARETYPE,
  (SELECT YDET.YDET_ADULTNO FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                              ADULTNO,
  NVL((SELECT SUM(YRGL_NUMBER) FROM YRGL,YDET WHERE YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID ),0) CHILDNO,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                   ROOM_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMNUMBER,
  (SELECT YRMS.YRMS_LONGDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMNUMBER_LDESC,  
  (SELECT YDET.YDET_YCAT_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                ROOMTYPE_ID,
  (SELECT YDET.YDET_YCAT_ID_RATE FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                          RATEROOMTYPE_ID,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) RATEROOMTYPE,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) RATEROOMTYPE_DESC,
  (SELECT YBLH.YBLH_ID FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BLOCK_ID,
  (SELECT YBLH.YBLH_SHORTDESC FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BLOCK_DESC,
  NVL((SELECT YDET.YDET_RATEAMOUNT FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                          RATEAMOUNT,
  NVL((SELECT YDET.YDET_RATEDIFF FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                      RATEDIFF,
  NVL((SELECT YDET.YDET_RATEDISCOUNTPERC FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                              RATEDISCOUNTPERC,
  (SELECT NVL(YDET.YDET_RATEMANUAL,0) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                      RATEMANUAL,
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL, NULL, decode(YDET.YDET_FOREIGNRATEAMOUNT, null, YDET.YDET_RATEAMOUNT, YDET.YDET_FOREIGNRATEAMOUNT),decode(YDET.YDET_FOREIGNRATEMANUAL,null,YDET.YDET_RATEMANUAL,decode(YDET.YDET_FOREIGNRATEMANUAL,0,YDET.YDET_RATEMANUAL,YDET.YDET_FOREIGNRATEMANUAL)))+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC, 0))/100))-NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, YPAL.YPAL_VALUE*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CONF_RATE,
  V8_FNC_NUM_TO_CHAR_FORMATING((SELECT (((DECODE(YDET.YDET_RATEMANUAL, NULL, decode(YDET.YDET_FOREIGNRATEAMOUNT, null, YDET.YDET_RATEAMOUNT, YDET.YDET_FOREIGNRATEAMOUNT),decode(YDET.YDET_FOREIGNRATEMANUAL,null,YDET.YDET_RATEMANUAL,decode(YDET.YDET_FOREIGNRATEMANUAL,0,YDET.YDET_RATEMANUAL,YDET.YDET_FOREIGNRATEMANUAL)))+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC, 0))/100))-NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, YPAL.YPAL_VALUE*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CONF_RATE_GS,
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id) Currency,
  ((SELECT ((DECODE(YDET.YDET_RATEMANUAL,NULL,YDET.YDET_RATEAMOUNT,YDET.YDET_RATEMANUAL)+NVL(YDET.YDET_RATEDIFF,0)) * (100-NVL(YDET.YDET_RATEDISCOUNTPERC,0)))/100 FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CALC_PRICE,
  (SELECT (NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                         ADD_ON_PACKAGES,
  NVL((SELECT SUM(YFXC.YFXC_AMOUNT*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                                                     TOTAL_FIX_CHARGES,
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL,NULL,YDET.YDET_RATEAMOUNT,YDET.YDET_RATEMANUAL)+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC,0))/100))-  NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) + NVL((SELECT SUM(YFXC.YFXC_AMOUNT*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) CONF_RATE_FIXCHARGE,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID)                                                                  CONF_RATE_FIXCHARGE_STAY,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1)                                          CONF_RATE_STAY,
  V8_FNC_NUM_TO_CHAR_FORMATING((SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1))              CONF_RATE_STAY_GS,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE=1)                                          FIXCHARGE_STAY,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS,YDET WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID and ydet_id=nvl(YRES.YRES_CURRENT_YDET_ID,YRES.YRES_LAST_YDET_ID) and YDET.YDET_DATE=YPOS.YPOS_DATE AND YPOS.YPOS_DT<>2 )        RATE_NET,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID  AND YPOS.YPOS_DT<>2 )                                                                                                                  RATE_NET_STAY,
  (SELECT YDET.YDET_YRCH_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                                               RATECODE_ID,
  (SELECT YRCH.YRCH_SHORTDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE,
  (SELECT YRCH.YRCH_LONGDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_DESC,
  (SELECT YRCH.YRCH_RATEINFO FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_INFO,
  (SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_TEXT,
  DECODE((NVL((SELECT YRCH.YRCH_COMPSTATUS FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)),1,'COMP',2,'HOUSE USE','')                                                     COMPSTATUS,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN 1 ELSE 0 END)                                                                                                                                       UNSPLITTED_GROUP_RES,
  YRES.YRES_XCMS_ID                                                                                                                                                                                                                   GUEST_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID)                                                                                                                                     GUEST_NAME,
  (SELECT V8_REP_NAME.NAME3 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID)                                                                                                                                     GUEST_GREET_NAME,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'DD')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_DAY,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'MM')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_MONTH,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'YYYY')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_YEAR,
  (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID)                                                                                                                                                     GROUP_RESERVATION_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID) END) NAME,
  (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                  GROUP_DETAIL_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_ID FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) COMPANY_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) TRAVELAGENT_NAME,
  (SELECT XCMS_ID FROM XCMS,YCLN,YDET WHERE XCMS.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                         TRAVELAGENT_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=3 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) SOURCE_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=4 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BOOKER_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=5 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) VENDOR_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=6 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) MPLANER_NAME,
  (SELECT YDET.YDET_XCMA_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                                               MARKETCODE_ID,
  (SELECT XCMA.XCMA_SHORTDESC FROM XCMA,YDET WHERE XCMA.XCMA_ID=YDET.YDET_XCMA_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                             MARKETCODE_DESC,
  (SELECT YRAS.YRAS_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                                   SOURCECODE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                           SOURCECODE_DESC,
  (SELECT YRAS.YRAS_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                                   CHANNELCODE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                           CHANNELCODE_DESC,
  (SELECT COUNT(YRES_S.YRES_ID) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                     NO_OF_PREV_STAYS,
  (SELECT COUNT(YDET.YDET_ID) FROM YDET,YRES YRES_S WHERE YDET.YDET_YRES_ID=YRES_S.YRES_ID AND YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)         NO_OF_PREV_ROOM_NIGHTS,
  (SELECT XPDA.XPDA_SHORTDESC FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=YRES.YRES_XCMS_ID)                                                                       VIPCODE,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN 1 ELSE 0 END)                                                                                     EXP_ROOM_MOVE_TODAY,
  (CASE WHEN YRES.YRES_YRMS_ID=(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) AND YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) THEN 1 ELSE 0 END) ROOM_MOVE_DONE_TODAY,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate)                                                                                             TODAY_ROOM_MOVE_TO_ROOM_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate)                                         TODAY_ROOM_MOVE_TO_ROOM,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN YRES.YRES_YRMS_ID ELSE (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ROOM_ID,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN (SELECT YRMS.YRMS_SHORTDESC FROM YRMS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID) ELSE (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ROOM,
  (SELECT MIN(TRUNC(YRES_S.YRES_ACTARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    FIRST_ARRIVAL,
  (SELECT MAX(TRUNC(YRES_S.YRES_ACTARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    LAST_ARRIVAL,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET YDET_S WHERE YRMS.YRMS_ID=YDET_S.YDET_YRMS_ID AND YDET_S.YDET_ID=(SELECT MAX(YDET.YDET_ID) FROM YDET WHERE YDET.YDET_YRES_ID=(SELECT YRES_S2.YRES_ID FROM YRES YRES_S2 WHERE TRUNC(YRES_S2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3 AND (YRES_ACTDEPTIME<=SYSDATE OR YRES_ACTDEPTIME>SYSDATE)) AND ROWNUM=1))) LAST_ROOMNUMBER,
  (SELECT MIN(TRUNC(YRES_S.YRES_EXPARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPARRTIME>YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    NEXT_ARRIVAL,
  (SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                 LAST_DEPARTURE,
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL,NULL,YDET.YDET_RATEAMOUNT,YDET.YDET_RATEMANUAL)+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC,0))/100))-  NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=(SELECT MAX(YDET.YDET_ID) FROM YDET WHERE YDET.YDET_YRES_ID=(SELECT YRES_S2.YRES_ID FROM YRES YRES_S2 WHERE TRUNC(YRES_S2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3 AND (YRES_ACTDEPTIME<=SYSDATE OR YRES_ACTDEPTIME>SYSDATE)) AND ROWNUM=1))) LAST_CONF_RATE,
  YRES.YRES_OPTION                                                                                                                                                                                                                         OPTIONDATE,
  (SELECT YCXL_3.YCXL_NUMBER FROM YCXL YCXL_3 WHERE YCXL_3.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                             CANCELLATION_NUMBER,
  (SELECT YCXL.YCXL_DATE FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                          CANCELLATION_DATE,
  (SELECT YCXL.YCXL_REASON FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                          CANCELLATION_REASON,
  (SELECT TRUNC(YCXL.YCXL_PCDATE) FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                  CANCELLATION_PCDATE,
  (SELECT TO_CHAR(YCXL.YCXL_PCDATE,'HH24:MI') FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                     CANCELLATION_PCTIME,
  DECODE(NVL((SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NULL),0),0,0,1)                                                                                           IS_WAITLIST,
  (SELECT YWLP.YWLP_LONGDESC FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))  WAITLIST_DESC,
  (SELECT YWLP.YWLP_PRIORITY FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))  WAITLIST_PRIORITY,
  (SELECT XCOM.XCOM_VALUE FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))    WAITLIST_COMM,
  (SELECT XCMT.XCMT_LONGDESC FROM XCMT WHERE XCMT.XCMT_ID=(SELECT XCOM.XCOM_XCMT_ID FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))) WAITLIST_COMM_TYPE,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)                                                                             COUNTRY_ID,
  (SELECT XCOU.XCOU_CUSTSHORTDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)) COUNTRY_SHORTDESC,
  (SELECT XCOU.XCOU_LONGDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END))      COUNTRY_LONGDESC,
  (SELECT SUM(YDEP.YDEP_AMOUNT_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID)                  DEPOSIT_REQUESTED,
  (SELECT MAX(YDEP.YDEP_DUEDATE_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID)              DEPOSIT_LIMIT,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.YRES_ID) DEPOSIT_PAID,
  (SELECT MAX(ZPOS.ZPOS_POSTDATE) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.YRES_ID)                                DEPOSIT_POSTDATE,
  (SELECT XCED.XCED_INITIALS FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                     ENTERED_BY_USER,
  (SELECT DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1)) FROM WMLG, XCED, XCMS WHERE XCMS.XCMS_ID=XCED.XCED_ID AND WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                     ENTERED_BY_USERNAME,
  (SELECT TO_DATE(WMLG.WMLG_TIME_CREATION) FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                ENTERED_WHEN,
  (SELECT TO_CHAR(WMLG.WMLG_TIME_CREATION,'HH24:MI') FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                ENTERED_WHEN_TIME,
  (SELECT XPDC.XPDC_VALUE FROM XPDC, XPDG WHERE XPDC.XPDC_XPDG_ID = XPDG.XPDG_ID AND XPDG.XPDG_ROLE=1 AND XPDC.XPDC_XCMS_ID = YRES.YRES_XCMS_ID AND ROWNUM=1)                    PASSPORT,
  (SELECT XCOU.XCOU_NATIONALITY FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                   NATIONALITY,
  (SELECT XCOU.XCOU_ISO2 FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                       NATIONALITY_ISO2,
  (SELECT XCOU.XCOU_ISO3 FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                       NATIONALITY_ISO3,
  (SELECT COUNT(YRES_ID) FROM YRES NS WHERE NS.YRES_NOAVAILREASON = 4 AND NS.YRES_XCMS_ID = YRES.YRES_XCMS_ID)                                                        NOSHOWS,
  (SELECT COUNT(YRES_ID) FROM YRES NS WHERE NS.YRES_NOAVAILREASON = 3 AND NS.YRES_XCMS_ID = YRES.YRES_XCMS_ID)                                                        CANCELLATIONS,
  V8_FNC_VALCURR_FORMATING(
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL, NULL, decode(YDET.YDET_FOREIGNRATEAMOUNT, null, YDET.YDET_RATEAMOUNT, YDET.YDET_FOREIGNRATEAMOUNT),decode(YDET.YDET_FOREIGNRATEMANUAL,null,YDET.YDET_RATEMANUAL,decode(YDET.YDET_FOREIGNRATEMANUAL,0,YDET.YDET_RATEMANUAL,YDET.YDET_FOREIGNRATEMANUAL)))+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC, 0))/100))-NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, YPAL.YPAL_VALUE*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id)) FCONF_RATE_GS,
  V8_FNC_VALCURR_FORMATING((SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1),
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id)) FCONF_RATE_STAY_GS
FROM
  YRES
where
  (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0)
  OR
  ((SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND NVL(YDET.YDET_YGRP_ID,0)<>0)>1);


CREATE OR REPLACE VIEW V8_EDITOR_GPS
AS 
SELECT
  YRES.YRES_ID                                                      RESERVATION_ID,
  YRES.YRES_YBOM_ID                                                                                     BOOKINGMASTER_ID,
  YRES.YRES_CURRENT_YDET_ID                                 RESERVATION_DETAIL_ID,
  YRES.YRES_EXPARRTIME                                              ARRIVAL,
  YRES.YRES_EXPDEPTIME                                         DEPARTURE,
  (YRES.YRES_EXPDEPTIME) - (YRES.YRES_EXPARRTIME) NIGHTS,
  TO_CHAR(YRES.YRES_EXPARRHOUR,'HH24:MI')                     ARRIVALTIME,
  TO_CHAR(YRES.YRES_EXPDEPHOUR,'HH24:MI')                     DEPARTURETIME,
  (select YDET.YDET_YCAT_ID from ydet where ydet.ydet_id=YRES.YRES_CURRENT_YDET_ID) YCAT_ID,
  (SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                               NOOFROOMS,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMTYPE,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMTYPE_DESC,
  WTRL_TRANSLATE('YCAT',(SELECT YDET.YDET_YCAT_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),'YCAT_LONGDESC',(SELECT XCMS.XCMS_WLAN_ID from XCMS where XCMS.XCMS_ID=YRES.YRES_XCMS_ID)) TRANSLATED_TYPE_DESC,
  (SELECT YGCO.YGCO_SHORTDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID)                                                        GUARANTEECODE,
  (SELECT YGCO.YGCO_LONGDESC FROM YGCO WHERE YGCO.YGCO_ID=YRES.YRES_YGCO_ID)                                                      GUARANTEECODE_DESC,
  DECODE(YRES.YRES_RESSTATUS,0,'TENTATIVE', 1,'DEFINITE',3,'NOT')                                                                 RESSTATUS,
  DECODE(YRES.YRES_NOAVAILREASON,1,'WAITLIST',2,'OFFER',3,'CANCELED',4,'NOSHOW')                                                  NOAVAILREASON,
  YRES.YRES_WALKIN                                                                                                                  WALKIN,
  YRES.YRES_FREETEXT1                                                                                                              FREETEXT1,
  YRES.YRES_FREETEXT2                                                                                                              FREETEXT2,
  NVL(YRES.YRES_HASSHARE,0)                                                                                                      HASSHARE,
  DECODE(YRES.YRES_HASSHARE,1,(DECODE(YRES.YRES_SHARETYPE,0,'INDEPENDANT',1,'MASTER',2,'SLAVE')),' ')                             SHARETYPE,
  (SELECT YDET.YDET_ADULTNO FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                              ADULTNO,
  NVL((SELECT SUM(YRGL_NUMBER) FROM YRGL,YDET WHERE YRGL.YRGL_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID ),0) CHILDNO,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                   ROOM_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMNUMBER,
  (SELECT YRMS.YRMS_LONGDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)      ROOMNUMBER_LDESC,  
  (SELECT YDET.YDET_YCAT_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                ROOMTYPE_ID,
  (SELECT YDET.YDET_YCAT_ID_RATE FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                          RATEROOMTYPE_ID,
  (SELECT YCAT.YCAT_SHORTDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) RATEROOMTYPE,
  (SELECT YCAT.YCAT_LONGDESC FROM YCAT,YDET WHERE YCAT.YCAT_ID=YDET.YDET_YCAT_ID_RATE AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) RATEROOMTYPE_DESC,
  (SELECT YBLH.YBLH_ID FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BLOCK_ID,
  (SELECT YBLH.YBLH_SHORTDESC FROM YBLH,YBLD,YDET WHERE YBLH.YBLH_ID=YBLD.YBLD_YBLH_ID AND YBLD.YBLD_ID=YDET.YDET_YBLD_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BLOCK_DESC,
  NVL((SELECT YDET.YDET_RATEAMOUNT FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                          RATEAMOUNT,
  NVL((SELECT YDET.YDET_RATEDIFF FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                      RATEDIFF,
  NVL((SELECT YDET.YDET_RATEDISCOUNTPERC FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                              RATEDISCOUNTPERC,
  (SELECT NVL(YDET.YDET_RATEMANUAL,0) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                      RATEMANUAL,
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL, NULL, decode(YDET.YDET_FOREIGNRATEAMOUNT, null, YDET.YDET_RATEAMOUNT, YDET.YDET_FOREIGNRATEAMOUNT),decode(YDET.YDET_FOREIGNRATEMANUAL,null,YDET.YDET_RATEMANUAL,decode(YDET.YDET_FOREIGNRATEMANUAL,0,YDET.YDET_RATEMANUAL,YDET.YDET_FOREIGNRATEMANUAL)))+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC, 0))/100))-NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, YPAL.YPAL_VALUE*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) CONF_RATE,
  V8_FNC_NUM_TO_CHAR_FORMATING((SELECT (((DECODE(YDET.YDET_RATEMANUAL, NULL, decode(YDET.YDET_FOREIGNRATEAMOUNT, null, YDET.YDET_RATEAMOUNT, YDET.YDET_FOREIGNRATEAMOUNT),decode(YDET.YDET_FOREIGNRATEMANUAL,null,YDET.YDET_RATEMANUAL,decode(YDET.YDET_FOREIGNRATEMANUAL,0,YDET.YDET_RATEMANUAL,YDET.YDET_FOREIGNRATEMANUAL)))+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC, 0))/100))-NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, YPAL.YPAL_VALUE*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CONF_RATE_GS,
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id) Currency,
  ((SELECT ((DECODE(YDET.YDET_RATEMANUAL,NULL,YDET.YDET_RATEAMOUNT,YDET.YDET_RATEMANUAL)+NVL(YDET.YDET_RATEDIFF,0)) * (100-NVL(YDET.YDET_RATEDISCOUNTPERC,0)))/100 FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)) CALC_PRICE,
  (SELECT (NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                         ADD_ON_PACKAGES,
  NVL((SELECT SUM(YFXC.YFXC_AMOUNT*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)                                                                     TOTAL_FIX_CHARGES,
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL,NULL,YDET.YDET_RATEAMOUNT,YDET.YDET_RATEMANUAL)+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC,0))/100))-  NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) + NVL((SELECT SUM(YFXC.YFXC_AMOUNT*YFXC.YFXC_QUANTITY) FROM YFXC,YDET WHERE YFXC.YFXC_YDET_ID=YDET.YDET_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) CONF_RATE_FIXCHARGE,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID)                                                                  CONF_RATE_FIXCHARGE_STAY,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1)                                          CONF_RATE_STAY,
  V8_FNC_NUM_TO_CHAR_FORMATING((SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1))              CONF_RATE_STAY_GS,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE=1)                                          FIXCHARGE_STAY,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS,YDET WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID and ydet_id=nvl(YRES.YRES_CURRENT_YDET_ID,YRES.YRES_LAST_YDET_ID) and YDET.YDET_DATE=YPOS.YPOS_DATE AND YPOS.YPOS_DT<>2 )    	RATE_NET,
  (SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID  AND YPOS.YPOS_DT<>2 )                                                                  											    RATE_NET_STAY,
  (SELECT YDET.YDET_YRCH_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                                               RATECODE_ID,
  (SELECT YRCH.YRCH_SHORTDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE,
  (SELECT YRCH.YRCH_LONGDESC FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_DESC,
  (SELECT YRCH.YRCH_RATEINFO FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_INFO,
  (SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                 RATECODE_TEXT,
  DECODE((NVL((SELECT YRCH.YRCH_COMPSTATUS FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0)),1,'COMP',2,'HOUSE USE','')                                                     COMPSTATUS,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN 1 ELSE 0 END)                                                                                                                                       UNSPLITTED_GROUP_RES,
  YRES.YRES_XCMS_ID                                                                                                                                                                                                                   GUEST_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID)                                                                                                                                     GUEST_NAME,
  (SELECT V8_REP_NAME.NAME3 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID)                                                                                                                                     GUEST_GREET_NAME,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'DD')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_DAY,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'MM')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_MONTH,
  (SELECT TO_NUMBER(TO_CHAR(XCID.XCID_BIRTHTIME, 'YYYY')) FROM XCID WHERE XCID.XCID_ID=YRES.YRES_XCMS_ID)                                                                                                                      GUEST_BIRTHDATE_YEAR,
  (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID)                                                                                                                                                     GROUP_RESERVATION_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME WHERE V8_REP_NAME.XCMS_ID=YRES.YRES_XCMS_ID) END) NAME,
  (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                  GROUP_DETAIL_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_NAME FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_NAME FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_NAME,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT YGRP.YGRP_ID FROM YGRP,YDET WHERE YGRP.YGRP_ID=YDET.YDET_YGRP_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) END) GROUP_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) COMPANY_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) TRAVELAGENT_NAME,
  (SELECT XCMS_ID FROM XCMS,YCLN,YDET WHERE XCMS.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                         TRAVELAGENT_ID,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=3 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) SOURCE_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=4 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) BOOKER_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=5 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) VENDOR_NAME,
  (SELECT V8_REP_NAME.NAME2 FROM V8_REP_NAME,YCLN,YDET WHERE V8_REP_NAME.XCMS_ID=YCLN.YCLN_XCMS_ID AND YCLN.YCLN_YDET_ID=YDET.YDET_ID AND YCLN.YCLN_XCCA_INTERNALCATEGORY=6 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) MPLANER_NAME,
  (SELECT YDET.YDET_XCMA_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                                                                               MARKETCODE_ID,
  (SELECT XCMA.XCMA_SHORTDESC FROM XCMA,YDET WHERE XCMA.XCMA_ID=YDET.YDET_XCMA_ID AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID)                                                                                             MARKETCODE_DESC,
  (SELECT YRAS.YRAS_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                                   SOURCECODE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 1 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                           SOURCECODE_DESC,
  (SELECT YRAS.YRAS_ID FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                                   CHANNELCODE_ID,
  (SELECT YRAS.YRAS_SHORTDESC FROM YRAL,YRAS,YRAC,YDET WHERE YRAL.YRAL_YDET_ID = YDET.YDET_ID AND YRAL.YRAL_YRAS_ID = YRAS.YRAS_ID AND YRAL.YRAL_YRAC_ID = YRAC.YRAC_ID AND YRAC.YRAC_ID = 2 AND YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND ROWNUM=1)                           CHANNELCODE_DESC,
  (SELECT COUNT(YRES_S.YRES_ID) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                     NO_OF_PREV_STAYS,
  (SELECT COUNT(YDET.YDET_ID) FROM YDET,YRES YRES_S WHERE YDET.YDET_YRES_ID=YRES_S.YRES_ID AND YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)         NO_OF_PREV_ROOM_NIGHTS,
  (SELECT XPDA.XPDA_SHORTDESC FROM XPDA,XPAL WHERE XPDA.XPDA_ID=XPAL.XPAL_XPDA_ID AND XPAL.XPAL_XPAC_ID=11 AND XPAL.XPAL_XCMS_ID=YRES.YRES_XCMS_ID)                                                                       VIPCODE,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN 1 ELSE 0 END)                                                                                     EXP_ROOM_MOVE_TODAY,
  (CASE WHEN YRES.YRES_YRMS_ID=(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) AND YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) THEN 1 ELSE 0 END) ROOM_MOVE_DONE_TODAY,
  (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate)                                                                                             TODAY_ROOM_MOVE_TO_ROOM_ID,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate)                                         TODAY_ROOM_MOVE_TO_ROOM,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN YRES.YRES_YRMS_ID ELSE (SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ROOM_ID,
  (CASE WHEN YRES.YRES_YRMS_ID<>(SELECT YDET.YDET_YRMS_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID) THEN (SELECT YRMS.YRMS_SHORTDESC FROM YRMS WHERE YRMS.YRMS_ID=YRES.YRES_YRMS_ID) ELSE (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_YRES_ID=YRES.YRES_ID AND YDET.YDET_DATE=V8_sys_fideliodate - 1) END) TODAY_ROOM_MOVE_FROM_ROOM,
  (SELECT MIN(TRUNC(YRES_S.YRES_ACTARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    FIRST_ARRIVAL,
  (SELECT MAX(TRUNC(YRES_S.YRES_ACTARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    LAST_ARRIVAL,
  (SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET YDET_S WHERE YRMS.YRMS_ID=YDET_S.YDET_YRMS_ID AND YDET_S.YDET_ID=(SELECT MAX(YDET.YDET_ID) FROM YDET WHERE YDET.YDET_YRES_ID=(SELECT YRES_S2.YRES_ID FROM YRES YRES_S2 WHERE TRUNC(YRES_S2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3) AND ROWNUM=1))) LAST_ROOMNUMBER,
  (SELECT MIN(TRUNC(YRES_S.YRES_EXPARRTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPARRTIME>YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                    NEXT_ARRIVAL,
  (SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3)                                 LAST_DEPARTURE,
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL,NULL,YDET.YDET_RATEAMOUNT,YDET.YDET_RATEMANUAL)+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC,0))/100))-  NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED,0,YPAL.YPAL_VALUE,0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=(SELECT MAX(YDET.YDET_ID) FROM YDET WHERE YDET.YDET_YRES_ID=(SELECT YRES_S2.YRES_ID FROM YRES YRES_S2 WHERE TRUNC(YRES_S2.YRES_ACTDEPTIME)=(SELECT MAX(TRUNC(YRES_S.YRES_ACTDEPTIME)) FROM YRES YRES_S WHERE YRES_S.YRES_XCMS_ID=YRES.YRES_XCMS_ID AND YRES_S.YRES_EXPDEPTIME<YRES.YRES_EXPARRTIME AND YRES_S.YRES_RESSTATUS<>3) AND ROWNUM=1))) LAST_CONF_RATE,
  YRES.YRES_OPTION                                                                                                                                                                                                                         OPTIONDATE,
  (SELECT YCXL_3.YCXL_NUMBER FROM YCXL YCXL_3 WHERE YCXL_3.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                             CANCELLATION_NUMBER,
  (SELECT YCXL.YCXL_DATE FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                          CANCELLATION_DATE,
  (SELECT YCXL.YCXL_REASON FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                          CANCELLATION_REASON,
  (SELECT TRUNC(YCXL.YCXL_PCDATE) FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                                  CANCELLATION_PCDATE,
  (SELECT TO_CHAR(YCXL.YCXL_PCDATE,'HH24:MI') FROM YCXL WHERE YCXL.YCXL_ID=(SELECT MAX(YCXL_2.YCXL_ID) FROM YCXL YCXL_2 WHERE YCXL_2.YCXL_YRES_ID=YRES.YRES_ID AND YCXL_2.YCXL_CANCELORREINSTATE=1))                     CANCELLATION_PCTIME,
  DECODE(NVL((SELECT MAX(YWLT.YWLT_ID) FROM YWLT WHERE YWLT.YWLT_YRES_ID=YRES.YRES_ID AND YWLT.YWLT_ACCEPTEDDATE IS NULL),0),0,0,1)                                                                                           IS_WAITLIST,
  (SELECT YWLP.YWLP_LONGDESC FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))  WAITLIST_DESC,
  (SELECT YWLP.YWLP_PRIORITY FROM YWLP WHERE YWLP.YWLP_ID=(SELECT YWLT.YWLT_YWLP_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))  WAITLIST_PRIORITY,
  (SELECT XCOM.XCOM_VALUE FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))    WAITLIST_COMM,
  (SELECT XCMT.XCMT_LONGDESC FROM XCMT WHERE XCMT.XCMT_ID=(SELECT XCOM.XCOM_XCMT_ID FROM XCOM WHERE XCOM.XCOM_ID =(SELECT YWLT.YWLT_XCOM_ID FROM YWLT WHERE YWLT.YWLT_ID=(SELECT MAX(YWLT_2.YWLT_ID) FROM YWLT YWLT_2 WHERE YWLT_2.YWLT_YRES_ID=YRES.YRES_ID AND YWLT_2.YWLT_ACCEPTEDDATE IS NULL)))) WAITLIST_COMM_TYPE,
  (CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)                                                                             COUNTRY_ID,
  (SELECT XCOU.XCOU_CUSTSHORTDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END)) COUNTRY_SHORTDESC,
  (SELECT XCOU.XCOU_LONGDESC FROM XCOU WHERE XCOU.XCOU_ID=(CASE WHEN (NVL(YRES.YRES_XCMS_ID,0)=0 AND NVL(YRES.YRES_YGRP_ID,0)<>0) THEN (SELECT YGRP.YGRP_XCOU_ID FROM YGRP WHERE YGRP.YGRP_ID=YRES.YRES_YGRP_ID) ELSE (SELECT XADR.XADR_XCOU_ID FROM XADR WHERE XADR.XADR_XCMS_ID=YRES.YRES_XCMS_ID AND XADR.XADR_PRIMARY=1 AND ROWNUM=1) END))      COUNTRY_LONGDESC,
  (SELECT SUM(YDEP.YDEP_AMOUNT_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID)                  DEPOSIT_REQUESTED,
  (SELECT MAX(YDEP.YDEP_DUEDATE_RMS) FROM YDEP WHERE YDEP.YDEP_YRES_ID=YRES.YRES_ID)              DEPOSIT_LIMIT,
  (SELECT NVL((SUM(ZPOS.ZPOS_QUANTITY * ZPOS.ZPOS_UNITPRICE)),0) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.YRES_ID) DEPOSIT_PAID,
  (SELECT MAX(ZPOS.ZPOS_POSTDATE) FROM ZPOS WHERE ZPOS.ZPOS_CDT= 5 AND ZPOS.ZPOS_LEDGERSTATUS=1 AND ZPOS.ZPOS_YRES_ID=YRES.YRES_ID)                                DEPOSIT_POSTDATE,
  (SELECT XCED.XCED_INITIALS FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                     ENTERED_BY_USER,
  (SELECT DECODE(XCMS_TYPE,1,XCMS_NAME1||' '||XCMS_NAME2||' '||XCMS_NAME3,TRIM(DECODE(NVL(XCMS_NAME3,''),'','',XCMS_NAME3||' ')||DECODE(NVL(XCMS_NAME2,''),'','',XCMS_NAME2||' ')||XCMS_NAME1)) FROM WMLG, XCED, XCMS WHERE XCMS.XCMS_ID=XCED.XCED_ID AND WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                     ENTERED_BY_USERNAME,
  (SELECT TO_DATE(WMLG.WMLG_TIME_CREATION) FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                ENTERED_WHEN,
  (SELECT TO_CHAR(WMLG.WMLG_TIME_CREATION,'HH24:MI') FROM WMLG, XCED WHERE WMLG.WMLG_TABLENAME='YRES' AND WMLG.WMLG_XCMS_UPDATE=XCED_ID AND WMLG.WMLG_TABLE_ID=YRES_ID AND ROWNUM=1)                ENTERED_WHEN_TIME,
  (SELECT XPDC.XPDC_VALUE FROM XPDC, XPDG WHERE XPDC.XPDC_XPDG_ID = XPDG.XPDG_ID AND XPDG.XPDG_ROLE=1 AND XPDC.XPDC_XCMS_ID = YRES.YRES_XCMS_ID AND ROWNUM=1)                    PASSPORT,
  (SELECT XCOU.XCOU_NATIONALITY FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                   NATIONALITY,
  (SELECT XCOU.XCOU_ISO2 FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                       NATIONALITY_ISO2,
  (SELECT XCOU.XCOU_ISO3 FROM XCOU, XCMS WHERE XCOU.XCOU_ID = XCMS.XCMS_XCOU_ID_NATION AND XCMS.XCMS_ID = YRES.YRES_XCMS_ID)                                       NATIONALITY_ISO3,
  (SELECT COUNT(YRES_ID) FROM YRES NS WHERE NS.YRES_NOAVAILREASON = 4 AND NS.YRES_XCMS_ID = YRES.YRES_XCMS_ID) 												       NOSHOWS,
  (SELECT COUNT(YRES_ID) FROM YRES NS WHERE NS.YRES_NOAVAILREASON = 3 AND NS.YRES_XCMS_ID = YRES.YRES_XCMS_ID) 												       CANCELLATIONS,
  V8_FNC_VALCURR_FORMATING(
  (SELECT (((DECODE(YDET.YDET_RATEMANUAL, NULL, decode(YDET.YDET_FOREIGNRATEAMOUNT, null, YDET.YDET_RATEAMOUNT, YDET.YDET_FOREIGNRATEAMOUNT),decode(YDET.YDET_FOREIGNRATEMANUAL,null,YDET.YDET_RATEMANUAL,decode(YDET.YDET_FOREIGNRATEMANUAL,0,YDET.YDET_RATEMANUAL,YDET.YDET_FOREIGNRATEMANUAL)))+NVL(YDET.YDET_RATEDIFF,0))*((100-NVL(YDET.YDET_RATEDISCOUNTPERC, 0))/100))-NVL((SELECT SUM(DECODE(YPAL.YPAL_INCLUDED, 0, YPAL.YPAL_VALUE*ypal_multiplier, 0)) FROM YPAL WHERE YPAL.YPAL_YDET_ID=YDET.YDET_ID),0)) FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id)) FCONF_RATE_GS,
  V8_FNC_VALCURR_FORMATING((SELECT SUM(YPOS.YPOS_QUANTITY* YPOS.YPOS_UNITPRICE) FROM YPOS WHERE YPOS.YPOS_YRES_ID=YRES.YRES_ID AND YPOS.YPOS_TYPE<>1),
  (SELECT zcur.zcur_shortdesc FROM zcur WHERE zcur_id = yres_contract_zcur_id)) FCONF_RATE_STAY_GS
FROM
  YRES
where
  (NVL(YRES.YRES_XCMS_ID,0)<>0 AND NVL(YRES.YRES_YGRP_ID,0)=0) and
  (NVL((SELECT YDET.YDET_YGRP_ID FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID),0) <> 0) and
  ((SELECT YDET.YDET_NOOFROOMS FROM YDET WHERE YDET.YDET_ID=YRES.YRES_CURRENT_YDET_ID AND NVL(YDET.YDET_YGRP_ID,0)<>0)<2) and
  (YRES.YRES_EXPDEPTIME > SYSDATE - 100);


CREATE OR REPLACE FORCE VIEW V8_ED_FOLIO_REV_ORI_ARR_PS
AS 
SELECT
  ZPOS_POSTDATE_S DAT,
  to_number(to_char(ZPOS_POSTDATE_S,'J')) DAT_J,
  (SELECT ZPOS.ZPOS_CDT FROM ZPOS WHERE ZPOS.ZPOS_ID=ZPOS_ID_S) CDT,
  ZPOS_ID_S ZPOS_ID,
  ZPOS_INTERNALGROUP_ID_S ZPOS_INTERNALGROUP_ID,
  -- Quantity makes no sense in this Style decode((select zpos.ZPOS_PAIDOUT from zpos where zpos.ZPOS_ID=ZPOS_ID_S),1,-1,1)*(select zpos.ZPOS_QUANTITY from zpos where zpos.ZPOS_ID=ZPOS_ID_S) QTY,
  GROSS_S GROSS,
  PAY_S PAY,
  REV_S REV,
  IS_RES,
  IS_FA,
  ORI_RES_FA_ID,
  ARRIVAL,
  DEPARTURE,
  ZFAC_OR_YRES,
  (SELECT ZDCO.ZDCO_NUMERICDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=ZPOS_ID_S) ZDCO_NUMERICDESC,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_CL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)<>2 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SPAY_NOCL,
  (SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) SREV,
  (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) OPENA,
  --(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID) RES_GUEST_NAME,
  --(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID) FA_GUEST_NAME,
  (DECODE(IS_FA,0,(SELECT V8_REP_NAME.NAME6 FROM YRES,V8_REP_NAME WHERE YRES.YRES_ID=ORI_RES_FA_ID AND YRES.YRES_XCMS_ID=V8_REP_NAME.XCMS_ID),(SELECT V8_REP_NAME.NAME6 FROM ZFAC,V8_REP_NAME WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID AND ZFAC.ZFAC_XCMS_ID=V8_REP_NAME.XCMS_ID))) REAL_ORIGINATOR_NAME,
  (SELECT ZPCF.ZPCF_CUSTOMORIGINATORNAME FROM ZPCF WHERE ZPCF.ZPCF_ZPOS_ID=ZPOS_ID_S) ORIGINATOR_NAME,
  (DECODE(IS_FA,0,(NVL((SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID),(SELECT YRMS.YRMS_SHORTDESC FROM YRMS,YDET WHERE YRMS.YRMS_ID=YDET.YDET_YRMS_ID AND YDET.YDET_DATE+1=ZPOS_POSTDATE_S AND YDET.YDET_YRES_ID=ORI_RES_FA_ID))),(NVL((SELECT ZFAN.ZFAN_LONGDESC FROM ZFAN,ZFAC WHERE ZFAN.ZFAN_ID=ZFAC.ZFAC_ZFAN_ID AND ZFAC.ZFAC_ID=ORI_RES_FA_ID),(SELECT ZFAC.ZFAC_AUTONUMBER FROM ZFAC WHERE ZFAC.ZFAC_ID=ORI_RES_FA_ID))))) ORI_RES_FA_DESC,
  TO_CHAR((CASE WHEN (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)=0 AND NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  
  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0)<>0 THEN NVL((SELECT SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND (SELECT ZDCO.ZDCO_PAYM_TYPE FROM ZDCO WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID)=2  AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S),0) ELSE (SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1, -ZPOS.zpos_unitprice*ZPOS.zpos_quantity, NULL),DECODE(ZPOS.ZPOS_CDT,5,0, ZPOS.zpos_sumupamount))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0) AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S)-(SELECT NVL(SUM(DECODE(ZPOS.zpos_cdt,5,DECODE(ZPOS.zpos_paidout,1,TO_NUMBER(NULL),(ZPOS.zpos_unitprice*ZPOS.zpos_quantity)),TO_NUMBER(NULL))),0) FROM ZPOS WHERE (zpos_zall_id IS NULL OR zpos_zall_id=0)
  AND ZPOS.zpos_id=ZPOS.zpos_internalsplit_zpos_id AND NVL(ZPOS.ZPOS_HIDDEN,0)=0 AND NVL(ZPOS.ZPOS_NOREPRINT,0)<>1 AND ZPOS.ZPOS_ZWIN_ID = ZWIN_ID_S) END)*100,'999999990,09') FSPAY_CL,
  (DECODE(INDICATE_ZPOS_ID,0,(Wtrl_Translate('ZARR',(DECODE(INDICATE_ZPOS_ID,0,(SELECT ZARR.ZARR_ID  FROM ZARR WHERE ZARR.ZARR_ID=ZARR_OR_ZPOS_S),0)),'ZARR_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))),
  (CASE WHEN (((SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))))=(SELECT ZDCO.ZDCO_LONGDESC FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))))
  OR (SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))=(SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1))
  THEN
  (DECODE(
  (SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),
  (SELECT YRCH.YRCH_FOLIOTEXT FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1)),
  DECODE(NVL((SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1)),0),0,'',Wtrl_Translate('YRCH',(SELECT YRCH.YRCH_ID FROM YRCH,YDET WHERE YRCH.YRCH_ID=YDET.YDET_YRCH_ID AND YDET.ydet_date=TRUNC(ZPOS_POSTDATE_S) AND YDET.YDET_YRES_ID=(SELECT ZPOS.ZPOS_ORIGINATED_YRES_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))) AND ROWNUM=1)),'YRCH_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))),
  (DECODE(NVL((SELECT ZDCO.ZDCO_FOLIOTEXT FROM ZDCO WHERE ZDCO.ZDCO_ID=(SELECT ZPOS.ZPOS_ZDCO_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S))))),' '),' ',
  DECODE(NVL((SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),0),0,' ',(Wtrl_Translate('ZDCO',(SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),'ZDCO_LONGDESC',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1))))),
  DECODE(NVL((SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),0),0,' ',(Wtrl_Translate('ZDCO',(SELECT ZDCO.ZDCO_ID FROM ZDCO,ZPOS WHERE ZDCO.ZDCO_ID=ZPOS.ZPOS_ZDCO_ID AND ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))),'ZDCO_FOLIOTEXT',NVL((DECODE((SELECT XCCA.XCCA_XCMSTYPE FROM XCCA,XCMS WHERE XCCA.XCCA_ID=XCMS.XCMS_XCCA_ID AND XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),2,(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_XCMS_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)),(SELECT XCMS.XCMS_WLAN_ID FROM XCMS WHERE XCMS.XCMS_ID=(SELECT ZWIN.ZWIN_INVOICE_TO_CONTACT_ID FROM ZWIN WHERE ZWIN.ZWIN_ID=ZWIN_ID_S)))),(SELECT WLAN.WLAN_ID FROM WLAN WHERE WLAN.WLAN_DEFAULT=1)))))
  ))))
  ELSE (SELECT ZPOS.ZPOS_DESCRIPT FROM ZPOS WHERE ZPOS.ZPOS_ID=(DECODE(INDICATE_ZPOS_ID,0,0,(SELECT ZPOS.ZPOS_ID FROM ZPOS WHERE ZPOS.ZPOS_ID=ZARR_OR_ZPOS_S)))) END)
  )) DDESC,
  ZARR_OR_ZPOS_S ZARR_OR_ZPOS, 
  INDICATE_ZPOS_ID, 
  decode(INDICATE_ZPOS_ID,1,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z2.ZPOS_ZTCD_ID from zpos Z2 where Z2.ZPOS_INTERNALSPLIT_ZPOS_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)) end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then ' ' else (select ZTXC.ZTXC_LONGDESC from ztxc, ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=(select distinct Z4.ZPOS_ZTCD_ID from zpos Z3, zpos Z4 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S  and Z3.ZPOS_TAXLINK_ID=Z4.ZPOS_TAXLINK_ID and Z3.ZPOS_POSTDATE=ZPOS_POSTDATE_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S and Z4.ZPOS_ZTCD_ID is not null and rownum=1)) end)) TAXCODE_DESC, 
  decode(INDICATE_ZPOS_ID,0,(case when (select count( distinct Z2.ZPOS_ZTCD_ID) from zpos Z2 where Z2.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z2.ZPOS_ZTCD_ID is not null)>1 then 1 else 0 end ),(case when (select count( distinct Z3.ZPOS_ZTCD_ID) from zpos Z3 where Z3.ZPOS_ZARR_ID=ZARR_OR_ZPOS_S and Z3.ZPOS_ZWIN_ID=ZWIN_ID_S)>1 then 1 else 0 end)) MULTI_TAXCODE_EXISTS, 
  ZWIN_ID_S ZWIN_ID
FROM
(
SELECT
--  distinct
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
  MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  1 IS_RES,
  0 IS_FA,
  ntd.ZPOS_ORIGINATED_YRES_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  (SELECT YRES.YRES_EXPARRTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) ARRIVAL,
  (SELECT YRES.YRES_EXPDEPTIME FROM YRES WHERE YRES.YRES_ID=NTD.ZPOS_ORIGINATED_YRES_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)=0
  --AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID, ntd.ZPOS_ORIGINATED_YRES_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID), DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
UNION ALL
SELECT
--  distinct
  MIN(ntd.ZPOS_POSTDATE) ZPOS_POSTDATE_S,
  MIN(ntd.ZPOS_ID) ZPOS_ID_S,
 MIN(NTD.ZPOS_INTERNALGROUP_ID) ZPOS_INTERNALGROUP_ID_S,
  0 IS_RES,
  1 IS_FA,
  ntd.ZPOS_ORIGINATED_ZFAC_ID ORI_RES_FA_ID,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,(-ntd.zpos_unitprice*ntd.zpos_quantity),-(ntd.zpos_unitprice*ntd.zpos_quantity)),DECODE(ntd.ZPOS_CDT,5,0,ntd.zpos_sumupamount))) GROSS_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1,NULL,(ntd.zpos_unitprice*ntd.zpos_quantity)), NULL)) PAY_S,
  SUM(DECODE(ntd.zpos_cdt,5,DECODE(ntd.zpos_paidout,1, -ntd.zpos_unitprice*ntd.zpos_quantity, NULL),DECODE(ntd.ZPOS_CDT,5,0, ntd.zpos_sumupamount))) REV_S,
  ZWIN.ZWIN_ID ZWIN_ID_S,
  (SELECT ZFAC.ZFAC_EXPVALIDFROM FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) ARRIVAL,
  (SELECT ZFAC.ZFAC_EXPVALIDUNTIL FROM ZFAC WHERE ZFAC.ZFAC_ID=ntd.ZPOS_ORIGINATED_ZFAC_ID) DEPARTURE,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0) INDICATE_ZPOS_ID,
  DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID) ZARR_OR_ZPOS_S,
  DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0) ZFAC_OR_YRES
FROM
  ZPOS ntd,
  ZWIN ZWIN,
  ZDCO
WHERE
  (zpos_zall_id IS NULL OR zpos_zall_id=0) AND
  ntd.zpos_id=ntd.zpos_internalsplit_zpos_id AND
  zdco_id=ntd.zpos_zdco_id AND
  ntd.ZPOS_ZWIN_ID = ZWIN.ZWIN_ID(+) AND
  NVL(ntd.ZPOS_HIDDEN,0)=0
  AND NVL(ntd.ZPOS_NOREPRINT,0)<>1
  AND nvl(NTD.ZPOS_ORIGINATED_ZFAC_ID,0)<>0
  --AND ZWIN.ZWIN_ID=:WINID
GROUP BY ZWIN.ZWIN_ID, ntd.ZPOS_ORIGINATED_ZFAC_ID, DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ZARR_ID),DECODE(NVL(ntd.ZPOS_ORIGINATED_YRES_ID,0),0,1,0), DECODE(NVL(ntd.ZPOS_ZARR_ID,0),0,1,0)--,decode(nvl(ntd.ZPOS_ZARR_ID,0),0,ntd.ZPOS_ID,ntd.ZPOS_ID)-- ntd.ZPOS_ID--,zdco.ZDCO_ID--, ntd.ZPOS_ID, ntd.ZPOS_ZARR_ID
) WHERE ABS(nvl(REV_S,PAY_S))<>(DECODE((SELECT WUSS.WUSS_VALUE FROM WUSS WHERE WUSS.WUSS_XCMS_ID=0 AND WUSS.WUSS_NAME='Show 0 Postings on Billing Screen'),1,-1,0)) ORDER BY CDT ASC;
  
  
CREATE OR REPLACE FORCE VIEW V8_ED_FOLIO_REV_GCHK_G_TAX_DET
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
SPLITID_S SPLITID,
ZCHK_OR_ZPOS_S ZCHK_OR_ZPOS,
--DESCRIPT_S DESCRIPT,
TAX_CODE_S TAX_CODE,
GROSS_REV_S GROSS_REV,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID_S,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
case when nvl((select z2.zpos_poscheck_nr from zpos z2 where z2.zpos_id=z1.zpos_internalsplit_zpos_id),'0')='0' then to_char(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) else to_char(Z1.ZPOS_POSCHECK_NR) end   SPLITID_S2,
to_char(DECODE(NVL(Z1.ZPOS_POSCHECK_NR,' '),' ',to_char(Z1.ZPOS_ID),Z1.ZPOS_POSCHECK_NR)) ZCHK_OR_ZPOS_S2,
--Z1.ZPOS_DESCRIPT DESCRIPT_S2,
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT<>2
and (Z1.ZPOS_POSCHECK_NR) is not null
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2--, DESCRIPT_S2
, ZCHK_OR_ZPOS_S2
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
SPLITID_S SPLITID,
ZCHK_OR_ZPOS_S ZCHK_OR_ZPOS,
--DESCRIPT_S DESCRIP,
TAX_CODE_S TAC_TAX,
GROSS_REV_S GROSS_REV,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
SPLITID_S2 SPLITID_S,
ZCHK_OR_ZPOS_S2 ZCHK_OR_ZPOS_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) SPLITID_S2,
to_CHAR(Z1.ZPOS_INTERNALSPLIT_ZPOS_ID) ZCHK_OR_ZPOS_S2,
--Z1.ZPOS_DESCRIPT DESCRIPT_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT<>2
and (Z1.ZPOS_POSCHECK_NR) is Null
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2--, DESCRIPT_S2
,ZCHK_OR_ZPOS_S2
);


CREATE OR REPLACE FORCE VIEW V8_ED_FOLIO_REV_APD_G_TAX_DESC
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
ZPOS_POSTDATE_S ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S SPLITID,
--DESCRIPT_S DESCRIPT,
TAX_CODE_S TAX_CODE,
GROSS_REV_S GROSS_REV,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE_S,
SPLITID_S2 SPLITID_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2, 
Z1.ZPOS_ZARR_ID SPLITID_S2, 
--Z1.ZPOS_DESCRIPT DESCRIPT_S2,
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,ZPOS_POSTDATE_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
ZPOS_POSTDATE_S ZPOS_POSTDATE,
to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S SPLITID,
--DESCRIPT_S DESCRIP,
TAX_CODE_S TAC_TAX,
GROSS_REV_S GROSS_REV,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
ZPOS_POSTDATE_S2 ZPOS_POSTDATE_S,
SPLITID_S2 SPLITID_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
--Z1.ZPOS_DESCRIPT DESCRIPT_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,ZPOS_POSTDATE_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
);


CREATE OR REPLACE FORCE VIEW V8_ED_FOLIO_REV_APS_G_TAX_DESC
AS 
select
0 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
--ZPOS_POSTDATE_S ZPOS_POSTDATE,
--to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S SPLITID,
--DESCRIPT_S DESCRIPT,
TAX_CODE_S TAX_CODE,
GROSS_REV_S GROSS_REV,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
--ZPOS_POSTDATE_S2 ZPOS_POSTDATE_S,
SPLITID_S2 SPLITID_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
--Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2, 
Z1.ZPOS_ZARR_ID SPLITID_S2, 
--Z1.ZPOS_DESCRIPT DESCRIPT_S2,
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)<>0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
)
union all
select
1 INDICATE_ZPOS_ID,
ZWIN_ID_S ZWIN_ID,
--ZPOS_POSTDATE_S ZPOS_POSTDATE,
--to_number(to_char(ZPOS_POSTDATE_S,'J')) ZPOS_POSTDATE_J,
SPLITID_S SPLITID,
--DESCRIPT_S DESCRIP,
TAX_CODE_S TAC_TAX,
GROSS_REV_S GROSS_REV,
(select ZTXC.ZTXC_LONGDESC from ztxc,ztcd where ZTXC.ZTXC_ID=ZTCD.ZTCD_ZTXC_ID and ZTCD.ZTCD_ID=TAX_CODE_S) TAX_CODE_DESC,
ORI_RES_FA_ID_S ORI_RES_FA_ID
from
(
select
ZWIN_ID_S2 ZWIN_ID_S,
--ZPOS_POSTDATE_S2 ZPOS_POSTDATE_S,
SPLITID_S2 SPLITID_S,
--DESCRIPT_S2 DESCRIPT_S,
TAX_CODE_S2 TAX_CODE_S,
SUM(GROSS_REV_S2) GROSS_REV_S,
ORI_RES_FA_ID_S2 ORI_RES_FA_ID_S
from
(
select
Z1.ZPOS_ZWIN_ID ZWIN_ID_S2,
--Z1.ZPOS_POSTDATE ZPOS_POSTDATE_S2,
Z1.ZPOS_INTERNALSPLIT_ZPOS_ID SPLITID_S2, 
--Z1.ZPOS_DESCRIPT DESCRIPT_S2, 
Z1.ZPOS_GROSSUNITPRICE*Z1.ZPOS_QUANTITY GROSS_REV_S2,
(select Z3.ZPOS_ZTCD_ID from zpos Z3 where Z3.ZPOS_TAXLINK_ID=Z1.ZPOS_TAXLINK_ID and Z3.ZPOS_ZTCD_ID is not null) TAX_CODE_S2,
decode(nvl(Z1.ZPOS_ORIGINATED_ZFAC_ID,0),0,Z1.ZPOS_ORIGINATED_YRES_ID,Z1.ZPOS_ORIGINATED_ZFAC_ID) ORI_RES_FA_ID_S2,
Z1.*
from
zpos Z1
where 
Z1.ZPOS_CDT=1
and nvl(Z1.ZPOS_ZARR_ID,0)=0
--and  Z1.ZPOS_ZWIN_ID=:zwin_id
) group by ZWIN_ID_S2,SPLITID_S2,TAX_CODE_S2,ORI_RES_FA_ID_S2
);


create or replace view v8_contact_posdept
as
select xcid_id contact_xcms_id, 0 company_xcms_id, xcid_position position, xcid_department department
from xcid
union
select xcil_individual_xcms_id, xcil_company_xcms_id, xcil_position, xcil_department
from xcil;


CREATE OR REPLACE FORCE VIEW V8_ED_CCM_YBOM_MAIN
AS 
     SELECT ybom.YBOM_ID YBOM_ID,
          ybom.YBOM_XCMS_ID YBOM_XCMS_ID,
          (SELECT v8_rep_name.NAME
             FROM v8_rep_name
            WHERE v8_rep_name.XCMS_ID = ybom.YBOM_XCMS_ID)
             YBOM_XCMS_NAME,
          ybom.YBOM_IND_XCMS_ID YBOM_IND_XCMS_ID,
          (SELECT v8_rep_name.NAME
             FROM v8_rep_name
            WHERE v8_rep_name.XCMS_ID = ybom.YBOM_IND_XCMS_ID)
             YBOM_IND_XCMS_NAME,
          ybom.YBOM_ARRIVAL YBOM_ARRIVAL,
          TRUNC (SYSDATE) PC_DATE,
          ybom.YBOM_DEPARTURE YBOM_DEPARTURE,
          ybom.YBOM_NAME YBOM_NAME,
          (SELECT yfty.YFTY_ID
             FROM yfty
            WHERE yfty.YFTY_ID = ybom.YBOM_YFTY_ID)
             YFTY_ID,
          (SELECT yfty.YFTY_LONGDESC
             FROM yfty
            WHERE yfty.YFTY_ID = ybom.YBOM_YFTY_ID)
             YFTY_LONGDESC,
          ybom.YBOM_CATERINGONLY YBOM_CATERINGONLY,
          ybom.YBOM_NOOFATTENDEES YBOM_NOOFATTENDEES,
          ybom.YBOM_INFOBOARD YBOM_INFOBOARD,
          ybom.YBOM_CATFOLLOWUPDATE YBOM_CATFOLLOWUPDATE,
          ybom.YBOM_CATDECISIONDATE YBOM_CATDECISIONDATE,
          ybom.YBOM_CATCUTOFFDATE YBOM_CATCUTOFFDATE,
          ybom.YBOM_DETAILSOK YBOM_DETAILSOK,
          ybom.YBOM_CONTRACTNUMBER YBOM_CONTRACTNUMBER,
          ybom.YBOM_RMS_YEST_ID YBOM_RMS_YEST_ID,
          (SELECT yest.YEST_LONGDESC
             FROM yest
            WHERE yest.YEST_ID = ybom.YBOM_RMS_YEST_ID)
             YEST_RMS_LONGDESC,
          (SELECT yblh.YBLH_ROOMINGLISTDATE
             FROM yblh
            WHERE yblh.YBLH_YBOM_ID = ybom.YBOM_ID)
             YBOM_ROOMINGLIST_DATE,
          ybom.YBOM_CON_YEST_ID YBOM_CON_YEST_ID,
          (SELECT yest.YEST_LONGDESC
             FROM yest
            WHERE yest.YEST_ID = ybom.YBOM_CON_YEST_ID)
             YEST_CON_LONGDESC,
          ybom.YBOM_CONTACTONSITE YBOM_CONTACTONSITE,
          (SELECT ybml.YBML_XCMS_ID
             FROM ybml
            WHERE ybml.YBML_YBOM_ID = ybom.YBOM_ID AND ybml.YBML_TYPE = 2)
             YBML_CONF_XCMS_ID,
          (SELECT v8_rep_name.NAME
             FROM v8_rep_name
            WHERE v8_rep_name.XCMS_ID =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             YBML_CONF_NAME,
          (SELECT UPPER (xced.XCED_INITIALS)
             FROM xced
            WHERE xced.XCED_ID =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             YBML_CONF_XCED_INITIALS,
          (SELECT UPPER (xced.XCED_LOGINNAME)
             FROM xced
            WHERE xced.XCED_ID =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             YMBL_CONF_XCED_LOGIN,
          (SELECT xced.XCED_TITLE
             FROM xced
            WHERE xced.XCED_ID =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             YMBL_XCED_TITLE,
          (SELECT Phone_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Phone_LongDesc,
          (SELECT xcom.XCOM_VALUE
             FROM xcom
            WHERE     xcom.XCOM_XECT_ID =
                         (SELECT xect.XECT_ID
                            FROM xect
                           WHERE xect.XECT_TYPE = 4 AND ROWNUM = 1)
                  AND ROWNUM = 1
                  AND xcom.XCOM_XCMS_ID =
                         (SELECT ybml.YBML_XCMS_ID
                            FROM ybml
                           WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                                 AND ybml.YBML_TYPE = 2))
             YBML_CONF_TELEFON,
          (SELECT Phone_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Phone_TypeID,
          (SELECT Fax_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Fax_LongDesc,
          (SELECT xcom.XCOM_VALUE
             FROM xcom
            WHERE     xcom.XCOM_XECT_ID =
                         (SELECT xect.XECT_ID
                            FROM xect
                           WHERE xect.XECT_TYPE = 3 AND ROWNUM = 1)
                  AND ROWNUM = 1
                  AND xcom.XCOM_XCMS_ID =
                         (SELECT ybml.YBML_XCMS_ID
                            FROM ybml
                           WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                                 AND ybml.YBML_TYPE = 2))
             YBML_CONF_TELEFAX,
          (SELECT Fax_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Fax_TypeID,
          (SELECT Email_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Email_LongDesc,
          (SELECT xcom.XCOM_VALUE
             FROM xcom
            WHERE     xcom.XCOM_XECT_ID =
                         (SELECT xect.XECT_ID
                            FROM xect
                           WHERE xect.XECT_TYPE = 1 AND ROWNUM = 1)
                  AND ROWNUM = 1
                  AND xcom.XCOM_XCMS_ID =
                         (SELECT ybml.YBML_XCMS_ID
                            FROM ybml
                           WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                                 AND ybml.YBML_TYPE = 2))
             YBML_CONF_EMAIL,
          (SELECT Email_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Email_TypeID,
          (SELECT Homepage_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Homepage_LongDesc,
          (SELECT xcom.XCOM_VALUE
             FROM xcom
            WHERE     xcom.XCOM_XECT_ID =
                         (SELECT xect.XECT_ID
                            FROM xect
                           WHERE xect.XECT_TYPE = 2 AND ROWNUM = 1)
                  AND ROWNUM = 1
                  AND xcom.XCOM_XCMS_ID =
                         (SELECT ybml.YBML_XCMS_ID
                            FROM ybml
                           WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                                 AND ybml.YBML_TYPE = 2))
             YBML_CONF_HOMEPAGE,
          (SELECT Homepage_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 2))
             ybml_conf_Homepage_TypeID,
          (SELECT Email_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Email_TypeID,
          (SELECT Email_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Email_LongDesc,
          (SELECT Homepage_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Homepage_TypeID,
          (SELECT Homepage_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Homepage_LongDesc,
          (SELECT Fax_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Fax_TypeID,
          (SELECT Fax_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Fax_LongDesc,
          (SELECT Phone_TypeID
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Phone_TypeID,
          (SELECT Phone_LongDesc
             FROM V8_EDITOR_USER_DET
            WHERE xced_id =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             ybml_rms_Phone_LongDesc,
          (SELECT ybml.YBML_XCMS_ID
             FROM ybml
            WHERE ybml.YBML_YBOM_ID = ybom.YBOM_ID AND ybml.YBML_TYPE = 3)
             YBML_RMS_XCMS_ID,
          (SELECT v8_rep_name.NAME
             FROM v8_rep_name
            WHERE v8_rep_name.XCMS_ID =
                     (SELECT ybml.YBML_XCMS_ID
                        FROM ybml
                       WHERE     ybml.YBML_YBOM_ID = ybom.YBOM_ID
                             AND ybml.YBML_TYPE = 3))
             YBML_RMS_NAME,
          V8_FNC_XCNO_REP_INFOS (ybom.YBOM_ID,
                                 'YBOM',
                                 1,
                                 0)
             NOTES_SHORT_NON_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (ybom.YBOM_ID,
                                 'YBOM',
                                 1,
                                 1)
             NOTES_SHORT_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (ybom.YBOM_ID,
                                 'YBOM',
                                 2,
                                 0)
             NOTES_LONG_NON_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (ybom.YBOM_ID,
                                 'YBOM',
                                 2,
                                 1)
             NOTES_LONG_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (ybom.YBOM_ID,
                                 'YBOM',
                                 3,
                                 0)
             NOTES_ND_NON_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (ybom.YBOM_ID,
                                 'YBOM',
                                 3,
                                 1)
             NOTES_ND_VIEWABLE,
          v1.FS_REV,
          v1.FS_NET_REV,
          v1.FS_VAT,
          V8_FNC_VALCURR_FORMATING (v1.FS_REV, ybom2.zcur_shortdesc) FFS_REV,
          NVL (
             (SELECT SUM (
                        DECODE (
                           NVL (yebs.yebs_perhour, 0),
                           0, yebs.YEBS_ORG_SPECIALRATE,
                           1,   (yebs.yebs_endtime - yebs.yebs_starttime)
                              * 24
                              * yebs.YEBS_ORG_SPECIALRATE))
                FROM yebs, yfsr, yevm
               WHERE     yebs.yebs_yfsr_id = yfsr.yfsr_id(+)
                     AND yebs.YEBS_YEVM_ID = yevm.YEVM_ID
                     AND yebs.YEBS_PKGINCL = 0
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
             0)
             FS_ORGREV,
          (SELECT SUM (yebs.yebs_costprice)
             FROM yebs, yfsr, yevm
            WHERE     yebs.yebs_yfsr_id = yfsr.yfsr_id(+)
                  AND yebs.YEBS_YEVM_ID = yevm.YEVM_ID
                  AND yebs.YEBS_PKGINCL = 0
                  AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID)
             FS_COSTPRICE,
          v1.ITEMS_REV,
          v1.ITEMS_NET_REV,
          v1.ITEMS_VAT,
          V8_FNC_VALCURR_FORMATING (v1.ITEMS_REV, ybom2.zcur_shortdesc)
             FITEMS_REV,
          (SELECT SUM (yebr.yebr_costprice * yebr.YEBR_QUANTITY)
             FROM yebr, ycir, yevm
            WHERE     yebr.yebr_ycir_id = ycir.ycir_id(+)
                  AND yebr.YEBR_YEVM_ID = yevm.YEVM_ID
                  AND yebr.YEBR_PKGINCL = 0
                  AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID)
             ITEMS_COSTPRICE,
          v1.BOOKED_MENU_REV,
          v1.BOOKED_MENU_NET_REV,
          v1.BOOKED_MENU_VAT,
          V8_FNC_VALCURR_FORMATING (V1.BOOKED_MENU_NET_REV,
                                    ybom2.zcur_shortdesc)
             FBOOKED_MENU_REV,
          (SELECT SUM (
                       yemn.YEMN_COSTPRICE
                     * DECODE (
                          NVL (yemn.YEMN_POSTGUARANTEED, 0),
                          0, yemn.YEMN_QUANTITY,
                          DECODE (
                             SIGN (
                                  yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                - yemn.YEMN_QUANTITY),
                             1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                             yemn.YEMN_QUANTITY)))
             FROM yemn, yevm
            WHERE     yemn.YEMN_YEVM_ID = yevm.YEVM_ID
                  AND yemn.YEMN_PKGINCL = 0
                  AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                  AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID)
             BOOKED_MENU_COSTPRICE,
          V1.MI_REV SUB_MENU_REV,
          V1.MI_NET_REV SUB_MENU_NET_REV,
          V1.MI_VAT SUB_MENU_VAT,
          V8_FNC_VALCURR_FORMATING (V1.MI_REV, ybom2.zcur_shortdesc)
             FSUB_MENU_REV,
          v1.FB_REV,
          v1.FB_NET_REV,
          v1.FB_VAT,
          V8_FNC_VALCURR_FORMATING (v1.FB_REV, ybom2.zcur_shortdesc) FFB_REV,
          NVL (V1.FS_REV + V1.ITEMS_REV + V1.BOOKED_MENU_REV + V1.MI_REV, 0)
             TOT_REV_NO_PKG,
          NVL (
               V1.FS_NET_REV
             + V1.ITEMS_NET_REV
             + V1.BOOKED_MENU_NET_REV
             + V1.MI_NET_REV,
             0)
             TOT_NET_REV_NO_PKG,
          NVL (V1.FS_VAT + V1.ITEMS_VAT + V1.BOOKED_MENU_VAT + V1.MI_VAT, 0)
             TOT_VAT_NO_PKG,
          V8_FNC_VALCURR_FORMATING (
             NVL (V1.FS_REV + V1.ITEMS_REV + V1.BOOKED_MENU_REV + V1.MI_REV,
                  0),
             ybom2.zcur_shortdesc)
             FTOT_REV_NO_PKG,
          (  NVL (
                (SELECT SUM (
                           DECODE (
                              NVL (yebs.yebs_perhour, 0),
                              0, yebs.YEBS_ORG_SPECIALRATE,
                              1,   (yebs.yebs_endtime - yebs.yebs_starttime)
                                 * 24
                                 * yebs.YEBS_ORG_SPECIALRATE))
                   FROM yebs, yfsr, yevm
                  WHERE     yebs.yebs_yfsr_id = yfsr.yfsr_id(+)
                        AND yebs.YEBS_YEVM_ID = yevm.YEVM_ID
                        AND yebs.YEBS_PKGINCL = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0)
           + NVL (
                (SELECT SUM (
                             DECODE (
                                NVL (yebr.yebr_perhour, 0),
                                0, yebr.YEBR_ORG_MANUALPRICE,
                                1,   (yebr.yebr_endtime - yebr.yebr_starttime)
                                   * 24
                                   * yebr.YEBR_ORG_MANUALPRICE)
                           * yebr.YEBR_QUANTITY)
                   FROM yebr, ycir, yevm
                  WHERE     yebr.yebr_ycir_id = ycir.ycir_id(+)
                        AND yebr.YEBR_YEVM_ID = yevm.YEVM_ID
                        AND yebr.YEBR_PKGINCL = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0)
           + NVL (
                (SELECT SUM (
                             yemn.YEMN_MENUPRICE
                           * DECODE (
                                NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                0, yemn.YEMN_QUANTITY,
                                DECODE (
                                   SIGN (
                                        yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                      - yemn.YEMN_QUANTITY),
                                   1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                   yemn.YEMN_QUANTITY)))
                   FROM yemn, yevm
                  WHERE     yemn.YEMN_YEVM_ID = yevm.YEVM_ID
                        AND yemn.YEMN_PKGINCL = 0
                        AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0)
           + NVL (
                (SELECT SUM (yemi.YEMI_ORG_PRICE * yemi.YEMI_QUANTITY)
                   FROM yemi, yemn, yevm
                  WHERE     yemi.YEMI_YEVM_ID = yevm.YEVM_ID
                        AND yemi.YEMI_YEMN_ID = yemn.YEMN_ID
                        AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                        AND yemi.YEMI_INCLUDEDINTHEMENU = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0))
             TOT_ORGREV_NO_PKG,
          v1.PACKAGE_REV,
          v1.PACKAGE_NET_REV,
          v1.PACKAGE_VAT,
          V8_FNC_VALCURR_FORMATING (v1.PACKAGE_REV, ybom2.zcur_shortdesc)
             FPACKAGE_REV,
          v1.TOT_REV,
          v1.TOT_NET_REV,
          v1.TOT_VAT,
          V8_FNC_VALCURR_FORMATING (v1.TOT_REV, ybom2.zcur_shortdesc)
             FTOT_REV,
          V8_FNC_VALCURR_FORMATING (v1.TOT_NET_REV, ybom2.zcur_shortdesc)
             FTOT_NET_REV,
          V8_FNC_VALCURR_FORMATING (v1.TOT_VAT, ybom2.zcur_shortdesc)
             FTOT_VAT,
          (  NVL (
                (SELECT SUM (
                           DECODE (
                              NVL (yebs.yebs_perhour, 0),
                              0, yebs.YEBS_ORG_SPECIALRATE,
                              1,   (yebs.yebs_endtime - yebs.yebs_starttime)
                                 * 24
                                 * yebs.YEBS_ORG_SPECIALRATE))
                   FROM yebs, yfsr, yevm
                  WHERE     yebs.yebs_yfsr_id = yfsr.yfsr_id(+)
                        AND yebs.YEBS_YEVM_ID = yevm.YEVM_ID
                        AND yebs.YEBS_PKGINCL = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0)
           + NVL (
                (SELECT SUM (
                             DECODE (
                                NVL (yebr.yebr_perhour, 0),
                                0, yebr.YEBR_ORG_MANUALPRICE,
                                1,   (yebr.yebr_endtime - yebr.yebr_starttime)
                                   * 24
                                   * yebr.YEBR_ORG_MANUALPRICE)
                           * yebr.YEBR_QUANTITY)
                   FROM yebr, ycir, yevm
                  WHERE     yebr.yebr_ycir_id = ycir.ycir_id(+)
                        AND yebr.YEBR_YEVM_ID = yevm.YEVM_ID
                        AND yebr.YEBR_PKGINCL = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0)
           + NVL (
                (SELECT SUM (
                             yemn.YEMN_MENUPRICE
                           * DECODE (
                                NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                0, yemn.YEMN_QUANTITY,
                                DECODE (
                                   SIGN (
                                        yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                      - yemn.YEMN_QUANTITY),
                                   1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                   yemn.YEMN_QUANTITY)))
                   FROM yemn, yevm
                  WHERE     yemn.YEMN_YEVM_ID = yevm.YEVM_ID
                        AND yemn.YEMN_PKGINCL = 0
                        AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                0)
           + NVL (
                (SELECT SUM (yemi.YEMI_ORG_PRICE * yemi.YEMI_QUANTITY)
                   FROM yemi, yemn, yevm
                  WHERE     yemi.YEMI_YEVM_ID = yevm.YEVM_ID
                        AND yemi.YEMI_YEMN_ID = yemn.YEMN_ID
                        AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                        AND yemi.YEMI_INCLUDEDINTHEMENU = 0
                        AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                        AND yemn.YEMN_PKGINCL = 0),
                0)
           + v1.PACKAGE_REV)
             TOT_ORGREV,
          V8_FNC_VALCURR_FORMATING (
             (  NVL (
                   (SELECT SUM (
                              DECODE (
                                 NVL (yebs.yebs_perhour, 0),
                                 0, yebs.YEBS_ORG_SPECIALRATE,
                                 1,   (  yebs.yebs_endtime
                                       - yebs.yebs_starttime)
                                    * 24
                                    * yebs.YEBS_ORG_SPECIALRATE))
                      FROM yebs, yfsr, yevm
                     WHERE     yebs.yebs_yfsr_id = yfsr.yfsr_id(+)
                           AND yebs.YEBS_YEVM_ID = yevm.YEVM_ID
                           AND yebs.YEBS_PKGINCL = 0
                           AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                   0)
              + NVL (
                   (SELECT SUM (
                                DECODE (
                                   NVL (yebr.yebr_perhour, 0),
                                   0, yebr.YEBR_ORG_MANUALPRICE,
                                   1,   (  yebr.yebr_endtime
                                         - yebr.yebr_starttime)
                                      * 24
                                      * yebr.YEBR_ORG_MANUALPRICE)
                              * yebr.YEBR_QUANTITY)
                      FROM yebr, ycir, yevm
                     WHERE     yebr.yebr_ycir_id = ycir.ycir_id(+)
                           AND yebr.YEBR_YEVM_ID = yevm.YEVM_ID
                           AND yebr.YEBR_PKGINCL = 0
                           AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                   0)
              + NVL (
                   (SELECT SUM (
                                yemn.YEMN_MENUPRICE
                              * DECODE (
                                   NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                   0, yemn.YEMN_QUANTITY,
                                   DECODE (
                                      SIGN (
                                           yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                         - yemn.YEMN_QUANTITY),
                                      1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                      yemn.YEMN_QUANTITY)))
                      FROM yemn, yevm
                     WHERE     yemn.YEMN_YEVM_ID = yevm.YEVM_ID
                           AND yemn.YEMN_PKGINCL = 0
                           AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                           AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID),
                   0)
              + NVL (
                   (SELECT SUM (yemi.YEMI_ORG_PRICE * yemi.YEMI_QUANTITY)
                      FROM yemi, yemn, yevm
                     WHERE     yemi.YEMI_YEVM_ID = yevm.YEVM_ID
                           AND yemi.YEMI_YEMN_ID = yemn.YEMN_ID
                           AND yemn.YEMN_CHARGEDONCONSUMPTION = 0
                           AND yemi.YEMI_INCLUDEDINTHEMENU = 0
                           AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                           AND yemn.YEMN_PKGINCL = 0),
                   0)
              + v1.PACKAGE_REV),
             ybom2.zcur_shortdesc)
             FTOT_ORGREV,
          (SELECT DECODE (NVL (zfac_autonumber, 0),
                          0, zfan_longdesc,
                          zfac_autonumber)
             FROM zfac, zfan
            WHERE zfac_zfan_id = zfan_id(+) AND zfac_id = ybom_zfac_id)
             FinancialAccount,
          (SELECT department
             FROM v8_contact_posdept v
            WHERE     v.contact_xcms_id = ybom_ind_xcms_id
                  AND v.company_xcms_id = ybom_xcms_id
                  AND ROWNUM = 1)
             xcid_department,
          (SELECT position
             FROM v8_contact_posdept v
            WHERE     v.contact_xcms_id = ybom_ind_xcms_id
                  AND v.company_xcms_id = ybom_xcms_id
                  AND ROWNUM = 1)
             xcid_position,
          NVL ( (SELECT MAX (yevm_minimumguarantee)
                   FROM yevm
                  WHERE yevm_ybom_id = ybom.ybom_id),
               0)
             max_yevm_minimumguarantee,
          ybom2.zcur_shortdesc zcur_shortdesc
     FROM ybom,
          v8_stat_ccm_res_forrev_totals v1,
          (SELECT ybom_id,
                  (SELECT zcur_shortdesc
                     FROM zcur
                    WHERE zcur_id = ybom_base_zcur_id)
                     zcur_shortdesc
             FROM ybom) ybom2
    WHERE ybom.ybom_id = v1.ybom_id AND ybom.ybom_id = ybom2.ybom_id;


CREATE OR REPLACE FORCE VIEW V8_ED_CCM_YEVM_YEBR
AS 
   SELECT yevm_ybom_id ybom_id,
          YEBR.YEBR_ID YEBR_ID,
          TO_NUMBER (TO_CHAR (TRUNC (YEBR_STARTTIME), 'J')) DATE_NUMBER,
          yebr.YEBR_YEVM_ID YEVM_ID,
          yebr.YEBR_NAME YEBR_NAME,
          yebr.YEBR_QUANTITY YEBR_QUANTITY,
          yebr.YEBR_PKGINCL YEBR_PKGINCL,
          yebr.yebr_description,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (
                      NVL (yebr.YEBR_MANUALPRICE, 0),
                      0, TO_CHAR (NULL),
                      V8_FNC_VALCURR_FORMATING (yebr.YEBR_MANUALPRICE,
                                                ybom.zcur_shortdesc))),
                14),
             15,
             ' ')
             RATE,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (NVL (yebr.YEBR_MANUALPRICE, 0),
                           0, TO_CHAR (NULL),
                           yebr.YEBR_PRICE_NET)),
                14),
             15,
             ' ')
             RATE_NET,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (NVL (yebr.YEBR_MANUALPRICE, 0),
                           0, TO_CHAR (NULL),
                           yebr.YEBR_PRICE_VAT)),
                14),
             15,
             ' ')
             RATE_VAT,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (
                      NVL (yebr.YEBR_MANUALPRICE, 0),
                      0, TO_CHAR (NULL),
                      V8_FNC_VALCURR_FORMATING (
                           yebr.YEBR_QUANTITY
                         * DECODE (
                              NVL (yebr.yebr_perhour, 0),
                              0, yebr.yebr_manualprice,
                              1,   (yebr_endtime - yebr_starttime)
                                 * 24
                                 * yebr.yebr_manualprice),
                         ybom.zcur_shortdesc))),
                14),
             15,
             ' ')
             TOT_RATE,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (
                      NVL (yebr.YEBR_MANUALPRICE, 0),
                      0, TO_CHAR (NULL),
                        yebr.YEBR_QUANTITY
                      * DECODE (
                           NVL (yebr.yebr_perhour, 0),
                           0, yebr.YEBR_PRICE_NET,
                           1,   (yebr_endtime - yebr_starttime)
                              * 24
                              * yebr.YEBR_PRICE_NET))),
                14),
             15,
             ' ')
             TOT_RATE_NET,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (
                      NVL (yebr.YEBR_MANUALPRICE, 0),
                      0, TO_CHAR (NULL),
                        yebr.YEBR_QUANTITY
                      * DECODE (
                           NVL (yebr.yebr_perhour, 0),
                           0, yebr.YEBR_PRICE_VAT,
                           1,   (yebr_endtime - yebr_starttime)
                              * 24
                              * yebr.YEBR_PRICE_VAT))),
                14),
             15,
             ' ')
             TOT_RATE_VAT,
          (SELECT ycrd.YCRD_LONGDESC
             FROM ycrd, ycir
            WHERE     ycrd.YCRD_ID = ycir.YCIR_YCRD_ID
                  AND ycir.YCIR_ID = yebr.YEBR_YCIR_ID)
             YCRD_LONGDESC,
          V8_FNC_XCNO_REP_INFOS (yebr.YEBR_ID,
                                 'YEBR',
                                 1,
                                 0)
             NOTES_SHORT_NON_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (yebr.YEBR_ID,
                                 'YEBR',
                                 1,
                                 1)
             NOTES_SHORT_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (yebr.YEBR_ID,
                                 'YEBR',
                                 2,
                                 0)
             NOTES_LONG_NON_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (yebr.YEBR_ID,
                                 'YEBR',
                                 2,
                                 1)
             NOTES_LONG_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (yebr.YEBR_ID,
                                 'YEBR',
                                 3,
                                 0)
             NOTES_ND_NON_VIEWABLE,
          V8_FNC_XCNO_REP_INFOS (yebr.YEBR_ID,
                                 'YEBR',
                                 3,
                                 1)
             NOTES_ND_VIEWABLE,
          yebr.yebr_ycit_id,
          (SELECT ycit_longdesc
             FROM ycit
            WHERE ycit_id = yebr_ycit_id)
             ycit_longdesc,
          yebr.yebr_ycir_id,
          yebr.yebr_ycia_id,
          (SELECT ycia_longdesc
             FROM ycia
            WHERE ycia_id = yebr_ycia_id)
             ycia_longdesc,
          TO_CHAR (YEBR.YEBR_STARTTIME, 'HH24:MI') FYEBR_STARTTIME,
          TO_CHAR (YEBR.YEBR_ENDTIME, 'HH24:MI') FYEBR_ENDTIME,
          YEBR.YEBR_DISPLORDER,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (
                      NVL (yebr.YEBR_ORG_MANUALPRICE, 0),
                      0, TO_CHAR (NULL),
                      V8_FNC_VALCURR_FORMATING (
                         yebr.YEBR_ORG_MANUALPRICE,
                         (SELECT zcur.ZCUR_SHORTDESC
                            FROM yevm, ybom, zcur
                           WHERE     yevm.YEVM_ID = yebr.YEBR_YEVM_ID
                                 AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                                 AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                                 AND ROWNUM = 1)))),
                14),
             15,
             ' ')
             ORG_RATE,
          LPAD (
             RTRIM (
                DECODE (
                   yebr.YEBR_PKGINCL,
                   1, 'Inklusive',
                   DECODE (
                      NVL (yebr.YEBR_ORG_MANUALPRICE, 0),
                      0, TO_CHAR (NULL),
                      V8_FNC_VALCURR_FORMATING (
                           yebr.YEBR_QUANTITY
                         * DECODE (
                              NVL (yebr.yebr_perhour, 0),
                              0, yebr.yebr_org_manualprice,
                              1,   (yebr_endtime - yebr_starttime)
                                 * 24
                                 * yebr.yebr_org_manualprice),
                         (SELECT zcur.ZCUR_SHORTDESC
                            FROM yevm, ybom, zcur
                           WHERE     yevm.YEVM_ID = yebr.YEBR_YEVM_ID
                                 AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                                 AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                                 AND ROWNUM = 1)))),
                14),
             15,
             ' ')
             TOT_ORG_RATE,
          yebr.yebr_rebate_perc,
          yebr.yebr_rebate_flat,
          V8_FNC_VALCURR_FORMATING (
             yebr_rebate_flat,
             (SELECT zcur.ZCUR_SHORTDESC
                FROM yevm, ybom, zcur
               WHERE     yevm.YEVM_ID = yebr.YEBR_YEVM_ID
                     AND yevm.YEVM_YBOM_ID = ybom.YBOM_ID
                     AND zcur.ZCUR_ID = ybom.YBOM_BASE_ZCUR_ID
                     AND ROWNUM = 1))
             Fyebr_rebate_flat,
          yebr.yebr_costprice,
          yevm_displorder,
          TO_NUMBER (TO_CHAR (TRUNC (yevm_starttime), 'J')) DATE_NUMBER_EVENT,
          yevm_name,
          yevm_yety_id,
          (SELECT yety_longdesc
             FROM yety
            WHERE yevm_yety_id = yety_id)
             yety_longdesc,
          V8_FNC_YEBS_yfsc_rep_INFOS (yebr_yevm_id, 3, 0) FUNCTION_SPACE,
          V8_FNC_YEBS_yfsc_rep_INFOS (yebr_yevm_id, 3, 1)
             FUNCTION_SPACE_ALTINFO,
          TO_CHAR (yevm_starttime, 'HH24:MI') fyevm_starttime,
          TO_CHAR (yevm_endtime, 'HH24:MI') fyevm_endtime,
          yebr_alternative AlternativeFlag,
          v8_fnc_ccm_alternatives ('YEBR',
                                   yebr_id,
                                   0,
                                   1)
             Alternatives,
          yevm_alternative EventAlternativeFlag,
          v8_fnc_ccm_alternatives ('YEVM',
                                   yevm_id,
                                   3,
                                   1)
             EventAlternatives,
          yebr.yebr_noofpax,
          yebr.yebr_perhour,
          yebr.yebr_manualprice,
          YEBR.YEBR_PRICE_NET YEBR_MANUALPRICE_NET,
          YEBR.YEBR_PRICE_VAT YEBR_MANUALPRICE_VAT,
          (yebr_endtime - yebr_starttime) * 24 hours,
          ybom.zcur_shortdesc
     FROM yebr,
          yevm,
          (SELECT ybom_id,
                  (SELECT zcur_shortdesc
                     FROM zcur
                    WHERE zcur_id = ybom_base_zcur_id)
                     zcur_shortdesc
             FROM ybom) ybom
    WHERE     YEVM.YEVM_YBOM_ID = ybom_id
          AND yebr_yevm_id = yevm_id
          AND NVL (yevm.yevm_mergedto_yevm_id, 0) = 0;
	
			
CREATE OR REPLACE VIEW V8_ED_CCM_PACKAGES
AS 
   SELECT ypkd.YPKD_ID YPKD_ID,
          ypkd.YPKD_YBOM_ID YPKD_YBOM_ID,
          ypkd.YPKD_YPKG_ID YPKD_YPKG_ID,
          DECODE (ypkd.YPKD_STARTTIME, YEVM_STARTTIME_S, 1, 2) DAY_FOLLOW,
          ypkd.YPKD_NAME YPKD_NAME,
          ypkd.YPKD_PRICE YPKD_PRICE,
          V8_FNC_VALCURR_FORMATING (ypkd.YPKD_PRICE, ybom.zcur_shortdesc)
             FYPKD_PRICE,
          NVL ( (SELECT SUM (YBEF.YBEF_REV_AMOUNT)
                   FROM ybef
                  WHERE YBEF.YBEF_YPKD_ID = YPKD.YPKD_ID),
               0)
             YPKD_PRICE_GROSS,
          NVL ( (SELECT SUM (YBEF.YBEF_PRICE_NET)
                   FROM ybef
                  WHERE YBEF.YBEF_YPKD_ID = YPKD.YPKD_ID),
               0)
             YPKD_PRICE_NET,
          NVL ( (SELECT SUM (YBEF.YBEF_PRICE_VAT)
                   FROM ybef
                  WHERE YBEF.YBEF_YPKD_ID = YPKD.YPKD_ID),
               0)
             YPKD_PRICE_VAT,
          ypkd.YPKD_NOOFATTENDEES YPKD_NOOFATTENDEES,
          DECODE (NVL (ypkd.YPKD_NOOFATTENDEES, 0),
                  0, TO_CHAR (NULL),
                  TO_CHAR (ypkd.YPKD_NOOFATTENDEES) || ' x')
             FYPKD_NOOFATTENDEES,
          ypkd.YPKD_NOOFATTENDEES * ypkd.YPKD_PRICE TOT_PRICE,
          V8_FNC_VALCURR_FORMATING (
             ypkd.YPKD_NOOFATTENDEES * ypkd.YPKD_PRICE,
             ybom.zcur_shortdesc)
             FTOT_PRICE,
          NVL ( (SELECT SUM (YBEF.YBEF_REV_AMOUNT * ypkd.ypkd_noofattendees)
                   FROM ybef
                  WHERE YBEF.YBEF_YPKD_ID = YPKD.YPKD_ID),
               0)
             TOT_PRICE_GROSS,
          NVL ( (SELECT SUM (YBEF.YBEF_PRICE_NET * ypkd.ypkd_noofattendees)
                   FROM ybef
                  WHERE YBEF.YBEF_YPKD_ID = YPKD.YPKD_ID),
               0)
             TOT_PRICE_NET,
          NVL ( (SELECT SUM (YBEF.YBEF_PRICE_VAT * ypkd.ypkd_noofattendees)
                   FROM ybef
                  WHERE YBEF.YBEF_YPKD_ID = YPKD.YPKD_ID),
               0)
             TOT_PRICE_VAT,
          ypkd.YPKD_STARTTIME YPKD_STARTTIME,
          TRUNC (ypkd.YPKD_STARTTIME) TRUNC_YPKD_STARTTIME,
          TO_NUMBER (TO_CHAR (TRUNC (YEVM_STARTTIME_S), 'J')) DATE_NUMBER,
          ypkd.YPKD_ENDTIME YPKD_ENDTIME,
          YBOM.YBOM_BASE_ZCUR_ID ZCUR_ID,
          YBOM.ZCUR_SHORTDESC,
          (SELECT ypkg_name
             FROM ypkg
            WHERE ypkg_id = ypkd_ypkg_id)
             YPKG_NAME,
          1 CHECKVALUE,
          YPKD_DISPLORDER,
          ypkd_alternative AlternativeFlag,
          v8_fnc_ccm_alternatives ('YPKD',
                                   ypkd_id,
                                   0,
                                   1)
             Alternatives,
          v8_fnc_ccm_alternatives ('YPKD',
                                   ypkd_id,
                                   1,
                                   1)
             AlternativesFrom
     FROM ypkd,
          (SELECT DISTINCT
                  yevm.YEVM_YPKD_ID YEVM_YPKD_ID_S,
                  TRUNC (yevm.YEVM_STARTTIME) YEVM_STARTTIME_S
             FROM ypkd, yevm
            WHERE ypkd.YPKD_ID = yevm.YEVM_YPKD_ID),
          (SELECT ybom_id,
                  YBOM_BASE_ZCUR_ID,
                  (SELECT zcur_shortdesc
                     FROM zcur
                    WHERE zcur_id = ybom_base_zcur_id)
                     zcur_shortdesc
             FROM ybom) ybom
    WHERE     ypkd.YPKD_ID = YEVM_YPKD_ID_S
          AND YPKD.YPKD_YBOM_ID = YBOM.YBOM_ID
          AND YEVM_STARTTIME_S BETWEEN TRUNC (ypkd.YPKD_STARTTIME)
                                   AND TRUNC (ypkd.YPKD_ENDTIME);


CREATE OR REPLACE FORCE VIEW V8_ED_CCM_YBOM_YEVM
AS
     SELECT YEVM.YEVM_ID YEVM_ID,
            YEVM.YEVM_YBOM_ID YEVM_YBOM_ID,
            TO_NUMBER (TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J')) DATE_NUMBER,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_fs v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               FS_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.net)
                  FROM v8_stat_ccm_res_forrev_fs v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               FS_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.vat)
                  FROM v8_stat_ccm_res_forrev_fs v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               FS_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_fs v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                  0),
               ybom.zcur_shortdesc)
               FFS_RATE_PER_DAY,
            (SELECT SUM (YEBS.YEBS_COSTPRICE)
               FROM YEBS, YEVM yevm_2, YFSR
              WHERE     YEBS.yebs_yfsr_id = YFSR.yfsr_id(+)
                    AND YEBS.YEBS_YEVM_ID = yevm_2.YEVM_ID
                    AND YEBS.YEBS_PKGINCL = 0
                    AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                    AND TO_NUMBER (
                           TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                           TO_NUMBER (
                              TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J')))
               FS_COSTPRICE,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_ci v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               ITEMS_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_ci v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               ITEMS_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_ci v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               ITEMS_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_ci v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                  0),
               ybom.zcur_shortdesc)
               FITEMS_RATE_PER_DAY,
            (SELECT SUM (YEBR.YEBR_COSTPRICE * YEBR.YEBR_QUANTITY)
               FROM YEBR, YEVM yevm_2, YCIR
              WHERE     YEBR.yebr_ycir_id = YCIR.ycir_id(+)
                    AND YEBR.YEBR_YEVM_ID = yevm_2.YEVM_ID
                    AND YEBR.YEBR_PKGINCL = 0
                    AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                    AND TO_NUMBER (
                           TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                           TO_NUMBER (
                              TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J')))
               ITEMS_COSTPRICE,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_sm v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               BOOKED_MENU_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_sm v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               BOOKED_MENU_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_SM v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               BOOKED_MENU_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_sm v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                  0),
               ybom.zcur_shortdesc)
               FBOOKED_MENU_RATE_PER_DAY,
            (SELECT SUM (
                         YEMN.YEMN_COSTPRICE
                       * DECODE (
                            NVL (yemn.YEMN_POSTGUARANTEED, 0),
                            0, yemn.YEMN_QUANTITY,
                            DECODE (
                               SIGN (
                                    yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                  - yemn.YEMN_QUANTITY),
                               1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                               yemn.YEMN_QUANTITY)))
               FROM YEMN, YEVM yevm_2
              WHERE     YEMN.YEMN_YEVM_ID = yevm_2.YEVM_ID
                    AND YEMN.YEMN_PKGINCL = 0
                    AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                    AND TO_NUMBER (
                           TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                           TO_NUMBER (
                              TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J')))
               BOOKED_MENU_COSTPRICE,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_mi v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               SUB_MENU_RATE_PER_DAY,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_mi v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               SUB_MENU_NET_PER_DAY,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_mi v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               SUB_MENU_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_mi v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                  0),
               ybom.zcur_shortdesc)
               FSUB_MENU_RATE_PER_DAY,
              NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
               TOT_REV_PER_DAY_NO_PKG,
              NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
               TOT_NET_REV_PER_DAY_NO_PKG,
              NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
               TOT_VAT_PER_DAY_NO_PKG,
            V8_FNC_VALCURR_FORMATING (
                 NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_fs v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_ci v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_sm v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_mi v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0),
               ybom.zcur_shortdesc)
               FTOT_REV_PER_DAY_NO_PKG,
            NVL (
               (SELECT SUM (v.grossrev)
                  FROM v8_stat_ccm_res_forrev_pk v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               PACKAGE_RATE,
            NVL (
               (SELECT SUM (v.NET)
                  FROM v8_stat_ccm_res_forrev_pk v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               PACKAGE_NET,
            NVL (
               (SELECT SUM (v.VAT)
                  FROM v8_stat_ccm_res_forrev_pk v
                 WHERE     v.ybom_id = yevm.yevm_ybom_id
                       AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
               0)
               PACKAGE_VAT,
            V8_FNC_VALCURR_FORMATING (
               NVL (
                  (SELECT SUM (v.grossrev)
                     FROM v8_stat_ccm_res_forrev_pk v
                    WHERE     v.ybom_id = yevm.yevm_ybom_id
                          AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                  0),
               ybom.zcur_shortdesc)
               FPACKAGE_RATE,
              NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.grossrev)
                    FROM v8_stat_ccm_res_forrev_pk v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
               TOT_REV_PER_DAY,
              NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.net)
                    FROM v8_stat_ccm_res_forrev_pk v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
               TOT_NET_REV_PER_DAY,
              NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_fs v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_ci v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_sm v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_mi v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
            + NVL (
                 (SELECT SUM (v.vat)
                    FROM v8_stat_ccm_res_forrev_pk v
                   WHERE     v.ybom_id = yevm.yevm_ybom_id
                         AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                 0)
               TOT_VAT_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
                 NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_fs v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_ci v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_sm v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_mi v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0)
               + NVL (
                    (SELECT SUM (v.grossrev)
                       FROM v8_stat_ccm_res_forrev_pk v
                      WHERE     v.ybom_id = yevm.yevm_ybom_id
                            AND v.wdat_date = TRUNC (yevm.yevm_starttime)),
                    0),
               ybom.zcur_shortdesc)
               FTOT_REV_PER_DAY,
            YEVM.YEVM_NAME YEVM_NAME,
            YEVM.YEVM_NOOFATTENDEES YEVM_NOOFATTENDEES,
            YEVM.YEVM_DOORCARD YEVM_DOORCARD,
            YEVM.YEVM_YPKD_ID YEVM_YPKD_ID,
            YEVM.YEVM_PKGINCL YEVM_PKGINCL,
            TRUNC (YEVM.YEVM_STARTTIME) YEVM_STARTTIME,
            TO_CHAR (YEVM.YEVM_STARTTIME, 'HH24:MI') FYEVM_STARTTIME,
            TRUNC (YEVM.YEVM_ENDTIME) YEVM_ENDTIME,
            TO_CHAR (YEVM.YEVM_ENDTIME, 'HH24:MI') FYEVM_ENDTIME,
            YEVM.YEVM_YETY_ID YEVM_YETY_ID,
            YEVM.YEVM_DISPLORDER,
            (SELECT YETY.YETY_SHORTDESC
               FROM YETY
              WHERE YETY.YETY_ID = YEVM.YEVM_YETY_ID)
               YETY_SHORTDESC,
            (SELECT YETY.YETY_LONGDESC
               FROM YETY
              WHERE YETY.YETY_ID = YEVM.YEVM_YETY_ID)
               YETY_LONGDESC,
            YEVM.YEVM_YEST_ID YEVM_YEST_ID,
            (SELECT YEST.YEST_SHORTDESC
               FROM YEST
              WHERE YEST.YEST_ID = YEVM.YEVM_YEST_ID)
               YEST_SHORTDESC,
            (SELECT YEST.YEST_LONGDESC
               FROM YEST
              WHERE YEST.YEST_ID = YEVM.YEVM_YEST_ID)
               YEST_LONGDESC,
            (SELECT YEST.YEST_CANCELSTATUS
               FROM YEST
              WHERE YEST.YEST_ID = YEVM.YEVM_YEST_ID)
               YEST_CANCELSTATUS,
            V8_FNC_YEBS_YFSC_REP_INFOS (YEVM.YEVM_ID, 3, 0) FUNCTION_SPACE,
            V8_FNC_YEBS_YFSC_REP_INFOS (YEVM.YEVM_ID, 3, 1)
               FUNCTION_SPACE_ALTINFO,
            V8_FNC_YEBS_YSSC_REP_INFOS (YEVM.YEVM_ID, 3) SEATING,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 0) RATES,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   1,
                                   0)
               NOTES_SHORT_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   1,
                                   1)
               NOTES_SHORT_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   2,
                                   0)
               NOTES_LONG_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   2,
                                   1)
               NOTES_LONG_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   3,
                                   0)
               NOTES_ND_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEVM',
                                   3,
                                   1)
               NOTES_ND_VIEWABLE,
            --(select yebs.YEBS_ID from yebs where yebs.YEBS_YEVM_ID=yevm.YEVM_ID and rownum=1) T1
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',1,0) FS_NOTES_SHORT_NON_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',1,1) FS_NOTES_SHORT_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',2,0) FS_NOTES_LONG_NON_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',2,1) FS_NOTES_LONG_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',3,0) FS_NOTES_ND_NON_VIEWABLE,
            --V8_FNC_XCNO_REP_INFOS((SELECT YEBS.YEBS_ID FROM YEBS WHERE YEBS.YEBS_YEVM_ID=YEVM.YEVM_ID AND ROWNUM=1),'YEBS',3,1) FS_NOTES_ND_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   1,
                                   0,
                                   NULL,
                                   1)
               FS_NOTES_SHORT_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   1,
                                   1,
                                   NULL,
                                   1)
               FS_NOTES_SHORT_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   2,
                                   0,
                                   NULL,
                                   1)
               FS_NOTES_LONG_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   2,
                                   1,
                                   NULL,
                                   1)
               FS_NOTES_LONG_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   3,
                                   0,
                                   NULL,
                                   1)
               FS_NOTES_ND_NON_VIEWABLE,
            V8_FNC_XCNO_REP_INFOS (YEVM.YEVM_ID,
                                   'YEBS',
                                   3,
                                   1,
                                   NULL,
                                   1)
               FS_NOTES_ND_VIEWABLE,
            DECODE ( (SELECT COUNT (yemn_id)
                        FROM YEMN
                       WHERE yemn_yevm_id = yevm_id),
                    0, 0,
                    1)
               StdMenusFlag,
            DECODE ( (SELECT COUNT (yebr_id)
                        FROM YEBR
                       WHERE yebr_yevm_id = yevm_id),
                    0, 0,
                    1)
               MiscItemsFlag,
            DECODE (
               (SELECT COUNT (a.yevm_id)
                  FROM yevm a
                 WHERE a.yevm_id = yevm.yevm_id AND a.yevm_doorcard <> ' '),
               0, 0,
               1)
               DoorCardFlag,
            YEVM.yevm_doorcard_prof,
              NVL (
                 (SELECT SUM (
                            DECODE (
                               NVL (YEBS.yebs_perhour, 0),
                               0, YEBS.YEBS_ORG_SPECIALRATE,
                               1,   (YEBS.yebs_endtime - YEBS.yebs_starttime)
                                  * 24
                                  * YEBS.YEBS_ORG_SPECIALRATE))
                    FROM YEBS, YEVM yevm_2, YFSR
                   WHERE     YEBS.yebs_yfsr_id = YFSR.yfsr_id(+)
                         AND YEBS.YEBS_YEVM_ID = yevm_2.YEVM_ID
                         AND YEBS.YEBS_PKGINCL = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND TO_NUMBER (
                                TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                TO_NUMBER (
                                   TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J'))),
                 0)
            + NVL (
                 (SELECT SUM (
                              DECODE (
                                 NVL (YEBR.yebr_perhour, 0),
                                 0, YEBR.YEBR_ORG_MANUALPRICE,
                                 1,   (YEBR.yebr_endtime - YEBR.yebr_starttime)
                                    * 24
                                    * YEBR.YEBR_ORG_MANUALPRICE)
                            * YEBR.YEBR_QUANTITY)
                    FROM YEBR, YEVM yevm_2, YCIR
                   WHERE     YEBR.yebr_ycir_id = YCIR.ycir_id(+)
                         AND YEBR.YEBR_YEVM_ID = yevm_2.YEVM_ID
                         AND YEBR.YEBR_PKGINCL = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND TO_NUMBER (
                                TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                TO_NUMBER (
                                   TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J'))),
                 0)
            + NVL (
                 (SELECT SUM (
                              YEMN.YEMN_MENUPRICE
                            * DECODE (
                                 NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                 0, yemn.YEMN_QUANTITY,
                                 DECODE (
                                    SIGN (
                                         yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                       - yemn.YEMN_QUANTITY),
                                    1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                    yemn.YEMN_QUANTITY)))
                    FROM YEMN, YEVM yevm_2
                   WHERE     YEMN.YEMN_YEVM_ID = yevm_2.YEVM_ID
                         AND YEMN.YEMN_PKGINCL = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND TO_NUMBER (
                                TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                TO_NUMBER (
                                   TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J'))),
                 0)
            + NVL (
                 (SELECT SUM (YEMI.YEMI_ORG_PRICE * YEMI.YEMI_QUANTITY)
                    FROM YEMI, YEVM yevm_2
                   WHERE     YEMI.YEMI_YEVM_ID = yevm_2.YEVM_ID
                         AND YEMI.YEMI_INCLUDEDINTHEMENU = 0
                         AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND TO_NUMBER (
                                TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                TO_NUMBER (
                                   TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J'))),
                 0)
            + NVL (
                 (SELECT SUM (YPKD.YPKD_PRICE * YPKD.YPKD_NOOFATTENDEES)
                    FROM YPKD
                   WHERE     YPKD.YPKD_YBOM_ID = YEVM.YEVM_YBOM_ID
                         AND TO_NUMBER (
                                TO_CHAR (TRUNC (YPKD.YPKD_STARTTIME), 'J')) =
                                TO_NUMBER (
                                   TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME), 'J'))),
                 0)
               TOT_ORGREV_PER_DAY,
            V8_FNC_VALCURR_FORMATING (
                 NVL (
                    (SELECT SUM (
                               DECODE (
                                  NVL (YEBS.yebs_perhour, 0),
                                  0, YEBS.YEBS_ORG_SPECIALRATE,
                                  1,   (YEBS.yebs_endtime - YEBS.yebs_starttime)
                                     * 24
                                     * YEBS.YEBS_ORG_SPECIALRATE))
                       FROM YEBS, YEVM yevm_2, YFSR
                      WHERE     YEBS.yebs_yfsr_id = YFSR.yfsr_id(+)
                            AND YEBS.YEBS_YEVM_ID = yevm_2.YEVM_ID
                            AND YEBS.YEBS_PKGINCL = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND TO_NUMBER (
                                   TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                   TO_NUMBER (
                                      TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME),
                                               'J'))),
                    0)
               + NVL (
                    (SELECT SUM (
                                 DECODE (
                                    NVL (YEBR.yebr_perhour, 0),
                                    0, YEBR.YEBR_ORG_MANUALPRICE,
                                    1,   (  YEBR.yebr_endtime
                                          - YEBR.yebr_starttime)
                                       * 24
                                       * YEBR.YEBR_ORG_MANUALPRICE)
                               * YEBR.YEBR_QUANTITY)
                       FROM YEBR, YEVM yevm_2, YCIR
                      WHERE     YEBR.yebr_ycir_id = YCIR.ycir_id(+)
                            AND YEBR.YEBR_YEVM_ID = yevm_2.YEVM_ID
                            AND YEBR.YEBR_PKGINCL = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND TO_NUMBER (
                                   TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                   TO_NUMBER (
                                      TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME),
                                               'J'))),
                    0)
               + NVL (
                    (SELECT SUM (
                                 YEMN.YEMN_MENUPRICE
                               * DECODE (
                                    NVL (yemn.YEMN_POSTGUARANTEED, 0),
                                    0, yemn.YEMN_QUANTITY,
                                    DECODE (
                                       SIGN (
                                            yemn.YEMN_GUARANTEEDNOOFPEOPLE
                                          - yemn.YEMN_QUANTITY),
                                       1, yemn.YEMN_GUARANTEEDNOOFPEOPLE,
                                       yemn.YEMN_QUANTITY)))
                       FROM YEMN, YEVM yevm_2
                      WHERE     YEMN.YEMN_YEVM_ID = yevm_2.YEVM_ID
                            AND YEMN.YEMN_PKGINCL = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND TO_NUMBER (
                                   TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                   TO_NUMBER (
                                      TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME),
                                               'J'))),
                    0)
               + NVL (
                    (SELECT SUM (YEMI.YEMI_ORG_PRICE * YEMI.YEMI_QUANTITY)
                       FROM YEMI, YEVM yevm_2
                      WHERE     YEMI.YEMI_YEVM_ID = yevm_2.YEVM_ID
                            AND YEMI.YEMI_INCLUDEDINTHEMENU = 0
                            AND yevm_2.YEVM_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND TO_NUMBER (
                                   TO_CHAR (TRUNC (yevm_2.YEVM_STARTTIME), 'J')) =
                                   TO_NUMBER (
                                      TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME),
                                               'J'))),
                    0)
               + NVL (
                    (SELECT SUM (YPKD.YPKD_PRICE * YPKD.YPKD_NOOFATTENDEES)
                       FROM YPKD
                      WHERE     YPKD.YPKD_YBOM_ID = YEVM.YEVM_YBOM_ID
                            AND TO_NUMBER (
                                   TO_CHAR (TRUNC (YPKD.YPKD_STARTTIME), 'J')) =
                                   TO_NUMBER (
                                      TO_CHAR (TRUNC (YEVM.YEVM_STARTTIME),
                                               'J'))),
                    0),
               ybom.zcur_shortdesc)
               FTOT_ORGREV_PER_DAY,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 1) ORG_RATES,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 2) REBATE_PERC,
            V8_FNC_YEBS_YFSR_REP_INFOS (YEVM.YEVM_ID, 3, 3) REBATE_FLAT,
            yevm_alternative AlternativeFlag,
            v8_fnc_ccm_alternatives ('YEVM',
                                     yevm.yevm_id,
                                     1,
                                     1)
               Alternatives,
            ybom.zcur_shortdesc zcur_shortdesc
       FROM YEVM,
            (SELECT ybom_id,
                    (SELECT zcur_shortdesc
                       FROM zcur
                      WHERE zcur_id = ybom_base_zcur_id)
                       zcur_shortdesc
               FROM ybom) ybom
      WHERE yevm_ybom_id = ybom.ybom_id AND NVL (yevm_mergedto_yevm_id, 0) = 0
   ORDER BY TRUNC (yevm.yevm_starttime), yevm.yevm_displorder;


