Project

General

Profile

Bug #3154 » PFILOUPDATEMDSTOCKINFO_20241101.sql

Tri Rizqiaty, 11/01/2024 05:00 PM

 
1
??USE [MinovaES_DEMO_Retail]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[PFILOUPDATEMDSTOCKINFO]    Script Date: 01/11/2024 16.55.54 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15

16

17
ALTER PROCEDURE [dbo].[PFILOUPDATEMDSTOCKINFO]
18

19
(
20

21
	@MaterialID VARCHAR(20)
22

23
	,@Quantity DECIMAL(22,2)
24

25
	,@TotalPrice DECIMAL(20,2)
26

27
	,@DocType VARCHAR(20)
28

29
	,@UserID VARCHAR(20)
30

31
	,@CompanyID VARCHAR(20)
32

33
	,@LogisticArea VARCHAR(20)
34

35
	,@Warehouse VARCHAR(20)
36

37
	,@Storage VARCHAR(20)
38

39
	,@Bin VARCHAR(20)
40

41
	,@ReceiverLogisticArea VARCHAR(20)
42

43
	,@ReceiverWarehouse VARCHAR(20) 
44

45
	,@ReceiverStorage VARCHAR(20)
46

47
	,@ReceiverBin VARCHAR(20)
48

49
	,@DocIDRef VARCHAR(20)
50

51
	,@DocTypeRef VARCHAR(20)
52

53
	,@TransTypeRef VARCHAR(20)
54

55
	,@ItemFlow VARCHAR(50)
56

57
	,@StockStatus VARCHAR(20)
58

59
	,@DocStatus VARCHAR(20)
60

61
)
62

63
AS
64

65

66

67
--DECLARE @MaterialID VARCHAR(20) = '00000012'
68

69
--DECLARE @Quantity DECIMAL(22,2) = 10
70

71
--DECLARE @TotalPrice DECIMAL(20,2) = 0
72

73
--DECLARE @DocType VARCHAR(20) = 'RCSP'
74

75
--DECLARE @UserID VARCHAR(20) = 'tri'
76

77
--DECLARE @CompanyID VARCHAR(20) = '1000'
78

79
--DECLARE @LogisticArea VARCHAR(20) = ''
80

81
--DECLARE @Warehouse VARCHAR(20) = '0001'
82

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

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

87
--DECLARE @ReceiverLogisticArea VARCHAR(20) = ''
88

89
--DECLARE @ReceiverWarehouse VARCHAR(20) = ''
90

91
--DECLARE @ReceiverStorage VARCHAR(20) = ''
92

93
--DECLARE @ReceiverBin VARCHAR(20) = ''
94

95
--DECLARE @DocIDRef VARCHAR(20) = '00000247'
96

97
--DECLARE @DocTypeRef VARCHAR(20) = 'PO'
98

99
--DECLARE @TransTypeRef VARCHAR(20) = 'PUR'
100

101
--DECLARE @ItemFlow VARCHAR(50) = ''--'ISSU' --ISSU --RCPT
102

103
--DECLARE @StockStatus VARCHAR(20) = ''
104

105
--DECLARE @DocStatus VARCHAR(20) = '3' -- 1:Draft , 2:In Process, 3:Done, 4:Cancel
106

107

108

109
DECLARE @now VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmdd')
110

111
DECLARE @now16 VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmddhhnnss')
112

113

114

115
DECLARE @IsSuccess VARCHAR(250)
116

117

118

119
IF(@StockStatus = '')
120

121
BEGIN SET @StockStatus = '0001' END
122

123

124

125
IF(@Quantity < 0)
126

127
BEGIN SET @Quantity = @Quantity * -1 END
128

129

130

131
DECLARE @TblMAT4 TABLE
132

133
(
134

135
	[MaterialID] [VARCHAR](10) NOT NULL,
136

137
	[SeqID] [BIGINT] NOT NULL,
138

139
	[CompanyID] [VARCHAR](4) NOT NULL,
140

141
	[LogisticAreaID] [VARCHAR](10) NOT NULL,
142

143
	[Warehouse] [VARCHAR](20) NOT NULL,
144

145
	[Storage] [VARCHAR](10) NOT NULL,
146

147
	[Bin] [VARCHAR](10) NOT NULL,
148

149
	[StockStatus] [VARCHAR](10) NOT NULL,
150

151
	[GLValuationID] [VARCHAR](20) NULL,
152

153
	[Qty] DECIMAL(22,2) NULL,
154

155
	[TotalValuation] [DECIMAL](18, 2) NULL,
156

157
	[Sequence] [INT] NULL,
158

159
	[SerialNumber] [VARCHAR](50) NULL,
160

161
	[CreateBy] [VARCHAR](18) NULL,
162

163
	[CreateDate] [VARCHAR](14) NULL,
164

165
	[ChangeBy] [VARCHAR](18) NULL,
166

167
	[ChangeDate] [VARCHAR](14) NULL,
168

169
	[SORDID] [VARCHAR](18) NULL,
170

171
	[PRODID] [VARCHAR](18) NULL,
172

173
	[INTRID] [VARCHAR](18) NULL,
174

175
	[POID] [VARCHAR](18) NULL,
176

177
	[StockType] [VARCHAR](10) NULL,
178

179
	[DocIDRef] [VARCHAR](10) NULL,
180

181
	[DocTypeRef] [VARCHAR](10) NULL,
182

183
	[TransTypeRef] [VARCHAR](10) NULL
184

185
	--,HargaSatuan DECIMAL(22,2)
186

187
)
188

189
INSERT INTO @TblMAT4
190

191
SELECT [MaterialID]
192

193
      ,[SeqID]
194

195
      ,[CompanyID]
196

197
      ,[LogisticAreaID]
198

199
      ,[Warehouse]
200

201
      ,[Storage]
202

203
      ,[Bin]
204

205
      ,[StockStatus]
206

207
      ,[GLValuationID]
208

209
      ,[Qty]
210

211
      ,[TotalValuation]
212

213
      ,[Sequence]
214

215
      ,[SerialNumber]
