ALTER TABLE dbo.SaleVoucher
	DROP CONSTRAINT FK_SaleVoucher_Waiters
GO
ALTER TABLE dbo.SaleVoucher
	DROP CONSTRAINT FK_SaleVoucher_Vouchers
GO
ALTER TABLE dbo.SaleVoucher
	DROP CONSTRAINT FK_SaleVoucher_Customers
GO
ALTER TABLE dbo.SaleVoucher
	DROP CONSTRAINT DF__Transacti__Credi__39E294A9
GO
ALTER TABLE dbo.SaleVoucher
	DROP CONSTRAINT DF_TransactionSale_IsImportant
GO
CREATE TABLE dbo.Tmp_SaleVoucher
	(
	VoucherID uniqueidentifier NOT NULL,
	Floor int NOT NULL,
	BillID nvarchar(10) NOT NULL,
	TableID nvarchar(10) NOT NULL,
	WaiterID uniqueidentifier NOT NULL,
	CustomerID uniqueidentifier NOT NULL,
	PaidStatus int NOT NULL,
	VoidReason nvarchar(50) NULL,
	Printed bit NOT NULL,
	Alarm datetime NULL,
	KotID nvarchar(10) NOT NULL,
	timestamp timestamp NOT NULL
	)  ON [PRIMARY]
GO
IF EXISTS(SELECT * FROM dbo.SaleVoucher)
	 EXEC('INSERT INTO dbo.Tmp_SaleVoucher (VoucherID, Floor, BillID, TableID, WaiterID, CustomerID, PaidStatus, VoidReason, Printed, Alarm, KotID)
		SELECT VoucherID, 1, BillID, TableID, WaiterID, CustomerID, 
CASE WHEN Paid = 0 and void = 0 then 
  1 
else 
  case when paid = 1 and creditcard = 0 then 
    2 
  else
    case when paid = 1 and creditcard = 1 then
      3
    else
      case when void = 1 then
        6
      end
    end
  end 
end as stat, 
VoidReason, Printed, Alarm, KotID 
FROM dbo.SaleVoucher WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.SaleVoucher
GO
EXECUTE sp_rename N'dbo.Tmp_SaleVoucher', N'SaleVoucher', 'OBJECT' 
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
	PK_TransactionSale PRIMARY KEY CLUSTERED 
	(
	VoucherID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE UNIQUE NONCLUSTERED INDEX IX_TransactionSale ON dbo.SaleVoucher
	(
	BillID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
	FK_SaleVoucher_Customers FOREIGN KEY
	(
	CustomerID
	) REFERENCES dbo.Customers
	(
	CustomerID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
	FK_SaleVoucher_Vouchers FOREIGN KEY
	(
	VoucherID
	) REFERENCES dbo.Vouchers
	(
	VoucherID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SaleVoucher ADD CONSTRAINT
	FK_SaleVoucher_Waiters FOREIGN KEY
	(
	WaiterID
	) REFERENCES dbo.Waiters
	(
	WaiterID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO