HL7 to HL7

Fix the Birth Date

If you look at the PID segment in this data you will notice the birth dates are formatted incorrectly:

For standard compliant HL7 the date should be YYYYMMDD, 19500121 in this case.

We need to write some code to alter the PID segment to reformat this field.

The first problem is to get the PID segment.

Our first attempt to code this works fine for an ADT message:

But it breaks when we look at an ORU^R01 (Lab) message:

When you open the Lab HL7 node tree in a dialog you see that the PID segment is listed under the ‘PATIENT’ group. Therefore, the Out.PID segment cannot be found.

The PID segment not located in the root of the message; to access it we need to go to Out.PATIENT.PID.

We need to write code to

This is the current code:

require 'split'
require 'zsegment'
require 'diff'

local function trace(a,b,c,d) return end

function main(Data)
   local Orig = hl7.parse  {vmd = 'transform.vmd', data = Data}

   if Orig:nodeName() == 'Catchall' then
      iguana.logInfo('Filtered '..Orig.MSH[9][1]..'^'..Orig.MSH[9][2])
      return
   end

   local Out  = hl7.message{vmd = 'transform.vmd', name = Orig:nodeName()}

   Out:mapTree(Orig)
   local Copy = zsegment.copyZSegments(Data, Out:S())
   CheckTransform(Data:StripLastReturns(), Copy:StripLastReturns())

   AlterMSH(Out.MSH)
   AlterPID(Out.PID)

   trace(Out)
   local Diff = diff.Compare(Orig:S(), Out:S(), 'transform.vmd')
   local DataOut = Out:S()
   DataOut = zsegment.copyZSegments(Data, DataOut)
   DataOut = DataOut:StripLastReturns()
   trace(DataOut)
   queue.push{data = DataOut}
end

function AlterPID(PID)

   return PID
end

function AlterMSH(MSH)
   MSH[3][1] = 'Acme'
   MSH[4][1] = 'Lab'
   return MSH
end

function CheckTransform(Orig, Copy)
   if Orig ~= Copy then
      trace(Orig)
      trace(Copy)
      error('Copy of HL7 message does not match the original')
   end   
end

function string.StripLastReturns(S)
   -- strip return(s) "\r" & "\n" from the end of a string
   local i = #S
   while S:byte(i) == 10 or S:byte(i) == 13 do
      i = i - 1
   end
   return S:sub(1,i)
end