Project

General

Profile

Feature #2886 » PTRFILOAUTOPOSTINGREGULAR_20241113.sql

Tri Rizqiaty, 11/13/2024 11:23 AM

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

3
AS
4

5

6

7
DECLARE @Now14 VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(),'yyyymmddhhnnss')
8

9
DECLARE @Now8 VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(),'yyyymmdd') --'20241007'--dbo.fn_formatdatetime(GETDATE(),'yyyymmdd')
10

11

12

13
DECLARE @DatePost VARCHAR(20) = RIGHT(@Now8,2)
14

15

16

17
DECLARE @thead TABLE
18

19
(
20

21
	[DocNo] [VARCHAR](20) NOT NULL,
22

23
	[DocType] [VARCHAR](20) NULL,
24

25
	[DocTypeTo] [VARCHAR](20) NULL,
26

27
	[DocDate] [VARCHAR](20) NULL,
28

29
	[DocStatus] [VARCHAR](20) NULL,
30

31
	[Description] [VARCHAR](500) NULL,
32

33
	[FiscalYear] [VARCHAR](20) NULL,
34

35
	[FiscalPeriod] [VARCHAR](20) NULL,
36

37
	[FiscalPeriodYear] [VARCHAR](20) NULL,
38

39
	[CompanyID] [VARCHAR](20) NULL,
40

41
	[IntDocRef] [VARCHAR](100) NULL,
42

43
	[ExtDocRef] [VARCHAR](50) NULL,
44

45
	[BusinessUnit] [VARCHAR](50) NULL,
46

47
	[PostingDate] [VARCHAR](20) NULL,
48

49
	[Curency] [VARCHAR](20) NULL,
50

51
	[CreateBy] [VARCHAR](50) NULL,
52

53
	[CreateDate] [VARCHAR](14) NULL,
54

55
	[ChangeBy] [VARCHAR](50) NULL,
56

57
	[ChangeDate] [VARCHAR](14) NULL,
58

59
	[CustomerID] [VARCHAR](50) NULL,
60

61
	[VendorID] [VARCHAR](50) NULL,
62

63
	[TotalDebet] [DECIMAL](30, 0) NULL,
64

65
	[TotalCredit] [DECIMAL](30, 0) NULL,
66

67
	[InvoiceNumber] [VARCHAR](50) NULL,
68

69
	[FakturNumber] [VARCHAR](50) NULL,
70

71
	[VendorInvoiceNumber] [VARCHAR](50) NULL,
72

73
	[IntDocTrType] [VARCHAR](50) NULL,
74

75
	[Regular] [VARCHAR](50) NULL,
76

77
	[CashPlan] [VARCHAR](50) NULL,
78

79
	[StartDate] [VARCHAR](8) NULL,
80

81
	[EndDate] [VARCHAR](8) NULL,
82

83
	[RegularDate] [VARCHAR](10) NULL,
84

85
	[RefType] [VARCHAR](2) NULL,
86

87
	[RefStatus] [VARCHAR](2) NULL,
88

89
	[TransTypeRef] [VARCHAR](20) NULL,
90

91
	[DocTypeRef] [VARCHAR](20) NULL,
92

93
	[DocIDRef] [VARCHAR](20) NULL,
94

95
	[DocItemRef] [VARCHAR](20) NULL,
96

97
	[ProcessID] [VARCHAR](8) NULL,
98

99
	[ExternalID] [VARCHAR](50) NULL,
100

101
	[CostCenter] [VARCHAR](50) NULL,
102

103
	[TotSalesRef] [DECIMAL](30, 0) SPARSE  NULL,
104

105
	[TotInvoiceRef] [DECIMAL](30, 0) NULL,
106

107
	[BalanceRef] [DECIMAL](30, 0) NULL,
108

109
	[DueDate] [VARCHAR](8) NULL,
110

111
	[Abbreviation] [VARCHAR](500) NULL,
112

113
	[TotAmoPurc] [DECIMAL](18, 2) NULL,
114

115
	[CustPONumber] [VARCHAR](50) NULL,
116

117
	[FakturFlag] [VARCHAR](4) NULL,
118

119
	[AutoFlag] [VARCHAR](20) NULL,
120

121
	[StartDatePlan] [VARCHAR](20) NULL,
122

123
	[FinishDatePlan] [VARCHAR](14) NULL,
124

125
	[StartDateActual] [VARCHAR](30) NULL,
126

127
	[FinishDateActual] [VARCHAR](14) NULL,
128

129
	[FakturDate] [VARCHAR](20) NULL,
130

131
	[InstallmentType] [VARCHAR](4) NULL,
132

133
	[Tenor] [VARCHAR](20) NULL
134

135
)
136

137

138

139
INSERT INTO @thead
140

141
SELECT *
142

143
FROM dbo.PTRFINANCEHEAD AS head
144

145
--LEFT JOIN dbo.PTRFINANCEITEM AS item
146

147
--	ON head.DocNo = item.DocNo
148

149
WHERE DocType = 'RG'
150