216

217
      ,[CreateBy]
218

219
      ,[CreateDate]
220

221
      ,[ChangeBy]
222

223
      ,[ChangeDate]
224

225
      ,[SORDID]
226

227
      ,[PRODID]
228

229
      ,[INTRID]
230

231
      ,[POID]
232

233
      ,[StockType]
234

235
      ,[DocIDRef]
236

237
      ,[DocTypeRef]
238

239
      ,[TransTypeRef]
240

241
	  --,HargaSatuan
242

243
FROM dbo.PMDMAT0004
244

245
WHERE MaterialID = @MaterialID
246

247

248

249
DECLARE @MAP_MAT1 DECIMAL(22,2) = 0 
250

251
DECLARE @NewTotalValuation DECIMAL(22,2)
252

253
DECLARE @CekItemNotDoneAloc VARCHAR(20)
254

255
DECLARE @CekItemNotDone VARCHAR(20)
256

257
DECLARE @SisaQty DECIMAL(22,2) = @Quantity
258

259

260

261
IF(@DocType = 'GICU' AND (@DocStatus = '3' OR @DocStatus = '2') )
262

263
BEGIN
264

265
	SELECT @MAP_MAT1 = MAP FROM dbo.PMDMAT0001 WHERE MaterialID = @MaterialID
266

267

268

269
	IF(@DocIDRef <> '' AND @DocTypeRef <> '' AND @TransTypeRef <>'')		---- // jika doc ref dr sales order
270

271
	BEGIN
272

273

274

275
		DECLARE @SumMat4Aloc DECIMAL(22,2) = 0
276

277
		SELECT @SumMat4Aloc = SUM(Qty)	FROM @TblMAT4 
278

279
		WHERE MaterialID = @MaterialID AND DocIDRef = @DocIDRef AND DocTypeRef = @DocTypeRef AND TransTypeRef = @TransTypeRef AND Warehouse = @Warehouse --AND StockStatus = @StockStatus
280

281

282

283
		IF(@Quantity > @SumMat4Aloc)
284

285
		BEGIN
286

287
			SET @IsSuccess = 'OVERQTY'
288

289
		END
290

291
		ELSE
292

293
		BEGIN 
294

295

296

297
			DECLARE @QtyAlocateMAT4 DECIMAL(22,2) = 0
298

299
			DECLARE @c_materialidA VARCHAR(20)
300

301
			DECLARE @c_seqA VARCHAR(20)
302

303
			DECLARE @c_qtyA VARCHAR(20)
304

305
			DECLARE cur_mat4A CURSOR FOR (SELECT MaterialID, SeqID, Qty FROM @TblMAT4 WHERE MaterialID = @MaterialID
306

307
										AND DocIDRef = @DocIDRef AND DocTypeRef = @DocTypeRef AND TransTypeRef = @TransTypeRef AND Warehouse = @Warehouse) --AND StockStatus = @StockStatus )
308

309
			OPEN cur_mat4A
310

311
			FETCH cur_mat4A INTO @c_materialidA, @c_seqA, @c_qtyA
312

313
			WHILE @@Fetch_Status = 0
314

315
			BEGIN
316

317
			
318

319
				SET @QtyAlocateMAT4 = @c_qtyA
320

321
				
322

323
				IF(@SisaQty = 0)
324

325
				BEGIN
326

327
					BREAK
328

329
				END
330

331
				ELSE IF(@QtyAlocateMAT4 > @SisaQty)				----// case good issue sebagian(partial)
332

333
				BEGIN
334

335
					DECLARE @SisaQtyAlocateMAT4 DECIMAL(22,2) = 0
336

337
					SET @SisaQtyAlocateMAT4 = @QtyAlocateMAT4 - @SisaQty
338

339

340

341
					UPDATE dbo.PMDMAT0004					----// update quantity mat4
342

343
					SET Qty = @SisaQtyAlocateMAT4
344

345
						,TotalValuation = @SisaQtyAlocateMAT4 * @MAP_MAT1
346

347
						,ChangeBy = @UserID
348

349
						,ChangeDate = @now16
350

351
					--SELECT 'update qty mat4', @SisaQtyAlocateMAT4, * FROM dbo.PMDMAT0004
352

353
					WHERE MaterialID = @MaterialID
354

355
						AND DocIDRef = @DocIDRef 
356

357
						AND DocTypeRef = @DocTypeRef 
358

359
						AND TransTypeRef = @TransTypeRef
360

361
						AND Warehouse = @Warehouse 
362

363
						AND SeqID = @c_seqA
364

365
				
366

367
					DECLARE @MaxDocItemID DECIMAL(22,0)
368

369
					SELECT @MaxDocItemID = MAX(DocItemID)
370

371
					FROM dbo.PTRALLOCATION WITH(NOLOCK)
372

373
					WHERE DocNo = @DocIDRef
374

375

376

377
					UPDATE dbo.PTRALLOCATION					----// update quantity ptraloc
378

379
					SET StockAllocation = @SisaQtyAlocateMAT4
380

381
						,ChangeBy = @UserID
382

383
						,ChangeDate = @now16
384

385
					--SELECT  'udpate stock ptraloc', @SisaQtyAlocateMAT4, * FROM dbo.PTRALLOCATION WITH(NOLOCK)
386

387
					WHERE DocNo = @DocIDRef
388

389
						AND MaterialID = @MaterialID
390

391
						AND WareHouse = @Warehouse
392

393
						AND ItemStatus = '1'
394

395
						AND StockAllocation = @c_qtyA
396

397

398

399
					INSERT INTO PTRALLOCATION
400

401
					SELECT DISTINCT	[DocNo] 
402

403
					--SELECT DISTINCT 'add ptraloc', DocNo
404

405
						,CONVERT(VARCHAR(20),@MaxDocItemID + 1) AS [DocItemID]
406

407
						,@SisaQty AS StockAllocation
408

409
						,[WareHouse]
410

411
						,[Storage]
412

413
						,[Bin]
414

415
						,[TransferOrderRef]
416

417
						,[TransferOrderItem]
