DROP VIEW IF EXISTS InvoiceCreatedBy
GO 
Create View InvoiceCreatedBy
as
SELECT a.CompanyID, a.DocType, a.RefNbr, r.FieldName, b.BAccountID as EmployeeBAccountID, b.AcctCD as EmployeeAcctCD, c.ContactID as EmployeeContactID, c.UserID as EmployeeUserID,u.Username as EmployeeUserName
from ARRegister a 
inner join ARRegisterKvExt r on a.CompanyID = r.CompanyID and a.NoteID = r.RecordID 
inner join BAccount b on b.CompanyID = r.CompanyID  and b.AcctCD = r.ValueString
inner join Contact c on b.CompanyID = c.CompanyID and b.DefContactID = c.ContactID 
INNER join Users u on u.CompanyID = c.CompanyID and c.UserID = u.PKID 
where a.DocType = 'INV'