// Розничная цена у препаратов ЖНВЛС и не ЖНВЛС рассчитывается от цены изготовителя,
// с пересчетом наценки, с округлением
// последнее изменение 08.04.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 Nacen_Recalc;
 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 ((LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС')) then
      begin
        If (PRICE <= 50) then
          If (NACEN_ROZN > 70) then
            begin
              NACEN_ROZN:= 70;
              ShowMessage('Процент розничной надбавки не больше 70%'+#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:= 70;
        If (PRICE > 50) and (PRICE <= 500) then
          If (NACEN_ROZN > 63) then
            begin
              NACEN_ROZN:= 63;
              ShowMessage('Процент розничной надбавки не больше 63%'+#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:= 63;
        If (PRICE > 500) then
          If (NACEN_ROZN > 40) then
            begin
              NACEN_ROZN:= 40;
              ShowMessage('Процент розничной надбавки не больше 40%'+#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 (PRICE <= 50) and (NACEN_OPT > 12.1) then ShowMessage('Наценка поставщика более 12.1%'+#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 > 12.1) then ShowMessage('Наценка поставщика более 12.1%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
        If (PRICE > 500) and (NACEN_OPT > 12.1) then ShowMessage('Наценка поставщика более 12.1%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
      end
    else
      begin
        If (NACEN_OPT_NDS <> 18) then
          If (NACEN_ROZN > 60.8) then
            begin
              //NACEN_ROZN:= 60.8;
              ShowMessage('Процент розничной надбавки не больше 60.8%'+#13#10#13#10#13#10#13#10#13#10#13#10#13#10#13#10);
            end
          else
            begin
              If NACEN_ROZN = 0 then NACEN_ROZN:= 60.8;
            end
        else
            begin
              //If CENA_IZG = 0 then PRICE:= CENA_OPT; // если не лс и цена изг нулевая - берем опт
              If NACEN_ROZN = 0 then NACEN_ROZN:= 60.8;   //  здесь менять наценку на НДС=18%
            end
      end;
    If ((LIVE_PREP = 1) or (NAME_REGTYPE = 'ЖНВЛС')) and (PRICE = 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;
 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
          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 > 0) or (CENA_OPT = 0)) and (CENA_IZG = 0) and (NACEN_OPT_NDS <> 18) then HAND_NACEN:= 1;
    CENA_ROZN_CLEAR:= CENA_ROZN - My_Round(CENA_ROZN - (CENA_ROZN / (1 + NACEN_OPT_NDS / 100)), 2);
 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 CENA_ROZN - Int(CENA_ROZN) > 0.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 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_GR > 0 then
    begin
      if CENA_GR <= CENA_IZG then
        if CENA_GR <= CENA_OPT then PRICE := CENA_GR
                               else PRICE := CENA_OPT
      else
        if CENA_IZG <= CENA_OPT then PRICE := CENA_IZG
                                else PRICE := CENA_OPT;
    end
  else
      If CENA_IZG <= CENA_OPT then PRICE := CENA_IZG
                              else PRICE := CENA_OPT;}

  //If (CENA_GR > 0) and (CENA_GR < CENA_IZG) then
  //    PRICE := CENA_GR
  //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_GR > 0) and (CENA_GR < CENA_IZG) then
    //    PRICE := CENA_GR
    //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);
    //If (CENA_GR > 0) and (CENA_GR < CENA_IZG) then
    //    PRICE := CENA_GR
    //else
      PRICE := CENA_IZG;
   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
    Nacen_Recalc;
    Cena_Raschet;
    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);
   end;


  'CENA_ROZN':  //Обратный расчет от цены розничной
   begin
    If (LIVE_PREP = 1) then HAND_NACEN:=1;
    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 > 70) and (PRICE <= 50) then
              begin
                ShowMessage('Процент розничной надбавки не больше 70%');
                NACEN_ROZN:= 70;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 63) and (PRICE > 50) and (PRICE <= 500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 63%');
                NACEN_ROZN:= 63;
                Cena_Raschet;
              end;
            If (NACEN_ROZN > 40) and (PRICE > 500) then
              begin
                ShowMessage('Процент розничной надбавки не больше 40%');
                NACEN_ROZN:= 40;
                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 (NACEN_OPT_NDS <> 18) then
          begin
            If (NACEN_ROZN > 60.8) then
              begin
                ShowMessage('Процент розничной надбавки не больше 60.8%');
                //NACEN_ROZN:= 60.8;
                //Cena_Raschet;
              end;
          end
        else
          begin
            //If CENA_IZG = 0 then PRICE:= CENA_OPT;
          end;
      end;
    CENA_ROZN_CLEAR:= CENA_ROZN - My_Round(CENA_ROZN - (CENA_ROZN / (1 + NACEN_OPT_NDS / 100)), 2);
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    SUM_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR * KOL_ALL, 2);
   end;


  'NAME_REGTYPE':
   begin
    Nacen_Recalc;
    Cena_Raschet;
    SUM_ROZN:= My_Round(CENA_ROZN * KOL_ALL, 2);
    SUM_ROZN_CLEAR:= My_Round(CENA_ROZN_CLEAR * KOL_ALL, 2);
   end;

  'LIVE_PREP':
   begin
    Nacen_Recalc;
    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.



