set nocount on

declare @StoragePDACollectionHeader uniqueidentifier
declare @PricingActionHeader        uniqueidentifier
declare @PricingActionRows          uniqueidentifier
declare @DateExecution              datetime
declare @PriceBruttoHUF             float
declare @Products                   uniqueidentifier
declare @Barcode                    nvarchar(50)
declare @RowIndex                   int

declare @IsOverrides                nvarchar(10)

set @PricingActionHeader='{0}'
set @StoragePDACollectionHeader='{1}'
set @DateExecution='{2}'
set @IsOverrides='{3}'


if @IsOverrides='True' 
begin
     delete from StoragePDACollectionRows where StoragePDACollectionHeader=@StoragePDACollectionHeader
end

declare CGU cursor fast_forward local for
select t1.Oid,t1.Products,t1.RowIndex
from pcs_whs.dbo.PricingActionRows t1
where t1.PricingActionHeader=@PricingActionHeader
order by t1.RowIndex

open CGU
fetch next from CGU into @PricingActionRows,@Products,@RowIndex
while @@FETCH_STATUS=0
begin
		set @PriceBruttoHUF=0
		
		select top 1 @Barcode=Barcode,@PriceBruttoHUF=PriceBruttoHUF 
		from PricingProductsBarcodeCustomers
		where Products=@Products and  ActualDate<=getdate() and convert(varchar(10),getdate(),111)<=convert(varchar(10),ExpirationDate,111) and Barcode not like '28%' and len(Barcode)>4
		order by IsAction desc,ActualDate desc

		insert StoragePDACollectionRows ( Oid, StoragePDACollectionHeader, Products, QTT,RowIndex, Barcode)        		
        select NEWID(),@StoragePDACollectionHeader,@Products,1,@RowIndex,@Barcode


		fetch next from CGU into @PricingActionRows,@Products,@RowIndex
end
close CGU
deallocate CGU




