
	;with prices as (
		select itemID, d.firmID, 'PLN' currency, correctedItemPrice, sysCorrectedItemPrice
		, isnull(cd.documentDate, d.documentDate) documentDate, d.accDate, isnull(cd.modDate, d.modDate) modDate
		, d.documentFullNR
		, ROW_NUMBER() OVER(PARTITION BY itemID, d.firmID ORDER BY isnull(cd.documentDate, d.documentDate) DESC, d.accDate DESC, dl.documentOrdinalNumber DESC, d.documentFullNR DESC) rowno
		from document.documentLine dl
		inner join document.document d on d.documentID = dl.documentID and d.firmID is not null
		inner join document.documentType dt on dt.documentType = d.documentType and dt.masterDocumentCategory = 'warehouse'	and dt.altersLastPrice = 1
		left join document.document cd on d.initialCorrectedDocumentId = cd.documentID
		where dl.[status] = 1
	)
	, pricesMaxNoPerDay as (
		select itemID, firmID, currency, documentDate, max(rowno) maxrowno from prices group by itemID, firmID, currency, documentDate
	)
	insert into [wms].[itemSupplier] 
	(
		[itemSupplierID],[itemID],[firmID],[lastPurchasePrice],[lastPurchasePriceDate],[addDate],[addUsername],[modDate],[modUsername]	
	) 
	select newID(), lpp.itemID, lpp.firmID, lpp.sysCorrectedItemPrice, lpp.documentDate, getutcdate(), 'sys', getutcdate(), 'sys'
	from (
		select * from prices src where src.rowno = 1
	) lpp
	inner join pricesMaxNoPerDay pmax on pmax.itemID = lpp.itemID and pmax.firmID = lpp.firmID and pmax.currency = lpp.currency and lpp.documentDate = pmax.documentDate
	inner join prices pfirst on pfirst.itemID = lpp.itemID and pfirst.firmID = lpp.firmID and pfirst.currency = lpp.currency and pfirst.rowno = pmax.maxrowno
	left join prices p2 on p2.itemID = lpp.itemID and p2.firmID = lpp.firmID and p2.currency = lpp.currency and p2.rowno = pmax.maxrowno - 1 and p2.rowno > 1
	full join [wms].[itemSupplier] isup on isup.itemID = lpp.itemID and isup.firmID = lpp.firmID
	where (isnull(lpp.sysCorrectedItemPrice, -1) != isnull(isup.lastPurchasePrice, -1)
	and isnull(pfirst.sysCorrectedItemPrice, -1) != isnull(isup.lastPurchasePrice, -1)
	and (p2.sysCorrectedItemPrice is null or isnull(p2.sysCorrectedItemPrice, -1) != isnull(isup.lastPurchasePrice, -1)))
	and isup.itemID is null and isup.firmID is null
	order by documentDate desc


	;with prices as (
		select itemID, d.firmID, 'PLN' currency, correctedItemPrice, sysCorrectedItemPrice
		, isnull(cd.documentDate, d.documentDate) documentDate, d.accDate, isnull(cd.modDate, d.modDate) modDate
		, d.documentFullNR
		, ROW_NUMBER() OVER(PARTITION BY itemID, d.firmID ORDER BY isnull(cd.documentDate, d.documentDate) DESC, d.accDate DESC, dl.documentOrdinalNumber DESC, d.documentFullNR DESC) rowno
		from document.documentLine dl
		inner join document.document d on d.documentID = dl.documentID and d.firmID is not null
		inner join document.documentType dt on dt.documentType = d.documentType and dt.masterDocumentCategory = 'warehouse'	and dt.altersLastPrice = 1
		left join document.document cd on d.initialCorrectedDocumentId = cd.documentID
		where dl.[status] = 1
	)
	, pricesMaxNoPerDay as (
		select itemID, firmID, currency, documentDate, max(rowno) maxrowno from prices group by itemID, firmID, currency, documentDate
	)
	update [wms].[itemSupplier] 
	set lastPurchasePrice = lpp.sysCorrectedItemPrice, lastPurchasePriceDate = lpp.documentDate
	from (
		select * from prices src where src.rowno = 1
	) lpp
	inner join pricesMaxNoPerDay pmax on pmax.itemID = lpp.itemID and pmax.firmID = lpp.firmID and pmax.currency = lpp.currency and lpp.documentDate = pmax.documentDate
	inner join prices pfirst on pfirst.itemID = lpp.itemID and pfirst.firmID = lpp.firmID and pfirst.currency = lpp.currency and pfirst.rowno = pmax.maxrowno
	left join prices p2 on p2.itemID = lpp.itemID and p2.firmID = lpp.firmID and p2.currency = lpp.currency and p2.rowno = pmax.maxrowno - 1 and p2.rowno > 1
	full join [wms].[itemSupplier] isup on isup.itemID = lpp.itemID and isup.firmID = lpp.firmID
	where (isnull(lpp.sysCorrectedItemPrice, -1) != isnull(isup.lastPurchasePrice, -1)
	and isnull(pfirst.sysCorrectedItemPrice, -1) != isnull(isup.lastPurchasePrice, -1)
	and (p2.sysCorrectedItemPrice is null or isnull(p2.sysCorrectedItemPrice, -1) != isnull(isup.lastPurchasePrice, -1)))


