// Розничная цена у препаратов ЖНВЛС и не ЖНВЛС рассчитывается от цены изготовителя,
// с пересчетом наценки, с округлением вниз до десятков копеек.


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
 // округление до 5 коп
    {If CENA_ROZN - Int(CENA_ROZN * 10) / 10 > 0.05 then
        CENA_ROZN:= Int(CENA_ROZN * 10) / 10 + 0.05
     else
        CENA_ROZN:= Int(CENA_ROZN * 10) / 10;}
 // округление до 10 коп
    CENA_ROZN:= Int(CENA_ROZN * 10) / 10;
 // округления до 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); }
 // округления до рубля
    {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 (LIVE_PREP=1) then
      begin
        HAND_NACEN:=1;
        CENA_ROZN:= Int((PRICE * NACEN_ROZN / 100 * (1 + NACEN_OPT_NDS / 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
          If (PRICE > 0) then
            begin
              CENA_ROZN:= Int((PRICE * NACEN_ROZN / 100 * (1 + NACEN_OPT_NDS / 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
              CENA_ROZN:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 100) / 100;
              Round_Cena;
              If CENA_OPT_NDS > 0 then NACEN_ROZN:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
            end
        else
          begin
            CENA_ROZN:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 100) / 100;
            Round_Cena;
            If CENA_OPT_NDS > 0 then NACEN_ROZN:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
          end;
      end;
    If CENA_OPT_NDS > 0 then NACEN_PR1:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
    CENA_PR1:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 10) / 10;
    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_NDS * 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) or (CENA_IZG=0)) 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 (CENA_OPT > 0) and ((CENA_OPT < CENA_IZG) or (CENA_IZG=0)) then
      PRICE := CENA_OPT
    else
      PRICE := CENA_IZG;
   end;

  'CENA_GR':
   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);
   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;
    SUM_OPT:= My_Round(CENA_OPT * KOL_ALL, 2);
   end;

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

  'SUM_OPT':
   begin
    If KOL_ALL > 0 then CENA_OPT:= My_Round(SUM_OPT / KOL_ALL, 4);
   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;
    If (CENA_OPT > 0) then
      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_NDS * KOL_ALL, 2);
   end;

  'CENA_OPT_NDS':
   begin
    SUM_OPT_NDS:= My_Round(CENA_OPT_NDS * 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 (live_prep=1) then
   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 (NACEN_OPT_NDS <> 18) then
      begin
        If (CENA_IZG <= 50) {and ((NACEN_ROZN > 35) or (NACEN_ROZN = 0))} then NACEN_ROZN:=35;
        If (CENA_IZG > 50) {and (CENA_IZG <= 500) and ((NACEN_ROZN > 30) or (NACEN_ROZN = 0))} then NACEN_ROZN:=30;
        If (CENA_IZG > 500) {and ((NACEN_ROZN > 20) or (NACEN_ROZN = 0))} then NACEN_ROZN:=20;
        If (CENA_IZG <= 50) and (NACEN_OPT > 17) then ShowMessage('Наценка поставщика более 17%'+#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 > 11) then ShowMessage('Наценка поставщика более 11%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
      end;
    //else NACEN_ROZN:=35;

    If (NACEN_OPT_NDS <> 18) then
      begin
        If (NACEN_ROZN > 35) and (CENA_IZG <= 50) then
          begin
            ShowMessage('Процент розничной надбавки не больше 35%');
            NACEN_ROZN:= 35;
          end;
        If (NACEN_ROZN > 30) and (CENA_IZG > 50) and (CENA_IZG <= 500) then
          begin
            ShowMessage('Процент розничной надбавки не больше 30%');
            NACEN_ROZN:= 30;
          end;
        If (NACEN_ROZN > 20) and (CENA_IZG > 500) then
          begin
            ShowMessage('Процент розничной надбавки не больше 20%');
            NACEN_ROZN:= 20;
          end;
      end;
    Cena_Raschet;
    SUM_PR1:= My_Round(CENA_PR1 * KOL_ALL, 2);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    end
     else
   //NACEN_ROZN:= 35;
     Cena_Raschet;
    SUM_PR1:= My_Round(CENA_PR1 * KOL_ALL, 2);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);

   end;


  'CENA_ROZN':  //Обратный расчет от цены розничной
  begin
  if (live_prep=1) then
   begin
    If (NACEN_OPT_NDS<>18) 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 > 35) and (CENA_IZG<=50) then
              begin
                ShowMessage('Процент розничной надбавки не больше 35%');
                //NACEN_ROZN:= 36;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 30) and (CENA_IZG>50) and (CENA_IZG<=500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 30%');
                //NACEN_ROZN:= 27;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 20) and (CENA_IZG>500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 20%');
                //NACEN_ROZN:= 23;
                Cena_Raschet;
              end;
          end;
      end
    else
      If CENA_OPT_NDS > 0 then NACEN_ROZN:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);

    If CENA_OPT_NDS > 0 then NACEN_PR1:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
    CENA_PR1:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 10) / 10;
    CENA_ROZN_CLEAR:= CENA_ROZN - (CENA_OPT * NACEN_OPT_NDS / 100);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    end
    else
      begin
    If (NACEN_OPT_NDS<>18) 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 > 35)  then
              begin
                ShowMessage('Процент розничной надбавки не больше 35%');
                //NACEN_ROZN:= 35;
                Cena_Raschet;
              end;
          end;
      end
    else
      If CENA_OPT_NDS > 0 then NACEN_ROZN:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);

    If CENA_OPT_NDS > 0 then NACEN_PR1:= My_Round((CENA_ROZN / CENA_OPT_NDS - 1) * 100, 3);
    CENA_PR1:= Int(CENA_OPT_NDS * (1 + NACEN_ROZN / 100) * 10) / 10;
    CENA_ROZN_CLEAR:= CENA_ROZN - (CENA_OPT * NACEN_OPT_NDS / 100);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    end
   end;

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

end.



