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

UNDERWORK_CALCUWACT_REGISTERS

 

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

Параметр

Тип

Описание

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

 

 

Описание

Расчет акта вторичной подработки

 

Определение

CREATE PROCEDURE UNDERWORK_CALCUWACT_REGISTERS(
    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 METODSOLUTION integer;
declare variable SELECTEDWASTE integer;
declare variable WRITING_OFF integer;
declare variable ALLNET integer = 0;
declare variable ALLWEIGHT integer = 0;
declare variable ALLDRYINGWEIGHT integer = 0;
declare variable SETTLEMENTWASTEWEIGHT integer = 0;
declare variable CLEANINGBEGINDATE date;
declare variable CLEANINGENDDATE date;
declare variable DRYINGRBEGINDATE date;
declare variable NET integer;
declare variable WEIGHT integer;
declare variable ACLEANINGBEGINDATE date;
declare variable ADRYINGRBEGINDATE date;
declare variable HUMIDITYBEFORE numeric(9,2);
declare variable HUMIDITY_BEFORE numeric(12,4) = 0;
declare variable HUMIDITYAFTER numeric(9,2);
declare variable HUMIDITY_AFTER numeric(12,4) = 0;
declare variable DIRTBEFORE numeric(9,2);
declare variable DIRT_BEFORE numeric(12,4) = 0;
declare variable DIRTAFTER numeric(9,2);
declare variable DIRT_AFTER numeric(12,4) = 0;
declare variable SCREENINGBEFORE numeric(9,2);
declare variable SCREENING_BEFORE numeric(12,4) = 0;
declare variable SCREENINGAFTER numeric(9,2);
declare variable SCREENING_AFTER numeric(12,4) = 0;
declare variable GRAINUNITBEFORE integer;
declare variable SMALLGRAINBEFORE numeric(9,2);
declare variable SCREENING numeric(9,2);
declare variable INVOICEID integer;
declare variable OWNERID integer;
declare variable STORINGID integer;
declare variable ACROPID integer;
declare variable ACLASSID integer;
declare variable REGISTERID integer;
declare variable PARTYID integer;
declare variable INVOICE integer;
declare variable CONTRACTID integer;
declare variable BEFOREDRYINGWEIGHT integer = 0;
declare variable AFTERDRYINGWEIGHT integer = 0;
declare variable DRYING numeric(9,2);
declare variable QUALITYNUMBERBEFOREDRY integer;
declare variable QUALITYNUMBERAFTERDRY integer;
declare variable HUMIDITYBEFOREDRY numeric(9,2);
declare variable HUMIDITYAFTERDRY numeric(9,2);
declare variable DIRTBEFOREDRY numeric(9,2);
declare variable DIRTAFTERDRY numeric(9,2);
declare variable SCREENINGBEFOREDRY numeric(9,2);
declare variable SCREENINGAFTERDRY numeric(9,2);
declare variable GRAINUNITBEFOREDRY integer;
declare variable GRAINUNITAFTERDRY integer;
declare variable SMALLGRAINBEFOREDRY numeric(9,2);
declare variable SMALLGRAINAFTERDRY numeric(9,2);
declare variable BEFORECLEANINGWEIGHT integer = 0;
declare variable CLEAN_D numeric(9,2) = 0;
declare variable CLEAN_S numeric(9,2) = 0;
declare variable QUALITYNUMBERBEFORECLEAN integer;
declare variable QUALITYNUMBERAFTERCLEAN integer;
declare variable HUMIDITYBEFORECLEAN numeric(9,2);
declare variable HUMIDITYAFTERCLEAN numeric(9,2);
declare variable DIRTBEFORECLEAN numeric(9,2);
declare variable DIRTAFTERCLEAN numeric(9,2);
declare variable SCREENINGBEFORECLEAN numeric(9,2);
declare variable SCREENINGAFTERCLEAN numeric(9,2);
declare variable GRAINUNITBEFORECLEAN integer;
declare variable GRAINUNITAFTERCLEAN integer;
declare variable SMALLGRAINBEFORECLEAN numeric(9,2);
declare variable SMALLGRAINAFTERCLEAN 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 FORAGE_QUALITYID 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 WEIGHTBEFOREDRYING integer;
declare variable SUMWEIGHTBEFOREDRYING integer = 0;
declare variable HUMWEIGHT integer;
declare variable SUMHUMWEIGHT integer = 0;
declare variable BEFOREDRYINGQUALITYID integer;
declare variable AFTERDRYINGQUALITYID integer;
declare variable WEIGHTBEFORECLEANING integer;
declare variable DIRTWEIGHT integer;
declare variable SUMDIRTWEIGHT integer = 0;
declare variable SCREENWEIGHT integer;
declare variable SUMSCREENWEIGHT integer = 0;
declare variable WEIGHTAFTERCLEANING integer;
declare variable BEFORECLEANINGQUALITYID integer;
declare variable AFTERCLEANINGQUALITYID integer;
declare variable UUWORDERID integer;
declare variable UWWEIGHT integer;
declare variable ALLDIRTWEIGHT integer = 0;
declare variable ALLSCREENWEIGHT integer = 0;
declare variable DRYINGWEIGHT integer = 0;
begin
  select
      coalesce(METODSOLUTION,0), SELECTEDWASTE, coalesce(WRITING_OFF,0)
    from UWTYPES
    where (UWTYPEID = :UWTYPEID)
  into :METODSOLUTION, :SELECTEDWASTE, :WRITING_OFF;

  if (:FINALCALCULATION = 0) then begin
    select QUALITY_SELECT.QUALITYNUMBER
      from QUALITY_SELECT(:QUALITYBEFOREID)
    into :QUALITYNUMBERBEFORECLEAN;

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

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

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

    select
        min(PARTYID), sum(NET), sum(WEIGHT),
        cast(cast(100 * sum(cast(WEIGHT * HUMIDITY / 100 as integer)) as numeric (15,4)) / sum(WEIGHT) as numeric (4,2)),
        cast(cast(100 * sum(cast(WEIGHT * DIRT / 100 as integer)) as numeric (15,4)) / sum(WEIGHT) as numeric (4,2)),
        cast(cast(100 * sum(cast(WEIGHT * SCREENING / 100 as integer)) as numeric (15,4)) / sum(WEIGHT) as numeric (4,2)),
        cast(cast(100 * sum(cast(WEIGHT * GRAINUNIT / 100 as integer)) as numeric (15,4)) / sum(WEIGHT) as numeric (4,2)),
        cast(cast(100 * sum(cast(WEIGHT * SMALLGRAIN / 100 as integer)) as numeric (15,4)) / sum(WEIGHT) as numeric (4,2)),
        cast(cast(100 * sum(cast(WEIGHT * R_SCREENING / 100 as integer)) as numeric (15,4)) / sum(WEIGHT) as numeric (4,2))
      from (
        select
            PARTYID, NET, NET - WEIGHT WEIGHT,
            HUMIDITY, DIRT, SCREENING, GRAINUNIT, SMALLGRAIN, R_SCREENING
          from (
            select
                min(IT.UWORDERID) UWORDERID, min(IT.PARTYID) PARTYID, sum(IT.NET) NET,
                coalesce(min((select sum(coalesce(ITUW.NET,0) + coalesce(ITUW.WEIGHT,0))
                                from IT ITUW
                                where (ITUW.INVOICEID = IT.INVOICEID) and (ITUW.UWORDERID = IT.UWORDERID)
                                  and (ITUW.STATE = -999)
                              group by ITUW.INVOICEID)),0) WEIGHT,
                min(coalesce(ADQUALITY.HUMIDITY,BQUALITY.HUMIDITY)) HUMIDITY,
                min(coalesce(ACQUALITY.DIRT,BQUALITY.DIRT)) DIRT,
                min(case
                      when (CROP.CROPTYPEID <> 5) then
                        coalesce(ACQUALITY.SCREENING,BQUALITY.SCREENING)
                      else coalesce(ACQUALITY.OILSEED,BQUALITY.OILSEED)
                    end) SCREENING,
                min(ACQUALITY.GRAINUNIT) GRAINUNIT, min(ACQUALITY.SMALLGRAIN) SMALLGRAIN,
                min(RQUALITY.SCREENING) R_SCREENING
              from IT
                left join UWACTS on (UWACTS.UWORDERID = IT.UWORDERID)
                left join QUALITY_SELECT(UWACTS.AFTERQUALITYID) ACQUALITY on (0=0)
                left join QUALITY_SELECT(UWACTS.AFTERDRYQUALITYID) ADQUALITY on (0=0)
                left join CROP on (CROP.CROPID = :CROPID)
                left join INVOICEREGISTERS on (INVOICEREGISTERS.INVOICEREGISTERID = IT.REGISTERID)
                left join QUALITY_SELECT(INVOICEREGISTERS.QUALITYID) RQUALITY on (0=0)
                left join QUALITY_SELECT(IT.BUHQUALITYID) BQUALITY on (0=0)
              where (IT.SUWORDERID = :UWORDERID)
                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 (IT.REGISTERID is not null)
                and ((IT.OPERATIONID = 1) and (IT.STATE > -1) and (IT.PARENTID <= 0))
            group by IT.REGISTERID
          )
      )
    into :PARTYID, :ALLNET, :BEFOREWEIGHT, :HUMIDITYBEFOREDRY, :DIRTBEFORECLEAN,
         :SCREENINGBEFORECLEAN, :GRAINUNITBEFORE, :SMALLGRAINBEFORE,:SCREENING;

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

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

    if (HUMIDITYBEFOREDRY > 0.2) then
      HUMIDITYAFTERDRY = HUMIDITYBEFOREDRY - 0.2;
    else HUMIDITYAFTERDRY = HUMIDITYBEFORE;

    if (DIRTBEFORECLEAN > 0.3) then
      DIRTAFTERCLEAN = DIRTBEFORECLEAN - 0.3;
    else DIRTAFTERCLEAN = DIRTBEFORECLEAN;

    if ((SCREENING <> 0) and (SCREENINGBEFORECLEAN > 0.4)) then
      SCREENINGAFTERCLEAN = SCREENINGBEFORECLEAN - 0.4;
    else SCREENINGAFTERCLEAN = SCREENINGBEFORECLEAN;

    DIRTBEFOREDRY = DIRTBEFORECLEAN;
    DIRTAFTERDRY = DIRTBEFORECLEAN;
    SCREENINGBEFOREDRY = SCREENINGBEFORECLEAN;
    SCREENINGAFTERDRY = SCREENINGBEFORECLEAN;

    HUMIDITYBEFORECLEAN = HUMIDITYAFTERDRY;
    HUMIDITYAFTERCLEAN = HUMIDITYAFTERDRY;

/*
    if (:FINALCALCULATION = 1) then begin

    end
*/



--  end

/*
    BEFOREDRYINGWEIGHT = BEFOREWEIGHT;
    DRYING = (100 * (:HUMIDITYBEFOREDRY - :HUMIDITYAFTERDRY)) / (100 - :HUMIDITYAFTERDRY);
    ALLDRYINGWEIGHT = round(BEFOREDRYINGWEIGHT * DRYING / 100);
    AFTERDRYINGWEIGHT = BEFOREDRYINGWEIGHT - ALLDRYINGWEIGHT;
    BEFORECLEANINGWEIGHT = BEFOREWEIGHT - ALLDRYINGWEIGHT;
*/
  end
  else begin
    select UWORDERS.WEIGHT
      from UWORDERS
      where (UWORDERS.UWORDERID = :UWORDERID)
    into :BEFOREWEIGHT;

    delete from IT
      where (IT.OPERATIONID in (4,9)) 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;

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

    select QUALITY_SELECT.QUALITYNUMBER, QUALITY_SELECT.HUMIDITY,
        QUALITY_SELECT.DIRT, QUALITY_SELECT.SCREENING, QUALITY_SELECT.GRAINUNIT
      from QUALITY_SELECT(:AFTERDRYQUALITYID)
    into :QUALITYNUMBERAFTERDRY, :HUMIDITYAFTERDRY, :DIRTAFTERDRY, :SCREENINGAFTERDRY, :GRAINUNITAFTERDRY;

    if (:METODSOLUTION in (54,154)) then begin
      DIRTBEFOREDRY = DIRTBEFORECLEAN;
      SCREENINGBEFOREDRY = SCREENINGAFTERCLEAN;
      HUMIDITYBEFORECLEAN = HUMIDITYAFTERDRY;
    end
    else begin
      HUMIDITYBEFORECLEAN = HUMIDITYBEFOREDRY;
      DIRTBEFOREDRY = DIRTAFTERCLEAN;
      SCREENINGBEFOREDRY = SCREENINGBEFORECLEAN;
    end

    DIRTAFTERDRY = DIRTBEFOREDRY;
    SCREENINGAFTERDRY = SCREENINGBEFOREDRY;
    HUMIDITYAFTERCLEAN = HUMIDITYBEFORECLEAN;

    for
      select
          min(INVOICEID), REGISTERID, OWNERID, sum(NET), min(STORAGEID),
          sum(NET) - sum(WEIGHT), min(STORINGID), min(CROPID), min(CLASSID),
          min(PARTYID), max(CONTRACTID)
        from (
          select
              IT.INVOICEID INVOICEID, IT.REGISTERID REGISTERID, IT.OWNERID OWNERID,
              sum(IT.NET) NET, min(IT.STORAGEID) STORAGEID,
              coalesce(min((select sum(coalesce(ITUW.NET,0) + coalesce(ITUW.WEIGHT,0))
                              from IT ITUW
                              where (ITUW.INVOICEID = IT.INVOICEID) and (ITUW.UWORDERID = IT.UWORDERID)
                                and (ITUW.STATE = -999)
                            group by ITUW.INVOICEID)),0) WEIGHT,
              min(IT.STORINGID) STORINGID, min(QUALITY_SELECT.CROPID) CROPID,
              min(QUALITY_SELECT.CLASSID) CLASSID, min(IT.PARTYID) PARTYID,
              max(IT.CONTRACTID) CONTRACTID
            from IT
              left join QUALITY_SELECT(IT.LABQUALITYID) on (0=0)
            where (IT.SUWORDERID = :UWORDERID)
              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 (IT.REGISTERID is not null)
              and ((IT.OPERATIONID = 1) and (IT.STATE > -1) and (IT.PARENTID <= 0))
          group by IT.INVOICEID, IT.REGISTERID, IT.OWNERID
        )
      group by REGISTERID, OWNERID
      into :INVOICEID, :REGISTERID, :OWNERID, :NET, :STORAGEID, :WEIGHT,
           :STORINGID, :ACROPID, :ACLASSID, :PARTYID, :CONTRACTID
    do begin
      if (:METODSOLUTION in (54,154)) then begin
--        WEIGHTBEFORECLEANING = WEIGHT;
        if (HUMIDITYBEFOREDRY > HUMIDITYAFTERDRY) then begin
          BEFOREDRYINGWEIGHT = BEFOREDRYINGWEIGHT + WEIGHT;
          WEIGHTBEFOREDRYING = WEIGHT;
          HUMWEIGHT = round(cast(WEIGHTBEFOREDRYING as numeric (12,6)) * cast(((100 * (HUMIDITYBEFOREDRY - HUMIDITYAFTERDRY)) / (100 - HUMIDITYAFTERDRY)) as numeric(4,2)) / 100);
          AFTERDRYINGWEIGHT = AFTERDRYINGWEIGHT + (WEIGHT - HUMWEIGHT);

          select RESULTQUALITYID
            from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYBEFOREDRY,
                null, null, null, :GRAINUNITBEFOREDRY, null, null, :DIRTBEFORE, :SCREENINGBEFOREDRY,
                null, :SMALLGRAINBEFOREDRY, null, null, null)
          into :BEFOREDRYINGQUALITYID;

          select RESULTQUALITYID
            from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYAFTERDRY,
                null, null, null, :GRAINUNITAFTERDRY, null, null, :DIRTAFTERDRY, :SCREENINGAFTERDRY,
                null, :SMALLGRAINAFTERDRY, 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

        if ((DIRTBEFORECLEAN > DIRTAFTERCLEAN) or (SCREENINGBEFORECLEAN > SCREENINGAFTERCLEAN)) then begin
          if (:METODSOLUTION in (54,154)) then begin
            DIRTWEIGHT = round(cast(WEIGHTBEFORECLEANING as numeric(18,4)) * (DIRTBEFORECLEAN - DIRTAFTERCLEAN) / 100);
          end

          BEFORECLEANINGWEIGHT = BEFORECLEANINGWEIGHT + WEIGHTBEFORECLEANING;
          WEIGHTAFTERCLEANING = WEIGHTBEFORECLEANING - DIRTWEIGHT;
          ALLDIRTWEIGHT = ALLDIRTWEIGHT + DIRTWEIGHT;

          select RESULTQUALITYID
            from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYBEFORECLEAN,
                null, null, null, :GRAINUNITBEFORECLEAN, null, null, :DIRTBEFORECLEAN, :SCREENINGBEFORECLEAN,
                null, :SMALLGRAINBEFORECLEAN, null, null, null)
          into :BEFORECLEANINGQUALITYID;

          select RESULTQUALITYID
            from QUALITY_INSERT(null, :ACROPID, :ACLASSID, :SORTID, null, null, :HUMIDITYAFTERCLEAN,
                null, null, null, :GRAINUNITBEFORECLEAN, null, null, :DIRTAFTERCLEAN, :SCREENINGAFTERCLEAN,
                null, null, 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
      end
    end
  end

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

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

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

  execute procedure QUALITY_UPDATE(2, :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(2, :QUALITYAFTERID, :CROPID, :CLASSID, :SORTID, null,
    :QUALITYNUMBERAFTERCLEAN, :HUMIDITYAFTERCLEAN, null, null, null, :GRAINUNITAFTERCLEAN, null, null,
    :DIRTAFTERCLEAN, :SCREENINGAFTERCLEAN, null, :SMALLGRAINAFTERCLEAN, null, null)
  returning_values :QUALITYAFTERID;

  if (:METODSOLUTION in (54,154)) then begin
-- корректировка отходов по вызенным
    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, nullif(sum(IT.NET),0),
          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
      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 begin
                    W_SMALLGRAIN = :DIRTWEIGHT;
                    select UWACTS.QUALITYID_SMALLGRAIN
                      from UWACTS
                      where (UWACTS.UWORDERID = :UWORDERID)
                    into :FORAGE_QUALITYID;
                  end

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

      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

    if (W_FORAGE3C > 0) then
      CLEAN_D = cast(cast(100 * (:DIRTBEFORECLEAN - :DIRTAFTERCLEAN) as numeric(9,5)) / (100 - :DIRTAFTERCLEAN) as numeric(4,2));
    if (W_FORAGE2C > 0) then
      CLEAN_S = cast(cast(100 * (:SCREENINGBEFORECLEAN - :SCREENINGAFTERCLEAN) as numeric(9,5)) / (100 - :SCREENINGAFTERCLEAN) as numeric(4,2));
  end
  else begin
    CLEAN_D = cast(cast((100 - DRYING) * (:DIRTBEFORECLEAN - :DIRTAFTERCLEAN) as numeric(9,5)) / (100 - :DIRTAFTERCLEAN) as numeric(4,2));
    W_FORAGE3C = round(BEFOREDRYINGWEIGHT * CLEAN_D / 100);
    CLEAN_S = cast(cast((100 - DRYING) * (:SCREENINGBEFORECLEAN - :SCREENINGAFTERCLEAN) as numeric(9,5)) / (100 - :SCREENINGAFTERCLEAN) as numeric(4,2));
    W_FORAGE2C = round(BEFOREDRYINGWEIGHT * CLEAN_S / 100);
  end

--      CLEAN_D = cast(cast(100 * (:DIRTBEFORECLEAN - :DIRTAFTERCLEAN) as numeric(9,5)) / (100 - :DIRTAFTERCLEAN) as numeric(4,2));

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

  update UWACTS
    set WEIGHT = :BEFOREWEIGHT - coalesce(:ALLDIRTWEIGHT,0) - coalesce(:ALLSCREENWEIGHT,0) - coalesce(:DRYINGWEIGHT,0),
        FORAGE1C_A = :W_FORAGE1C_A,
        FORAGE1C_B = :W_FORAGE1C_B,
        FORAGE2C = :W_FORAGE2C,
        FORAGE3C = :W_FORAGE3C,
        FORAGE_SMALLGRAIN = :W_SMALLGRAIN,
        FORAGE_BY_PRODUCT_A = :W_BYPRODUCT_A,
        FORAGE_BY_PRODUCT_B = :W_BYPRODUCT_B,
        DRYING = :DRYING,
        CLEAN = :CLEAN_D + :CLEAN_S,
        AFTERDRYQUALITYID = :AFTERDRYQUALITYID,
        AFTERDRYINGWEIGHT = :AFTERDRYINGWEIGHT,
        AFTERCLEANINGWEIGHT = :BEFORECLEANINGWEIGHT - :ALLDIRTWEIGHT - :ALLSCREENWEIGHT
    where (UWORDERID = :UWORDERID);

  suspend;
end

 

     Previous topic Chapter index Next topic