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

UNDERWORK_CALCUWACT_REGISTER

 

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

Параметр

Тип

Описание

UWORDERDATE

DATE

 

UWACTDATE

DATE

 

UWORDERID

INTEGER

 

QUALITYBEFOREID

INTEGER

 

QUALITYAFTERID

INTEGER

 

CROPID

INTEGER

 

CLASSID

INTEGER

 

UWTYPEID

INTEGER

 

SORTID

INTEGER

 

CLIENTID

INTEGER

 

BEFOREDRYQUALITYID

INTEGER

 

AFTERDRYQUALITYID

INTEGER

 

FINALCALCULATION

INTEGER

 

CURRENTPARTY

INTEGER

 

 

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

Параметр

Тип

Описание

BEFOREWEIGHT

INTEGER

 

STORAGEID

INTEGER

 

 

Описание

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

 

Определение

CREATE PROCEDURE UNDERWORK_CALCUWACT_REGISTER(
    UWORDERDATE DATE,
    UWACTDATE DATE,
    UWORDERID INTEGER,
    QUALITYBEFOREID INTEGER,
    QUALITYAFTERID INTEGER,
    CROPID INTEGER,
    CLASSID INTEGER,
    UWTYPEID INTEGER,
    SORTID INTEGER,
    CLIENTID INTEGER,
    BEFOREDRYQUALITYID INTEGER,
    AFTERDRYQUALITYID INTEGER,
    FINALCALCULATION INTEGER,
    CURRENTPARTY INTEGER)
RETURNS (
    BEFOREWEIGHT INTEGER,
    STORAGEID INTEGER)
