50 lines
3.4 KiB
Transact-SQL
50 lines
3.4 KiB
Transact-SQL
-- 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.
|
||
CREATE OR ALTER PROCEDURE [dbo].[Prc_P_PurchaseImportItem]
|
||
@Key nvarchar(80)
|
||
AS
|
||
BEGIN
|
||
SET NOCOUNT ON;
|
||
|
||
DECLARE @DocId uniqueidentifier = TRY_CAST(LEFT(@Key, 36) AS uniqueidentifier),
|
||
@ItemId uniqueidentifier = TRY_CAST(RIGHT(@Key, 36) AS uniqueidentifier),
|
||
@IsOrder bit = 0, @Tenant uniqueidentifier, @Branch uniqueidentifier, @Supplier uniqueidentifier, @Agr uniqueidentifier, @Status nvarchar(16),
|
||
@ProductId uniqueidentifier, @Description nvarchar(512), @Quantity decimal(18, 4), @UomId nvarchar(50),
|
||
@UnitPrice decimal(18, 4), @TaxRate decimal(18, 4), @DueDate datetime;
|
||
|
||
SELECT @Tenant = TenantId, @Branch = BranchId, @Supplier = SupplierId, @Agr = AgreementId, @Status = Status
|
||
FROM Prc_T_PurchaseRfq WHERE Id = @DocId AND IsDeleted = 0 AND Status IN ('Draft', 'Sent');
|
||
IF @Status IS NULL
|
||
SELECT @Tenant = TenantId, @Branch = BranchId, @Supplier = SupplierId, @Agr = AgreementId, @Status = Status, @IsOrder = 1
|
||
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
|
||
WHERE a.Id = @ItemId AND a.BranchId = @Branch AND (a.TenantId = @Tenant OR (a.TenantId IS NULL AND @Tenant IS NULL));
|
||
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
|