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

UNDERWORK_CALCUWACT_REGISTERCL

 

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

Параметр

Тип

Описание

UWORDERDATE

DATE

 

UWACTDATE

DATE

 

UWORDERID

INTEGER

 

QUALITYBEFOREID

INTEGER

 

QUALITYAFTERID

INTEGER

 

CROPID

INTEGER

 

CLASSID

INTEGER

 

UWTYPEID

INTEGER

 

SORTID

INTEGER

 

CLIENTID

INTEGER

 

FINALCALCULATION

INTEGER

 

 

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

Параметр

Тип

Описание

BEFOREWEIGHT

INTEGER

 

STORAGEID

INTEGER

 

 

Описание

Расчет акта очистки после сформированного акта на сушку по реестрам

 

Определение

CREATE PROCEDURE UNDERWORK_CALCUWACT_REGISTERCL(
    UWORDERDATE DATE,
    UWACTDATE DATE,
    UWORDERID INTEGER,
    QUALITYBEFOREID INTEGER,
    QUALITYAFTERID INTEGER,
    CROPID INTEGER,
    CLASSID INTEGER,
    UWTYPEID INTEGER,
    SORTID INTEGER,
    CLIENTID INTEGER,
    FINALCALCULATION INTEGER)
RETURNS (
    BEFOREWEIGHT INTEGER,
    STORAGEID INTEGER)
