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

ACN_FORMA36_MAIN

 

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

Параметр

Тип

Описание

DATEBEG

DATE

 

DATEEND

DATE

 

PARTYID

INTEGER

 

CROPID

INTEGER

 

CLASSID

INTEGER

 

SORTID

INTEGER

 

STORAGEID

INTEGER

 

STORINGID

INTEGER

 

SILAGEID

INTEGER

 

CLIENTID

INTEGER

 

CONTRACTID

INTEGER

 

YEARCROP

INTEGER

 

CURTAILOWNERPROVIDERNAME

INTEGER

 

CURTAILWASTE

INTEGER

 

SHORTFORM

INTEGER

 

SHOWRENEWALS

INTEGER

 

SHOWRENEWALSINARRIVAL

INTEGER

 

PRELIMINARYSHRINKAGE

INTEGER

 

SHOWNUMBERRAILWAYCAR

INTEGER

 

SHOWTRANSFERINEXPENSE

INTEGER

 

 

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

Параметр

Тип

Описание

REGDATE

DATE

 

ANALIS_NUM

VARCHAR(10)

 

ANALIS_ID

INTEGER

 

NUMDOC

VARCHAR(50)

 

CLIENTFULLNAME

VARCHAR(150)

 

OWNERFULLNAME

VARCHAR(150)

 

PROVIDERNAME

VARCHAR(150)

 

HUMIDITY

NUMERIC(4,2)

 

DIRT

NUMERIC(4,2)

 

INCOMETARE

INTEGER

 

COMNET

INTEGER

 

HUMIDITY_C

INTEGER

 

HUMIDITY_C_2

NUMERIC(15,4)

 

DIRT_C

INTEGER

 

DIRT_C_2

NUMERIC(15,4)

 

OUTCOMETARE

INTEGER

 

EXPENSENET

INTEGER

 

HUMIDITY_C_EXP

INTEGER

 

HUMIDITY_C_EXP_2

NUMERIC(15,4)

 

DIRT_C_EXP

INTEGER

 

DIRT_C_EXP_2

NUMERIC(15,4)

 

HUMWEIGHT

INTEGER

 

HUMWEIGHT_2

INTEGER

 

OPERATIONID

INTEGER

 

OPERATION_ID

INTEGER

 

WASTECLASSID

INTEGER

 

DIRTWEIGHT

INTEGER

 

DIRTWEIGHT_2

INTEGER

 

SCREENWEIGHT

INTEGER

 

SCREENWEIGHT_2

INTEGER

 

WEIGHT

INTEGER

 

REST_NET

INTEGER

 

REST_HUMIDITY_C

INTEGER

 

REST_DIRT_C

INTEGER

 

 

Описание

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

 

Определение

CREATE PROCEDURE ACN_FORMA36_MAIN(
    DATEBEG DATE,
    DATEEND DATE,
    PARTYID INTEGER,
    CROPID INTEGER,
    CLASSID INTEGER,
    SORTID INTEGER,
    STORAGEID INTEGER,
    STORINGID INTEGER,
    SILAGEID INTEGER,
    CLIENTID INTEGER,
    CONTRACTID INTEGER,
    YEARCROP INTEGER,
    CURTAILOWNERPROVIDERNAME INTEGER,
    CURTAILWASTE INTEGER,
    SHORTFORM INTEGER,
    SHOWRENEWALS INTEGER,
    SHOWRENEWALSINARRIVAL INTEGER,
    PRELIMINARYSHRINKAGE INTEGER,
    SHOWNUMBERRAILWAYCAR INTEGER,
    SHOWTRANSFERINEXPENSE INTEGER)
RETURNS (
    REGDATE DATE,
    ANALIS_NUM VARCHAR(10),
    ANALIS_ID INTEGER,
    NUMDOC VARCHAR(50),
    CLIENTFULLNAME VARCHAR(150),
    OWNERFULLNAME VARCHAR(150),
    PROVIDERNAME VARCHAR(150),
    HUMIDITY NUMERIC(4,2),
    DIRT NUMERIC(4,2),
    INCOMETARE INTEGER,
    COMNET INTEGER,
    HUMIDITY_C INTEGER,
    HUMIDITY_C_2 NUMERIC(15,4),
    DIRT_C INTEGER,
    DIRT_C_2 NUMERIC(15,4),
    OUTCOMETARE INTEGER,
    EXPENSENET INTEGER,
    HUMIDITY_C_EXP INTEGER,
    HUMIDITY_C_EXP_2 NUMERIC(15,4),
    DIRT_C_EXP INTEGER,
    DIRT_C_EXP_2 NUMERIC(15,4),
    HUMWEIGHT INTEGER,
    HUMWEIGHT_2 INTEGER,
    OPERATIONID INTEGER,
    OPERATION_ID INTEGER,
    WASTECLASSID INTEGER,
    DIRTWEIGHT INTEGER,
    DIRTWEIGHT_2 INTEGER,
    SCREENWEIGHT INTEGER,
    SCREENWEIGHT_2 INTEGER,
    WEIGHT INTEGER,
    REST_NET INTEGER,
    REST_HUMIDITY_C INTEGER,
    REST_DIRT_C INTEGER)
