Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
control_sql_-_check_inventory_received_levels_on_pos [2022/07/07 11:16] orichards [SQL Query to Reduce Received without changing Billed Qty] |
control_sql_-_check_inventory_received_levels_on_pos [2022/07/07 11:20] (current) shendrix [SQL Query to Reduce Received without changing Billed Qty] |
||
|---|---|---|---|
| Line 178: | Line 178: | ||
| ELSE | ELSE | ||
| SELECT InventoryID, | SELECT InventoryID, | ||
| - | |||
| </ | </ | ||
| Line 186: | Line 185: | ||
| <code sql> | <code sql> | ||
| - | DECLARE @CommitChanges BIT;\\ | + | DECLARE @CommitChanges BIT; |
| - | SET @CommitChanges = 0;\\ | + | SET @CommitChanges = 0; |
| - | DECLARE @EntryDate DateTime;\\ | + | DECLARE @EntryDate DateTime; |
| - | SET @EntryDate = GETDATE();\\ | + | SET @EntryDate = GETDATE(); |
| - | DECLARE @Description VARCHAR(255); | + | DECLARE @Description VARCHAR(255); |
| - | SET @Description = ' | + | SET @Description = ' |
| - | DECLARE @Notes VARCHAR(MAX); | + | DECLARE @Notes VARCHAR(MAX); |
| - | SET @Notes = ' | + | SET @Notes = ' |
| - | DECLARE @ModifiedDate DateTime;\\ | + | DECLARE @ModifiedDate DateTime; |
| - | SET @ModifiedDate = getdate();\\ | + | SET @ModifiedDate = getdate(); |
| - | DECLARE @ModifiedByUser VARCHAR(100); | + | DECLARE @ModifiedByUser VARCHAR(100); |
| - | SET @ModifiedByUser = ' | + | SET @ModifiedByUser = ' |
| - | DECLARE @ModifiedByComputer VARCHAR(100); | + | DECLARE @ModifiedByComputer VARCHAR(100); |
| - | SET @ModifiedByComputer = ' | + | SET @ModifiedByComputer = ' |
| - | DECLARE @T TABLE(InventoryID INT,\\ | + | DECLARE @T TABLE(InventoryID INT, |
| - | | + | |
| - | ItemName VARCHAR(100), | + | ItemName VARCHAR(100), |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | ;\\ | + | ; |
| - | -- Insert all of the inventory items into the temp table\\ | + | -- Insert all of the inventory items into the temp table |
| - | INSERT INTO @T\\ | + | INSERT INTO @T |
| - | (InventoryID, | + | (InventoryID, |
| - | SELECT I.ID, I.PartID, I.ItemName, I.DivisionID, | + | SELECT I.ID, I.PartID, I.ItemName, I.DivisionID, |
| - | FROM | + | FROM |
| - | LEFT JOIN Part P ON P.ID = I.PartID\\ | + | LEFT JOIN Part P ON P.ID = I.PartID |
| - | WHERE P.PartType = 0 AND I.IsGroup = 0 AND P.TrackInventory = 1\\ | + | WHERE P.PartType = 0 AND I.IsGroup = 0 AND P.TrackInventory = 1 |
| - | ;\\ | + | ; |
| - | -- Set the current inventory items\\ | + | -- Set the current inventory items |
| - | UPDATE @T\\ | + | UPDATE @T |
| - | SET CurrentReceivedOnly = ISNULL( IL.QuantityReceivedOnly, | + | SET CurrentReceivedOnly = ISNULL( IL.QuantityReceivedOnly, |
| - | FROM @T T\\ | + | FROM @T T |
| - | LEFT JOIN ( SELECT | + | LEFT JOIN ( SELECT |
| - | , ROUND( SUM( ISNULL( QuantityReceivedOnly, | + | , ROUND( SUM( ISNULL( QuantityReceivedOnly, |
| - | | + | |
| - | | + | |
| - | GROUP BY InventoryID ) IL ON IL.InventoryID = T.InventoryID\\ | + | GROUP BY InventoryID ) IL ON IL.InventoryID = T.InventoryID |
| - | ;\\ | + | ; |
| - | -- Compute the actual on order quantity and set the value\\ | + | -- Compute the actual on order quantity and set the value |
| - | DECLARE @ActualReceivedOnly TABLE (InventoryID INT PRIMARY KEY, Amount FLOAT);\\ | + | DECLARE @ActualReceivedOnly TABLE (InventoryID INT PRIMARY KEY, Amount FLOAT); |
| - | INSERT INTO @ActualReceivedOnly\\ | + | INSERT INTO @ActualReceivedOnly |
| - | SELECT I.ID InventoryID, | + | SELECT I.ID InventoryID, |
| - | SUM( CASE WHEN TD.UnitID = P.UnitID THEN TD.POQuantityReceivedOnly\\ | + | SUM( CASE WHEN TD.UnitID = P.UnitID THEN TD.POQuantityReceivedOnly |
| - | ELSE ( TD.POQuantityReceivedOnly * ISNULL(IC.FxUpper / IC.FxLower, 1 ) )\\ | + | ELSE ( TD.POQuantityReceivedOnly * ISNULL(IC.FxUpper / IC.FxLower, 1 ) ) |
| - | END ) POQuantityReceivedOnly\\ | + | END ) POQuantityReceivedOnly |
| - | FROM ( SELECT TransHeaderID, | + | FROM ( SELECT TransHeaderID, |
| - | ( CASE WHEN ItemClassTypeID = 12076 THEN (SELECT TOP 1 PartID FROM CatalogItem WHERE ID = ItemID)\\ | + | ( CASE WHEN ItemClassTypeID = 12076 THEN (SELECT TOP 1 PartID FROM CatalogItem WHERE ID = ItemID) |
| - | ELSE ItemID\\ | + | ELSE ItemID |
| - | END ) PartID,\\ | + | END ) PartID, |
| - | UnitID,\\ | + | UnitID, |
| - | Quantity,\\ | + | Quantity, |
| - | BillQuantity, | + | BillQuantity, |
| - | ( ISNULL( Quantity, 0 ) - ISNULL( BillQuantity, | + | ( ISNULL( Quantity, 0 ) - ISNULL( BillQuantity, |
| - | WarehouseID\\ | + | WarehouseID |
| - | FROM VendorTransDetail\\ | + | FROM VendorTransDetail |
| - | WHERE ID > 0\\ | + | WHERE ID > 0 |
| - | AND ItemID IS NOT NULL\\ | + | AND ItemID IS NOT NULL |
| - | AND StationID IN (108, 109) -- Received, Partially Received\\ | + | AND StationID IN (108, 109) -- Received, Partially Received |
| - | ) TD\\ | + | ) TD |
| - | JOIN TransHeader TH ON TH.ID = TD.TransHeaderID\\ | + | JOIN TransHeader TH ON TH.ID = TD.TransHeaderID |
| - | LEFT JOIN Inventory I ON (I.PartID = TD.PartID AND I.WarehouseID = TD.WarehouseID)\\ | + | LEFT JOIN Inventory I ON (I.PartID = TD.PartID AND I.WarehouseID = TD.WarehouseID) |
| - | LEFT JOIN Part P ON P.ID = TD.PartID\\ | + | LEFT JOIN Part P ON P.ID = TD.PartID |
| - | LEFT JOIN ( SELECT PartID,\\ | + | LEFT JOIN ( SELECT PartID, |
| - | UnitID,\\ | + | UnitID, |
| - | PartInventoryConversion.ConversionFormula, | + | PartInventoryConversion.ConversionFormula, |
| - | CASE\\ | + | CASE |
| - | WHEN PartInventoryConversion.ConversionFormula IS NULL THEN 1.00\\ | + | WHEN PartInventoryConversion.ConversionFormula IS NULL THEN 1.00 |
| - | WHEN PATINDEX( ' | + | WHEN PATINDEX( ' |
| - | ELSE CAST(CAST(PartInventoryConversion.ConversionFormula AS VARCHAR(100)) AS FLOAT)\\ | + | ELSE CAST(CAST(PartInventoryConversion.ConversionFormula AS VARCHAR(100)) AS FLOAT) |
| - | END FxUpper,\\ | + | END FxUpper, |
| - | CASE\\ | + | CASE |
| - | WHEN PartInventoryConversion.ConversionFormula IS NULL THEN 1.00\\ | + | WHEN PartInventoryConversion.ConversionFormula IS NULL THEN 1.00 |
| - | WHEN PATINDEX( ' | + | WHEN PATINDEX( ' |
| - | ELSE 1.00\\ | + | ELSE 1.00 |
| - | END FxLower\\ | + | END FxLower |
| - | FROM PartInventoryConversion ) IC ON IC.PartID = TD.PartID AND IC.UnitID = TD.UnitID\\ | + | FROM PartInventoryConversion ) IC ON IC.PartID = TD.PartID AND IC.UnitID = TD.UnitID |
| - | WHERE TH.TransactionType = 7 AND I.ID IS NOT NULL -- PO, we don't filter on status since we're filtering on line item statuses.\\ | + | WHERE TH.TransactionType = 7 AND I.ID IS NOT NULL -- PO, we don't filter on status since we're filtering on line item statuses. |
| - | GROUP BY I.ID\\ | + | GROUP BY I.ID |
| - | ;\\ | + | ; |
| - | UPDATE @T\\ | + | UPDATE @T |
| - | SET ActualReceivedOnly = ISNULL( CR.Amount, 0 )\\ | + | SET ActualReceivedOnly = ISNULL( CR.Amount, 0 ) |
| - | FROM @T T\\ | + | FROM @T T |
| - | LEFT JOIN @ActualReceivedOnly CR ON CR.InventoryID = T.InventoryID\\ | + | LEFT JOIN @ActualReceivedOnly CR ON CR.InventoryID = T.InventoryID |
| - | ;\\ | + | ; |
| - | -- Delete all inventory records that do not require a change from the temp table\\ | + | -- Delete all inventory records that do not require a change from the temp table |
| - | DELETE FROM @T\\ | + | DELETE FROM @T |
| - | WHERE CurrentReceivedOnly = ActualReceivedOnly\\ | + | WHERE CurrentReceivedOnly = ActualReceivedOnly |
| - | ;\\ | + | ; |
| - | IF @CommitChanges = 1\\ | + | IF @CommitChanges = 1 |
| - | BEGIN\\ | + | BEGIN |
| - | -- Compute the IDs for the new records\\ | + | -- Compute the IDs for the new records |
| - | DECLARE @LastJournalID INT;\\ | + | DECLARE @LastJournalID INT; |
| - | SET @LastJournalID = (SELECT MAX(ID) FROM Journal);\\ | + | SET @LastJournalID = (SELECT MAX(ID) FROM Journal); |
| - | UPDATE T\\ | + | UPDATE T |
| - | SET New_JournalID = RowNum + @LastJournalID\\ | + | SET New_JournalID = RowNum + @LastJournalID |
| - | FROM\\ | + | FROM |
| - | (\\ | + | ( |
| - | SELECT | + | SELECT |
| - | ROW_NUMBER() OVER(ORDER BY x.InventoryID) AS RowNum\\ | + | ROW_NUMBER() OVER(ORDER BY x.InventoryID) AS RowNum |
| - | FROM @T x\\ | + | FROM @T x |
| - | ) AS T\\ | + | ) AS T |
| - | ;\\ | + | ; |
| - | -- Insert Journal entry\\ | + | -- Insert Journal entry |
| - | INSERT INTO Journal\\ | + | INSERT INTO Journal |
| - | (ID, StoreID, ClassTypeID, | + | (ID, StoreID, ClassTypeID, |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | SELECT New_JournalID, | + | SELECT New_JournalID, |
| - | 17, 'Part Usage', | + | 17, 'Part Usage', |
| - | 1, 1, 0, 0, NULL, NULL, 0,\\ | + | 1, 1, 0, 0, NULL, NULL, 0, |
| - | | + | |
| - | 43, ' | + | 43, ' |
| - | FROM @T\\ | + | FROM @T |
| - | ;\\ | + | ; |
| - | -- Insert InventoryLog entry\\ | + | -- Insert InventoryLog entry |
| - | INSERT INTO InventoryLog\\ | + | INSERT INTO InventoryLog |
| - | (ID, StoreID, ClassTypeID, | + | (ID, StoreID, ClassTypeID, |
| - | | + | |
| - | | + | |
| - | | + | |
| - | SELECT New_JournalID, | + | SELECT New_JournalID, |
| - | 0, ActualReceivedOnly - CurrentReceivedOnly, | + | 0, ActualReceivedOnly - CurrentReceivedOnly, |
| - | 0, 0, PartID, 12014,\\ | + | 0, 0, PartID, 12014, |
| - | | + | |
| - | FROM @T T\\ | + | FROM @T T |
| - | ;\\ | + | ; |
| - | END\\ | + | END |
| - | ELSE\\ | + | ELSE |
| SELECT InventoryID, | SELECT InventoryID, | ||
| - | + | </ | |
| ====== Version Information ====== | ====== Version Information ====== | ||
| * Revised : 07/2018 | * Revised : 07/2018 | ||