CREATE FUNCTION dbo.GetDiscountWareInfo ()  
RETURNS @Result TABLE 
(
  DiscountWareNumber INT NOT NULL,
  DiscountResultAccountNumber INT NOT NULL,
  DiscountVatValueWare FLOAT NOT NULL,
  DiscountWareType INT, -- MISValue
  DiscountMISValueType INT
) AS 
BEGIN
 
/*
Changes:
  KJ 20230412: Valuetype wrong for discount if ruuning charge discount as product
  OF 20120601: Ext: Discount Trial.
  OF 20120522: New 
*/

  DECLARE @DiscountWareNumber INT
  DECLARE @DiscountResultAccountNumber INT
  DECLARE @VatValueNumber INT
  DECLARE @DiscountVatValueWare FLOAT	

  INSERT INTO @Result
    SELECT WareNumber, ResultAccountNumber, ISNULL(dbo.GetVatPercent(Accounts.VatValueNumber), 0), WareType,
  CASE 
    WHEN WareType = 1 THEN 5
    WHEN WareType = 2 THEN 4
  END AS MISValueType
  FROM Ware WITH (NOLOCK) 
  INNER JOIN  WareGroups WITH (NOLOCK) ON Ware.WareGroupNumber = WareGroups.WareGroupNumber
  INNER JOIN Accounts WITH (NOLOCK) ON Accounts.AccountNumber = Ware.ResultAccountNumber
  WHERE Ware.Type = 19
  AND Ware.MarkAsDeleted IS NULL
  AND WareGroups.Payment=5 
  AND ((WareGroups.MarkAsDeleted IS NULL) OR (WareGroups.MarkAsDeleted='0')) 

RETURN

END