418

419
						,[PurchaseOrderRef]
420

421
						,[PurchaseOrderItem]
422

423
						,[ProductionOrderRef]
424

425
						,[ProductionOrderItem]
426

427
						,@UserID
428

429
						,@now16
430

431
						,@UserID
432

433
						,@now16
434

435
						,[MaterialID]
436

437
						,[StockType]
438

439
						,[MAP]
440

441
						,'2' AS ItemStatus				----// 2 = Done
442

443
						,[ItemType]
444

445
						,IsAllocated
446

447
					FROM dbo.PTRALLOCATION
448

449
					WHERE MaterialID = @MaterialID
450

451
						AND DocNo = @DocIDRef 
452

453
						AND Warehouse = @Warehouse 
454

455
						AND ItemStatus = '1'
456

457

458

459
					SET @IsSuccess = 'Y' 
460

461

462

463
					BREAK
464

465
				END
466

467
				ELSE IF(@SisaQty >= @QtyAlocateMAT4)
468

469
				BEGIN
470

471
					SET @SisaQtyAlocateMAT4 = 0
472

473
					SET @SisaQty = @SisaQty - @QtyAlocateMAT4
474

475

476

477
					DELETE FROM dbo.PMDMAT0004						--// delete mat4
478

479
					--SELECT 'delete mat4',* FROM dbo.PMDMAT0004 
480

481
					WHERE MaterialID = @MaterialID AND SeqID = @c_seqA
482

483

484

485
					UPDATE dbo.PTRALLOCATION						--// update ptraloc
486

487
					SET ItemStatus = '2'
488

489
						,ChangeBy = @UserID
490

491
						,ChangeDate = @now16
492

493
					--SELECT'update items status ptraloc jd 2', * FROM dbo.PTRALLOCATION
494

495
					WHERE MaterialID = @c_materialidA
496

497
						AND DocNo = @DocIDRef 
498

499
						AND Warehouse = @Warehouse
500

501
						AND ItemStatus = '1'
502

503
						AND StockAllocation = @c_qtyA	
504

505

506

507
					SET @IsSuccess = 'Y'
508

509
				END
510

511
				FETCH cur_mat4A INTO @c_materialidA, @c_seqA, @c_qtyA 
512

513
			END
514

515
			CLOSE cur_mat4A
516

517
			DEALLOCATE cur_mat4A
518

519

520

521
			SELECT @CekItemNotDoneALoc = COUNT(ItemStatus)
522

523
			FROM dbo.PTRALLOCATION 
524

525
			WHERE DocNo = @DocIDRef AND (ItemStatus = '0' OR ItemStatus = '1') AND MaterialID = @MaterialID
526

527
			
528

529
			IF(@CekItemNotDoneALoc = 0)
530

531
			BEGIN
532

533
				UPDATE dbo.PTRSALESITEMS
534

535
				SET ItemStatus = '2'
536

537
				WHERE DocNo = @DocIDRef AND Material = @MaterialID
538

539
			END
540

541
			ELSE
542

543
			BEGIN
544

545
				UPDATE dbo.PTRSALESITEMS
546

547
				SET ItemStatus = '1'
548

549
				WHERE DocNo = @DocIDRef AND Material = @MaterialID
550

551
			END
552

553

554

555
			SELECT @CekItemNotDone = COUNT(ItemStatus)
556

557
			FROM dbo.PTRALLOCATION 
558

559
			WHERE DocNo = @DocIDRef AND (ItemStatus = '0' OR ItemStatus = '1')
560

561

562

563
			IF(@CekItemNotDone = 0)
564

565
			BEGIN
566

567
				UPDATE dbo.PTRSALESHEADER
568

569
				SET ProcessStatus = 'C'
570

571
				WHERE DocNo = @DocIDRef 
572

573
			END
574

575

576

577

578

579
		END
580

581
	END
582

583
	ELSE
584

585
	BEGIN
586

587
		DECLARE @SumQtyMAT4 DECIMAL(22,2)
588

589
		SELECT @SumQtyMAT4 =  SUM(CONVERT(DECIMAL(22,2),Qty)) FROM @TblMAT4 
590

591
		WHERE (CompanyID = @CompanyID OR @CompanyID = '')
592

593
			AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
594

595
			AND (Warehouse = @Warehouse OR @Warehouse = '')
596

597
			AND (Storage = @Storage OR @Storage = '')
598

599
			AND StockStatus = @StockStatus
600

601

602

603
		IF(@Quantity <= @SumQtyMAT4)
604

605
		BEGIN
606

607
			DECLARE @SisaQuantity DECIMAL(22,2) = @Quantity
608

609
			DECLARE @c_materialid VARCHAR(20)
610

611
			DECLARE @c_seq VARCHAR(20)
612

613
			DECLARE @c_qty VARCHAR(20)
614

615
			DECLARE cur_mat4 CURSOR FOR (SELECT MaterialID, SeqID, Qty FROM @TblMAT4 WHERE (CompanyID = @CompanyID OR @CompanyID = '') 
616

617
										AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '') AND (Warehouse = @Warehouse OR @Warehouse = '')	
618

619
										AND (Storage = @Storage OR @Storage = '') AND (Storage = @Bin OR @Bin = '') AND StockStatus = @StockStatus)
620

621
			OPEN cur_mat4
622

623
			FETCH cur_mat4 INTO @c_materialid, @c_seq, @c_qty
624

625
			WHILE @@Fetch_Status = 0
626

627
			BEGIN
628

629
				IF(@SisaQuantity <= 0)
630

631
				BREAK
632

633

634

635
				DECLARE @SisaQtyMat4 DECIMAL(22,2)
636

637
				IF(@SisaQuantity > @c_qty)
638

639
				BEGIN 
640

641
					SET @SisaQtyMat4 = 0
642

643
					SET @SisaQuantity = @SisaQuantity - @c_qty
644

645
				END
646

647
				ELSE IF(@SisaQuantity < @c_qty)
648

649
				BEGIN  
650

651
					SET @SisaQtyMat4 = @c_qty - @SisaQuantity
