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

FORM117_TOP

 

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

Параметр

Тип

Описание

BEGDATE

DATE

 

ENDDATE

DATE

 

STORAGEID

INTEGER

 

REPORTTYPE

INTEGER

 

RECIPEID

INTEGER

 

 

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

Параметр

Тип

Описание

QNAME

VARCHAR(20)

 

QVALUE

VARCHAR(5)

 

FACTOR

DOUBLE PRECISION

 

SEMOLINA

DOUBLE PRECISION

 

MEALEXTRA

DOUBLE PRECISION

 

MEAL1

DOUBLE PRECISION

 

MEAL2

DOUBLE PRECISION

 

MEAL3

DOUBLE PRECISION

 

SUMPRODUCTION

DOUBLE PRECISION

 

BRAN

DOUBLE PRECISION

 

FLAKES

DOUBLE PRECISION

 

MEALFEEDING

DOUBLE PRECISION

 

WASTE

DOUBLE PRECISION

 

WASTEUSELES

DOUBLE PRECISION

 

DRY

DOUBLE PRECISION

 

TOTAL

DOUBLE PRECISION

 

TOTALWEIGHT

INTEGER

 

 

Описание

Расчет формы 117 верхняя часть

 

Определение

CREATE PROCEDURE FORM117_TOP(
    BEGDATE DATE,
    ENDDATE DATE,
    STORAGEID INTEGER,
    REPORTTYPE INTEGER,
    RECIPEID INTEGER)
RETURNS (
    QNAME VARCHAR(20),
    QVALUE VARCHAR(5),
    FACTOR DOUBLE PRECISION,
    SEMOLINA DOUBLE PRECISION,
    MEALEXTRA DOUBLE PRECISION,
    MEAL1 DOUBLE PRECISION,
    MEAL2 DOUBLE PRECISION,
    MEAL3 DOUBLE PRECISION,
    SUMPRODUCTION DOUBLE PRECISION,
    BRAN DOUBLE PRECISION,
    FLAKES DOUBLE PRECISION,
    MEALFEEDING DOUBLE PRECISION,
    WASTE DOUBLE PRECISION,
    WASTEUSELES DOUBLE PRECISION,
    DRY DOUBLE PRECISION,
    TOTAL DOUBLE PRECISION,
    TOTALWEIGHT INTEGER)
