Add invoice

258 views
192 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
258
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Add invoice

  1. 1. addInvoiceALTERProcedure [dbo].[addInvoice](@INo Varchar(50),@IDate DateTime,@CusVarchar(50),@Total Numeric(18,2),@Paid Numeric(18,2),@rtAMNumeric(18,2),@writeOff Numeric(18,2))ASBeginInsertInto INVOICE Values(@INo,@IDate,@Cus,@Total,@Paid,@rtAM,@writeOff)Declare @credit Numeric(18,2)set @Credit=@Total-(@Paid+@WriteOff+@rtAM)If(@Credit>0) Begin declare @bgCredit Numeric(18,2) DECLARE @endCredit Numeric(18,2) select @bgCredit=Credit from Customers Where C_ID=@Cus set @endCredit=@bgCredit+@credit Update Customers set CREDIT=@endCredit where C_ID=@Cus InsertInto RecievableHistory Values(@iDate,@Cus,@BgCredit,@Credit,0,0,@endCredit,@INo) endif(@paid>0) Begin declare @bAM Numeric(18,2),@eAM Numeric(18,2) declare @CID Int set @CID=(SelectMax(ID)from cashFlow) if(@CID Isnull) set @bAM=0 else set @bAM=(Select EndBalance from CashFlow where ID=@CID) set @eAM=@bAM+@Paid InsertInto CashFLow Values(@IDate,@bAM,@Paid,@eAM,@INo,N ,N )endEND addInvoiceItemALTERProcedure [dbo].[addInvoiceItem](@INo Varchar(50),@PRONAMENVARCHAR(100),@W_CAPACITY NUMERIC(18,2),@UN NVarchar(50),@Price Numeric(18,3),@Qty1 Numeric(18,2),@rtQty Numeric(18,2),@Dis Numeric(18,2),@Am Numeric(18,2))asBegindeclare @iDate DateTimeset @iDate=(Select INVDate from Invoice where InvNo=@iNo)InsertInto InvoiceItemsValues(@INo,@PRONAME,@W_CAPACITY,@Un,@Price,@Qty1,@rtQty,@Dis,@AM)if(@Qty1>0)exec processStock 1,@iDate,@W_CAPACITY,@Qty1,N ,@ino,@PRONAMEif(@rtQty>0)exec processStock 0,@iDate,@W_CAPACITY,@rtQty,N ,@ino,@PRONAMEENDEND
  2. 2. deleteInvoiceALTERProc [dbo].[deleteInvoice](@invNo Varchar(50))asDeclare @Cid Varchar(50),@credit Numeric(18,2)Beginset @Cid=(Select C_id from Invoice where InvNo=@InvNo)set @Credit=(Select Credit from Invoice Where InvNo=@InvNo)declare @rID Intselect @rid=CID from RecievableHistorywhere C_ID=@CID and INVNO=@InvNoupdate customers set credit=credit-@creditwhere C_ID=@CIDdeletefrom RecievableHistory where CID=@ridUpdate RecievableHistoryset BeginBalance=BeginBalance-@credit,EndBalance=EndBalance-@Creditwhere C_ID=@CID and CID>@ridDeclare ItemCur CURSOR SCROLL DYNAMICforselect PRONAME,QtyS,reQty,W_CAPACITYfrom InvoiceItems where InvNo=@InvNoDeclare @pro nVarchar(50),@Qt Numeric(18,2),@retQt Numeric(18,2),@capacitynumeric(18,2)open ItemCurfetch first from ItemCur Into @pro,@qt,@retQt,@capacitywhile(@@fetch_status=0)Beginif(@qt>0)begin Declare @RID1 Int select @RID1=SID from StockHistory where TranType=N and RefNo=@invNo Update StockHistory set BeginStock=BeginStock+@Qt,EndStock=EndStock+@Qt where SID>@RID1 and RefNo=@invNo deletefrom StockHistory where SID=@RID1 Update WARTERS set Qty=Qty+@Qt where SUPLIERName=@Pro and W_capacity=@capacityendif(@retQt>0)BeginDeclare @RID2 Int select @RID2=SID from StockHistory where TranType=N and RefNo=@invNo Update StockHistory set BeginStock=BeginStock-@retQt,EndStock=EndStock-@retQt where SID>@RID2 and RefNo=@invNo Deletefrom StockHistory where SID=@RID2 Update WARTERS set Qty=Qty-@retQt where SuplierName=@Pro and W_capacity=@capacityendfetch next from ItemCur Into @pro,@qt,@retQt,@capacityendclose itemCurdeallocate ItemCurDeletefrom Invoice where InvNo=@invNodeletefrom InvoiceItems where InvNo=@InvNoEND
  3. 3. deleteImportALTERProcedure [dbo].[deleteImport](@ImpNO Varchar(20))asBeginDeclare @sup NVarchar(100),@RID Int,@credit Numeric(18,3),@InvRef Varchar(30)Select @Sup=Supliername,@credit=Credit,@InvRef=Refno from Importswhere ImportNo=@ImpNoselect @RID=PID from PayableHistory Where SuplierName=@sup and RefNo=@InvRefAND Credit>0Deletefrom PayableHistory Where PID=@RIDUpdate PayableHistory Set BeginCredit=BeginCredit-@Credit,EndCredit=EndCredit-@CreditWhere PID>@RIDUpdate Supliers set Credit=Credit-credit where SuplierName=@SupDeclare imCursor CURSOR SCROLL DYNAMIC FORSELECTCapacity,ImportQty+FreeQty,supliernamefrom ImportItems WHere ImportNo=@ImpNoDeclare @capcity numeric(18,2),@Qty Numeric(18,2),@supliernameasnvarchar(100)Open ImCursorFetch first from imCursor Into@capcity,@Qty,@supliernameWhile(@@fetch_Status=0) Begin declare @SID Int Select @SID=SID from StockHistory Where RefNo=@ImpNo and W_CAPACITY=@capcity Deletefrom StockHistory Where SID=@SID Update StockHistory Set BeginStock=BeginStock-@QTy,ENDStock=EndStock-@QTy Where Sid>@sid Update WARTERS set QTy=Qty-@QTY Where W_capacity=@capcity and supliername=@supliernameFetch next from imCursor Into@capcity,@Qty,@supliername Endclose imCursordeallocate imCursordeletefrom ImportItems Where REFNO=@ImpNoDeletefrom Imports Where REFNO=@ImpNoENDEND doImportItemALTERProcedure [dbo].[doImportItem](@INo nVarchar(50),@capacity numeric(18,2),@UN NVarchar(50),@Price Numeric(18,2),@Qty Numeric(18,2),@FreeQty Numeric(18,2),@Disc Numeric(18,2),@Am Numeric(18,2), @REFNO ASVARCHAR(50),@suppliername asnvarchar(100))asBegindeclare @iDate DateTimeset @iDate=(Select ImportDate from Imports where ImportNo=@iNo)InsertInto ImportItemsValues(@INo,@capacity,@Un,@Price,@Qty,@freeQty,@Disc,@AM,@REFNO,@suppliername)Declare @TTQT Numeric(18,2)set @TTQT=@QTy+@FreeQtyexec processStock 0,@iDate,@capacity,@ttQT,N ,@ino,@suppliernameEND
  4. 4. processStockALTERProc [dbo].[processStock](@State Int,@Date DateTime,@WCAPACITY NUMERIC(18,2),@Qty Numeric(18,2),@Desc NVarchar(50),@Ref Varchar(50),@suppliername asnvarchar(100))asBeginDeclare @BStock Numeric(18),@EStock Numeric(18,2)Select @BStock=QTY from WARTERS where W_CAPACITY=@WCAPACITY andsupliername=@suppliernameif(@State=0)--Import Begin set @eStock=@Bstock+@Qty endif(@State=1)--Export Begin set @eStock=@Bstock-@Qty endUpdate WARTERSset Qty=@EStockwhere W_CAPACITY=@WCAPACITY and supliername=@suppliernameInsertInto StockHistoryValues(@Date,@WCAPACITY,@BStock,@Qty,@DESC,@EStock,@REF)END doCPAIDDetailALTERProc [dbo].[doCPAIDDetail](@pNo Varchar(40),@date DateTime,@CusVarchar(50),@Inv Varchar(50),@Total Numeric(18,2),@PAM Numeric(18,2))ASBeginInsertInto CPAIDDETAILSValues(@PNO,@Inv,@Total,@PAM)Declare @Bg Numeric(18,2),@end Numeric(18,2)Select @bg=Credit from Customers WhereC_ID=@Cusset @end=@bg-@PAMInsertInto RecievableHistoryValues(@date,@cus,@bg,0,@pam,0,@end,@inv)Update Customersset Credit=@endWhere C_ID=@cusUpdate Invoice set Paid=Paid+@PAMwhere INVNO=@INVdeclare @bAM Numeric(18,2),@eAM Numeric(18,2) declare @CID Int set @CID=(SelectMax(ID)from cashFlow) if(@CID Isnull) set @bAM=0 else set @bAM=(Select EndBalance from CashFlow where ID=@CID) set @eAM=@bAM+@pam InsertInto CashFLow Values(@Date,@bAM,@Pam,@eAM,@inv,N ,N )END
  5. 5. doimportsALTERproc [dbo].[doimports](@IMPNO NVARCHAR(50),@IMPDATE DATETIME,@SUPLIERS NVARCHAR(200),@TOTALAM NUMERIC(18,2),@PAID NUMERIC(18,2),@REFNO NVARCHAR(200),@USER VARCHAR(50))ASBEGININSERTINTO ImportsVALUES(@IMPNO,@IMPDATE,@SUPLIERS,@TOTALAM,@PAID,@REFNO,@USER)declare @CREDIT numeric(18,2)set @CREDIT=@TOTALAM-@PAIDif(@TOTALAM>0) begin declare @bAM Numeric(18,2),@eAM Numeric(18,2) declare @CID Int set @CID=(SelectMax(ID)from cashFlow) if(@CID Isnull) set @bAM=0 else set @bAM=(Select EndBalance from CashFlow where ID=@CID) set @eAM=@bAM-@TOTALAM InsertInto CashFLow Values(@IMPDATE,@bAM,@TOTALAM,@eAM,@REFNO,N ,N ) endif(@CREDIT>0) Begin declare @sbcredit numeric(18,2) declare @secredit numeric(18,2) select @sbcredit=credit from Supliers where supliername=@supliers set @secredit=@sbcredit+@credit insertinto PayableHistory values(@IMPDATE,@SUPLIERS,@sbcredit,@credit,0,0,@secredit,@REFNO) Update Supliers set Credit=@secredit where supliername=@supliers endEND RECIECALTERPROC [dbo].[RECIEC](@RID VARCHAR(50),@CUSNAME NVARCHAR(100),@INVNOVARCHAR(100),@DATE DATETIME,@TOTAL NUMERIC(18,2),@PAID NUMERIC(18,2),@RETURN NUMERIC(18,2),@CREDITNUMERIC(18,2),@PAY NUMERIC(18,2),@NEWCREDIT NUMERIC(18,2))ASBEGININSERTINTO ReceicVALUES(@RID,@CUSNAME,@INVNO,@DATE,@TOTAL,@PAID,@RETURN,@CREDIT,@PAY,@NEWCREDIT)END DOCPAIDALTERProc [dbo].[DOCPAID](@CPNo Varchar(50),@pDate DateTime,@cusNVarchar(100),@total Numeric(18,2),@USER VARCHAR(50))asBeginInsertInto CPAIDS Values(@CPNo,@pDate,@cus,@total,@USER)
  6. 6. PAIDDetailALTERProc [dbo].[PAIDDetail](@pNo NVarchar(40),@IMPDATE DateTime,@supliersNVarchar(50),@Inv NVarchar(200),@credit Numeric(18,2),@PAM Numeric(18,2))ASBeginInsertInto PAIDDETAILSValues(@PNO,@Inv,@credit,@PAM) declare @sbcredit numeric(18,2) declare @secredit numeric(18,2) select @sbcredit=credit from Supliers where supliername=@supliers set @secredit=@sbcredit-@PAM insertinto PayableHistory values(@IMPDATE,@SUPLIERS,@sbcredit,0,@PAM,0,@secredit,@Inv) Update Supliers set Credit=@secredit where supliername=@supliersUpdate imports set Paid=Paid+@PAMWHERE REFNO=@INV AND SUPLIERNAME=@SUPLIERSdeclare @bAM Numeric(18,2),@eAM Numeric(18,2) declare @CID Int set @CID=(SelectMax(ID)from cashFlow) if(@CID Isnull) set @bAM=0 else set @bAM=(Select EndBalance from CashFlow where ID=@CID) set @eAM=@bAM-@pam InsertInto CashFLow Values(@IMPDATE,@bAM,@Pam,@eAM,@inv,N ,N )END

×