652

653
					SET @SisaQuantity = @SisaQuantity - @c_qty	 
654

655
				END
656

657
				ELSE IF(@SisaQuantity = @c_qty)
658

659
				BEGIN 
660

661
					SET @SisaQtyMat4 = 0
662

663
					SET @SisaQuantity = @SisaQuantity - @c_qty
664

665
				END
666

667

668

669
				SET @NewTotalValuation = @MAP_MAT1 * @SisaQtyMat4
670

671
				UPDATE dbo.PMDMAT0004
672

673
				SET Qty = @SisaQtyMat4
674

675
					,TotalValuation = @NewTotalValuation
676

677
					,ChangeBy = @UserID
678

679
					,ChangeDate = @now16
680

681
				--SELECT * FROM dbo.PMDMAT0004
682

683
				WHERE MaterialID = @c_materialid
684

685
					AND SeqID = @c_seq
686

687

688

689
			FETCH cur_mat4 INTO @c_materialid, @c_seq, @c_qty 
690

691
			END
692

693
			CLOSE cur_mat4
694

695
			DEALLOCATE cur_mat4
696

697

698

699
			SET @IsSuccess = 'Y'
700

701
		END
702

703
		ELSE 
704

705
		BEGIN
706

707
			SET @IsSuccess = 'OVERQTY'
708

709
		END
710

711
	END
712

713
END
714

715

716

717

718

719
IF(@DocType = 'RCSP' AND (@DocStatus = '3' OR @DocStatus = '2') )
720

721
BEGIN
722

723
	DECLARE @IsMat4Exist VARCHAR(20) = ISNULL((SELECT COUNT(*) FROM @TblMAT4),0)
724

725
	SELECT @MAP_MAT1 = MAP FROM dbo.PMDMAT0001 WHERE MaterialID = @MaterialID
726

727

728

729
	DECLARE @TotalValuationAllMAT4 DECIMAL(22,2)
730

731
	DECLARE @TotalQtyAllMAT4 DECIMAL(22,2)
732

733
	DECLARE @TotalValuationAll DECIMAL(22,2)
734

735
	DECLARE @TotalQtyAll DECIMAL(22,2)
736

737
	DECLARE @NewMAP DECIMAL(22,2)
738

739

740

741
	IF(@IsMat4Exist <= 0)
742

743
	BEGIN
744

745
		
746

747
		IF(@DocIDRef <> '')
748

749
		BEGIN
750

751
			SELECT @NewMAP = (CONVERT(DECIMAL(22,2),TotAmount) / CONVERT(DECIMAL(22,0),MaterialQuantity))
752

753
			FROM dbo.PTRPURCHITEM
754

755
			WHERE DocNo = @DocIDRef
756

757
				AND Material = @MaterialID
758

759
		END
760

761
		ELSE
762

763
		BEGIN
764

765
			SELECT @NewMAP = @MAP_MAT1
766

767
		END
768

769

770

771
		INSERT INTO PMDMAT0004
772

773
		SELECT @MaterialID
774

775
			  ,'1'
776

777
			  ,@CompanyID
778

779
			  ,@LogisticArea
780

781
			  ,@Warehouse
782

783
			  ,@Storage
784

785
			  ,@Bin
786

787
			  ,@StockStatus --0001
788

789
			  ,''
790

791
			  ,@Quantity
792

793
			  ,@Quantity * @NewMAP AS TotalValuation
794

795
			  ,'1' AS [Sequence]
796

797
			  ,'' AS [SerialNumber]
798

799
			  ,@UserID
800

801
			  ,@now16
802

803
			  ,@UserID
804

805
			  ,@now16
806

807
			  ,'' AS [SORDID]
808

809
			  ,'' AS [PRODID]
810

811
			  ,'' AS [INTRID]
812

813
			  ,'' AS [POID]
814

815
			  ,'1' AS [StockType]
816

817
			  ,'' AS [DocIDRef]
818

819
			  ,'' AS [DocTypeRef]
820

821
			  ,'' AS [TransTypeRef]
822

823
			  --,@MAP_MAT1 AS HargaSatuan
824

825
		SET @IsSuccess = 'Y'
826

827

828

829
		UPDATE dbo.PMDMAT0001
830

831
		SET MAP = @NewMAP
832

833
		WHERE MaterialID = @MaterialID
834

835
	END
836

837
	ELSE
838

839
	BEGIN
840

841
		DECLARE @MaxSeqMat4Detail VARCHAR(20) = (SELECT MAX(SeqID) FROM @TblMAT4)
842

843

844

845
		DECLARE @IsMat4ExistDetail VARCHAR(20) 
846

847
		SELECT @IsMat4ExistDetail = COUNT(MaterialID)
848

849
		FROM @TblMAT4
850

851
		WHERE (CompanyID = @CompanyID OR @CompanyID = '')
852

853
			AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
854

855
			AND (Warehouse = @Warehouse OR @Warehouse = '')
856

857
			AND (Storage = @Storage OR @Storage = '')
858

859
			AND (Storage = @Bin OR @Bin = '')
860

861
			AND (StockStatus = @StockStatus)
862

863

864

865
		DECLARE @PricePurSatuan DECIMAL(22,2) = 0
866

867
		IF(@DocIDRef <> '')
868

869
		BEGIN
870

871
			SELECT @PricePurSatuan = (CONVERT(DECIMAL(22,2),TotAmount) / CONVERT(DECIMAL(22,0),MaterialQuantity))
872

873
			FROM dbo.PTRPURCHITEM
874

875
			WHERE DocNo = @DocIDRef
876

877
				AND Material = @MaterialID
878

879
		END
880

881
		ELSE
882

883
		BEGIN
884

885
			SELECT @PricePurSatuan = @TotalPrice / @Quantity
886

887
		END
888

889
		SET @TotalValuationAllMAT4 = (SELECT SUM(CONVERT(DECIMAL(22,2),Qty)) FROM @TblMAT4) * @MAP_MAT1 --(SELECT SUM(CONVERT(DECIMAL(22,2),TotalValuation)) FROM @TblMAT4)
