This page describes the process to recreate Part Usage Cards. This may be helpful for a specific order or for an entire system in order to correct errors in the historic inventory valuations. It should ONLY be used by a highly skilled technician as real data is permanently created and there are financial ramifications of the changes.
This process will delete all the affected Part Usage Cards, their related Journal entries, and the related GL entries for inventory and expenses from parts. It will then recreate these entries and they will be backdated to the appropriate date.
For orders that are in a Sale or Closed status, they must first be changed as Built (unless they Built Date equals the Sale Date) so that the transfer is recorded into the Cost of WIP/Built GL Account. Those entries are then backdated to the sale date.
Those orders that were Sale or Closed are changed to the final status. An Edit/Save is run on all of them to record the Expenses. Those entries and then backdated to the Sale Date.
The following caveats should be understood before running this sequence:
SELECT ID AS TransHeaderID, StatusID, StatusText, BuiltDate, SaleDate, ClosedDate INTO RecreatedPartOrders FROM TransHeader WHERE ( TransactionType IN (1,6) ) -- include Orders and Service Tickets AND ( StatusID NOT IN (1,9) ) -- skip WIP and voided orders ... ; -- For speed, create an index on this table CREATE INDEX TempIndex ON RecreatedPartOrders (TransHeaderID) ; -- Store the Last Journal ID so we can use it to determine what changes were made SELECT MAX(ID) AS JournalID, 'MaxPreBuilt' AS Description INTO RecreatedPartJournalIDs FROM Journal ;
DELETE FROM PartUsageCard WHERE TransHeaderID IN (SELECT TransHeaderID FROM RecreatedPartOrders) ;
DELETE FROM InventoryLog WHERE ID IN (SELECT ID FROM Journal WHERE ClassTypeID = 20520 AND TransactionID IN (SELECT TransHeaderID FROM RecreatedPartOrders)) ; DELETE FROM Journal WHERE ClasstypeID = 20520 AND TransactionID IN (SELECT TransHeaderID FROM RecreatedPartOrders) ;
DELETE FROM Ledger WHERE TransactionID IN (SELECT TransHeaderID FROM RecreatedPartOrders) AND PartID IS NOT NULL ;
UPDATE TransPart SET ___ = Quantity * (GET part cost) WHERE TransHeaderID IN (SELECT TransHeaderID FROM RecreatedPartOrders) ;
UPDATE TransHeader SET StatusID = 2, StatusText = 'Built', SaleDate = NULL, ClosedDate = NULL, BuiltDate = '1/1/2050' -- set the built date far in the future so we can identify these orders WHERE ( ID IN (SELECT TransHeaderID FROM RecreatedPartOrders) )
UPDATE TransHeader SET BuiltDate = (SELECT R.BuiltDate FROM RecreatedPartOrders R WHERE R.TransHeaderID = ID), StatusText = 'Built' WHERE ID IN (SELECT TransHeaderID FROM RecreatedPartOrders)
UPDATE Journal SET CompletedDateTime = (SELECT BuiltDate FROM TransHeader TH WHERE TH.ID = Journal.TransHeaderID) WHERE ID > (SELECT JournalID FROM RecreatedPartJournalIDs WHERE Description = 'MaxPreBuilt') ; -- now update all the rest of the Journal Dates UPDATE Journal SET = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime WHERE ID > (SELECT JournalID FROM RecreatedPartJournalIDs WHERE Description = 'MaxPreBuilt') ; -- now update the GL UPDATE GL SET EntryDateTime = (SELECT BuiltDate FROM TransHeader TH WHERE TH.ID = GL.TransactionID) WHERE TransactionID IS NOT NULL AND JournalID > (SELECT JournalID FROM RecreatedPartJournalIDs WHERE Description = 'MaxPreBuilt') ;
UPDATE TransHeader SET StatusID = 3, StatusText = 'Sale', ClosedDate = NULL SaleDate = '1/1/2050' -- set the sale date far in the future so we can identify these orders WHERE ID IN (SELECT TransHeaderID FROM RecreatedPartOrders WHERE StatusID IN (3,4)) ; -- Store the Last Journal ID so we can use it to determine what changes were made SELECT MAX(ID) AS JournalID, 'MaxPreSale' AS Description INTO RecreatedPartJournalIDs FROM Journal ;
UPDATE TransHeader SET SaleDate = (SELECT R.SaleDate FROM RecreatedPartOrders R WHERE R.TransHeaderID = ID), ClosedDate = (SELECT R.ClosedDate FROM RecreatedPartOrders R WHERE R.TransHeaderID = ID), StatusText = (SELECT R.StatusText FROM RecreatedPartOrders R WHERE R.TransHeaderID = ID) WHERE ID IN (SELECT TransHeaderID FROM RecreatedPartOrders AND StatusID IN (3,4)) ;
UPDATE Journal SET CompletedDateTime = (SELECT SaleDate FROM TransHeader TH WHERE TH.ID = Journal.TransHeaderID) WHERE ID > (SELECT JournalID FROM RecreatedPartJournalIDs WHERE Description = 'MaxPreSale') ; -- now update all the rest of the Journal Dates UPDATE Journal SET = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime, = CompletedDateTime WHERE ID > (SELECT JournalID FROM RecreatedPartJournalIDs WHERE Description = 'MaxPreSale') ; -- now update the GL UPDATE GL SET EntryDateTime = (SELECT SaleDate FROM TransHeader TH WHERE TH.ID = GL.TransactionID) WHERE TransactionID IS NOT NULL AND JournalID > (SELECT JournalID FROM RecreatedPartJournalIDs WHERE Description = 'MaxPreSale') ;