AS
declare variable AREGDATE date;
declare variable AANALIS_NUM varchar(10);
declare variable AANALIS_ID integer;
declare variable ANUMDOC varchar(50);
declare variable ACLIENTFULLNAME varchar(150);
declare variable AOWNERFULLNAME varchar(150);
declare variable APROVIDERNAME varchar(150);
declare variable AINCOMETARE integer;
declare variable ACOMNET integer;
declare variable AHUMIDITY_C integer;
declare variable ADIRT_C integer;
declare variable AOUTCOMETARE integer;
declare variable AEXPENSENET integer;
declare variable AHUMIDITY_C_EXP integer;
declare variable AHUMIDITY_C_EXP_2 numeric(15,4);
declare variable ADIRT_C_EXP integer;
declare variable ADIRT_C_EXP_2 numeric(15,4);
declare variable AHUMWEIGHT integer;
declare variable AOPERATIONID integer;
declare variable AOPERATION_ID integer;
declare variable AHUMIDITY numeric(4,2);
declare variable AVERAGEHUMIDITY numeric(4,2);
declare variable ADIRT numeric(4,2);
declare variable AVERAGEDIRT numeric(4,2);
declare variable WASTEROWCOUNT integer = 0;
declare variable AREST_NET integer;
declare variable AREST_HUMIDITY_C integer;
declare variable AREST_DIRT_C integer;
declare variable AWASTECLASSID integer;
begin
  OUTCOMETARE = 0;
  EXPENSENET = 0;
  HUMIDITY_C_EXP = 0;
  HUMIDITY_C_EXP_2 = 0;
  DIRT_C_EXP = 0;
  DIRT_C_EXP_2 = 0;

  for
    select
        REGDATE,
        case
          when (:SHORTFORM = 1) then
            ''
          else ANALIS_NUM
        end ANALIS_NUM,
        case
          when (:SHORTFORM = 1) then
            0
          else ANALIS_ID
        end ANALIS_ID,
        NUMDOC NUMDOC,
        CLIENTFULLNAME CLIENTFULLNAME,
        min(OWNERFULLNAME) OWNERFULLNAME,
        min(PROVIDERNAME) PROVIDERNAME,
        sum(INCOMETARE) INCOMETARE,
        sum(COMNET) COMNET,
        sum(HUMIDITY_C) HUMIDITY_C,
        sum(DIRT_C) DIRT_C,
        sum(OUTCOMETARE) OUTCOMETARE,
        sum(EXPENSENET) EXPENSENET,
        sum(HUMIDITY_C_EXP) HUMIDITY_C_EXP,
        sum(HUMIDITY_C_EXP_2) HUMIDITY_C_EXP_2,
        sum(DIRT_C_EXP) DIRT_C_EXP,
        sum(DIRT_C_EXP_2) DIRT_C_EXP_2,
        sum(HUMWEIGHT) HUMWEIGHT,
        sum(HUMWEIGHT_2) HUMWEIGHT_2,
        OPERATIONID OPERATIONID,
        OPERATION_ID OPERATION_ID,
        WASTECLASSID WASTECLASSID,
        sum(HUMIDITY_C_2) HUMIDITY_C_2,
        sum(DIRT_C_2) DIRT_C_2,
        sum(DIRTWEIGHT) DIRTWEIGHT,
        sum(DIRTWEIGHT_2) DIRTWEIGHT_2,
        sum(SCREENWEIGHT) SCREENWEIGHT,
        sum(SCREENWEIGHT_2) SCREENWEIGHT_2,
        sum(WEIGHT) WEIGHT,
        sum(REST_NET) REST_NET,
        sum(REST_HUMIDITY_C) REST_HUMIDITY_C,
        sum(REST_DIRT_C) REST_DIRT_C
      from (
        select
            IT.REGDATE REGDATE,
            case
              when (QUALITY_SELECT.QUALITYNUMBER is null) then
                case
                  when (IT.OPERATIONID in (22)) then
                    'ср.зв.'
                  else ''
                end
             else cast(QUALITY_SELECT.QUALITYNUMBER as integer)
            end ANALIS_NUM,
            case
              when (QUALITY_SELECT.QUALITYNUMBER is null) then
                IT.INVENTORYID
              else IT.BUHQUALITYID
            end ANALIS_ID,
            case
              when not(IT.OPERATIONID in (45,46)) then
                case
                  when (ITTRANSPORT.TRANSPORTTYPEID = 3) then
                    SHIPS.SHIPNAME
                  when ((ITTRANSPORT.TRANSPORTTYPEID in (0,1)) and (IT.OPERATIONID = 5)) then
                    case
                      when (:SHORTFORM = 1) then
                        0
                      else INVOICES.INVOICENUMBER
                    end
                  when ((OPERATION.OPERATIONTYPE in (1,2,3,4,5,6)) or (OPERATION.OPERATIONSUBTYPE = 1)) then
                    INVOICES.INVOICENUMBER
                  else INVOICEREGISTERS.REGISTERNUMBER
                end
            end NUMDOC,
            case
              when (:CURTAILOWNERPROVIDERNAME = 0) then
                case
                  when (IT.OPERATIONID = 22) then
                    case
                      when (:CURTAILWASTE = 1) then
                        OPERATIONS_OPERATIONNAME.OPERATIONNAME
                      else FORM36ADATATEXT.OWNERPROVIDERNAME3
                    end
                  else
                    case
                      when ((:SHORTFORM = 0) and (:SHOWNUMBERRAILWAYCAR = 1) and (ITTRANSPORT.TRANSPORTTYPEID = 2)) then
                        cast('ЖД (' || ITTRANSPORT.TRANSPORTNUMBER || ') - ' ||
                             substring(FORM36ADATATEXT.OWNERPROVIDERNAME from 6 for char_length(FORM36ADATATEXT.OWNERPROVIDERNAME) - 5) as varchar(150))
                      else cast(FORM36ADATATEXT.OWNERPROVIDERNAME as varchar(150))
                    end
                end
              else
                case
                  when (IT.OPERATIONID = 22) then
                    case
                      when (:CURTAILWASTE = 1) then
                        OPERATIONS_OPERATIONNAME.OPERATIONNAME
                      else FORM36ADATATEXT.OWNERPROVIDERNAME3
                    end
                  else
                    case
                      when ((:SHORTFORM = 0) and (:SHOWNUMBERRAILWAYCAR = 1) and (ITTRANSPORT.TRANSPORTTYPEID = 2)) then
                        cast('ЖД (' || ITTRANSPORT.TRANSPORTNUMBER || ') - ' ||
                             substring(FORM36ADATATEXT.OWNERPROVIDERNAME3 from 6 for char_length(FORM36ADATATEXT.OWNERPROVIDERNAME3) - 5) as varchar(150))
                      else FORM36ADATATEXT.OWNERPROVIDERNAME3
                    end
                end
            end CLIENTFULLNAME,
            OWNER.CLIENTFULLNAME OWNERFULLNAME,
            min(case
                  when (:SHORTFORM = 1) then
                    case
                      when (IT.OPERATIONID = 22) then
                        case
                          when ((:CURTAILWASTE = 1) or (coalesce(:CLIENTID,-1) <> -1)) then
                            OPERATIONS_OPERATIONNAME.OPERATIONNAME
                          else FORM36ADATATEXT.OWNERPROVIDERNAME3
                        end
                      else
                        case
                          when (ITTRANSPORT.TRANSPORTTYPEID in (0,1)) then
                            'АМ'
                          when (ITTRANSPORT.TRANSPORTTYPEID = 2) then
                           'ЖД'
                          when (ITTRANSPORT.TRANSPORTTYPEID = 4) then
                           'К'
                          else FORM36ADATATEXT.OWNERPROVIDERNAME4
                        end
                    end
                  else
                    case
                      when ((:SHOWNUMBERRAILWAYCAR = 1) and (ITTRANSPORT.TRANSPORTTYPEID = 2)) then
                        cast('ЖД (' || ITTRANSPORT.TRANSPORTNUMBER || ') - ' ||
                             substring(FORM36ADATATEXT.OWNERPROVIDERNAME4 from 6 for char_length(FORM36ADATATEXT.OWNERPROVIDERNAME4) - 5) as varchar(150))
                      else FORM36ADATATEXT.OWNERPROVIDERNAME4
                    end
                end) PROVIDERNAME,
            case
              when (sum(FORM36ACOMING.OUTNET) > 0) then
                sum(IT.CARGOQUANTITY)
            end INCOMETARE,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                sum(FORM36ACOMING.OUTNET)
              else
                case
                  when (sum(FORM36ACOMING.OUTNET) <> 0) then
                    sum(FORM36ACOMING.OUTNET)
                  when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                    sum(-FORM36AEXPENSE.OUTNET)
                end
            end COMNET,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
              else
                case
                  when (sum(FORM36ACOMING.OUTNET) <> 0) then
                    cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
                  when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                    cast(sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
                end
            end HUMIDITY_C,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100 as integer)
              else
                case
                  when (sum(FORM36ACOMING.OUTNET) <> 0) then
                    cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100 as integer)
                  when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                    cast(sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (3,1))) / 100 as integer)
                end
            end DIRT_C,
            case
              when (sum(FORM36AEXPENSE.OUTNET) > 0) then
                sum(IT.CARGOQUANTITY)
            end OUTCOMETARE,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                sum(FORM36AEXPENSE.OUTNET)
            end EXPENSENET,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                case
                  when (IT.OPERATIONID = 22) then
                    cast(sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (4,1))) / 100 as integer)
                  else cast(sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (4,1))) / 100 as integer)
                end
            end HUMIDITY_C_EXP,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
