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

20 lines
993 B
MySQL
Raw Normal View History

2026-10-11 14:01:11 +00:00
CREATE OR ALTER PROCEDURE [dbo].[Prc_P_PurchaseRfqFromAgreement]
@Id uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Agr uniqueidentifier, @Tenant uniqueidentifier;
2026-10-11 19:57:23 +00:00
SELECT @Agr = AgreementId, @Tenant = TenantId FROM Prc_T_PurchaseRfq WHERE Id = @Id AND Status IN ('Draft', 'Sent');
2026-10-11 14:01:11 +00:00
IF @Agr IS NULL RETURN;
2026-10-11 19:57:23 +00:00
IF EXISTS (SELECT 1 FROM Prc_T_PurchaseRfqLine WHERE PurchaseRfqId = @Id AND IsDeleted = 0) RETURN;
2026-10-11 14:01:11 +00:00
2026-10-11 19:57:23 +00:00
INSERT INTO Prc_T_PurchaseRfqLine (Id, TenantId, PurchaseRfqId, ProductId, Description, Quantity, UomId,
2026-10-11 14:01:11 +00:00
UnitPrice, TaxRate, Amount, CreationTime, IsDeleted)
2026-10-11 19:57:23 +00:00
SELECT NEWID(), @Tenant, @Id, al.ProductId, al.Description, al.Quantity - al.OrderedQuantity,
2026-10-11 14:01:11 +00:00
al.UomId, al.UnitPrice, ISNULL(p.PurchaseTaxRate, 0), 0, GETUTCDATE(), 0
FROM Prc_V_PurchaseAgreementLine al
2026-10-19 14:35:08 +00:00
LEFT JOIN Stk_T_Product p ON p.Id = al.ProductId
2026-10-11 14:01:11 +00:00
WHERE al.AgreementId = @Agr AND al.IsDeleted = 0
AND al.Quantity - al.OrderedQuantity > 0;
END