sozsoft-platform/configs/seeds/sql/object/Prc_P_MaterialRequestPlanConfirm.sql
2026-10-14 22:26:03 +03:00

38 lines
1.9 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.

-- Planla diyaloğunun Onayla'sı (App.Prc.MaterialRequestPlan › UpdateCommand): talep kaleminin taslak planındaki her satırı
-- Prc_P_MaterialRequestPlan ile belgeye çevirir (önce iç transferler ve Stoktan, sonra teklif talepleri) ve taslağı siler. Tek transaction'dır:
-- bir satır reddedilirse (stok, kalan, operasyon türü) hiçbir belge açılmaz ve taslak yerinde kalır.
CREATE OR ALTER PROCEDURE [dbo].[Prc_P_MaterialRequestPlanConfirm]
@Id uniqueidentifier,
@TenantId uniqueidentifier,
@LastModifierId uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE @Method nvarchar(16), @Quantity decimal(18, 4), @Supplier uniqueidentifier, @Location uniqueidentifier, @Lot nvarchar(64);
BEGIN TRANSACTION;
IF NOT EXISTS (SELECT 1 FROM Prc_T_MaterialRequestPlanLine WITH (UPDLOCK, HOLDLOCK) WHERE MaterialRequestItemId = @Id AND IsDeleted = 0)
THROW 50201, N'Onaylanacak taslak plan yok: önce Öner butonunu kullanın ya da İç Transfer veya Satınalma sekmesinde Plan Miktarı girin.', 1;
DECLARE plan_lines CURSOR LOCAL FAST_FORWARD FOR
SELECT PlanMethod, Quantity, SupplierId, SourceLocationId, LotNo
FROM Prc_T_MaterialRequestPlanLine
WHERE MaterialRequestItemId = @Id AND IsDeleted = 0
ORDER BY CASE PlanMethod WHEN 'Transfer' THEN 0 WHEN 'Stock' THEN 1 ELSE 2 END, CreationTime;
OPEN plan_lines;
FETCH NEXT FROM plan_lines INTO @Method, @Quantity, @Supplier, @Location, @Lot;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC Prc_P_MaterialRequestPlan @Id, @Method, @Quantity, @Supplier, @Location, @Lot, @TenantId, @LastModifierId;
FETCH NEXT FROM plan_lines INTO @Method, @Quantity, @Supplier, @Location, @Lot;
END
CLOSE plan_lines;
DEALLOCATE plan_lines;
DELETE FROM Prc_T_MaterialRequestPlanLine WHERE MaterialRequestItemId = @Id;
COMMIT TRANSACTION;
END
GO