AS
DECLARE VARIABLE HUMIDITY DOUBLE PRECISION;
DECLARE VARIABLE ASH DOUBLE PRECISION;
DECLARE VARIABLE GRAINUNIT INTEGER;
DECLARE VARIABLE GLASSNESS INTEGER;
DECLARE VARIABLE DIRT DOUBLE PRECISION;
DECLARE VARIABLE SCREENING DOUBLE PRECISION;
DECLARE VARIABLE SMALLGRAIN DOUBLE PRECISION;
DECLARE VARIABLE FACTORHUMIDITY DOUBLE PRECISION;
DECLARE VARIABLE FACTORASH DOUBLE PRECISION;
DECLARE VARIABLE FACTORGRAINUNIT DOUBLE PRECISION;
DECLARE VARIABLE FACTORGLASSNESS DOUBLE PRECISION;
DECLARE VARIABLE FACTORDIRT DOUBLE PRECISION;
DECLARE VARIABLE FACTORSCREENING DOUBLE PRECISION;
DECLARE VARIABLE FACTORSMALLGRAIN DOUBLE PRECISION;
DECLARE VARIABLE BASISSEMOLINA DOUBLE PRECISION;
DECLARE VARIABLE BASISMEALEXTRA DOUBLE PRECISION;
DECLARE VARIABLE BASISMEAL1 DOUBLE PRECISION;
DECLARE VARIABLE BASISMEAL2 DOUBLE PRECISION;
DECLARE VARIABLE BASISMEAL3 DOUBLE PRECISION;
DECLARE VARIABLE BASISSUMPRODUCTION DOUBLE PRECISION;
DECLARE VARIABLE BASISBRAN DOUBLE PRECISION;
DECLARE VARIABLE BASISFLAKES DOUBLE PRECISION;
DECLARE VARIABLE BASISMEALFEEDING DOUBLE PRECISION;
DECLARE VARIABLE BASISWASTE DOUBLE PRECISION;
DECLARE VARIABLE BASISWASTEUSELES DOUBLE PRECISION;
DECLARE VARIABLE BASISDRY DOUBLE PRECISION;
DECLARE VARIABLE SUMSEMOLINA DOUBLE PRECISION;
DECLARE VARIABLE SUMMEALEXTRA DOUBLE PRECISION;
DECLARE VARIABLE SUMMEAL1 DOUBLE PRECISION;
DECLARE VARIABLE SUMMEAL2 DOUBLE PRECISION;
DECLARE VARIABLE SUMMEAL3 DOUBLE PRECISION;
DECLARE VARIABLE SUMSUMPRODUCTION DOUBLE PRECISION;
DECLARE VARIABLE SUMBRAN DOUBLE PRECISION;
DECLARE VARIABLE SUMFLAKES DOUBLE PRECISION;
DECLARE VARIABLE SUMMEALFEEDING DOUBLE PRECISION;
DECLARE VARIABLE SUMWASTE DOUBLE PRECISION;
DECLARE VARIABLE SUMWASTEUSELES DOUBLE PRECISION;
DECLARE VARIABLE SUMDRY DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTSEMOLINA DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTMEALEXTRA DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTMEAL1 DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTMEAL2 DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTMEAL3 DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTSUMPRODUCTION DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTBRAN DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTFLAKES DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTWASTE DOUBLE PRECISION;
DECLARE VARIABLE WEIGHTALL DOUBLE PRECISION;
DECLARE VARIABLE WEIGHT INTEGER;
DECLARE VARIABLE CLASSID INTEGER;
DECLARE VARIABLE MIDIHUMIDITY DOUBLE PRECISION;
DECLARE VARIABLE CE_SEMOLINA DOUBLE PRECISION;
DECLARE VARIABLE CE_MEALEXTRA DOUBLE PRECISION;
DECLARE VARIABLE CE_MEAL1 DOUBLE PRECISION;
DECLARE VARIABLE CE_MEAL2 DOUBLE PRECISION;
DECLARE VARIABLE CE_MEAL3 DOUBLE PRECISION;
DECLARE VARIABLE CE_SUMPRODUCTION DOUBLE PRECISION;
DECLARE VARIABLE CE_BRAN DOUBLE PRECISION;
DECLARE VARIABLE CE_FLAKES DOUBLE PRECISION;
DECLARE VARIABLE CE_MEALFEEDING DOUBLE PRECISION;
DECLARE VARIABLE CE_WASTE DOUBLE PRECISION;
DECLARE VARIABLE CE_WASTEUSELES DOUBLE PRECISION;
DECLARE VARIABLE CE_DRY DOUBLE PRECISION;
DECLARE VARIABLE FEK_SEMOLINA DOUBLE PRECISION;
DECLARE VARIABLE FEK_MEALEXTRA DOUBLE PRECISION;
DECLARE VARIABLE FEK_MEAL1 DOUBLE PRECISION;
DECLARE VARIABLE FEK_MEAL2 DOUBLE PRECISION;
DECLARE VARIABLE FEK_MEAL3 DOUBLE PRECISION;
DECLARE VARIABLE FEK_SUMPRODUCTION DOUBLE PRECISION;
DECLARE VARIABLE FEK_BRAN DOUBLE PRECISION;
DECLARE VARIABLE FEK_FLAKES DOUBLE PRECISION;
DECLARE VARIABLE FEK_MEALFEEDING DOUBLE PRECISION;
DECLARE VARIABLE FEK_WASTE DOUBLE PRECISION;
DECLARE VARIABLE FEK_WASTEUSELES DOUBLE PRECISION;
DECLARE VARIABLE FEK_DRY DOUBLE PRECISION;
DECLARE VARIABLE FEP_SEMOLINA DOUBLE PRECISION;
DECLARE VARIABLE FEP_MEALEXTRA DOUBLE PRECISION;
DECLARE VARIABLE FEP_MEAL1 DOUBLE PRECISION;
DECLARE VARIABLE FEP_MEAL2 DOUBLE PRECISION;
DECLARE VARIABLE FEP_MEAL3 DOUBLE PRECISION;
DECLARE VARIABLE FEP_SUMPRODUCTION DOUBLE PRECISION;
DECLARE VARIABLE FEP_BRAN DOUBLE PRECISION;
DECLARE VARIABLE FEP_FLAKES DOUBLE PRECISION;
DECLARE VARIABLE FEP_MEALFEEDING DOUBLE PRECISION;
DECLARE VARIABLE FEP_WASTE DOUBLE PRECISION;
DECLARE VARIABLE FEP_WASTEUSELES DOUBLE PRECISION;
DECLARE VARIABLE FEP_DRY DOUBLE PRECISION;
DECLARE VARIABLE DRYOPERATION INTEGER;
DECLARE VARIABLE ABEGDATE DATE;
DECLARE VARIABLE AENDDATE DATE;
DECLARE VARIABLE CROPID INTEGER;
begin
/* ReportType = 0 - акт
              = 1 - предварительный
              = 2 - расчетный выход
              = 3 - прогнозные данные
              = 4 - расчетный выход на партию машин (RecipeID - ссылка на ITRelations) */

/* Определение базисов */
  select cropid, semolina, mealextra, meal1, meal2, meal3, waste, bran, flakes, wasteuseless
  from gradebasises
  where gradebasises.storageid = :StorageID and gradebasises.state = 1
  into :CROPID, :basissemolina, :basismealextra, :basismeal1, :basismeal2, :basismeal3,
    :basiswaste, :basisbran, :basisflakes, :basiswasteuseles;
--  basismealfeeding = null;
  basissumproduction = 0;
  if (:basissemolina is not null) then basissumproduction = :basissemolina;
  if (:basismealextra is not null) then basissumproduction = :basissumproduction + :basismealextra;
  if (:basismeal1 is not null) then basissumproduction = :basissumproduction + :basismeal1;
  if (:basismeal2 is not null) then basissumproduction = :basissumproduction + :basismeal2;
  if (:basismeal3 is not null) then basissumproduction = :basissumproduction + :basismeal3;

  if (:CROPID = 3) then basisdry = 0.30;
  else basisdry = 0;