890

891
		SET @TotalQtyAllMAT4 = (SELECT SUM(CONVERT(DECIMAL(22,2),Qty)) FROM @TblMAT4)
892

893
		SET @TotalValuationAll = @TotalValuationAllMAT4 + (@PricePurSatuan * @Quantity) --@TotalPrice
894

895
		SET @TotalQtyAll = @TotalQtyAllMAT4 + @Quantity
896

897
		SET @NewMAP = @TotalValuationAll / @TotalQtyAll
898

899
		SET @NewTotalValuation = @NewMAP * @Quantity
900

901

902

903
		IF(@IsMat4ExistDetail <= 0)
904

905
		BEGIN
906

907
			INSERT INTO PMDMAT0004
908

909
			SELECT @MaterialID
910

911
				  ,@MaxSeqMat4Detail + 1
912

913
				  ,@CompanyID
914

915
				  ,@LogisticArea
916

917
				  ,@Warehouse
918

919
				  ,@Storage
920

921
				  ,@Bin
922

923
				  ,@StockStatus--'0001'
924

925
				  ,''
926

927
				  ,@Quantity
928

929
				  ,@NewTotalValuation AS TotalValuation
930

931
				  ,'1' AS [Sequence]
932

933
				  ,'' AS [SerialNumber]
934

935
				  ,@UserID
936

937
				  ,@now16
938

939
				  ,@UserID
940

941
				  ,@now16
942

943
				  ,'' AS [SORDID]
944

945
				  ,'' AS [PRODID]
946

947
				  ,'' AS [INTRID]
948

949
				  ,'' AS [POID]
950

951
				  ,'1' AS [StockType]
952

953
				  ,'' AS [DocIDRef]
954

955
				  ,'' AS [DocTypeRef]
956

957
				  ,'' AS [TransTypeRef]
958

959
			
960

961
			UPDATE dbo.PMDMAT0001
962

963
			SET MAP = @NewMAP
964

965
				,ChangeBy = @UserID
966

967
				,ChangeDate = @now16
968

969
			FROM dbo.PMDMAT0001
970

971
			WHERE MaterialID = @MaterialID
972

973

974

975
			UPDATE dbo.PMDMAT0004
976

977
			SET TotalValuation = (CONVERT(DECIMAL(22,2),Qty) * @NewMAP)
978

979
				,ChangeBy = @UserID
980

981
				,ChangeDate = @now16
982

983
			WHERE (MaterialID = @MaterialID)
984

985

986

987
			SET @IsSuccess = 'Y'
988

989
		END
990

991
		ELSE
992

993
		BEGIN
994

995
			SET @TotalValuationAllMAT4 = (SELECT SUM(CONVERT(DECIMAL(22,2),Qty)) FROM @TblMAT4) * @MAP_MAT1 --(SELECT SUM(CONVERT(DECIMAL(22,2),TotalValuation)) FROM @TblMAT4)
996

997
			SET @TotalQtyAllMAT4 = (SELECT SUM(CONVERT(DECIMAL(22,2),Qty)) FROM @TblMAT4)
998

999
			SET @TotalValuationAll = @TotalValuationAllMAT4 + (@PricePurSatuan * @Quantity) --@TotalPrice
1000

1001
			SET @TotalQtyAll = @TotalQtyAllMAT4 + @Quantity
1002

1003
			SET @NewMAP = @TotalValuationAll / @TotalQtyAll
1004

1005

1006

1007
			DECLARE @MinSeqMat4 VARCHAR(20)
1008

1009
			SELECT @MinSeqMat4 = MIN(SeqID)
1010

1011
			FROM @TblMAT4
1012

1013
			WHERE (CompanyID = @CompanyID OR @CompanyID = '')
1014

1015
				AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
1016

1017
				AND (Warehouse = @Warehouse OR @Warehouse = '')
1018

1019
				AND (Storage = @Storage OR @Storage = '')
1020

1021
				AND (Storage = @Bin OR @Bin = '')
1022

1023
				AND (StockStatus = @StockStatus)
1024

1025
			
1026

1027
			DECLARE @NewQty DECIMAL(22,2)
1028

1029
			SELECT @NewQty = CONVERT(DECIMAL(22,2),Qty) + @Quantity
1030

1031
				,@NewTotalValuation = (CONVERT(DECIMAL(22,2),Qty) + @Quantity) * @NewMAP
1032

1033
			FROM @TblMAT4
1034

1035
			WHERE (CompanyID = @CompanyID OR @CompanyID = '')
1036

1037
				AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
1038

1039
				AND (Warehouse = @Warehouse OR @Warehouse = '')
1040

1041
				AND (Storage = @Storage OR @Storage = '')
1042

1043
				AND (Storage = @Bin OR @Bin = '')
1044

1045
				AND (SeqID = @MinSeqMat4)
1046

1047
 
1048

1049
			IF(@DocIDRef <> '' AND @DocTypeRef = 'PO' AND @TransTypeRef <> '')
1050

1051
			BEGIN
1052

1053
				DECLARE @QtyPurchaseReal DECIMAL(22,1) = 0
1054

1055
				SELECT @QtyPurchaseReal = PurchaseRealization
1056

1057
				FROM dbo.PTRPURCHREALIZATION
1058

1059
				WHERE DocNo = @DocIDRef
1060

1061
					AND MaterialID = @MaterialID 
1062

1063
					AND ItemStatus = '1'
1064

1065
				--SELECT @Quantity, @QtyPurchaseReal
1066

1067
				IF(@Quantity < @QtyPurchaseReal)
1068

1069
				BEGIN
1070

1071
					UPDATE dbo.PMDMAT0004
1072

1073
					SET Qty = @NewQty
1074

1075
						,TotalValuation = @NewTotalValuation
1076

1077
						,ChangeBy = @UserID
1078

1079
						,ChangeDate = @now16
1080

1081
					WHERE (CompanyID = @CompanyID OR @CompanyID = '')
1082

1083
						AND (MaterialID = @MaterialID)
