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

UNDERWORK_CALCUWACT

 

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

Параметр

Тип

Описание

UWORDERDATE

DATE

 

UWACTDATE

DATE

 

UWORDERID

INTEGER

 

QUALITYBEFOREID

INTEGER

 

QUALITYAFTERID

INTEGER

 

CROPID

INTEGER

 

CLASSID

INTEGER

 

STORAGE_IN

INTEGER

 

UWTYPEID

INTEGER

 

CLIENTID

INTEGER

 

SORTID

INTEGER

 

FINALCALCULATION

INTEGER

 

 

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

Параметр

Тип

Описание

BEFOREWEIGHT

INTEGER

 

STORAGEID

INTEGER

 

 

Описание

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

 

Определение

CREATE PROCEDURE UNDERWORK_CALCUWACT(
    UWORDERDATE DATE,
    UWACTDATE DATE,
    UWORDERID INTEGER,
    QUALITYBEFOREID INTEGER,
    QUALITYAFTERID INTEGER,
    CROPID INTEGER,
    CLASSID INTEGER,
    STORAGE_IN INTEGER,
    UWTYPEID INTEGER,
    CLIENTID INTEGER,
    SORTID INTEGER,
    FINALCALCULATION INTEGER)
RETURNS (
    BEFOREWEIGHT INTEGER,
    STORAGEID INTEGER)
