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 10:39] shendrix [Risk of Data Corruption if Run Improperly] |
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 32: | Line 32: | ||
| ====== SQL Queries ====== | ====== SQL Queries ====== | ||
| + | There are two SQL queries listed below, it is very important that you understand the difference between these two queries and only apply the one that fits the scenario that you are encountering. | ||
| - | |||
| - | There are two SQL queries listed below, it is very important that you understand the difference between these two queries and only apply the one that fits the scenario that you are encountering. | ||
| - The first query will post any adjustment necessary to Received while also posting an opposite adjustment to the Billed Qty on the part. | - The first query will post any adjustment necessary to Received while also posting an opposite adjustment to the Billed Qty on the part. | ||
| - The second query will post any adjustment necessary to Received while not posting any adjustment to the Billed Qty on the part. | - The second query will post any adjustment necessary to Received while not posting any adjustment to the Billed Qty on the part. | ||
| - | |||
| - | |||
| If you do not understand when you should use one of the queries above vs. the other please consult assistance prior to running these the database. | If you do not understand when you should use one of the queries above vs. the other please consult assistance prior to running these the database. | ||
| - | |||
| - | |||
| All users should be out of the system when running this query | All users should be out of the system when running this query | ||
| - | **Restart Chapi and the SSLIP** otherwise duplicate Journal records will be created. | + | **Restart Chapi and the SSLIP** |
| - | + | ||
| - | + | ||
| - | + | ||
| - | < | + | |
| + | <code sql> | ||
| DECLARE @CommitChanges BIT; | DECLARE @CommitChanges BIT; | ||
| SET @CommitChanges = 0; | SET @CommitChanges = 0; | ||
| Line 186: | Line 178: | ||
| ELSE | ELSE | ||
| SELECT InventoryID, | SELECT InventoryID, | ||
| - | |||
| </ | </ | ||
| + | |||
| ===== SQL Query to Reduce Received without changing Billed Qty ===== | ===== SQL Query to Reduce Received without changing Billed Qty ===== | ||
| - | |||
| - | |||
| <code sql> | <code sql> | ||
| + | |||
| DECLARE @CommitChanges BIT; | DECLARE @CommitChanges BIT; | ||
| SET @CommitChanges = 0; | SET @CommitChanges = 0; | ||
| Line 209: | Line 200: | ||
| SET @ModifiedByComputer = ' | SET @ModifiedByComputer = ' | ||
| DECLARE @T TABLE(InventoryID INT, | DECLARE @T TABLE(InventoryID INT, | ||
| - | PartID INT, | + | PartID INT, |
| - | | + | |
| - | | + | DivisionID INT, |
| - | | + | |
| - | | + | |
| - | | + | |
| + | | ||
| ; | ; | ||
| -- 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.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 | ||
| ; | ; | ||
| Line 228: | Line 220: | ||
| 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, |
| - | | + | |
| - | | + | |
| - | | + | |
| ; | ; | ||
| -- Compute the actual on order quantity and set the value | -- Compute the actual on order quantity and set the value | ||
| Line 238: | Line 230: | ||
| 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 |
| - | | + | |
| - | END ) POQuantityReceivedOnly | + | END ) POQuantityReceivedOnly |
| FROM ( SELECT TransHeaderID, | FROM ( SELECT TransHeaderID, | ||
| - | | + | |
| - | | + | |
| - | END ) PartID, | + | END ) PartID, |
| - | UnitID, | + | UnitID, |
| - | Quantity, | + | Quantity, |
| - | BillQuantity, | + | BillQuantity, |
| - | ( ISNULL( Quantity, 0 ) - ISNULL( BillQuantity, | + | ( ISNULL( Quantity, 0 ) - ISNULL( BillQuantity, |
| - | WarehouseID | + | WarehouseID |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | 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 |
| - | | + | |
| 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 | ||
| Line 278: | Line 270: | ||
| 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 | ||
| Line 286: | Line 278: | ||
| IF @CommitChanges = 1 | IF @CommitChanges = 1 | ||
| BEGIN | BEGIN | ||
| - | | + | |
| - | 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, |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | 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, |
| - | | + | |
| - | | + | |
| - | | + | |
| - | FROM @T T | + | FROM @T T |
| - | ; | + | ; |
| END | END | ||
| ELSE | ELSE | ||
| - | | + | |
| </ | </ | ||
| - | |||
| - | |||
| - | |||
| ====== Version Information ====== | ====== Version Information ====== | ||
| * Revised : 07/2018 | * Revised : 07/2018 | ||