1084

1085
						AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
1086

1087
						AND (Warehouse = @Warehouse OR @Warehouse = '')
1088

1089
						AND (Storage = @Storage OR @Storage = '')
1090

1091
						AND (Storage = @Bin OR @Bin = '')
1092

1093
						AND (SeqID = @MinSeqMat4)
1094

1095

1096

1097
					UPDATE dbo.PMDMAT0001
1098

1099
					SET MAP = @NewMAP
1100

1101
						,ChangeBy = @UserID
1102

1103
						,ChangeDate = @now16
1104

1105
					FROM dbo.PMDMAT0001
1106

1107
					WHERE MaterialID = @MaterialID
1108

1109

1110

1111
					UPDATE dbo.PMDMAT0004
1112

1113
					SET TotalValuation = (CONVERT(DECIMAL(22,2),Qty) * @NewMAP)
1114

1115
						,ChangeBy = @UserID
1116

1117
						,ChangeDate = @now16
1118

1119
					WHERE (MaterialID = @MaterialID)
1120

1121
						AND (SeqID <> @MinSeqMat4)	
1122

1123

1124

1125
					DECLARE @MaxSeqPurReal DECIMAL(22,2) = 0
1126

1127
					SELECT @MaxSeqPurReal = MAX(DocItemID)
1128

1129
					FROM dbo.PTRPURCHREALIZATION
1130

1131
					WHERE DocNo = @DocIDRef
1132

1133
					
1134

1135
					INSERT INTO PTRPURCHREALIZATION
1136

1137
					SELECT @DocIDRef AS [DocNo]
1138

1139
						  ,@MaxSeqPurReal + 1 AS [DocItemID]
1140

1141
						  ,[ItemType]
1142

1143
						  ,[MaterialID]
1144

1145
						  ,@Quantity
1146

1147
						  ,[MaterialUnit]
1148

1149
						  ,@CompanyID
1150

1151
						  ,@LogisticArea
1152

1153
						  ,@Warehouse
1154

1155
						  ,@Storage
1156

1157
						  ,@Bin
1158

1159
						  ,'2' AS ItemStatus
1160

1161
						  ,@Quantity * @PricePurSatuan AS TotAmount
1162

1163
						  ,[StockType]
1164

1165
						  ,@UserID
1166

1167
						  ,@now16
1168

1169
						  ,@UserID
1170

1171
						  ,@now16
1172

1173
					FROM dbo.PTRPURCHREALIZATION 
1174

1175
					WHERE DocNo = @DocIDRef 
1176

1177
						AND MaterialID = @MaterialID
1178

1179
						AND ItemStatus = '1'
1180

1181

1182

1183
					UPDATE dbo.PTRPURCHREALIZATION
1184

1185
					SET PurchaseRealization = (PurchaseRealization - @Quantity)
1186

1187
						,TotAmount = (PurchaseRealization - @Quantity) * @PricePurSatuan
1188

1189
						,ChangeBy = @UserID
1190

1191
						,ChangeDate = @now16
1192

1193
					WHERE DocNo = @DocIDRef 
1194

1195
						AND MaterialID = @MaterialID
1196

1197
						AND ItemStatus = '1'
1198

1199

1200

1201
					UPDATE dbo.PTRPURCHITEM 
1202

1203
					SET ItemStatus = '1'
1204

1205
						,ChangeBy = @UserID
1206

1207
						,ChangeDate = @now16
1208

1209
					WHERE DocNo = @DocIDRef
1210

1211
						AND Material = @MaterialID
1212

1213

1214

1215
					SET @IsSuccess = 'Y'
1216

1217
				END
1218

1219
				ELSE IF(@Quantity = @QtyPurchaseReal)
1220

1221
				BEGIN
1222

1223
					UPDATE dbo.PMDMAT0004
1224

1225
					SET Qty = @NewQty
1226

1227
						,TotalValuation = @NewTotalValuation
1228

1229
						,ChangeBy = @UserID
1230

1231
						,ChangeDate = @now16
1232

1233
					WHERE (CompanyID = @CompanyID OR @CompanyID = '')
1234

1235
						AND (MaterialID = @MaterialID)
1236

1237
						AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
1238

1239
						AND (Warehouse = @Warehouse OR @Warehouse = '')
1240

1241
						AND (Storage = @Storage OR @Storage = '')
1242

1243
						AND (Storage = @Bin OR @Bin = '')
1244

1245
						AND (SeqID = @MinSeqMat4)
1246

1247

1248

1249
					UPDATE dbo.PMDMAT0001
1250

1251
					SET MAP = @NewMAP
1252

1253
						,ChangeBy = @UserID
1254

1255
						,ChangeDate = @now16
1256

1257
					FROM dbo.PMDMAT0001
1258

1259
					WHERE MaterialID = @MaterialID
1260

1261

1262

1263
					UPDATE dbo.PMDMAT0004
1264

1265
					SET TotalValuation = (CONVERT(DECIMAL(22,2),Qty) * @NewMAP)
1266

1267
						,ChangeBy = @UserID
1268

1269
						,ChangeDate = @now16
1270

1271
					WHERE (MaterialID = @MaterialID)
1272

1273
						AND (SeqID <> @MinSeqMat4)	
1274

1275

1276

1277
					UPDATE dbo.PTRPURCHREALIZATION
1278

1279
					SET ItemStatus = '2'
1280

1281
						,Warehouse = @Warehouse
1282

1283
						,LogisticAreaID = @LogisticArea
1284

1285
						,Storage = @Storage
1286

1287
						,Bin = @Bin
1288

1289
						,ChangeBy = @UserID
1290

1291
						,ChangeDate = @now16
1292

1293
					WHERE DocNo = @DocIDRef 
1294

1295
						AND MaterialID = @MaterialID
1296

1297
						AND ItemStatus = '1'
1298

1299

1300

1301
					UPDATE dbo.PTRPURCHITEM 
1302

1303
					SET ItemStatus = '2'
1304

1305
						,ChangeBy = @UserID
1306

1307
						,ChangeDate = @now16
1308

