set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[STPCreateDocRow] @docSysCode int, @AmountIn money, @AmountOut money, @Comment nvarchar(1000)='', @Account Int, @SeccondAccount Int, @IgnorCreditLimit int , @LinkFrom int AS DECLARE @Identity int, @credit money , @rem money, @OldAmount money, --to check with likfrom @BedehkarOnly bit /* find out credit limit permision ------------------- */ if @IgnorCreditLimit=0 and @amountIn>0 begin SELECT @rem = SUM(AmmountIN - AmmountOUT) FROM DocDetailTbl WHERE (Fk_AccountSyscode = @Account); if @rem is null set @rem=0 sELECT @credit=CreditLimit FROM CodeMNGDWNTbl WHERE (AccountSysCode = @Account) if @credit < (@rem+ @AmountIn) begin raiserror(50201,16,1) --pass throw credit limit return 1 end end /* ----------------------------------------------------------------- */ sELECT @BedehkarOnly=bedehkaronly FROM CodeMNGDWNTbl WHERE (AccountSysCode = @Account) if @BedehkarOnly=1 and @amountOut>0 begin SELECT @rem = SUM(AmmountIN - AmmountOUT) FROM DocDetailTbl WHERE (Fk_AccountSyscode = @Account); if @rem is null set @rem=0 if 0 > (@rem- @amountOut) begin raiserror(50207,16,1) --bedehkar only cant go bestankar return 1 end end /* ----------------------------------------------------------------- */ /* Add a row to table ----------------------------------------------- */ if @linkfrom <>0 begin select @oldAmount=(ammountout-ammountin) from DocDetailTbl where syscode=@linkfrom if @oldamount<>(@amountin-@amountout) begin raiserror (50205,16,1) return 1 end end if @@error<>0 return 0 begin tran t1 INSERT INTO DocDetailTbl (Fk_docSysCode, Fk_AccountSyscode, AmmountIN, AmmountOUT, UserRowNo, ArchiveName, SndFk_AccountSyscode, Comment) VALUES (@docSysCode,@Account,@AmountIn,@AmountOut,0,'',@SeccondAccount,@Comment) set @identity=@@identity if @linkfrom <> 0 exec dbo.STPCreateDocLink @linkfrom, @identity if @@error <>0 begin rollback tran t1 return 0 end else begin commit tran t1 return @Identity end