set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[STPCreateInvoice] 
		@CustomerName	nvarchar(1000),
		@FK_AccountSysCode	int	,
		@Comment	nvarchar(1000),	
		@InvoiceDate	char(8)	,
		@FK_DocsysCode	int	,
		@UserNo	int=null	, --no longer needed
		@amani	bit	,
		@InvoiceNo	int,	
		@VisitorSyscode	int,	
		--@VisitorPer	real	,
		--@VisitorAmount	money,	
		@paymentDate char(8),
		@DeliveredtDate char(8),
		@RecievedDate char(8),
		@Type	int	  -- 1 - sale   2- buy   3-from sale  4-from buy

AS
	declare @Sign int
	select @sign=
		case @Type
		when 1 then -1
		when 3 then 1
		when 2  then 1
		when 4 then -1
		when 5 then -1
		when 6 then 1
	end


if @FK_DocsysCode=0 set @FK_DocsysCode=null
if @VisitorSyscode=0 set @VisitorSyscode=null
if @FK_AccountSysCode=0 set @FK_AccountSysCode=null
INSERT INTO InvoiceTbl
                      (CustomerName, FK_AccountSysCode, Comment, InvoiceDate, FK_DocsysCode,  amani, InvoiceNo, VisitorSyscode, 
                       Sign, Type, RecivedDate, DelivaredDate, PaymentDate)
VALUES     (@CustomerName,@FK_AccountSysCode,@Comment,@InvoiceDate,@FK_DocsysCode,@amani,@InvoiceNo,@VisitorSyscode,
                       @sign,@Type, @RecievedDate, @DeliveredtDate, @paymentDate)

return IDENT_CURRENT('InvoiceTbl')