I want to write a stored procedure for my invoice reportviewer.
After invoice is generated in reportviewer it must also add the data to my Invoice table.
This is all my tables in my database:
CREATE TABLE [dbo].[Waybills](
[WaybillID] [int] IDENTITY(1,1) NOT NULL,
[SenderName] [varchar](50) NULL,
[SenderAddress] [varchar](50) NULL,
[SenderContact] [int] NULL,
[ReceiverName] [varchar](50) NULL,
[ReceiverAddress] [varchar](50) NULL,
[ReceiverContact] [int] NULL,
[UnitDescription] [varchar](50) NULL,
[UnitWeight] [int] NULL,
[DateReceived] [date] NULL,
[Payee] [varchar](50) NULL,
[CustomerID] [int] NULL,
PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[Customer](
[CustomerID] [int] IDENTITY(1,1) NOT NULL,
[customerName] [varchar](30) NULL,
[CustomerAddress] [varchar](30) NULL,
[CustomerContact] [varchar](30) NULL,
[VatNo] [int] NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
)
CREATE TABLE [dbo].[Cycle](
[CycleID] [int] IDENTITY(1,1) NOT NULL,
[CycleNumber] [int] NULL,
[StartDate] [date] NULL,
[EndDate] [date] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Payments](
[PaymentID] [int] IDENTITY(1,1) NOT NULL,
[Amount] [money] NULL,
[PaymentDate] [date] NULL,
[CustomerID] [int] NULL,
PRIMARY KEY CLUSTERED
Create table Invoices
(
InvoiceID int IDENTITY(1,1),
InvoiceNumber int,
InvoiceDate date,
BalanceBroughtForward money,
OutstandingAmount money,
CustomerID int,
WaybillID int,
PaymentID int,
CycleID int
PRIMARY KEY (InvoiceID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
FOREIGN KEY (WaybillID) REFERENCES Waybills(WaybillID),
FOREIGN KEY (PaymentID) REFERENCES Payments(PaymentID),
FOREIGN KEY (CycleID) REFERENCES Cycle(CycleID)
)
I want my sp to find all waybills for specific customer in a specific cycle with payments made from this client.
All this data must then be added into the INVOICE table.
Can someone please help me or show me on the right direction?
create proc GenerateInvoice
@StartDate date,
@EndDate date,
@Payee varchar(30)
AS
SELECT Waybills.WaybillNumber Waybills.SenderName, Waybills.SenderAddress, Waybills.SenderContact, Waybills.ReceiverName, Waybills.ReceiverAddress,
Waybills.ReceiverContact, Waybills.UnitDescription, Waybills.UnitWeight, Waybills.DateReceived, Waybills.Payee,
Payments.Amount, Payments.PaymentDate, Cycle.CycleNumber, Cycle.StartDate, Cycle.EndDate
FROM Waybills CROSS JOIN
Payments CROSS JOIN
Cycle
WHERE Waybills.ReceiverName = @Payee
AND (Waybills.DateReceived BETWEEN (@StartDate) AND (@EndDate))
Insert Into Invoices (InvoiceNumber, InvoiceDate, BalanceBroughtForward, OutstandingAmount)
Values (@InvoiceNumber, @InvoiceDate, @BalanceBroughtForward, @ OutstandingAmount)
go