1309
					WHERE DocNo = @DocIDRef
1310

1311
						AND Material = @MaterialID
1312

1313

1314

1315
					SET @IsSuccess = 'Y'
1316

1317
				END
1318

1319
				ELSE 
1320

1321
				BEGIN
1322

1323
					SET @IsSuccess = 'OVERQTY'
1324

1325
				END
1326

1327

1328

1329
				SELECT @CekItemNotDone = COUNT(ItemStatus)
1330

1331
				FROM dbo.PTRPURCHITEM 
1332

1333
				WHERE DocNo = @DocIDRef AND (ItemStatus = '0' OR ItemStatus = '1')
1334

1335

1336

1337
				IF(@CekItemNotDone = 0)
1338

1339
				BEGIN
1340

1341
					UPDATE dbo.PTRPURCHHEAD
1342

1343
					SET ProcessStatus = 'C'
1344

1345
					WHERE DocNo = @DocIDRef 
1346

1347
				END
1348

1349
			END
1350

1351
			IF(@DocIDRef <> ''AND @TransTypeRef = 'PRD')
1352

1353
			BEGIN
1354

1355
				UPDATE dbo.PMDMAT0004
1356

1357
				SET Qty = @NewQty
1358

1359
					,TotalValuation = @NewTotalValuation
1360

1361
					,ChangeBy = @UserID
1362

1363
					,ChangeDate = @now16
1364

1365
				WHERE (CompanyID = @CompanyID OR @CompanyID = '')
1366

1367
					AND (MaterialID = @MaterialID)
1368

1369
					AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
1370

1371
					AND (Warehouse = @Warehouse OR @Warehouse = '')
1372

1373
					AND (Storage = @Storage OR @Storage = '')
1374

1375
					AND (Storage = @Bin OR @Bin = '')
1376

1377
					AND (SeqID = @MinSeqMat4)
1378

1379

1380

1381
				UPDATE dbo.PMDMAT0001
1382

1383
				SET MAP = @NewMAP
1384

1385
					,ChangeBy = @UserID
1386

1387
					,ChangeDate = @now16
1388

1389
				FROM dbo.PMDMAT0001
1390

1391
				WHERE MaterialID = @MaterialID
1392

1393

1394

1395
				UPDATE dbo.PMDMAT0004
1396

1397
				SET TotalValuation = (CONVERT(DECIMAL(22,2),Qty) * @NewMAP)
1398

1399
					,ChangeBy = @UserID
1400

1401
					,ChangeDate = @now16
1402

1403
				WHERE (MaterialID = @MaterialID)
1404

1405
					AND (SeqID <> @MinSeqMat4)
1406

1407

1408

1409
				UPDATE PMDMATWIP
1410

1411
				SET StockStatus = '0005'
1412

1413
					,ChangeBy = @UserID
1414

1415
					,ChangeDate = @now16
1416

1417
				--SELECT * FROM PMDMATWIP
1418

1419
				WHERE ProdID = @DocIDRef
1420

1421
					AND MaterialID = @MaterialID
1422

1423
				
1424

1425
				DECLARE @SPK VARCHAR(20)
1426

1427
				SELECT @SPK = SpkID
1428

1429
				FROM PMDMATWIP
1430

1431
				WHERE ProdID = @DocIDRef
1432

1433
					AND MaterialID = @MaterialID
1434

1435

1436

1437
				UPDATE PMDMATWIP
1438

1439
				SET DocStatus = '4'
1440

1441
					,StockStatus = '0005'
1442

1443
					,ChangeBy = @UserID
1444

1445
					,ChangeDate = @now16
1446

1447
				--SELECT * FROM PMDMATWIP
1448

1449
				WHERE ProdID = @SPK
1450

1451
					AND MaterialID = @MaterialID 
1452

1453

1454

1455
				UPDATE PTRPRODUCTIONITEM
1456

1457
				SET Status = '2'
1458

1459
					,ChangeBy = @UserID
1460

1461
					,ChangeDate = @now16
1462

1463
				--SELECT * FROM PTRPRODUCTIONITEM
1464

1465
				WHERE DocNo = @DocIDRef
1466

1467
					AND FlowType = 'RCPT'
1468

1469
				
1470

1471
				UPDATE PTRPRODUCTIONHEAD
1472

1473
				SET DocStatus = '4'
1474

1475
					,ProcessStatus = 'C'
1476

1477
					,ChangeBy = @UserID
1478

1479
					,ChangeDate = @now16
1480

1481
				--SELECT * FROM PTRPRODUCTIONHEAD
1482

1483
				WHERE DocNo = @SPK
1484

1485

1486

1487
				UPDATE PTRPRODUCTIONITEM
1488

1489
				SET Status = '2'
1490

1491
					,ChangeBy = @UserID
1492

1493
					,ChangeDate = @now16
1494

1495
				--SELECT * FROM PTRPRODUCTIONITEM
1496

1497
				WHERE DocNo = @SPK
1498

1499
					AND FlowType = 'RCPT'
1500

1501

1502

1503
				SET @IsSuccess = 'Y'
1504

1505
			END
1506

1507
			ELSE
1508

1509
			BEGIN
1510

1511
				UPDATE dbo.PMDMAT0004
1512

1513
				SET Qty = @NewQty
1514

1515
					,TotalValuation = @NewTotalValuation
1516

1517
					,ChangeBy = @UserID
1518

1519
					,ChangeDate = @now16
1520

1521
				WHERE (CompanyID = @CompanyID OR @CompanyID = '')
1522

1523
					AND (MaterialID = @MaterialID)
1524

1525
					AND (LogisticAreaID = @LogisticArea OR @LogisticArea = '')
1526

1527
					AND (Warehouse = @Warehouse OR @Warehouse = '')
1528

1529
					AND (Storage = @Storage OR @Storage = '')
1530

1531
					AND (Storage = @Bin OR @Bin = '')
1532

1533
					AND (SeqID = @MinSeqMat4)
1534

1535

1536

1537
				UPDATE dbo.PMDMAT0001
1538

