﻿set nocount on

declare @StoragePDACollectionHeader uniqueidentifier
declare @StoragePDACollectionRows   uniqueidentifier
declare @Barcode                    nvarchar(50)
declare @Barcode2                   nvarchar(50)
declare @MasterKey                  uniqueidentifier 
declare @PriceBruttoHUF             float
declare @PriceDiscountHUF           float
declare @Products                   uniqueidentifier
declare @ActualDate                 datetime
declare @ExpirationDate             datetime
declare @PricingCodeDescription     nvarchar(255)
declare @QTT                        int 
declare @J                          int
set @StoragePDACollectionHeader='{0}'
set @MasterKey='{1}'

begin tran 
delete from PricingProductsBarcodePrint Where MasterKey=@MasterKey

declare CSPDAR cursor local fast_forward for

select Oid,Products,ltrim(rtrim(isnull(Barcode,''))),convert(int,QTT)
from StoragePDACollectionRows
where StoragePDACollectionHeader=@StoragePDACollectionHeader
order by RowIndex

open CSPDAR
fetch next from CSPDAR into @StoragePDACollectionRows,@Products,@Barcode,@QTT
while @@FETCH_STATUS=0
begin
		
		-- Eladási ár meghatározása
        set @PriceBruttoHUF=0
		
		if @Barcode=''
		begin
			select top 1 @Barcode2=Barcode,@PriceBruttoHUF=PriceBruttoHUF,@ActualDate=ActualDate,@ExpirationDate=ExpirationDate,@PricingCodeDescription=PricingCodeDescription 
			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
		end
		else
		begin
			select top 1 @Barcode2=Barcode,@PriceBruttoHUF=PriceBruttoHUF,@ActualDate=ActualDate,@ExpirationDate=ExpirationDate,@PricingCodeDescription=PricingCodeDescription 
			from PricingProductsBarcodeCustomers
			where Barcode=@Barcode 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
		end
		set @PriceDiscountHUF=@PriceBruttoHUF
		if substring(@Barcode,1,2)='99'
		begin
		     select top 1 @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
			       and Barcode not like '99%'
		     order by IsAction desc,ActualDate desc
		end

		set @J=0
		while @J<@QTT
		begin
			if isnull(@Barcode,'')=''
			begin
					insert PricingProductsBarcodePrint ( Oid, MasterKey, Products, RowIndex, Barcode, PriceBruttoHUF, PriceDiscountHUF)
					select NEWID(),'{1}',t1.Products,t1.RowIndex,@Barcode2,@PriceBruttoHUF,@PriceBruttoHUF
					from StoragePDACollectionRows t1
					where Oid=@StoragePDACollectionRows
			end
			else
			begin
					insert PricingProductsBarcodePrint ( Oid, MasterKey, Products, RowIndex, Barcode, PriceBruttoHUF, PriceDiscountHUF,ActualDate,ExpirationDate,PricingCodeDescription)
					select NEWID(),'{1}',t1.Products,t1.RowIndex,@Barcode,@PriceBruttoHUF,@PriceDiscountHUF,@ActualDate,@ExpirationDate,@PricingCodeDescription
					from StoragePDACollectionRows t1
					where Oid=@StoragePDACollectionRows
			end
			set @J=@J+1
		end
		fetch next from CSPDAR into @StoragePDACollectionRows,@Products,@Barcode,@QTT
end
close CSPDAR
deallocate CSPDAR

commit