AS
declare variable RWEIGHT double precision;
declare variable PARTYID integer;
declare variable DIRTBEFORE numeric(9,2);
declare variable SCREENINGBEFORE numeric(9,2);
declare variable DIRTAFTER numeric(9,2);
declare variable SCREENINGAFTER numeric(9,2);
declare variable HUMIDITYBEFORE numeric(9,2);
declare variable HUMIDITYAFTER numeric(9,2);
declare variable GRAINUNITBEFORE integer;
declare variable GRAINUNITAFTER integer;
declare variable SMALLGRAINBEFORE numeric(9,2);
declare variable SMALLGRAINAFTER numeric(9,2);
declare variable FULLWEIGHT double precision;
declare variable AWEIGHT double precision;
declare variable ACLASSID 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_SMALLGRAIN double precision = 0;
declare variable DIRT_SMALLGRAIN numeric(9,2) = 0;
declare variable Q_FORAGE1C_A integer;
declare variable Q_FORAGE1C_B integer;
declare variable Q_FORAGE2C integer;
declare variable Q_FORAGE3C integer;
declare variable Q_BYPRODUCT_A integer;
declare variable Q_BYPRODUCT_B integer;
declare variable Q_SMALLGRAIN integer;
declare variable FORAGEDIRT numeric(9,2);
declare variable FORAGE_DIRT numeric(9,2);
declare variable FORAGESCREENING numeric(9,2);
declare variable FORAGE_SCREENING numeric(9,2);
declare variable FORAGEHUMIDITY numeric(9,2);
declare variable FORAGE_HUMIDITY numeric(9,2);
declare variable FORAGEGRAINUNIT integer;
declare variable FORAGE_GRAINUNIT integer;
declare variable FORAGESMALLGRAIN numeric(9,2);
declare variable FORAGE_SMALLGRAIN numeric(9,2);
declare variable RBEGINDATE date;
declare variable RENDDATE date;
declare variable METODSELECTIONWASTE smallint;
declare variable Q_WASTE integer;
declare variable CLEAN numeric(9,2);
declare variable INVID integer;
declare variable INVOICE integer;
declare variable METODSOLUTION integer;
declare variable SELECTEDWASTE integer;
declare variable WRITING_OFF integer;
declare variable FORAGE_QUALITYID integer;
declare variable FORAGE_QUALITYNUMBER integer;
declare variable QUALITYNUMBERBEFORECLEAN integer;
declare variable QUALITYNUMBERAFTERCLEAN integer;
begin

  select
      coalesce(METODSOLUTION,1), SELECTEDWASTE, coalesce(WRITING_OFF,0)
    from UWTYPES
    where (UWTYPEID = :UWTYPEID)
  into :METODSOLUTION, :SELECTEDWASTE, :WRITING_OFF;

  RWEIGHT = 0;
  RBEGINDATE = null;
  RENDDATE = null;
  CLEAN = 0;
  STORAGEID = :STORAGE_IN;

  select sum(IT.NET), min(IT.REGDATE), max(IT.REGDATE), min(IT.PARTYID) PARTYID,
      sum(IT.NET * SQUALITY.HUMIDITY) / nullif(sum(IT.NET + SQUALITY.HUMIDITY - SQUALITY.HUMIDITY), 0),
      sum(IT.NET * SQUALITY.DIRT) / nullif(sum(IT.NET + SQUALITY.DIRT - SQUALITY.DIRT), 0),
      case
        when (min(CROP.CROPTYPEID) = 5) then
          sum(IT.NET * SQUALITY.OILSEED) / nullif(sum(IT.NET + SQUALITY.OILSEED - SQUALITY.OILSEED), 0)
        else sum(IT.NET * SQUALITY.SCREENING) / nullif(sum(IT.NET + SQUALITY.SCREENING - SQUALITY.SCREENING), 0)
      end,
      sum(IT.NET * SQUALITY.GRAINUNIT) / nullif(sum(IT.NET + SQUALITY.GRAINUNIT - SQUALITY.GRAINUNIT), 0),
      sum(IT.NET * SQUALITY.SMALLGRAIN) / nullif(sum(IT.NET + SQUALITY.SMALLGRAIN - SQUALITY.SMALLGRAIN), 0),
      sum(IT.NET * LQUALITY.HUMIDITY) / nullif(sum(IT.NET + LQUALITY.HUMIDITY - LQUALITY.HUMIDITY), 0),
      sum(IT.NET * LQUALITY.DIRT) / nullif(sum(IT.NET + LQUALITY.DIRT - LQUALITY.DIRT), 0),
      case
        when (min(CROP.CROPTYPEID) = 5) then
          sum(IT.NET * LQUALITY.OILSEED) / nullif(sum(IT.NET + LQUALITY.OILSEED - LQUALITY.OILSEED), 0)
        else sum(IT.NET * LQUALITY.SCREENING) / nullif(sum(IT.NET + LQUALITY.SCREENING - LQUALITY.SCREENING), 0)
      end,
      sum(IT.NET * LQUALITY.GRAINUNIT) / nullif(sum(IT.NET + LQUALITY.GRAINUNIT - LQUALITY.GRAINUNIT), 0),
      sum(IT.NET * LQUALITY.SMALLGRAIN) / nullif(sum(IT.NET + LQUALITY.SMALLGRAIN - LQUALITY.SMALLGRAIN), 0)
    from IT
      left join UWOUTACTS on (UWOUTACTS.INVOICEID = IT.INVOICEID)
      left join QUALITY_SELECT(case
                                 when (UWOUTACTS.RECIPEID is null) then
                                   UWOUTACTS.BEFOREQUALITYID
                                 else IT.SAMPLERQUALITYID
                               end) SQUALITY on (0=0)
      left join QUALITY_SELECT(IT.LABQUALITYID) LQUALITY on (0=0)
      left join CROP on (CROP.CROPID = SQUALITY.CROPID)
    where (IT.REGDATE between :UWORDERDATE and :UWACTDATE) and (IT.OPERATIONID in (7,29,30,31))
      and (UWOUTACTS.INVOICEID is not null) and IT.UWORDERID = :UWORDERID
      and ((IT.STATE > -1) and (IT.PARENTID <= 0))
  into :RWEIGHT, :RBEGINDATE, :RENDDATE, :PARTYID,
       :HUMIDITYBEFORE, :DIRTBEFORE, :SCREENINGBEFORE, :GRAINUNITBEFORE, :SMALLGRAINBEFORE,
       :HUMIDITYAFTER, :DIRTAFTER, :SCREENINGAFTER, :GRAINUNITAFTER, :SMALLGRAINAFTER;

/* ----------------------  */

  if (DIRTBEFORE > DIRTAFTER) then
    CLEAN = :DIRTBEFORE - :DIRTAFTER;

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

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

  execute procedure QUALITY_UPDATE(0, :QUALITYBEFOREID, :CROPID, :CLASSID, :SORTID,
      null, :QUALITYNUMBERBEFORECLEAN, :HUMIDITYBEFORE, null, null, null, :GRAINUNITBEFORE, null, null,
      :DIRTBEFORE, :SCREENINGBEFORE, null, :SMALLGRAINBEFORE, null, null)
  returning_values :QUALITYBEFOREID;

  execute procedure QUALITY_UPDATE(0, :QUALITYAFTERID, :CROPID, :CLASSID, :SORTID,
      null, :QUALITYNUMBERAFTERCLEAN, :HUMIDITYAFTER, null, null, null, :GRAINUNITAfter, null, null,
      :DIRTAFTER, :SCREENINGAFTER, null, :SMALLGRAINAfter, null, null)
  returning_values :QUALITYAFTERID;

  select METODSELECTIONWASTE
    from UWTYPES
    where (UWTYPEID = :UWTYPEID)
  into :METODSELECTIONWASTE;

  if (:METODSELECTIONWASTE is null) then
    exception IT_NO_DELETE;

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

