Procedura-CalculatePrice

Popis:
Vrací prodejní cenu pro zadané parametry

Parametry:

NázevPopisDatový typ
StorePriceIDChar(10)
PriceDefIDChar(10)
QUnitCodeChar(5)

Návratové hodnoty:

NázevPopisDatový typ
PriceAmountNumeric(15, 5)
OutPriceDefIDChar(10)

Tělo:

BEGIN
  UnitRate = 0;
  Amount = 0;
  PriceAmount = 0;
  MinUnitRate = 0;
  IsFirst = 'A';
  /*rychlost 
  Select SC.MainUnitCode from StoreCards SC where
    SC.ID in (select SP.StoreCard_ID from StorePrices SP where SP.ID = :StorePriceID)
  */  
  Select 
    SC.MainUnitCode 
  from 
    StoreCards SC
    join StorePrices SP on SP.StoreCard_ID = SC.ID
  where
    SP.ID = :StorePriceID
  into :MainUnitCode;
  /* nejdriv to zkusim podle hlavni jednotky */
  select SP2.UnitRate, SP2.Amount from StorePrices2 SP2 where
    (SP2.Parent_ID = :StorePriceID) and (SP2.Price_ID = :PriceDefID) and (SP2.QUnit = :MainUnitCode)
      into :UnitRate, :Amount;
  if ((Amount <> 0) and (UnitRate <> 0)) then begin
    /*rychlost select SU.UnitRate from StoreUnits SU where
      (SU.Parent_ID in (select SP.StoreCard_ID from StorePrices SP where SP.ID = :StorePriceID)) and
        (SU.Code = :QUnitCode)*/
    select 
      SU.UnitRate 
    from 
      StoreUnits SU
      join StoreCards SC on SC.ID = SU.Parent_ID
      join StorePrices SP on SP.StoreCard_ID = SC.ID
    where
      SP.ID = :StorePriceID and SU.Code = :QUnitCode
    into :UnitRate;
    
    PriceAmount = (Amount * UnitRate);
  end else begin
    /* najdu nejmensi unitrate */
    /*rychlost
    for select SP2.UnitRate, SP2.Amount from StorePrices2 SP2 where
      (SP2.Parent_ID = :StorePriceID) and (SP2.Price_ID = :PriceDefID)
        into :UnitRate, :Amount
    do begin
      if ((UnitRate <= MinUnitRate) or (IsFirst = 'A')) then begin
        MinUnitRate = UnitRate;
        UsedAmount = Amount;
        IsFirst = 'N';
      end
    end
    */
    MinUnitRate = 0;
    UsedAmount = 0;
    for select SP2.UnitRate, SP2.Amount from StorePrices2 SP2 where
      (SP2.Parent_ID = :StorePriceID) and (SP2.Price_ID = :PriceDefID)
    order by SP2.UnitRate  
        into :UnitRate, :Amount
    do begin
        MinUnitRate = UnitRate;
        UsedAmount = Amount;
        break;
    end
    if ((MinUnitRate > 0) and (UsedAmount > 0)) then begin
      /* pokud vstupni jednotka neni hlavni */
      /*rychlost
      select SU.UnitRate from StoreUnits SU where
        (SU.Parent_ID in (select SP.StoreCard_ID from StorePrices SP where SP.ID = :StorePriceID)) and
        (SU.Code = :QUnitCode)
        into :UnitRate;
      */   
      select 
        SU.UnitRate 
      from 
        StoreUnits SU
        join StoreCards SC on SC.ID = SU.Parent_ID
        join StorePrices SP on SP.StoreCard_ID = SC.ID
      where
        SP.ID = :StorePriceID and SU.Code = :QUnitCode
      into :UnitRate;
        
      PriceAmount = ((Unitrate / MinUnitRate) * UsedAmount);
    end
  end
  OutPriceDefID =  PriceDefID;
  Suspend;
end;

Generated by ABRA Software a.s. 27.10.2021 16:34:12