"localhost:/firebird/data/ILICHEVSK2.FDB". Процедуры Previous topic Chapter index Next topic

ACN_RESULTS_PERSONAL_ACCOUNT

 

Входные параметры

Параметр

Тип

Описание

PARTYID

INTEGER

 

PARTYDATE

DATE

 

BEGINDATE

DATE

 

ENDDATE

DATE

 

CROPID

INTEGER

 

CLASSID

INTEGER

 

SORTID

INTEGER

 

STORAGEID

INTEGER

 

OWNERID

INTEGER

 

CONTRACTID

INTEGER

 

STORINGID

INTEGER

 

SHOWRENEWALS

INTEGER

 

SHOWRENEWALSINARRIVAL

INTEGER

 

SHOWTRANSFERINEXPENSE

INTEGER

 

 

Выходные параметры

Параметр

Тип

Описание

CLIENTFULLNAME

VARCHAR(100)

 

ARRIVAL_INVENTORY_DATE

DATE

 

ARRIVAL_INVENTORY_NET

INTEGER

 

ARRIVAL_INVENTORY_HUM

NUMERIC(4,2)

 

ARRIVAL_INVENTORY_CHUM

NUMERIC(15,4)

 

ARRIVAL_INVENTORY_DIRT

NUMERIC(4,2)

 

ARRIVAL_INVENTORY_CDIRT

NUMERIC(15,4)

 

ARRIVAL_MINDATE

DATE

 

ARRIVAL_MAXDATE

DATE

 

ARRIVAL_NET

INTEGER

 

ARRIVAL_HUM

NUMERIC(4,2)

 

ARRIVAL_CHUM

NUMERIC(15,4)

 

ARRIVAL_DIRT

NUMERIC(4,2)

 

ARRIVAL_CDIRT

NUMERIC(15,4)

 

ARRIVAL_RENEWAL_MINDATE

DATE

 

ARRIVAL_RENEWAL_MAXDATE

DATE

 

ARRIVAL_RENEWAL_NET

INTEGER

 

ARRIVAL_RENEWAL_HUM

NUMERIC(4,2)

 

ARRIVAL_RENEWAL_CHUM

NUMERIC(15,4)

 

ARRIVAL_RENEWAL_DIRT

NUMERIC(4,2)

 

ARRIVAL_RENEWAL_CDIRT

NUMERIC(15,4)

 

ARRIVAL_TOTAL_NET

BIGINT

 

ARRIVAL_TOTAL_HUM

NUMERIC(4,2)

 

ARRIVAL_TOTAL_CHUM

NUMERIC(15,4)

 

ARRIVAL_TOTAL_DIRT

NUMERIC(4,2)

 

ARRIVAL_TOTAL_CDIRT

NUMERIC(15,4)

 

EXPENSE_MINDATE

DATE

 

EXPENSE_MAXDATE

DATE

 

EXPENSE_NET

INTEGER

 

EXPENSE_HUM

NUMERIC(4,2)

 

EXPENSE_CHUM

NUMERIC(15,4)

 

EXPENSE_DIRT

NUMERIC(4,2)

 

EXPENSE_CDIRT

INTEGER

 

EXPENSE_RENEWAL_MINDATE

DATE

 

EXPENSE_RENEWAL_MAXDATE

DATE

 

EXPENSE_RENEWAL_NET

INTEGER

 

EXPENSE_RENEWAL_HUM

NUMERIC(4,2)

 

EXPENSE_RENEWAL_CHUM

NUMERIC(15,4)

 

EXPENSE_RENEWAL_DIRT

NUMERIC(4,2)

 

EXPENSE_RENEWAL_CDIRT

NUMERIC(15,4)

 

WASTE1_NET

INTEGER

 

WASTE1_HUM

NUMERIC(4,2)

 

WASTE1_CHUM

INTEGER

 

WASTE1_CHUM_2

NUMERIC(15,4)

 

WASTE1_DIRT

NUMERIC(4,2)

 

WASTE1_CDIRT

INTEGER

 

WASTE1_CDIRT_2

NUMERIC(15,4)

 

WASTE3_NET

INTEGER

 

WASTE3_HUM

NUMERIC(4,2)

 

WASTE3_CHUM

INTEGER

 

WASTE3_CHUM_2

NUMERIC(15,4)

 

WASTE3_DIRT

NUMERIC(4,2)

 

WASTE3_CDIRT

INTEGER

 

WASTE3_CDIRT_2

NUMERIC(15,4)

 

WASTE_NET

INTEGER

 

WASTE_HUM

NUMERIC(4,2)

 

WASTE_CHUM

INTEGER

 

WASTE_CHUM_2

NUMERIC(15,4)

 

WASTE_DIRT

NUMERIC(4,2)

 

WASTE_CDIRT

INTEGER

 

WASTE_CDIRT_2

NUMERIC(15,4)

 

EXPENSE_INVENTORY_DATE

DATE

 

EXPENSE_INVENTORY_NET

INTEGER

 

EXPENSE_INVENTORY_HUM

NUMERIC(4,2)

 

EXPENSE_INVENTORY_CHUM

NUMERIC(15,4)

 

EXPENSE_INVENTORY_DIRT

NUMERIC(5,2)

 

EXPENSE_INVENTORY_CDIRT

NUMERIC(15,4)

 

EXPENSE_TOTAL_NET

BIGINT

 

EXPENSE_TOTAL_HUM

NUMERIC(4,2)

 

EXPENSE_TOTAL_CHUM

NUMERIC(15,4)

 

EXPENSE_TOTAL_DIRT

NUMERIC(4,2)

 

EXPENSE_TOTAL_CDIRT

NUMERIC(15,4)

 

ARRIVAL_TOTAL_NET_2

INTEGER

 

ARRIVAL_TOTAL_HUM_2

NUMERIC(4,2)

 

ARRIVAL_TOTAL_CHUM_2

NUMERIC(15,4)

 

ARRIVAL_TOTAL_DIRT_2

NUMERIC(4,2)

 

ARRIVAL_TOTAL_CDIRT_2

NUMERIC(15,4)

 

EXPENSE_TOTAL_NET_2

INTEGER

 

EXPENSE_TOTAL_HUM_2

NUMERIC(4,2)

 

EXPENSE_TOTAL_CHUM_2

NUMERIC(15,4)

 

EXPENSE_TOTAL_DIRT_2

NUMERIC(4,2)

 

EXPENSE_TOTAL_CDIRT_2

NUMERIC(15,4)

 

HUMWEIGHT

INTEGER

 

DIRTWEIGHT

INTEGER

 

DIRTWEIGHT_2

INTEGER

 

SCREENWEIGHT

INTEGER

 

SCREENWEIGHT_2

INTEGER

 

WEIGHT

INTEGER

 

REST_WEIGHT

INTEGER

 

REST_HUM

NUMERIC(4,2)

 

REST_HUM_C

NUMERIC(15,4)

 

REST_DIRT

NUMERIC(4,2)

 

REST_DIRT_C

NUMERIC(15,4)

 

 

Описание

(Нет описания для процедуры ACN_RESULTS_PERSONAL_ACCOUNT)

 