/*
                case
                  when (IT.OPERATIONID in (5,22)) then
                    sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (4,2)) / 100)
                end
*/
              sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (4,2)) / 100)
            end HUMIDITY_C_EXP_2,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                case
                  when (IT.OPERATIONID = 22) then
                    cast(sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (4,1))) / 100  as integer)
                  else cast(sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (4,1))) / 100 as integer)
                end
            end DIRT_C_EXP,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
/*
                case
                  when (IT.OPERATIONID in (5,22)) then
                    sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (4,2)) / 100)
                end
*/
              sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (4,2)) / 100)
            end DIRT_C_EXP_2,
            case
              when (:PRELIMINARYSHRINKAGE = 2) then
                sum(ITTRANSPORT.HUMWEIGHT)
              else null
            end HUMWEIGHT,
            sum(ITTRANSPORT.HUMWEIGHT) HUMWEIGHT_2,
            IT.OPERATIONID OPERATIONID,
            case
              when (FORM36ACOMING.OUTNET <> 0) then
                IT.OPERATIONID
              when (FORM36AEXPENSE.OUTNET <> 0) then
                -IT.OPERATIONID
            end OPERATION_ID,
            OPERATIONS_OPERATIONNAME.CLASSID WASTECLASSID,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                case
                  when (sum(FORM36ACOMING.OUTNET) < 6000) then
                    sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (4,2)) / 100)
                  else sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100
                end
              else
                case
                  when (sum(FORM36ACOMING.OUTNET) <> 0) then
                    sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100
                  when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                    sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (3,1))) / 100
                end
            end HUMIDITY_C_2,
            case
              when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                case
                  when (sum(FORM36ACOMING.OUTNET) < 6000) then
                    sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (4,2)) / 100)
                  else sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100
                end
              else
                case
                  when (sum(FORM36ACOMING.OUTNET) <> 0) then
                    sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,2)) / 100)
                  when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                    sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (3,1))) / 100
                end
            end DIRT_C_2,
            sum(ITTRANSPORT.DIRTWEIGHT) DIRTWEIGHT,
            case
              when ((min(IT.UWORDERID) is null) or (min(UWACTS.UWACTDATE) > :DATEEND)) then
                sum(ITTRANSPORT.DIRTWEIGHT)
              else null
            end DIRTWEIGHT_2,
            sum(ITTRANSPORT.SCREENWEIGHT) SCREENWEIGHT,
            case
              when ((min(IT.UWORDERID) is null) or (min(UWACTS.UWACTDATE) > :DATEEND)) then
                sum(ITTRANSPORT.SCREENWEIGHT)
              else null
            end SCREENWEIGHT_2,
            sum(IT.WEIGHT) WEIGHT,
            case
              when ((coalesce(min(STORAGETICKETS.STATE),0) = 0) and (min(coalesce(STORAGETICKETS.CANCELDATE,:DATEEND)) >= :DATEEND)) then
                case
                  when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                    sum(FORM36ACOMING.OUTNET)
                  else
                    case
                      when (sum(FORM36ACOMING.OUTNET) <> 0) then
                        sum(FORM36ACOMING.OUTNET)
                      when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                        sum(-FORM36AEXPENSE.OUTNET)
                    end
                end
              else 0
            end REST_NET,
            case
              when ((coalesce(min(STORAGETICKETS.STATE),0) = 0) and (min(coalesce(STORAGETICKETS.CANCELDATE,:DATEEND)) >= :DATEEND)) then
                case
                  when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                    case
                      when ((min(FORM36ACOMING.OUTDIRT) > min(REGISTERSQUALITY.HUMIDITY)) and (coalesce(min(REGISTERSQUALITY.DIRT),0) <> 0)) then
                        cast(sum(FORM36ACOMING.OUTNET * cast(REGISTERSQUALITY.HUMIDITY as numeric (3,1))) / 100 as integer)
                      else cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
                    end
                  else
                    case
                      when (sum(FORM36ACOMING.OUTNET) <> 0) then
                        cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
                      when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                        cast(sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
                    end
                end
            end REST_HUMIDITY_C,
            case
              when ((coalesce(min(STORAGETICKETS.STATE),0) = 0) and (min(coalesce(STORAGETICKETS.CANCELDATE,:DATEEND)) >= :DATEEND)) then
                case
                  when ((:SHOWRENEWALSINARRIVAL = 0) or (IT.OPERATIONID <> 17)) then
                    case
                      when ((min(FORM36ACOMING.OUTDIRT) > min(REGISTERSQUALITY.DIRT)) and (coalesce(min(REGISTERSQUALITY.DIRT),0) <> 0)) then
                        cast(sum(FORM36ACOMING.OUTNET * cast(REGISTERSQUALITY.DIRT as numeric (3,1))) / 100 as integer)
                      else cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100 as integer)
                    end
                  else
                    case
                      when (sum(FORM36ACOMING.OUTNET) <> 0) then
                        cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100 as integer)
                      when (sum(FORM36AEXPENSE.OUTNET) <> 0) then
                        cast(sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (3,1))) / 100 as integer)
                    end
                end
            end REST_DIRT_C
          from IT
