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

51 lines
3.4 KiB
MySQL
Raw Normal View History

2026-10-12 10:27:54 +00:00
-- Talep Kalemleri sekmesinde seçilen kalemi (@Key = belge|kalem) teklif talebine ya da taslak siparişe kopyalar.
-- Kalem yalnız belgenin şubesindeki talepten gelir. Fiyat: sözleşme kalemi › tedarikçi fiyatı › 0; termin talebin istenen tarihidir.
2026-10-11 19:57:23 +00:00
CREATE OR ALTER PROCEDURE [dbo].[Prc_P_PurchaseImportItem]
2026-10-12 10:27:54 +00:00
@Key nvarchar(80)
2026-10-11 19:57:23 +00:00
AS
BEGIN
SET NOCOUNT ON;
DECLARE @DocId uniqueidentifier = TRY_CAST(LEFT(@Key, 36) AS uniqueidentifier),
@ItemId uniqueidentifier = TRY_CAST(RIGHT(@Key, 36) AS uniqueidentifier),
2026-10-12 10:27:54 +00:00
@IsOrder bit = 0, @Tenant uniqueidentifier, @Branch uniqueidentifier, @Supplier uniqueidentifier, @Agr uniqueidentifier, @Status nvarchar(16),
2026-10-11 19:57:23 +00:00
@ProductId uniqueidentifier, @Description nvarchar(512), @Quantity decimal(18, 4), @UomId nvarchar(50),
@UnitPrice decimal(18, 4), @TaxRate decimal(18, 4), @DueDate datetime;
2026-10-12 10:27:54 +00:00
SELECT @Tenant = TenantId, @Branch = BranchId, @Supplier = SupplierId, @Agr = AgreementId, @Status = Status
2026-10-11 19:57:23 +00:00
FROM Prc_T_PurchaseRfq WHERE Id = @DocId AND IsDeleted = 0 AND Status IN ('Draft', 'Sent');
IF @Status IS NULL
2026-10-12 10:27:54 +00:00
SELECT @Tenant = TenantId, @Branch = BranchId, @Supplier = SupplierId, @Agr = AgreementId, @Status = Status, @IsOrder = 1
2026-10-11 19:57:23 +00:00
FROM Prc_T_PurchaseOrder WHERE Id = @DocId AND IsDeleted = 0 AND Status = 'Draft';
IF @Status IS NULL
THROW 50161, N'Talep kalemi yalnız teklif aşamasındaki teklif talebine ya da taslak siparişe aktarılabilir.', 1;
SELECT @ProductId = a.ProductId, @Description = a.RequestReason, @Quantity = a.Quantity, @UomId = a.UomId, @DueDate = a.DueDate,
@TaxRate = ISNULL(p.PurchaseTaxRate, 0),
@UnitPrice = COALESCE((SELECT TOP 1 al.UnitPrice FROM Prc_T_PurchaseAgreementLine al
WHERE al.AgreementId = @Agr AND al.ProductId = a.ProductId AND al.IsDeleted = 0),
(SELECT TOP 1 ps.Price FROM Inv_T_ProductSupplier ps
WHERE ps.ProductId = a.ProductId AND ps.ContactId = @Supplier AND ps.IsDeleted = 0
ORDER BY ps.MinQuantity), 0)
FROM Prc_V_MaterialRequestAvailableItem a
LEFT JOIN Inv_T_Product p ON p.Id = a.ProductId
2026-10-12 10:27:54 +00:00
WHERE a.Id = @ItemId AND a.BranchId = @Branch AND (a.TenantId = @Tenant OR (a.TenantId IS NULL AND @Tenant IS NULL));
2026-10-11 19:57:23 +00:00
IF @ProductId IS NULL
THROW 50162, N'Talep kalemi artık aktarılamaz: başka bir teklife ya da siparişe aktarılmış veya talep değişmiş.', 1;
IF @IsOrder = 1
BEGIN
INSERT INTO Prc_T_PurchaseOrderLine (Id, TenantId, PurchaseOrderId, ProductId, Description, Quantity, UomId,
UnitPrice, TaxRate, Amount, DeliveryDate, MaterialRequestItemId, CreationTime, IsDeleted)
VALUES (NEWID(), @Tenant, @DocId, @ProductId, @Description, @Quantity, @UomId, @UnitPrice, @TaxRate, 0, @DueDate, @ItemId, GETUTCDATE(), 0);
EXEC Prc_P_PurchaseOrderRecalc @DocId;
END
ELSE
BEGIN
INSERT INTO Prc_T_PurchaseRfqLine (Id, TenantId, PurchaseRfqId, ProductId, Description, Quantity, UomId,
UnitPrice, TaxRate, Amount, DeliveryDate, MaterialRequestItemId, CreationTime, IsDeleted)
VALUES (NEWID(), @Tenant, @DocId, @ProductId, @Description, @Quantity, @UomId, @UnitPrice, @TaxRate, 0, @DueDate, @ItemId, GETUTCDATE(), 0);
EXEC Prc_P_PurchaseRfqRecalc @DocId;
END
END