Определение

CREATE PROCEDURE ACN_RESULTS_PERSONAL_ACCOUNT(
    PARTYID INTEGER,
    PARTYDATE DATE,
    BEGINDATE DATE,
    ENDDATE DATE,
    CROPID INTEGER,
    CLASSID INTEGER,
    SORTID INTEGER,
    STORAGEID INTEGER,
    OWNERID INTEGER,
    CONTRACTID INTEGER,
    STORINGID INTEGER,
    SHOWRENEWALS INTEGER,
    SHOWRENEWALSINARRIVAL INTEGER,
    SHOWTRANSFERINEXPENSE INTEGER)
RETURNS (
    CLIENTFULLNAME VARCHAR(100),
    ARRIVAL_INVENTORY_DATE DATE,
    ARRIVAL_INVENTORY_NET INTEGER,
    ARRIVAL_INVENTORY_HUM NUMERIC(4,2),
    ARRIVAL_INVENTORY_CHUM NUMERIC(15,4),
    ARRIVAL_INVENTORY_DIRT NUMERIC(4,2),
    ARRIVAL_INVENTORY_CDIRT NUMERIC(15,4),
    ARRIVAL_MINDATE DATE,
    ARRIVAL_MAXDATE DATE,
    ARRIVAL_NET INTEGER,
    ARRIVAL_HUM NUMERIC(4,2),
    ARRIVAL_CHUM NUMERIC(15,4),
    ARRIVAL_DIRT NUMERIC(4,2),
    ARRIVAL_CDIRT NUMERIC(15,4),
    ARRIVAL_RENEWAL_MINDATE DATE,
    ARRIVAL_RENEWAL_MAXDATE DATE,
    ARRIVAL_RENEWAL_NET INTEGER,
    ARRIVAL_RENEWAL_HUM NUMERIC(4,2),
    ARRIVAL_RENEWAL_CHUM NUMERIC(15,4),
    ARRIVAL_RENEWAL_DIRT NUMERIC(4,2),
    ARRIVAL_RENEWAL_CDIRT NUMERIC(15,4),
    ARRIVAL_TOTAL_NET BIGINT,
    ARRIVAL_TOTAL_HUM NUMERIC(4,2),
    ARRIVAL_TOTAL_CHUM NUMERIC(15,4),
    ARRIVAL_TOTAL_DIRT NUMERIC(4,2),
    ARRIVAL_TOTAL_CDIRT NUMERIC(15,4),
    EXPENSE_MINDATE DATE,
    EXPENSE_MAXDATE DATE,
    EXPENSE_NET INTEGER,
    EXPENSE_HUM NUMERIC(4,2),
    EXPENSE_CHUM NUMERIC(15,4),
    EXPENSE_DIRT NUMERIC(4,2),
    EXPENSE_CDIRT INTEGER,
    EXPENSE_RENEWAL_MINDATE DATE,
    EXPENSE_RENEWAL_MAXDATE DATE,
    EXPENSE_RENEWAL_NET INTEGER,
    EXPENSE_RENEWAL_HUM NUMERIC(4,2),
    EXPENSE_RENEWAL_CHUM NUMERIC(15,4),
    EXPENSE_RENEWAL_DIRT NUMERIC(4,2),
    EXPENSE_RENEWAL_CDIRT NUMERIC(15,4),
    WASTE1_NET INTEGER,
    WASTE1_HUM NUMERIC(4,2),
    WASTE1_CHUM INTEGER,
    WASTE1_CHUM_2 NUMERIC(15,4),
    WASTE1_DIRT NUMERIC(4,2),
    WASTE1_CDIRT INTEGER,
    WASTE1_CDIRT_2 NUMERIC(15,4),
    WASTE3_NET INTEGER,
    WASTE3_HUM NUMERIC(4,2),
    WASTE3_CHUM INTEGER,
    WASTE3_CHUM_2 NUMERIC(15,4),
    WASTE3_DIRT NUMERIC(4,2),
    WASTE3_CDIRT INTEGER,
    WASTE3_CDIRT_2 NUMERIC(15,4),
    WASTE_NET INTEGER,
    WASTE_HUM NUMERIC(4,2),
    WASTE_CHUM INTEGER,
    WASTE_CHUM_2 NUMERIC(15,4),
    WASTE_DIRT NUMERIC(4,2),
    WASTE_CDIRT INTEGER,
    WASTE_CDIRT_2 NUMERIC(15,4),
    EXPENSE_INVENTORY_DATE DATE,
    EXPENSE_INVENTORY_NET INTEGER,
    EXPENSE_INVENTORY_HUM NUMERIC(4,2),
    EXPENSE_INVENTORY_CHUM NUMERIC(15,4),
    EXPENSE_INVENTORY_DIRT NUMERIC(5,2),
    EXPENSE_INVENTORY_CDIRT NUMERIC(15,4),
    EXPENSE_TOTAL_NET BIGINT,
    EXPENSE_TOTAL_HUM NUMERIC(4,2),
    EXPENSE_TOTAL_CHUM NUMERIC(15,4),
    EXPENSE_TOTAL_DIRT NUMERIC(4,2),
    EXPENSE_TOTAL_CDIRT NUMERIC(15,4),
    ARRIVAL_TOTAL_NET_2 INTEGER,
    ARRIVAL_TOTAL_HUM_2 NUMERIC(4,2),
    ARRIVAL_TOTAL_CHUM_2 NUMERIC(15,4),
    ARRIVAL_TOTAL_DIRT_2 NUMERIC(4,2),
    ARRIVAL_TOTAL_CDIRT_2 NUMERIC(15,4),
    EXPENSE_TOTAL_NET_2 INTEGER,
    EXPENSE_TOTAL_HUM_2 NUMERIC(4,2),
    EXPENSE_TOTAL_CHUM_2 NUMERIC(15,4),
    EXPENSE_TOTAL_DIRT_2 NUMERIC(4,2),
    EXPENSE_TOTAL_CDIRT_2 NUMERIC(15,4),
    HUMWEIGHT INTEGER,
    DIRTWEIGHT INTEGER,
    DIRTWEIGHT_2 INTEGER,
    SCREENWEIGHT INTEGER,
    SCREENWEIGHT_2 INTEGER,
    WEIGHT INTEGER,
    REST_WEIGHT INTEGER,
    REST_HUM NUMERIC(4,2),
    REST_HUM_C NUMERIC(15,4),
    REST_DIRT NUMERIC(4,2),
    REST_DIRT_C NUMERIC(15,4))