--            join GETWORKPARTIES(null, :DATEEND, :CROPID, :CLASSID, :STORAGEID) on (GETWORKPARTIES.PARTYID = IT.PARTYID)
            left join QUALITY_SELECT(IT.BUHQUALITYID) on (0=0)
            left join PARTIES on (PARTIES.PARTYID = IT.PARTYID)
            left join QUALITY on (QUALITY.QUALITYID = IT.SAMPLERQUALITYID)
            left join CLIENTS OWNER on (IT.OWNERID = OWNER.CLIENTID)
            left join CLIENTS PROVIDER on (PROVIDER.CLIENTID = IT.PROVIDERID)
            left join ITTRANSPORT on (ITTRANSPORT.INVENTORYID = IT.INVENTORYID)
            left join OPERATION on (OPERATION.OPERATIONID = IT.OPERATIONID)
            left join INVOICES on (INVOICES.INVOICEID = IT.INVOICEID)
            left join INVOICEREGISTERS on (INVOICEREGISTERS.INVOICEREGISTERID = IT.REGISTERID)
            left join QUALITY_SELECT (INVOICEREGISTERS.QUALITYID) REGISTERSQUALITY on (0=0)
            left join STATIONS on (STATIONS.STATIONID = INVOICES.STATIONID)
            left join OPERATIONS_OPERATIONNAME(IT.OPERATIONID, IT.OPERATIONPARAM) on (0=0)
            left join FORM36ADATATEXT(ITTRANSPORT.TRANSPORTNUMBER, STATIONS.STATIONNAME, OPERATIONS_OPERATIONNAME.OPERATIONNAME, IT.OPERATIONID, PROVIDER.CLIENTNAME, OWNER.CLIENTNAME, ITTRANSPORT.TRANSPORTTYPEID) on (0=0)
            left join FORM36AEXPENSE(IT.OPERATIONID, QUALITY_SELECT.DIRT, QUALITY_SELECT.HUMIDITY, IT.NET) on (0=0)
            left join FORM36ACOMING(QUALITY_SELECT.DIRT, QUALITY_SELECT.HUMIDITY, IT.OPERATIONID, IT.NET, QUALITY.CROPID) on (0=0)
            left join IT ITS on ((ITS.INVENTORYID = IT.RELATIONID) and (ITS.PARENTID = -1) and (ITS.STATE <> -1))
            left join SHIPS on (SHIPS.SHIPID = ITS.RELATIONID)
            left join ORDERLOADITEMS on (ORDERLOADITEMS.ORDERLOADITEMID = IT.ORDERLOADITEMID)
            left join ORDERLOAD on (ORDERLOAD.ORDERLOADID = ORDERLOADITEMS.ORDERLOADID)
            left join UWACTS on (UWACTS.UWORDERID = IT.UWORDERID)
            left join STORAGETICKETS on (STORAGETICKETS.STORAGETICKETID = INVOICEREGISTERS.STORAGETICKETID)
          where (IT.REGDATE between :DATEBEG and :DATEEND)
            and ((IT.OPERATIONID in (1,4,5,7,13,18,19,22,23,70,71,72))
                 or ((IT.OPERATIONID = 17) and (:SHOWRENEWALS = 1) and (IT.OPERATIONPARAM is null))
                 or ((IT.OPERATIONID in (45,46)) and (IT.OPERATIONPARAM is null)))
            and (IT.BUHQUALITYID is not null)
            and (((PARTIES.STATE = 1) and (:PARTYID = -100)) or (PARTIES.PARTYID = :PARTYID))
            and ((QUALITY.CROPID = :CROPID) and ((QUALITY.CLASSID = :CLASSID) or (coalesce(:CLASSID,-1) = -1)))
            and ((QUALITY.SORTID = :SORTID) or (coalesce(:SORTID,-1) = -1))
            and ((IT.STORAGEID = :STORAGEID) or (coalesce(:STORAGEID,-1) = -1))
            and ((IT.STORINGID = :STORINGID) or (coalesce(:STORINGID,-1) = -1))
            and ((IT.SILAGEID = :SILAGEID) or (coalesce(:SILAGEID,-1) = -1))
            and ((IT.NET is not null) and (IT.NET <> 0))
            and ((IT.STATE >= 0) and (IT.PARENTID <> -1))
            and ((IT.OWNERID = :CLIENTID) or (coalesce(:CLIENTID,-1) = -1))
            and ((IT.CONTRACTID = :CONTRACTID) or (coalesce(:CONTRACTID,-1) = -1))
            and (QUALITY_SELECT.HARVESTYEAR = coalesce(:YEARCROP,-1) or (coalesce(:YEARCROP,-1) = -1))
          group by IT.REGDATE, ANALIS_NUM, NUMDOC, CLIENTFULLNAME, OWNERFULLNAME, IT.OPERATIONID,
            OPERATION_ID, QUALITY.CLASSID, ANALIS_ID, OPERATIONS_OPERATIONNAME.CLASSID
