This shows you the differences between two versions of the page.

Link to this comparison view

control_sql_-_create_inventorylog_records_so_they_match_inventory [2019/01/27 11:28] (current)
Line 1: Line 1:
 +====== Explanation of SQL ======
 +This SQL is used to adjust the InventoryLog so that the totals match the current inventory in the Inventory table. It does this by creating an adjustment record in the InventoryLog table for any difference found.
 +*Notes:​* ​
 +  * This query does //not// adjust the GL. Therefore, the cummulative value of inventory could become out of sync with the actual inventory counts. This should only be run under the direction of Cyrious technical support.
 +  * This query uses the SQL function [[control_sql_-_add_function_to_compare_inventory_totals_to_log_records]] to determine any adjustments that need to be made. If it is not already there, you may need to add this query.
 +  * After this query is run, you **must** either restart the SSLIP or log into the SSLIP and tell it to Resync IDs. This SQL inserts new records in the database that Control is unaware of. If allowed to continue without either of these actions, the users will receive Key Violations soon after this query is run.
 +  * You may need to run a maintenance query on the InventoryLog table to update the InventoryID to the proper inventory record. In at least some customer data, the InventoryID has been observed to point to a warehouse summary record and not the actual inventory record. The correcting query can be found [[control_sql_-_fix_inventorylog_to_point_to_proper_inventory_record]].
 +===== Risk of Data Corruption if Run Improperly =====
 +**High**. Data is modified in this query. Do not run this except under the direction of a Cyrious Technical Support staff member. Doing otherwise may result in lost or contaminated data. All data modifications done through direct SQL **are permanent and non-reversable**.
 +===== SQL =====
 +As written, the following SQL **will only display** the changes to be made. It will not actually make the changes. To have this query actually insert the records otherwise displayed, uncomment out the two lines that begin with "​Insert Into" so that the results are then added to their respective database tables.
 +<code sql>
 +-- This query creates the corresponding InventoryLog journal
 +-- entries so that the cummulative Log matches the current Inventory.
 +-- It does this by posting a manual Log adjustment entry
 +-- for any difference.
 +-- Step 1
 +-- Save the starting ID for the next journal
 +-- so that the process of posting the updates doesn'​t
 +-- adjust it.
 +declare @FirstID int;
 +set @FirstID = 1000 + (select max(ID) from Journal)
 +-- Step 2
 +-- Update the Journal table first!
 +-- If you do the Log table first, there will be nothing in the
 +-- Check_CompareInventoryToLog() to create the Journal entries.
 +-- insert into Journal
 +RowID + @FirstID,
 +cast(-1 as int) as StoreID,
 +cast(20530 as int) ClassTypeID,​
 +'​CyriousTech'​ as ModifiedByUser,​
 +''​ as ModifiedByComputer,​
 +GetDate() as ModifiedDate,​
 +cast(0 as int) as SeqID,
 +cast(0 as bit) as IsSystem,
 +cast(1 as bit) as IsActive,
 +cast(10 as int) as EmployeeID,
 +cast(17 as int) as JournalActivityType,​
 +'Part Usage' as JournalActivityText,​
 +'Query Correction'​ as Description,​
 +''​ as Notes,
 +GetDate() as StartDateTime,​
 +GetDate() as EndDateTime,​
 +cast(null as DateTime) as TotalTime,
 +cast(null as DateTime) as ScheduledDateTime,​
 +cast(10 as int) as CompletedByID,​
 +GetDate() as CompletedDateTime,​
 +cast(1 as bit) as IsSummary,
 +cast(1 as bit) as IsDetail,
 +cast(null as int) as SummaryID,
 +cast(null as int) as SummaryClassTypeID,​
 +cast(0 as float) as SummaryAmount,​
 +cast(0 as float) as DetailAmount,​
 +cast(null as int) as StartGLGroupID,​
 +cast(null as int) as EndGLGroupID,​
 +cast(null as int) as AccountID,
 +cast(null as int) as AccountClassTypeID,​
 +cast(null as int) as ContactID,
 +cast(null as int) as ContactClassTypeID,​
 +cast(null as int) as TransactionID,​
 +cast(null as int) as TransactionClassTypeID,​
 +cast(0 as bit) as IsVoided,
 +cast(null as DateTime) as VoidedDateTime,​
 +cast(null as int) as VoidedEntryID,​
 +cast(null as int) as VoidedEntryClassTypeID,​
 +''​ as VoidedReason,​
 +GetDate() as QueryStartDateTime,​
 +GetDate() QueryEndDateTime,​
 +cast(null as DateTime) as ReminderDateTime,​
 +cast(0 as bit) as ReminderPrompt,​
 +cast(null as int) as PartID,
 +cast(0 as int) as ActivityType,​
 +''​ as ActivityTypeText,​
 +cast(0 as bit) as IsBillable,
 +cast(null as datetime) as BillableDateTime,​
 +cast(0 as bit) as UseActualTime,​
 +''​ as BillingNotes,​
 +cast(0 as int) as BillingType,​
 +cast(0 as float) as TotalBilledTime,​
 +cast(null as int) as RecurringActivityID,​
 +cast(null as int) as LinkID,
 +cast(null as int) as LinkStoreID,​
 +cast(null as int) as LinkClassTypeID,​
 +''​ as SpecialCode,​
 +cast(0 as bit) as HasCalendarLinks,​
 +cast(null as int) as TipRecipientID,​
 +cast(12014 as int) as PartClassTypeID,​
 +cast(null as int) as RecurringClassTypeID,​
 +cast(null as int) as StationID,
 +cast(null as int) as StationClassTypeID,​
 +--cast(null as varchar(10)) as CalendarLinksXML,​
 +cast(null as int) as CurrentState,​
 +cast(null as int) as StageID,
 +cast(null as int) as StageClassTypeID
 +from Check_CompareInventoryToLog()
 +-- Step 3
 +-- now update the InventoryLog Table
 +-- insert into InventoryLog
 +RowID + @FirstID as ID,
 +cast(-1 as int) as StoreID,
 +cast(20530 as int) ClassTypeID,​
 +'​CyriousTech'​ as ModifiedByUser,​
 +''​ as ModifiedByComputer,​
 +GetDate() as ModifiedDate,​
 +cast(0 as int) as SeqID,
 +cast(0 as bit) as IsSystem,
 +cast(1 as bit) as IsActive,
 +BilledDelta as QuantityBilled,​
 +RODelta as QuantityReceivedOnly,​
 +OnHandDelta as QuantityOnHand,​
 +ReservedDelta as QuantityReserved,​
 +AvailableDelta as QuantityAvailable,​
 +OnOrderDelta as QuantityOnOrder,​
 +ExpectedDelta as QuantityExpected,​
 +(Select UnitCost from Part where Part.ID = PartID ) as UnitCost,
 +(Select UnitCost from Part where Part.ID = PartID ) * BilledDelta as Cost,
 +cast(12014 as int) as PartClassTypeID,​
 +cast(12200 as int) as InventoryClassTypeID,​
 +cast(null as int) as TransDetailID,​
 +cast(null as int) as TransDetailClassTypeID,​
 +cast(null as int) as TransPartID,​
 +cast(null as int) as TransPartClassTypeID,​
 +cast(null as int) as ProductID,
 +cast(null as int) as ProductClassTypeID,​
 +cast(null as int) as ModifierID,
 +cast(null as int) as ModifierClassTypeID,​
 +cast(null as int) as ActivityLinkID,​
 +cast(null as int) as ActivityLinkClassTypeID,​
 +cast(null as int) as ToInventoryID,​
 +cast(null as int) as ToWarehouseID,​
 +cast(null as int) as ToDivisionID,​
 +coalesce(WarehouseID,​10) as FromWarehouseID,​
 +coalesce(DivisionID,​10) as FromDivisionID,​
 +cast(null as int) as SortIndex,
 +cast(null as int) as UnitID
 +from Check_CompareInventoryToLog()
 +===== Version Information =====
 +  * Entered : 01/2011
 +  * Version : 4.5+