/* Инициалицация весов выработаной продукции */
  weightmealextra = 0;
  weightmeal1 = 0;
  weightmeal2 = 0;
  weightsemolina = 0;
  weightbran = 0;
  weightflakes = 0;
  weightmeal3 = 0;
  weightwaste = 0;
if (:reporttype in (0, 1)) then begin
/* Определение веса выработаной продукции по Мельзаводу */
  weightall = 0;
  for
    select quality_select.classid, sum(it.net)
    from it
      left join quality_select(it.labqualityid) on (0 = 0)
      left join ittransport on it.inventoryid = ittransport.inventoryid
    where it.operationid = 5
      and ittransport.sampleroutdate between :begdate and :enddate
      and it.storageid = :storageid
      and it.state > -1
      and it.parentid <= 0
      and it.labqualityid is not null
      and quality_select.classid in (select classid from storingrelation where storingrelation.storageid = :storageid and storingrelation.operationparam = 1 and storingrelation.storingid = 6)
    group by quality_select.classid

    union

    select gradeouts.classid, sum(gradeouts.weight)
    from graderesults
      left join gradeouts on (graderesults.graderesultid = gradeouts.graderesultid)
    where ((graderesults.workingdate between :BegDate and :EndDate
            and graderesults.storingid in (3, 4, 5)
           )
           or graderesults.graderesultid < -1
          )
      and graderesults.storageid = :StorageID
      and gradeouts.classid in (select classid from storingrelation where storingrelation.storageid = :storageid and storingrelation.operationparam = 1 and storingrelation.storingid = graderesults.storingid)
    group by gradeouts.classid

    into :classid, :weight
  do begin
    if (:weight is not null) then begin
      weightall = weightall + :weight;
      if (:classid = 13) then weightmealextra = weightmealextra + :weight;
      if (:classid = 14) then weightmeal1 = weightmeal1 + :weight;
      if (:classid = 15) then weightmeal2 = weightmeal2 + :weight;
      if (:classid = 44) then weightsemolina = weightsemolina + :weight;
      if (:classid = 46) then weightbran = weightbran + :weight;
      if (:classid = 47) then weightflakes = weightflakes + :weight;
      if (:classid = 65) then weightmeal3 = weightmeal3 + :weight;
      if (:classid = 66) then weightwaste = weightwaste + :weight;
    end
  end
  weightsumproduction = :weightmealextra + :weightmeal1 + :weightmeal2 + :weightmeal3 + :weightsemolina;
end

/* установка дат поиска midihumidity при прогнозных данных */
  if (:reporttype = 3) then begin
    select recipedate from recipes where recipeid = :recipeid
    into :aenddate;
    abegdate = :aenddate - 10;
  end
  else begin
    abegdate = :begdate;
    aenddate = :enddate;
  end

/* Определение ср.взвеш. показателей Мельзавод*/
--_______________________________________________________________________________________
  select
    cast(sum(humidity * weight) / sum(nullif(weight, 0) + humidity - humidity) as numeric(9,2))
  from FORM117_CALC_MIDI_HUMIDITY(:abegdate, :aenddate, :storageid)
  into :midihumidity;

/* Определение общего веса и качества зерна до очистки */
if (:reporttype in (0, 1, 2)) then begin
  select
    Sum(uwoutacts.weight),
    cast(Sum(quality_select.humidity * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.humidity - quality_select.humidity) as numeric(9,2)) humidity,
    cast(Sum(quality_select.ash * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.ash - quality_select.ash) as numeric(9,2)) ash,
    cast(Sum(quality_select.grainunit * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.grainunit - quality_select.grainunit) as integer) grainunit,
    cast(Sum(quality_select.glassness * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.glassness - quality_select.glassness) as integer) glassness,
    cast(Sum(quality_select.dirt * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.dirt - quality_select.dirt) as numeric(9,2)) dirt,
    cast(Sum(quality_select.screening * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.screening - quality_select.screening) as numeric(9,2)) screening,
    cast(Sum(quality_select.smallgrain * uwoutacts.weight) / Sum(nullif(uwoutacts.weight, 0) + quality_select.smallgrain - quality_select.smallgrain) as numeric(9,2)) smallgrain
  from uwoutacts
    left join quality_select(uwoutacts.afterqualityid) on (0 = 0)
    left join recipes on (recipes.recipeid = uwoutacts.recipeid)
  where uwoutacts.workingdate between :begdate and :enddate
    and uwoutacts.state > -1
    and (:reporttype <> 2 or :recipeid = uwoutacts.recipeid)
    and recipes.storageid = :storageid
  into :totalweight, :humidity, :ash, :grainunit, :glassness, :dirt, :screening, :smallgrain;