151
	AND RegularDate = @DatePost
152

153
	AND DocStatus = '2'
154

155
	AND head.StartDate <= @Now8
156

157
	AND head.EndDate >= @Now8
158

159

160

161
DECLARE @titem TABLE
162

163
(
164

165
	[DocNo] [VARCHAR](20) NOT NULL,
166

167
	[DocItemID] [INT] NOT NULL,
168

169
	[FiscalYear] [VARCHAR](20) NULL,
170

171
	[FiscalPeriod] [VARCHAR](20) NULL,
172

173
	[CompanyID] [VARCHAR](50) NULL,
174

175
	[DocItemRef] [VARCHAR](20) NULL,
176

177
	[DKFlag] [VARCHAR](20) NULL,
178

179
	[AccountGroup] [VARCHAR](50) NULL,
180

181
	[GLAccountID] [VARCHAR](20) NULL,
182

183
	[Currency] [VARCHAR](20) NULL,
184

185
	[SubLedgerType] [VARCHAR](30) NULL,
186

187
	[SubLedgerID] [VARCHAR](30) NULL,
188

189
	[CostCenter] [VARCHAR](50) NULL,
190

191
	[ProductID] [VARCHAR](30) NULL,
192

193
	[CreateBy] [VARCHAR](50) NULL,
194

195
	[CreateDate] [VARCHAR](14) NULL,
196

197
	[ChangeBy] [VARCHAR](50) NULL,
198

199
	[ChangeDate] [VARCHAR](14) NULL,
200

201
	[AmountDebet] [DECIMAL](30, 0) NULL,
202

203
	[AmountCredit] [DECIMAL](30, 0) NULL,
204

205
	[AmountDebetCompCurr] [DECIMAL](30, 0) NULL,
206

207
	[AmountCreditCompCurr] [DECIMAL](30, 0) NULL,
208

209
	[CompanyCurrency] [VARCHAR](50) NULL,
210

211
	[ExchangeRate] [DECIMAL](30, 0) NULL,
212

213
	[ItemStatus] [VARCHAR](50) NULL,
214

215
	[DueDate] [VARCHAR](50) NULL,
216

217
	[RefDocTr] [VARCHAR](50) NULL,
218

219
	[RefDocTy] [VARCHAR](50) NULL,
220

221
	[RefDocID] [VARCHAR](50) NULL,
222

223
	[Description] [VARCHAR](500) NULL,
224

225
	[BusinessUnit] [VARCHAR](30) NULL
226

227
)
228

229

230

231
INSERT INTO @titem
232

233
SELECT item.*
234

235
FROM dbo.PTRFINANCEITEM AS item
236

237
INNER JOIN @thead AS head
238

239
	ON head.DocNo = item.DocNo
240

241

242

243
DECLARE @CurrentID VARCHAR(20)
244

245
SELECT DISTINCT @CurrentID = CurrentID
246

247
FROM dbo.PCMBSENTERPRISENUMBERDOC 
248

249
WHERE TableName = 'PTRFINANCEHEAD'
250

251
--SELECT @CurrentID
252

253

254

255
DECLARE @LastDocNo VARCHAR(20) = @CurrentID
256

257

258

259
DECLARE @c_docno VARCHAR(8)
260

261
DECLARE cur_doc CURSOR FOR SELECT DocNo FROM @thead
262

263
OPEN cur_doc
264

265
FETCH cur_doc INTO @c_docno
266

267
WHILE @@Fetch_Status = 0
268

269
	BEGIN
270

271
	SET @LastDocNo = RIGHT('00000000'+ CONVERT(VARCHAR(8),(@LastDocNo + 1)),8)
272

273
	--SELECT @LastDocNo
274

275

276

277
	INSERT INTO PTRFINANCEHEAD
278

279
	SELECT DISTINCT @LastDocNo AS DocNo
280

281
		,CASE WHEN DocTypeTo <> '' THEN DocTypeTo ELSE 'GL' END AS [DocType]
282

283
		,'' AS [DocTypeTo]
284

285
		,@Now8 AS [DocDate]
286

287
		,'9' AS [DocStatus]
288

289
		,[Description]
290

291
		,LEFT(@Now8,4) AS [FiscalYear]
292

293
		,SUBSTRING(@Now8, 5,2) AS [FiscalPeriod]
294

295
		,LEFT(@Now8,6) AS [FiscalPeriodYear]
296

297
		,[CompanyID]
298

299
		,DocNo AS [IntDocRef]
300

301
		,[ExtDocRef]
302

303
		,[BusinessUnit]
304

305
		,@Now8 AS [PostingDate]
306

307
		,[Curency]
308

309
		,'ServicePostRegular' AS [CreateBy]
310

311
		,@Now14 AS [CreateDate]
312

313
		,'ServicePostRegular' AS[ChangeBy]
314

315
		,@Now14 AS[ChangeDate]
316

317
		,[CustomerID]
318

319
		,[VendorID]
320

321
		,[TotalDebet]
322

323
		,[TotalCredit]
324

