Project

General

Profile

Bug #1519 » PFILOUPDATEMDSTOCKINFOMTRI_20220826.sql

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

 
1
??USE [MinovaES_Demo]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[PFILOUPDATEMDSTOCKINFOMTRI]    Script Date: 26/08/2022 11.10.21 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[PFILOUPDATEMDSTOCKINFOMTRI]
16

17
(
18

19
	@MaterialID VARCHAR(20)
20

21
	,@Quantity DECIMAL(22,0)
22

23
	,@TotalPrice DECIMAL(20,0)
24

25
	,@DocType VARCHAR(20)
26

27
	,@UserID VARCHAR(20)
28

29
	,@CompanyID VARCHAR(20)
30

31
	,@LogisticArea VARCHAR(20)
32

33
	,@Warehouse VARCHAR(20)
34

35
	,@Storage VARCHAR(20)
36

37
	,@Bin VARCHAR(20)
38

39
	,@ReceiverLogisticArea VARCHAR(20)
40

41
	,@ReceiverWarehouse VARCHAR(20) 
42

43
	,@ReceiverStorage VARCHAR(20)
44

45
	,@ReceiverBin VARCHAR(20)
46

47
	,@DocIDRef VARCHAR(20)
48

49
	,@DocStatus VARCHAR(20)
50

51
)
52

53
AS
54

55

56

57
--DECLARE @MaterialID VARCHAR(20) = '00000003'
58

59
--DECLARE @Quantity DECIMAL(22,0) = 100
60

61
--DECLARE @TotalPrice DECIMAL(20,0) = 0
62

63
--DECLARE @DocType VARCHAR(20) = 'INTR'
64

65
--DECLARE @UserID VARCHAR(20) = 'tri'
66

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

69
--DECLARE @LogisticArea VARCHAR(20) = ''
70

71
--DECLARE @Warehouse VARCHAR(20) = '0001'
72

73
--DECLARE @Storage VARCHAR(20) = ''
74

75
--DECLARE @Bin VARCHAR(20) = ''
76

77
--DECLARE @ReceiverLogisticArea VARCHAR(20) = ''
78

79
--DECLARE @ReceiverWarehouse VARCHAR(20) = '0003'
80

81
--DECLARE @ReceiverStorage VARCHAR(20) = ''
82

83
--DECLARE @ReceiverBin VARCHAR(20) = ''
84

85
--DECLARE @DocIDRef VARCHAR(20) = ''
86

87
--DECLARE @DocStatus VARCHAR(20) = '2'
88

89

90

91
DECLARE @now VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmdd')
92

93
DECLARE @now16 VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmddhhnnss')
94

95

96

97
DECLARE @IsExist VARCHAR(20)
98

99
DECLARE @ExistStock DECIMAL(22,0)
100

101
DECLARE @ExistTotalMAP DECIMAL(22,0)
102

103
DECLARE @ExistData DECIMAL(22,0)
104

105
DECLARE @IsSuccess VARCHAR(250)
106

107

108

109
IF(@DocType = 'INTR')
110

111
BEGIN
112

113
	IF(@DocStatus = '2' )
114

115
	BEGIN
116

117
		SELECT DISTINCT @ExistStock = qty, @ExistTotalMAP = TotalValuation
118

119
		FROM PMDMAT0004 AS mat4
120

121
		WHERE StockStatus = '0001' AND (MaterialID = @MaterialID OR @MaterialID = '')
122

123
				AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
124

125
		--SELECT @ExistStock
126

127
		DECLARE @MaxSeqMateExist INT
128

129
		SELECT @MaxSeqMateExist = MAX(SeqID) FROM PMDMAT0004 WHERE MaterialID = @MaterialID
130

131

132

133
		IF(@ExistStock > 0)
134

135
		BEGIN
136

137
			SET @IsSuccess = 'Y'
138

139
			INSERT INTO dbo.PMDMAT0004
140

141
			SELECT DISTINCT @MaterialID					
142

143
						,@CompanyID
144

