USE [MinovaES_Tulisan_Prod]
GO
/****** Object:  StoredProcedure [dbo].[STOCKADJUSTMENTINSERTITEM]    Script Date: 20/08/2025 14.37.32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[STOCKADJUSTMENTINSERTITEM]
(
 @Warehouse VARCHAR (50),
 @StockStatus VARCHAR (20),
 @MaterialID VARCHAR (100),
 @QtyAdjust INT,
 @Qty INT,
 @QtyReal INT,
 @ItemFlow VARCHAR (10),
 @UserID VARCHAR (50)
)
AS

--DECLARE @Warehouse VARCHAR (50) = '0001'
--DECLARE @StockStatus VARCHAR (20)=''
--DECLARE @MaterialID VARCHAR (100)='00000001'
--DECLARE @QtyAdjust INT = 30
--DECLARE @Qty INT = 40
--DECLARE @QtyReal INT = 10
--DECLARE @ItemFlow VARCHAR (10)='RCPT'
--DECLARE @UserID VARCHAR (50)='shofwan'

DECLARE @now VARCHAR(18) = (SELECT CONVERT(varchar(14), GETDATE(), 112) + REPLACE(CONVERT(varchar(8), GETDATE(), 108), ':', ''))

DECLARE @DocNoAddition VARCHAR (8)=RIGHT('00000000' + CAST(CAST((select distinct MAX(CurrentID) FROM PCMBSENTERPRISENUMBERDOC WHERE TableName = 'PTRMATERIALHEAD') AS INT) + 1 AS VARCHAR(8)), 8)

SELECT
 @DocNoAddition AS DocNo
 , CASE WHEN (SELECT TOP(1) DocNo FROM PTRMATERIALITEM_SA where DocNo = @DocNoAddition) IS NULL Then '1' 
	Else CAST(CAST((SELECT  MAX(CAST(DocItemID AS INT)) FROM PTRMATERIALITEM_SA where DocNo = @DocNoAddition) AS INT) + 1 AS VARCHAR(2)) END AS DocItemID
--,'1' AS DocItemID
,'' as DocItemRef
,@MaterialID as MaterialID
,'' AS ItemNumber
,'0' AS ItemType
,@ItemFlow AS ItemFlow
,@Qty AS Quantity
,'1' AS UnitMeasurement
,'' AS CustSoRef
,'' AS CustSoItemRef
,'' AS PORef
,'' AS POItemRef
,'' AS DeliveryRef
,'' AS DeliveryItemRef
,'0001' AS QualityInspectionResult
,'' AS QualityAssuranceResult
,'' AS Storage
,'' AS Bin
,'' AS Startdate
,'' AS EndDate
,NULL AS Valuation
,NULL AS RefDocTr
,NULL AS RefDocTy
,NULL AS RefDocID
,@UserID AS CreateBy
,@now AS CreateDate
,@UserID AS ChangeBy
,@now AS ChangeDate
,NULL AS Warehouse
,'' AS MaterialType
,NULL AS MAP
,0 AS NetPrice
,NULL AS NoLot
,NULL AS UnitConversion
,NULL AS QuantityDua
,'' AS MaterialDesc
,0 AS UnitPrice
,@QtyReal AS QtyReal
,@QtyAdjust AS QtyAdjust
,@StockStatus AS StockStatus
,NULL AS Notes


INSERT INTO PTRMATERIALITEM_SA
SELECT
 @DocNoAddition AS DocNo
 , CASE WHEN (SELECT TOP(1) DocNo FROM PTRMATERIALITEM_SA where DocNo = @DocNoAddition) IS NULL Then '1' 
	Else CAST(CAST((SELECT  MAX(CAST(DocItemID AS INT)) FROM PTRMATERIALITEM_SA where DocNo = @DocNoAddition) AS INT) + 1 AS VARCHAR(2)) END AS DocItemID
--,'1' AS DocItemID
,'' as DocItemRef
,@MaterialID as MaterialID
,'' AS ItemNumber
,'0' AS ItemType
,@ItemFlow AS ItemFlow
,@Qty AS Quantity
,'1' AS UnitMeasurement
,'' AS CustSoRef
,'' AS CustSoItemRef
,'' AS PORef
,'' AS POItemRef
,'' AS DeliveryRef
,'' AS DeliveryItemRef
,'0001' AS QualityInspectionResult
,'' AS QualityAssuranceResult
,'' AS Storage
,'' AS Bin
,'' AS Startdate
,'' AS EndDate
,NULL AS Valuation
,NULL AS RefDocTr
,NULL AS RefDocTy
,NULL AS RefDocID
,@UserID AS CreateBy
,@now AS CreateDate
,@UserID AS ChangeBy
,@now AS ChangeDate
,NULL AS Warehouse
,'' AS MaterialType
,NULL AS MAP
,0 AS NetPrice
,NULL AS NoLot
,NULL AS UnitConversion
,NULL AS QuantityDua
,'' AS MaterialDesc
,0 AS UnitPrice
,@QtyReal AS QtyReal
,@QtyAdjust AS QtyAdjust
,@StockStatus AS StockStatus
,NULL AS Notes


