sozsoft-platform/configs/seeds/sql/object/Stk_P_TransferFromPurchaseOrder.sql
2026-10-19 17:35:08 +03:00

35 lines
2.2 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.

-- Onaylanan satınalma siparişinden siparişin operasyon türünde Taslak alım açar: konumlar operasyon türünden, satırlar
-- siparişin envanter takipli kalemlerinin henüz alınmamış miktarıdır. Siparişin iptal olmayan alımı varsa yeni alım açılmaz.
CREATE OR ALTER PROCEDURE [dbo].[Stk_P_TransferFromPurchaseOrder]
@OrderId uniqueidentifier,
@UserId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @New uniqueidentifier = NEWID();
IF EXISTS (SELECT 1 FROM Stk_T_Transfer WHERE PurchaseOrderId = @OrderId AND IsDeleted = 0 AND Status <> 'Cancelled')
RETURN;
INSERT INTO Stk_T_Transfer (Id, TenantId, BranchId, WarehouseId, OperationTypeId, OperationKind, PartnerId, SourceLocationId, DestinationLocationId,
ScheduledDate, SourceDocument, PurchaseOrderId, ResponsibleUserId, ProjectId, Status, CreationTime, CreatorId, IsDeleted)
SELECT @New, o.TenantId, ot.BranchId, ot.WarehouseId, o.OperationTypeId, ot.OperationKind, o.SupplierId, ot.SourceLocationId, ot.DestinationLocationId,
COALESCE(o.ExpectedArrival, GETUTCDATE()), o.OrderNo, o.Id, o.BuyerUserId, o.ProjectId, 'Draft', GETUTCDATE(), @UserId, 0
FROM Prc_T_PurchaseOrder o
INNER JOIN Stk_T_OperationType ot ON ot.Id = o.OperationTypeId
WHERE o.Id = @OrderId
AND EXISTS (SELECT 1 FROM Prc_T_PurchaseOrderLine l
INNER JOIN Stk_T_Product p ON p.Id = l.ProductId
WHERE l.PurchaseOrderId = o.Id AND l.IsDeleted = 0 AND p.TrackInventory = 1 AND l.Quantity > l.ReceivedQuantity);
IF @@ROWCOUNT = 0
RETURN;
INSERT INTO Stk_T_TransferLine (Id, TenantId, TransferId, ProductId, Description, DemandQuantity, Quantity, UomId, PurchaseOrderLineId, MaterialRequestItemId, CreationTime, CreatorId, IsDeleted)
SELECT NEWID(), l.TenantId, @New, l.ProductId, l.Description, l.Quantity - l.ReceivedQuantity, 0, l.UomId, l.Id, l.MaterialRequestItemId, GETUTCDATE(), @UserId, 0
FROM Prc_T_PurchaseOrderLine l
INNER JOIN Stk_T_Product p ON p.Id = l.ProductId
WHERE l.PurchaseOrderId = @OrderId AND l.IsDeleted = 0 AND p.TrackInventory = 1 AND l.Quantity > l.ReceivedQuantity;
EXEC Stk_P_TransferNumber @New;
EXEC Stk_P_TransferRecalc @New;
END