145
						,@MaxSeqMateExist + 1
146

147
						,@LogisticArea
148

149
						,@Warehouse
150

151
						,@Storage
152

153
						,@Bin
154

155
						,'0004'
156

157
						,'122324'
158

159
						,CONVERT(VARCHAR(4),@Quantity) * -1
160

161
						,CONVERT(DECIMAL(18,2),@TotalPrice)
162

163
						,1
164

165
						,'113124'
166

167
						,@UserID AS CreateBy
168

169
						,@now16 AS CreateDate
170

171
						,@UserID AS ChangeBy
172

173
						,@now16 AS ChangeDate
174

175
						,'' AS SORDID 
176

177
						,'' AS PRODID
178

179
						,@DocIDRef AS INTRID
180

181
						,'' AS POID 
182

183

184

185
			INSERT INTO dbo.PMDMAT0004
186

187
			SELECT DISTINCT @MaterialID	
188

189
						,@MaxSeqMateExist + 2
190

191
						,@CompanyID
192

193
						,@LogisticArea
194

195
						,@ReceiverWarehouse
196

197
						,@ReceiverStorage
198

199
						,@ReceiverBin
200

201
						,'0004'
202

203
						,'122324'
204

205
						,CONVERT(VARCHAR(4),@Quantity)
206

207
						,CONVERT(DECIMAL(18,2),@TotalPrice)
208

209
						,1
210

211
						,'113124'
212

213
						,@UserID AS CreateBy
214

215
						,@now16 AS CreateDate
216

217
						,@UserID AS ChangeBy
218

219
						,@now16 AS ChangeDate
220

221
						,'' AS SORDID 
222

223
						,'' AS PRODID
224

225
						,@DocIDRef AS INTRID
226

227
						,'' AS POID 
228

229
		END
230

231
		ELSE
232

233
		BEGIN
234

235
			SET @IsSuccess = 'NoData' 
236

237
		END
238

239
	END
240

241
	IF(@DocStatus = '3' )
242

243
	BEGIN
244

245
		SELECT DISTINCT @ExistData = COUNT(INTRID), @ExistStock = SUM(CONVERT(DECIMAL(22,0),Qty)), @ExistTotalMAP = SUM(CONVERT(DECIMAL(22,0),mat4.TotalValuation)), @IsExist = COUNT(mat4.MaterialID)
246

247
		FROM dbo.PMDMAT0004 AS mat4
248

249
		WHERE INTRID = @DocIDRef AND (MaterialID = @MaterialID OR @MaterialID = '')
250

251

252

253
		IF(@ExistData <= 0)
254

255
		BEGIN
256

257
			SET @IsSuccess = 'NoData' 
258

259
		END
260

261
		ELSE
262

263
		BEGIN
264

265
			SET @IsSuccess = 'Y'
266

267

268

269
			DECLARE @c_intrid VARCHAR(20)
270

271
			DECLARE @c_materialid VARCHAR(20)
272

273
			DECLARE @c_stockstatus VARCHAR(20)
274

275
			DECLARE @c_warehouse VARCHAR(20)
276

277
			DECLARE @c_storage VARCHAR(20)
278

279
			DECLARE @c_bin VARCHAR(20)
280

281
			DECLARE @c_quantity DECIMAL(18,0)
282

283

284

285
			DECLARE cur_mat4 CURSOR FOR
286

287
			SELECT INTRID, MaterialID, StockStatus, Warehouse, Storage, Bin, Qty FROM PMDMAT0004 WHERE (INTRID = @DocIDRef)
288

289
			OPEN cur_mat4
290

291
			FETCH cur_mat4 INTO @c_intrid, @c_materialid, @c_stockstatus, @c_warehouse, @c_storage, @c_bin, @c_quantity
292

293
			WHILE @@Fetch_Status = 0
294

295
			BEGIN
296

297

298

299
				SET @ExistStock = 0
300

301
				SET @ExistTotalMAP = 0
302

303
				DECLARE @NewStock DECIMAL(22,0)
304