end
/* Определение общего веса и качества зерна до очистки при прогнозных данных*/
if (:reporttype = 3) then begin
  select
    Sum(recipesilages.weight),
    cast(Sum(quality_select.humidity * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.humidity - quality_select.humidity) as numeric(9,2)) humidity,
    cast(Sum(quality_select.ash * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.ash - quality_select.ash) as numeric(9,2)) ash,
    cast(Sum(quality_select.grainunit * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.grainunit - quality_select.grainunit) as integer) grainunit,
    cast(Sum(quality_select.glassness * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.glassness - quality_select.glassness) as integer) glassness,
    cast(Sum(quality_select.dirt * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.dirt - quality_select.dirt) as numeric(9,2)) dirt,
    cast(Sum(quality_select.screening * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.screening - quality_select.screening) as numeric(9,2)) screening,
    cast(Sum(quality_select.smallgrain * recipesilages.weight) / Sum(nullif(recipesilages.weight, 0) + quality_select.smallgrain - quality_select.smallgrain) as numeric(9,2)) smallgrain
  from recipes
    left join recipesilages on (recipes.recipeid = recipesilages.recipeid)
    left join quality_select(recipesilages.qualityid) on (0 = 0)
  where recipes.recipeid = :recipeid
  into :totalweight, :humidity, :ash, :grainunit, :glassness, :dirt, :screening, :smallgrain;
end

/* Определение общего веса и качества зерна до очистки при расчете партии машин*/
if (:reporttype = 4) then begin
  select
    Sum(itrelations.weight),
    cast(Sum(quality_select.humidity * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.humidity - quality_select.humidity) as numeric(9,2)) humidity,
    cast(Sum(quality_select.ash * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.ash - quality_select.ash) as numeric(9,2)) ash,
    cast(Sum(quality_select.grainunit * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.grainunit - quality_select.grainunit) as integer) grainunit,
    cast(Sum(quality_select.glassness * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.glassness - quality_select.glassness) as integer) glassness,
    cast(Sum(quality_select.dirt * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.dirt - quality_select.dirt) as numeric(9,2)) dirt,
    cast(Sum(quality_select.screening * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.screening - quality_select.screening) as numeric(9,2)) screening,
    cast(Sum(quality_select.smallgrain * itrelations.weight) / Sum(nullif(itrelations.weight, 0) + quality_select.smallgrain - quality_select.smallgrain) as numeric(9,2)) smallgrain
  from itrelations
    left join it on itrelations.inventoryid = it.inventoryid
    left join quality_select(it.labqualityid) on 0 = 0
  where itrelations.relationid = :recipeid
  into :totalweight, :humidity, :ash, :grainunit, :glassness, :dirt, :screening, :smallgrain;
end

/* Определение коэфициентов влияния качества */

  if (:humidity < 12) then factorhumidity = 1.46;
  else factorhumidity = cast((((14.5 - :humidity) * 100) / 85.5) * 0.5 as numeric(9,2));

  if (:ash > 1.85) then factorash = cast((ash - 1.85) * 18 as numeric(9,2));
  else factorash = 0;

  if (:CROPID = 3) then begin
    if (:grainunit < 700) then factorgrainunit = cast((700 - :grainunit) * 0.05 as numeric(9,2));
    else factorgrainunit = 0;
  end
  else begin
    if (:grainunit < 775) then factorgrainunit = cast((775 - :grainunit) * 0.05 as numeric(9,2));
    else factorgrainunit = 0;
  end

  if (:glassness < 50) then factorglassness = cast((50 - :glassness) * 0.05 as numeric(9,2));
  else factorglassness = 0;

  if (:dirt > 1) then factordirt = cast(:dirt - 1 as numeric(9,2));
  else factordirt = 0;

  if ((:screening + :smallgrain) > 1) then factorscreening = cast((:screening - 1 + :smallgrain) * 0.35 as numeric(9,2));
  else factorscreening = 0;

  if ((:screening + :smallgrain) > 1) then factorsmallgrain = cast((:screening - 1 + :smallgrain) * 0.18 as numeric(9,2));
  else factorsmallgrain = 0;

/* Определение Влажности и коэфициентов влияния качества при Мельзавод 3 */
  if (:CROPID = 3) then begin
    if (:humidity > :midihumidity) then begin /* Усушка */
      if ((:humidity - :midihumidity) > 0.3) then dryoperation = 2;
      else dryoperation = 1;
      humidity = cast((((:midihumidity - :humidity) * 100) / (100 - :midihumidity)) as numeric(9,2));
      if (:humidity < 0) then humidity = - :humidity;

      factorhumidity = :humidity - 0.30;
      if (:factorhumidity < 0) then factorhumidity = - :factorhumidity;
    end
    else begin /* Увлажнение */
      humidity = cast((((:midihumidity - :humidity) * 100) / (100 - :midihumidity)) as numeric(9,2));
      factorhumidity = :humidity + 0.30;
      dryoperation = 0;
    end
  end

/* Инициалицация сум*/
  SumSEMOLINA = 0;
  SumMEALEXTRA = 0;
  SumMEAL1 = 0;
  SumMEAL2 = 0;
  SumMEAL3 = 0;
  SumSUMPRODUCTION = 0;
  SumBRAN = 0;
  SumFLAKES = 0;
  SumMEALFEEDING = 0;
  SumWASTE = 0;
  SumWASTEUSELES = 0;
  SumDRY = 0;