AS
declare variable METODSOLUTION integer;
declare variable SELECTEDWASTE integer;
declare variable WRITING_OFF integer;
declare variable CLEANINGBEGINDATE date;
declare variable CLEANINGENDDATE date;
declare variable NET integer;
declare variable WEIGHT integer;
declare variable HUMIDITYBEFORE numeric(9,2);
declare variable DIRTBEFORE numeric(9,2);
declare variable SCREENINGBEFORE numeric(9,2);
declare variable GRAINUNITBEFORE integer;
declare variable SMALLGRAINBEFORE numeric(9,2);
declare variable HUMIDITYAFTER numeric(9,2);
declare variable DIRTAFTER numeric(9,2);
declare variable SCREENINGAFTER numeric(9,2);
declare variable GRAINUNITAFTER integer;
declare variable SMALLGRAINAFTER decimal(9,2);
declare variable OWNERID integer;
declare variable REGISTERID integer;
declare variable ACROPID integer;
declare variable ACLASSID integer;
declare variable PARTYID integer;
declare variable INVOICE integer;
declare variable CONTRACTID integer;
declare variable STORINGID integer;
declare variable DIRTWEIGHT integer = 0;
declare variable SCREENWEIGHT integer = 0;
declare variable ALLNET integer = 0;
declare variable ALLDIRTWEIGHT integer = 0;
declare variable ALLSCREENWEIGHT integer = 0;
declare variable SETTLEMENTWASTEWEIGHT integer;
declare variable SETTLEMENTSCREENINGWEIGHT integer;
declare variable CLEAN numeric(9,2) = 0;
declare variable BEFORECLEANINGQUALITYID integer;
declare variable QUALITYNUMBERBEFORECLEAN integer;
declare variable HUMIDITY_BEFORECLEAN integer = 0;
declare variable HUMIDITYBEFORECLEAN numeric(9,2);
declare variable DIRT_BEFORECLEAN integer = 0;
declare variable DIRTBEFORECLEAN numeric(9,2);
declare variable SCREENING_BEFORECLEAN integer = 0;
declare variable SCREENINGBEFORECLEAN numeric(9,2);
declare variable GRAINUNITBEFORECLEAN integer;
declare variable SMALLGRAINBEFORECLEAN numeric(9,2);
declare variable AFTERCLEANINGQUALITYID integer;
declare variable QUALITYNUMBERAFTERCLEAN smallint;
declare variable HUMIDITY_AFTERCLEAN integer = 0;
declare variable HUMIDITYAFTERCLEAN numeric(9,2);
declare variable DIRT_AFTERCLEAN integer = 0;
declare variable DIRTAFTERCLEAN numeric(9,2);
declare variable SCREENING_AFTERCLEAN integer = 0;
declare variable SCREENINGAFTERCLEAN numeric(9,2);
declare variable GRAINUNITAFTERCLEAN integer;
declare variable SMALLGRAINAFTERCLEAN numeric(9,2);
declare variable RESULTQUALITYID integer;
declare variable ADIRTWEIGHT integer;
declare variable COEFFICIENT numeric(6,4);
declare variable INVENTORYID integer;
declare variable DELTAWEIGHT integer;
declare variable TARE integer;
declare variable QUALITYNUMBERFORAGE integer;
declare variable FORAGEHUMIDITY numeric(9,2);
declare variable HUMIDITYFORAGE numeric(9,2);
declare variable FORAGEDIRT numeric(9,2);
declare variable DIRTFORAGE numeric(9,2);
declare variable FORAGESCREENING numeric(9,2);
declare variable SCREENINGFORAGE numeric(9,2);
declare variable FORAGEGRAINUNIT integer;
declare variable GRAINUNITFORAGE integer;
declare variable FORAGESMALLGRAIN numeric(9,2);
declare variable SMALLGRAINFORAGE numeric(9,2);
declare variable W_FORAGE1C_A integer;
declare variable W_FORAGE1C_B integer;
declare variable W_FORAGE2C integer;
declare variable W_FORAGE3C integer;
declare variable W_SMALLGRAIN integer;
declare variable W_BYPRODUCT_A integer;
declare variable W_BYPRODUCT_B integer;
declare variable W_BYPRODUCT_D integer;
declare variable FORAGE_QUALITYID 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;
  CLEANINGBEGINDATE = null;
  CLEANINGENDDATE = null;

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

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

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

    if (:METODSOLUTION in (64,164)) then
      select sum(WASTEWEIGHT), sum(SCREENINGWEIGHT)
        from (
          select
              case
                when (QUALITY.CLASSID = 41) then
                  IT.NET
              end WASTEWEIGHT,
              case
                when (QUALITY.CLASSID <> 41) then
                  IT.NET
              end SCREENINGWEIGHT
            from IT
              left join QUALITY on (QUALITY.QUALITYID = IT.LABQUALITYID)
              left join CROP on (CROP.CROPID = QUALITY.CROPID)
            where ((IT.UWORDERID = :UWORDERID) and (IT.NET > 0))
              and ((IT.STATE > -1) and ((IT.OPERATIONID = 5) and (CROP.CROPTYPEID = 3)))
        )
      into :SETTLEMENTWASTEWEIGHT, :SETTLEMENTSCREENINGWEIGHT;
  end
  else begin
    select QUALITY_SELECT.QUALITYNUMBER
      from QUALITY_SELECT(:QUALITYBEFOREID)
    into :QUALITYNUMBERBEFORECLEAN;

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

    select
        cast(cast(100 * sum(cast(NET * DIRT / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2)),
        cast(cast(100 * sum(cast(NET * SCREENING / 100 as integer)) as numeric (15,4)) / sum(NET) as numeric (4,2))
      from (
        select
            sum(IT.NET) NET, IT.REGISTERID REGISTERID, IT.RELATIONID UWORDERID,
            min(QUALITY_SELECT.SCREENING) SCREENING, 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.RELATIONID = :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(1, null, :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.RELATIONID, IT.REGISTERID
        order by IT.REGISTERID
      )
    group by UWORDERID
    into :DIRTAFTERCLEAN, :SCREENINGAFTERCLEAN;

  end

  for
    select
        sum(IT.NET) NET, sum(IT.NET) - min(coalesce(ITDRY.NET,0)) WEIGHT,
        sum(ITTRANSPORT.DIRTWEIGHT) DIRTWEIGHT, sum(coalesce(ITTRANSPORT.SCREENWEIGHT,0)) SCREENWEIGHT,
        min(coalesce(DRYQUALITY.HUMIDITY,RQUALITY.HUMIDITY)) L_HUMIDITY, min(LQUALITY.DIRT) L_DIRT,
        min(case
              when (CROP.CROPTYPEID <> 5) then
                LQUALITY.SCREENING
              else LQUALITY.OILSEED
            end) L_SCREENING,
        min(LQUALITY.GRAINUNIT) L_GRAINUNIT, min(LQUALITY.SMALLGRAIN) L_SMALLGRAIN,
        min(coalesce(DRYQUALITY.HUMIDITY,RQUALITY.HUMIDITY)) R_HUMIDITY, min(RQUALITY.DIRT) R_DIRT,
        min(RQUALITY.SCREENING) R_SCREENING, min(RQUALITY.GRAINUNIT) R_GRAINUNIT,
        min(RQUALITY.SMALLGRAIN) R_SMALLGRAIN,
        min(IT.STORAGEID) STORAGEID, min(IT.OWNERID) OWNERID, IT.REGISTERID REGISTERID,
        min(LQUALITY.CROPID) CROPID, min(LQUALITY.CLASSID) CLASSID, min(IT.PARTYID) PARTYID,
        min(IT.INVOICEID), max(IT.CONTRACTID), min(IT.STORINGID) STORINGID
      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)
        left join IT ITDRY on ((ITDRY.REGISTERID = IT.REGISTERID) and (ITDRY.OPERATIONID = 9) and (ITDRY.STATE = -999))
        left join QUALITY_SELECT(ITDRY.OPERATIONPARAM) DRYQUALITY on (0=0)
      where (IT.RELATIONID = :UWORDERID)
        and ((LQUALITY.DIRT > :DIRTAFTERCLEAN) or ((:SCREENINGAFTERCLEAN > 0) and (LQUALITY.SCREENING > :SCREENINGAFTERCLEAN)))
        and (IT.REGDATE between
              (case
                 when (:METODSOLUTION >= 100) then
                   (coalesce ((select min(GWP.BEGINDATE)
                                 from GETWORKPARTIES(1, null, :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, :WEIGHT, :DIRTWEIGHT, :SCREENWEIGHT,
         :HUMIDITYBEFORE, :DIRTBEFORE, :SCREENINGBEFORE, :GRAINUNITBEFORE, :SMALLGRAINBEFORE,
         :HUMIDITYAFTER, :DIRTAFTER, :SCREENINGAFTER, :GRAINUNITAFTER, :SMALLGRAINAFTER,
         :STORAGEID, :OWNERID, :REGISTERID,
         :ACROPID, :ACLASSID, :PARTYID, :INVOICE, :CONTRACTID, :STORINGID

  do begin
    if ((SCREENINGAFTER > SCREENINGBEFORE) or (coalesce(SCREENINGAFTER,0) = 0)) then
      SCREENINGAFTER = SCREENINGBEFORE;

    if (:FINALCALCULATION = 1) then begin
      if (HUMIDITYAFTERCLEAN is not null) then
        HUMIDITYAFTER = :HUMIDITYAFTERCLEAN;

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

      if (SCREENINGAFTERCLEAN is not null) then
        SCREENINGAFTER = :SCREENINGAFTERCLEAN;

      if (GRAINUNITAFTERCLEAN is not null) then
        GRAINUNITAFTER = :GRAINUNITAFTERCLEAN;
    end

    BEFOREWEIGHT = BEFOREWEIGHT + WEIGHT;
    ALLNET = ALLNET + NET;

--    DIRTWEIGHT = round(cast(WEIGHT as numeric(18,4)) * (DIRTBEFORE - DIRTAFTER) / 100);

    ALLDIRTWEIGHT = ALLDIRTWEIGHT + DIRTWEIGHT;

    if ((SCREENWEIGHT = 0) and (SETTLEMENTSCREENINGWEIGHT > 0)) then
      SCREENWEIGHT = DIRTWEIGHT * SETTLEMENTSCREENINGWEIGHT / SETTLEMENTWASTEWEIGHT;

    ALLSCREENWEIGHT = ALLSCREENWEIGHT + SCREENWEIGHT;

    if (WEIGHT < 6000) then begin
      HUMIDITY_BEFORECLEAN = HUMIDITY_BEFORECLEAN + cast(((cast(HUMIDITYBEFORE as numeric(12,1)) * WEIGHT) / 100) as numeric (12,4));
      HUMIDITY_AFTERCLEAN = HUMIDITY_AFTERCLEAN + cast(((cast(HUMIDITYAFTER as numeric(12,1)) * WEIGHT) / 100) as numeric (12,4));
    end
    else begin
      HUMIDITY_BEFORECLEAN = HUMIDITY_BEFORECLEAN + cast((cast(HUMIDITYBEFORE as numeric(12,1)) * WEIGHT) / 100 as integer);
      HUMIDITY_AFTERCLEAN = HUMIDITY_AFTERCLEAN + cast((cast(HUMIDITYAFTER as numeric(12,1)) * WEIGHT) / 100 as integer);
    end

    DIRT_BEFORECLEAN = DIRT_BEFORECLEAN + cast((DIRTBEFORE * WEIGHT) / 100 as integer);
    DIRT_AFTERCLEAN = DIRT_AFTERCLEAN + cast((DIRTAFTER * WEIGHT) / 100 as integer);

    SCREENING_BEFORECLEAN = SCREENING_BEFORECLEAN + cast((SCREENINGBEFORE * WEIGHT) / 100 as integer);
    SCREENING_AFTERCLEAN = SCREENING_AFTERCLEAN + cast((SCREENINGAFTER * WEIGHT) / 100 as integer);

    if ((:FINALCALCULATION = 1) and (DIRTBEFORE > DIRTAFTER)
        and ((not (:METODSOLUTION in (64,164))) or (:SETTLEMENTWASTEWEIGHT is not null))) then begin
      select RESULTQUALITYID
        from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYBEFORE,
            null, null, null, :GRAINUNITBEFORE, null, null, :DIRTBEFORE, :SCREENINGBEFORE,
            null, :SMALLGRAINBEFORE, null, null, null)
      into :BEFORECLEANINGQUALITYID;

      select RESULTQUALITYID
        from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYBEFORE,
            null, null, null, :GRAINUNITBEFORE, null, null, :DIRTAFTER, :SCREENINGAFTER,
            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, :WEIGHT, :WEIGHT - :DIRTWEIGHT - :SCREENWEIGHT, :DIRTWEIGHT,
            :UWORDERID, :BEFORECLEANINGQUALITYID, :BEFORECLEANINGQUALITYID, :UWACTDATE, 4, :SCREENWEIGHT, -999,
            :AFTERCLEANINGQUALITYID, :OWNERID, :STORINGID, :REGISTERID, :PARTYID, :INVOICE, :CONTRACTID);
    end

  end

  if (BEFOREWEIGHT <> 0) then begin
    HUMIDITYBEFORECLEAN = cast(cast(HUMIDITY_BEFORECLEAN * 100 as numeric (12,4)) / BEFOREWEIGHT as numeric (4,2));
    HUMIDITYAFTERCLEAN = cast(cast(HUMIDITY_AFTERCLEAN * 100 as numeric (12,4)) / BEFOREWEIGHT as numeric (4,2));
    DIRTBEFORECLEAN = cast(cast(DIRT_BEFORECLEAN * 100 as numeric (12,4)) / BEFOREWEIGHT as numeric (4,2));
    DIRTAFTERCLEAN = cast(cast(DIRT_AFTERCLEAN * 100 as numeric (12,4)) / BEFOREWEIGHT as numeric (4,2));
    SCREENINGBEFORECLEAN = cast(cast(SCREENING_BEFORECLEAN * 100 as numeric (12,4)) / BEFOREWEIGHT as numeric (4,2));
    SCREENINGAFTERCLEAN = cast(cast(SCREENING_AFTERCLEAN * 100 as numeric (12,4)) / BEFOREWEIGHT as numeric (4,2));
  end
  else begin
    DIRTBEFORECLEAN = null;
    DIRTAFTERCLEAN = null;
    HUMIDITYBEFORECLEAN = null;
    HUMIDITYAFTERCLEAN = null;
    SCREENINGBEFORECLEAN = null;
    SCREENINGAFTERCLEAN = null;
  end

  execute procedure QUALITY_UPDATE(0, :QUALITYBEFOREID, :CROPID, :CLASSID, :SORTID, null, :QUALITYNUMBERBEFORECLEAN,
    :HUMIDITYBEFORECLEAN, null, null, null, :GRAINUNITBEFORECLEAN, null, null, :DIRTBEFORECLEAN, :SCREENINGBEFORECLEAN,
    null, :SMALLGRAINBEFORECLEAN, null, null)
  returning_values :QUALITYBEFOREID;

  execute procedure QUALITY_UPDATE(0, :QUALITYAFTERID, :CROPID, :CLASSID, :SORTID, null, :QUALITYNUMBERAFTERCLEAN,
    :HUMIDITYAFTERCLEAN, null, null, null, :GRAINUNITAFTERCLEAN, null, null, :DIRTAFTERCLEAN, :SCREENINGAFTERCLEAN,
    null, :SMALLGRAINAFTERCLEAN, null, null)
  returning_values :QUALITYAFTERID;

  if ((DIRTBEFORECLEAN > DIRTAFTERCLEAN) and ((not (:METODSOLUTION in (64,164))) or (:SETTLEMENTWASTEWEIGHT is not null))) then
    CLEAN = :DIRTBEFORECLEAN - :DIRTAFTERCLEAN;

  if (:METODSOLUTION in (64,164)) then begin
    -- корректировка отходов по вызенным
    if (:FINALCALCULATION = 1) then
      select UNDERWORK_CALCULATE_WASTE.ALLDIRTWEIGHT, UNDERWORK_CALCULATE_WASTE.ALLSCREENWEIGHT
        from UNDERWORK_CALCULATE_WASTE(:UWORDERID, :UWTYPEID, :METODSOLUTION, :FINALCALCULATION, 0, 0, 0)
      into :ALLDIRTWEIGHT, :ALLSCREENWEIGHT;

/*
    if ((:FINALCALCULATION = 1) and (:METODSOLUTION in (64,164))) then begin
      if (ALLDIRTWEIGHT <> SETTLEMENTWASTEWEIGHT) then begin
        ADIRTWEIGHT = SETTLEMENTWASTEWEIGHT;
        COEFFICIENT = cast((cast(SETTLEMENTWASTEWEIGHT as numeric (15,8)) / ALLDIRTWEIGHT) as numeric (5,4));

        for
          select max(IT.INVENTORYID), sum(IT.NET)
            from IT
            where ((IT.UWORDERID = :UWORDERID) and (IT.NET > 0))
              and ((IT.STATE = -999) and (IT.OPERATIONID = 4))
          group by IT.OWNERID
          into :INVENTORYID, :WEIGHT

        do begin
          ALLDIRTWEIGHT = ALLDIRTWEIGHT - WEIGHT;

          if (ALLDIRTWEIGHT = 0) then
            DELTAWEIGHT = ADIRTWEIGHT - WEIGHT;
          else DELTAWEIGHT = round(WEIGHT * COEFFICIENT) - WEIGHT;

          if (DELTAWEIGHT <> 0) then begin
            select IT.NET, IT.TARE, IT.INVOICEID
              from IT
              where (IT.INVENTORYID = :INVENTORYID)
            into :NET, :TARE, :INVOICE;

            update IT
              set NET = :NET + :DELTAWEIGHT,
                  TARE = :TARE - :DELTAWEIGHT
            where (INVENTORYID = :INVENTORYID);
          end

          ADIRTWEIGHT = ADIRTWEIGHT - WEIGHT - DELTAWEIGHT;

        end

        ALLDIRTWEIGHT = SETTLEMENTWASTEWEIGHT;
      end
    end
*/

    WEIGHT = BEFOREWEIGHT;

    for
      select QUALITY_SELECT.CLASSID, sum(IT.NET),
          sum(IT.NET * QUALITY_SELECT.HUMIDITY) / nullif(sum(IT.NET), 0),
          sum(IT.NET * QUALITY_SELECT.DIRT) / nullif(sum(IT.NET), 0),
          sum(IT.NET * QUALITY_SELECT.SCREENING) / nullif(sum(IT.NET), 0),
          sum(IT.NET * QUALITY_SELECT.GRAINUNIT) / nullif(sum(IT.NET), 0),
          sum(IT.NET * QUALITY_SELECT.SMALLGRAIN) / nullif(sum(IT.NET), 0)
        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 (:METODSOLUTION >= 100) then
                     (coalesce ((select min(GWP.BEGINDATE)
                                   from GETWORKPARTIES(1, null, :CROPID, :CLASSID, :STORAGEID) GWP),:UWORDERDATE))
                   else :UWORDERDATE
                 end)
               and :UWACTDATE)
          and ((IT.STATE > -1) and (IT.STORAGEID = :STORAGEID) and (IT.UWORDERID = :UWORDERID))
        group by QUALITY_SELECT.CLASSID
      into :ACLASSID, :DIRTWEIGHT,
           :FORAGEHUMIDITY, :FORAGEDIRT, :FORAGESCREENING, :FORAGEGRAINUNIT, :FORAGESMALLGRAIN

    do begin
      WEIGHT = WEIGHT - DIRTWEIGHT;

      if (:ACLASSID = 39) then begin -- отходы 1 категори А
        W_FORAGE1C_A = :DIRTWEIGHT;
        select UWACTS.QUALITYID_1C_A
          from UWACTS
          where (UWACTS.UWORDERID = :UWORDERID)
        into :FORAGE_QUALITYID;
      end
      else
        if (:ACLASSID = 68) then begin  -- отходы 1 категории Б
          W_FORAGE1C_B = :DIRTWEIGHT;
          select UWACTS.QUALITYID_1C_B
            from UWACTS
            where (UWACTS.UWORDERID = :UWORDERID)
          into :FORAGE_QUALITYID;
        end
        else
          if (:ACLASSID = 40) then begin -- отходы 2 категории
            W_FORAGE2C = :DIRTWEIGHT;
            select UWACTS.QUALITYID_2C
              from UWACTS
              where (UWACTS.UWORDERID = :UWORDERID)
            into :FORAGE_QUALITYID;
          end
          else
            if (:ACLASSID = 41) then begin  -- отходы 3 категории
              W_FORAGE3C = :DIRTWEIGHT;
              select UWACTS.QUALITYID_3C
                from UWACTS
                where (UWACTS.UWORDERID = :UWORDERID)
              into :FORAGE_QUALITYID;
            end
            else
              if (:ACLASSID = 106) then begin  -- мелкое зерно
                W_SMALLGRAIN = :DIRTWEIGHT;
                select UWACTS.QUALITYID_SMALLGRAIN
                  from UWACTS
                  where (UWACTS.UWORDERID = :UWORDERID)
                into :FORAGE_QUALITYID;
              end
              else
                if (:ACLASSID = 107) then begin -- побочн. продукт А
                  W_BYPRODUCT_A = :DIRTWEIGHT;
                  select UWACTS.QUALITYID_BY_PRODUCT_A
                    from UWACTS
                    where (UWACTS.UWORDERID = :UWORDERID)
                  into :FORAGE_QUALITYID;
                end
                else
                  if (:ACLASSID = 105) then begin  -- побочн. продукт Б
                    W_BYPRODUCT_B = :DIRTWEIGHT;
                    select UWACTS.QUALITYID_BY_PRODUCT_B
                      from UWACTS
                      where (UWACTS.UWORDERID = :UWORDERID)
                    into :FORAGE_QUALITYID;
                  end
                  else
                    if (:ACLASSID = 999) then begin  -- побочн. продукт Д
                      W_BYPRODUCT_D = :DIRTWEIGHT;
                      select UWACTS.QUALITYID_BY_PRODUCT_D
                        from UWACTS
                        where (UWACTS.UWORDERID = :UWORDERID)
                      into :FORAGE_QUALITYID;
                    end
                    else begin
                      W_SMALLGRAIN = :DIRTWEIGHT;
                      select UWACTS.QUALITYID_SMALLGRAIN
                        from UWACTS
                        where (UWACTS.UWORDERID = :UWORDERID)
                      into :FORAGE_QUALITYID;
                    end

      select QUALITY_SELECT.HUMIDITY, QUALITY_SELECT.DIRT, QUALITY_SELECT.SCREENING,
          QUALITY_SELECT.GRAINUNIT, QUALITY_SELECT.SMALLGRAIN, QUALITY_SELECT.QUALITYNUMBER
        from QUALITY_SELECT(:FORAGE_QUALITYID)
          left join CROP on (CROP.CROPID = QUALITY_SELECT.CROPID)
      into :HUMIDITYFORAGE, :DIRTFORAGE, :SCREENINGFORAGE,
           :GRAINUNITFORAGE, :SMALLGRAINFORAGE, :QUALITYNUMBERFORAGE;

      if (:FINALCALCULATION = 1) then begin
        if (HUMIDITYFORAGE is not null) then
          FORAGEHUMIDITY = :HUMIDITYFORAGE;
        if (DIRTFORAGE is not null) then
          FORAGEDIRT = :DIRTFORAGE;
        if (SCREENINGFORAGE is not null) then
          FORAGESCREENING = :SCREENINGFORAGE;
        if (GRAINUNITFORAGE is not null) then
          FORAGEGRAINUNIT = :GRAINUNITFORAGE;
        if (SMALLGRAINFORAGE is not null) then
          FORAGESMALLGRAIN = :SMALLGRAINFORAGE;
      end
      else FORAGEHUMIDITY = HUMIDITYAFTERCLEAN;

      execute procedure QUALITY_UPDATE(0, :FORAGE_QUALITYID, :CROPID, :CLASSID, :SORTID, null,
        :QUALITYNUMBERFORAGE, :FORAGEHUMIDITY, null, null, null, :FORAGEGRAINUNIT, null, null,
        :FORAGEDIRT, :FORAGESCREENING, null, :FORAGESMALLGRAIN, null, null)
      returning_values :FORAGE_QUALITYID;
    end
  end
  else begin
  end

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

  update UWACTS
    set WEIGHT = :WEIGHT,
        FORAGE1C_A = nullif(:W_FORAGE1C_A,0),
        FORAGE1C_B = nullif(:W_FORAGE1C_B,0),
        FORAGE2C = coalesce(:W_FORAGE2C,0),
        FORAGE3C = coalesce(:W_FORAGE3C,0),
        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),
        CLEAN = :CLEAN,
        AFTERCLEANINGWEIGHT = :WEIGHT
    where (UWORDERID = :UWORDERID);

  suspend;

end

 

     Previous topic Chapter index Next topic