// Схема ценообразования по Тюменской области


var PRICE : Double;

// Функция округления до определенного разряда
function My_Round(X:extended; n:integer):extended;
 begin
   Result:=Round(X * Exp(n * Ln(10))) / Round(Exp(n * Ln(10)));
 end;


//Расчет отпускных цен
procedure Round_Cena;
 begin
    //CENA_ROZN:= Int(CENA_ROZN * 10) / 10;  // округление до 10 коп
    //CENA_ROZN:= Int(CENA_ROZN) // округления до рубля в меньшую сторону

 // схема округления до 50 коп.
    {If (Int(CENA_ROZN * 10) - (Int(CENA_ROZN) * 10)) > 5
    then CENA_ROZN:= Int(CENA_ROZN) + 0.5
    else CENA_ROZN:= Int(CENA_ROZN); }

 // схема округления в зависимости от Цены розн.
    If CENA_ROZN <= 10 then CENA_ROZN:=Int(CENA_ROZN * 10) / 10;
    If (CENA_ROZN > 10) and (CENA_ROZN <= 100) then
      If (Int(CENA_ROZN * 10) - (Int(CENA_ROZN) * 10)) > 5
      then CENA_ROZN:= Int(CENA_ROZN) + 0.5
      else CENA_ROZN:= Int(CENA_ROZN);
    If CENA_ROZN > 100 then cena_rozn:= Int(CENA_ROZN);
 end;


//Расчет отпускных цен
procedure Cena_Raschet;
 begin
    If NACEN_OPT_NDS > 0 then
         CENA_OPT_NDS:= My_Round(CENA_OPT * (1 + NACEN_OPT_NDS / 100), 4)
    else
        CENA_OPT_NDS:= CENA_OPT;
    If (LIVE_PREP=1) then
      begin
        HAND_NACEN:=1;
        CENA_ROZN:= Int((PRICE * (1 + NACEN_OPT_NDS / 100) * NACEN_ROZN / 100  + CENA_OPT_NDS) * 100) / 100;
        Round_Cena;
        If PRICE > 0 then NACEN_ROZN:= My_Round(((CENA_ROZN - CENA_OPT_NDS) / (PRICE * (1 + NACEN_OPT_NDS / 100)) * 100), 3);
      end
    else
      begin
        HAND_NACEN:=0;
        If (NACEN_OPT_NDS = 18) then
           CENA_ROZN:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 10) / 10
        else
           CENA_ROZN:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 10) / 10;
        Round_Cena;
        If CENA_OPT_NDS > 0 then NACEN_ROZN:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
      end;

    CENA_ROZN_CLEAR:= CENA_ROZN - (CENA_OPT * NACEN_OPT_NDS / 100);
 end;

// Расчет сумм
procedure Sum_Raschet;
 begin
    SUM_OPT:= My_Round(CENA_OPT * KOL_ALL, 2);
    SUM_OPT_NDS:= My_Round(CENA_OPT * (1 + NACEN_OPT_NDS / 100) * KOL_ALL, 2);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    SUM_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR * KOL_ALL, 2);
    SUM_PR1:= My_Round(CENA_PR1 * KOL_ALL, 2);
    SUM_PR2:= My_Round(CENA_PR2 * KOL_ALL, 2);
    SUM_PR3:= My_Round(CENA_PR3 * KOL_ALL, 2);
 end;