/* Вывод базисов */
  qname = 'Базисный вых';
  qvalue = null;
  factor = null;
  semolina = :basissemolina;
  mealextra = :basismealextra;
  meal1 = :basismeal1;
  meal2 = :basismeal2;
  meal3 = :basismeal3;
  sumproduction = :basissumproduction;
  bran = :basisbran;
  flakes = :basisflakes;
  mealfeeding = :basismealfeeding;
  waste = :basiswaste;
  wasteuseles = :basiswasteuseles;
  dry = :basisdry;
  total = 100;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод влажности */
  qname = 'Влажность';
  qvalue = cast(:humidity as numeric(9,2));
  factor = -:factorhumidity;
  if (:CROPID = 3) then
    if (:factor < 0) then factor = -:factor;

  if (:dryoperation = 2) then factorhumidity = -:factorhumidity;
  sumproduction = cast(:factorhumidity * :basissumproduction / (:basissumproduction + :basisbran) as numeric(9,2));
  semolina = cast(:sumproduction * :basissemolina / (:basissumproduction) as numeric(9,2));
  mealextra = cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
  meal1 = cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
  meal2 = cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
  meal3 = cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Влажность';
    sumproduction = :factorhumidity * :basissumproduction / (:basissumproduction + :basisbran);
    semolina = cast(:sumproduction * :basissemolina / (:basissumproduction) as numeric(9,2));
    mealextra = cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
    meal1 = cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
    meal2 = cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
    meal3 = cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));
    sumproduction = cast(:sumproduction as numeric(9,2));
  end

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = '  Влажность';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end

  bran = :factorhumidity * :basisbran / (:basissumproduction + :basisbran);
  flakes = null;
  mealfeeding = null;
  waste = null;
  wasteuseles = null;
  dry = - :factorhumidity;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод зольности */
  qname = 'Зольность';
  qvalue = :ash;
  factor = :factorash;
  sumproduction = - cast(:factorash as numeric(9,2));
  bran = :factorash;
                
  semolina = :sumproduction * :basissemolina / (:basissumproduction);
  mealextra = :sumproduction * :basismealextra / (:basissumproduction);
  meal1 = :sumproduction * :basismeal1 / (:basissumproduction );
  meal2 = :sumproduction * :basismeal2 / (:basissumproduction );
  meal3 = :sumproduction * :basismeal3 / (:basissumproduction );

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Зольность';
    sumproduction = - (ash - 1.85) * 18;
    semolina = Cast(:sumproduction * :basissemolina / :basissumproduction as numeric(9,2));
    mealextra = Cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
    meal1 = Cast(:sumproduction * :basismeal1 / (:basissumproduction ) as numeric(9,2));
    meal2 = Cast(:sumproduction * :basismeal2 / (:basissumproduction ) as numeric(9,2));
    meal3 = Cast(:sumproduction * :basismeal3 / (:basissumproduction ) as numeric(9,2));
    sumproduction = Cast(:sumproduction as numeric(9,2));
  end

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = '  Зольность';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end

  flakes = null;
  mealfeeding = null;
  waste = null;
  wasteuseles = null;
  dry = null;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод натуры */
  qname = 'Натура';
  qvalue = :grainunit;
  factor = :factorgrainunit;
  bran = :factorgrainunit;
  sumproduction = -cast(:factorgrainunit as numeric(9,2));
  semolina = cast(:sumproduction * :basissemolina / (:basissumproduction) as numeric(9,2));
  mealextra = cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
  meal1 = cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
  meal2 = cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
  meal3 = cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Натура';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end

  flakes = null;
  mealfeeding = null;
  waste = null;
  wasteuseles = null;
  dry = null;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод Стекловидности */
  qname = 'Стекловидн.';
  qvalue = :glassness;
  factor = :factorglassness;
  bran = :factorglassness;
  sumproduction = - cast(:factorglassness as numeric(9,2));

  semolina = cast(:sumproduction * :basissemolina / (:basissumproduction) as numeric(9,2));
  mealextra = cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
  meal1 = cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
  meal2 = cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
  meal3 = cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Стекловидн.';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end

  flakes = null;
  mealfeeding = null;
  waste = null;
  wasteuseles = null;
  dry = null;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод Сорной примеси */
  qname = 'Сорн. прим.';
  qvalue = :dirt;
  factor = :factordirt;
  waste = :factordirt;
  sumproduction = - cast(:factordirt * :basissumproduction / (:basissumproduction + :basisbran) as numeric(9,2));

  semolina = Cast(:sumproduction * :basissemolina / (:basissumproduction ) as numeric(9,2));
  mealextra = Cast(:sumproduction * :basismealextra / (:basissumproduction ) as numeric(9,2));
  meal1 = Cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
  meal2 = Cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
  meal3 = Cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Сорн. прим.';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end

  bran = -:factordirt * :basisbran / (:basissumproduction + :basisbran);
  flakes = null;
  mealfeeding = null;
  wasteuseles = null;
  dry = null;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод Зерновой примеси */
  qname = 'Зерн. прим.';
  qvalue = :screening;
  factor = :factorscreening;
  waste = :factorscreening;
  sumproduction = - cast(:factorscreening * :basissumproduction / (:basissumproduction + :basisbran) as numeric(9,2));

  semolina = Cast(:sumproduction * :basissemolina / (:basissumproduction) as numeric(9,2));
  mealextra = Cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
  meal1 = Cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
  meal2 = Cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
  meal3 = Cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));

  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Зерн. прим.';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end

  bran = -:factorscreening * :basisbran / (:basissumproduction + :basisbran);
  flakes = null;
  mealfeeding = null;
  wasteuseles = null;
  dry = null;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Вывод мелкого зерна */
  qname = 'Мелк. зерно';
  qvalue = :smallgrain;
  factor = :factorsmallgrain;
  bran = :factorsmallgrain;
  sumproduction = -cast(:factorsmallgrain * :basissumproduction / (:basissumproduction) as numeric(9,2));

  semolina = cast(:sumproduction * :basissemolina / (:basissumproduction) as numeric(9,2));
  mealextra = cast(:sumproduction * :basismealextra / (:basissumproduction) as numeric(9,2));
  meal1 = cast(:sumproduction * :basismeal1 / (:basissumproduction) as numeric(9,2));
  meal2 = cast(:sumproduction * :basismeal2 / (:basissumproduction) as numeric(9,2));
  meal3 = cast(:sumproduction * :basismeal3 / (:basissumproduction) as numeric(9,2));
  if (:sumproduction <> semolina + mealextra + meal1 + meal2 + meal3) then begin
    qname = ' Мелк. зерно';
    if (:basismealextra > :basismeal1 and :basismealextra > :basismeal2 and :basismealextra > :basismeal3 and :basismealextra > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :mealextra, :meal1, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :mealextra;
    if (:basismeal1 > :basismealextra and :basismeal1 > :basismeal2 and :basismeal1 > :basismeal3 and :basismeal1 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal1, :mealextra, :meal2, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal1;
    if (:basismeal2 > :basismeal1 and :basismeal2 > :basismealextra and :basismeal2 > :basismeal3 and :basismeal2 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal2, :mealextra, :meal1, :meal3, :semolina, null, null, null, null, null, null) returning_values :meal2;
    if (:basismeal3 > :basismeal1 and :basismeal3 > :basismeal2 and :basismeal3 > :basismealextra and :basismeal3 > :basissemolina)
    then execute procedure form117_calc(:sumproduction, :meal3, :mealextra, :meal1, :meal2, :semolina, null, null, null, null, null, null) returning_values :meal3;
    if (:basissemolina > :basismeal1 and :basissemolina > :basismeal2 and :basissemolina > :basismeal3 and :basissemolina > :basismealextra)
    then execute procedure form117_calc(:sumproduction, :semolina, :mealextra, :meal1, :meal2, :meal3, null, null, null, null, null, null) returning_values :semolina;
  end
  flakes = null;
  mealfeeding = null;
  waste = null;
  wasteuseles = null;
  dry = null;
  total = null;
  suspend;

  if (:semolina is not null) then SumSEMOLINA = :SumSEMOLINA + cast(:semolina as numeric(9, 2));
  if (:mealextra is not null) then SumMEALEXTRA = :SumMEALEXTRA + cast(:mealextra as numeric(9, 2));
  if (:meal1 is not null) then SumMEAL1 = :SumMEAL1 + cast(:meal1 as numeric(9, 2));
  if (:meal2 is not null) then SumMEAL2 = :SumMEAL2 + cast(:meal2 as numeric(9, 2));
  if (:meal3 is not null) then SumMEAL3 = :SumMEAL3 + cast(:meal3 as numeric(9, 2));
  if (:sumproduction is not null) then SumSUMPRODUCTION = :SumSUMPRODUCTION + cast(:sumproduction as numeric(9, 2));
  if (:bran is not null) then SumBRAN = :SumBRAN + cast(:bran as numeric(9, 2));
  if (:flakes is not null) then SumFLAKES = :SumFLAKES + cast(:flakes as numeric(9, 2));
  if (:mealfeeding is not null) then SumMEALFEEDING = :SumMEALFEEDING + cast(:mealfeeding as numeric(9, 2));
  if (:waste is not null) then SumWASTE = :SumWASTE + cast(:waste as numeric(9, 2));
  if (:wasteuseles is not null) then SumWASTEUSELES = :SumWASTEUSELES + cast(:wasteuseles as numeric(9, 2));
  if (:dry is not null) then SumDRY = :SumDRY + cast(:dry as numeric(9, 2));
/* Отступ */
  qname = null;
  qvalue = null;
  factor = null;
  semolina = :SumSEMOLINA - :basissemolina;
  mealextra = :summealextra - :basismealextra;
  meal1 = :summeal1 - :basismeal1;
  meal2 = :summeal2 - :basismeal2;
  meal3 = :summeal3 - :basismeal3;
  sumproduction = :sumsumproduction - :basissumproduction;
  bran = :sumbran - :basisbran;
  flakes = :sumflakes - :basisflakes;
  mealfeeding = :summealfeeding - :basismealfeeding;
  waste = :sumwaste - :basiswaste;
  wasteuseles = :sumwasteuseles - :basiswasteuseles;
  dry = :sumdry - :basisdry;
  total = null;
  suspend;

/* Расчетный выход проц.*/
  qname = 'Расчетный';
  qvalue = 'проц.';
  factor = null;
  semolina = :sumsemolina;
  mealextra = :summealextra;
  meal1 = :summeal1;
  meal2 = :summeal2;
  meal3 = :summeal3;
  sumproduction = :sumsumproduction;
  bran = :sumbran;
  flakes = :sumflakes;
  mealfeeding = :summealfeeding;
  waste = :sumwaste;
  wasteuseles = :sumwasteuseles;
  dry = :sumdry;
  total = 100;
  suspend;
/* Херня какая-то получается*/
  if (:CROPID = 3) then begin
    if (:dryoperation = 0) then fep_dry = -:humidity;
    else fep_dry = :humidity;
  end
  else fep_dry = cast(((:humidity - :midihumidity) * 100) / (100 - :midihumidity) as numeric(9, 2));

/* При предварительном расчете общий вес находим вот так*/
  if (:reporttype = 1) then begin
/*    if (:cropid = 3) then begin
      if (:dryoperation = 0) then fep_dry = -:humidity;
      else fep_dry = :humidity;
    end
    else fep_dry = cast(((cast(:humidity as numeric(9,2)) - :midihumidity) * 100) / (100 - :midihumidity) as numeric(9, 2));
*/
    totalweight = cast((:weightsumproduction + :weightbran + :weightwaste) / (100 - (0.65 + :fep_dry)) * 100 as Integer);
  end

/* Расчетный выход кг.*/
  qname = 'выход';
  qvalue = 'кг.';
  factor = null;

  CE_semolina = cast(:sumsemolina * :totalweight / 100 as integer);
  CE_mealextra = cast(:summealextra * :totalweight / 100 as integer);
  CE_meal1 = cast(:summeal1 * :totalweight / 100 as integer);
  CE_meal2 = cast(:summeal2 * :totalweight / 100 as integer);
  CE_meal3 = cast(:summeal3 * :totalweight / 100 as integer);

  CE_sumproduction = 0;
  if (:CE_semolina is not null) then CE_sumproduction = :CE_sumproduction + :CE_semolina;
  if (:ce_mealextra is not null) then CE_sumproduction = :CE_sumproduction + :ce_mealextra;
  if (:ce_meal1 is not null) then CE_sumproduction = :CE_sumproduction + :ce_meal1;
  if (:ce_meal2 is not null) then CE_sumproduction = :CE_sumproduction + :ce_meal2;
  if (:ce_meal3 is not null) then CE_sumproduction = :CE_sumproduction + :ce_meal3;

  CE_bran = cast(:sumbran * :totalweight / 100 as integer);
  CE_flakes = cast(:sumflakes * :totalweight / 100 as integer);
  CE_mealfeeding = cast(:summealfeeding * :totalweight / 100 as integer);
  CE_waste = cast(:sumwaste * :totalweight / 100 as integer);
  CE_dry = cast(:sumdry * :totalweight / 100 as integer);
  CE_wasteuseles = cast(:sumwasteuseles * :totalweight / 100 as integer);

  execute procedure form117_calc(:totalweight, :CE_wasteuseles, :CE_semolina, :CE_mealextra, :CE_meal1, :CE_meal2, :CE_meal3, :CE_bran, :CE_flakes, :CE_mealfeeding, :CE_waste, :CE_dry) returning_values :CE_wasteuseles;

  semolina = :CE_semolina;
  mealextra = :CE_mealextra;
  meal1 = :CE_meal1;
  meal2 = :CE_meal2;
  meal3 = :CE_meal3;
  sumproduction = :CE_sumproduction;
  bran = :CE_bran;
  flakes = :CE_flakes;
  mealfeeding = :CE_mealfeeding;
  waste = :CE_waste;
  dry = :CE_dry;
  wasteuseles = :CE_wasteuseles;
  total = :totalweight;
  suspend;


/* Фактический выход проц.*/
  qname = 'Фактический';
  qvalue = 'проц.';
  factor = null;

  fep_sumproduction = cast(:weightsumproduction * 100 / :totalweight as numeric(9,2));
  fep_semolina = cast(:weightsemolina * :fep_sumproduction / :weightsumproduction as numeric(9,2));
  fep_mealextra = cast(:weightmealextra * :fep_sumproduction / :weightsumproduction as numeric(9,2));
  fep_meal1 = cast(:weightmeal1 * :fep_sumproduction / :weightsumproduction as numeric(9,2));
  fep_meal2 = cast(:weightmeal2 * :fep_sumproduction / :weightsumproduction as numeric(9,2));
  fep_meal3 = cast(:weightmeal3 * :fep_sumproduction / :weightsumproduction as numeric(9,2));

  if (:fep_sumproduction <> :fep_semolina + :fep_mealextra + :fep_meal1 + :fep_meal2 + :fep_meal3) then begin
    fep_sumproduction = :weightsumproduction * 100 / :totalweight;
    fep_semolina = cast(:weightsemolina * :fep_sumproduction / :weightsumproduction as numeric(9,2));
    fep_mealextra = cast(:weightmealextra * :fep_sumproduction / :weightsumproduction as numeric(9,2));
    fep_meal1 = cast(:weightmeal1 * :fep_sumproduction / :weightsumproduction as numeric(9,2));
    fep_meal2 = cast(:weightmeal2 * :fep_sumproduction / :weightsumproduction as numeric(9,2));
    fep_meal3 = cast(:weightmeal3 * :fep_sumproduction / :weightsumproduction as numeric(9,2));
    fep_sumproduction = cast(:fep_sumproduction as numeric(9,2));
  end

  if (:fep_sumproduction <> :fep_semolina + :fep_mealextra + :fep_meal1 + :fep_meal2 + :fep_meal3) then begin
    fep_semolina = cast(:weightsemolina * 100 / :totalweight as numeric(9,2));
    fep_mealextra = cast(:weightmealextra * 100 / :totalweight as numeric(9,2));
    fep_meal1 = cast(:weightmeal1 * 100 / :totalweight as numeric(9,2));
    fep_meal2 = cast(:weightmeal2 * 100 / :totalweight as numeric(9,2));
    fep_meal3 = cast(:weightmeal3 * 100 / :totalweight as numeric(9,2));

    fep_sumproduction = 0;
    if (:fep_semolina is not null) then fep_sumproduction = :fep_sumproduction + :fep_semolina;
    if (:fep_mealextra is not null) then fep_sumproduction = :fep_sumproduction + :fep_mealextra;
    if (:fep_meal1 is not null) then fep_sumproduction = :fep_sumproduction + :fep_meal1;
    if (:fep_meal2 is not null) then fep_sumproduction = :fep_sumproduction + :fep_meal2;
    if (:fep_meal3 is not null) then fep_sumproduction = :fep_sumproduction + :fep_meal3;
  end

  fep_bran = cast(:weightbran * 100 / :totalweight as numeric(9,2));
  fep_flakes = null;
  fep_waste = cast(:weightwaste * 100 / :totalweight as numeric(9,2));
  fep_mealfeeding = null;

  fep_wasteuseles =cast(100 - :fep_sumproduction - :fep_bran - :fep_waste - fep_dry as numeric(9,2));

  semolina = :fep_semolina;
  mealextra = :fep_mealextra;
  meal1 = :fep_meal1;
  meal2 = :fep_meal2;
  meal3 = :fep_meal3;
  sumproduction = :fep_sumproduction;
  bran = :fep_bran;
  flakes = :fep_flakes;
  mealfeeding = null;
  waste = :fep_waste;
  dry = :fep_dry;
  wasteuseles = :fep_wasteuseles;
  total = 100;
  suspend;

/* Фактический выход кг.*/
  qname = ' выход';
  qvalue = 'кг.';
  factor = null;

  fek_semolina = cast(:weightsemolina as integer);
  fek_mealextra = cast(:weightmealextra as integer);
  fek_meal1 = cast(:weightmeal1 as integer);
  fek_meal2 = cast(:weightmeal2 as integer);
  fek_meal3 = cast(:weightmeal3 as integer);
  fek_sumproduction = cast(:weightsumproduction as integer);
  fek_bran = cast(:weightbran as integer);
  fek_flakes = cast(:weightflakes as integer);
  fek_mealfeeding = null;
  fek_waste = cast(:weightwaste as integer);
  fek_dry = cast(:fep_dry * :totalweight / 100 as integer);
  fek_wasteuseles = cast(:fep_wasteuseles * :totalweight / 100 as integer);

  execute procedure form117_calc(:totalweight, :fek_wasteuseles, :fek_dry, :fek_waste, :fek_mealfeeding, :fek_flakes, :fek_bran, :fek_meal3, :fek_meal2, :fek_meal1, :fek_mealextra, :fek_semolina) returning_values :fek_wasteuseles;

  semolina = :fek_semolina;
  mealextra = :fek_mealextra;
  meal1 = :fek_meal1;
  meal2 = :fek_meal2;
  meal3 = :fek_meal3;
  sumproduction = :fek_sumproduction;
  bran = :fek_bran;
  flakes = :fek_flakes;
  mealfeeding = null;
  waste = :fek_waste;
  dry = :fek_dry;
  wasteuseles = :fek_wasteuseles;

  total = :totalweight;
  suspend;

/* Отклонение от норм*/
  qname = 'Отклонение';
  qvalue = 'проц.';
  factor = null;

  semolina = :fep_semolina - :sumsemolina;
  mealextra = :fep_mealextra - :summealextra;
  meal1 = :fep_meal1 - :summeal1;
  meal2 = :fep_meal2 - :summeal2;
  meal3 = :fep_meal3 - :summeal3;
  sumproduction =  :fep_sumproduction - :sumsumproduction;

  bran = :fep_bran - :sumbran;
  flakes = null;
  mealfeeding = null;
  waste = :fep_waste - :sumwaste;

  dry = :fep_dry - :sumdry;
  wasteuseles = :fep_wasteuseles - :sumwasteuseles;
  total = null;
  suspend;

/* Отклонение от норм*/
  qname = 'от норм';
  qvalue = 'кг.';
  factor = null;
  semolina = :fek_semolina - :ce_semolina;
  mealextra = :fek_mealextra - :ce_mealextra;
  meal1 = :fek_meal1 - :ce_meal1;
  meal2 = :fek_meal2 - :ce_meal2;
  meal3 =  :fek_meal3 - :ce_meal3;
  sumproduction = :fek_sumproduction - :ce_sumproduction;
  bran = :fek_bran - :ce_bran;
  flakes = :fek_flakes - :ce_flakes;
  mealfeeding = null;
  waste = :fek_waste - :ce_waste;
  dry = :fek_dry - :ce_dry;
  wasteuseles = :fek_wasteuseles - :ce_wasteuseles;

  total = null;
  suspend;
END

 

     Previous topic Chapter index Next topic