/*
  W_FORAGE1C_A = 0;
  W_FORAGE1C_B = 0;
  W_FORAGE2C = 0;
  W_FORAGE3C = 0;
  W_SMALLGRAIN = 0;
  W_BYPRODUCT_A = 0;
  W_BYPRODUCT_B = 0;
*/

  for
    select QUALITY_SELECT.CLASSID, sum(ITMAIN.NET),
--        sum(ITMAIN.NET * QUALITY_SELECT.DIRT) / nullif(sum(ITMAIN.NET + QUALITY_SELECT.DIRT - QUALITY_SELECT.DIRT), 0),
        cast(cast(sum(QUALITY_SELECT.DIRT * ITMAIN.NET / 100) as integer) * 100 as numeric(9,2)) / sum(ITMAIN.NET),
        sum(ITMAIN.NET * QUALITY_SELECT.SCREENING) / nullif(sum(ITMAIN.NET + QUALITY_SELECT.SCREENING - QUALITY_SELECT.SCREENING), 0),
        sum(ITMAIN.NET * QUALITY_SELECT.HUMIDITY) / nullif(sum(ITMAIN.NET + QUALITY_SELECT.HUMIDITY - QUALITY_SELECT.HUMIDITY), 0),
        sum(ITMAIN.NET * QUALITY_SELECT.GRAINUNIT) / nullif(sum(ITMAIN.NET + QUALITY_SELECT.GRAINUNIT - QUALITY_SELECT.GRAINUNIT), 0),
        sum(ITMAIN.NET * QUALITY_SELECT.SMALLGRAIN) / nullif(sum(ITMAIN.NET + QUALITY_SELECT.SMALLGRAIN - QUALITY_SELECT.SMALLGRAIN), 0)
      from IT ITMAIN
        left join QUALITY_SELECT(ITMAIN.LABQUALITYID) on (0=0)
        left join CLASS on (CLASS.CLASSID = QUALITY_SELECT.CLASSID)
      where (ITMAIN.REGDATE between :UWORDERDATE and :UWACTDATE) and (ITMAIN.OPERATIONID in (5,14))
        and (ITMAIN.STATE > -1) and (ITMAIN.STORAGEID = :STORAGEID)
        and ((ITMAIN.PARENTID <= 0) or (0 = (select PARENTID from IT IT2 where IT2.INVENTORYID = ITMAIN.PARENTID)))
--        and (((coalesce(:METODSELECTIONWASTE,0) = 0) and (QUALITY_SELECT.CLASSID in (39,40,41,68,105,106,107)))
/*
        and (((coalesce(:METODSELECTIONWASTE,0) = 0) and (QUALITY_SELECT.CLASSID in (41)))
             or (ITMAIN.UWORDERID = :UWORDERID))
*/
        and (ITMAIN.UWORDERID = :UWORDERID)
    group by QUALITY_SELECT.CLASSID
    into :ACLASSID, :AWEIGHT, :FORAGEDIRT, :FORAGESCREENING, :FORAGEHUMIDITY, :FORAGEGRAINUNIT, :FORAGESMALLGRAIN
  do begin
    if (:ACLASSID = 39) then begin
/*
      if (:METODSELECTIONWASTE = 0) then
        AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);

      select RESULTQUALITYID
        from QUALITY_INSERT(null, 9, 39, null, null, null, :FORAGEHUMIDITY,
            null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREENING,
            null, :FORAGESMALLGRAIN, null, null, null)
      into :FORAGE_QUALITYID;
*/
      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
/*
        if (:METODSELECTIONWASTE = 0) then
          AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);

        select RESULTQUALITYID
          from QUALITY_INSERT(null, 9, 68, null, null, null, :FORAGEHUMIDITY,
              null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREENING,
              null, :FORAGESMALLGRAIN, null, null, null)
        into :FORAGE_QUALITYID;
*/
        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
/*
          if (:METODSELECTIONWASTE = 0) then
            AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);

          select RESULTQUALITYID
            from QUALITY_INSERT(null, 9, 40, null, null, null, :FORAGEHUMIDITY,
                null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREENING,
                null, :FORAGESMALLGRAIN, null, null, null)
          into :FORAGE_QUALITYID;
*/
          select UWACTS.QUALITYID_2C
            from UWACTS
            where (UWACTS.UWORDERID = :UWORDERID)
          into :FORAGE_QUALITYID;

          W_FORAGE2C = :AWEIGHT;
          DIRT_FORAGE2C = :FORAGEDIRT;
        end
        else
          if (:ACLASSID = 41) then begin
/*
            if (:METODSELECTIONWASTE = 0) then
              AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);
*/
            select UWACTS.QUALITYID_3C
              from UWACTS
              where (UWACTS.UWORDERID = :UWORDERID)
            into :FORAGE_QUALITYID;

            if (:METODSELECTIONWASTE > 0) then
              W_FORAGE3C = :AWEIGHT;

            DIRT_FORAGE3C = :FORAGEDIRT;
          end
          else
           if (:ACLASSID = 107) then begin
/*
            if (:METODSELECTIONWASTE = 0) then
              AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);

              select RESULTQUALITYID
                from QUALITY_INSERT(null, 9, 107, null, null, null, :FORAGEHUMIDITY,
                    null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREENING,
                    null, :FORAGESMALLGRAIN, null, null, null)
              into :FORAGE_QUALITYID;
*/
              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 = 105) then begin
