Website.InvoiceCustomerOrders
Documentation | WideWorldImporters |
Schema | Website |
Name | InvoiceCustomerOrders |
Input/Output
Mode | Name | Data type | Description | |
---|---|---|---|---|
1 | IN | OrdersToInvoice | table type | |
2 | IN | PackedByPersonID | int | |
3 | IN | InvoicedByPersonID | int |
Script
CREATE PROCEDURE Website.InvoiceCustomerOrders @OrdersToInvoice Website.OrderIDList READONLY, @PackedByPersonID int, @InvoicedByPersonID int WITH EXECUTE AS OWNER AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; DECLARE @InvoicesToGenerate TABLE ( OrderID int PRIMARY KEY, InvoiceID int NOT NULL, TotalDryItems int NOT NULL, TotalChillerItems int NOT NULL ); BEGIN TRY; -- Check that all orders exist, have been fully picked, and not already invoiced. Also allocate new invoice numbers. INSERT @InvoicesToGenerate (OrderID, InvoiceID, TotalDryItems, TotalChillerItems) SELECT oti.OrderID, NEXT VALUE FOR Sequences.InvoiceID, COALESCE((SELECT SUM(CASE WHEN si.IsChillerStock <> 0 THEN 0 ELSE 1 END) FROM Sales.OrderLines AS ol INNER JOIN Warehouse.StockItems AS si ON ol.StockItemID = si.StockItemID WHERE ol.OrderID = oti.OrderID), 0), COALESCE((SELECT SUM(CASE WHEN si.IsChillerStock <> 0 THEN 1 ELSE 0 END) FROM Sales.OrderLines AS ol INNER JOIN Warehouse.StockItems AS si ON ol.StockItemID = si.StockItemID WHERE ol.OrderID = oti.OrderID), 0) FROM @OrdersToInvoice AS oti INNER JOIN Sales.Orders AS o ON oti.OrderID = o.OrderID WHERE NOT EXISTS (SELECT 1 FROM Sales.Invoices AS i WHERE i.OrderID = oti.OrderID) AND o.PickingCompletedWhen IS NOT NULL; IF EXISTS (SELECT 1 FROM @OrdersToInvoice AS oti WHERE NOT EXISTS (SELECT 1 FROM @InvoicesToGenerate AS itg WHERE itg.OrderID = oti.OrderID)) BEGIN PRINT N'At least one order ID either does not exist, is not picked, or is already invoiced'; THROW 51000, N'At least one orderID either does not exist, is not picked, or is already invoiced', 1; END; BEGIN TRAN; INSERT Sales.Invoices (InvoiceID, CustomerID, BillToCustomerID, OrderID, DeliveryMethodID, ContactPersonID, AccountsPersonID, SalespersonPersonID, PackedByPersonID, InvoiceDate, CustomerPurchaseOrderNumber, IsCreditNote, CreditNoteReason, Comments, DeliveryInstructions, InternalComments, TotalDryItems, TotalChillerItems, DeliveryRun, RunPosition, ReturnedDeliveryData, LastEditedBy, LastEditedWhen) SELECT itg.InvoiceID, c.CustomerID, c.BillToCustomerID, itg.OrderID, c.DeliveryMethodID, o.ContactPersonID, btc.PrimaryContactPersonID, o.SalespersonPersonID, @PackedByPersonID, SYSDATETIME(), o.CustomerPurchaseOrderNumber, 0, NULL, NULL, c.DeliveryAddressLine1 + N', ' + c.DeliveryAddressLine2, NULL, itg.TotalDryItems, itg.TotalChillerItems, c.DeliveryRun, c.RunPosition, JSON_MODIFY(N'{"Events": []}', N'append $.Events', JSON_MODIFY(JSON_MODIFY(JSON_MODIFY(N'{ }', N'$.Event', N'Ready for collection'), N'$.EventTime', CONVERT(nvarchar(20), SYSDATETIME(), 126)), N'$.ConNote', N'EAN-125-' + CAST(itg.InvoiceID + 1050 AS nvarchar(20)))), @InvoicedByPersonID, SYSDATETIME() FROM @InvoicesToGenerate AS itg INNER JOIN Sales.Orders AS o ON itg.OrderID = o.OrderID INNER JOIN Sales.Customers AS c ON o.CustomerID = c.CustomerID INNER JOIN Sales.Customers AS btc ON btc.CustomerID = c.BillToCustomerID; INSERT Sales.InvoiceLines (InvoiceID, StockItemID, [Description], PackageTypeID, Quantity, UnitPrice, TaxRate, TaxAmount, LineProfit, ExtendedPrice, LastEditedBy, LastEditedWhen) SELECT itg.InvoiceID, ol.StockItemID, ol.[Description], ol.PackageTypeID, ol.PickedQuantity, ol.UnitPrice, ol.TaxRate, ROUND(ol.PickedQuantity * ol.UnitPrice * ol.TaxRate / 100.0, 2), ROUND(ol.PickedQuantity * (ol.UnitPrice - sih.LastCostPrice), 2), ROUND(ol.PickedQuantity * ol.UnitPrice, 2) + ROUND(ol.PickedQuantity * ol.UnitPrice * ol.TaxRate / 100.0, 2), @InvoicedByPersonID, SYSDATETIME() FROM @InvoicesToGenerate AS itg INNER JOIN Sales.OrderLines AS ol ON itg.OrderID = ol.OrderID INNER JOIN Warehouse.StockItems AS si ON ol.StockItemID = si.StockItemID INNER JOIN Warehouse.StockItemHoldings AS sih ON si.StockItemID = sih.StockItemID ORDER BY ol.OrderID, ol.OrderLineID; INSERT Warehouse.StockItemTransactions (StockItemID, TransactionTypeID, CustomerID, InvoiceID, SupplierID, PurchaseOrderID, TransactionOccurredWhen, Quantity, LastEditedBy, LastEditedWhen) SELECT il.StockItemID, (SELECT TransactionTypeID FROM [Application].TransactionTypes WHERE TransactionTypeName = N'Stock Issue'), i.CustomerID, i.InvoiceID, NULL, NULL, SYSDATETIME(), 0 - il.Quantity, @InvoicedByPersonID, SYSDATETIME() FROM @InvoicesToGenerate AS itg INNER JOIN Sales.InvoiceLines AS il ON itg.InvoiceID = il.InvoiceID INNER JOIN Sales.Invoices AS i ON il.InvoiceID = i.InvoiceID ORDER BY il.InvoiceID, il.InvoiceLineID; WITH StockItemTotals AS ( SELECT il.StockItemID, SUM(il.Quantity) AS TotalQuantity FROM Sales.InvoiceLines aS il WHERE il.InvoiceID IN (SELECT InvoiceID FROM @InvoicesToGenerate) GROUP BY il.StockItemID ) UPDATE sih SET sih.QuantityOnHand -= sit.TotalQuantity, sih.LastEditedBy = @InvoicedByPersonID, sih.LastEditedWhen = SYSDATETIME() FROM Warehouse.StockItemHoldings AS sih INNER JOIN StockItemTotals AS sit ON sih.StockItemID = sit.StockItemID; INSERT Sales.CustomerTransactions (CustomerID, TransactionTypeID, InvoiceID, PaymentMethodID, TransactionDate, AmountExcludingTax, TaxAmount, TransactionAmount, OutstandingBalance, FinalizationDate, LastEditedBy, LastEditedWhen) SELECT i.BillToCustomerID, (SELECT TransactionTypeID FROM [Application].TransactionTypes WHERE TransactionTypeName = N'Customer Invoice'), itg.InvoiceID, NULL, SYSDATETIME(), (SELECT SUM(il.ExtendedPrice - il.TaxAmount) FROM Sales.InvoiceLines AS il WHERE il.InvoiceID = itg.InvoiceID), (SELECT SUM(il.TaxAmount) FROM Sales.InvoiceLines AS il WHERE il.InvoiceID = itg.InvoiceID), (SELECT SUM(il.ExtendedPrice) FROM Sales.InvoiceLines AS il WHERE il.InvoiceID = itg.InvoiceID), (SELECT SUM(il.ExtendedPrice) FROM Sales.InvoiceLines AS il WHERE il.InvoiceID = itg.InvoiceID), NULL, @InvoicedByPersonID, SYSDATETIME() FROM @InvoicesToGenerate AS itg INNER JOIN Sales.Invoices AS i ON itg.InvoiceID = i.InvoiceID; COMMIT; END TRY BEGIN CATCH IF XACT_STATE() <> 0 ROLLBACK; PRINT N'Unable to invoice these orders'; THROW; RETURN -1; END CATCH; RETURN 0; END; |
Exported: 2019-02-04 23:04, Last imported: 2018-08-10 09:51