/*
        union
          select
              IT.REGDATE REGDATE, cast(QUALITY_SELECT.QUALITYNUMBER as integer) ANALIS_NUM,
              IT.BUHQUALITYID ANALIS_ID, INVOICES.INVOICENUMBER NUMDOC,
              OPERATIONS_OPERATIONNAME.OPERATIONNAME || ' ' || OWNER.CLIENTFULLNAME CLIENTFULLNAME,
              OWNER.CLIENTFULLNAME OWNERFULLNAME,
              null PROVIDERNAME, null INCOMETARE, null COMNET, null HUMIDITY_C,
              null DIRT_C, null OUTCOMETARE, null EXPENSENET, null HUMIDITY_C_EXP,
              null HUMIDITY_C_EXP_2, null DIRT_C_EXP, null DIRT_C_EXP_2,
              sum(FORM36AEXPENSE.OUTNET) HUMWEIGHT, sum(FORM36AEXPENSE.OUTNET) HUMWEIGHT_2,
              IT.OPERATIONID OPERATIONID, min(OPERATIONS_OPERATIONNAME.CLASSID) WASTECLASSID,
              null, null, null, null, null, null, null, null, null, null, IT.OPERATIONID OPERATION_ID
            from IT
--              join GETWORKPARTIES(null, :DATEEND, :CROPID, :CLASSID, :STORAGEID) on (GETWORKPARTIES.PARTYID = IT.PARTYID)
              left join QUALITY_SELECT(IT.BUHQUALITYID) on (0=0)
              left join PARTIES on (PARTIES.PARTYID = IT.PARTYID)
              left join QUALITY on (QUALITY.QUALITYID = IT.SAMPLERQUALITYID)
              left join CLIENTS OWNER on (IT.OWNERID = OWNER.CLIENTID)
              left join CLIENTS PROVIDER on (PROVIDER.CLIENTID = IT.PROVIDERID)
              left join ITTRANSPORT on (ITTRANSPORT.INVENTORYID = IT.INVENTORYID)
              left join OPERATION on (OPERATION.OPERATIONID = IT.OPERATIONID)
              left join INVOICES on (INVOICES.INVOICEID = IT.INVOICEID)
              left join OPERATIONS_OPERATIONNAME(IT.OPERATIONID, IT.OPERATIONPARAM) on (0=0)
              left join FORM36AEXPENSE(IT.OPERATIONID, QUALITY_SELECT.DIRT, QUALITY_SELECT.HUMIDITY, IT.NET) on (0=0)
            where (IT.REGDATE between :DATEBEG and :DATEEND)
              and (((PARTIES.STATE = 1) and (:PARTYID = -100)) or (PARTIES.PARTYID = :PARTYID))
              and (IT.OPERATIONID in (65))
              and ((QUALITY.CROPID = :CROPID) and ((QUALITY.CLASSID = :CLASSID) or (coalesce(:CLASSID,-1) = -1)))
              and ((QUALITY.SORTID = :SORTID) or (coalesce(:SORTID,-1) = -1))
              and ((IT.STORAGEID = :STORAGEID) or (coalesce(:STORAGEID,-1) = -1))
              and ((IT.STORINGID = :STORINGID) or (coalesce(:STORINGID,-1) = -1))
              and ((IT.SILAGEID = :SILAGEID) or (coalesce(:SILAGEID,-1) = -1))
              and ((IT.NET is not null) and (IT.NET <> 0))
              and ((IT.STATE >= 0) and (IT.PARENTID <> -1))
              and ((IT.OWNERID = :CLIENTID) or (coalesce(:CLIENTID,-1) = -1))
              and ((IT.CONTRACTID = :CONTRACTID) or (coalesce(:CONTRACTID,-1) = -1))
              and (QUALITY_SELECT.HARVESTYEAR = coalesce(:YEARCROP,-1) or (coalesce(:YEARCROP,-1) = -1))
          group by IT.REGDATE, NUMDOC, ANALIS_NUM, CLIENTFULLNAME, OWNERFULLNAME, IT.OPERATIONID, QUALITY.CLASSID, ANALIS_ID
*/
        union
          select
              UWACTS.UWACTDATE, '' ANALIS_NUM,
              UWORDERS.BEFOREQUALITYID ANALIS_ID, UWACTS.UWACTN NUMDOC,
              'Акт очищення та сушіння' CLIENTFULLNAME, null,
              'Акт очищення та сушіння' PROVIDERNAME, null, null,
              null, null, null, null, null, null, null, null,
              sum(IT.NET), null, 31, 31, null, null, null, null,
              null, null, null, null, null, null, null
            from IT
              left join PARTIES on (PARTIES.PARTYID = IT.PARTYID)
              left join UWORDERS on (UWORDERS.UWORDERID = IT.UWORDERID)
              left join UWACTS on (UWACTS.UWORDERID = UWORDERS.UWORDERID)
              left join QUALITY on (QUALITY.QUALITYID = UWORDERS.BEFOREQUALITYID)
            where (:PRELIMINARYSHRINKAGE = 1)
              and (((PARTIES.STATE = 1) and (:PARTYID = -100)) or (PARTIES.PARTYID = :PARTYID))
              and ((UWORDERS.UWTYPEID in (3,34)) and ((IT.OPERATIONID = 9) and (IT.STATE = -999)))
              and (IT.REGDATE between :DATEBEG and :DATEEND) and (IT.STORAGEID = :STORAGEID)
              and ((IT.OWNERID = :CLIENTID) or (coalesce(:CLIENTID,-1) = -1))
              and ((IT.CONTRACTID = :CONTRACTID) or (coalesce(:CONTRACTID,-1) = -1))
              and ((IT.STORINGID = :STORINGID) or (coalesce(:STORINGID,-1) = -1))
              and ((QUALITY.CROPID = :CROPID) and ((QUALITY.CLASSID = :CLASSID) or (coalesce(:CLASSID,-1) = -1)))
              and ((QUALITY.SORTID = :SORTID) or (coalesce(:SORTID,-1) = -1))
          group by UWACTS.UWACTDATE, ANALIS_NUM, ANALIS_ID, NUMDOC
        union
          select
              UWACTS.UWACTDATE, '' ANALIS_NUM,
              min(UWORDERS.BEFOREQUALITYID) ANALIS_ID, min(UWACTS.UWACTN) NUMDOC,
              min(case
                    when (UWORDERS.UWTYPEID = 2) then
                      'Акт сушіння' || coalesce(' - ' || CLIENTS.CLIENTNAME,'')
                    else 'Акт сушіння без палива' || coalesce(' - ' || CLIENTS.CLIENTNAME,'')
                  end) CLIENTFULLNAME,
              null,
              min(case
                    when (UWORDERS.UWTYPEID = 2) then
                      'Акт сушіння'
                    else 'Акт сушіння без палива'
                  end) PROVIDERNAME,
              null, null, null, null, null, null, null, null, null, null, sum(IT.NET),
              null, 31, 31, null, null, null, null, null, null, null, null, null, null, null
            from IT
              left join PARTIES on (PARTIES.PARTYID = IT.PARTYID)
              left join CLIENTS on ((CLIENTS.CLIENTID = IT.OWNERID) and (coalesce(:CLIENTID,-1) > -1))
              left join UWORDERS on (UWORDERS.UWORDERID = IT.UWORDERID)
              left join UWACTS on (UWACTS.UWORDERID = IT.UWORDERID)
              left join QUALITY on (QUALITY.QUALITYID = UWORDERS.BEFOREQUALITYID)
            where (UWACTS.UWACTDATE between :DATEBEG and :DATEEND) --and (:PRELIMINARYSHRINKAGE = 1)
              and (((PARTIES.STATE = 1) and (:PARTYID = -100)) or (PARTIES.PARTYID = :PARTYID))
              and ((UWORDERS.UWTYPEID in (2,5,90)) and ((IT.OPERATIONID = 9) and (IT.STATE = -999)))
              and ((QUALITY.CROPID = :CROPID) or (coalesce(:CROPID,-1) = -1))
              and ((QUALITY.CLASSID = :CLASSID) or (coalesce(:CLASSID,-1) = -1))
              and ((QUALITY.SORTID = :SORTID) or (coalesce(:SORTID,-1) = -1))
              and ((UWORDERS.STORAGEID = :STORAGEID) or (coalesce(:STORAGEID,-1) = -1))
              and ((IT.OWNERID = :CLIENTID) or (coalesce(:CLIENTID,-1) = -1))
              and ((IT.CONTRACTID = :CONTRACTID) or (coalesce(:CONTRACTID,-1) = -1))
              and ((IT.STORINGID = :STORINGID) or (coalesce(:STORINGID,-1) = -1))
