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


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) or (NAME_REGTYPE='ЖНВЛС') 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;
        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;
    //If ((CENA_OPT > 0) or (CENA_OPT = 0)) and (CENA_IZG = 0) and (NACEN_OPT_NDS <> 18) then HAND_NACEN:= 1;
    //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_PR1 / 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
  //If (CENA_OPT > 0) and (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
    //If (CENA_OPT > 0) and (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 (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);
   // If ((CENA_OPT > 0) or (CENA_OPT = 0)) and (CENA_IZG = 0) and (NACEN_OPT_NDS <> 18) then HAND_NACEN:=1;

    If ((LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС')) then
      begin
        If (PRICE <= 50) then
          If (NACEN_ROZN > 40) then
            begin
              NACEN_ROZN:= 40;
              ShowMessage('Процент розничной надбавки не больше 40%');
            end
          else If NACEN_ROZN = 0 then NACEN_ROZN:= 40;
        If (PRICE > 50) and (PRICE <= 500) then
          If (NACEN_ROZN > 37) then
            begin
              NACEN_ROZN:= 37;
              ShowMessage('Процент розничной надбавки не больше 37%');
            end
          else If NACEN_ROZN = 0 then NACEN_ROZN:= 37;
        If (PRICE > 500) then
          If (NACEN_ROZN > 35) then
            begin
              NACEN_ROZN:= 35;
              ShowMessage('Процент розничной надбавки не больше 35%');
            end
          else If NACEN_ROZN = 0 then NACEN_ROZN:= 35;
        If (PRICE <= 50) and (NACEN_OPT > 25) then ShowMessage('Наценка поставщика более 25%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (PRICE > 50) and (PRICE <= 500) and (NACEN_OPT > 22) then ShowMessage('Наценка поставщика более 22%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (PRICE > 500) and (NACEN_OPT > 20) then ShowMessage('Наценка поставщика более 20%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
      end
    else If (NACEN_ROZN = 0) then NACEN_ROZN:=40;

    If ((LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС')) and (CENA_IZG = 0) then
      begin
        ShowMessage('Внимание! Цена Изг = Цена ГР = 0, ЖНВЛС'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        NACEN_ROZN:= 0;
      end;
    Cena_Raschet;
    //SUM_PR1:= My_Round(CENA_PR1 * KOL_ALL, 2);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    SUM_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR * KOL_ALL, 2);
   end;


  'CENA_ROZN':  //Обратный расчет от цены розничной
   begin
    If (LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС') 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 > 40) and (PRICE <= 50) then
              begin
                ShowMessage('Процент розничной надбавки не больше 40%');
                NACEN_ROZN:= 40;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 37) and (PRICE > 50) and (PRICE <= 500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 37%');
                NACEN_ROZN:= 37;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 35) and (PRICE > 500) 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_PR1 / 100) * 10) / 10;
    CENA_ROZN_CLEAR:= CENA_ROZN - (CENA_OPT * NACEN_OPT_NDS / 100);
    //SUM_PR1:= My_Round(CENA_PR1 * KOL_ALL, 2);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    SUM_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR * KOL_ALL, 2);
   end;

  {'NACEN_PR1':
   begin
    CENA_PR1:= Int(CENA_OPT_NDS * (1 + NACEN_PR1 / 100) * 10) / 10;
    SUM_PR1:= My_Round(CENA_PR1 * KOL_ALL, 2);
   end; }

  'NAME_REGTYPE':
   begin
    If ((LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС')) then
      begin
        If (PRICE <= 50) then
          If (NACEN_ROZN > 40) then
            begin
              NACEN_ROZN:= 40;
              ShowMessage('Процент розничной надбавки не больше 40%');
            end
          else If NACEN_ROZN = 0 then NACEN_ROZN:= 40;
        If (PRICE > 50) and (PRICE <= 500) then
          If (NACEN_ROZN > 37) then
            begin
              NACEN_ROZN:= 37;
              ShowMessage('Процент розничной надбавки не больше 37%');
            end
          else If NACEN_ROZN = 0 then NACEN_ROZN:= 37;
        If (PRICE > 500) then
          If (NACEN_ROZN > 35) then
            begin
              NACEN_ROZN:= 35;
              ShowMessage('Процент розничной надбавки не больше 35%');
            end
          else If NACEN_ROZN = 0 then NACEN_ROZN:= 35;
        If (PRICE <= 50) and (NACEN_OPT > 25) then ShowMessage('Наценка поставщика более 25%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (PRICE > 50) and (PRICE <= 500) and (NACEN_OPT > 22) then ShowMessage('Наценка поставщика более 22%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (PRICE > 500) and (NACEN_OPT > 20) then ShowMessage('Наценка поставщика более 20%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
      end
    else If (NACEN_ROZN = 0) then NACEN_ROZN:=40;

    If ((LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС')) and (CENA_IZG = 0) then
      begin
        ShowMessage('Внимание! Цена Изг = Цена ГР = 0, ЖНВЛС'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        NACEN_ROZN:= 0;
      end;
    Cena_Raschet;
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    SUM_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR * KOL_ALL, 2);
   end;    //КОНЕЦ ВЫБОРА РЕДАКТИРУЕМОГО ПОЛЯ

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

end.