/*
                if (:METODSELECTIONWASTE = 0) then
                  AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);
                  select RESULTQUALITYID
                    from QUALITY_INSERT(null, 9, 105, null, null, null, :FORAGEHUMIDITY,
                        null, null, null, :FORAGEGRAINUNIT, null, null, :FORAGEDIRT, :FORAGESCREENING,
                        null, :FORAGESMALLGRAIN, null, null, null)
                  into :FORAGE_QUALITYID;
*/
                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 begin
                if (:METODSELECTIONWASTE = 0) then
                  AWEIGHT = cast((:RWEIGHT * :AWEIGHT / :FULLWEIGHT) as integer);

                W_SMALLGRAIN = :AWEIGHT;
                DIRT_SMALLGRAIN = :FORAGEDIRT;

                select RESULTQUALITYID
                  from QUALITY_INSERT(null,
                      (select CROPID from CLASS where (CLASSID = :ACLASSID)),
                      :ACLASSID, null, null, null, :FORAGEHUMIDITY, null, null, null, :FORAGEGRAINUNIT,
                      null, null, :FORAGEDIRT, :FORAGESCREENING, null, :FORAGESMALLGRAIN, null, null, null)
                into :FORAGE_QUALITYID;
              end

    if (:AWEIGHT > 0) then begin
      select DIRT, SCREENING, HUMIDITY, GRAINUNIT, SMALLGRAIN, QUALITYNUMBER
        from QUALITY_SELECT(:FORAGE_QUALITYID)
      into :FORAGE_DIRT, :FORAGE_SCREENING, :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_SCREENING is not null) then
          FORAGESCREENING = :FORAGE_SCREENING;

        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
        FORAGEHUMIDITY = HUMIDITYBEFORE;
      end

      execute procedure QUALITY_UPDATE(0, :FORAGE_QUALITYID, :CROPID, :CLASSID, :SORTID, null,
        :FORAGE_QUALITYNUMBER, :FORAGEHUMIDITY, null, null, null, :FORAGEGRAINUNIT, null, null,
        :FORAGEDIRT, :FORAGESCREENING, 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, :FORAGESCREENING,
            null, :FORAGESMALLGRAIN, null, null, null)
        into :Q_WASTE;

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

        insert into QUALITYDATA (QUALITYID, QUALITYTYPEID, QUALITYVALUE)
          values (:FORAGE_QUALITYID, 3222, :CLASSID);
                           
        INVOICE = GEN_ID(INVOICEIDGEN, 1);

        insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID, REGDATE,
            OPERATIONID, WEIGHT, STATE, OPERATIONPARAM, OWNERID, INVOICEID)
        values (:STORAGEID, :AWEIGHT, :UWORDERID, :Q_WASTE, :Q_WASTE, :UWACTDATE,
            22, :AWEIGHT, 0, :FORAGE_QUALITYID, :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);

        INVOICE = GEN_ID(INVOICEIDGEN, 1);

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

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

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

  if ((:METODSELECTIONWASTE in (0,4)) and (:W_FORAGE3C = 0)) then begin
    select UWACTS.QUALITYID_3C
      from UWACTS
      where (UWACTS.UWORDERID = :UWORDERID)
    into :FORAGE_QUALITYID;

    update QUALITY
    set QUALITY.CROPID = 9,
        QUALITY.CLASSID = 41
    where (QUALITY.QUALITYID = :FORAGE_QUALITYID);

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

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

    if (FORAGEHUMIDITY is null) then
      FORAGEHUMIDITY = :HUMIDITYBEFORE;

    if (:METODSELECTIONWASTE = 0) then begin
      if ((W_BYPRODUCT_A + W_BYPRODUCT_B + W_FORAGE1C_A + W_FORAGE1C_B + W_FORAGE2C + W_SMALLGRAIN) > 0) then begin
        W_FORAGE3C = trunc(((CLEAN * RWEIGHT) - ((DIRT_BYPRODUCT_A - DIRTAFTER) * W_BYPRODUCT_A)
                                              - ((DIRT_BYPRODUCT_B - DIRTAFTER) * W_BYPRODUCT_B)
                                              - ((DIRT_FORAGE1C_A - DIRTAFTER) * W_FORAGE1C_A)
                                              - ((DIRT_FORAGE1C_B - DIRTAFTER) * W_FORAGE1C_B)
                                              - ((DIRT_FORAGE2C - DIRTAFTER) * W_FORAGE2C)
                                              - ((DIRT_SMALLGRAIN - DIRTAFTER) * W_SMALLGRAIN)
                           ) / (FORAGEDIRT - DIRTAFTER) / METODSOLUTION) * METODSOLUTION;