1539
				SET MAP = @NewMAP
1540

1541
					,ChangeBy = @UserID
1542

1543
					,ChangeDate = @now16
1544

1545
				FROM dbo.PMDMAT0001
1546

1547
				WHERE MaterialID = @MaterialID
1548

1549

1550

1551
				UPDATE dbo.PMDMAT0004
1552

1553
				SET TotalValuation = (CONVERT(DECIMAL(22,2),Qty) * @NewMAP)
1554

1555
					,ChangeBy = @UserID
1556

1557
					,ChangeDate = @now16
1558

1559
				WHERE (MaterialID = @MaterialID)
1560

1561
					AND (SeqID <> @MinSeqMat4)
1562

1563

1564

1565
				SET @IsSuccess = 'Y'
1566

1567
			END
1568

1569
		END
1570

1571
	END
1572

1573
END
1574

1575

1576

1577
--IF(@DocType = 'MTPR')
1578

1579
--BEGIN
1580

1581
--	IF(@ItemFlow = 'ISSU')
1582

1583
--	BEGIN
1584

1585
--		IF(ISNULL(@ExistStock,0) < @Quantity)
1586

1587
--		BEGIN 
1588

1589
--			SET @IsSuccess = 'OVERQTY' 
1590

1591
--		END
1592

1593
--		ELSE
1594

1595
--		BEGIN
1596

1597
--			SET @NewStock = @ExistStock - @Quantity
1598

1599
--			SET @NewEachMAP = @ExistTotalMAP / @ExistStock  ---- menggunakan MAP sebelumnya
1600

1601
--			SET @NewTotalMAP = @NewEachMAP * @NewStock
1602

1603
--			--SELECT @NewStock, @NewEachMAP, @NewTotalMAP
1604

1605
--			IF (@IsExist > 0)
1606

1607
--			BEGIN
1608

1609
--				SET @IsSuccess = 'Y'
1610

1611
--				IF(@DocIDRef <> '' AND @DocTypeRef <> '' AND @TransTypeRef <> '')
1612

1613
--				BEGIN
1614

1615
--					DELETE FROM PMDMAT0004
1616

1617
--					--SELECT * FROM PMDMAT0004
1618

1619
--					WHERE MaterialID = @MaterialID
1620

1621
--						AND DocIDRef = @DocIDRef 
1622

1623
--						AND DocTypeRef = @DocTypeRef 
1624

1625
--						AND TransTypeRef = @TransTypeRef
1626

1627
--				END
1628

1629
--				ELSE
1630

1631
--				BEGIN
1632

1633
--					UPDATE dbo.PMDMAT0004
1634

1635
--					SET Qty = CONVERT(VARCHAR(4), ISNULL(@NewStock,0))
1636

1637
--						,TotalValuation = ISNULL(@NewTotalMAP,0)
1638

1639
--						,ChangeBy = @UserID
1640

1641
--						,ChangeDate = @now16
1642

1643
--					WHERE MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
1644

1645
--						AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
1646

1647
--						AND SeqID = @MinSeqMatExist
1648

1649
--				END
1650

1651
--			END
1652

1653
--		END
1654

1655
--	END
1656

1657
--	ELSE IF(@ItemFlow = 'RCPT')
1658

1659
--	BEGIN
1660

1661
--		SET @NewStock = @ExistStock + @Quantity
1662

1663
--		SET @NewEachMAP = @TotalPrice / @Quantity
1664

1665
--		SET @NewTotalMAP = @NewEachMAP * @NewStock
1666

1667

1668

1669
--		IF (@IsExist > 0)
1670

1671
--		BEGIN
1672

1673

1674

1675
--			SET @IsSuccess = 'Y'
1676

1677
--			UPDATE dbo.PMDMAT0004
1678

1679
--			SET Qty = @NewStock
1680

1681
--				,TotalValuation = @NewTotalMAP
1682

1683
--				,ChangeBy = @UserID
1684

1685
--				,ChangeDate = @now16
1686

1687
--			WHERE MaterialID = @MaterialID AND StockStatus = '0001' AND (CompanyID = @CompanyID OR @CompanyID = '')
1688

1689
--				AND (Warehouse = @Warehouse OR @Warehouse = '') AND (Storage = @Storage OR @Storage = '') AND (Bin = @Bin OR @Bin = '')
1690

1691
--				AND SeqID = @MinSeqMatExist
1692

1693
--		END
1694

1695
--		ELSE IF (@IsExist = 0)
1696

1697
--		BEGIN
1698

1699
--			SET @IsSuccess = 'Y'	
1700

1701
--			INSERT INTO dbo.PMDMAT0004
1702

1703
--			SELECT DISTINCT @MaterialID	
1704

1705
--							,@MaxSeqMatExist + 1
1706

1707
--							,@CompanyID
1708

1709
--							,@LogisticArea
1710

1711
--							,@Warehouse
1712

1713
--							,@Storage
1714

1715
--							,@Bin
1716

1717
--							,'0001'
1718

1719
--							,'1' AS StockType
1720

1721
--							,'122324'
1722

1723
--							,CONVERT(VARCHAR(4),@Quantity)
1724

1725
--							,CONVERT(DECIMAL(18,2),@TotalPrice)
1726

1727
--							,1
1728

1729
--							,'113124'
1730

1731
--							,@UserID AS CreateBy
1732

1733
--							,@now16 AS CreateDate
1734

1735
--							,@UserID AS ChangeBy
1736

1737
--							,@now16 AS ChangeDate
1738

1739
--							,'' AS SORDID 
1740

1741
--							,'' AS PRODID
1742

1743
--							,'' AS INTRID
1744

1745
--							,'' AS POID 
1746

1747
--							,'' AS DocIDREf
1748

1749
--							,'' AS DocTypeRef
1750

1751
--							,'' AS TransTypeRef
1752

1753
--		END
1754

1755
--	END
1756

1757
--END
1758

1759

1760

1761
SELECT @IsSuccess AS IsSuccess
1762

1763

1764

1765
DELETE FROM @TblMAT4
1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

(2-2/2)