305
				DECLARE @NewTotalMAP DECIMAL(22,0)
306

307
				DECLARE @NewEachMAP DECIMAL(22,0)
308

309

310

311
				SELECT DISTINCT @ExistStock = qty, @ExistTotalMAP = TotalValuation
312

313
				FROM PMDMAT0004 AS mat4
314

315
				WHERE StockStatus = '0001' AND (MaterialID = @c_materialid OR @c_materialid = '')
316

317
					 AND (Warehouse = @c_warehouse OR @c_warehouse = '') AND (Storage = @c_storage OR @c_storage = '') AND (Bin = @c_bin OR @c_bin = '')
318

319

320

321
				SET @NewStock = @ExistStock + @c_quantity
322

323
				SET @NewEachMAP = @ExistTotalMAP / @ExistStock  ---- menggunakan MAP sebelumnya
324

325
				SET @NewTotalMAP = @NewEachMAP * @NewStock
326

327

328

329
				UPDATE dbo.PMDMAT0004
330

331
				SET Qty = @NewStock
332

333
					,TotalValuation = @NewTotalMAP
334

335
					,ChangeBy = @UserID
336

337
					,ChangeDate = @now16
338

339
				WHERE StockStatus = '0001' AND (MaterialID = @c_materialid OR @c_materialid = '')
340

341
					 AND (Warehouse = @c_warehouse OR @c_warehouse = '') AND (Storage = @c_storage OR @c_storage = '') AND (Bin = @c_bin OR @c_bin = '') 
342

343

344

345
				DELETE FROM PMDMAT0004 
346

347
				WHERE INTRID = @c_intrid AND (MaterialID = @c_materialid OR @c_materialid = '')
348

349
					 AND (Warehouse = @c_warehouse OR @c_warehouse = '') AND (Storage = @c_storage OR @c_storage = '') AND (Bin = @c_bin OR @c_bin = '') 
350

351

352

353
			FETCH cur_mat4 INTO @c_intrid, @c_materialid, @c_stockstatus, @c_warehouse, @c_storage, @c_bin, @c_quantity
354

355
			END
356

357
			CLOSE cur_mat4
358

359
			DEALLOCATE cur_mat4
360

361
		END
362

363
	END	
364

365
END	
366

367
IF(@DocType = 'GINT')
368

369
BEGIN	
370

371
	SELECT DISTINCT @ExistData = COUNT(INTRID), @ExistStock = SUM(CONVERT(DECIMAL(22,0),Qty)), @ExistTotalMAP = SUM(CONVERT(DECIMAL(22,0),mat4.TotalValuation)), @IsExist = COUNT(mat4.MaterialID)
372

373
	FROM dbo.PMDMAT0004 AS mat4
374

375
	WHERE MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
376

377
		AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
378

379

380

381
	IF(ISNULL(@ExistStock,0) < @Quantity)
382

383
	BEGIN 
384

385
		SET @IsSuccess = 'OVERQTY' 
386

387
	END
388

389
	ELSE
390

391
	BEGIN
392

393
		SET @NewStock = @ExistStock - @Quantity
394

395
		SET @NewEachMAP = @ExistTotalMAP / @ExistStock  ---- menggunakan MAP sebelumnya
396

397
		SET @NewTotalMAP = @NewEachMAP * @NewStock
398

399

400

401
		IF (@IsExist > 0)
402

403
		BEGIN
404

405
			SET @IsSuccess = 'Y'
406

407
			UPDATE dbo.PMDMAT0004
408

409
			SET Qty = CONVERT(VARCHAR(4), ISNULL(@NewStock,0))
410

411
				,TotalValuation = ISNULL(@NewTotalMAP,0)
412

413
				,ChangeBy = @UserID
414

415
				,ChangeDate = @now16
416

417
			WHERE MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
418

419
				AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
420

421
		END
422

423
	END
424

425
END
426

427

428

429
SELECT @IsSuccess AS IsSuccess
430

431

432

433

434

435

436

437

438

439

440

441

442

443

(4-4/5)