SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'stor.[sp_FillInvoiceItem]') and xtype = N'P')
	drop procedure stor.[sp_FillInvoiceItem]
GO

-- =============================================
-- Author:		Anatoliy Pokus
-- Create date: 2026-03
-- Description:	Заполнение элемента накладной
-- =============================================
CREATE PROCEDURE stor.[sp_FillInvoiceItem]
	@InvoiceItem uniqueidentifier
AS 
BEGIN 
	set nocount on 
	
	update ii
	set ii.BillOfGoods = iiDonor.BillOfGoods, 
		ii.PackageType = iiDonor.PackageType,
		ii.TotalPackageType = iiDonor.PackageType,
		ii.TotalApplicationCount = iiDonor.TotalApplicationCount,
		ii.QuantityInPackage = iiDonor.QuantityInPackage
	from STOR.T_INVOICE_ITEM ii
	join STOR.T_INVOICE_ITEM iiDonor on iiDonor.ElementID = @InvoiceItem
	where ii.StorageInvoice = iiDonor.StorageInvoice 
	and ii.ElementID <> iiDonor.ElementID
	and SubString(ii.GTINSerialNumber, 3, 14) = SubString(iiDonor.GTINSerialNumber, 3, 14)

END
GO