AS
declare variable RWEIGHT integer = 0;
declare variable DIRTBEFORE numeric(9,2);
declare variable DIRT_BEFORE numeric(12,4) = 0;
declare variable HUMIDITYBEFORE numeric(9,2);
declare variable HUMIDITY_BEFORE numeric(12,4) = 0;
declare variable SCREENBEFORE numeric(9,2) = 0;
declare variable SCREEN_BEFORE numeric(12,4) = 0;
declare variable GRAINUNITBEFORE integer;
declare variable GRAINUNIT_BEFORE integer;
declare variable SMALLGRAINBEFORE numeric(9,2);
declare variable SMALLGRAIN_BEFORE numeric(12,4) = 0;
declare variable DIRTAFTER numeric(9,2);
declare variable DIRT_AFTER numeric(12,4) = 0;
declare variable HUMIDITYAFTER numeric(9,2);
declare variable HUMIDITY_AFTER numeric(12,4) = 0;
declare variable SCREENAFTER numeric(9,2);
declare variable SCREEN_AFTER numeric(12,4) = 0;
declare variable GRAINUNITAFTER integer;
declare variable GRAINUNIT_AFTER integer;
declare variable SMALLGRAINAFTER numeric(9,2);
declare variable SMALLGRAIN_AFTER numeric(12,4) = 0;
declare variable ACLEANINGBEGINDATE date;
declare variable CLEANINGBEGINDATE date;
declare variable ACLEANINGENDDATE timestamp;
declare variable CLEANINGENDDATE date;
declare variable DRYING numeric(9,2) = 0;
declare variable CLEAN numeric(9,2) = 0;
declare variable ABEFOREWEIGHT integer;
declare variable METODSOLUTION integer;
declare variable WRITING_OFF integer;
declare variable SELECTEDWASTE integer;
declare variable AWEIGHT double precision;
declare variable ACROPID integer;
declare variable ACLASSID integer;
declare variable REGISTERID integer;
declare variable PARTYID integer;
declare variable W_FORAGE1C_A double precision = 0;
declare variable DIRT_FORAGE1C_A numeric(9,2) = 0;
declare variable W_FORAGE1C_B double precision = 0;
declare variable DIRT_FORAGE1C_B numeric(9,2) = 0;
declare variable W_FORAGE2C double precision = 0;
declare variable DIRT_FORAGE2C numeric(9,2) = 0;
declare variable W_FORAGE3C double precision = 0;
declare variable DIRT_FORAGE3C numeric(9,2) = 0;
declare variable W_BYPRODUCT_A double precision = 0;
declare variable DIRT_BYPRODUCT_A numeric(9,2) = 0;
declare variable W_BYPRODUCT_B double precision = 0;
declare variable DIRT_BYPRODUCT_B numeric(9,2) = 0;
declare variable W_BYPRODUCT_D double precision = 0;
declare variable DIRT_BYPRODUCT_D numeric(9,2) = 0;
declare variable W_SMALLGRAIN double precision = 0;
declare variable DIRT_SMALLGRAIN numeric(9,2) = 0;
declare variable FORAGE_QUALITYID integer;
declare variable FORAGE_IT_QUALITYID integer;
declare variable FORAGE_QUALITYNUMBER integer;
declare variable FORAGEDIRT numeric(9,2);
declare variable FORAGE_DIRT numeric(4,2);
declare variable FORAGESCREEN numeric(9,2);
declare variable FORAGE_SCREEN numeric(4,2);
declare variable FORAGEHUMIDITY numeric(9,2);
declare variable FORAGE_HUMIDITY numeric(4,2);
declare variable FORAGEGRAINUNIT integer;
declare variable FORAGE_GRAINUNIT integer;
declare variable FORAGESMALLGRAIN numeric(9,2);
declare variable FORAGE_SMALLGRAIN numeric(4,2);
declare variable NOTCLIENTID integer;
declare variable CLEANWEIGHT integer;
declare variable SCREENWEIGHT integer;
declare variable DIRTWEIGHT integer;
declare variable HUMWEIGHT integer;
declare variable INVOICE integer;
declare variable CONTRACTID integer;
declare variable INVID integer;
declare variable WASTEQUALITYID integer;
declare variable CROPTYPEID integer;
declare variable BEFORECLEANINGWEIGHT integer = 0;
declare variable AFTERCLEANINGWEIGHT integer = 0;
declare variable BEFOREDRYINGWEIGHT integer = 0;
declare variable AFTERDRYINGWEIGHT integer = 0;
declare variable DRYINGRBEGINDATE date;
declare variable ADRYINGRBEGINDATE date;
declare variable HUMIDITY_DECREASE numeric(9,2);
declare variable DRYSTORAGEID integer;
declare variable SETTLEMENTDIRTWEIGHT integer;
declare variable SETTLEMENTSCREENWEIGHT integer;
declare variable WASTEWEIGHT integer;
declare variable NET integer;
declare variable NET_DIRT integer;
declare variable NET_HUM integer;
declare variable OWNERID integer;
declare variable STORINGID integer;
declare variable SUMDIRTWEIGHT integer = 0;
declare variable WEIGHTBEFORECLEANING double precision = 0;
declare variable WEIGHTAFTERCLEANING double precision = 0;
declare variable SUMHUMWEIGHT integer = 0;
declare variable WEIGHTBEFOREDRYING double precision = 0;
declare variable DRYINGWEIGHT double precision = 0;
declare variable SETTLEMENTSHRINKAGEWEIGHT integer;
declare variable BEFORECLEANINGQUALITYID integer;
declare variable QUALITYNUMBERBEFORECLEAN integer;
declare variable BEFORECLEANHUMIDITY numeric(9,2);
declare variable BEFORECLEAN_HUMIDITY numeric(12,4) = 0;
declare variable HUMIDITYBEFORECLEAN numeric(9,2);
declare variable GRAINUNITBEFORECLEAN integer;
declare variable DIRTBEFORECLEAN numeric(12,4);
declare variable SCREENBEFORECLEAN numeric(9,2);
declare variable SMALLGRAINBEFORECLEAN numeric(9,2);
declare variable AFTERCLEANINGQUALITYID integer;
declare variable QUALITYNUMBERAFTERCLEAN integer;
declare variable AFTERCLEANHUMIDITY numeric(9,2);
declare variable AFTERCLEAN_HUMIDITY numeric(12,4) = 0;
declare variable HUMIDITYAFTERCLEAN numeric(9,2);
declare variable GRAINUNITAFTERCLEAN integer;
declare variable DIRTAFTERCLEAN numeric(9,2);
declare variable SCREENAFTERCLEAN numeric(9,2);
declare variable SMALLGRAINAFTERCLEAN numeric(9,2);
declare variable BEFOREDRYINGQUALITYID integer;
declare variable QUALITYNUMBERBEFOREDRY integer;
declare variable HUMIDITYBEFOREDRY numeric(9,2);
declare variable GRAINUNITBEFOREDRY integer;
declare variable BEFOREDRYDIRT numeric(9,2);
declare variable BEFOREDRY_DIRT numeric(12,4) = 0;
declare variable DIRTBEFOREDRY numeric(9,2);
declare variable BEFOREDRYSCREEN numeric(9,2);
declare variable BEFOREDRY_SCREEN numeric(12,4) = 0;
declare variable SCREENBEFOREDRY numeric(9,2);
declare variable SMALLGRAINBEFOREDRY numeric(9,2);
declare variable AFTERDRYINGQUALITYID integer;
declare variable QUALITYNUMBERAFTERDRY integer;
declare variable HUMIDITYAFTERDRY numeric(9,2);
declare variable GRAINUNITAFTERDRY integer;
declare variable AFTERDRYDIRT numeric(9,2);
declare variable AFTERDRY_DIRT numeric(12,4) = 0;
declare variable DIRTAFTERDRY numeric(9,2);
declare variable AFTERDRYSCREEN numeric(9,2);
declare variable AFTERDRY_SCREEN numeric(12,4) = 0;
declare variable SCREENAFTERDRY numeric(9,2);
declare variable SMALLGRAINAFTERDRY numeric(9,2);
declare variable ALLWEIGHTBEFORECLEANING double precision = 0;
declare variable ALLDIRTWEIGHT integer = 0;
declare variable ALLSCREENWEIGHT integer = 0;
declare variable ALLWEIGHTBEFOREDRYING double precision = 0;
declare variable SHRINKAGEWEIGHT integer;
declare variable DEFAULTVALUE numeric(4,2) = 0;
declare variable AOWNERID integer;
declare variable ADIRT_BEFORE numeric(12,4);
declare variable ADIRT_AFTER numeric(12,4);
declare variable AWEIGHTBEFORECLEANING integer;
declare variable ADIRTBEFORE numeric(9,2);
declare variable ADIRTAFTER numeric(9,2);
declare variable ADIRTWEIGHT integer = 0;
declare variable ASCREENBEFORE numeric(9,2);
declare variable ASCREENAFTER numeric(9,2);
declare variable ASCREEN_BEFORE numeric(12,4);
declare variable ASCREEN_AFTER numeric(12,4);
declare variable ASCREENWEIGHT integer = 0;
declare variable HOWNERID integer;
declare variable AHUMIDITY_BEFORE numeric(12,4);
declare variable AHUMIDITY_AFTER numeric(12,4);
declare variable AWEIGHTBEFOREDRYING integer;
declare variable A_WEIGHTBEFOREDRYING integer;
declare variable AHUMIDITYBEFORE numeric(9,2);
declare variable AHUMIDITYAFTER numeric(9,2);
declare variable AHUMWEIGHT integer = 0;
declare variable A_BEFOREDRYINGWEIGHT integer = 0;
declare variable A_HUMIDITY_BEFORE numeric(15,4) = 0;
declare variable INVENTORYID integer;
declare variable AINVENTORYID integer;
declare variable COEFFICIENT numeric(9,8);
declare variable DELTAWEIGHT integer;
declare variable GROSS integer;
declare variable TARE integer;
declare variable WEIGHT integer;
declare variable ASUMHUMWEIGHT integer;
begin
  select
      coalesce(METODSOLUTION,0), SELECTEDWASTE, coalesce(WRITING_OFF,0)
    from UWTYPES
    where (UWTYPEID = :UWTYPEID)
  into :METODSOLUTION, :SELECTEDWASTE, :WRITING_OFF;

  BEFOREWEIGHT = 0;
  ALLDIRTWEIGHT = 0;
  DRYINGWEIGHT = 0;
  CLEANINGBEGINDATE = null;
  CLEANINGENDDATE = null;
  DRYINGRBEGINDATE = null;

  if (:FINALCALCULATION = 1) then begin
    delete from IT
      where (IT.OPERATIONID in (4,9)) and (IT.UWORDERID = :UWORDERID) and (IT.STATE = -999);

    select HUMIDITY, GRAINUNIT, DIRT, SCREENING, QUALITYNUMBER
      from QUALITY_SELECT(:QUALITYBEFOREID)
    into :HUMIDITYBEFORECLEAN, :GRAINUNITBEFORECLEAN, :DIRTBEFORECLEAN, :SCREENBEFORECLEAN, :QUALITYNUMBERBEFORECLEAN;

    select HUMIDITY, GRAINUNIT, DIRT, SCREENING, QUALITYNUMBER
      from QUALITY_SELECT(:QUALITYAFTERID)
    into :HUMIDITYAFTERCLEAN, :GRAINUNITAFTERCLEAN, :DIRTAFTERCLEAN, :SCREENAFTERCLEAN, :QUALITYNUMBERAFTERCLEAN;

    select HUMIDITY, GRAINUNIT, DIRT, SCREENING, QUALITYNUMBER
      from QUALITY_SELECT(:BEFOREDRYQUALITYID)
    into :HUMIDITYBEFOREDRY, :GRAINUNITBEFOREDRY, :DIRTBEFOREDRY, :SCREENBEFOREDRY, :QUALITYNUMBERBEFOREDRY;

    select HUMIDITY, GRAINUNIT, DIRT, SCREENING, QUALITYNUMBER
      from QUALITY_SELECT(:AFTERDRYQUALITYID)
    into :HUMIDITYAFTERDRY, :GRAINUNITAFTERDRY, :DIRTAFTERDRY, :SCREENAFTERDRY, :QUALITYNUMBERAFTERDRY;
  end
  else begin
    select QUALITYNUMBER
      from QUALITY_SELECT(:QUALITYBEFOREID)
    into :QUALITYNUMBERBEFORECLEAN;

    select QUALITYNUMBER
      from QUALITY_SELECT(:QUALITYAFTERID)
    into :QUALITYNUMBERAFTERCLEAN;

    select QUALITYNUMBER
      from QUALITY_SELECT(:BEFOREDRYQUALITYID)
    into :QUALITYNUMBERBEFOREDRY;

    select QUALITYNUMBER
      from QUALITY_SELECT(:AFTERDRYQUALITYID)
    into :QUALITYNUMBERAFTERDRY;

    if (:METODSOLUTION in (43,143,45,145,47,147,48,148)) then
      select
          cast(cast(100 * sum(cast(NET * cast(HUMIDITY as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)),
          cast(cast(100 * sum(cast(NET * DIRT / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2))
        from (
          select
              sum(IT.NET) NET, IT.REGISTERID REGISTERID, IT.UWORDERID UWORDERID,
              min(QUALITY_SELECT.HUMIDITY) HUMIDITY, min(QUALITY_SELECT.DIRT) DIRT
            from IT
              left join INVOICEREGISTERS on (INVOICEREGISTERS.INVOICEREGISTERID = IT.REGISTERID)
              left join QUALITY_SELECT(INVOICEREGISTERS.QUALITYID) on (0=0)
              left join ITTRANSPORT on (ITTRANSPORT.INVENTORYID = IT.INVENTORYID)
            where (IT.UWORDERID = :UWORDERID)
              and ((ITTRANSPORT.DIRTWEIGHT > 0) or (ITTRANSPORT.HUMWEIGHT > 0))
              and (IT.REGDATE between
                    (case
                       when (:METODSOLUTION >= 100) then
                         (coalesce ((select min(GWP.BEGINDATE)
                                       from GETWORKPARTIES(:CURRENTPARTY, :UWORDERDATE, :CROPID, :CLASSID, IT.STORAGEID) GWP
                                    ),:UWORDERDATE))
                       else :UWORDERDATE
                     end)
                   and :UWACTDATE)
              and (INVOICEREGISTERS.INVOICEREGISTERID is not null)
              and ((IT.OPERATIONID = 1) and (IT.STATE > -1) and (IT.PARENTID <= 0))
          group by IT.UWORDERID, IT.REGISTERID
          order by IT.REGISTERID
        )
      group by UWORDERID
      into :HUMIDITYAFTERDRY, :DIRTAFTERCLEAN;
  end

  select coalesce(coalesce(GETCONTRACTCOSTS.HUMB,GET_CONTRACT_COSTS.HUMB),0)
    from GET_CONTRACT_COSTS(:UWACTDATE, null, 3, :CROPID, :CLASSID)
      left join GET_CONTRACT_COSTS(:UWACTDATE, null, 22, :CROPID, :CLASSID) GETCONTRACTCOSTS on (0=0)
  into :DEFAULTVALUE;


  select UNDERWORK_CALCUWACT_PREV.ALLWEIGHTBEFORECLEANING, UNDERWORK_CALCUWACT_PREV.SETTLEMENTWASTEWEIGHT,
      UNDERWORK_CALCUWACT_PREV.WASTEWEIGHT, UNDERWORK_CALCUWACT_PREV.ALLWEIGHTBEFOREDRYING,
      UNDERWORK_CALCUWACT_PREV.SHRINKAGEWEIGHT, UNDERWORK_CALCUWACT_PREV.SETTLEMENTSHRINKAGEWEIGHT
    from UNDERWORK_CALCUWACT_PREV(:METODSOLUTION, :UWORDERID, :CROPID, :DEFAULTVALUE, :HUMIDITYAFTERDRY, :DIRTAFTERCLEAN)
  into :ALLWEIGHTBEFORECLEANING, :SETTLEMENTDIRTWEIGHT, :WASTEWEIGHT,
       :ALLWEIGHTBEFOREDRYING, :SHRINKAGEWEIGHT, :SETTLEMENTSHRINKAGEWEIGHT;


  for
    select
        sum(IT.NET) NET, min(IT.STORAGEID) STORAGEID,
        min(coalesce((case
                        when ((ITTRANSPORT.DIRTWEIGHT > 0) or (ITTRANSPORT.SCREENWEIGHT > 0)) then
                          IT.REGDATE
                      end),current_date)) MINREGDATE,
        max(IT.REGDATE) MAXREGDATE,
        min(coalesce((case
                        when (ITTRANSPORT.HUMWEIGHT > 0) then
                          IT.REGDATE
                      end),current_date)) DRYINGRBEGINDATE,
        cast(cast(100 * sum(cast(NET * cast(LQUALITY.DIRT as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)) L_DIRT,
        cast(cast(100 * sum(cast(NET * cast(LQUALITY.HUMIDITY as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)) L_HUMIDITY,
        min(case
              when (CROP.CROPTYPEID = 5) then
                LQUALITY.OILSEED
              else LQUALITY.SCREENING
            end) L_SCREENING,
--        cast(cast(100 * sum(cast(NET * cast(LQUALITY.GRAINUNIT as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)) L_GRAINUNIT,
--        cast(cast(100 * sum(cast(NET * cast(LQUALITY.GRAINUNIT as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as integer) L_GRAINUNIT,
        749,
        cast(cast(100 * sum(cast(NET * cast(LQUALITY.SMALLGRAIN as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)) L_SMALLGRAIN,
        min(RQUALITY.DIRT) R_DIRT, min(RQUALITY.HUMIDITY) R_HUMIDITY,
        min(RQUALITY.SCREENING) R_SCREENING, min(RQUALITY.GRAINUNIT) R_GRAINUNIT,
        min(RQUALITY.SMALLGRAIN) R_SMALLGRAIN, sum(ITTRANSPORT.HUMWEIGHT) HUMWEIGHT,
        sum(ITTRANSPORT.DIRTWEIGHT) DIRTWEIGHT, sum(ITTRANSPORT.SCREENWEIGHT) SCREENWEIGHT,
        min(IT.OWNERID) OWNERID, min(CROP.CROPTYPEID) CROPTYPEID,
        min(IT.STORINGID) STORINGID, min(LQUALITY.CROPID) CROPID,
        min(LQUALITY.CLASSID) CLASSID, IT.REGISTERID,
        min(IT.PARTYID) PARTYID, min(IT.INVOICEID), max(IT.CONTRACTID)
      from IT
        left join QUALITY_SELECT(IT.LABQUALITYID) LQUALITY on (0=0)
        left join INVOICEREGISTERS on (INVOICEREGISTERS.INVOICEREGISTERID = IT.REGISTERID)
        left join QUALITY_SELECT(INVOICEREGISTERS.QUALITYID) RQUALITY on (0=0)
        left join ITTRANSPORT on (ITTRANSPORT.INVENTORYID = IT.INVENTORYID)
        left join CROP on (CROP.CROPID = :CROPID)
      where (IT.UWORDERID = :UWORDERID)
        and ((((ITTRANSPORT.DIRTWEIGHT > 0) or (ITTRANSPORT.HUMWEIGHT > 0) or (ITTRANSPORT.SCREENWEIGHT > 0))
              and (not (:METODSOLUTION in (43,143,47,147,48,148))))
             or (((LQUALITY.DIRT > :DIRTAFTERCLEAN) or (LQUALITY.HUMIDITY > :HUMIDITYAFTERDRY))
                 and (:METODSOLUTION in (43,143,47,147,48,148))))
        and (IT.REGDATE between
              (case
                 when (:METODSOLUTION >= 100) then
                   (coalesce ((select min(GWP.BEGINDATE)
                                 from GETWORKPARTIES(:CURRENTPARTY, :UWORDERDATE, :CROPID, :CLASSID, IT.STORAGEID) GWP
                              ),:UWORDERDATE))
/*
                 when (:UWTYPEID in (3,34)) then
                   (coalesce ((select min(GWP.BEGINDATE)
                                 from GETWORKPARTIES(1, null, :CROPID, :CLASSID, IT.STORAGEID) GWP), :UWORDERDATE))

                   (coalesce ((select min(GWP.BEGINDATE)
                                 from GETWORKPARTIES(0, :UWORDERDATE, :CROPID, :CLASSID, IT.STORAGEID) GWP), :UWORDERDATE))
*/
                 else :UWORDERDATE
               end)
             and :UWACTDATE)
        and (INVOICEREGISTERS.INVOICEREGISTERID is not null)
        and ((IT.OPERATIONID = 1) and (IT.STATE > -1) and (IT.PARENTID <= 0))
    group by IT.REGISTERID
    order by OWNERID, IT.REGISTERID
    into :NET, :STORAGEID, :ACLEANINGBEGINDATE, :CLEANINGENDDATE, :ADRYINGRBEGINDATE,
         :DIRTBEFORE, :HUMIDITYBEFORE, :SCREENBEFORE, :GRAINUNITBEFORE, :SMALLGRAINBEFORE,
         :DIRTAFTER, :HUMIDITYAFTER, :SCREENAFTER, :GRAINUNITAFTER, :SMALLGRAINAFTER,
         :HUMWEIGHT, :DIRTWEIGHT, :SCREENWEIGHT, :OWNERID, :CROPTYPEID, :STORINGID,
         :ACROPID, :ACLASSID, :REGISTERID,
         :PARTYID, :INVOICE, :CONTRACTID

  do begin
    if (ACLEANINGBEGINDATE < coalesce(CLEANINGBEGINDATE,current_date)) then
      CLEANINGBEGINDATE = ACLEANINGBEGINDATE;

    if (ADRYINGRBEGINDATE < coalesce(DRYINGRBEGINDATE,current_date)) then
      DRYINGRBEGINDATE = ADRYINGRBEGINDATE;

    BEFOREWEIGHT = BEFOREWEIGHT + NET;
    WEIGHTBEFORECLEANING = 0;
    WEIGHTAFTERCLEANING = 0;
    WEIGHTBEFOREDRYING = 0;

    if (HUMIDITYAFTER > HUMIDITYBEFORE) then
      HUMIDITYAFTER = HUMIDITYBEFORE;

    if (:FINALCALCULATION = 1) then begin
      if (GRAINUNITAFTERCLEAN is not null) then
        GRAINUNITAFTER = :GRAINUNITAFTERCLEAN;

      if (DIRTAFTERCLEAN is not null) then
        DIRTAFTER = :DIRTAFTERCLEAN;

      if ((SCREENAFTERCLEAN is not null) and (coalesce(:SCREENAFTER,0) > 0)) then
        SCREENAFTER = :SCREENAFTERCLEAN;

      if (HUMIDITYAFTERDRY is not null) then
        HUMIDITYAFTER = :HUMIDITYAFTERDRY;
    end

    if ((coalesce(:SCREENAFTER,0) = 0) or (:SCREENAFTER < 0)) then
      SCREENAFTER = :SCREENBEFORE;

    if (:METODSOLUTION in (41,141,54,154)) then begin
-- сначала сушка потом очистка
      if (:METODSOLUTION in (41,141)) then begin
        if (HUMIDITYBEFORE > HUMIDITYAFTER) then
          WEIGHTBEFOREDRYING = NET;
        if ((DIRTBEFORE > DIRTAFTER) or (SCREENBEFORE > SCREENAFTER)) then begin
          WEIGHTBEFORECLEANING = NET - coalesce(HUMWEIGHT,0);
          WEIGHTAFTERCLEANING = WEIGHTBEFORECLEANING - coalesce(DIRTWEIGHT,0) - coalesce(SCREENWEIGHT,0);
          DIRTWEIGHT = coalesce(DIRTWEIGHT,0);
        end
      end
      else begin
        if (coalesce(AOWNERID,-1) <> OWNERID) then begin
          AOWNERID = OWNERID;
          A_BEFOREDRYINGWEIGHT = 0;
          ASUMHUMWEIGHT = 0;

          select
              coalesce(NET,0),
              cast(NET * cast((cast(HUMIDITY as numeric(6,4))- cast(:HUMIDITYAFTERDRY as numeric(4,2))) / (100 - cast(:HUMIDITYAFTERDRY as numeric(4,2))) as numeric (15,4)) as integer)
            from (
              select
                  sum(NET) NET,
                  cast(cast(100 * sum(cast(NET * cast(HUMIDITY as numeric(4,1)) / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)) HUMIDITY
                from (
                  select
                      sum(IT.NET) NET, min(LQUALITY.HUMIDITY) HUMIDITY, min(IT.OWNERID) OWNERID
                    from IT
                      left join QUALITY_SELECT(IT.LABQUALITYID) LQUALITY on (0=0)
                      left join ITTRANSPORT on (ITTRANSPORT.INVENTORYID = IT.INVENTORYID)
                      left join CROP on (CROP.CROPID = :CROPID)
                    where (IT.UWORDERID = :UWORDERID) and (LQUALITY.HUMIDITY > :DEFAULTVALUE)
                      and (IT.REGISTERID is not null) and (IT.OWNERID = :AOWNERID)
                      and ((IT.OPERATIONID = 1) and (IT.STATE > -1) and (IT.PARENTID <= 0))
                  group by IT.REGISTERID
                  order by 3, IT.REGISTERID
                )
              group by OWNERID
            )
          into :AWEIGHTBEFOREDRYING, :AHUMWEIGHT;

          AHUMWEIGHT = round(cast(AHUMWEIGHT as numeric (12,6)) * SETTLEMENTSHRINKAGEWEIGHT / SHRINKAGEWEIGHT);
        end

        if ((HUMIDITYBEFORE > HUMIDITYAFTER) and (HUMIDITYBEFORE > :DEFAULTVALUE)) then begin
          WEIGHTBEFOREDRYING = NET;

          if ((BEFOREDRYINGWEIGHT = (ALLWEIGHTBEFOREDRYING - WEIGHTBEFOREDRYING)) and (FINALCALCULATION = 1)) then
            HUMWEIGHT = SETTLEMENTSHRINKAGEWEIGHT - SUMHUMWEIGHT;
--          else HUMWEIGHT = round(cast(WEIGHTBEFOREDRYING as numeric (12,6)) * cast(((100 * (HUMIDITYBEFORE - HUMIDITYAFTER)) / (100 - HUMIDITYAFTER)) as numeric(4,2)) / 100);
          else
            if (A_BEFOREDRYINGWEIGHT = (AWEIGHTBEFOREDRYING - WEIGHTBEFOREDRYING)) then
              HUMWEIGHT = AHUMWEIGHT - ASUMHUMWEIGHT;
            else begin
              HUMWEIGHT = round(cast(WEIGHTBEFOREDRYING as numeric (12,6)) * cast(((100 * (HUMIDITYBEFORE - HUMIDITYAFTER)) / (100 - HUMIDITYAFTER)) as numeric(4,2)) / 100);
              HUMWEIGHT = round(cast(HUMWEIGHT as numeric (12,6)) * SETTLEMENTSHRINKAGEWEIGHT / SHRINKAGEWEIGHT);
            end

          SUMHUMWEIGHT = SUMHUMWEIGHT + coalesce(HUMWEIGHT,0);
          ASUMHUMWEIGHT = ASUMHUMWEIGHT + coalesce(HUMWEIGHT,0);
          A_BEFOREDRYINGWEIGHT = A_BEFOREDRYINGWEIGHT + WEIGHTBEFOREDRYING;
        end
        else begin
          HUMIDITYAFTER = HUMIDITYBEFORE;
          HUMWEIGHT = 0;
        end

        if ((DIRTBEFORE > DIRTAFTER) and ((not (:METODSOLUTION in (54,154))) or (:WASTEWEIGHT is not null))) then begin
          WEIGHTBEFORECLEANING = NET - coalesce(HUMWEIGHT,0);
          DIRTWEIGHT = round(cast(WEIGHTBEFORECLEANING as numeric(18,4)) * (DIRTBEFORE - DIRTAFTER) / 100);
          WEIGHTAFTERCLEANING = WEIGHTBEFORECLEANING - DIRTWEIGHT;
        end
      end

      BEFORECLEANHUMIDITY = HUMIDITYAFTER;

      if (HUMIDITYBEFORECLEAN is not null) then
        AFTERCLEANHUMIDITY = HUMIDITYBEFORECLEAN;
      else AFTERCLEANHUMIDITY = BEFORECLEANHUMIDITY;

      if (DIRTBEFOREDRY is not null) then
        BEFOREDRYDIRT = DIRTBEFOREDRY;
      else BEFOREDRYDIRT = DIRTBEFORE;

      if (DIRTAFTERDRY is not null) then
        AFTERDRYDIRT = DIRTAFTERDRY;
      else AFTERDRYDIRT = DIRTBEFORE;

      if (SCREENBEFOREDRY is not null) then
        BEFOREDRYSCREEN = SCREENBEFOREDRY;
      else BEFOREDRYSCREEN = SCREENBEFORE;

      if (SCREENAFTERDRY is not null) then
        AFTERDRYSCREEN = SCREENAFTERDRY;
      else AFTERDRYSCREEN = SCREENBEFORE;
    end
    else begin
-- сначала очистка потом сушка
      if ((DIRTBEFORE > DIRTAFTER) or (SCREENBEFORE > SCREENAFTER)) then begin
        WEIGHTBEFORECLEANING = NET;

        if (:METODSOLUTION not in (40,140)) then begin
          DIRTWEIGHT = 0;
          SCREENWEIGHT = 0;
        end

        if (:METODSOLUTION in (42,142,44,144,46,146)) then begin
          if (coalesce(AOWNERID,-1) <> OWNERID) then begin
            AOWNERID = OWNERID;
            ADIRT_BEFORE = 0;
            ADIRT_AFTER = 0;
            ASCREEN_BEFORE = 0;
            ASCREEN_AFTER = 0;
            AWEIGHTBEFORECLEANING = 0;
            ADIRTBEFORE = 0;
            ADIRTAFTER = 0;
            ADIRTWEIGHT = 0;
            ASCREENBEFORE = 0;
            ASCREENAFTER = 0;
            ASCREENWEIGHT = 0;
          end

          ADIRT_BEFORE = ADIRT_BEFORE + cast((DIRTBEFORE * WEIGHTBEFORECLEANING) / 100 as integer);
          ADIRT_AFTER = ADIRT_AFTER + cast((DIRTAFTER * WEIGHTBEFORECLEANING) / 100 as integer);
          ASCREEN_BEFORE = ASCREEN_BEFORE + cast((SCREENBEFORE * WEIGHTBEFORECLEANING) / 100 as integer);
          ASCREEN_AFTER = ASCREEN_AFTER + cast((SCREENAFTER * WEIGHTBEFORECLEANING) / 100 as integer);
          AWEIGHTBEFORECLEANING = AWEIGHTBEFORECLEANING + WEIGHTBEFORECLEANING;

          if (AWEIGHTBEFORECLEANING > 0) then begin
            ADIRTBEFORE = cast(cast(ADIRT_BEFORE * 100 as numeric (12,4)) / AWEIGHTBEFORECLEANING as numeric (4,2));
            ADIRTAFTER = cast(cast(ADIRT_AFTER * 100 as numeric (12,4)) / AWEIGHTBEFORECLEANING as numeric (4,2));
            ASCREENBEFORE = cast(cast(ASCREEN_BEFORE * 100 as numeric (12,4)) / AWEIGHTBEFORECLEANING as numeric (4,2));
            ASCREENAFTER = cast(cast(ASCREEN_AFTER * 100 as numeric (12,4)) / AWEIGHTBEFORECLEANING as numeric (4,2));
          end

          if (DIRTBEFORE > DIRTAFTER) then begin
            if (:METODSOLUTION in (42,142)) then begin
              if (mod(cast((AWEIGHTBEFORECLEANING * (ADIRTBEFORE - ADIRTAFTER) / 100) as integer), 10) <> 5) then
                DIRTWEIGHT = (round(AWEIGHTBEFORECLEANING * (ADIRTBEFORE - ADIRTAFTER) / 1000) * 10) - ADIRTWEIGHT;
              else DIRTWEIGHT = round(AWEIGHTBEFORECLEANING * (ADIRTBEFORE - ADIRTAFTER) / 100) - ADIRTWEIGHT;
            end
            else begin
              DIRTWEIGHT = round((AWEIGHTBEFORECLEANING) * cast((ADIRTBEFORE - ADIRTAFTER) as numeric(4,2)) / 100) - ADIRTWEIGHT;
              SUMDIRTWEIGHT = SUMDIRTWEIGHT + coalesce(DIRTWEIGHT,0);
            end
          end
          
          if (SCREENBEFORE > SCREENAFTER) then begin
            SCREENWEIGHT = round((AWEIGHTBEFORECLEANING) * cast((ASCREENBEFORE - ASCREENAFTER) as numeric(4,2)) / 100) - ASCREENWEIGHT;
--            SUMDIRTWEIGHT = SUMDIRTWEIGHT + coalesce(DIRTWEIGHT,0);
          end


          ADIRTWEIGHT = ADIRTWEIGHT + DIRTWEIGHT;
          ASCREENWEIGHT = ASCREENWEIGHT + SCREENWEIGHT;
        end

        if (:METODSOLUTION in (43,143,45,145,47,147,48,148)) then begin
          if (DIRTBEFORE > DIRTAFTER) then begin
            if (BEFORECLEANINGWEIGHT = (ALLWEIGHTBEFORECLEANING - WEIGHTBEFORECLEANING)) then
              DIRTWEIGHT = SETTLEMENTDIRTWEIGHT - SUMDIRTWEIGHT;
            else begin
              if (:METODSOLUTION in (47,147,48,148)) then
                DIRTWEIGHT = round(cast(WEIGHTBEFORECLEANING as numeric(18,4)) * cast((DIRTBEFORE - DIRTAFTER) as numeric(18,4)) / (cast(100 as numeric(18,4)) - DIRTAFTER));
              else
                if (:METODSOLUTION in (45,145)) then
                  DIRTWEIGHT = round(cast(WEIGHTBEFORECLEANING as numeric(18,4)) * cast((DIRTBEFORE - DIRTAFTER) as numeric(18,4)) / 100);

              ADIRTWEIGHT = ADIRTWEIGHT + DIRTWEIGHT;

              if (WASTEWEIGHT <> 0) then
                DIRTWEIGHT = round(cast(ADIRTWEIGHT as numeric(18,8)) * SETTLEMENTDIRTWEIGHT / WASTEWEIGHT) - SUMDIRTWEIGHT;
            end
          end
          
          SUMDIRTWEIGHT = SUMDIRTWEIGHT + DIRTWEIGHT;
        end

        WEIGHTAFTERCLEANING = WEIGHTBEFORECLEANING - DIRTWEIGHT - SCREENWEIGHT;
      end
      else begin
        DIRTWEIGHT = 0;
        SCREENWEIGHT = 0;
      end

      if ((HUMIDITYBEFORE > HUMIDITYAFTER) and (HUMIDITYBEFORE > :DEFAULTVALUE)) then begin
        WEIGHTBEFOREDRYING = NET - coalesce(DIRTWEIGHT,0) - coalesce(SCREENWEIGHT,0);

        if (:METODSOLUTION in (42,142,45,145,47,147,48,148)) then begin

          if (coalesce(HOWNERID,-1) <> OWNERID) then begin
            HOWNERID = OWNERID;
            AHUMIDITY_BEFORE = 0;
            AHUMIDITY_AFTER = 0;
            AWEIGHTBEFOREDRYING = 0;
            A_WEIGHTBEFOREDRYING = 0;
            AHUMIDITYBEFORE = 0;
            AHUMIDITYAFTER = 0;
            AHUMWEIGHT = 0;
          end

          if (NET < 10000) then begin
            AHUMIDITY_BEFORE = AHUMIDITY_BEFORE + cast(((cast(HUMIDITYBEFORE as numeric(4,1)) * NET) / 100) as numeric (12,4));
            AHUMIDITY_AFTER = AHUMIDITY_AFTER + cast(((cast(HUMIDITYAFTER as numeric(4,1)) * NET) / 100) as numeric (12,4));
          end
          else begin
            AHUMIDITY_BEFORE = AHUMIDITY_BEFORE + cast((cast(HUMIDITYBEFORE as numeric(4,1)) * NET) / 100 as integer);
            AHUMIDITY_AFTER = AHUMIDITY_AFTER + cast((cast(HUMIDITYAFTER as numeric(4,1)) * NET) / 100 as integer);
          end

          AWEIGHTBEFOREDRYING = AWEIGHTBEFOREDRYING + WEIGHTBEFOREDRYING;
          A_WEIGHTBEFOREDRYING = A_WEIGHTBEFOREDRYING + NET;

          if (AWEIGHTBEFOREDRYING > 0) then begin
            AHUMIDITYBEFORE = cast(cast(AHUMIDITY_BEFORE * 100 as numeric (12,4)) / A_WEIGHTBEFOREDRYING as numeric (4,2));
            AHUMIDITYAFTER = cast(cast(AHUMIDITY_AFTER * 100 as numeric (12,4)) / A_WEIGHTBEFOREDRYING as numeric (4,2));
          end

          HUMWEIGHT = round((AWEIGHTBEFOREDRYING) * cast(((100 * (AHUMIDITYBEFORE - AHUMIDITYAFTER)) / (100 - AHUMIDITYAFTER)) as numeric(4,2)) / 100) - AHUMWEIGHT;

          AHUMWEIGHT = AHUMWEIGHT + coalesce(HUMWEIGHT,0);
          SUMHUMWEIGHT = SUMHUMWEIGHT + coalesce(HUMWEIGHT,0);
        end
        else
          if (:METODSOLUTION not in (40,140)) then begin
            if (:METODSOLUTION in (44,144,46,146)) then begin
              if ((BEFOREDRYINGWEIGHT = (ALLWEIGHTBEFOREDRYING - WEIGHTBEFOREDRYING)) and (FINALCALCULATION = 1)) then
                HUMWEIGHT = SHRINKAGEWEIGHT - SUMHUMWEIGHT;
              else HUMWEIGHT = round((WEIGHTBEFOREDRYING) * cast(((100 * (HUMIDITYBEFORE - HUMIDITYAFTER)) / (100 - HUMIDITYAFTER)) as numeric(4,2)) / 100);
              SUMHUMWEIGHT = SUMHUMWEIGHT + coalesce(HUMWEIGHT,0);
            end
            else HUMWEIGHT = round((WEIGHTBEFOREDRYING) * cast(((100 * (HUMIDITYBEFORE - HUMIDITYAFTER)) / (100 - HUMIDITYAFTER)) as numeric(4,2)) / 100);
          end

        if (NET < 10000) then
          A_HUMIDITY_BEFORE = A_HUMIDITY_BEFORE + cast(((cast(HUMIDITYBEFORE as numeric(4,2)) * NET) / 100) as numeric (12,4));
        else A_HUMIDITY_BEFORE = A_HUMIDITY_BEFORE + cast((cast(HUMIDITYBEFORE as numeric(4,2)) * NET) / 100 as integer);

        A_BEFOREDRYINGWEIGHT = A_BEFOREDRYINGWEIGHT + NET;
      end
      else HUMWEIGHT = 0;

      BEFORECLEANHUMIDITY = HUMIDITYBEFORE;

      if (HUMIDITYBEFORECLEAN is not null) then
        AFTERCLEANHUMIDITY = HUMIDITYBEFORECLEAN;
      else AFTERCLEANHUMIDITY = HUMIDITYBEFORE;

      if (DIRTBEFOREDRY is not null) then
        BEFOREDRYDIRT = DIRTBEFOREDRY;
      else BEFOREDRYDIRT = DIRTAFTER;

      if (DIRTAFTERDRY is not null) then
        AFTERDRYDIRT = DIRTAFTERDRY;
      else AFTERDRYDIRT = DIRTAFTER;

      if (SCREENBEFOREDRY is not null) then
        BEFOREDRYSCREEN = SCREENBEFOREDRY;
      else BEFOREDRYSCREEN = SCREENAFTER;

      if (SCREENAFTERDRY is not null) then
        AFTERDRYSCREEN = SCREENAFTERDRY;
      else AFTERDRYSCREEN = SCREENAFTER;
    end

    if (((SCREENAFTER > SCREENBEFORE) or (coalesce(SCREENAFTER,0) = 0)) and (SCREENAFTERCLEAN is null)) then
      SCREENAFTER = SCREENBEFORE;

    if (WEIGHTBEFORECLEANING < 10000) then begin
      BEFORECLEAN_HUMIDITY = BEFORECLEAN_HUMIDITY + cast(((cast(BEFORECLEANHUMIDITY as numeric(12,1)) * WEIGHTBEFORECLEANING) / 100) as numeric (12,4));
      AFTERCLEAN_HUMIDITY = AFTERCLEAN_HUMIDITY + cast(((cast(AFTERCLEANHUMIDITY as numeric(12,1)) * WEIGHTBEFORECLEANING) / 100) as numeric (12,4));
      DIRT_BEFORE = DIRT_BEFORE + cast(((DIRTBEFORE * WEIGHTBEFORECLEANING) / 100) as numeric (12,4));
      DIRT_AFTER = DIRT_AFTER + cast(((DIRTAFTER * WEIGHTBEFORECLEANING) / 100) as numeric (12,4));
      SCREEN_BEFORE = SCREEN_BEFORE + cast(((SCREENBEFORE * WEIGHTBEFORECLEANING) / 100) as numeric (12,4));
      SCREEN_AFTER = SCREEN_AFTER + cast(((SCREENAFTER * WEIGHTBEFORECLEANING) / 100) as numeric (12,4));
    end
    else begin
      BEFORECLEAN_HUMIDITY = BEFORECLEAN_HUMIDITY + cast((cast(BEFORECLEANHUMIDITY as numeric(12,1)) * WEIGHTBEFORECLEANING) / 100 as integer);
      AFTERCLEAN_HUMIDITY = AFTERCLEAN_HUMIDITY + cast((cast(AFTERCLEANHUMIDITY as numeric(12,1)) * WEIGHTBEFORECLEANING) / 100 as integer);
      DIRT_BEFORE = DIRT_BEFORE + cast((DIRTBEFORE * WEIGHTBEFORECLEANING) / 100 as integer);
      DIRT_AFTER = DIRT_AFTER + cast((DIRTAFTER * WEIGHTBEFORECLEANING) / 100 as integer);
      SCREEN_BEFORE = SCREEN_BEFORE + cast((SCREENBEFORE * WEIGHTBEFORECLEANING) / 100 as integer);
      SCREEN_AFTER = SCREEN_AFTER + cast((SCREENAFTER * WEIGHTBEFORECLEANING) / 100 as integer);
    end

    if (WEIGHTBEFOREDRYING < 10000) then begin
      HUMIDITY_BEFORE = HUMIDITY_BEFORE + cast(((cast(HUMIDITYBEFORE as numeric(12,1)) * WEIGHTBEFOREDRYING) / 100) as numeric (12,4));
      HUMIDITY_AFTER = HUMIDITY_AFTER + cast(((cast(HUMIDITYAFTER as numeric(12,1)) * WEIGHTBEFOREDRYING) / 100) as numeric (12,4));
      BEFOREDRY_DIRT = BEFOREDRY_DIRT + cast(((BEFOREDRYDIRT * WEIGHTBEFOREDRYING) / 100) as numeric (12,4));
      AFTERDRY_DIRT = AFTERDRY_DIRT + cast(((AFTERDRYDIRT * WEIGHTBEFOREDRYING) / 100) as numeric (12,4));
      BEFOREDRY_SCREEN = BEFOREDRY_SCREEN + cast(((BEFOREDRYSCREEN * WEIGHTBEFOREDRYING) / 100) as numeric (12,4));
      AFTERDRY_SCREEN = AFTERDRY_SCREEN + cast(((AFTERDRYSCREEN * WEIGHTBEFOREDRYING) / 100) as numeric (12,4));
    end
    else begin
      HUMIDITY_BEFORE = HUMIDITY_BEFORE + cast((cast(HUMIDITYBEFORE as numeric(12,1)) * WEIGHTBEFOREDRYING) / 100 as integer);
      HUMIDITY_AFTER = HUMIDITY_AFTER + cast((cast(HUMIDITYAFTER as numeric(12,1)) * WEIGHTBEFOREDRYING) / 100 as integer);
      BEFOREDRY_DIRT = BEFOREDRY_DIRT + cast((BEFOREDRYDIRT * WEIGHTBEFOREDRYING) / 100 as integer);
      AFTERDRY_DIRT = AFTERDRY_DIRT + cast((AFTERDRYDIRT * WEIGHTBEFOREDRYING) / 100 as integer);
      BEFOREDRY_SCREEN = BEFOREDRY_SCREEN + cast((BEFOREDRYSCREEN * WEIGHTBEFOREDRYING) / 100 as integer);
      AFTERDRY_SCREEN = AFTERDRY_SCREEN + cast((AFTERDRYSCREEN * WEIGHTBEFOREDRYING) / 100 as integer);
    end

    if (:FINALCALCULATION = 1) then begin
      if (((DIRTBEFORE > DIRTAFTER) or (SCREENBEFORE > SCREENAFTER))
          and ((not (:METODSOLUTION in (54,154))) or (:WASTEWEIGHT is not null))) then begin
        select RESULTQUALITYID
          from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null,
              case
                when (:METODSOLUTION in (40,140,42,142,43,143,44,144,45,145,46,146,47,147,48,148)) then
                  :HUMIDITYBEFORE
                else :HUMIDITYAFTER
              end,
              null, null, null, :GRAINUNITBEFORE, null, null, :DIRTBEFORE, :SCREENBEFORE,
              null, :SMALLGRAINBEFORE, null, null, null)
        into :BEFORECLEANINGQUALITYID;

        select RESULTQUALITYID
          from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null,
              case
                when (:METODSOLUTION in (40,140,42,142,43,143,44,144,45,145,46,146,47,147,48,148)) then
                  :HUMIDITYBEFORE
                else :HUMIDITYAFTER
              end,
              null, null, null, :GRAINUNITBEFORE, null, null, :DIRTAFTER, :SCREENAFTER,
              null, :SMALLGRAINAFTER, null, null, null)
        into :AFTERCLEANINGQUALITYID;

        insert into IT (STORAGEID, GROSS, TARE, NET, UWORDERID,
            LABQUALITYID, SAMPLERQUALITYID, REGDATE, OPERATIONID, WEIGHT, STATE,
            OPERATIONPARAM, OWNERID, STORINGID, REGISTERID, PARTYID, INVOICEID, CONTRACTID)
          values (:STORAGEID, :WEIGHTBEFORECLEANING, :WEIGHTAFTERCLEANING, :DIRTWEIGHT, :UWORDERID,
              :BEFORECLEANINGQUALITYID, :BEFORECLEANINGQUALITYID, :UWACTDATE, 4, :SCREENWEIGHT, -999,
              :AFTERCLEANINGQUALITYID, :OWNERID, :STORINGID, :REGISTERID, :PARTYID, :INVOICE, :CONTRACTID);
      end

      if ((HUMIDITYBEFORE > HUMIDITYAFTER) and (HUMIDITYBEFORE > :DEFAULTVALUE)) then begin
        select RESULTQUALITYID
          from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYBEFORE,
              null, null, null, :GRAINUNITBEFORE, null, null, :DIRTBEFORE, :SCREENBEFORE,
              null, :SMALLGRAINBEFORE, null, null, null)
        into :BEFOREDRYINGQUALITYID;

        select RESULTQUALITYID
          from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYAFTER,
              null, null, null, :GRAINUNITBEFORE, null, null, :DIRTAFTER, :SCREENAFTER,
              null, :SMALLGRAINAFTER, null, null, null)
        into :AFTERDRYINGQUALITYID;

        insert into IT (STORAGEID, GROSS, TARE, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
            REGDATE, OPERATIONID, WEIGHT, STATE, OPERATIONPARAM, OWNERID, STORINGID, REGISTERID,
            PARTYID, INVOICEID, CONTRACTID)
          values (:STORAGEID, :WEIGHTBEFOREDRYING, :NET, :HUMWEIGHT, :UWORDERID, :BEFOREDRYINGQUALITYID, :BEFOREDRYINGQUALITYID,
              :UWACTDATE, 9, 0, -999, :AFTERDRYINGQUALITYID, :OWNERID, :STORINGID, :REGISTERID,
              :PARTYID, :INVOICE, :CONTRACTID);
      end
    end

    BEFORECLEANINGWEIGHT = BEFORECLEANINGWEIGHT + WEIGHTBEFORECLEANING;
    AFTERCLEANINGWEIGHT = AFTERCLEANINGWEIGHT + WEIGHTAFTERCLEANING;
    ALLDIRTWEIGHT = ALLDIRTWEIGHT + coalesce(DIRTWEIGHT,0);
    ALLSCREENWEIGHT = ALLSCREENWEIGHT + coalesce(SCREENWEIGHT,0);
    BEFOREDRYINGWEIGHT = BEFOREDRYINGWEIGHT + WEIGHTBEFOREDRYING;
    AFTERDRYINGWEIGHT = AFTERDRYINGWEIGHT + (WEIGHTBEFOREDRYING - HUMWEIGHT);
    DRYINGWEIGHT = DRYINGWEIGHT + coalesce(HUMWEIGHT,0);
  end

  if (BEFOREWEIGHT <> 0) then begin
    if (BEFORECLEANINGWEIGHT > 0) then begin
      HUMIDITYBEFORECLEAN = cast(cast(BEFORECLEAN_HUMIDITY * 100 as numeric (12,4)) / BEFORECLEANINGWEIGHT as numeric (4,2));

      if (HUMIDITYBEFORECLEAN is null) then
        HUMIDITYAFTERCLEAN = cast(cast(AFTERCLEAN_HUMIDITY * 100 as numeric (12,4)) / BEFORECLEANINGWEIGHT as numeric (4,2));
      else HUMIDITYAFTERCLEAN = HUMIDITYBEFORECLEAN;

      DIRTBEFORECLEAN = cast(cast(DIRT_BEFORE * 100 as numeric (12,4)) / BEFORECLEANINGWEIGHT as numeric (4,2));
      DIRTAFTERCLEAN = cast(cast(DIRT_AFTER * 100 as numeric (12,4)) / BEFORECLEANINGWEIGHT as numeric (4,2));
      SCREENBEFORECLEAN = cast(cast(SCREEN_BEFORE * 100 as numeric (12,4)) / BEFORECLEANINGWEIGHT as numeric (4,2));
      SCREENAFTERCLEAN = cast(cast(SCREEN_AFTER * 100 as numeric (12,4)) / BEFORECLEANINGWEIGHT as numeric (4,2));
    end
    else begin
      DIRTBEFORECLEAN = null;
      DIRTAFTERCLEAN = null;
      SCREENBEFORECLEAN = null;
      SCREENAFTERCLEAN = null;
    end
    if (BEFOREDRYINGWEIGHT > 0) then begin
      if (not (:METODSOLUTION in (41,141,54,154))) then begin
        if (A_BEFOREDRYINGWEIGHT > 0) then
          HUMIDITYBEFOREDRY = cast(cast(A_HUMIDITY_BEFORE * 100 as numeric (12,4)) / A_BEFOREDRYINGWEIGHT as numeric (4,2));
      end
      else HUMIDITYBEFOREDRY = cast(cast(HUMIDITY_BEFORE * 100 as numeric (12,4)) / BEFOREDRYINGWEIGHT as numeric (4,2));

      HUMIDITYAFTERDRY = cast(cast(HUMIDITY_AFTER * 100 as numeric (12,4)) / BEFOREDRYINGWEIGHT as numeric (4,2));
      DIRTBEFOREDRY = cast(cast(BEFOREDRY_DIRT * 100 as numeric (12,4)) / BEFOREDRYINGWEIGHT as numeric (4,2));
      DIRTAFTERDRY = cast(cast(AFTERDRY_DIRT * 100 as numeric (12,4)) / BEFOREDRYINGWEIGHT as numeric (4,2));
      SCREENBEFOREDRY = cast(cast(BEFOREDRY_SCREEN * 100 as numeric (12,4)) / BEFOREDRYINGWEIGHT as numeric (4,2));
      SCREENAFTERDRY = cast(cast(AFTERDRY_SCREEN * 100 as numeric (12,4)) / BEFOREDRYINGWEIGHT as numeric (4,2));
    end
    else begin
      HUMIDITYBEFOREDRY = null;
      HUMIDITYAFTERDRY = null;
    end
  end
  else begin
    DIRTBEFORECLEAN = null;
    HUMIDITYBEFOREDRY = null;
    SCREENBEFORECLEAN = null;

    DIRTAFTERCLEAN = null;
    HUMIDITYAFTERDRY = null;
    SCREENAFTERCLEAN = null;
  end

  DRYING = (100 * (:HUMIDITYBEFOREDRY - :HUMIDITYAFTERDRY)) / (100 - :HUMIDITYAFTERDRY);

/*
  STORAGEID = coalesce(:STORAGEID, :DRYSTORAGEID);
  if (:BEFOREWEIGHT = :BEFOREDRYINGWEIGHT) then begin
    BEFOREWEIGHT = :BEFOREWEIGHT - :HUMWEIGHT;
    HUMIDITYBEFORE = :HUMIDITYAFTER;
  end
  else begin
    if (:METODSOLUTION in (5, 41)) then
      HUMIDITYAFTER = :HUMIDITYBEFORE;
  end

  if (:METODSOLUTION in (5,41,43,53)) then
    HUMIDITYAFTER = :HUMIDITYBEFORE;

  if (coalesce(:BEFOREWEIGHT,0) = 0) then begin
    BEFOREWEIGHT = :BEFOREDRYINGWEIGHT;
    RBEGINDATE = :DRYINGRBEGINDATE;
  end
*/

  if (:WRITING_OFF = 1) then
    delete from IT
      where (IT.OPERATIONID in (4,22)) and (IT.UWORDERID = :UWORDERID);

  execute procedure QUALITY_UPDATE(0, :QUALITYBEFOREID, :CROPID, :CLASSID, :SORTID, null, :QUALITYNUMBERBEFORECLEAN,
    case
      when (:METODSOLUTION in (41,141,54,154)) then
        case
          when (:HUMIDITYAFTERDRY is null) then
            :HUMIDITYBEFORECLEAN
          else :HUMIDITYAFTERDRY
        end
      else :HUMIDITYBEFORECLEAN
    end,
    null, null, null, :GRAINUNITBEFORECLEAN, null, null, :DIRTBEFORECLEAN, :SCREENBEFORECLEAN, null,
    :SMALLGRAINBEFORECLEAN, null, null)
  returning_values :QUALITYBEFOREID;

  execute procedure QUALITY_UPDATE(0, :QUALITYAFTERID, :CROPID, :CLASSID, :SORTID, null, :QUALITYNUMBERAFTERCLEAN,
    case
      when (:METODSOLUTION in (41,141,54,154)) then
        case
          when (:HUMIDITYAFTERDRY is null) then
            :HUMIDITYBEFORECLEAN
          else :HUMIDITYAFTERDRY
        end
      else :HUMIDITYAFTERCLEAN
    end,
    null, null, null, :GRAINUNITAFTERCLEAN, null, null, :DIRTAFTERCLEAN, :SCREENAFTERCLEAN, null,
    :SMALLGRAINAFTERCLEAN, null, null)
  returning_values :QUALITYAFTERID;

  execute procedure QUALITY_UPDATE(0, :BEFOREDRYQUALITYID, :CROPID, :CLASSID, :SORTID,  null,
    :QUALITYNUMBERBEFOREDRY, :HUMIDITYBEFOREDRY, null, null, null, :GRAINUNITBEFOREDRY, null, null,
    :DIRTBEFOREDRY, :SCREENBEFOREDRY, null, :SMALLGRAINBEFOREDRY, null, null)
  returning_values :BEFOREDRYQUALITYID;

  execute procedure QUALITY_UPDATE(0, :AFTERDRYQUALITYID, :CROPID, :CLASSID, :SORTID, null,
    :QUALITYNUMBERAFTERDRY, :HUMIDITYAFTERDRY, null, null, null, :GRAINUNITAFTERDRY, null, null,
    :DIRTAFTERDRY, :SCREENAFTERDRY, null, :SMALLGRAINAFTERDRY, null, null)
  returning_values :AFTERDRYQUALITYID;

--  if (BEFORECLEANINGWEIGHT > 0) then
--    BEFOREWEIGHT = BEFORECLEANINGWEIGHT;
  ABEFOREWEIGHT = BEFOREWEIGHT;

  if ((DIRTBEFORECLEAN is not null) and (:DIRTAFTERCLEAN is not null) and (DIRTBEFORECLEAN > DIRTAFTERCLEAN)) then
    if (:METODSOLUTION in (42,142)) then
      CLEAN = :DIRTBEFORECLEAN - :DIRTAFTERCLEAN;
    else CLEAN = cast(cast(100 * (:DIRTBEFORECLEAN - :DIRTAFTERCLEAN) as numeric(9,5)) / (100 - :DIRTAFTERCLEAN) as numeric(4,2));

  if ((SCREENBEFORECLEAN is not null) and (:SCREENAFTERCLEAN is not null) and (SCREENBEFORECLEAN > SCREENAFTERCLEAN)) then
    if (:METODSOLUTION in (42,142)) then
      CLEAN = coalesce(CLEAN,0) + (:SCREENBEFORECLEAN - :SCREENAFTERCLEAN);
    else CLEAN = coalesce(CLEAN,0) + cast(cast(100 * (:SCREENBEFORECLEAN - :SCREENAFTERCLEAN) as numeric(9,5)) / (100 - :SCREENAFTERCLEAN) as numeric(4,2));

-- корректировка усушки по расчетной от общего веса до сушки
  if ((:FINALCALCULATION = 1) and (:METODSOLUTION in (45,145))) then begin
    select max(IT.INVENTORYID), sum(IT.NET)
      from IT
      where (IT.UWORDERID = :UWORDERID) and (IT.NET > 0)
        and (IT.STATE = -999) and (IT.OPERATIONID = 9)
    group by IT.UWORDERID
    into :INVENTORYID, :SHRINKAGEWEIGHT;

    if ((SHRINKAGEWEIGHT > 0) and (SHRINKAGEWEIGHT <> SETTLEMENTSHRINKAGEWEIGHT)) then begin
      COEFFICIENT = cast((cast(SETTLEMENTSHRINKAGEWEIGHT as numeric (15,8)) / SHRINKAGEWEIGHT) as numeric (9,8));
      SUMHUMWEIGHT = 0;

      for
        select IT.INVENTORYID, IT.NET
          from IT
          where ((IT.UWORDERID = :UWORDERID) and (IT.NET > 0))
            and ((IT.STATE = -999) and (IT.OPERATIONID = 9))
        order by IT.INVENTORYID
        into :AINVENTORYID, :RWEIGHT

      do begin
        if (AINVENTORYID <> INVENTORYID) then begin
          HUMWEIGHT = round(RWEIGHT * COEFFICIENT);
          SUMHUMWEIGHT = SUMHUMWEIGHT + HUMWEIGHT;
        end
        else HUMWEIGHT = SETTLEMENTSHRINKAGEWEIGHT - SUMHUMWEIGHT;

        if (HUMWEIGHT <> RWEIGHT) then
          update IT
            set NET = :HUMWEIGHT
          where (INVENTORYID = :AINVENTORYID);
      end

      AFTERDRYINGWEIGHT = BEFOREDRYINGWEIGHT - SETTLEMENTSHRINKAGEWEIGHT;
    end
  end

  if (:METODSOLUTION in (3,44,144,45,145,46,146,47,147,54,154)) then begin
--  if (:METODSOLUTION in (3,44,144,45,145,46,146,54,154)) then begin

-- корректировка отходов по вызенным
    if (:METODSOLUTION not in (3,45,145)) then
      select UNDERWORK_CALCULATE_WASTE.ALLDIRTWEIGHT, UNDERWORK_CALCULATE_WASTE.ALLSCREENWEIGHT,
          UNDERWORK_CALCULATE_WASTE.BEFOREDRYINGWEIGHT, UNDERWORK_CALCULATE_WASTE.AFTERDRYINGWEIGHT
        from UNDERWORK_CALCULATE_WASTE(:UWORDERID, :UWTYPEID, :METODSOLUTION, :FINALCALCULATION, :BEFOREDRYINGWEIGHT, :AFTERDRYINGWEIGHT, :DRYING)
      into :ALLDIRTWEIGHT, :ALLSCREENWEIGHT, :BEFOREDRYINGWEIGHT, :AFTERDRYINGWEIGHT;

    for
      select QUALITY_SELECT.CLASSID, sum(IT.NET),
          cast(sum(NET * cast(QUALITY_SELECT.HUMIDITY as numeric (15,4))) / sum(NET) as numeric (4,2)),
          cast(sum(NET * cast(QUALITY_SELECT.DIRT as numeric (15,4))) / sum(NET) as numeric (4,2)),
          case
            when (min(CROP.CROPTYPEID) = 5) then
              cast(sum(NET * cast(QUALITY_SELECT.OILSEED as numeric (15,4))) / sum(NET) as numeric (4,2))
            else cast(sum(NET * cast(QUALITY_SELECT.SCREENING as numeric (15,4))) / sum(NET) as numeric (4,2))
          end,
          cast(sum(NET * cast(QUALITY_SELECT.GRAINUNIT as numeric (15,4))) / sum(NET) as numeric (4,2)),
          cast(sum(NET * cast(QUALITY_SELECT.SMALLGRAIN as numeric (15,4))) / sum(NET) as numeric (4,2))
        from IT
          left join QUALITY_SELECT(IT.LABQUALITYID) on (0=0)
          left join CLASS on (CLASS.CLASSID = QUALITY_SELECT.CLASSID)
          left join CROP on (CROP.CROPID = QUALITY_SELECT.CROPID)
        where (IT.OPERATIONID in (5,14))
          and (IT.REGDATE between
                (case
                   when (:UWTYPEID = 3) then
                     (coalesce((select min(GWP.BEGINDATE)
                                  from GETWORKPARTIES(:CURRENTPARTY, :UWORDERDATE, QUALITY_SELECT.CROPID, QUALITY_SELECT.CLASSID, :STORAGEID) GWP
                               ),:UWORDERDATE))
                   else :UWORDERDATE
                 end)
               and :UWACTDATE)
          and (IT.UWORDERID = :UWORDERID) and (IT.STORAGEID = :STORAGEID) and (IT.STATE > -1)
      group by QUALITY_SELECT.CLASSID
      union
        select 9999, null, null, null, null, null, null
          from rdb$database
      into :ACLASSID, :AWEIGHT, :FORAGEHUMIDITY, :FORAGEDIRT, :FORAGESCREEN, :FORAGEGRAINUNIT, :FORAGESMALLGRAIN

    do begin
      if (:ACLASSID = 106) then begin  -- мелкое зерно
        select UWACTS.QUALITYID_SMALLGRAIN
          from UWACTS
          where (UWACTS.UWORDERID = :UWORDERID)
        into :FORAGE_QUALITYID;

        W_SMALLGRAIN = :AWEIGHT;
        DIRT_SMALLGRAIN = :FORAGEDIRT;
      end
      else
        if (:ACLASSID = 999) then begin  -- побочн. продукт Д
          select UWACTS.QUALITYID_BY_PRODUCT_D
            from UWACTS
            where (UWACTS.UWORDERID = :UWORDERID)
          into :FORAGE_QUALITYID;

          W_BYPRODUCT_D = :AWEIGHT;
          DIRT_BYPRODUCT_D = :FORAGEDIRT;
        end
        else
          if (:ACLASSID = 105) then begin  -- побочн. продукт Б
            select UWACTS.QUALITYID_BY_PRODUCT_B
              from UWACTS
              where (UWACTS.UWORDERID = :UWORDERID)
            into :FORAGE_QUALITYID;

            W_BYPRODUCT_B = :AWEIGHT;
            DIRT_BYPRODUCT_B = :FORAGEDIRT;
          end
          else
            if (:ACLASSID = 107) then begin -- побочн. продукт А
              select UWACTS.QUALITYID_BY_PRODUCT_A
                from UWACTS
                where (UWACTS.UWORDERID = :UWORDERID)
              into :FORAGE_QUALITYID;

              W_BYPRODUCT_A = :AWEIGHT;
              DIRT_BYPRODUCT_A = :FORAGEDIRT;
            end
            else
              if (:ACLASSID = 39) then begin -- отходы 1 категори А
                select UWACTS.QUALITYID_1C_A
                  from UWACTS
                  where (UWACTS.UWORDERID = :UWORDERID)
                into :FORAGE_QUALITYID;

                W_FORAGE1C_A = :AWEIGHT;
                DIRT_FORAGE1C_A = :FORAGEDIRT;
              end
              else
                if (:ACLASSID = 68) then begin  -- отходы 1 категории Б
                  select UWACTS.QUALITYID_1C_B
                    from UWACTS
                    where (UWACTS.UWORDERID = :UWORDERID)
                  into :FORAGE_QUALITYID;

                  W_FORAGE1C_B = :AWEIGHT;
                  DIRT_FORAGE1C_B = :FORAGEDIRT;
                end
                else
                  if (:ACLASSID = 40) then begin -- отходы 2 категории
                    select UWACTS.QUALITYID_2C
                      from UWACTS
                      where (UWACTS.UWORDERID = :UWORDERID)
                    into :FORAGE_QUALITYID;

                    W_FORAGE2C = :AWEIGHT;
                    DIRT_FORAGE2C = :FORAGEDIRT;
                  end
                  else begin  -- отходы 3 категории
                    select UWACTS.QUALITYID_3C
                      from UWACTS
                      where (UWACTS.UWORDERID = :UWORDERID)
                    into :FORAGE_QUALITYID;

                    if (:ACLASSID = 41) then begin -- отходы 3 категории
                      W_FORAGE3C = :AWEIGHT;
                      DIRT_FORAGE3C = :FORAGEDIRT;
                    end
                    else begin
                      if ((:METODSOLUTION in (47,147)) and (W_FORAGE3C = 0)) then begin
                        select DIRT
                          from QUALITY_SELECT(:FORAGE_QUALITYID)
                        into :FORAGEDIRT;

                        ALLSCREENWEIGHT = W_BYPRODUCT_A + W_BYPRODUCT_B + W_BYPRODUCT_D + W_FORAGE1C_A + W_FORAGE1C_B + W_FORAGE2C + W_SMALLGRAIN;

                        if (ALLSCREENWEIGHT > 0) then begin
                          ALLDIRTWEIGHT = trunc((((DIRTBEFORECLEAN - DIRTAFTERCLEAN) * BEFORECLEANINGWEIGHT) - ((DIRT_BYPRODUCT_A - DIRTAFTERCLEAN) * W_BYPRODUCT_A)
                                                                   - ((DIRT_BYPRODUCT_B - DIRTAFTERCLEAN) * W_BYPRODUCT_B)
                                                                   - ((DIRT_BYPRODUCT_D - DIRTAFTERCLEAN) * W_BYPRODUCT_D)
                                                                   - ((DIRT_FORAGE1C_A - DIRTAFTERCLEAN) * W_FORAGE1C_A)
                                                                   - ((DIRT_FORAGE1C_B - DIRTAFTERCLEAN) * W_FORAGE1C_B)
                                                                   - ((DIRT_FORAGE2C - DIRTAFTERCLEAN) * W_FORAGE2C)
                                                                   - ((DIRT_SMALLGRAIN - DIRTAFTERCLEAN) * W_SMALLGRAIN)
                                                ) / (FORAGEDIRT - DIRTAFTERCLEAN) / 10) * 10;
                        end
                        else ALLDIRTWEIGHT = trunc(cast(BEFORECLEANINGWEIGHT as numeric(18,4)) * cast((DIRTBEFORECLEAN - DIRTAFTERCLEAN) as numeric(18,4)) / (cast(100 as numeric(18,4)) - DIRTAFTERCLEAN) / 10) * 10;

                        AWEIGHT = ALLDIRTWEIGHT;
                      end
                    end
                  end

      select
          QUALITY_SELECT.DIRT, QUALITY_SELECT.SCREENING, QUALITY_SELECT.HUMIDITY,
          QUALITY_SELECT.GRAINUNIT, QUALITY_SELECT.SMALLGRAIN, QUALITY_SELECT.QUALITYNUMBER
        from QUALITY_SELECT(:FORAGE_QUALITYID)
          left join CROP on (CROP.CROPID = QUALITY_SELECT.CROPID)
      into :FORAGE_DIRT, :FORAGE_SCREEN, :FORAGE_HUMIDITY,
           :FORAGE_GRAINUNIT, :FORAGE_SMALLGRAIN, :FORAGE_QUALITYNUMBER;

      if (:FINALCALCULATION = 1) then begin
        if (FORAGE_DIRT is not null) then
          FORAGEDIRT = :FORAGE_DIRT;

        if (FORAGE_SCREEN is not null) then
          FORAGESCREEN = :FORAGE_SCREEN;

        if (FORAGE_HUMIDITY is not null) then
          FORAGEHUMIDITY = :FORAGE_HUMIDITY;

        if (FORAGE_GRAINUNIT is not null) then
          FORAGEGRAINUNIT = :FORAGE_GRAINUNIT;

        if (FORAGE_SMALLGRAIN is not null) then
          FORAGESMALLGRAIN = :FORAGE_SMALLGRAIN;
      end
      else begin
        if (:METODSOLUTION in (42,142,43,143,44,144,45,145,46,146,47,147,48,148)) then
          FORAGEHUMIDITY = HUMIDITYAFTERCLEAN;
        else FORAGEHUMIDITY = HUMIDITYAFTERDRY;
      end

      execute procedure QUALITY_UPDATE(0, :FORAGE_QUALITYID, :CROPID, :CLASSID, :SORTID, null,
        :FORAGE_QUALITYNUMBER, :FORAGEHUMIDITY, null, null, null, :FORAGEGRAINUNIT, null, null,
        :FORAGEDIRT, :FORAGESCREEN, null, :FORAGESMALLGRAIN, null, null)
      returning_values :FORAGE_QUALITYID;

      if (:WRITING_OFF = 1) then begin
        INVOICE = gen_id(INVOICEIDGEN, 1);

        insert into INVOICES (INVOICEID)
          values (:INVOICE);

        select RESULTQUALITYID  -- качество для 4-й операции
          from QUALITY_INSERT(null, :CROPID, :CLASSID, :SORTID, null, null, :FORAGEHUMIDITY,
              null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREEN,
              null, :FORAGESMALLGRAIN, null, null, null)
        into :WASTEQUALITYID;

        insert into QUALITYDATA (QUALITYID, QUALITYTYPEID, QUALITYVALUE)
          values (:WASTEQUALITYID, 3220, :CROPID);

        insert into QUALITYDATA (QUALITYID, QUALITYTYPEID, QUALITYVALUE)
          values (:WASTEQUALITYID, 3222, :CLASSID);

        if (:METODSOLUTION = 3) then begin   -- если отходы не 3-ей категории 4-я операция не пишется
          insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
              REGDATE, OPERATIONID, WEIGHT, STATE, OPERATIONPARAM, OWNERID, INVOICEID)
            values (:STORAGEID, :AWEIGHT, :UWORDERID, :FORAGE_IT_QUALITYID, :FORAGE_IT_QUALITYID,
              :UWACTDATE, 22, :AWEIGHT, 0, :WASTEQUALITYID, :CLIENTID, :INVOICE);

           insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
              REGDATE, OPERATIONID, WEIGHT, STATE, OWNERID, INVOICEID)
            values (:STORAGEID, :AWEIGHT, :UWORDERID, :WASTEQUALITYID, :WASTEQUALITYID,
              :UWACTDATE, 4, :AWEIGHT, 0,  :CLIENTID, :INVOICE);

          insert into ITTRANSPORT (INVENTORYID, TRANSPORTTYPEID)
            values (:FORAGE_IT_QUALITYID, 0);
        end
        else begin
          insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
              REGDATE, OPERATIONID, WEIGHT, STATE, OPERATIONPARAM, OWNERID, INVOICEID)
            values (:STORAGEID, :AWEIGHT, :UWORDERID, :FORAGE_IT_QUALITYID, :FORAGE_IT_QUALITYID,
              :UWACTDATE, 22, :AWEIGHT, 0, :WASTEQUALITYID, :CLIENTID, :INVOICE);

          select IT.INVENTORYID
            from IT
            where (IT.UWORDERID = :UWORDERID)
              and (IT.OPERATIONID = 22)
              and (IT.STATE = 0)
          into :INVID;

          insert into ITTRANSPORT (INVENTORYID, TRANSPORTTYPEID)
            values (:INVID,1);

          insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
              REGDATE, OPERATIONID, WEIGHT, STATE, OWNERID, INVOICEID)
            values (:STORAGEID, :AWEIGHT, :UWORDERID, :WASTEQUALITYID, :WASTEQUALITYID,
              :UWACTDATE, 4, :AWEIGHT, 0,  :CLIENTID, :INVOICE);
        end
      end
    end

    HUMWEIGHT = iif((:METODSOLUTION = 3), 0, :HUMWEIGHT);
  end
  else begin
    if (coalesce(:SELECTEDWASTE,3) = 2) then -- по умолчанию в третью категорию
      W_FORAGE2C = :CLEANWEIGHT;
    else W_FORAGE3C = :CLEANWEIGHT;

    select                                         
        case
          when (coalesce(:SELECTEDWASTE,3) = 2) then
            UWACTS.QUALITYID_2C
          else UWACTS.QUALITYID_3C
        end
      from UWACTS
      where (UWACTS.UWORDERID = :UWORDERID)
    into :FORAGE_QUALITYID;

    update QUALITY
      set QUALITY.CLASSID = :ACLASSID
      where (QUALITY.QUALITYID = :FORAGE_QUALITYID);

    if (row_count = 0) then
      insert into QUALITY (QUALITYID, CLASSID)
        values (:FORAGE_QUALITYID, :ACLASSID);

    select HUMIDITY, GRAINUNIT, DIRT, SCREENING, SMALLGRAIN, QUALITYNUMBER
      from QUALITY_SELECT(:FORAGE_QUALITYID)
    into :FORAGEHUMIDITY, :FORAGEGRAINUNIT, :FORAGEDIRT, :FORAGESCREEN,
         :FORAGESMALLGRAIN, :FORAGE_QUALITYNUMBER;

    if (:FINALCALCULATION = 0) then
      FORAGEHUMIDITY = HUMIDITYBEFORECLEAN;

    execute procedure QUALITY_UPDATE(0, :FORAGE_QUALITYID, :CROPID, :CLASSID, :SORTID, null,
        :FORAGE_QUALITYNUMBER, :FORAGEHUMIDITY, null, null, null, :FORAGEGRAINUNIT, null, null,
        :FORAGEDIRT, :FORAGESCREEN, null, :FORAGESMALLGRAIN, null, null)
      returning_values :FORAGE_QUALITYID;

    if (:WRITING_OFF = 1) then begin
      select RESULTQUALITYID
        from QUALITY_INSERT(null, :CROPID, :CLASSID, :SORTID, null, null, :FORAGEHUMIDITY,
            null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREEN,
            null, :FORAGESMALLGRAIN, null, null, null)
      into :FORAGE_IT_QUALITYID;

      INVOICE = GEN_ID(INVOICEIDGEN, 1);

      select RESULTQUALITYID
        from QUALITY_INSERT(null, :CROPID, :CLASSID, :SORTID, null, null, :FORAGEHUMIDITY,
            null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREEN,
            null, :FORAGESMALLGRAIN, null, null, null)
      into :WASTEQUALITYID;

      insert into QUALITYDATA (QUALITYID, QUALITYTYPEID, QUALITYVALUE)
        values (:WASTEQUALITYID, 3220, :CROPID);

      insert into QUALITYDATA (QUALITYID, QUALITYTYPEID, QUALITYVALUE)
        values (:WASTEQUALITYID, 3222, :CLASSID);
                           
      insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
          REGDATE, OPERATIONID, WEIGHT, STATE, OPERATIONPARAM, OWNERID, INVOICEID)
        values (:STORAGEID, :CLEANWEIGHT, :UWORDERID, :FORAGE_IT_QUALITYID,
          :FORAGE_IT_QUALITYID, :UWACTDATE, 22, :CLEANWEIGHT, 0, :WASTEQUALITYID,
          :CLIENTID, :INVOICE);

      select IT.INVENTORYID
        from IT
        where (IT.UWORDERID = :UWORDERID)
          and (IT.OPERATIONID = 22)
          and (IT.STATE = 0)
      into :INVID;

      insert into INVOICES (INVOICEID)
        values (:INVOICE);

      insert into ITTRANSPORT (INVENTORYID, TRANSPORTTYPEID)
        values (:INVID, 1);

      insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID,
          REGDATE, OPERATIONID, WEIGHT, STATE, OWNERID, INVOICEID)
        values (:STORAGEID, :CLEANWEIGHT, :UWORDERID, :WASTEQUALITYID,
          :WASTEQUALITYID, :UWACTDATE, 4, :CLEANWEIGHT, 0, :CLIENTID, :INVOICE);

    end
  end

-- корректировка усушки по расчетной от общего веса до сушки
  if ((:FINALCALCULATION = 1) and (:METODSOLUTION in (47,147,48,148))) then begin
    select max(IT.INVENTORYID), sum(IT.GROSS), sum(IT.NET)
      from IT
      where (IT.UWORDERID = :UWORDERID) and (IT.NET > 0)
        and (IT.STATE = -999) and (IT.OPERATIONID = 9)
    group by IT.UWORDERID
    into :INVENTORYID, :ALLWEIGHTBEFOREDRYING, :SHRINKAGEWEIGHT;

    SETTLEMENTSHRINKAGEWEIGHT = ALLWEIGHTBEFOREDRYING * DRYING / 100;

    if ((SHRINKAGEWEIGHT > 0) and (SHRINKAGEWEIGHT <> SETTLEMENTSHRINKAGEWEIGHT)) then begin
      COEFFICIENT = cast((cast(SETTLEMENTSHRINKAGEWEIGHT as numeric (15,8)) / SHRINKAGEWEIGHT) as numeric (9,8));
      SUMHUMWEIGHT = 0;

      for
        select IT.INVENTORYID, IT.NET
          from IT
          where ((IT.UWORDERID = :UWORDERID) and (IT.NET > 0))
            and ((IT.STATE = -999) and (IT.OPERATIONID = 9))
        order by IT.INVENTORYID
        into :AINVENTORYID, :RWEIGHT

      do begin
        if (AINVENTORYID <> INVENTORYID) then begin
          HUMWEIGHT = round(RWEIGHT * COEFFICIENT);
          SUMHUMWEIGHT = SUMHUMWEIGHT + HUMWEIGHT;
        end
        else HUMWEIGHT = SETTLEMENTSHRINKAGEWEIGHT - SUMHUMWEIGHT;

        if (HUMWEIGHT <> RWEIGHT) then
          update IT
            set NET = :HUMWEIGHT
          where (INVENTORYID = :AINVENTORYID);
      end

      AFTERDRYINGWEIGHT = BEFOREDRYINGWEIGHT - SETTLEMENTSHRINKAGEWEIGHT;
    end
  end

  update UWORDERS
    set BEFOREDRYINGWEIGHT = :BEFOREDRYINGWEIGHT,
        BEFOREDRYQUALITYID = :BEFOREDRYQUALITYID,
        BEFORECLEANINGWEIGHT = :BEFORECLEANINGWEIGHT,
        PARTYID = :PARTYID
    where (UWORDERID = :UWORDERID);

  if (:METODSOLUTION in (40,140,41,141)) then
    W_FORAGE2C = ALLSCREENWEIGHT;

  update UWACTS
--    set WEIGHT = :RWEIGHT,
    set WEIGHT = :BEFOREWEIGHT - coalesce(:ALLDIRTWEIGHT,0) - coalesce(:ALLSCREENWEIGHT,0) - coalesce(:DRYINGWEIGHT,0),
--      BEGINDATE = :RBEGINDATE,
--      ENDDATE = :RENDDATE,
        FORAGE1C_A = nullif(:W_FORAGE1C_A,0),
        FORAGE1C_B = nullif(:W_FORAGE1C_B,0),
        FORAGE2C = nullif(:W_FORAGE2C,0),
        FORAGE3C = :ALLDIRTWEIGHT,
        FORAGE_SMALLGRAIN = nullif(:W_SMALLGRAIN,0),
        FORAGE_BY_PRODUCT_A = nullif(:W_BYPRODUCT_A,0),
        FORAGE_BY_PRODUCT_B = nullif(:W_BYPRODUCT_B,0),
        FORAGE_BY_PRODUCT_D = nullif(:W_BYPRODUCT_D,0),
        DRYING = :DRYING,
        CLEAN = :CLEAN,
        AFTERDRYQUALITYID = :AFTERDRYQUALITYID,
        AFTERDRYINGWEIGHT = :AFTERDRYINGWEIGHT,
        AFTERCLEANINGWEIGHT = :BEFORECLEANINGWEIGHT - coalesce(:ALLDIRTWEIGHT,0) - coalesce(:ALLSCREENWEIGHT,0)
    where (UWORDERID = :UWORDERID);

  suspend;

end

 

     Previous topic Chapter index Next topic