begin

  If (CENA_OPT > 0) and (CENA_OPT < CENA_IZG) then
    PRICE := CENA_OPT
  else
    PRICE := CENA_IZG;

 Case EDITED_FIELD  of // ВЫБОР РЕДАКТИРУЕМОГО ПОЛЯ

  'KOL_ALL':
   begin
    If (CENA_OPT_NDS = 0) and (CENA_ROZN = 0) then
       NACEN_OPT_NDS:= 10;
    Sum_Raschet;
   end;

  'KOL_RAZB':
   begin
    If KOL_RAZB < 1 then
     begin
       ShowMessage('Поле должно принимать целые значения >= 1');
       KOL_RAZB:= 1;
     end;
    CENA_GR:= My_Round(CENA_GR / KOL_RAZB, 2);
    CENA_IZG:= My_Round(CENA_IZG / KOL_RAZB, 2);
    CENA_OPT:= My_Round(CENA_OPT / KOL_RAZB, 4);
    CENA_OPT_NDS:= My_Round(CENA_OPT_NDS / KOL_RAZB, 4);
    CENA_ROZN:= My_Round(CENA_ROZN / KOL_RAZB, 2);
    CENA_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR / KOL_RAZB, 2);
    CENA_PR1:= My_Round(CENA_PR1 / KOL_RAZB, 4);
    CENA_PR2:= My_Round(CENA_PR2 / KOL_RAZB, 4);
    CENA_PR3:= My_Round(CENA_PR3 / KOL_RAZB, 4);
    Sum_Raschet;
   end;

  'CENA_IZG':
   begin
    If NACEN_OPT > 0 then
         CENA_OPT:= My_Round(CENA_IZG * (1 + NACEN_OPT / 100), 4)
    else
         CENA_OPT:= CENA_IZG;
    If (CENA_GR > 0) and (CENA_GR < CENA_IZG) then
       PRICE := CENA_GR
    else
       PRICE := CENA_IZG;
    Cena_Raschet;
    Sum_Raschet;
   end;

 'CENA_GR':
   begin
    If (CENA_GR > 0) and (CENA_GR < CENA_IZG) then
       PRICE := CENA_GR
    else
       PRICE := CENA_IZG;
    Cena_Raschet;
    Sum_Raschet;
   end;

  'NACEN_OPT':
   begin
    If NACEN_OPT > 0 then
         CENA_OPT:= My_Round(CENA_IZG * (1 + NACEN_OPT / 100), 4)
    else
         CENA_OPT:= CENA_IZG;
    Cena_Raschet;
    Sum_Raschet;
   end;

  'CENA_OPT':
   begin
    If CENA_IZG > 0 then
       NACEN_OPT:= My_Round((CENA_OPT / CENA_IZG - 1) * 100, 3);
    Cena_Raschet;
    Sum_Raschet;
   end;

  'SUM_OPT':
   begin
    If KOL_ALL > 0 then
       CENA_OPT:= My_Round(SUM_OPT / KOL_ALL, 4);
    If NACEN_OPT_NDS > 0 then
         CENA_OPT_NDS:= My_Round(CENA_OPT * (1 + NACEN_OPT_NDS / 100), 4)
    else
         CENA_OPT_NDS:= CENA_OPT;
    SUM_OPT_NDS:= My_Round(CENA_OPT * (1 + NACEN_OPT_NDS / 100) * KOL_ALL, 2);
   end;

  'NACEN_OPT_NDS':
   begin
    If (NACEN_OPT_NDS <> 0) and (NACEN_OPT_NDS <> 10) and (NACEN_OPT_NDS <> 18) then
     begin
         ShowMessage('Поле должно принимать значения 0, 10 или 18');
         NACEN_OPT_NDS:= 0;
     end;
    Cena_Raschet;
    Sum_Raschet;
   end;

  'CENA_OPT_NDS':
   begin
    SUM_OPT_NDS:= My_Round(CENA_OPT * (1 + NACEN_OPT_NDS / 100) * KOL_ALL, 2);
   end;

  'SUM_OPT_NDS':
   begin
    If KOL_ALL > 0 then
       CENA_OPT_NDS:= My_Round(SUM_OPT_NDS / KOL_ALL, 4);
   end;

  'NACEN_ROZN':
   begin

    If (CENA_IZG > CENA_GR) and (CENA_GR > 0) then ShowMessage('Внимание! Цена Изг > Цена ГР'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
    CENA_PR1:=CENA_ROZN;

    If (LIVE_PREP=1) then
      begin
        If (CENA_IZG <= 50) and ((NACEN_ROZN > 27) or (NACEN_ROZN = 0)) then NACEN_ROZN:=27;
        If (CENA_IZG > 50) and (CENA_IZG <= 500) and ((NACEN_ROZN > 25) or (NACEN_ROZN = 0)) then NACEN_ROZN:=25;
        If (CENA_IZG > 500) and ((NACEN_ROZN > 24.3) or (NACEN_ROZN = 0)) then NACEN_ROZN:=24.3;
        If (CENA_IZG <= 50) and (NACEN_OPT > 16) then ShowMessage('Наценка поставщика более 16%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (CENA_IZG > 50) and (CENA_IZG <= 500) and (NACEN_OPT > 15) then ShowMessage('Наценка поставщика более 15%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (CENA_IZG > 500) and (NACEN_OPT > 14.3) then ShowMessage('Наценка поставщика более 14.3%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
      end;
    //else NACEN_ROZN:=35;

    If (LIVE_PREP=1) then
      begin
        If (NACEN_ROZN > 27) and (CENA_IZG <= 50) then
          begin
            ShowMessage('Процент розничной надбавки не больше 27%');
            NACEN_ROZN:= 27;
          end;
        If (NACEN_ROZN > 25) and (CENA_IZG > 50) and (CENA_IZG <= 500) then
          begin
            ShowMessage('Процент розничной надбавки не больше 25%');
            NACEN_ROZN:= 25;
          end;
        If (NACEN_ROZN > 24.3) and (CENA_IZG > 500) then
          begin
            ShowMessage('Процент розничной надбавки не больше 24.3%');
            NACEN_ROZN:= 24.3;
          end;
      end
    else
      begin
        If (LIVE_PREP<>1) AND (NACEN_ROZN > 35) AND (NACEN_OPT_NDS <> 18) then
          begin
            ShowMessage('Процент розничной надбавки превышает 35%');
            NACEN_ROZN:= 35;
          end;
         If (LIVE_PREP<>1) AND (NACEN_ROZN <= 0) AND (NACEN_OPT_NDS <> 18) then  NACEN_ROZN:= 35;
         If (LIVE_PREP<>1) AND (NACEN_ROZN <= 0) AND (NACEN_OPT_NDS = 18) then  NACEN_ROZN:= 50;
      end;

    Cena_Raschet;
    Sum_Raschet;
   end;


  'CENA_ROZN':  //Обратный расчет от цены розничной
   begin
    If (LIVE_PREP=1) then
      begin
        If PRICE > 0 then NACEN_ROZN:= My_Round(((CENA_ROZN - CENA_OPT_NDS) / (PRICE * (1 + NACEN_OPT_NDS / 100)) * 100), 3);
          begin
           If (NACEN_ROZN > 27) and (CENA_IZG<=50) then
              begin
                ShowMessage('Процент розничной надбавки не больше 27%');
                NACEN_ROZN:= 27;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 25) and (CENA_IZG>50) and (CENA_IZG<=500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 25%');
                NACEN_ROZN:= 25;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 24.3) and (CENA_IZG>500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 24.3%');
                NACEN_ROZN:= 24.3;
                Cena_Raschet;
              end;
          end;
      end
    else
      begin
        If CENA_OPT_NDS > 0 then NACEN_ROZN:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
        If (LIVE_PREP<>1) AND (NACEN_ROZN > 35) AND (NACEN_OPT_NDS <> 18) then
          begin
            ShowMessage('Процент розничной надбавки превышает 35%');
            //NACEN_ROZN:= 35;
            //Cena_Raschet;
          end;
          If (LIVE_PREP<>1) AND (NACEN_ROZN <= 0) AND (NACEN_OPT_NDS <> 18) then  NACEN_ROZN:= 35;
          If (LIVE_PREP<>1) AND (NACEN_ROZN <= 0) AND (NACEN_OPT_NDS = 18) then  NACEN_ROZN:= 50;
      end;
    Sum_Raschet;
   end;

  end;    //КОНЕЦ ВЫБОРА РЕДАКТИРУЕМОГО ПОЛЯ

end.



