sozsoft-platform/configs/seeds/sql/object/Prc_P_PurchaseRfqCreateAlternative.sql
2026-10-12 13:27:54 +03:00

43 lines
3 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Alternatif Oluştur › Tedarikçiler'de seçilen kontak (@Key = teklif|kontak) için belgenin kopyasını aynı ihalede açar: kalemler TenderLineId ile
-- birlikte kopyalanır, fiyat tedarikçinin fiyat listesinden, para birimi ve koşullar kontak kartından gelir.
CREATE OR ALTER PROCEDURE [dbo].[Prc_P_PurchaseRfqCreateAlternative]
@Key nvarchar(80),
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Id uniqueidentifier = TRY_CAST(LEFT(@Key, 36) AS uniqueidentifier),
@SupplierId uniqueidentifier = TRY_CAST(RIGHT(@Key, 36) AS uniqueidentifier),
@Tenant uniqueidentifier, @Status nvarchar(16), @Group uniqueidentifier, @New uniqueidentifier = NEWID(), @No nvarchar(128);
SELECT @Tenant = TenantId, @Status = Status, @Group = TenderGroupId FROM Prc_T_PurchaseRfq WHERE Id = @Id;
IF ISNULL(@Status, '') NOT IN ('Draft', 'Sent')
THROW 50164, N'Siparişe dönüştürülmüş ya da iptal edilmiş teklife alternatif eklenemez.', 1;
IF EXISTS (SELECT 1 FROM Prc_T_PurchaseRfq WHERE TenderGroupId = @Group AND SupplierId = @SupplierId AND IsDeleted = 0 AND Status <> 'Cancelled')
THROW 50167, N'Bu tedarikçinin ihalede zaten bir teklif talebi var.', 1;
EXEC Adm_P_SequenceNext N'PurchaseRfq', @Tenant, @No OUTPUT;
INSERT INTO Prc_T_PurchaseRfq (Id, TenantId, RfqNo, TenderGroupId, SupplierId, AgreementId, BranchId, OperationTypeId, WarehouseId, CurrencyId, OrderDeadline,
ExpectedArrival, BuyerUserId, ProjectId, IncotermId, IncotermLocation, PaymentTermId,
Terms, PriceWeight, DeliveryWeight, CreationTime, CreatorId, IsDeleted)
SELECT @New, r.TenantId, @No, r.TenderGroupId, c.Id, NULL, r.BranchId, r.OperationTypeId, r.WarehouseId, COALESCE(c.SupplierCurrency, r.CurrencyId), r.OrderDeadline,
r.ExpectedArrival, r.BuyerUserId, r.ProjectId,
COALESCE(c.PurchaseIncotermId, r.IncotermId), COALESCE(c.PurchaseIncotermLocation, r.IncotermLocation),
COALESCE(c.PurchasePaymentTermId, r.PaymentTermId), r.Terms, r.PriceWeight, r.DeliveryWeight, GETUTCDATE(), @UserId, 0
FROM Prc_T_PurchaseRfq r
INNER JOIN Adm_T_Contact c ON c.Id = @SupplierId
WHERE r.Id = @Id;
INSERT INTO Prc_T_PurchaseRfqLine (Id, TenantId, PurchaseRfqId, TenderLineId, ProductId, Description, Quantity, UomId,
UnitPrice, TaxRate, Amount, DeliveryDate, MaterialRequestItemId, CreationTime, CreatorId, IsDeleted)
SELECT NEWID(), l.TenantId, @New, l.TenderLineId, l.ProductId, l.Description, l.Quantity, l.UomId,
ISNULL((SELECT TOP 1 ps.Price FROM Inv_T_ProductSupplier ps
WHERE ps.ProductId = l.ProductId AND ps.ContactId = @SupplierId AND ps.IsDeleted = 0
ORDER BY ps.MinQuantity), 0),
l.TaxRate, 0, l.DeliveryDate, l.MaterialRequestItemId, GETUTCDATE(), @UserId, 0
FROM Prc_T_PurchaseRfqLine l
WHERE l.PurchaseRfqId = @Id AND l.IsDeleted = 0;
EXEC Prc_P_PurchaseRfqRecalc @New;
END