sozsoft-platform/configs/seeds/sql/object/Prc_P_PurchaseRfqCreateAlternative.sql

43 lines
2.8 KiB
MySQL
Raw Normal View History

2026-10-11 19:57:23 +00:00
-- Teklifler sekmesinde Ekle ile seçilen tedarikçi 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]
@Id uniqueidentifier,
@SupplierId uniqueidentifier,
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @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, CurrencyId, OrderDeadline,
ExpectedArrival, RequireConfirmation, OperationTypeId, BuyerUserId, ProjectId, IncotermId, IncotermLocation, PaymentTermId,
Terms, PriceWeight, DeliveryWeight, CreationTime, CreatorId, IsDeleted)
SELECT @New, r.TenantId, @No, r.TenderGroupId, c.Id, NULL, COALESCE(c.SupplierCurrency, r.CurrencyId), r.OrderDeadline,
r.ExpectedArrival, r.RequireConfirmation, r.OperationTypeId, 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