AS
declare variable WASTE1_AVERAGE_HUM numeric(4,2);
declare variable WASTE1_AVERAGE_DIRT numeric(4,2);
declare variable WASTE3_AVERAGE_HUM numeric(4,2);
declare variable WASTE3_AVERAGE_DIRT numeric(4,2);
declare variable WASTE_AVERAGE_HUM numeric(4,2);
declare variable WASTE_AVERAGE_DIRT numeric(4,2);
begin

  if (:BEGINDATE is null) then
    select
        min(IT.REGDATE) MINDATE
      from IT
      where (IT.PARTYID = :PARTYID)
        and (IT.REGDATE is not null)
        and (IT.WEIGHT is not null)
        and (IT.STATE <> -1)
    into :BEGINDATE;

  for

    select
        CLIENTFULLNAME,

        max(ARRIVAL_INVENTORY_DATE) ARRIVAL_INVENTORY_DATE,
        coalesce(sum(ARRIVAL_INVENTORY_NET),0) ARRIVAL_INVENTORY_NET,
        case
          when (sum(ARRIVAL_INVENTORY_NET) <> 0) then
            cast(100 * cast(sum(ARRIVAL_INVENTORY_CHUM) as numeric(15,4)) / sum(ARRIVAL_INVENTORY_NET) as numeric(4,2))
          else 0
        end ARRIVAL_INVENTORY_HUM,
        coalesce(sum(ARRIVAL_INVENTORY_CHUM),0) ARRIVAL_INVENTORY_CHUM,
        case
          when (sum(ARRIVAL_INVENTORY_NET) <> 0) then
            cast(100 * cast(sum(ARRIVAL_INVENTORY_CDIRT) as numeric(15,4)) / sum(ARRIVAL_INVENTORY_NET) as numeric(4,2))
          else 0
        end ARRIVAL_INVENTORY_DIRT,
        coalesce(sum(ARRIVAL_INVENTORY_CDIRT),0) ARRIVAL_INVENTORY_CDIRT,

        min(ARRIVAL_MINDATE) ARRIVAL_MINDATE,
        max(ARRIVAL_MAXDATE) ARRIVAL_MAXDATE,
        coalesce(sum(ARRIVAL_NET),0) ARRIVAL_NET,
        case
          when (sum(ARRIVAL_NET) <> 0) then
            cast(100 * cast(sum(ARRIVAL_CHUM) as numeric(15,4)) / sum(ARRIVAL_NET) as numeric(4,2))
          else 0
        end ARRIVAL_HUM,
        coalesce(sum(ARRIVAL_CHUM),0) ARRIVAL_CHUM,
        case
          when (sum(ARRIVAL_NET) <> 0) then
            cast(100 * cast(sum(ARRIVAL_CDIRT) as numeric(15,4)) / sum(ARRIVAL_NET) as numeric(4,2))
          else 0
        end ARRIVAL_DIRT,
        coalesce(sum(ARRIVAL_CDIRT),0) ARRIVAL_CDIRT,

        min(ARRIVAL_RENEWAL_MINDATE) ARRIVAL_RENEWAL_MINDATE,
        max(ARRIVAL_RENEWAL_MAXDATE) ARRIVAL_RENEWAL_MAXDATE,
        coalesce(sum(ARRIVAL_RENEWAL_NET),0) ARRIVAL_RENEWAL_NET,
        case
          when (sum(ARRIVAL_RENEWAL_NET) <> 0) then
            cast(100 * cast(sum(ARRIVAL_RENEWAL_CHUM) as numeric(15,4)) / sum(ARRIVAL_RENEWAL_NET) as numeric(4,2))
          else 0
        end ARRIVAL_RENEWAL_HUM,
        coalesce(sum(ARRIVAL_RENEWAL_CHUM),0) ARRIVAL_RENEWAL_CHUM,
        case
          when (sum(ARRIVAL_RENEWAL_NET) <> 0) then
            cast(100 * cast(sum(ARRIVAL_RENEWAL_CDIRT) as numeric(15,4)) / sum(ARRIVAL_RENEWAL_NET) as numeric(4,2))
          else 0
        end ARRIVAL_RENEWAL_DIRT,
        coalesce(sum(ARRIVAL_RENEWAL_CDIRT),0) ARRIVAL_RENEWAL_CDIRT,

        min(EXPENSE_MINDATE) EXPENSE_MINDATE,
        max(coalesce(EXPENSE_MAXDATE,:ENDDATE)) EXPENSE_MAXDATE,
        coalesce(sum(EXPENSE_NET),0) EXPENSE_NET,
        case
          when (sum(EXPENSE_NET) <> 0) then
--            cast(100 * cast(sum(EXPENSE_CHUM_2) as numeric(15,4)) / sum(EXPENSE_NET) as numeric(4,2))
            cast(100 * cast(sum(EXPENSE_CHUM) as numeric(15,4)) / sum(EXPENSE_NET) as numeric(4,2))
          else 0
        end EXPENSE_HUM,
        coalesce(sum(EXPENSE_CHUM),0) EXPENSE_CHUM,
        case
          when (sum(EXPENSE_NET) <> 0) then