--              and (QUALITY_SELECT.HARVESTYEAR = coalesce(:YEARCROP,-1) or (coalesce(:YEARCROP,-1) = -1))
          group by UWACTS.UWACTDATE, UWACTS.UWACTN
        union
          select
              IT.REGDATE REGDATE,
              cast(QUALITY_SELECT.QUALITYNUMBER as integer) ANALIS_NUM,
              IT.BUHQUALITYID ANALIS_ID,
              INVOICES.INVOICENUMBER NUMDOC,
              case
                when (FORM36ACOMING.OUTNET <> 0) then
                  'Переведення з ' || CROP.CROPNAME || ' ' || CLASS.CLASSNAME || ' - ' || coalesce(CLIENTS.CLIENTFULLNAME,'')
                when (FORM36AEXPENSE.OUTNET <> 0) then
                  'Переведення до ' || CROP.CROPNAME || ' ' || CLASS.CLASSNAME || ' - ' || coalesce(CLIENTS.CLIENTFULLNAME,'')
              end CLIENTFULLNAME,
              CLIENTS.CLIENTFULLNAME OWNERFULLNAME,
              case
                when (FORM36ACOMING.OUTNET <> 0) then
                  'Переведення з ' || CROP.CROPNAME || ' ' || CLASS.CLASSNAME
                when (FORM36AEXPENSE.OUTNET <> 0) then
                  'Переведення до ' || CROP.CROPNAME || ' ' || CLASS.CLASSNAME
              end PROVIDERNAME,
              null,
              case
                when (sum(FORM36ACOMING.OUTNET) <> 0) then
                  sum(FORM36ACOMING.OUTNET)
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 0)) then
                  sum(-FORM36AEXPENSE.OUTNET)
              end COMNET,
              case
                when (sum(FORM36ACOMING.OUTNET) <> 0) then
                  cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 0)) then
                  cast(sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (3,1))) / 100 as integer)
              end HUMIDITY_C,
              case
                when (sum(FORM36ACOMING.OUTNET) <> 0) then
                  cast(sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100 as integer)
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 0)) then
                  cast(sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (3,1))) / 100 as integer)
              end DIRT_C,
              null,
              case
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                  sum(FORM36AEXPENSE.OUTNET)
              end EXPENSENET,
              case
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                  cast(sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (4,1))) / 100 as integer)
              end HUMIDITY_C_EXP,
              case
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                  sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (4,2)) / 100)
              end HUMIDITY_C_EXP_2,
              case
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                  cast(sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (4,1))) / 100  as integer)
              end DIRT_C_EXP,
              case
              when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                sum(FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (4,2)) / 100)
              end DIRT_C_EXP_2,
              null, null,
              case
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                  5
                else 1
              end OPERATIONID,
              case
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 1)) then
                  5
                else 1
              end OPERATION_ID,
              null,
              case
                when (sum(FORM36ACOMING.OUTNET) <> 0) then
                  sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTHUMIDITY as numeric (3,1))) / 100
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 0)) then
                  sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTHUMIDITY as numeric (3,1))) / 100
              end HUMIDITY_C_2,
              case
                when (sum(FORM36ACOMING.OUTNET) <> 0) then
                  sum(FORM36ACOMING.OUTNET * cast(FORM36ACOMING.OUTDIRT as numeric (3,1))) / 100
                when ((sum(FORM36AEXPENSE.OUTNET) <> 0) and (:SHOWTRANSFERINEXPENSE = 0)) then
                  sum(-FORM36AEXPENSE.OUTNET * cast(FORM36AEXPENSE.OUTDIRT as numeric (3,1))) / 100
              end DIRT_C_2,
              null, null, null, null, null, null, null, null
            from IT
