Project

General

Profile

Bug #960 » ctrlloan.js

Tri Rizqiaty, 04/19/2022 01:43 PM

 
1
Ext.define('MinovaES.controller.masterdata.loan.ctrlloan', {
2
	extend: 'Ext.app.ViewController',
3
	alias: 'controller.ctrlloan',
4
	_action: "0",
5
	init: function () {
6
		this.control({
7
			//'[name=Amount]': {
8
			//    change: this.amountChange,
9
			//},
10
			'[name=cancelLOAN]': {
11
				click: this.cancel,
12
			},
13
			'[name=addLOAN]': {
14
				click: this.addLOAN,
15
			},
16
			'[name=copyLOAN]': {
17
				click: this.copyLOAN,
18
			},
19
			'[name=editLOAN]': {
20
				click: this.editLOAN,
21
			},
22
			'[name=btnsimulation]': {
23
				click: this.NewSimulateLoan,
24
			},
25
			'[name=phrpa0033Add]': {
26
				click: this.addPayment,
27
			},
28
			'[name=phrpa0033Delete]': {
29
				click: this.deletePayment,
30
			},
31
			'[name=phrpa0034Add]': {
32
				click: this.addRepayment,
33
			},
34
			'[name=phrpa0034Delete]': {
35
				click: this.deleteRepayment,
36
			},
37
			//'[name=gridloanpayment]': {
38
			//    //beforeedit: this.setHeaderSeqEmpGrid,
39
			//   // afterrender: this.afterrenderGrid,
40
			//    //edit: this.afterEditPayment,
41
			//},
42
			//'[name=gridloanregularrepayment]': {
43
			//    //beforeedit: this.setHeaderSeqEmpGridRegularRepayment,
44
			//    //afterrender: this.afterrenderGrid,
45
			//    //edit: this.afterEditRegPayment
46
			//},
47
			//'[name=simulation]': {
48
			////    afterrender: this.afterrenderGrid
49
			//},
50
			'[name=phrpa0032]': {
51
				itemdblclick: this.overViewDblClick,
52
				afterrender: this.afterRenderOverview
53
			},
54
			'[name=btnLoadDataLoan]': {
55
				click: this.btnLoadDataLoan,
56
			},
57
			'button': {
58
				click: this.buttonOnClick
59
			},
60
			'[name=gridloanregularrepayment]': {
61
				edit: this.hitungTenor
62
			},
63
			'[id=phrpa0034Tenor]': {
64
				expand: this.mapComboTenor
65
			},
66
			'[id=phrpa0033PaymentType]': {
67
				select: this.specialPay
68
			},
69
			'[id=phrpa0033StartDate]': {
70
				change: this.specialPay
71
			}
72
		});
73
	},
74

    
75
	cekAuth: function () {
76
		actionMenu_ = MinovaUtil.GetActionMenu();
77
		if (actionMenu_ == '0') {
78
			Ext.ComponentQuery.query('[name=actionGrid]')[0].setHidden(true);
79
			Ext.ComponentQuery.query('[name=saveLOAN]')[0].setHidden(true);
80
			Ext.ComponentQuery.query('[name=deleteLOAN]')[0].setHidden(true);
81
		}
82
	},
83
	afterRenderOverview: function () {
84
		this.cekAuth();
85
		var overView = Ext.StoreMgr.lookup("loanOverview");
86

    
87
		overView.proxy.extraParams = {
88
			tableName: 'phrpa0032',
89
			param: 'EmployeeID[=]' + getParam('EmployeeID')
90
		};
91
		overView.removeAll();
92
		overView.reload();
93
		overView.loadPage(1);
94
		var FullName = getParam("FullName");
95
		var EmpID = getParam('EmployeeID');
96
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Edit - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
97
	},
98

    
99
	afterEditPayment: function (editor, context, eOpts) {
100
		var _date = record.get("StartDate");
101
		var cek = this.cekDate(_date);
102
		if (cek) {}
103
		else {
104
			// start date > start date header
105
			record.set("StartDate", _date);
106
			MinovaMessage('', 'BS0002', 'Start Date Payment,Start Date Header ', "E");
107
		}
108
	},
109
	afterEditRegPayment: function (editor, context, eOpts) {
110
		var _date = record.get("StartDate");
111
		var cek = this.cekDate(_date);
112
		if (cek) {}
113
		else {
114
			// start date > start date header
115
			record.set("StartDate", _date);
116
			MinovaMessage('', 'BS0002', 'Start Date Regular Payment,Start Date Header ', "E");
117
		}
118
	},
119
	cekDate: function (date_) {
120
		var mainForm = Ext.ComponentQuery.query('[name=formLoanMain]')[0];
121
		var formValues = mainForm.getValues();
122
		//header start date
123
		var _startDate = parseInt(formValues.StartDate);
124
		var date_ = parseInt(date_);
125
		if (_startDate <= date_) {
126
			return true;
127
		} else {
128
			return false;
129
		}
130
	},
131
	cekPlafond: function (f) {
132
		var amount_ = Ext.ComponentQuery.query('[name=Amount]')[0].getValue();
133
		var loanType = Ext.ComponentQuery.query('[name=LoanType]')[0].getValue();
134
		var nowDate = MinovaUtil.GetNowDate();
135
		var EmployeeID = getParam('EmployeeID');
136

    
137
		var payrolBasic = null;
138
		Ext.Ajax.request({
139
			async: false,
140
			method: 'POST',
141
			url: '/UserControl/GetStore',
142
			params: {
143
				tableName: 'PHRPA0003',
144
				param: 'EmployeeID[equal]' + EmployeeID + ',StartDate[<=]' + nowDate + ',EndDate[>=]' + now
145
			},
146
			success: function (response) {
147
				var results = Ext.decode(response.responseText);
148
				payrolBasic = results.data[0];
149
			}
150
		});
151

    
152
		// get plafond data
153
		var plafond = null;
154
		Ext.Ajax.request({
155
			async: false,
156
			method: 'POST',
157
			url: '/UserControl/GetStore',
158
			params: {
159
				tableName: 'PHRBNLNPARAM',
160
				param: 'StartDate[<=]' + nowDate + ',EndDate[>=]' + now + ',LoanType[=]' + loanType + ',PayClass[=]' + payrolBasic.PayrollClass + ',PayGrade[=]' + payrolBasic.PayrollGrade
161
			},
162
			success: function (response) {
163
				var results = Ext.decode(response.responseText);
164
				plafond = results.data[0];
165
			}
166
		});
167

    
168
		// get wt data
169
		var WTdata = null;
170
		Ext.Ajax.request({
171
			async: false,
172
			method: 'POST',
173
			url: '/UserControl/GetStore',
174
			params: {
175
				tableName: 'PHRPYCU0300',
176
				param: 'StartDate[<=]' + nowDate + ',EndDate[>=]' + now + ',WageTypeDefinition[=]' + plafond.PlafondWagetype
177
			},
178
			success: function (response) {
179
				var results = Ext.decode(response.responseText);
180
				WTdata = results.data[0];
181
			}
182
		});
183

    
184
		// get wt amount
185
		var Valuation = undefined;
186
		var wtAmount = undefined;
187
		if (WTdata) {
188
			Valuation = WTdata.Valuation;
189
		} else {
190
			// wt tidak terdaftar
191
		}
192

    
193
		//get wt amount
194
		if (Valuation) {}
195

    
196
		var plafondAmount = parseFloat(plafond.PlafondWagetype) * parseFloat(plafond.PlafondMutiplier);
197
		// get amount
198

    
199

    
200
	},
201

    
202
	cekPaymentTotal: function () {
203
		var me = this;
204
		var allPayment = me.AllLoanPaymentData();
205
		var totalPinjaman = 0;
206
		var spesialRepayment = 0;
207
		var val_ = 0;
208

    
209
		if (allPayment) {
210
			//
211
			Ext.each(allPayment, function (rec) {
212
				val_ = parseInt(rec.Amount);
213
				if (rec.PaymentType == '1000') //1000 = payment, 2000=spesial repayment
214
				{
215
					totalPinjaman = totalPinjaman + val_;
216
				} else {
217
					spesialRepayment = spesialRepayment + val_;
218
				}
219
			});
220

    
221
			var paymentTotal = totalPinjaman - spesialRepayment
222
				var headerAmount = Ext.ComponentQuery.query('[name=Amount]')[0].getValue();
223
			if (paymentTotal <= headerAmount) {
224
				return true;
225
			} else {
226
				return false;
227
			}
228
		}
229
	},
230

    
231
	//afterrenderGrid: function (g) {
232
	//    g.down('[dataIndex=HeaderSequence]').setVisible(false);
233
	//    g.down('[dataIndex=EmployeeID]').setVisible(false);
234
	//    /*
235
	//	if (g.down('[dataIndex=Currency]')) {
236
	//	g.down('[dataIndex=Currency]').setVisible(false);
237
	//	}
238
	//	 */
239
	//},
240
	setHeaderSeqEmpGrid: function (editor, context, eOpts) {
241
		var mainForm = Ext.ComponentQuery.query('[name=mainformLoan]')[0];
242
		var EmployeeID = getParam('EmployeeID');
243
		var HeaderSequence = Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue();
244
		var Currency = Ext.ComponentQuery.query('[name=Currency]')[0].getValue();
245

    
246
		if (mainForm.getForm().isValid()) {
247
			var grid_ = context.grid;
248
			record = grid_.getSelectionModel().getSelection()[0];
249
			record.set("EmployeeID", EmployeeID);
250
			record.set("HeaderSequence", HeaderSequence);
251
			record.set("Currency", Currency);
252

    
253
			var g = context.grid.getPlugin('rowEditing').editor.form.findField('Currency');
254
			if (g) {
255
				g.disable();
256
			}
257

    
258
		} else {
259
			MinovaMessage('', '000009', '', "E");
260
			return false;
261
		}
262

    
263
	},
264

    
265
	setHeaderSeqEmpGridRegularRepayment: function (editor, context, eOpts) {
266
		this.setHeaderSeqEmpGrid(editor, context, eOpts);
267
		var act_ = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getActionSubmit();
268
		if (act_ == 0) { // add
269
			var simulationStore = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
270
			var storeRegPayment = context.grid.getStore();
271
			var count_ = storeRegPayment.count();
272
			if (count_ > 1) {
273
				if (simulationStore.count() == 0) {
274
					MinovaMessage('', '000009', '', "E");
275
					var selection = context.grid.getView().getSelectionModel().getSelection()[0];
276
					if (context.rowIdx != 0) {
277
						storeRegPayment.remove(selection);
278
					}
279

    
280
					return false;
281
				}
282
			}
283
		} else { // edit
284
			// load last balance on
285
		}
286

    
287
	},
288

    
289
	setHeaderSeqEmpForm: function (act) {
290
		//set header sequence
291
		var EmployeeID = getParam('EmployeeID');
292

    
293
		var HeaderSequence = Ext.ComponentQuery.query('[name=HeaderSequence]')[0];
294
		var data_ = undefined;
295
		Ext.Ajax.request({
296
			async: false,
297
			method: 'POST',
298
			url: '/UserControl/GetStore',
299
			params: {
300
				tableName: 'phrpa0032',
301
				param: 'EmployeeID[equal]' + EmployeeID
302
			},
303
			success: function (response) {
304
				var results = Ext.decode(response.responseText);
305
				data_ = results.data;
306
				if (act != 1) {
307
					var _frm = Ext.ComponentQuery.query('[name=mainformLoan]')[0];
308
					_frm.getForm().findField("EmployeeID").setValue(EmployeeID);
309

    
310
					if (data_.length > 0) {
311
						var dt_ = data_[data_.length - 1].HeaderSequence;
312
						HeaderSequence.setValue(dt_ + 1);
313
					} else {
314
						HeaderSequence.setValue(1);
315
					}
316
				}
317

    
318
			}
319
		});
320
	},
321

    
322
	addLOAN: function () {
323
		var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
324
		card_.getLayout().setActiveItem(1);
325
		form_ = Ext.ComponentQuery.query('[name=mainformLoan]')[0];
326
		form_.getForm().reset();
327
		form_.getForm().findField('EmployeeID').setValue(getParam('EmployeeID'));
328
		form_.setActionSubmit(0);
329
		//this.setHeaderSeqEmpForm(0);
330
		var me = this;
331
		me.setKeyDisabled(false);
332
		me._action = "0";
333
		var FullName = getParam("FullName");
334
		var EmpID = getParam('EmployeeID');
335
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Add - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
336
		this.btnLoadDataLoan();
337
			setTimeout(function(){ 	var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
338
		var store_2 = Ext.ComponentQuery.query('[name=gridloanpayment]')[0].getStore();
339
		var store_3 = Ext.ComponentQuery.query('[name=gridloanregularrepayment]')[0].getStore();
340
		store_.removeAll(); 
341
		store_2.removeAll(); 
342
		store_3.removeAll(); 
343
		}, 1200);
344
	},
345
	copyLOAN: function () {
346
		this.overViewDblClick();
347
		form = Ext.ComponentQuery.query('[name=mainformLoan]')[0]; // edit proses
348
		form.setActionSubmit(0);
349
		var me = this;
350
		me.setKeyDisabled(false);
351
		me._action = "0";
352
		var FullName = getParam("FullName");
353
		var EmpID = getParam('EmployeeID');
354
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Copy - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
355
		this.btnLoadDataLoan();
356
	},
357
	cancel: function () {
358
		Ext.Msg.show({
359
			title: "",
360
			msg: MinovaMessageData('000008', 'Over View'),
361
			buttons: Ext.Msg.YESNO,
362
			icon: Ext.Msg.QUESTION,
363
			fn: function (choice) {
364
				if (choice === 'yes') {
365
					var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
366
					card_.getLayout().setActiveItem(0);
367
					var FullName = getParam("FullName");
368
					var EmpID = getParam('EmployeeID');
369
					Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Loan Maintenance' + ' - ' + EmpID + ' - ' + FullName);
370
				}
371

    
372
			}
373
		});
374

    
375
	},
376
	editLOAN: function () {
377
		this.overViewDblClick();
378
		form = Ext.ComponentQuery.query('[name=mainformLoan]')[0]; // edit proses
379
		form.setActionSubmit(0);
380
		var me = this;
381
		me.setKeyDisabled(true);
382
		me._action = "1";
383
		var FullName = getParam("FullName");
384
		var EmpID = getParam('EmployeeID');
385
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Edit - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
386
		this.btnLoadDataLoan();
387
	},
388

    
389
	// get all loanpayment data
390
	AllLoanPaymentData: function () {
391
		var loanpaymentStore = Ext.StoreMgr.lookup("loanpayment");
392
		var loanpaymentData = undefined;
393
		if (loanpaymentStore.count() > 0) { //cek apakah loan payment tidak kosong
394

    
395
			var loanpaymentData = Ext.pluck(loanpaymentStore.data.items, 'data');
396

    
397
		} else {
398
			alert('isi loan payment');
399
		}
400

    
401
		return loanpaymentData;
402
	},
403

    
404
	// get all regular repayment data
405
	AllRepaymentData: function () {
406
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
407
		var loanregrepaymentData = undefined;
408
		if (loanregrepaymentStore.count() > 0) { //cek apakah loan reg repayment tidak kosong
409

    
410
			var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
411

    
412
		} else {
413
			alert('isi loan reg repayment');
414
		}
415

    
416
		return loanregrepaymentData;
417
	},
418
	simulateLoan: function () {
419
		var me = this;
420
		var mainForm = Ext.ComponentQuery.query('[name=formLoanMain]')[0];
421
		var formValues = mainForm.getValues();
422
		var AllLoanPaymentData = me.AllLoanPaymentData();
423
		var AllRepaymentData = me.AllRepaymentData();
424

    
425
		//cek apakah form , payment dan regular payment sudah diisi
426
		var loanpaymentStore = Ext.StoreMgr.lookup("loanpayment").count();
427
		var loanregpaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment").count();
428

    
429
		if ((mainForm.getForm().isValid()) && loanpaymentStore > 0 && loanregpaymentStore > 0) {
430
			me.runSimulateLoan(formValues, AllLoanPaymentData, AllRepaymentData);
431
		} else {
432
			if (loanpaymentStore = 0) {
433
				MinovaMessage('', 'BS0001', 'Loan Payment', "E");
434
			} else if (loanregpaymentStore = 0) {
435
				MinovaMessage('', 'BS0001', 'Loan Regular Payment', "E");
436
			} else {
437
				MinovaMessage('', 'BS0001', 'Loan Main Form', "E");
438
			}
439
		}
440

    
441
	},
442

    
443
	calculateFlatPrincipal: function (tempBalance, loanTypeData, sdate, edate, formValues) {
444

    
445
		var mounth_ = parseInt(sdate.substring(4, 6)); // bulan awal cicilan
446
		var year_ = parseInt(sdate.substring(0, 4)); // tahun awal cicilan
447
		var period_ = undefined;
448
		var _interestMethod = loanTypeData.InterestMethod;
449
		var balance = parseFloat(tempBalance);
450
		var lastStoreCal = undefined;
451
		//var _interestMethod = "act/360";
452
		//var _interestMethod = Ext.ComponentQuery.query('[name=RepaymentType]')[0].getValue();
453
		var totalDayinMounth = undefined; // jumlah hari pengali perbulan
454
		var dayinYear = undefined; // jumlah hari pembagi dalam 1 thn
455
		var interestMethod = _interestMethod.substring(0, 3);
456
		var act = interestMethod.substring(0, 3);
457
		if (act == 'act') {
458
			dayinYear = _interestMethod.substring(4, 7);
459
			dayinYear = parseInt(dayinYear);
460
			totalDayinMounth = parseInt(MinovaUtil.GetDays(mounth_, year_));
461
		} else {
462
			totalDayinMounth = parseInt(_interestMethod.substring(0, 2));
463
			dayinYear = _interestMethod.substring(3, 6);
464
			dayinYear = parseInt(dayinYear)
465
		}
466

    
467
		//get all regular repayment
468
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
469
		var loanregrepaymentData = undefined;
470
		if (loanregrepaymentStore.count() > 0) { //cek apakah loan reg repayment tidak kosong
471

    
472
			var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
473

    
474
		} else {
475
			alert('isi loan reg repayment');
476
		}
477
		var countRepayment = loanregrepaymentData.length;
478
		var sdateRepayment = undefined;
479
		var edateRepayment = undefined;
480
		var seq = 0;
481
		//var totalDays = undefined;
482

    
483
		//var totalDayinMounth = undefined;// jumlah hari pengali perbulan
484
		//var dayinYear = undefined;// jumlah hari pembagi dalam 1 thn
485

    
486
		for (i = 0; i < countRepayment; i++) {
487

    
488
			if (countRepayment > 1) {
489
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
490
				edateRepayment = parseInt(loanregrepaymentData[i + 1].StartDate);
491

    
492
			} else {
493
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
494
				edateRepayment = 99991231;
495
			}
496

    
497
			// calculate simulate
498
			var bunga = parseFloat(loanregrepaymentData[i].RateofInterest);
499

    
500
			if (parseInt(sdate) >= sdateRepayment && parseInt(edate) <= edateRepayment) { // cek startdate dan endate payment dengan repayment valid kah?
501
				var cicilanPokok = loanregrepaymentData[i].Amount;
502
				var tenor = loanregrepaymentData[i].Tenor;
503

    
504
				var _StartDate = undefined;
505
				var _EndDate = undefined;
506

    
507
				for (tempTenor = 0; tempTenor < parseInt(tenor); tempTenor++) {
508
					period_ = year_;
509
					if (act == 'act') {
510
						_StartDate = year_.toString() + mounth_.toString() + "01";
511
						_EndDate = year_.toString() + mounth_.toString() + totalDayinMounth;
512
						if (mounth_ == 12) {
513
							year_ = year_ + 1;
514
							mounth_ = 1;
515

    
516
						} else {
517
							mounth_ = mounth_ + 1;
518
						}
519
					} else {
520
						if (mounth_ < 10) {
521
							_StartDate = year_.toString() + "0" + mounth_.toString() + "01";
522
							_EndDate = year_.toString() + "0" + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
523
						} else {
524
							_StartDate = year_.toString() + mounth_.toString() + "01";
525
							_EndDate = year_.toString() + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
526
						}
527

    
528
						if (mounth_ == 12) {
529
							year_ = year_ + 1;
530
							mounth_ = 1;
531

    
532
						} else {
533
							mounth_ = mounth_ + 1;
534
						}
535
					}
536

    
537
					//get interest total
538
					var totInterest = (totalDayinMounth / dayinYear) * (balance * bunga) / 100;
539
					totInterest = Math.round(totInterest * 1000) / 1000; // round up 3 angka dibelakang koma
540
					//get New Balance
541
					balance = balance - parseFloat(cicilanPokok);
542
					//get employee must pay
543
					var totalRepayment = parseFloat(cicilanPokok) + totInterest;
544

    
545
					//insert store to simulate grid
546
					seq++;
547
					var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
548
					idx = store_.getCount();
549
					data = {
550
						StartDate: _StartDate,
551
						EndDate: _EndDate,
552
						EmployeeID: formValues.EmployeeID,
553
						HeaderSequence: formValues.HeaderSequence,
554
						Period: period_,
555
						Principal: cicilanPokok,
556
						Interest: totInterest,
557
						Repayment: totalRepayment,
558
						Balance: balance,
559
						Sequence: seq,
560
					};
561
					lastStoreCal = data;
562
					store_.insert(idx, data);
563
				}
564
			}
565
		}
566
		return lastStoreCal;
567
	},
568

    
569
	calculateFlatTotal: function (tempBalance, loanTypeData, sdate, edate, formValues) {
570
		var lastStoreCal = undefined;
571
		var mounth_ = parseInt(sdate.substring(4, 6)); // bulan awal cicilan
572
		var year_ = parseInt(sdate.substring(0, 4)); // tahun awal cicilan
573
		var period_ = undefined;
574
		var _interestMethod = loanTypeData.InterestMethod;
575
		//var _interestMethod = "act/360";
576
		//var _interestMethod = Ext.ComponentQuery.query('[name=RepaymentType]')[0].getValue();
577
		var totalDayinMounth = undefined; // jumlah hari pengali perbulan
578
		var dayinYear = undefined; // jumlah hari pembagi dalam 1 thn
579
		var interestMethod = _interestMethod.substring(0, 3);
580
		var act = interestMethod.substring(0, 3);
581
		if (act == 'act') {
582
			dayinYear = _interestMethod.substring(4, 7);
583
			dayinYear = parseInt(dayinYear);
584
			totalDayinMounth = parseInt(MinovaUtil.GetDays(mounth_, year_));
585
		} else {
586
			totalDayinMounth = parseInt(_interestMethod.substring(0, 2));
587
			dayinYear = _interestMethod.substring(3, 6);
588
			dayinYear = parseInt(dayinYear)
589
		}
590

    
591
		//get all regular repayment
592
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
593
		var loanregrepaymentData = undefined;
594
		if (loanregrepaymentStore.count() > 0) { //cek apakah loan reg repayment tidak kosong
595

    
596
			var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
597

    
598
		} else {
599
			alert('isi loan reg repayment');
600
		}
601
		var countRepayment = loanregrepaymentData.length;
602
		var sdateRepayment = undefined;
603
		var edateRepayment = undefined;
604
		for (i = 0; i < countRepayment; i++) {
605

    
606
			if (countRepayment > 1) {
607
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
608
				edateRepayment = parseInt(loanregrepaymentData[i + 1].StartDate);
609

    
610
			} else {
611
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
612
				edateRepayment = 99991231;
613
			}
614

    
615
			// calculate simulate
616
			var bunga = parseFloat(loanregrepaymentData[i].RateofInterest);
617

    
618
			// if (parseInt(sdate) >= sdateRepayment && parseInt(edate) <= edateRepayment) { // cek startdate dan endate payment dengan repayment valid kah?
619
			var _StartDate = undefined;
620
			var _EndDate = undefined;
621
			if (parseInt(sdate) <= sdateRepayment && parseInt(edate) <= edateRepayment) {
622
				var cicilan = parseFloat(loanregrepaymentData[i].Amount);
623
				var tenor = loanregrepaymentData[i].Tenor;
624
				var balance = parseFloat(tempBalance);
625
				var seq = 0;
626
				//for (tempTenor = 0; tempTenor <= tenor; tempTenor++)
627
				do {
628
					period_ = year_;
629
					if (act == 'act') {
630
						_StartDate = year_.toString() + mounth_.toString() + "01";
631
						_EndDate = year_.toString() + mounth_.toString() + totalDayinMounth;
632
						if (mounth_ == 12) {
633
							year_ = year_ + 1;
634
							mounth_ = 1;
635

    
636
						} else {
637
							mounth_ = mounth_ + 1;
638
						}
639
					} else {
640
						if (mounth_ < 10) {
641
							_StartDate = year_.toString() + "0" + mounth_.toString() + "01";
642
							_EndDate = year_.toString() + "0" + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
643
						} else {
644
							_StartDate = year_.toString() + mounth_.toString() + "01";
645
							_EndDate = year_.toString() + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
646
						}
647

    
648
						if (mounth_ == 12) {
649
							year_ = year_ + 1;
650
							mounth_ = 1;
651

    
652
						} else {
653
							mounth_ = mounth_ + 1;
654
						}
655
					}
656

    
657
					//get interest total
658
					var totInterest = (totalDayinMounth / dayinYear) * (balance * bunga) / 100;
659
					totInterest = Math.round(totInterest * 1000) / 1000; // round up 3 angka dibelakang koma
660
					// get real pricipal
661
					var pricipal_ = cicilan - totInterest;
662
					if (cicilan > balance) {
663
						pricipal_ = balance
664
							//cicilan = Math.round((bunga + balance - cicilan) * 1000) / 1000;
665
							cicilan = bunga + balance;
666
						balance = 0;
667

    
668
					} else {
669
						balance = Math.round((balance - pricipal_) * 1000) / 1000;
670
					}
671

    
672
					//insert store to simulate grid
673
					//simulation
674
					seq++;
675
					var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
676
					idx = store_.getCount();
677
					data = {
678
						StartDate: _StartDate,
679
						EndDate: _EndDate,
680
						EmployeeID: formValues.EmployeeID,
681
						HeaderSequence: formValues.HeaderSequence,
682
						Period: period_,
683
						Principal: pricipal_,
684
						Interest: totInterest,
685
						Repayment: cicilan,
686
						Balance: balance,
687
						Sequence: seq,
688
					};
689
					lastStoreCal = data;
690
					store_.insert(idx, data);
691
				} while (balance != 0);
692
			}
693
		}
694
		return lastStoreCal;
695
	},
696
	// func for calculate loan type Flat Principal
697
	runSimulateLoan: function (formValues, AllLoanPaymentData, AllRepaymentData) {
698
		var me = this;
699
		var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
700
		store_.removeAll();
701

    
702
		jmlLoan = formValues.Amount;
703
		loanType_ = formValues.LoanType;
704
		var loanTypeData = null;
705
		Ext.Ajax.request({
706
			async: false,
707
			method: 'POST',
708
			url: '/UserControl/GetStore',
709
			params: {
710
				tableName: 'PHRBNLOANTYP',
711
				param: 'LoanType[equal]' + loanType_
712
			},
713
			success: function (response) {
714
				var results = Ext.decode(response.responseText);
715
				loanTypeData = results.data[0];
716

    
717
			}
718
		});
719
		var sdate = undefined;
720
		var edate = undefined;
721
		var tempBalance = undefined;
722
		var tempBalanceNew = undefined;
723
		var tempCurrency = undefined;
724
		var tempCurrencyNew = undefined;
725
		var balanceAfterCal = undefined;
726
		var countPayment = AllLoanPaymentData.length;
727
		if (countPayment > 0) {
728

    
729
			for (i = 0; i < countPayment; i++) {
730
				if (AllLoanPaymentData[i].PaymentType = '1000') //1000 = payment
731
				{
732
					var c = i + 1;
733
					if (i == 0) { // data index 0 , frist payment
734
						tempBalance = AllLoanPaymentData[i].Amount;
735
						tempCurrency = AllLoanPaymentData[i].Currency;
736
						if (countPayment > c) {
737
							sdate = AllLoanPaymentData[i].StartDate;
738
							edate = AllLoanPaymentData[i++].StartDate;
739
						} else {
740
							sdate = AllLoanPaymentData[i].StartDate;
741
							edate = "99991231";
742
						}
743

    
744
						//Calculate simulate loan
745
						// me.calculateFlatPrincipal(tempBalance, loanType, sdate, edate);
746
					} else { // penambahan pokok pinjaman
747

    
748
						//get balance on date range in simulation grid
749
						tempBalanceNew = lastStoreCal.balance;
750

    
751
						tempCurrencyNew = AllLoanPaymentData[i].Currency;
752
						/*
753
						if (tempCurrencyNew != tempCurrency) { // jika beda currancy harus di convert ke currancy awal
754
						// get rate currancy baru ke currancy awal @rate
755
						var rate = 100;
756
						// konversi tempBalanceNew ke currancy awal
757
						tempBalanceNew_ = tempBalanceNew * rate;
758
						tempBalance = tempBalanceNew_ + AllLoanPaymentData[i].Amount;
759
						} else {
760
						tempBalance = tempBalanceNew + AllLoanPaymentData[i].Amount;
761
						}
762
						 */
763
						tempBalance = tempBalanceNew + AllLoanPaymentData[i].Amount;
764
						var c = i + 1;
765
						if (countPayment > c) {
766
							sdate = AllLoanPaymentData[i].StartDate;
767
							edate = AllLoanPaymentData[i++].StartDate;
768
						} else {
769
							sdate = AllLoanPaymentData[i].StartDate;
770
							edate = "99991231";
771
						}
772

    
773
						//Calculate simulate loan
774
						// me.calculateFlatPrincipal(tempBalance, loanType, sdate, edate);
775
					}
776
					if (formValues.RepaymentType == '10') { //flat pricipal
777
						lastStoreCal = me.calculateFlatPrincipal(tempBalance, loanTypeData, sdate, edate, formValues);
778
					} else if (formValues.RepaymentType == '20') { //flat total
779
						lastStoreCal = me.calculateFlatTotal(tempBalance, loanTypeData, sdate, edate, formValues);
780
					}
781

    
782
				} else if (AllLoanPaymentData[i].PaymentType = '2000') { // spesial repayment (pelunasan sebagian/seluruh)
783
					//get balance on date range in simulation grid
784
					tempBalanceNew = lastStoreCal.balance;
785

    
786
					tempCurrencyNew = AllLoanPaymentData[i].Currency;
787
					/*
788
					if (tempCurrencyNew != tempCurrency) { // jika beda currancy harus di convert ke currancy awal
789
					// get rate ke currancy awal @rate
790
					var rate = 100;
791
					// konversi tempBalanceNew ke currancy awal
792
					tempBalanceNew_ = tempBalanceNew * rate;
793
					tempBalance = tempBalanceNew_ - AllLoanPaymentData[i].Amount;
794
					} else {
795
					tempBalance = tempBalanceNew - AllLoanPaymentData[i].Amount;
796
					}
797
					 */
798
					tempBalance = tempBalanceNew - AllLoanPaymentData[i].Amount;
799
					var c = i + 1;
800
					if (countPayment > c) {
801
						sdate = AllLoanPaymentData[i].StartDate;
802
						edate = AllLoanPaymentData[i++].StartDate;
803
					} else {
804
						//sdate = AllLoanPaymentData[i].StartDate;
805
						sdate = tempBalanceNew.StartDate;
806
						//edate = "99991231";
807
						if (sdate.substring(4, 6) == '12') {
808
							y = parseInt(sdate.substring(0, 4)) + 1;
809
							m = '01';
810
							d = MinovaUtil.GetDays(1, 2016);
811
							edate = y + m + d
812

    
813
						}
814
					}
815
					//insert store to simulate grid
816
					var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
817
					idx = store_.getCount();
818
					data = {
819
						StartDate: sdate,
820
						EndDate: edate,
821
						EmployeeID: formValues.EmployeeID,
822
						HeaderSequence: formValues.HeaderSequence,
823
						Period: '',
824
						Principal: '',
825
						Interest: '',
826
						Repayment: AllLoanPaymentData[i].Amount,
827
						Balance: tempBalance,
828
					};
829
					lastStoreCal = data;
830
					/*
831
					if (formValues.RepaymentType == '01') { //flat pricipal
832

    
833
					} else if (formValues.RepaymentType == '02') { //flat total
834

    
835
					}
836
					 */
837

    
838
				}
839
			}
840

    
841
			// set EndDate to real enddte
842
			var store_ = Ext.ComponentQuery.query('[name=gridloanpayment]')[0].getStore();
843
			idx = store_.getCount();
844
			var edate = store_.data.items[idx - 1].data.EndDate;
845
			Ext.ComponentQuery.query('[name=EndDate]')[0].setValue(edate);
846
		}
847

    
848
	},
849

    
850
	overViewDblClick: function (me_, record, item, index, e, eOpts) {
851
		me = Ext.ComponentQuery.query('[name=phrpa0032]')[0];
852
		form = Ext.ComponentQuery.query('[name=mainformLoan]')[0]; // edit proses
853
		form.setActionSubmit(1);
854
		var selection = me.getView().getSelectionModel().getSelection()[0];
855
		var form_ = form.getForm();
856
		form_.setValues(selection.data);
857

    
858
		var HeaderSequence = selection.data.HeaderSequence;
859
		var EmployeeID = selection.data.EmployeeID;
860

    
861
		var param = 'EmployeeID[equal]' + EmployeeID + ',HeaderSequence[equal]' + HeaderSequence
862
			//load loan payment data
863
			var loanpaymentStore = Ext.StoreMgr.lookup("loanpayment");
864
		Ext.Ajax.request({
865
			//method : 'POST',
866
			url: '/UserControl/GetStore',
867
			params: {
868
				tableName: 'phrpa0033',
869
				param: param
870
			},
871

    
872
			success: function (response) {
873
				var results = Ext.decode(response.responseText);
874
				loanpaymentStore.loadData(results.data);
875

    
876
			}
877
		});
878

    
879
		//load loan regular payment data
880
		var loanregpaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
881
		Ext.Ajax.request({
882
			//method : 'POST',
883
			url: '/UserControl/GetStore',
884
			params: {
885
				tableName: 'phrpa0034',
886
				param: param
887
			},
888

    
889
			success: function (response) {
890
				var results = Ext.decode(response.responseText);
891
				loanregpaymentStore.loadData(results.data);
892

    
893
			}
894
		});
895

    
896
		var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
897
		card_.getLayout().setActiveItem(1);
898

    
899
		var __me = this;
900
		__me._action = "1";
901
		this.btnLoadDataLoan();
902

    
903
	},
904

    
905
	btnLoadDataLoan: function () {
906
		var loanData = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
907

    
908
		var mainForm = Ext.ComponentQuery.query('[name=formLoanMain]')[0];
909
		var formValues = mainForm.getValues();
910

    
911
		var HeaderSequence = formValues.HeaderSequence;
912
		var EmployeeID = formValues.EmployeeID;
913

    
914
		var param = 'EmployeeID[equal]' + EmployeeID + ',HeaderSequence[equal]' + HeaderSequence;
915

    
916
		Ext.Ajax.request({
917
			//method : 'POST',
918
			url: '/UserControl/GetStore',
919
			params: {
920
				tableName: 'phrpa0035',
921
				param: param
922
			},
923

    
924
			success: function (response) {
925
				var results = Ext.decode(response.responseText);
926
				loanData.loadData(results.data);
927

    
928
			}
929
		});
930
	},
931
	buttonOnClick: function (_button) {
932
		var me = this;
933
		switch (_button.name) {
934
		case 'saveLOAN':
935
			me.saveLoan();
936
			break;
937

    
938
		case 'deleteLOAN':
939
			MinovaMessage('Message', '000007', '', 'C', function (respone) {
940
				if (respone == "yes") {
941
					me.deleteLOAN();
942
				}
943
			});
944
			break;
945

    
946
		default:
947

    
948
		}
949
	},
950
	deleteLOAN: function () {
951
		var me = this;
952
		var Formheader = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
953
		if (Formheader.isValid()) {
954
			var pnl = Ext.ComponentQuery.query('[name=loanCard]')[0];
955
			pnl.mask('Loading...');
956
			Ext.Ajax.request({
957
				async: false,
958
				method: 'POST',
959
				url: '/Devt/DeleteLoanMaintenance',
960
				params: {
961
					header: Ext.encode(Formheader.getValues()),
962
				},
963
				waitMsg: 'Saving Data...',
964
				success: function (response) {
965

    
966
					var result = Ext.decode(response.responseText);
967
					if (result.success) {
968
						var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
969
						card_.getLayout().setActiveItem(0);
970
						MinovaMessage('', '000006', '', 'S');
971
						me.afterRenderOverview();
972
						pnl.unmask();
973
					} else {
974
						pnl.unmask();
975
						MinovaMessage('', '000005', result.message.text, "E")
976
					}
977
				},
978
				failure: function (response) {
979
					var result = Ext.decode(response.responseText);
980
					pnl.unmask();
981
					MinovaMessage('', '000005', result.message.text, "E")
982
				}
983
			});
984
		}
985
	},
986
	saveLoan: function () {
987
		var me = this;
988
		var Formheader = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
989
		if (Formheader.isValid()) {
990
			var pnl = Ext.ComponentQuery.query('[name=loanCard]')[0];
991
			pnl.mask('Loading...');
992
			var loanpayment = Ext.StoreMgr.lookup("loanpayment");
993
			var loanregularrepayment = Ext.StoreMgr.lookup("storeloanregularrepayment");
994
			var loanBalance = Ext.StoreMgr.lookup("loanBalance");
995
			var payment = Ext.encode(Ext.pluck(loanpayment.data.items, 'data'));
996
			var regular = Ext.encode(Ext.pluck(loanregularrepayment.data.items, 'data'));
997
			var balance = Ext.encode(Ext.pluck(loanBalance.data.items, 'data'));
998
			
999
			/*var countrepay = (Ext.StoreMgr.lookup("storeloanregularrepayment").count());
1000
			var maxtenor = "";
1001
			var maxstdt = "";
1002
			for (i = 0; i < countrepay; i++) {
1003
				var tenor = Ext.pluck(loanregularrepayment.data.items, 'data')[i].Tenor;
1004
				maxtenor = tenor; 
1005
				var stdt = Ext.pluck(loanregularrepayment.data.items, 'data')[i].StartDate;
1006
				maxstdt = stdt; 
1007
				var payment2000 = Ext.pluck(loanregularrepayment.data.items, 'data')[i].PaymentType;
1008
				if( payment2000 == "2000") { break; }
1009
			}*/
1010
			
1011
			var countpay = (Ext.StoreMgr.lookup("loanpayment").count());
1012
			var maxstdtpay = "";
1013
			for (i = 0; i < countpay; i++) {
1014
				var stdt = Ext.pluck(loanpayment.data.items, 'data')[i].StartDate;
1015
				maxstdtpay = stdt; 
1016
				var payment2000 = Ext.pluck(loanpayment.data.items, 'data')[i].PaymentType;
1017
				if( payment2000 == "2000") { break; }
1018
			}
1019
			
1020
			var countbalance = (Ext.StoreMgr.lookup("loanBalance").count());
1021
			var maxbalseq = "";
1022
			var maxbalstdt = "";
1023
			var balstdtbefore = "";
1024
			for (i = 0; i < countbalance; i++) {
1025
				var balseq = Ext.pluck(loanBalance.data.items, 'data')[i].Sequence;
1026
				maxbalseq = balseq;					
1027
				var balstdt = Ext.pluck(loanBalance.data.items, 'data')[i].StartDate;
1028
				if(parseInt(balstdt) < parseInt(balstdtbefore)) { break;}
1029
				maxbalstdt = balstdt;
1030
			} ////jika sequence terbesar ada di terakhir
1031

    
1032
			var errorsim = '';
1033
			if (countpay == '2')
1034
			{
1035
				if(maxbalstdt != maxstdtpay) { errorsim = 'Y'; }
1036
			}
1037
			/*if(countrepay == '1')
1038
			{
1039
				if(maxbalseq != maxtenor) { errorsim = 'Y'; } //else { errorsim = "N"; }
1040
			}
1041
			else if (countrepay == '2')
1042
			{
1043
				if(maxbalstdt != maxstdt) { errorsim = 'Y'; } //else { errorsim = "N"; }
1044
			}*/
1045
			
1046
			if(errorsim == 'Y')
1047
			{
1048
				MinovaMessage('', '000005', 'Can not save this data, please simulation first!', "E");
1049
				pnl.unmask();
1050
			}
1051
			else
1052
			{ 	console.log(Ext.ComponentQuery.query('[name=EmployeeID]')[0].getValue()); console.log(Ext.ComponentQuery.query('[name=StartDate]')[0].getValue());
1053
				var IsBackPeriod = '';
1054
				MinovaUtil.ExecuteParamQuery({ 
1055
					ID: 'LOANCEKPERIODPAYROLL',
1056
					EmployeeID: Ext.ComponentQuery.query('[name=EmployeeID]')[0].getValue(),
1057
					StartDate: Ext.ComponentQuery.query('[name=StartDate]')[0].getValue()
1058
				}, function (s) {
1059
					var result = Ext.decode(s.responseText);
1060
					var dt = Ext.decode(Ext.decode(result.data));
1061
					IsBackPeriod = dt[0].IsBackPeriod; 
1062
					pnl.unmask();
1063
				}); ////--- cek create loan apakah backdate perioda payroll atau tidak? add By Tri nwh 20220322
1064
				
1065
				if(Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue() == '' && IsBackPeriod == 'BACKPERIOD') 
1066
				{	
1067
					MinovaMessage('Not Null', 'LOAN1', '', "E");
1068
					pnl.unmask(); console.log('ssh'); ////---- untuk create loan baru backdate add by Tri nwh 20220322
1069
				}
1070
				else
1071
				{	console.log('khj');
1072
					Ext.Ajax.request({
1073
						async: false,
1074
						method: 'POST',
1075
						url: '/Devt/SaveLoanMaintenance',
1076
						params: {
1077
							header: Ext.encode(Formheader.getValues()),
1078
							payment: payment,
1079
							regular: regular,
1080
							balance: balance,
1081
							action: this._action
1082
						},
1083
						waitMsg: 'Saving Data...',
1084
						success: function (response) {
1085

    
1086
							var result = Ext.decode(response.responseText);
1087
							if (result.success) {
1088
								var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
1089
								card_.getLayout().setActiveItem(0);
1090
								MinovaMessage('', '000006', '', 'S');
1091
								me.afterRenderOverview();
1092
								pnl.unmask();
1093
							} else {
1094
								pnl.unmask();
1095
								//MinovaMessage('', result.message.id, result.message.text, "E");
1096
								MinovaMessage('', '000005', result.message.text, "E")
1097
							}
1098
						},
1099
						failure: function (response) {
1100
							var result = Ext.decode(response.responseText);
1101
							pnl.unmask();
1102
							// MinovaMessage('', result.message.id, result.message.text, "E")
1103
							MinovaMessage('', '000005', result.message.text, "E")
1104
						}
1105
					});
1106
				}
1107
			}
1108
		} else {
1109
			var param_ = me.cekMandatoryIsNull();
1110
			if (param_ != "") {
1111
				MinovaMessage('Not Null', '000009', param_, 'E');
1112
			} else {
1113
				// ada field yang error
1114
				var error_ = me.getErrorFields();
1115
				if (error_ != '') {
1116
					MinovaMessage('title', '000005', error_, 'E');
1117
				}
1118

    
1119
			}
1120
		}
1121
	},
1122
	cekMandatoryIsNull: function () {
1123
		var fields_ = Ext.ComponentQuery.query('[wasValid=false][allowBlank=false]');
1124
		var param_ = '';
1125
		if (fields_.length > 0) {
1126
			for (var i = 0; i < fields_.length; i++) {
1127
				if (i == 0) {
1128
					param_ = ':' + fields_[i].fieldLabel
1129
				} else {
1130
					param_ = param_ + ',' + fields_[i].fieldLabel;
1131
				}
1132

    
1133
			}
1134
		}
1135
		return param_
1136
	},
1137
	getErrorFields: function () {
1138
		var fields_ = Ext.ComponentQuery.query('[wasValid=false]');
1139
		var param_ = '';
1140
		if (fields_.length > 0) {
1141
			for (var i = 0; i < fields_.length; i++) {
1142
				if (i == 0) {
1143
					param_ = ':' + fields_[i].fieldLabel;
1144
				} else {
1145
					param_ = param_ + ',' + fields_[i].fieldLabel;
1146
				}
1147

    
1148
			}
1149
		}
1150
		return param_
1151
	},
1152
	setKeyDisabled: function (param) {
1153
		var frmEdit = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
1154
		var fields_ = frmEdit.getFields().items;
1155
		for (var i = 0; i < fields_.length; i++) {
1156
			var field_ = frmEdit.getFields().items[i];
1157
			iskey_ = field_.IsPrimaryKey;
1158
			if (iskey_ == '1') {
1159
				field_.setReadOnly(param);
1160
			}
1161
		}
1162
	},
1163
	hitungTenor: function (editor, e) {
1164
		//alert("HITUNG TENOR");
1165
		var main_ = Ext.getCmp('gridloanregularrepayment');
1166
		var record = main_.getSelectionModel().getSelection()[0];
1167
		var amount_ = 0;
1168
		var tenor_ = 0;
1169
		if (record.data.Tenor != '0' || record.data.Amount != '0') {
1170
			var loanpayment = Ext.StoreMgr.lookup("loanpayment");
1171
			var payment = Ext.pluck(loanpayment.data.items, 'data');
1172
			var countPayment = payment.length;
1173
			var Balance = 0;
1174
			if (countPayment > 0) {
1175
				for (i = 0; i < countPayment; i++) {
1176
					Balance += parseFloat(payment[i].Amount);
1177
				}
1178
			}
1179
			var Data = 0;
1180
			if (record.data.Amount == "0")
1181
				record.data.Amount = Math.ceil(Math.round(Balance / parseFloat(record.data.Tenor) * 100) / 100);
1182
			else if (record.data.Tenor == "0")
1183
				record.data.Tenor = Math.ceil(Math.round(Balance / parseFloat(record.data.Amount) * 100) / 100);
1184
			main_.getView().refresh();
1185
		}
1186
		//edit amount
1187
		var Interest = 0;
1188
		var Tenor = 0;
1189
		var Koefisien = 0;
1190
		var amount1 = parseFloat(Ext.ComponentQuery.query('[name=Amount]')[0].getValue());
1191
		console.log("amount1=" + amount1);
1192
		var hasil = 0;
1193
		Ext.Ajax.request({
1194
			async: false,
1195
			method: 'POST',
1196
			url: '/UserControl/GetStore',
1197
			params: {
1198
				tableName: 'PHRBNLOANTYP',
1199
				param: 'LoanType[equal]' + Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm().getValues().LoanType + ',Tenor[equal]' + record.data.Tenor
1200
			},
1201
			success: function (response) {
1202
				results = Ext.decode(response.responseText);
1203
				Interest = record.data.RateofInterest;
1204
				console.log("Interest=" + Interest);
1205
				amount_ = record.data.Amount;
1206
				Tenor = record.data.Tenor;
1207
				console.log("Tenor=" + Tenor);
1208
				Koefisien = parseFloat("0");//parseFloat(results.data[0].Koefisien);
1209
				console.log("Koefisien=" + Koefisien);
1210
				/* hasil = amount1 * (1 + Koefisien) / Tenor; */
1211
				hasil = ((amount1*((Math.pow((1+(Interest/100/12)),Tenor))*(Interest/100/12))/((Math.pow((1+(Interest/100/12)),Tenor))-1)));
1212
				console.log("hasil=" + hasil);
1213
				//record.set('Amount', hasil);
1214
				record.set('Amount', amount_);
1215
			}
1216
		});
1217
	},
1218
	addPayment: function () {
1219
		var store = Ext.StoreMgr.lookup('loanpayment');
1220
		idx = store.getCount();
1221
		var amount = Ext.ComponentQuery.query('[name=Amount]')[0].getValue();
1222
		var Sequence = 0;
1223
		if (idx == 0) {
1224
			Sequence = 1;
1225
		} else {
1226
			Sequence = 1 + idx;
1227
		}
1228
		var data = {
1229
			StartDate: Ext.ComponentQuery.query('[name=StartDate]')[0].getValue(),
1230
			EndDate: Ext.ComponentQuery.query('[name=EndDate]')[0].getValue(),
1231
			Currency: 'IDR',
1232
			PaymentType: '1000',
1233
			Amount: amount,
1234
			EmployeeID:'',
1235
			HeaderSequence:'',
1236
			Sequence:'',
1237
			UID:'',
1238
			Notes:''
1239
		}
1240
		store.insert(idx, data);
1241
	},
1242
	deletePayment: function () {
1243
		var me = this,
1244
		store = Ext.StoreMgr.lookup('loanpayment')
1245
			var grid = Ext.getCmp('gridloanpayment');
1246
		Ext.MessageBox.show({
1247
			title: 'Remove tab',
1248
			msg: "This will remove. Do you want to continue?",
1249
			buttons: Ext.MessageBox.YESNO,
1250
			fn: function (choice) {
1251
				if (choice === 'yes') {
1252
					var selection = grid.getView().getSelectionModel().getSelection()[0];
1253
					if (selection) {
1254
						var ExistDataPayment = ''; var HeaderSequence = ''; 	////---- untuk case tidak boleh delete special repayment
1255
						if(Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue() == '') { HeaderSequence = '0'; } else { HeaderSequence = Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue();}
1256
						MinovaUtil.ExecuteParamQuery({ 
1257
							ID: 'LOANCEKDATAPAYMENT',
1258
							EmployeeID: Ext.ComponentQuery.query('[name=EmployeeID]')[0].getValue(),
1259
							HeaderSequence: HeaderSequence
1260
						}, function (s) {
1261
							var result = Ext.decode(s.responseText);
1262
							var dt = Ext.decode(Ext.decode(result.data));
1263
							ExistDataPayment = dt[0].ExistData; console.log(ExistDataPayment);
1264
						}); ////--- cek apakah data payment sebelumnya atau tidak? add By Tri nwh 20220418
1265
						if(ExistDataPayment != '0')
1266
						{ 
1267
							MinovaMessage('Not Null', 'LOAN2', '', "E");  console.log('seunghyun');
1268
						}
1269
						else
1270
						{
1271
							store.remove(selection); console.log('chani');
1272
						}
1273
						//store.remove(selection); 	////---- dipindah keatas untuk case tidak boleh delete special repayment
1274
					}
1275
				}
1276
			}
1277
		});
1278
	},
1279
	addRepayment: function () {
1280
		var store = Ext.StoreMgr.lookup('storeloanregularrepayment');
1281
		idx = store.getCount();
1282
		var action = getParam('action');
1283
		var Sequence = 0;
1284
		if (idx == 0) {
1285
			Sequence = 1;
1286
		} else {
1287
			Sequence = 1 + idx;
1288
		}
1289
		var Interest = 0;
1290
		var Tenor = 0;
1291
		var Koefisien = 0;
1292
		var amount1 = parseFloat(Ext.ComponentQuery.query('[name=Amount]')[0].getValue());
1293
		console.log("amount1=" + amount1);
1294
		var hasil = 0;
1295
		Ext.Ajax.request({
1296
			async: false,
1297
			method: 'POST',
1298
			url: '/UserControl/GetStore',
1299
			params: {
1300
				tableName: 'PHRBNLOANTYP',
1301
				param: 'LoanType[equal]' + Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm().getValues().LoanType
1302
			},
1303
			success: function (response) {
1304
				results = Ext.decode(response.responseText);
1305
				Interest = parseFloat(results.data[0].Interest);
1306
				Tenor = parseFloat(results.data[0].Tenor);
1307
				console.log("Tenor=" + Tenor);
1308
				Koefisien = parseFloat(results.data[0].Koefisien);
1309
				console.log("Koefisien=" + Koefisien);
1310
				hasil = amount1 * (1 + Koefisien) / Tenor;
1311
				console.log("hasil=" + hasil);
1312
			}
1313
		});
1314
		var data = {
1315
			StartDate: Ext.ComponentQuery.query('[name=StartDate]')[0].getValue(),
1316
			EndDate: Ext.ComponentQuery.query('[name=EndDate]')[0].getValue(),
1317
			Sequence: Sequence,
1318
			RateofInterest: Interest,
1319
			Currency: 'IDR',
1320
			RepaymentType: '20',
1321
			Tenor: 0,
1322
			Amount: 0
1323

    
1324
		}
1325
		store.insert(idx, data);
1326
	},
1327
	deleteRepayment: function () {
1328
		var me = this,
1329
		store = Ext.StoreMgr.lookup('storeloanregularrepayment')
1330

    
1331
			var grid = Ext.getCmp('gridloanregularrepayment');
1332

    
1333
		Ext.MessageBox.show({
1334
			title: 'Remove tab',
1335
			msg: "This will remove. Do you want to continue?",
1336
			buttons: Ext.MessageBox.YESNO,
1337
			fn: function (choice) {
1338
				console.log(choice);
1339
				if (choice === 'yes') {
1340
					var selection = grid.getView().getSelectionModel().getSelection()[0];
1341
					if (selection) {
1342
						store.remove(selection);
1343
					}
1344
				}
1345
				//delete panel.pendingClose;
1346
			}
1347
		});
1348
	},
1349
	NewSimulateLoan: function () {
1350
		var Formheader = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
1351
		var loanpayment = Ext.StoreMgr.lookup("loanpayment");
1352
		var loanregularrepayment = Ext.StoreMgr.lookup("storeloanregularrepayment");
1353
		var params = {
1354
			serviceName: 'HRLoan.asmx',
1355
			methodName: 'SimulateLoan',
1356
			parameter:
1357
			'dataHeader=' + Ext.encode(Formheader.getValues())
1358
			 + '&dataPayment=' + Ext.encode(Ext.pluck(loanpayment.data.items, 'data'))
1359
			 + '&dataRepayment=' + Ext.encode(Ext.pluck(loanregularrepayment.data.items, 'data'))
1360
		};
1361
		MinovaAjaxPost('/InvokeService/Invoke/', params, function (xhr) {
1362
			result = Ext.decode(xhr.responseText);
1363
			var loanBalance = Ext.StoreMgr.lookup("loanBalance");
1364
			loanBalance.removeAll();
1365
			loanBalance.add(Ext.decode(result.data));
1366
		});
1367
	},
1368
	mapComboTenor: function () {
1369
		var loanType = Ext.ComponentQuery.query('[name=LoanType]')[0].getValue();
1370
		var storeTenor = Ext.StoreMgr.lookup("store_phrpa0034Tenor");
1371

    
1372
		storeTenor.proxy.extraParams = {
1373
			tableName: 'PDSBNLOANTENOR',
1374
			param: loanType
1375
		};
1376
		storeTenor.removeAll();
1377
		storeTenor.reload();
1378
	},
1379
	
1380
	specialPay: function () {
1381
		var paymentType = Ext.ComponentQuery.query('[id=phrpa0033PaymentType]')[0].getValue();
1382
		var StartDate = Ext.ComponentQuery.query('[id=phrpa0033StartDate]')[0].getValue();
1383
		var EmployeeID = getParam('EmployeeID');
1384
		var HeaderSequence = Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue();
1385
		var LoanType = Ext.ComponentQuery.query('[name=LoanType]')[0].getValue();
1386

    
1387
		if (paymentType == '2000') {
1388
					
1389
			var IsBackPeriod = '';
1390
			MinovaUtil.ExecuteParamQuery({ 
1391
				ID: 'LOANCEKPERIODPAYROLL',
1392
				EmployeeID: Ext.ComponentQuery.query('[name=EmployeeID]')[0].getValue(),
1393
				StartDate: StartDate
1394
			}, function (s) {
1395
				var result = Ext.decode(s.responseText);
1396
				var dt = Ext.decode(Ext.decode(result.data));
1397
				IsBackPeriod = dt[0].IsBackPeriod; 
1398
				console.log(IsBackPeriod);
1399
			}); ////--- cek create loan apakah backdate perioda payroll atau tidak? add By Tri nwh 20220322
1400
			
1401
			//Ext.ComponentQuery.query('[id=phrpa0033Amount]')[0].setReadOnly(true);
1402
			var Amount = 0;
1403
			
1404
			Ext.Ajax.request({
1405
			async: false,
1406
			method: 'POST',
1407
			url: '/UserControl/GetStore',
1408
			params: {
1409
				tableName: 'PDSLOANLASTBALANCEPELUNASAN',
1410
				param: EmployeeID + ',' + StartDate + ',' + HeaderSequence + ',' + LoanType
1411
			},
1412
			success: function (response) {
1413
				var results = Ext.decode(response.responseText);
1414
				var dt = results.data;
1415
				data_ = dt[0];
1416
				Amount = parseFloat(data_.LastBalance);
1417
			}
1418
			});
1419
			Ext.ComponentQuery.query('[id=phrpa0033Amount]')[0].setValue(Amount);
1420
			
1421
		} /*else {
1422
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].allowBlank = false;
1423
			Ext.ComponentQuery.query('[id=phrpa0034Amount]')[0].setReadOnly(true);
1424
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].validate();
1425
		}*/
1426
	}
1427
});
(8-8/8)