--      W_FORAGE3C = ((CLEAN * RWEIGHT) - ((DIRT_BYPRODUCT_A - DIRTAFTER) * W_BYPRODUCT_A) / (FORAGEDIRT - DIRTAFTER));
      end
      else begin
        if (:SELECTEDWASTE = 3) then begin
          W_FORAGE3C = round(round(RWEIGHT * cast(CLEAN / (100 - DIRTAFTER) as numeric(9,4))) / METODSOLUTION) * METODSOLUTION;
        end

        execute procedure QUALITY_UPDATE(0, :FORAGE_QUALITYID, 9, 41, null, null,
          :FORAGE_QUALITYNUMBER, :FORAGEHUMIDITY, null, null, null, :FORAGEGRAINUNIT, null, null,
          :FORAGEDIRT, :FORAGESCREENING, null, :FORAGESMALLGRAIN, null, null)
        returning_values :FORAGE_QUALITYID;

        select sum(IT.NET)
          from IT
            left join UWOUTACTS on (UWOUTACTS.INVOICEID = IT.INVOICEID)
          where (IT.REGDATE between :UWORDERDATE and :UWACTDATE) and (IT.OPERATIONID in (7,29,30,31))
            and (IT.STORAGEID = :STORAGEID) and (UWOUTACTS.INVOICEID is not null)
            and ((IT.STATE > -1) and (IT.PARENTID <= 0))
        into :FULLWEIGHT;

        if (:FULLWEIGHT > 0) then begin