--            cast(100 * cast(sum(EXPENSE_CDIRT_2) as numeric(15,4)) / sum(EXPENSE_NET) as numeric(4,2))
            cast(100 * cast(sum(EXPENSE_CDIRT) as numeric(15,4)) / sum(EXPENSE_NET) as numeric(4,2))
          else 0
        end EXPENSE_DIRT,
        coalesce(sum(EXPENSE_CDIRT),0) EXPENSE_CDIRT,

        min(EXPENSE_RENEWAL_MINDATE) EXPENSE_RENEWAL_MINDATE,
        max(coalesce(EXPENSE_RENEWAL_MAXDATE,:BEGINDATE)) EXPENSE_RENEWAL_MAXDATE,
        coalesce(sum(EXPENSE_RENEWAL_NET),0) EXPENSE_RENEWAL_NET,
        case
          when (sum(EXPENSE_RENEWAL_NET) <> 0) then
            cast(100 * cast(sum(EXPENSE_RENEWAL_CHUM) as numeric(15,4)) / sum(EXPENSE_RENEWAL_NET) as numeric(4,2))
          else 0
        end EXPENSE_RENEWAL_HUM,
        coalesce(sum(EXPENSE_RENEWAL_CHUM),0) EXPENSE_CHUM,
        case
          when (sum(EXPENSE_RENEWAL_NET) <> 0) then
            cast(100 * cast(sum(EXPENSE_RENEWAL_CDIRT) as numeric(15,4)) / sum(EXPENSE_RENEWAL_NET) as numeric(4,2))
          else 0
        end EXPENSE_RENEWAL_DIRT,
        coalesce(sum(EXPENSE_RENEWAL_CDIRT),0) EXPENSE_RENEWAL_CDIRT,

        coalesce(sum(WASTE1_NET),0) WASTE1_NET,
        max(WASTE1_HUM) WASTE1_HUM,
        coalesce(sum(WASTE1_CHUM),0) WASTE1_CHUM,
        coalesce(sum(WASTE1_CHUM_2),0) WASTE1_CHUM_2,
        max(WASTE1_DIRT) WASTE1_DIRT,
        coalesce(sum(WASTE1_CDIRT),0) WASTE1_CDIRT,
        coalesce(sum(WASTE1_CDIRT_2),0) WASTE1_CDIRT_2,

        coalesce(sum(WASTE3_NET),0) WASTE3_NET,
        max(WASTE3_HUM) WASTE3_HUM,
        coalesce(sum(WASTE3_CHUM),0) WASTE3_CHUM,
        coalesce(sum(WASTE3_CHUM_2),0) WASTE3_CHUM_2,
        max(WASTE3_DIRT) WASTE3_DIRT,
        coalesce(sum(WASTE3_CDIRT),0) WASTE3_CDIRT,
        coalesce(sum(WASTE3_CDIRT_2),0) WASTE3_CDIRT_2,

        coalesce(sum(WASTE_NET),0) WASTE_NET,
        max(WASTE_HUM) WASTE_HUM,
        coalesce(sum(WASTE_CHUM),0) WASTE_CHUM,
        coalesce(sum(WASTE_CHUM_2),0) WASTE_CHUM_2,
        max(WASTE_DIRT) WASTE_DIRT,
        coalesce(sum(WASTE_CDIRT),0) WASTE_CDIRT,
        coalesce(sum(WASTE_CDIRT_2),0) WASTE_CDIRT_2,

        max(coalesce(EXPENSE_INVENTORY_DATE,:BEGINDATE)) EXPENSE_INVENTORY_DATE,
        coalesce(sum(EXPENSE_INVENTORY_NET),0) EXPENSE_INVENTORY_NET,
         case
           when (sum(EXPENSE_INVENTORY_NET) <> 0) then
             cast(100 * cast(sum(EXPENSE_INVENTORY_CHUM) as numeric(15,4)) / sum(EXPENSE_INVENTORY_NET) as numeric(4,2))
          else 0
        end EXPENSE_INVENTORY_HUM,
        coalesce(sum(EXPENSE_INVENTORY_CHUM),0) EXPENSE_INVENTORY_CHUM,
        case
          when (sum(EXPENSE_INVENTORY_NET) <> 0) then
            cast(100 * cast(sum(EXPENSE_INVENTORY_CDIRT) as numeric(15,4)) / sum(EXPENSE_INVENTORY_NET) as numeric(4,2))
          else 0
        end EXPENSE_INVENTORY_DIRT,
        coalesce(sum(EXPENSE_INVENTORY_CDIRT),0) EXPENSE_INVENTORY_CDIRT,

        coalesce(sum(HUMWEIGHT),0) HUMWEIGHT,

        coalesce(sum(DIRTWEIGHT),0) DIRTWEIGHT,
        coalesce(sum(DIRTWEIGHT_2),0) DIRTWEIGHT_2,

        coalesce(sum(SCREENWEIGHT),0) SCREENWEIGHT,
        coalesce(sum(SCREENWEIGHT_2),0) SCREENWEIGHT_2,

        coalesce(sum(WEIGHT),0) WEIGHT,

        coalesce(sum(REST_WEIGHT),0) REST_WEIGHT,
        case
          when (sum(REST_WEIGHT) <> 0) then
            cast(100 * cast(sum(REST_HUM_C) as numeric(15,4)) / sum(REST_WEIGHT) as numeric(4,2))
          else 0
        end REST_HUM,
        coalesce(sum(REST_HUM_C),0) REST_HUM_C,
        case
          when (sum(REST_WEIGHT) <> 0) then
            cast(100 * cast(sum(REST_DIRT_C) as numeric(15,4)) / sum(REST_WEIGHT) as numeric(4,2))
          else 0
        end REST_DIRT,
        coalesce(sum(REST_DIRT_C),0) REST_DIRT_C
      from (
        select
            CLIENTFULLNAME,
--            REGDATE,
--            OPERATIONID,

            max(ARRIVAL_INVENTORY_DATE) ARRIVAL_INVENTORY_DATE,
            sum(ARRIVAL_INVENTORY_NET) ARRIVAL_INVENTORY_NET,
            sum(ARRIVAL_INVENTORY_CHUM) ARRIVAL_INVENTORY_CHUM,
            sum(ARRIVAL_INVENTORY_CDIRT) ARRIVAL_INVENTORY_CDIRT,

            min(ARRIVAL_MINDATE) ARRIVAL_MINDATE,
            max(ARRIVAL_MAXDATE) ARRIVAL_MAXDATE,
            sum(ARRIVAL_NET) ARRIVAL_NET,
            sum(ARRIVAL_CHUM) ARRIVAL_CHUM,
            sum(ARRIVAL_CDIRT) ARRIVAL_CDIRT,

            min(ARRIVAL_RENEWAL_MINDATE) ARRIVAL_RENEWAL_MINDATE,
            max(ARRIVAL_RENEWAL_MAXDATE) ARRIVAL_RENEWAL_MAXDATE,
            sum(ARRIVAL_RENEWAL_NET) ARRIVAL_RENEWAL_NET,
            sum(ARRIVAL_RENEWAL_CHUM) ARRIVAL_RENEWAL_CHUM,
            sum(ARRIVAL_RENEWAL_CDIRT) ARRIVAL_RENEWAL_CDIRT,

            min(EXPENSE_MINDATE) EXPENSE_MINDATE,
            max(EXPENSE_MAXDATE) EXPENSE_MAXDATE,
            sum(EXPENSE_NET) EXPENSE_NET,
            sum(EXPENSE_CHUM) EXPENSE_CHUM,
            sum(EXPENSE_CHUM_2) EXPENSE_CHUM_2,
            sum(EXPENSE_CDIRT) EXPENSE_CDIRT,
            sum(EXPENSE_CDIRT_2) EXPENSE_CDIRT_2,

            min(EXPENSE_RENEWAL_MINDATE) EXPENSE_RENEWAL_MINDATE,
            max(EXPENSE_RENEWAL_MAXDATE) EXPENSE_RENEWAL_MAXDATE,
            sum(EXPENSE_RENEWAL_NET) EXPENSE_RENEWAL_NET,
            sum(EXPENSE_RENEWAL_CHUM) EXPENSE_RENEWAL_CHUM,
            sum(EXPENSE_RENEWAL_CDIRT) EXPENSE_RENEWAL_CDIRT,

            sum(WASTE1_NET) WASTE1_NET,
            max(WASTE1_HUM) WASTE1_HUM,
            max(WASTE1_DIRT) WASTE1_DIRT,
            sum(WASTE1_CHUM) WASTE1_CHUM,
            sum(WASTE1_CDIRT) WASTE1_CDIRT,
            sum(WASTE1_CHUM_2) WASTE1_CHUM_2,
            sum(WASTE1_CDIRT_2) WASTE1_CDIRT_2,

            sum(WASTE3_NET) WASTE3_NET,
            max(WASTE3_HUM) WASTE3_HUM,
            max(WASTE3_DIRT) WASTE3_DIRT,
            sum(WASTE3_CHUM) WASTE3_CHUM,
            sum(WASTE3_CDIRT) WASTE3_CDIRT,
            sum(WASTE3_CHUM_2) WASTE3_CHUM_2,
            sum(WASTE3_CDIRT_2) WASTE3_CDIRT_2,

            sum(WASTE_NET) WASTE_NET,
            max(WASTE_HUM) WASTE_HUM,
            max(WASTE_DIRT) WASTE_DIRT,
            sum(WASTE_CHUM) WASTE_CHUM,
            sum(WASTE_CDIRT) WASTE_CDIRT,
            sum(WASTE_CHUM_2) WASTE_CHUM_2,
            sum(WASTE_CDIRT_2) WASTE_CDIRT_2,

            max(EXPENSE_INVENTORY_DATE) EXPENSE_INVENTORY_DATE,
            sum(EXPENSE_INVENTORY_NET) EXPENSE_INVENTORY_NET,
            sum(EXPENSE_INVENTORY_CHUM) EXPENSE_INVENTORY_CHUM,
            sum(EXPENSE_INVENTORY_CDIRT) EXPENSE_INVENTORY_CDIRT,
            
            sum(HUMWEIGHT) HUMWEIGHT,

            sum(DIRTWEIGHT) DIRTWEIGHT,
            sum(DIRTWEIGHT_2) DIRTWEIGHT_2,

            sum(SCREENWEIGHT) SCREENWEIGHT,
            sum(SCREENWEIGHT_2) SCREENWEIGHT_2,

            sum(WEIGHT) WEIGHT,

            sum(REST_WEIGHT) REST_WEIGHT,
            sum(REST_HUM_C) REST_HUM_C,
            sum(REST_DIRT_C) REST_DIRT_C
          from (
            select
                ACN_FORMA36_MAIN.OWNERFULLNAME CLIENTFULLNAME,
--                ACN_FORMA36_MAIN.REGDATE REGDATE,
--                ACN_FORMA36_MAIN.ANALIS_ID ANALIS_ID,
                ACN_FORMA36_MAIN.OPERATIONID OPERATIONID,
                ACN_FORMA36_MAIN.WASTECLASSID WASTECLASSID,
                ACN_FORMA36_MAIN.OPERATION_ID OPERATION_ID,

                case
                  when (ACN_FORMA36_MAIN.OPERATIONID in (18,45)) then
                    max(ACN_FORMA36_MAIN.REGDATE)
                end ARRIVAL_INVENTORY_DATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID in (18,45)) then
                    coalesce(sum(ACN_FORMA36_MAIN.COMNET),0)
                end ARRIVAL_INVENTORY_NET,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID in (18,45)) then