325
		,[InvoiceNumber]
326

327
		,[FakturNumber]
328

329
		,[VendorInvoiceNumber]
330

331
		,[IntDocTrType]
332

333
		,[Regular]
334

335
		,[CashPlan]
336

337
		,'' AS [StartDate]
338

339
		,'' AS [EndDate]
340

341
		,'' AS [RegularDate]
342

343
		,[RefType]
344

345
		,[RefStatus]
346

347
		,'FIN' AS [TransTypeRef]
348

349
		,'RG' AS [DocTypeRef]
350

351
		,DocNo AS [DocIDRef]
352

353
		,[DocItemRef]
354

355
		,[ProcessID]
356

357
		,[ExternalID]
358

359
		,[CostCenter]
360

361
		,[TotSalesRef]
362

363
		,[TotInvoiceRef]
364

365
		,[BalanceRef]
366

367
		,[DueDate]
368

369
		,[Abbreviation]
370

371
		,[TotAmoPurc]
372

373
		,[CustPONumber]
374

375
		,[FakturFlag]
376

377
		,[AutoFlag]
378

379
		,[StartDatePlan]
380

381
		,[FinishDatePlan]
382

383
		,[StartDateActual]
384

385
		,[FinishDateActual]
386

387
		,[FakturDate]
388

389
		,[InstallmentType]
390

391
		,[Tenor] 
392

393
	FROM @thead
394

395
	WHERE DocNo = @c_docno
396

397

398

399
	INSERT INTO PTRFINANCEITEM
400

401
	SELECT DISTINCT @LastDocNo AS [DocNo]
402

403
		,[DocItemID]
404

405
		,LEFT(@Now8,4) AS [FiscalYear]
406

407
		,SUBSTRING(@Now8, 5,2) AS [FiscalPeriod]
408

409
		,[CompanyID]
410

411
		,[DocItemRef]
412

413
		,[DKFlag]
414

415
		,[AccountGroup]
416

417
		,[GLAccountID]
418

419
		,[Currency]
420

421
		,[SubLedgerType]
422

423
		,[SubLedgerID]
424

425
		,[CostCenter]
426

427
		,[ProductID]
428

429
		,'ServicePostRegular' AS[CreateBy]
430

431
		,@Now14 AS [CreateDate]
432

433
		,'ServicePostRegular' AS[ChangeBy]
434

435
		,@Now14 AS [ChangeDate]
436

437
		,[AmountDebet]
438

439
		,[AmountCredit]
440

441
		,[AmountDebetCompCurr]
442

443
		,[AmountCreditCompCurr]
444

445
		,[CompanyCurrency]
446

447
		,[ExchangeRate]
448

449
		,[ItemStatus]
450

451
		,[DueDate]
452

453
		,[RefDocTr]
454

455
		,[RefDocTy]
456

457
		,[RefDocID]
458

459
		,[Description]
460

461
		,[BusinessUnit]
462

463
	FROM @titem
464

465
	WHERE DocNo = @c_docno
466

467

468

469
	INSERT INTO PTRFILOPOSTING
470

471
	SELECT DISTINCT @LastDocNo AS [DocNo]
472

473
		,item.[DocItemID]
474

475
		,head.[DocType]
476

477
		,@Now8 AS [DocDate]
478

479
		,'9' AS [DocStatus]
480

481
		,LEFT(@Now8,4) AS [FiscalYear]
482

483
		,SUBSTRING(@Now8, 5,2) AS [FiscalPeriod]
484

485
		,head.[CompanyID]
486

487
		,head.[BusinessUnit]
488

489
		,head.[CostCenter]
490

491
		,item.SubLedgerType AS [SLType]
492

493
		,item.SubLedgerID AS [SLID]
494

495
		,head.[PostingDate]
496

497
		,item.[GLAccountID]
498

499
		,item.[Currency]
500

501
		,[AmountDebet]
502

503
		,[AmountCredit]
504

505
		,[AmountDebetCompCurr]
506

507
		,[AmountCreditCompCurr]
508

509
		,'ServicePostRegular' AS [CreateBy]
510

511
		,@Now14 AS [CreateDate]
512

513
		,'ServicePostRegular' AS[ChangeBy]
514

515
		,@Now14 AS [ChangeDate]
516

517
		,item.[CompanyCurrency]
518

519
		,item.[ItemStatus]
520

521
	FROM @thead AS head
522

523
	LEFT JOIN @titem AS item
524

525
		ON head.DocNo = item.DocNo
526

527
	WHERE head.DocNo = @c_docno
528

529

530

531
FETCH cur_doc INTO @c_docno
532

533
END
534

535
CLOSE cur_doc
536

537
DEALLOCATE cur_doc
538

539

540

541
UPDATE PCMBSENTERPRISENUMBERDOC SET CurrentID = @LastDocNo
542

543
--SELECT DISTINCT * FROM dbo.PCMBSENTERPRISENUMBERDOC 
544

545
WHERE TableName = 'PTRFINANCEHEAD'
546

547

(4-4/4)