CREATE PROCEDURE dbo.InsertWare @AWareName VARCHAR(50), @AWareGroupNumber INT, @ASortNumber INT, @APrice MONEY, @AResultAccountNumber INT, @ABalanceAccountNumber INT, 
  @ABarCodeNumber VARCHAR(15), @ADepartmentCSV VARCHAR(500) AS
SET NOCOUNT ON

/*
  Created by Kent Johansen March 2011
  Used to insert new ware
  
  Changes:
  
*/

IF @AWareName <> '' AND @ADepartmentCSV <> ''
BEGIN
  DECLARE @DepartmentNumber INT
  DECLARE @WareNumber INT
  DECLARE @Idx INT
  
  INSERT INTO Ware (WareGroupNumber, SortNumber, WareName, Price, Arrangement, Payment, ResultAccountNumber, BalanceAccountNumber, StockAccountNumber, CanBeDeleted,
    Note, BarcodeNumber, PriceInPercent, Type, AllowZeroPrice,
    PrintZeroPrice, DiscountValue, IsMenuWare, AllowFilterPicassoOnline,
    ReservationDiscount, BonusPointsPlus, BonusPointsMinus, ManualDiscount, PostForfait, PricePerTimeEnabled)
  VALUES 
    (@AWareGroupNumber, @ASortNumber, @AWareName, @APrice, 0, 0, @AResultAccountNumber, @ABalanceAccountNumber, 0, 1, '', @ABarCodeNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0)

  SELECT @Idx = @@IDENTITY FROM Ware
  
  DECLARE db_cur CURSOR FOR
    SELECT DISTINCT Number FROM iter_intlist_to_table_No_Dummy(@ADepartmentCSV)
   OPEN db_cur
     
   FETCH NEXT FROM db_cur INTO @DepartmentNumber
     
   WHILE @@FETCH_STATUS = 0
   BEGIN
     INSERT INTO WareDepartmentsXtab (WareNumber, FK_Department, Type) VALUES (@Idx, @DepartmentNumber, 1)
     INSERT INTO WareDepartmentsXtab (WareNumber, FK_Department, Type) VALUES (@Idx, @DepartmentNumber, 2)
     
     FETCH NEXT FROM db_cur INTO @DepartmentNumber
   END
   CLOSE db_cur
   DEALLOCATE db_cur
END