/*
                        case
                          when (ACN_FORMA36_MAIN.COMNET < 20000) then
                            ACN_FORMA36_MAIN.HUMIDITY_C_2
                          else ACN_FORMA36_MAIN.HUMIDITY_C
                        end
*/
                      ACN_FORMA36_MAIN.HUMIDITY_C
                    end) ARRIVAL_INVENTORY_CHUM,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID in (18,45)) then
/*
                        case
                          when (ACN_FORMA36_MAIN.COMNET < 20000) then
                            ACN_FORMA36_MAIN.DIRT_C_2
                          else ACN_FORMA36_MAIN.DIRT_C
                        end
*/
                      ACN_FORMA36_MAIN.DIRT_C
                    end) ARRIVAL_INVENTORY_CDIRT,

                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
                    min(ACN_FORMA36_MAIN.REGDATE)
                end ARRIVAL_MINDATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
                    max(ACN_FORMA36_MAIN.REGDATE)
                end ARRIVAL_MAXDATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
                    coalesce(sum(ACN_FORMA36_MAIN.COMNET),0)
                end ARRIVAL_NET,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
/*
                        case
                          when (ACN_FORMA36_MAIN.COMNET < 20000) then
                            ACN_FORMA36_MAIN.HUMIDITY_C_2
                          else ACN_FORMA36_MAIN.HUMIDITY_C

*/
                        ACN_FORMA36_MAIN.HUMIDITY_C
                    end) ARRIVAL_CHUM,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
/*
                        case
                          when (ACN_FORMA36_MAIN.COMNET < 20000) then
                            ACN_FORMA36_MAIN.DIRT_C_2
                          else ACN_FORMA36_MAIN.DIRT_C
                        end
*/
                      ACN_FORMA36_MAIN.DIRT_C
                    end) ARRIVAL_CDIRT,

                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 17) and (min(ACN_FORMA36_MAIN.COMNET) > 0)) then
                    min(ACN_FORMA36_MAIN.REGDATE)
                end ARRIVAL_RENEWAL_MINDATE,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 17) and (min(ACN_FORMA36_MAIN.COMNET) > 0)) then
                    max(ACN_FORMA36_MAIN.REGDATE)
                end ARRIVAL_RENEWAL_MAXDATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 17) then
                    coalesce(sum(ACN_FORMA36_MAIN.COMNET),0)
                end ARRIVAL_RENEWAL_NET,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 17) then
/*
                        case
                          when (ACN_FORMA36_MAIN.COMNET < 20000) then
                            ACN_FORMA36_MAIN.HUMIDITY_C_2
                          else ACN_FORMA36_MAIN.HUMIDITY_C
                        end
*/
                      ACN_FORMA36_MAIN.HUMIDITY_C
                    end) ARRIVAL_RENEWAL_CHUM,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 17) then
/*
                        case
                          when (ACN_FORMA36_MAIN.COMNET < 20000) then
                            ACN_FORMA36_MAIN.DIRT_C_2
                          else ACN_FORMA36_MAIN.DIRT_C
                        end
*/
                      ACN_FORMA36_MAIN.DIRT_C
                    end) ARRIVAL_RENEWAL_CDIRT,

                case
                  when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
                    min(ACN_FORMA36_MAIN.REGDATE)
/*                  else
                    case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
                        min(ACN_FORMA36_MAIN.REGDATE)
                    end*/
                end EXPENSE_MINDATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
                    max(ACN_FORMA36_MAIN.REGDATE)
/*
                  else
                    case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
                        min(ACN_FORMA36_MAIN.REGDATE)
                    end
*/
                end EXPENSE_MAXDATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
                    coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                  else
                    case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 1) then
                        coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                    end
                end EXPENSE_NET,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
                        ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                    end) EXPENSE_CHUM,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
/*
                        case
                          when (ACN_FORMA36_MAIN.EXPENSENET < 20000) then
                            ACN_FORMA36_MAIN.HUMIDITY_C_EXP_2
                          else ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                        end
*/
                      ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                    end) EXPENSE_CHUM_2,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
                        ACN_FORMA36_MAIN.DIRT_C_EXP
                    end) EXPENSE_CDIRT,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID in (5,70)) then
/*
                        case
                          when (ACN_FORMA36_MAIN.EXPENSENET < 20000) then
                            ACN_FORMA36_MAIN.DIRT_C_EXP_2
                          else ACN_FORMA36_MAIN.DIRT_C_EXP
                        end
*/
                      ACN_FORMA36_MAIN.DIRT_C_EXP
                    end) EXPENSE_CDIRT_2,

                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 17) and (min(ACN_FORMA36_MAIN.EXPENSENET) > 0)) then
                    min(ACN_FORMA36_MAIN.REGDATE)
                end EXPENSE_RENEWAL_MINDATE,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 17) and (min(ACN_FORMA36_MAIN.EXPENSENET) > 0)) then
                    max(ACN_FORMA36_MAIN.REGDATE)
                end EXPENSE_RENEWAL_MAXDATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 17) then
                    coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                end EXPENSE_RENEWAL_NET,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 17) then