--        W_FORAGE1C_A = cast((:RWEIGHT * :W_FORAGE1C_A / :FULLWEIGHT) as integer);
--        W_FORAGE1C_B = cast((:RWEIGHT * :W_FORAGE1C_B / :FULLWEIGHT) as integer);
--        W_FORAGE2C = cast((:RWEIGHT * :W_FORAGE2C / :FULLWEIGHT) as integer);
--        W_FORAGE3C = cast((:RWEIGHT * :W_FORAGE3C / :FULLWEIGHT) as integer);
--        W_SMALLGRAIN = cast((:RWEIGHT * :W_SMALLGRAIN / :FULLWEIGHT) as integer);
--        W_BYPRODUCT_A = cast((:RWEIGHT * :W_BYPRODUCT_A / :FULLWEIGHT) as integer);
--        W_BYPRODUCT_B = cast((:RWEIGHT * :W_BYPRODUCT_B / :FULLWEIGHT) as integer);

        end
      end
    end
    else begin
      select sum(IT.NET)
        from IT
          left join QUALITY on (QUALITY.QUALITYID = IT.LABQUALITYID)
        where (IT.UWORDERID = :UWORDERID) and (IT.OPERATIONID = 4)
          and (IT.STATE > -1) and (IT.STORAGEID = :STORAGEID)
          and (QUALITY.CLASSID = 41)
      into :W_FORAGE3C;
    end
  end

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

  update UWACTS
  set WEIGHT = :RWEIGHT - coalesce(:W_FORAGE1C_A,0) - coalesce(:W_FORAGE1C_B,0) -
        coalesce(:W_FORAGE2C,0) - coalesce(:W_FORAGE3C,0) - coalesce(:W_SMALLGRAIN,0) -
        coalesce(:W_BYPRODUCT_A,0) - coalesce(:W_BYPRODUCT_B,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 = nullif(: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),
/*
      QUALITYID_1C_A = :Q_FORAGE1C_A,
      QUALITYID_1C_B = :Q_FORAGE1C_B,
      QUALITYID_2C = :Q_FORAGE2C,
      QUALITYID_3C = :Q_FORAGE3C
      QUALITYID_SMALLGRAIN = :Q_SMALLGRAIN,
      QUALITYID_BY_PRODUCT_A = :Q_BYPRODUCT_A,
      QUALITYID_BY_PRODUCT_B = :Q_BYPRODUCT_B,
*/
      CLEAN = :CLEAN,
      AFTERCLEANINGWEIGHT = :RWEIGHT - coalesce(:W_FORAGE1C_A,0) - coalesce(:W_FORAGE1C_B,0) -
        coalesce(:W_FORAGE2C,0) - coalesce(:W_FORAGE3C,0) - coalesce(:W_SMALLGRAIN,0) -
        coalesce(:W_BYPRODUCT_A,0) - coalesce(:W_BYPRODUCT_B,0)
  where (UWORDERID = :UWORDERID);

  BEFOREWEIGHT = :RWEIGHT;

/*
  if (:W_SMALLGRAIN > 0) then begin
    select DIRT, SCREENING, HUMIDITY, GRAINUNIT, SMALLGRAIN
      from QUALITY_SELECT(:Q_SMALLGRAIN)
    into :FORAGEDIRT, :FORAGESCREENING, :FORAGEHUMIDITY, :FORAGEGRAINUNIT, :FORAGESMALLGRAIN;

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

    insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID, REGDATE,
        OPERATIONID, WEIGHT, STATE, OPERATIONPARAM, OWNERID)
    values (:STORAGEID, :W_SMALLGRAIN, :UWORDERID, :Q_WASTE, :Q_WASTE, :UWACTDATE,
        22, :W_SMALLGRAIN, 0, :Q_SMALLGRAIN, :CLIENTID);
  end

  if (:W_FORAGE3C > 0) then begin
    select DIRT, SCREENING, HUMIDITY, GRAINUNIT, SMALLGRAIN
      from QUALITY_SELECT(:Q_FORAGE3C)
    into :FORAGEDIRT, :FORAGESCREENING, :FORAGEHUMIDITY, :FORAGEGRAINUNIT, :FORAGESMALLGRAIN;

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

    insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID, REGDATE, OPERATIONID,
        WEIGHT, STATE, OPERATIONPARAM, OWNERID)
    values (:STORAGEID, :W_FORAGE3C, :UWORDERID, :Q_WASTE, :Q_WASTE, :UWACTDATE, 22,
        :W_FORAGE3C, 0, :Q_FORAGE3C, :CLIENTID);
  end

  if (:W_BYPRODUCT_B > 0) then begin
    select DIRT, SCREENING, HUMIDITY, GRAINUNIT, SMALLGRAIN
      from QUALITY_SELECT(:Q_BYPRODUCT_B)
    into :FORAGEDIRT, :FORAGESCREENING, :FORAGEHUMIDITY, :FORAGEGRAINUNIT, :FORAGESMALLGRAIN;

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

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

    insert into QUALITYDATA (QUALITYID, QUALITYTYPEID, QUALITYVALUE)
      values (:Q_WASTE, 3222, :CLASSID);
                           
    insert into IT (STORAGEID, NET, UWORDERID, LABQUALITYID, SAMPLERQUALITYID, REGDATE, OPERATIONID,
        WEIGHT, STATE, OPERATIONPARAM, OWNERID)
    values (:STORAGEID, :W_BYPRODUCT_B, :UWORDERID, :Q_WASTE, :Q_WASTE, :UWACTDATE, 22,
        :W_BYPRODUCT_B, 0, :Q_BYPRODUCT_B, :CLIENTID);

    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, :W_BYPRODUCT_B, :UWORDERID, :Q_WASTE,
          :Q_WASTE, :UWACTDATE, 4, :W_BYPRODUCT_B, 0, :CLIENTID, :INVOICE);

  end
*/

  suspend;
end

 

     Previous topic Chapter index Next topic