--              join GETWORKPARTIES(null, :DATEEND, :CROPID, :CLASSID, :STORAGEID) on (GETWORKPARTIES.PARTYID = IT.PARTYID)
              left join QUALITY_SELECT(IT.BUHQUALITYID) on (0=0)
              left join PARTIES on (PARTIES.PARTYID = IT.PARTYID)
              left join QUALITY on (QUALITY.QUALITYID = IT.SAMPLERQUALITYID)
              left join CLIENTS on (IT.OWNERID = CLIENTS.CLIENTID)
              left join OPERATION on (OPERATION.OPERATIONID = IT.OPERATIONID)
              left join INVOICES on (INVOICES.INVOICEID = IT.INVOICEID)
              left join FORM36AEXPENSE(IT.OPERATIONID, QUALITY_SELECT.DIRT, QUALITY_SELECT.HUMIDITY, IT.NET) on (0=0)
              left join FORM36ACOMING(QUALITY_SELECT.DIRT, QUALITY_SELECT.HUMIDITY, IT.OPERATIONID, IT.NET, QUALITY.CROPID) on (0=0)
              left join IT IT2 on ((IT2.INVENTORYID <> IT.INVENTORYID) and (IT2.INVOICEID = IT.INVOICEID))
              left join QUALITY QUALITY2 on (QUALITY2.QUALITYID = IT2.SAMPLERQUALITYID)
              left join CLASS on (CLASS.CLASSID = QUALITY2.CLASSID)
              left join CROP on (CROP.CROPID = QUALITY2.CROPID)
            where (IT.REGDATE between :DATEBEG and :DATEEND)
              and (((PARTIES.STATE = 1) and (:PARTYID = -100)) or (PARTIES.PARTYID = :PARTYID))
              and ((IT.OPERATIONID = 17) and (IT.OPERATIONPARAM is not null))
              and ((QUALITY.CROPID = :CROPID) and ((QUALITY.CLASSID = :CLASSID) or (coalesce(:CLASSID,-1) = -1)))
              and ((QUALITY.SORTID = :SORTID) or (coalesce(:SORTID,-1) = -1))
              and ((IT.STORAGEID = :STORAGEID) or (coalesce(:STORAGEID,-1) = -1))
              and ((IT.STORINGID = :STORINGID) or (coalesce(:STORINGID,-1) = -1))
              and ((IT.NET is not null) and (IT.NET <> 0))
              and ((IT.STATE >= 0) and (IT.PARENTID <> -1))
              and ((IT.OWNERID = :CLIENTID) or (coalesce(:CLIENTID,-1) = -1))
              and ((IT.CONTRACTID = :CONTRACTID) or (coalesce(:CONTRACTID,-1) = -1))
              and (QUALITY_SELECT.HARVESTYEAR = coalesce(:YEARCROP,-1) or (coalesce(:YEARCROP,-1) = -1))
          group by IT.REGDATE, ANALIS_NUM, NUMDOC, CLIENTFULLNAME, OWNERFULLNAME, PROVIDERNAME, IT.OPERATIONID, QUALITY.CLASSID, ANALIS_ID
        union
          select
              '31.12.2099', null, null, null, null, null, null, null, null, null,
              null, null, null, null, null, null, null, null, null, 1000, 1000, null,
              null, null, null, null, null, null, null, null, null, null
            from rdb$database
        order by 1, 20
      )
    group by REGDATE, NUMDOC, CLIENTFULLNAME, ANALIS_NUM, ANALIS_ID, OPERATIONID, OPERATION_ID, WASTECLASSID
    order by REGDATE, OPERATIONID, NUMDOC, ANALIS_NUM
    into :AREGDATE, :AANALIS_NUM, :AANALIS_ID, :ANUMDOC, :ACLIENTFULLNAME, :AOWNERFULLNAME, :APROVIDERNAME,
      :AINCOMETARE, :ACOMNET, :AHUMIDITY_C, :ADIRT_C, :AOUTCOMETARE, :AEXPENSENET, :AHUMIDITY_C_EXP,
      :AHUMIDITY_C_EXP_2, :ADIRT_C_EXP, :ADIRT_C_EXP_2, :AHUMWEIGHT, :HUMWEIGHT_2,
      :AOPERATIONID, :AOPERATION_ID, :WASTECLASSID, :HUMIDITY_C_2, :DIRT_C_2, :DIRTWEIGHT, :DIRTWEIGHT_2,
      :SCREENWEIGHT, :SCREENWEIGHT_2, :WEIGHT, :AREST_NET, :AREST_HUMIDITY_C, :AREST_DIRT_C
  do begin

    if (:WASTEROWCOUNT = 0) then begin
      HUMIDITY = null;
      DIRT = null;
    end
    
    if (:AOPERATIONID = 22) then begin
      if ((:WASTEROWCOUNT > 0) and (NUMDOC <> :ANUMDOC)) then begin
        if (:EXPENSENET > 0) then begin
          AVERAGEHUMIDITY = :HUMIDITY_C_EXP_2 * 100 / :EXPENSENET;

          if (:HUMIDITY <> :AVERAGEHUMIDITY) then
            if (:EXPENSENET < 6000) then
              HUMIDITY = cast((:HUMIDITY_C_EXP_2 * 100) as numeric(15,2)) / :EXPENSENET;
            else HUMIDITY = cast((:HUMIDITY_C_EXP * 100) as numeric(15,2)) / :EXPENSENET;

          AVERAGEDIRT = :DIRT_C_EXP_2 * 100 / :EXPENSENET;

          if (:DIRT <> :AVERAGEDIRT) then
            if (:EXPENSENET < 6000) then
              DIRT = cast((:DIRT_C_EXP_2 * 100) as numeric(15,2)) / :EXPENSENET;
            else DIRT = cast((:DIRT_C_EXP * 100) as numeric(15,2)) / :EXPENSENET;

          if (:CURTAILWASTE = 1) then begin
            HUMIDITY_C_EXP = cast(:HUMIDITY as numeric(4,1)) * :EXPENSENET / 100;
            DIRT_C_EXP = cast(:DIRT as numeric(4,1)) * :EXPENSENET / 100;
          end
        end

        suspend;

        OUTCOMETARE = 0;
        EXPENSENET = 0;
        HUMIDITY_C_EXP = 0;
        HUMIDITY_C_EXP_2 = 0;
        DIRT_C_EXP = 0;
        DIRT_C_EXP_2 = 0;
        WASTEROWCOUNT = 0;
      end

      REGDATE = :AREGDATE;
      ANALIS_NUM = :AANALIS_NUM;
      ANALIS_ID = :AANALIS_ID;
      NUMDOC = :ANUMDOC;
      CLIENTFULLNAME = :ACLIENTFULLNAME;
      OWNERFULLNAME = :AOWNERFULLNAME;
      PROVIDERNAME = :APROVIDERNAME;
      INCOMETARE = :AINCOMETARE;
      COMNET = :ACOMNET;
      HUMIDITY_C = :AHUMIDITY_C;
      DIRT_C = :ADIRT_C;
      OUTCOMETARE = :OUTCOMETARE + coalesce(:AOUTCOMETARE,0);
      EXPENSENET = :EXPENSENET + :AEXPENSENET;
      HUMIDITY_C_EXP = :HUMIDITY_C_EXP + :AHUMIDITY_C_EXP;
      HUMIDITY_C_EXP_2 = :HUMIDITY_C_EXP_2 + :AHUMIDITY_C_EXP_2;
      DIRT_C_EXP = :DIRT_C_EXP + :ADIRT_C_EXP;
      DIRT_C_EXP_2 = :DIRT_C_EXP_2 + :ADIRT_C_EXP_2;
      HUMWEIGHT = :AHUMWEIGHT;
      OPERATIONID = :AOPERATIONID;
      OPERATION_ID = :AOPERATION_ID;
      REST_NET = :AREST_NET;
      REST_HUMIDITY_C = :AREST_HUMIDITY_C;
      REST_DIRT_C = :AREST_DIRT_C;

      if (:AEXPENSENET > 0) then begin
        AHUMIDITY = cast((:AHUMIDITY_C_EXP_2 * 100 / :AEXPENSENET) as numeric (4,2));
        ADIRT = cast((:ADIRT_C_EXP_2 * 100 / :AEXPENSENET) as numeric (4,2));
      end

      if (:CURTAILWASTE = 0) then begin
        HUMIDITY = :AHUMIDITY;
        DIRT = :ADIRT;

        suspend;

        OUTCOMETARE = 0;
        EXPENSENET = 0;
        HUMIDITY_C_EXP = 0;
        HUMIDITY_C_EXP_2 = 0;
        DIRT_C_EXP = 0;
        DIRT_C_EXP_2 = 0;
      end
      else begin
        if (coalesce(:HUMIDITY,0) < :AHUMIDITY) then
          HUMIDITY = :AHUMIDITY;

        if (coalesce(:DIRT,0) < :ADIRT) then
          DIRT = :ADIRT;

        AWASTECLASSID = WASTECLASSID;
        WASTEROWCOUNT = :WASTEROWCOUNT + 1;
      end
    end
    else begin
      if (:WASTEROWCOUNT > 0) then begin
        if (:EXPENSENET > 0) then begin
          AVERAGEHUMIDITY = :HUMIDITY_C_EXP_2 * 100 / :EXPENSENET;

          if (:HUMIDITY <> :AVERAGEHUMIDITY) then
            if (:EXPENSENET < 6000) then
              HUMIDITY = cast((cast((:HUMIDITY_C_EXP_2 * 100) as numeric(15,4)) / :EXPENSENET) as numeric(15,2));
            else HUMIDITY = cast((cast((:HUMIDITY_C_EXP * 100) as numeric(15,4)) / :EXPENSENET) as numeric(15,2));

          AVERAGEDIRT = :DIRT_C_EXP_2 * 100 / :EXPENSENET;

          if (:DIRT <> :AVERAGEDIRT) then
            if (:EXPENSENET < 6000) then
              DIRT = cast((cast((:DIRT_C_EXP_2 * 100) as numeric(15,4)) / :EXPENSENET) as numeric(4,2));
            else DIRT = cast((cast((:DIRT_C_EXP * 100) as numeric(15,4)) / :EXPENSENET) as numeric(4,2));

          if (:CURTAILWASTE = 1) then begin
            HUMIDITY_C_EXP = cast(:HUMIDITY as numeric(4,1)) * :EXPENSENET / 100;
            DIRT_C_EXP = cast(:DIRT as numeric(4,1)) * :EXPENSENET / 100;
          end
        end

        WASTECLASSID = AWASTECLASSID;

        suspend;

        OUTCOMETARE = 0;
        EXPENSENET = 0;
        HUMIDITY_C_EXP = 0;
        HUMIDITY_C_EXP_2 = 0;
        DIRT_C_EXP = 0;
        DIRT_C_EXP_2 = 0;
        WASTECLASSID = null;
        WASTEROWCOUNT = 0;
      end

      if (:AOPERATIONID < 1000) then begin
        REGDATE = :AREGDATE;
        ANALIS_NUM = :AANALIS_NUM;
        ANALIS_ID = :AANALIS_ID;
        NUMDOC = :ANUMDOC;
        CLIENTFULLNAME = :ACLIENTFULLNAME;
        OWNERFULLNAME = :AOWNERFULLNAME;
        PROVIDERNAME = :APROVIDERNAME;
        INCOMETARE = :AINCOMETARE;
        COMNET = :ACOMNET;
        HUMIDITY_C = :AHUMIDITY_C;
        DIRT_C = :ADIRT_C;
        OUTCOMETARE = :AOUTCOMETARE;
        EXPENSENET = :AEXPENSENET;

        if (:AOPERATIONID = 22) then begin
          HUMIDITY_C_EXP = 0;
          HUMIDITY_C_EXP_2 = 0;
          DIRT_C_EXP = 0;
          DIRT_C_EXP_2 = 0;
        end
        else begin
          HUMIDITY_C_EXP = :AHUMIDITY_C_EXP;
          DIRT_C_EXP = :ADIRT_C_EXP;
          HUMIDITY_C_EXP_2 = :AHUMIDITY_C_EXP_2;
          DIRT_C_EXP_2 = :ADIRT_C_EXP_2;
        end

        HUMWEIGHT = :AHUMWEIGHT;
        OPERATIONID = :AOPERATIONID;
        OPERATION_ID = :AOPERATION_ID;
        REST_NET = :AREST_NET;
        REST_HUMIDITY_C = :AREST_HUMIDITY_C;
        REST_DIRT_C = :AREST_DIRT_C;

        if (:ACOMNET <> 0) then begin
          if (:ACOMNET < 6000) then begin
            HUMIDITY = cast((cast((:HUMIDITY_C_2 * 100) as numeric(15,4)) / :ACOMNET) as numeric(4,2));
            DIRT = cast((cast((:DIRT_C_2 * 100) as numeric(15,4)) / :ACOMNET) as numeric(4,2));
          end
          else begin
            HUMIDITY = cast((cast((:AHUMIDITY_C * 100) as numeric(15,4)) / :ACOMNET) as numeric(4,2));
            DIRT = cast((cast((:ADIRT_C * 100) as numeric(15,4)) / :ACOMNET) as numeric(4,2));
          end
        end
        else
          if (:AEXPENSENET > 0) then begin
            if (:AEXPENSENET < 6000) then begin
              HUMIDITY = cast((cast((:AHUMIDITY_C_EXP_2 * 100) as numeric(15,4)) / :AEXPENSENET) as numeric(4,2));
              DIRT = cast((cast((:ADIRT_C_EXP_2 * 100) as numeric(15,4)) / :AEXPENSENET) as numeric(4,2));
            end
            else begin
              HUMIDITY = cast((cast((:AHUMIDITY_C_EXP * 100) as numeric(15,4)) / :AEXPENSENET) as numeric(4,2));
              DIRT = cast((cast((:ADIRT_C_EXP * 100) as numeric(15,4)) / :AEXPENSENET) as numeric(4,2));
            end
          end

        suspend;

        OUTCOMETARE = 0;
        EXPENSENET = 0;
        HUMIDITY_C_EXP = 0;
        HUMIDITY_C_EXP_2 = 0;
        DIRT_C_EXP = 0;
        DIRT_C_EXP_2 = 0;
        REST_NET = null;
        REST_HUMIDITY_C = null;
        REST_DIRT_C = null;
      end
    end

  end
end

 

     Previous topic Chapter index Next topic