/*
                        case
                          when (ACN_FORMA36_MAIN.EXPENSENET < 20000) then
                            ACN_FORMA36_MAIN.HUMIDITY_C_EXP_2
                          else ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                        end
*/
                      ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                    end) EXPENSE_RENEWAL_CHUM,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 17) then
/*
                        case
                          when (ACN_FORMA36_MAIN.EXPENSENET < 20000) then
                            ACN_FORMA36_MAIN.DIRT_C_EXP_2
                          else ACN_FORMA36_MAIN.DIRT_C_EXP
                        end
*/
                      ACN_FORMA36_MAIN.DIRT_C_EXP
                    end) EXPENSE_RENEWAL_CDIRT,

                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                end WASTE1_NET,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    max(ACN_FORMA36_MAIN.HUMIDITY)
                end WASTE1_HUM,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    max(ACN_FORMA36_MAIN.DIRT)
                end WASTE1_DIRT,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    sum(ACN_FORMA36_MAIN.HUMIDITY_C_EXP)
                end WASTE1_CHUM,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    sum(ACN_FORMA36_MAIN.DIRT_C_EXP)
                end WASTE1_CDIRT,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    sum(ACN_FORMA36_MAIN.HUMIDITY_C_EXP_2)
                end WASTE1_CHUM_2,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (not (ACN_FORMA36_MAIN.WASTECLASSID in (41,141)))) then
                    sum(ACN_FORMA36_MAIN.DIRT_C_EXP_2)
                end WASTE1_CDIRT_2,

                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                end WASTE3_NET,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    max(ACN_FORMA36_MAIN.HUMIDITY)
                end WASTE3_HUM,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    max(ACN_FORMA36_MAIN.DIRT)
                end WASTE3_DIRT,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    sum(ACN_FORMA36_MAIN.HUMIDITY_C_EXP)
                end WASTE3_CHUM,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    sum(ACN_FORMA36_MAIN.DIRT_C_EXP)
                end WASTE3_CDIRT,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    sum(ACN_FORMA36_MAIN.HUMIDITY_C_EXP_2)
                end WASTE3_CHUM_2,
                case
                  when ((ACN_FORMA36_MAIN.OPERATIONID = 22) and (ACN_FORMA36_MAIN.WASTECLASSID in (41,141))) then
                    sum(ACN_FORMA36_MAIN.DIRT_C_EXP_2)
                end WASTE3_CDIRT_2,

                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                end WASTE_NET,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    max(ACN_FORMA36_MAIN.HUMIDITY)
                end WASTE_HUM,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    max(ACN_FORMA36_MAIN.DIRT)
                end WASTE_DIRT,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    sum(ACN_FORMA36_MAIN.HUMIDITY_C_EXP)
                end WASTE_CHUM,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    sum(ACN_FORMA36_MAIN.DIRT_C_EXP)
                end WASTE_CDIRT,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    sum(ACN_FORMA36_MAIN.HUMIDITY_C_EXP_2)
                end WASTE_CHUM_2,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 22) then
                    sum(ACN_FORMA36_MAIN.DIRT_C_EXP_2)
                end WASTE_CDIRT_2,

                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 46) then
                    max(ACN_FORMA36_MAIN.REGDATE)
                end EXPENSE_INVENTORY_DATE,
                case
                  when (ACN_FORMA36_MAIN.OPERATIONID = 46) then
                    coalesce(sum(ACN_FORMA36_MAIN.EXPENSENET),0)
                end EXPENSE_INVENTORY_NET,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 46) then
/*
                        case
                          when (ACN_FORMA36_MAIN.EXPENSENET < 20000) then
                            ACN_FORMA36_MAIN.HUMIDITY_C_EXP_2
                          else ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                        end
*/
                      ACN_FORMA36_MAIN.HUMIDITY_C_EXP
                    end) EXPENSE_INVENTORY_CHUM,
                sum(case
                      when (ACN_FORMA36_MAIN.OPERATIONID = 46) then
/*
                        case
                          when (ACN_FORMA36_MAIN.EXPENSENET < 20000) then
                            ACN_FORMA36_MAIN.DIRT_C_EXP_2
                          else ACN_FORMA36_MAIN.DIRT_C_EXP
                        end
*/
                      ACN_FORMA36_MAIN.DIRT_C_EXP
                    end) EXPENSE_INVENTORY_CDIRT,

                coalesce(sum(ACN_FORMA36_MAIN.HUMWEIGHT_2),0) HUMWEIGHT,

                coalesce(sum(ACN_FORMA36_MAIN.DIRTWEIGHT),0) DIRTWEIGHT,
                coalesce(sum(ACN_FORMA36_MAIN.DIRTWEIGHT_2),0) DIRTWEIGHT_2,

                coalesce(sum(ACN_FORMA36_MAIN.SCREENWEIGHT),0) SCREENWEIGHT,
                coalesce(sum(ACN_FORMA36_MAIN.SCREENWEIGHT_2),0) SCREENWEIGHT_2,

                case
                  when (not (ACN_FORMA36_MAIN.OPERATIONID in (22))) then
                    coalesce(sum(ACN_FORMA36_MAIN.WEIGHT),0)
                end WEIGHT,

                0 REST_WEIGHT,
                0 REST_HUM_C,
                0 REST_DIRT_C

              from ACN_FORMA36_MAIN(:BEGINDATE, :ENDDATE, :PARTYID, :CROPID, :CLASSID, :SORTID,
                                    :STORAGEID, :STORINGID, -1, :OWNERID, :CONTRACTID, -1, 1, 0, 0,
                                    :SHOWRENEWALS, :SHOWRENEWALSINARRIVAL, 0, 0, :SHOWTRANSFERINEXPENSE)
              where (ACN_FORMA36_MAIN.OWNERFULLNAME is not null)
            group by 2, 1, 3, 4
            union
              select
                  CLIENTS.CLIENTFULLNAME CLIENTFULLNAME,
                  10000,
                  null,
                  10000,

                  (current_date - 3650) ARRIVAL_INVENTORY_DATE,
                  0 ARRIVAL_INVENTORY_NET,
                  0 ARRIVAL_INVENTORY_CHUM,
                  0 ARRIVAL_INVENTORY_CDIRT,

                  current_date ARRIVAL_MINDATE,
                  (current_date - 3650) ARRIVAL_MAXDATE,
                  0 ARRIVAL_NET,
                  0 ARRIVAL_CHUM,
                  0 ARRIVAL_CDIRT,

                  current_date ARRIVAL_RENEWAL_MINDATE,
                  (current_date - 3650) ARRIVAL_RENEWAL_MAXDATE,
                  0 ARRIVAL_RENEWAL_NET,
                  0 ARRIVAL_RENEWAL_CHUM,
                  0 ARRIVAL_RENEWAL_CDIRT,

                  current_date EXPENSE_MINDATE,
                  (current_date - 3650) EXPENSE_MAXDATE,
                  0 EXPENSE_NET,
                  0 EXPENSE_CHUM,
                  0 EXPENSE_CHUM_2,
                  0 EXPENSE_CDIRT,
                  0 EXPENSE_CDIRT_2,

                  current_date EXPENSE_RENEWAL_MINDATE,
                  (current_date - 3650) EXPENSE_RENEWAL_MAXDATE,
                  0 EXPENSE_RENEWAL_NET,
                  0 EXPENSE_RENEWAL_CHUM,
                  0 EXPENSE_RENEWAL_CDIRT,

                  0 WASTE1_NET,
                  0 WASTE1_HUM,
                  0 WASTE1_DIRT,
                  0 WASTE1_CHUM,
                  0 WASTE1_CDIRT,
                  0 WASTE1_CHUM_2,
                  0 WASTE1_CDIRT_2,

                  0 WASTE3_NET,
                  0 WASTE3_HUM,
                  0 WASTE3_DIRT,
                  0 WASTE3_CHUM,
                  0 WASTE3_CDIRT,
                  0 WASTE3_CHUM_2,
                  0 WASTE3_CDIRT_2,

                  0 WASTE_NET,
                  0 WASTE_HUM,
                  0 WASTE_DIRT,
                  0 WASTE_CHUM,
                  0 WASTE_CDIRT,
                  0 WASTE_CHUM_2,
                  0 WASTE_CDIRT_2,

                  (current_date - 3650) EXPENSE_INVENTORY_DATE,
                  0 EXPENSE_INVENTORY_NET,
                  0 EXPENSE_INVENTORY_CHUM,
                  0 EXPENSE_INVENTORY_CDIRT,

                  0 HUMWEIGHT,

                  0 DIRTWEIGHT,
                  0 DIRTWEIGHT_2,

                  0 SCREENWEIGHT,
                  0 SCREENWEIGHT_2,

                  0 WEIGHT,

                  sum(STORAGETICKETS.WEIGHT) REST_WEIGHT,
                  sum(case
                        when ((QUALITY_INVOICEREGISTERS.HUMIDITY is null) or (QUALITY_INVOICEREGISTERS.HUMIDITY > QUALITY_SELECT.HUMIDITY)) then
                          cast(STORAGETICKETS.WEIGHT * cast(QUALITY_SELECT.HUMIDITY as numeric (3,1)) / 100 as integer)
                        else cast(STORAGETICKETS.WEIGHT * cast(QUALITY_INVOICEREGISTERS.HUMIDITY as numeric (3,1)) / 100 as integer)
                  end) REST_HUMIDITY_C,
                  sum(case
                        when ((QUALITY_INVOICEREGISTERS.DIRT is null) or (QUALITY_INVOICEREGISTERS.DIRT > QUALITY_SELECT.DIRT)) then
                          cast(STORAGETICKETS.WEIGHT * cast(QUALITY_SELECT.DIRT as numeric (3,1)) / 100 as integer)
                        else cast(STORAGETICKETS.WEIGHT * cast(QUALITY_INVOICEREGISTERS.DIRT as numeric (3,1)) / 100 as integer)
                  end) REST_DIRT_C
                from STORAGETICKETS
