Project

General

Profile

Bug #1519 » PFILOUPDATEMDSTOCKINFO_20220826.sql

Tri Rizqiaty, 08/26/2022 03:09 PM

 
1
??ALTER PROCEDURE [dbo].[PFILOUPDATEMDSTOCKINFO]
2

3
(
4

5
	@MaterialID VARCHAR(20)
6

7
	,@Quantity DECIMAL(22,0)
8

9
	,@TotalPrice DECIMAL(20,0)
10

11
	,@DocType VARCHAR(20)
12

13
	,@UserID VARCHAR(20)
14

15
	,@CompanyID VARCHAR(20)
16

17
	,@LogisticArea VARCHAR(20)
18

19
	,@Warehouse VARCHAR(20)
20

21
	,@Storage VARCHAR(20)
22

23
	,@Bin VARCHAR(20)
24

25
	,@ReceiverLogisticArea VARCHAR(20)
26

27
	,@ReceiverWarehouse VARCHAR(20) 
28

29
	,@ReceiverStorage VARCHAR(20)
30

31
	,@ReceiverBin VARCHAR(20)
32

33
	,@DocIDRef VARCHAR(20)
34

35
)
36

37
AS
38

39

40

41
--DECLARE @MaterialID VARCHAR(20) = '00000003'
42

43
--DECLARE @Quantity DECIMAL(22,0) = 1
44

45
--DECLARE @TotalPrice DECIMAL(20,0) = 0
46

47
--DECLARE @DocType VARCHAR(20) = 'GICU'
48

49
--DECLARE @UserID VARCHAR(20) = 'chani'
50

51
--DECLARE @CompanyID VARCHAR(20) = ''
52

53
--DECLARE @LogisticArea VARCHAR(20) = ''
54

55
--DECLARE @Warehouse VARCHAR(20) = '0001'
56

57
--DECLARE @Storage VARCHAR(20) = 'ST01'
58

59
--DECLARE @Bin VARCHAR(20) = 'R1-L1-0001'
60

61
--DECLARE @ReceiverLogisticArea VARCHAR(20) = ''
62

63
--DECLARE @ReceiverWarehouse VARCHAR(20) = ''
64

65
--DECLARE @ReceiverStorage VARCHAR(20) = ''
66

67
--DECLARE @ReceiverBin VARCHAR(20) = ''
68

69
--DECLARE @DocIDRef VARCHAR(20) = '00000020'
70

71

72

73
DECLARE @now VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmdd')
74

75
DECLARE @now16 VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmddhhnnss')
76

77

78

79
DECLARE @IsExist VARCHAR(20)
80

81
DECLARE @ExistStock DECIMAL(22,0)
82

83
DECLARE @ExistTotalMAP DECIMAL(22,0)
84

85
DECLARE @NewStock DECIMAL(22,0)
86

87
DECLARE @NewTotalMAP DECIMAL(22,0)
88

89
DECLARE @NewEachMAP DECIMAL(22,0)
90

91

92

93
DECLARE @IsSuccess VARCHAR(250)
94

95

96

97
SELECT DISTINCT @ExistStock = SUM(CONVERT(DECIMAL(22,0),Qty)), @ExistTotalMAP = SUM(CONVERT(DECIMAL(22,0),mat4.TotalValuation)), @IsExist = COUNT(mat4.MaterialID)
98

99
FROM dbo.PMDMAT0004 AS mat4
100

101
WHERE mat4.MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
102

103
	AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '') 
104

105

106

107
DECLARE @MaxSeqMateExist INT
108

109
SELECT @MaxSeqMateExist = MAX(SeqID) FROM PMDMAT0004 WHERE MaterialID = @MaterialID
110

111

112

113
IF(@DocType = 'RCSP')
114

115
BEGIN
116

117
	SET @NewStock = @ExistStock + @Quantity
118

119
	SET @NewEachMAP = @TotalPrice / @Quantity
120

121
	SET @NewTotalMAP = @NewEachMAP * @NewStock
122

123

124

125
	IF (@IsExist > 0)
126

127
	BEGIN
128

129
		SET @IsSuccess = 'Y'
130

131
		UPDATE dbo.PMDMAT0004
132

133
		SET Qty = @NewStock
134

135
			,TotalValuation = @NewTotalMAP
136

137
			,ChangeBy = @UserID
138

139
			,ChangeDate = @now16
140

141
		WHERE MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
142

143
			AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
144

145
	END
146

147
	ELSE IF (@IsExist = 0)
148

149
	BEGIN
150

151
		SET @IsSuccess = 'Y'	
152

153
		INSERT INTO dbo.PMDMAT0004
154

155
		SELECT DISTINCT @MaterialID	
156

157
						,@MaxSeqMateExist + 1
158

159
						,@CompanyID
160

161
						,@LogisticArea
162

163
						,@Warehouse
164

165
						,@Storage
166

167
						,@Bin
168

169
						,'0001'
170

171
						,'122324'
172

173
						,CONVERT(VARCHAR(4),@Quantity)
174

175
						,CONVERT(DECIMAL(18,2),@TotalPrice)
176

177
						,1
178

179
						,'113124'
180

181
						,@UserID AS CreateBy
182

183
						,@now16 AS CreateDate
184

185
						,@UserID AS ChangeBy
186

187
						,@now16 AS ChangeDate
188

189
						,'' AS SORDID 
190

191
						,'' AS PRODID
192

193
						,'' AS INTRID
194

195
						,'' AS POID 
196

197

198

199
	END
200

201
END
202

203
IF(@DocType = 'GICU')
204

205
BEGIN
206

207
	IF(ISNULL(@ExistStock,0) < @Quantity)
208

209
	BEGIN 
210

211
		SET @IsSuccess = 'OVERQTY' 
212

213
	END
214

215
	ELSE
216

217
	BEGIN
218

219
		SET @NewStock = @ExistStock - @Quantity
220

221
		SET @NewEachMAP = @ExistTotalMAP / @ExistStock  ---- menggunakan MAP sebelumnya
222

223
		SET @NewTotalMAP = @NewEachMAP * @NewStock
224

225
		--SELECT @NewStock, @NewEachMAP, @NewTotalMAP
226

227
		IF (@IsExist > 0)
228

229
		BEGIN
230

231
			SET @IsSuccess = 'Y'
232

233
			UPDATE dbo.PMDMAT0004
234

235
			SET Qty = CONVERT(VARCHAR(4), ISNULL(@NewStock,0))
236

237
				,TotalValuation = ISNULL(@NewTotalMAP,0)
238

239
				,ChangeBy = @UserID
240

241
				,ChangeDate = @now16
242

243
			WHERE MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
244

245
				AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
246

247
		END
248

249
	END
250

251
END
252

253

254

255
SELECT @IsSuccess AS IsSuccess
256

257

258

259

260

261

262

263

264

265

266

267

268

269

(3-3/5)