$date = Get-Date 'September 15, 2016'

#$path = 'C:\Users\ewpmni\Desktop\einvoice\'
$path = '\\rjkprod.local\axfileshare\AXFiles\EInvoice\'

$pathSent = $path + 'sent'
$pathArchive = $path + 'archive'
$pathProcessed = $path + 'processed files'

Write-host 'Loading sent items'
$itemsSent = Get-Item "$pathSent\*.xml" | where { $_.LastWriteTime.Date -ge $date.AddMonths(-1).Date }
Write-host 'Loading processed items'
$itemsProcessed = Get-Item "$pathProcessed\*.xml" | where { $_.LastWriteTime.Date -ge $date.AddMonths(-1).Date }
Write-host 'Loading archived items'
$itemsArchive = Get-Item "$pathArchive\*.xml" | where { $_.LastWriteTime.Date -ge $date.AddMonths(-1).Date }

Write-Host 'Scanning processed items'
$processed = foreach($item in $itemsProcessed) {
    $xml = [xml](Get-Content $item)

    [pscustomobject]@{
        Path = "Processed Files\" + $item.Name
        InvoiceId = $xml.Envelope.Body.MessageParts.SalesInvoice_Einvoice.CustInvoiceJour.InvoiceId
        LastChange = $item.LastWriteTime
    }
}

Write-host 'Scanning archived items'
$archived = foreach($item in $itemsArchive) {
    $xml = [xml](Get-Content $item)

    [pscustomobject]@{
        Path = "Archived\" + $item.Name
        InvoiceId = $xml.StatusMessage.DocumentResponse.DocumentReference.ID
        LastChange = $item.LastWriteTime
    }
}

Write-Host 'Scanning sent items'
$sent = foreach($item in $itemsSent) {
    $invoiceId = $item.Name.Substring('DKDSB'.Length).Replace('.xml','')

    [pscustomobject]@{
        Path = "Sent\" + $item.Name
        InvoiceId = $invoiceId
        LastChange = $item.LastWriteTime
    }
}


# logic happens here

Write-Host 'Finding sent-processed-archived relations'
$relation = foreach($item in ($archived | sort LastChange)) {
    # we dont want to look at archived files more than 7 days older than the $date specified
    
    if($item.LastChange.Date -lt $date.AddDays(-7).Date) {
        continue
    }

    [pscustomobject]@{
        ArchivedItem = $item
        ProcessedItem = $processed | where { $_.InvoiceId -eq $item.InvoiceId } | Select-Object Path, LastChange
        SentItem = $sent | where { $_.InvoiceId -eq $item.InvoiceId } | Select-Object Path, LastChange
    }
}

$i = 0
$dupes = foreach($item in ($sent | sort LastChange)) { 
    if($item.LastChange.Date -lt $date.AddDays(-7).Date) {
        continue
    }

    [pscustomobject]@{
        ArchivedItem = ($archived | where { $_.InvoiceId -eq $item.InvoiceId } | % { "Path:$($_.Path), InvoiceId:$($_.InvoiceId);" }) -join "`n"
        ProcessedItem = ($processed | where { $_.InvoiceId -eq $item.InvoiceId } | % { "Path:$($_.Path), InvoiceId:$($_.InvoiceId);" }) -join "`n"
        #SentItem = $sent | where { $_.InvoiceId -eq $item.InvoiceId } | Select-Object Path, LastChange
        SentItem = $item
    }

    $i++
    Write-Progress -Activity "Finding dupes" -Status "$i of $(@($sent).Count))"
}



$relation | ConvertTo-Html -Head '<style>table { border: 1px solid black } td { border: 1px solid black; padding: 2px }</style>' | Set-Content relations.html
$dupes | ConvertTo-Html -Head '<style>table { border: 1px solid black } td { border: 1px solid black; padding: 2px }</style>' | Set-Content dupes.html