/*
              from ACN_FORMA36_MAIN(:BEGINDATE, :ENDDATE, :PARTYID, :CROPID, :CLASSID, :SORTID,
                                    :STORAGEID, :STORINGID, -1, :OWNERID, :CONTRACTID, -1, 1, 0, 0,
                                    :SHOWRENEWALS, :SHOWRENEWALSINARRIVAL,0,0)
*/
                  left join CLIENTS on (CLIENTS.CLIENTID = :OWNERID)
                  left join QUALITY_SELECT(STORAGETICKETS.QUALITYID) on (0=0)
                  left join INVOICEREGISTERS on (INVOICEREGISTERS.STORAGETICKETID = STORAGETICKETS.STORAGETICKETID)
                  left join QUALITY_SELECT(INVOICEREGISTERS.QUALITYID) QUALITY_INVOICEREGISTERS on (0=0)
                where (STORAGETICKETS.REGDATE between :BEGINDATE and :ENDDATE)
                  and ((STORAGETICKETS.CANCELDATE is null) or (STORAGETICKETS.CANCELDATE >= :ENDDATE))
                  and ((STORAGETICKETS.STORAGEID = :STORAGEID) or (coalesce(:STORAGEID,-1) = -1))
                  and (STORAGETICKETS.OWNERID = :OWNERID) and (STORAGETICKETS.CONTRACTID = :CONTRACTID)
                  and (STORAGETICKETS.STATE = 0) and (INVOICEREGISTERS.INVOICEREGISTERID is not null)
                  and ((QUALITY_SELECT.CROPID = :CROPID) and ((QUALITY_SELECT.CLASSID = :CLASSID) or (coalesce(:CLASSID,-1) = -1)))
                  and ((QUALITY_SELECT.SORTID = :SORTID) or (coalesce(:SORTID,-1) = -1))
              group by 1
          )
        group by 1
      )
    group by 1
    into :CLIENTFULLNAME,
        :ARRIVAL_INVENTORY_DATE, :ARRIVAL_INVENTORY_NET,
        :ARRIVAL_INVENTORY_HUM, :ARRIVAL_INVENTORY_CHUM,
        :ARRIVAL_INVENTORY_DIRT, :ARRIVAL_INVENTORY_CDIRT,
        :ARRIVAL_MINDATE, :ARRIVAL_MAXDATE, :ARRIVAL_NET,
        :ARRIVAL_HUM, :ARRIVAL_CHUM,
        :ARRIVAL_DIRT, :ARRIVAL_CDIRT,
        :ARRIVAL_RENEWAL_MINDATE, :ARRIVAL_RENEWAL_MAXDATE, :ARRIVAL_RENEWAL_NET,
        :ARRIVAL_RENEWAL_HUM, :ARRIVAL_RENEWAL_CHUM,
        :ARRIVAL_RENEWAL_DIRT, :ARRIVAL_RENEWAL_CDIRT,
        :EXPENSE_MINDATE, :EXPENSE_MAXDATE, :EXPENSE_NET,
        :EXPENSE_HUM, :EXPENSE_CHUM,
        :EXPENSE_DIRT, :EXPENSE_CDIRT,
        :EXPENSE_RENEWAL_MINDATE, :EXPENSE_RENEWAL_MAXDATE, :EXPENSE_RENEWAL_NET,
        :EXPENSE_RENEWAL_HUM, :EXPENSE_RENEWAL_CHUM,
        :EXPENSE_RENEWAL_DIRT, :EXPENSE_RENEWAL_CDIRT,
        :WASTE1_NET,
        :WASTE1_HUM, :WASTE1_CHUM, :WASTE1_CHUM_2,
        :WASTE1_DIRT, :WASTE1_CDIRT, :WASTE1_CDIRT_2,
        :WASTE3_NET,
        :WASTE3_HUM, :WASTE3_CHUM, :WASTE3_CHUM_2,
        :WASTE3_DIRT, :WASTE3_CDIRT, :WASTE3_CDIRT_2,
        :WASTE_NET,
        :WASTE_HUM, :WASTE_CHUM, :WASTE_CHUM_2,
        :WASTE_DIRT, :WASTE_CDIRT, :WASTE_CDIRT_2,
        :EXPENSE_INVENTORY_DATE, :EXPENSE_INVENTORY_NET,
        :EXPENSE_INVENTORY_HUM, :EXPENSE_INVENTORY_CHUM,
        :EXPENSE_INVENTORY_DIRT, :EXPENSE_INVENTORY_CDIRT,
        :HUMWEIGHT,
        :DIRTWEIGHT, :DIRTWEIGHT_2,
        :SCREENWEIGHT, :SCREENWEIGHT_2,
        :WEIGHT,
        :REST_WEIGHT, :REST_HUM, :REST_HUM_C, :REST_DIRT, :REST_DIRT_C

  do begin
    ARRIVAL_TOTAL_NET = :ARRIVAL_INVENTORY_NET + :ARRIVAL_NET + :ARRIVAL_RENEWAL_NET;
    ARRIVAL_TOTAL_CHUM = :ARRIVAL_INVENTORY_CHUM + :ARRIVAL_CHUM + :ARRIVAL_RENEWAL_CHUM;
    ARRIVAL_TOTAL_CDIRT = :ARRIVAL_INVENTORY_CDIRT + :ARRIVAL_CDIRT + :ARRIVAL_RENEWAL_CDIRT;

    if (:ARRIVAL_TOTAL_NET <> 0) then begin
      ARRIVAL_TOTAL_HUM = ARRIVAL_TOTAL_CHUM * 100 / :ARRIVAL_TOTAL_NET;
      ARRIVAL_TOTAL_DIRT = ARRIVAL_TOTAL_CDIRT * 100 / :ARRIVAL_TOTAL_NET;
    end

    ARRIVAL_TOTAL_NET_2 = :ARRIVAL_INVENTORY_NET + :ARRIVAL_NET;
    ARRIVAL_TOTAL_CHUM_2 = :ARRIVAL_INVENTORY_CHUM + :ARRIVAL_CHUM;
    ARRIVAL_TOTAL_CDIRT_2 = :ARRIVAL_INVENTORY_CDIRT + :ARRIVAL_CDIRT;

    if (:ARRIVAL_TOTAL_NET_2 <> 0) then begin
      ARRIVAL_TOTAL_HUM_2 = cast(ARRIVAL_TOTAL_CHUM_2 * 100 as numeric(15,4)) / :ARRIVAL_TOTAL_NET_2;
      ARRIVAL_TOTAL_DIRT_2 = cast(ARRIVAL_TOTAL_CDIRT_2 * 100 as numeric(15,4)) / :ARRIVAL_TOTAL_NET_2;
    end

    EXPENSE_TOTAL_NET = :EXPENSE_NET + :WASTE_NET + :EXPENSE_RENEWAL_NET + :EXPENSE_INVENTORY_NET;
    EXPENSE_TOTAL_CHUM = :EXPENSE_CHUM + :WASTE_CHUM + :EXPENSE_RENEWAL_CHUM + :EXPENSE_INVENTORY_CHUM;
    EXPENSE_TOTAL_CDIRT = :EXPENSE_CDIRT + :WASTE_CDIRT + :EXPENSE_RENEWAL_CDIRT + :EXPENSE_INVENTORY_CDIRT;

    if (:EXPENSE_TOTAL_NET <> 0) then begin
      EXPENSE_TOTAL_HUM = cast(EXPENSE_TOTAL_CHUM * 100 as numeric(15,4)) / :EXPENSE_TOTAL_NET;
      EXPENSE_TOTAL_DIRT = cast(EXPENSE_TOTAL_CDIRT * 100 as numeric(15,4)) / :EXPENSE_TOTAL_NET;
    end

    EXPENSE_TOTAL_NET_2 = :EXPENSE_NET + :WASTE_NET + :EXPENSE_INVENTORY_NET;
    EXPENSE_TOTAL_CHUM_2 = :EXPENSE_CHUM + :WASTE_CHUM + :EXPENSE_INVENTORY_CHUM;
    EXPENSE_TOTAL_CDIRT_2 = :EXPENSE_CDIRT + :WASTE_CDIRT + :EXPENSE_INVENTORY_CDIRT;

    if (:EXPENSE_TOTAL_NET_2 <> 0) then begin
      EXPENSE_TOTAL_HUM_2 = cast(EXPENSE_TOTAL_CHUM_2 * 100 as numeric(15,4)) / :EXPENSE_TOTAL_NET_2;
      EXPENSE_TOTAL_DIRT_2 = cast(EXPENSE_TOTAL_CDIRT_2 * 100 as numeric(15,4)) / :EXPENSE_TOTAL_NET_2;
    end
    
    if (:WASTE1_NET <> 0) then begin
      WASTE1_AVERAGE_HUM = cast((100 * WASTE1_CHUM_2 / WASTE1_NET) as numeric(4,2));

      if (WASTE1_HUM <> WASTE1_AVERAGE_HUM) then
        WASTE1_HUM = cast((100 * cast(WASTE1_CHUM as numeric(15,4)) / WASTE1_NET) as numeric(4,2));

      WASTE1_AVERAGE_DIRT = cast((100 * WASTE1_CDIRT_2 / WASTE1_NET) as numeric(4,2));

      if (WASTE1_DIRT <> WASTE1_AVERAGE_DIRT) then
        WASTE1_DIRT = cast((100 * cast(WASTE1_CDIRT as numeric(15,4)) / WASTE1_NET) as numeric(4,2));
    end

    if (:WASTE3_NET <> 0) then begin
      WASTE3_AVERAGE_HUM = cast((100 * WASTE3_CHUM_2 / WASTE3_NET) as numeric(4,2));

      if (WASTE3_HUM <> WASTE3_AVERAGE_HUM) then
        WASTE3_HUM = cast((100 * cast(WASTE3_CHUM as numeric(15,4)) / WASTE3_NET) as numeric(4,2));

      WASTE3_AVERAGE_DIRT = cast((100 * WASTE3_CDIRT_2 / WASTE3_NET) as numeric(4,2));

      if (WASTE3_DIRT <> WASTE3_AVERAGE_DIRT) then
        WASTE3_DIRT = cast((100 * cast(WASTE3_CDIRT as numeric(15,4)) / WASTE3_NET) as numeric(4,2));
    end

    if (:WASTE_NET <> 0) then begin
      WASTE_AVERAGE_HUM = cast((100 * WASTE_CHUM_2 / WASTE_NET) as numeric(4,2));

      if (WASTE_HUM <> WASTE_AVERAGE_HUM) then
        WASTE_HUM = cast((100 * cast(WASTE_CHUM as numeric(15,4)) / WASTE_NET) as numeric(4,2));

      WASTE_AVERAGE_DIRT = cast((100 * WASTE_CDIRT_2 / WASTE_NET) as numeric(4,2));

      if (WASTE_DIRT <> WASTE_AVERAGE_DIRT) then
        WASTE_DIRT = cast((100 * cast(WASTE_CDIRT as numeric(15,4)) / WASTE_NET) as numeric(4,2));
    end

    suspend;

  end

end

 

     Previous topic Chapter index Next topic