Project

General

Profile

Bug #1020 » ctrlloan.js

Tri Rizqiaty, 04/19/2022 04:32 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=phrpa0034PaymentType]': {
67
				select: this.specialRep
68
			},
69
			'[id=phrpa0034StartDate]': {
70
				change: this.specialRep
71
			},
72
			'[id=phrpa0033PaymentType]': {
73
				select: this.specialRep
74
			},
75
			'[id=phrpa0033StartDate]': {
76
				change: this.specialRep
77
			}
78
		});
79
	},
80

    
81
	cekAuth: function () {
82
		actionMenu_ = MinovaUtil.GetActionMenu();
83
		if (actionMenu_ == '0') {
84
			Ext.ComponentQuery.query('[name=actionGrid]')[0].setHidden(true);
85
			Ext.ComponentQuery.query('[name=saveLOAN]')[0].setHidden(true);
86
			Ext.ComponentQuery.query('[name=deleteLOAN]')[0].setHidden(true);
87
		}
88
	},
89
	afterRenderOverview: function () {
90
	
91
		this.cekAuth();
92
		var overView = Ext.StoreMgr.lookup("loanOverview");
93

    
94
		overView.proxy.extraParams = {
95
			tableName: 'phrpa0032',
96
			param: 'EmployeeID[=]' + getParam('EmployeeID')
97
		};
98
		overView.removeAll();
99
		overView.reload();
100
		overView.loadPage(1);
101
		var FullName = getParam("FullName");
102
		var EmpID = getParam('EmployeeID');
103
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Edit - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
104
	},
105

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

    
144
		var payrolBasic = null;
145
		Ext.Ajax.request({
146
			async: false,
147
			method: 'POST',
148
			url: '/UserControl/GetStore',
149
			params: {
150
				tableName: 'PHRPA0003',
151
				param: 'EmployeeID[equal]' + EmployeeID + ',StartDate[<=]' + nowDate + ',EndDate[>=]' + now
152
			},
153
			success: function (response) {
154
				var results = Ext.decode(response.responseText);
155
				payrolBasic = results.data[0];
156
			}
157
		});
158

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

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

    
191
		// get wt amount
192
		var Valuation = undefined;
193
		var wtAmount = undefined;
194
		if (WTdata) {
195
			Valuation = WTdata.Valuation;
196
		} else {
197
			// wt tidak terdaftar
198
		}
199

    
200
		//get wt amount
201
		if (Valuation) {}
202

    
203
		var plafondAmount = parseFloat(plafond.PlafondWagetype) * parseFloat(plafond.PlafondMutiplier);
204
		// get amount
205

    
206

    
207
	},
208

    
209
	cekPaymentTotal: function () {
210
		var me = this;
211
		var allPayment = me.AllLoanPaymentData();
212
		var totalPinjaman = 0;
213
		var spesialRepayment = 0;
214
		var val_ = 0;
215

    
216
		if (allPayment) {
217
			//
218
			Ext.each(allPayment, function (rec) {
219
				val_ = parseInt(rec.Amount);
220
				if (rec.PaymentType == '1000') //1000 = payment, 2000=spesial repayment
221
				{
222
					totalPinjaman = totalPinjaman + val_;
223
				} else {
224
					spesialRepayment = spesialRepayment + val_;
225
				}
226
			});
227

    
228
			var paymentTotal = totalPinjaman - spesialRepayment
229
				var headerAmount = Ext.ComponentQuery.query('[name=Amount]')[0].getValue();
230
			if (paymentTotal <= headerAmount) {
231
				return true;
232
			} else {
233
				return false;
234
			}
235
		}
236
	},
237

    
238
	//afterrenderGrid: function (g) {
239
	//    g.down('[dataIndex=HeaderSequence]').setVisible(false);
240
	//    g.down('[dataIndex=EmployeeID]').setVisible(false);
241
	//    /*
242
	//	if (g.down('[dataIndex=Currency]')) {
243
	//	g.down('[dataIndex=Currency]').setVisible(false);
244
	//	}
245
	//	 */
246
	//},
247
	setHeaderSeqEmpGrid: function (editor, context, eOpts) {
248
		var mainForm = Ext.ComponentQuery.query('[name=mainformLoan]')[0];
249
		var EmployeeID = getParam('EmployeeID');
250
		var HeaderSequence = Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue();
251
		var Currency = Ext.ComponentQuery.query('[name=Currency]')[0].getValue();
252

    
253
		if (mainForm.getForm().isValid()) {
254
			var grid_ = context.grid;
255
			record = grid_.getSelectionModel().getSelection()[0];
256
			record.set("EmployeeID", EmployeeID);
257
			record.set("HeaderSequence", HeaderSequence);
258
			record.set("Currency", Currency);
259

    
260
			var g = context.grid.getPlugin('rowEditing').editor.form.findField('Currency');
261
			if (g) {
262
				g.disable();
263
			}
264

    
265
		} else {
266
			MinovaMessage('', '000009', '', "E");
267
			return false;
268
		}
269

    
270
	},
271

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

    
287
					return false;
288
				}
289
			}
290
		} else { // edit
291
			// load last balance on
292
		}
293

    
294
	},
295

    
296
	setHeaderSeqEmpForm: function (act) {
297
		//set header sequence
298
		var EmployeeID = getParam('EmployeeID');
299

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

    
317
					if (data_.length > 0) {
318
						var dt_ = data_[data_.length - 1].HeaderSequence;
319
						HeaderSequence.setValue(dt_ + 1);
320
					} else {
321
						HeaderSequence.setValue(1);
322
					}
323
				}
324

    
325
			}
326
		});
327
	},
328

    
329
	addLOAN: function () {
330
		var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
331
		card_.getLayout().setActiveItem(1);
332
		form_ = Ext.ComponentQuery.query('[name=mainformLoan]')[0];
333
		form_.getForm().reset();
334
		form_.getForm().findField('EmployeeID').setValue(getParam('EmployeeID'));
335
		form_.setActionSubmit(0);
336
		//this.setHeaderSeqEmpForm(0);
337
		var store_simulation = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();	////// --- add by Tri nwh 20211227
338
		console.log('nwh'); store_simulation.removeAll(); ////// --- add by Tri nwh 20211227
339
		var store_payment = Ext.ComponentQuery.query('[name=gridloanpayment]')[0].getStore(); ////// --- add by Tri nwh 20211227
340
		store_payment.removeAll(); ////// --- add by Tri nwh 20211227
341
		var store_repayment = Ext.ComponentQuery.query('[name=gridloanregularrepayment]')[0].getStore(); ////// --- add by Tri nwh 20211227
342
		store_payment.removeAll(); ////// --- add by Tri nwh 20211227
343
		var me = this;
344
		me.setKeyDisabled(false);
345
		me._action = "0";
346
		var FullName = getParam("FullName");
347
		var EmpID = getParam('EmployeeID');
348
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Add - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
349
		 this.btnLoadDataLoan();
350
		setTimeout(function(){ 	var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
351
		var store_2 = Ext.ComponentQuery.query('[name=gridloanpayment]')[0].getStore();
352
		var store_3 = Ext.ComponentQuery.query('[name=gridloanregularrepayment]')[0].getStore();
353
		store_.removeAll(); 
354
		store_2.removeAll(); 
355
		store_3.removeAll(); 
356
		}, 1200);	 	
357

    
358
	},
359
	copyLOAN: function () {
360
		this.overViewDblClick();
361
		form = Ext.ComponentQuery.query('[name=mainformLoan]')[0]; // edit proses
362
		form.setActionSubmit(0);
363
		var me = this;
364
		me.setKeyDisabled(false);
365
		me._action = "0";
366
		var FullName = getParam("FullName");
367
		var EmpID = getParam('EmployeeID');
368
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Copy - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
369
		this.btnLoadDataLoan();
370
	},
371
	cancel: function () {
372
		Ext.Msg.show({
373
			title: "",
374
			msg: MinovaMessageData('000008', 'Over View'),
375
			buttons: Ext.Msg.YESNO,
376
			icon: Ext.Msg.QUESTION,
377
			fn: function (choice) {
378
				if (choice === 'yes') {
379
					var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
380
					card_.getLayout().setActiveItem(0);
381
					var FullName = getParam("FullName");
382
					var EmpID = getParam('EmployeeID');
383
					Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Loan Maintenance' + ' - ' + EmpID + ' - ' + FullName);
384
				}
385

    
386
			}
387
		});
388

    
389
	},
390
	editLOAN: function () {
391
		this.overViewDblClick();
392
		form = Ext.ComponentQuery.query('[name=mainformLoan]')[0]; // edit proses
393
		form.setActionSubmit(0);
394
		var me = this;
395
		me.setKeyDisabled(true);
396
		me._action = "1";
397
		var FullName = getParam("FullName");
398
		var EmpID = getParam('EmployeeID');
399
		Ext.ComponentQuery.query('[name=mainformLoan]')[0].setTitle('Edit - Loan Transaction' + ' - ' + EmpID + ' - ' + FullName);
400
		this.btnLoadDataLoan();
401
	},
402

    
403
	// get all loanpayment data
404
	AllLoanPaymentData: function () {
405
		var loanpaymentStore = Ext.StoreMgr.lookup("loanpayment");
406
		var loanpaymentData = undefined;
407
		if (loanpaymentStore.count() > 0) { //cek apakah loan payment tidak kosong
408

    
409
			var loanpaymentData = Ext.pluck(loanpaymentStore.data.items, 'data');
410

    
411
		} else {
412
			alert('isi loan payment');
413
		}
414

    
415
		return loanpaymentData;
416
	},
417

    
418
	// get all regular repayment data
419
	AllRepaymentData: function () {
420
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
421
		var loanregrepaymentData = undefined;
422
		if (loanregrepaymentStore.count() > 0) { //cek apakah loan reg repayment tidak kosong
423

    
424
			var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
425

    
426
		} else {
427
			alert('isi loan reg repayment');
428
		}
429

    
430
		return loanregrepaymentData;
431
	},
432
	simulateLoan: function () {
433
		var me = this;
434
		var mainForm = Ext.ComponentQuery.query('[name=formLoanMain]')[0];
435
		var formValues = mainForm.getValues();
436
		var AllLoanPaymentData = me.AllLoanPaymentData();
437
		var AllRepaymentData = me.AllRepaymentData();
438

    
439
		//cek apakah form , payment dan regular payment sudah diisi
440
		var loanpaymentStore = Ext.StoreMgr.lookup("loanpayment").count();
441
		var loanregpaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment").count();
442

    
443
		if ((mainForm.getForm().isValid()) && loanpaymentStore > 0 && loanregpaymentStore > 0) {
444
			me.runSimulateLoan(formValues, AllLoanPaymentData, AllRepaymentData);
445
		} else {
446
			if (loanpaymentStore = 0) {
447
				MinovaMessage('', 'BS0001', 'Loan Payment', "E");
448
			} else if (loanregpaymentStore = 0) {
449
				MinovaMessage('', 'BS0001', 'Loan Regular Payment', "E");
450
			} else {
451
				MinovaMessage('', 'BS0001', 'Loan Main Form', "E");
452
			}
453
		}
454

    
455
	},
456

    
457
	calculateFlatPrincipal: function (tempBalance, loanTypeData, sdate, edate, formValues) {
458

    
459
		var mounth_ = parseInt(sdate.substring(4, 6)); // bulan awal cicilan
460
		var year_ = parseInt(sdate.substring(0, 4)); // tahun awal cicilan
461
		var period_ = undefined;
462
		var _interestMethod = loanTypeData.InterestMethod;
463
		var balance = parseFloat(tempBalance);
464
		var lastStoreCal = undefined;
465
		//var _interestMethod = "act/360";
466
		//var _interestMethod = Ext.ComponentQuery.query('[name=RepaymentType]')[0].getValue();
467
		var totalDayinMounth = undefined; // jumlah hari pengali perbulan
468
		var dayinYear = undefined; // jumlah hari pembagi dalam 1 thn
469
		var interestMethod = _interestMethod.substring(0, 3);
470
		var act = interestMethod.substring(0, 3);
471
		if (act == 'act') {
472
			dayinYear = _interestMethod.substring(4, 7);
473
			dayinYear = parseInt(dayinYear);
474
			totalDayinMounth = parseInt(MinovaUtil.GetDays(mounth_, year_));
475
		} else {
476
			totalDayinMounth = parseInt(_interestMethod.substring(0, 2));
477
			dayinYear = _interestMethod.substring(3, 6);
478
			dayinYear = parseInt(dayinYear)
479
		}
480

    
481
		//get all regular repayment
482
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
483
		var loanregrepaymentData = undefined;
484
		if (loanregrepaymentStore.count() > 0) { //cek apakah loan reg repayment tidak kosong
485

    
486
			var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
487

    
488
		} else {
489
			alert('isi loan reg repayment');
490
		}
491
		var countRepayment = loanregrepaymentData.length;
492
		var sdateRepayment = undefined;
493
		var edateRepayment = undefined;
494
		var seq = 0;
495
		//var totalDays = undefined;
496

    
497
		//var totalDayinMounth = undefined;// jumlah hari pengali perbulan
498
		//var dayinYear = undefined;// jumlah hari pembagi dalam 1 thn
499

    
500
		for (i = 0; i < countRepayment; i++) {
501

    
502
			if (countRepayment > 1) {
503
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
504
				edateRepayment = parseInt(loanregrepaymentData[i + 1].StartDate);
505

    
506
			} else {
507
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
508
				edateRepayment = 99991231;
509
			}
510

    
511
			// calculate simulate
512
			var bunga = parseFloat(loanregrepaymentData[i].RateofInterest);
513

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

    
518
				var _StartDate = undefined;
519
				var _EndDate = undefined;
520

    
521
				for (tempTenor = 0; tempTenor < parseInt(tenor); tempTenor++) {
522
					period_ = year_;
523
					if (act == 'act') {
524
						_StartDate = year_.toString() + mounth_.toString() + "01";
525
						_EndDate = year_.toString() + mounth_.toString() + totalDayinMounth;
526
						if (mounth_ == 12) {
527
							year_ = year_ + 1;
528
							mounth_ = 1;
529

    
530
						} else {
531
							mounth_ = mounth_ + 1;
532
						}
533
					} else {
534
						if (mounth_ < 10) {
535
							_StartDate = year_.toString() + "0" + mounth_.toString() + "01";
536
							_EndDate = year_.toString() + "0" + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
537
						} else {
538
							_StartDate = year_.toString() + mounth_.toString() + "01";
539
							_EndDate = year_.toString() + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
540
						}
541

    
542
						if (mounth_ == 12) {
543
							year_ = year_ + 1;
544
							mounth_ = 1;
545

    
546
						} else {
547
							mounth_ = mounth_ + 1;
548
						}
549
					}
550

    
551
					//get interest total
552
					var totInterest = (totalDayinMounth / dayinYear) * (balance * bunga) / 100;
553
					totInterest = Math.round(totInterest * 1000) / 1000; // round up 3 angka dibelakang koma
554
					//get New Balance
555
					balance = balance - parseFloat(cicilanPokok);
556
					//get employee must pay
557
					var totalRepayment = parseFloat(cicilanPokok) + totInterest;
558

    
559
					//insert store to simulate grid
560
					seq++;
561
					var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
562
					idx = store_.getCount();
563
					data = {
564
						StartDate: _StartDate,
565
						EndDate: _EndDate,
566
						EmployeeID: formValues.EmployeeID,
567
						HeaderSequence: formValues.HeaderSequence,
568
						Period: period_,
569
						Principal: cicilanPokok,
570
						Interest: totInterest,
571
						Repayment: totalRepayment,
572
						Balance: balance,
573
						Sequence: seq,
574
					};
575
					lastStoreCal = data;
576
					store_.insert(idx, data);
577
				}
578
			}
579
		}
580
		return lastStoreCal;
581
	},
582

    
583
	calculateFlatTotal: function (tempBalance, loanTypeData, sdate, edate, formValues) {
584
		var lastStoreCal = undefined;
585
		var mounth_ = parseInt(sdate.substring(4, 6)); // bulan awal cicilan
586
		var year_ = parseInt(sdate.substring(0, 4)); // tahun awal cicilan
587
		var period_ = undefined;
588
		var _interestMethod = loanTypeData.InterestMethod;
589
		//var _interestMethod = "act/360";
590
		//var _interestMethod = Ext.ComponentQuery.query('[name=RepaymentType]')[0].getValue();
591
		var totalDayinMounth = undefined; // jumlah hari pengali perbulan
592
		var dayinYear = undefined; // jumlah hari pembagi dalam 1 thn
593
		var interestMethod = _interestMethod.substring(0, 3);
594
		var act = interestMethod.substring(0, 3);
595
		if (act == 'act') {
596
			dayinYear = _interestMethod.substring(4, 7);
597
			dayinYear = parseInt(dayinYear);
598
			totalDayinMounth = parseInt(MinovaUtil.GetDays(mounth_, year_));
599
		} else {
600
			totalDayinMounth = parseInt(_interestMethod.substring(0, 2));
601
			dayinYear = _interestMethod.substring(3, 6);
602
			dayinYear = parseInt(dayinYear)
603
		}
604

    
605
		//get all regular repayment
606
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
607
		var loanregrepaymentData = undefined;
608
		if (loanregrepaymentStore.count() > 0) { //cek apakah loan reg repayment tidak kosong
609

    
610
			var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
611

    
612
		} else {
613
			alert('isi loan reg repayment');
614
		}
615
		var countRepayment = loanregrepaymentData.length;
616
		var sdateRepayment = undefined;
617
		var edateRepayment = undefined;
618
		for (i = 0; i < countRepayment; i++) {
619

    
620
			if (countRepayment > 1) {
621
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
622
				edateRepayment = parseInt(loanregrepaymentData[i + 1].StartDate);
623

    
624
			} else {
625
				sdateRepayment = parseInt(loanregrepaymentData[i].StartDate);
626
				edateRepayment = 99991231;
627
			}
628

    
629
			// calculate simulate
630
			var bunga = parseFloat(loanregrepaymentData[i].RateofInterest);
631

    
632
			// if (parseInt(sdate) >= sdateRepayment && parseInt(edate) <= edateRepayment) { // cek startdate dan endate payment dengan repayment valid kah?
633
			var _StartDate = undefined;
634
			var _EndDate = undefined;
635
			if (parseInt(sdate) <= sdateRepayment && parseInt(edate) <= edateRepayment) {
636
				var cicilan = parseFloat(loanregrepaymentData[i].Amount);
637
				var tenor = loanregrepaymentData[i].Tenor;
638
				var balance = parseFloat(tempBalance);
639
				var seq = 0;
640
				//for (tempTenor = 0; tempTenor <= tenor; tempTenor++)
641
				do {
642
					period_ = year_;
643
					if (act == 'act') {
644
						_StartDate = year_.toString() + mounth_.toString() + "01";
645
						_EndDate = year_.toString() + mounth_.toString() + totalDayinMounth;
646
						if (mounth_ == 12) {
647
							year_ = year_ + 1;
648
							mounth_ = 1;
649

    
650
						} else {
651
							mounth_ = mounth_ + 1;
652
						}
653
					} else {
654
						if (mounth_ < 10) {
655
							_StartDate = year_.toString() + "0" + mounth_.toString() + "01";
656
							_EndDate = year_.toString() + "0" + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
657
						} else {
658
							_StartDate = year_.toString() + mounth_.toString() + "01";
659
							_EndDate = year_.toString() + mounth_.toString() + new Date(year_, mounth_, 0).getDate();
660
						}
661

    
662
						if (mounth_ == 12) {
663
							year_ = year_ + 1;
664
							mounth_ = 1;
665

    
666
						} else {
667
							mounth_ = mounth_ + 1;
668
						}
669
					}
670

    
671
					//get interest total
672
					var totInterest = (totalDayinMounth / dayinYear) * (balance * bunga) / 100;
673
					totInterest = Math.round(totInterest * 1000) / 1000; // round up 3 angka dibelakang koma
674
					// get real pricipal
675
					var pricipal_ = cicilan - totInterest;
676
					if (cicilan > balance) {
677
						pricipal_ = balance
678
							//cicilan = Math.round((bunga + balance - cicilan) * 1000) / 1000;
679
							cicilan = bunga + balance;
680
						balance = 0;
681

    
682
					} else {
683
						balance = Math.round((balance - pricipal_) * 1000) / 1000;
684
					}
685

    
686
					//insert store to simulate grid
687
					//simulation
688
					seq++;
689
					var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
690
					idx = store_.getCount();
691
					data = {
692
						StartDate: _StartDate,
693
						EndDate: _EndDate,
694
						EmployeeID: formValues.EmployeeID,
695
						HeaderSequence: formValues.HeaderSequence,
696
						Period: period_,
697
						Principal: pricipal_,
698
						Interest: totInterest,
699
						Repayment: cicilan,
700
						Balance: balance,
701
						Sequence: seq,
702
					};
703
					lastStoreCal = data;
704
					store_.insert(idx, data);
705
				} while (balance != 0);
706
			}
707
		}
708
		return lastStoreCal;
709
	},
710
	// func for calculate loan type Flat Principal
711
	runSimulateLoan: function (formValues, AllLoanPaymentData, AllRepaymentData) {
712
		var me = this;
713
		var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
714
		store_.removeAll();
715

    
716
		jmlLoan = formValues.Amount;
717
		loanType_ = formValues.LoanType;
718
		var loanTypeData = null;
719
		Ext.Ajax.request({
720
			async: false,
721
			method: 'POST',
722
			url: '/UserControl/GetStore',
723
			params: {
724
				tableName: 'PHRBNLOANTYP',
725
				param: 'LoanType[equal]' + loanType_
726
			},
727
			success: function (response) {
728
				var results = Ext.decode(response.responseText);
729
				loanTypeData = results.data[0];
730

    
731
			}
732
		});
733
		var sdate = undefined;
734
		var edate = undefined;
735
		var tempBalance = undefined;
736
		var tempBalanceNew = undefined;
737
		var tempCurrency = undefined;
738
		var tempCurrencyNew = undefined;
739
		var balanceAfterCal = undefined;
740
		var countPayment = AllLoanPaymentData.length;
741
		if (countPayment > 0) {
742

    
743
			for (i = 0; i < countPayment; i++) {
744
				if (AllLoanPaymentData[i].PaymentType = '1000') //1000 = payment
745
				{
746
					var c = i + 1;
747
					if (i == 0) { // data index 0 , frist payment
748
						tempBalance = AllLoanPaymentData[i].Amount;
749
						tempCurrency = AllLoanPaymentData[i].Currency;
750
						if (countPayment > c) {
751
							sdate = AllLoanPaymentData[i].StartDate;
752
							edate = AllLoanPaymentData[i++].StartDate;
753
						} else {
754
							sdate = AllLoanPaymentData[i].StartDate;
755
							edate = "99991231";
756
						}
757

    
758
						//Calculate simulate loan
759
						// me.calculateFlatPrincipal(tempBalance, loanType, sdate, edate);
760
					} else { // penambahan pokok pinjaman
761

    
762
						//get balance on date range in simulation grid
763
						tempBalanceNew = lastStoreCal.balance;
764

    
765
						tempCurrencyNew = AllLoanPaymentData[i].Currency;
766
						/*
767
						if (tempCurrencyNew != tempCurrency) { // jika beda currancy harus di convert ke currancy awal
768
						// get rate currancy baru ke currancy awal @rate
769
						var rate = 100;
770
						// konversi tempBalanceNew ke currancy awal
771
						tempBalanceNew_ = tempBalanceNew * rate;
772
						tempBalance = tempBalanceNew_ + AllLoanPaymentData[i].Amount;
773
						} else {
774
						tempBalance = tempBalanceNew + AllLoanPaymentData[i].Amount;
775
						}
776
						 */
777
						tempBalance = tempBalanceNew + AllLoanPaymentData[i].Amount;
778
						var c = i + 1;
779
						if (countPayment > c) {
780
							sdate = AllLoanPaymentData[i].StartDate;
781
							edate = AllLoanPaymentData[i++].StartDate;
782
						} else {
783
							sdate = AllLoanPaymentData[i].StartDate;
784
							edate = "99991231";
785
						}
786

    
787
						//Calculate simulate loan
788
						// me.calculateFlatPrincipal(tempBalance, loanType, sdate, edate);
789
					}
790
					if (formValues.RepaymentType == '10') { //flat pricipal
791
						lastStoreCal = me.calculateFlatPrincipal(tempBalance, loanTypeData, sdate, edate, formValues);
792
					} else if (formValues.RepaymentType == '20') { //flat total
793
						lastStoreCal = me.calculateFlatTotal(tempBalance, loanTypeData, sdate, edate, formValues);
794
					}
795

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

    
800
					tempCurrencyNew = AllLoanPaymentData[i].Currency;
801
					/*
802
					if (tempCurrencyNew != tempCurrency) { // jika beda currancy harus di convert ke currancy awal
803
					// get rate ke currancy awal @rate
804
					var rate = 100;
805
					// konversi tempBalanceNew ke currancy awal
806
					tempBalanceNew_ = tempBalanceNew * rate;
807
					tempBalance = tempBalanceNew_ - AllLoanPaymentData[i].Amount;
808
					} else {
809
					tempBalance = tempBalanceNew - AllLoanPaymentData[i].Amount;
810
					}
811
					 */
812
					tempBalance = tempBalanceNew - AllLoanPaymentData[i].Amount;
813
					var c = i + 1;
814
					if (countPayment > c) {
815
						sdate = AllLoanPaymentData[i].StartDate;
816
						edate = AllLoanPaymentData[i++].StartDate;
817
					} else {
818
						//sdate = AllLoanPaymentData[i].StartDate;
819
						sdate = tempBalanceNew.StartDate;
820
						//edate = "99991231";
821
						if (sdate.substring(4, 6) == '12') {
822
							y = parseInt(sdate.substring(0, 4)) + 1;
823
							m = '01';
824
							d = MinovaUtil.GetDays(1, 2016);
825
							edate = y + m + d
826

    
827
						}
828
					}
829
					//insert store to simulate grid
830
					var store_ = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
831
					idx = store_.getCount();
832
					data = {
833
						StartDate: sdate,
834
						EndDate: edate,
835
						EmployeeID: formValues.EmployeeID,
836
						HeaderSequence: formValues.HeaderSequence,
837
						Period: '',
838
						Principal: '',
839
						Interest: '',
840
						Repayment: AllLoanPaymentData[i].Amount,
841
						Balance: tempBalance,
842
					};
843
					lastStoreCal = data;
844
					/*
845
					if (formValues.RepaymentType == '01') { //flat pricipal
846

    
847
					} else if (formValues.RepaymentType == '02') { //flat total
848

    
849
					}
850
					 */
851

    
852
				}
853
			}
854

    
855
			// set EndDate to real enddte
856
			var store_ = Ext.ComponentQuery.query('[name=gridloanpayment]')[0].getStore();
857
			idx = store_.getCount();
858
			var edate = store_.data.items[idx - 1].data.EndDate;
859
			Ext.ComponentQuery.query('[name=EndDate]')[0].setValue(edate);
860
		}
861

    
862
	},
863

    
864
	overViewDblClick: function (me_, record, item, index, e, eOpts) {
865
		me = Ext.ComponentQuery.query('[name=phrpa0032]')[0];
866
		form = Ext.ComponentQuery.query('[name=mainformLoan]')[0]; // edit proses
867
		form.setActionSubmit(1);
868
		var selection = me.getView().getSelectionModel().getSelection()[0];
869
		var form_ = form.getForm();
870
		form_.setValues(selection.data);
871

    
872
		var HeaderSequence = selection.data.HeaderSequence;
873
		var EmployeeID = selection.data.EmployeeID;
874

    
875
		var param = 'EmployeeID[equal]' + EmployeeID + ',HeaderSequence[equal]' + HeaderSequence
876
			//load loan payment data
877
			var loanpaymentStore = Ext.StoreMgr.lookup("loanpayment");
878
		Ext.Ajax.request({
879
			//method : 'POST',
880
			url: '/UserControl/GetStore',
881
			params: {
882
				tableName: 'phrpa0033',
883
				param: param
884
			},
885

    
886
			success: function (response) {
887
				var results = Ext.decode(response.responseText);
888
				loanpaymentStore.loadData(results.data);
889

    
890
			}
891
		});
892

    
893
		//load loan regular payment data
894
		var loanregpaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
895
		Ext.Ajax.request({
896
			//method : 'POST',
897
			url: '/UserControl/GetStore',
898
			params: {
899
				tableName: 'phrpa0034',
900
				param: param
901
			},
902

    
903
			success: function (response) {
904
				var results = Ext.decode(response.responseText);
905
				loanregpaymentStore.loadData(results.data);
906

    
907
			}
908
		});
909

    
910
		//load simulation ///// ---- add by Tri nwh 20211227
911
		var loanData = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
912
		Ext.Ajax.request({
913
			//method : 'POST',
914
			url: '/UserControl/GetStore',
915
			params: {
916
				tableName: 'phrpa0035',
917
				param: param
918
			},
919

    
920
			success: function (response) {
921
				var results = Ext.decode(response.responseText);
922
				loanData.loadData(results.data);
923
			}
924
		});
925
		
926
		var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
927
		card_.getLayout().setActiveItem(1);
928

    
929
		var __me = this;
930
		__me._action = "1";
931
		this.btnLoadDataLoan();
932

    
933
	},
934

    
935
	btnLoadDataLoan: function () {
936
	
937
	
938
	/*Ext.ComponentQuery.query('[name=AtasNamaKendaraan]')[0].setHidden(true);
939
	Ext.ComponentQuery.query('[name=MerekKendaraan]')[0].setHidden(true);
940
	Ext.ComponentQuery.query('[name=TypeKendaraan]')[0].setHidden(true);
941
	Ext.ComponentQuery.query('[name=TahunPembuatan]')[0].setHidden(true);
942
	Ext.ComponentQuery.query('[name=NomorRangka]')[0].setHidden(true);
943
	Ext.ComponentQuery.query('[name=NomorMesin]')[0].setHidden(true);
944
	Ext.ComponentQuery.query('[name=NomorPolisi]')[0].setHidden(true);*/ //// ---- comment by Tri nwh 20211227
945
	
946
		var loanData = Ext.ComponentQuery.query('[name=simulation]')[0].getStore();
947

    
948
		var mainForm = Ext.ComponentQuery.query('[name=formLoanMain]')[0];
949
		var formValues = mainForm.getValues();
950

    
951
		var HeaderSequence = formValues.HeaderSequence;
952
		var EmployeeID = formValues.EmployeeID;
953

    
954
		var param = 'EmployeeID[equal]' + EmployeeID + ',HeaderSequence[equal]' + HeaderSequence;
955

    
956
		Ext.Ajax.request({
957
			//method : 'POST',
958
			url: '/UserControl/GetStore',
959
			params: {
960
				tableName: 'phrpa0035',
961
				param: param
962
			},
963

    
964
			success: function (response) {
965
				var results = Ext.decode(response.responseText);
966
				loanData.loadData(results.data);
967

    
968
			}
969
		});
970
	},
971
	buttonOnClick: function (_button) {
972
		var me = this;
973
		switch (_button.name) {
974
		case 'saveLOAN':
975
			me.saveLoan();
976
			break;
977

    
978
		case 'deleteLOAN':
979
			MinovaMessage('Message', '000007', '', 'C', function (respone) {
980
				if (respone == "yes") {
981
					me.deleteLOAN();
982
				}
983
			});
984
			break;
985

    
986
		default:
987

    
988
		}
989
	},
990
	deleteLOAN: function () {
991
		var me = this;
992
		var Formheader = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
993
		if (Formheader.isValid()) {
994
			var pnl = Ext.ComponentQuery.query('[name=loanCard]')[0];
995
			pnl.mask('Loading...');
996
			Ext.Ajax.request({
997
				async: false,
998
				method: 'POST',
999
				url: '/Devt/DeleteLoanMaintenance',
1000
				params: {
1001
					header: Ext.encode(Formheader.getValues()),
1002
				},
1003
				waitMsg: 'Saving Data...',
1004
				success: function (response) {
1005

    
1006
					var result = Ext.decode(response.responseText);
1007
					if (result.success) {
1008
						var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
1009
						card_.getLayout().setActiveItem(0);
1010
						MinovaMessage('', '000006', '', 'S');
1011
						me.afterRenderOverview();
1012
						pnl.unmask();
1013
					} else {
1014
						pnl.unmask();
1015
						MinovaMessage('', '000005', result.message.text, "E")
1016
					}
1017
				},
1018
				failure: function (response) {
1019
					var result = Ext.decode(response.responseText);
1020
					pnl.unmask();
1021
					MinovaMessage('', '000005', result.message.text, "E")
1022
				}
1023
			});
1024
		}
1025
	},
1026
	saveLoan: function () {
1027
		var me = this;
1028
		var Formheader = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
1029
		var IsSubmit = 1;
1030
		var loanregrepaymentStore = Ext.StoreMgr.lookup("storeloanregularrepayment");
1031
		var loanregrepaymentData = Ext.pluck(loanregrepaymentStore.data.items, 'data');
1032
		var tenor = loanregrepaymentData[0].Tenor;
1033
		var amount = Ext.ComponentQuery.query('[name=Amount]')[0].getValue();
1034
		var loanType = Ext.ComponentQuery.query('[name=LoanType]')[0].getValue();
1035
		//var jenjangPend = Ext.ComponentQuery.query('[name=JenjangPendidikan]')[0].getValue(); ///// ---- comment by Tri nwh 20211227
1036
		var jenjangPend = '';
1037
		var maxtenor = '';
1038
		var maxplafond = '';
1039
		var seq = loanregrepaymentData[0].Sequence;
1040
		var paymenttype = loanregrepaymentData[0].PaymentType;
1041

    
1042
		MinovaUtil.ExecuteParamQuery({
1043
			ID: 'WFBNLOANMAXPLAFTENOR',
1044
			LoanType: loanType,
1045
			EmployeeID: MinovaUtil.WORKFLOW.getEmpId(),
1046
			Education: jenjangPend
1047
		}, function (s) {
1048
			var result = Ext.decode(s.responseText);
1049
			var dt = Ext.decode(Ext.decode(result.data));
1050
			if (dt.length !== 0 && dt !== null) {
1051
				maxtenor = dt[0].MaksimalTenor;
1052
				maxplafond = dt[0].MaksimalPlafond;
1053
			}
1054
		}, function (f) {});
1055
		if (parseFloat(tenor) > parseFloat(maxtenor)) {
1056
			IsSubmit = 0;
1057
			messageID = 'WFBNLOANMAXTENOR';
1058
		}
1059
		if (parseFloat(amount) > parseFloat(maxplafond)) {
1060
			IsSubmit = 0;
1061
			messageID = 'WFBNLOANMAXAMOUNT';
1062
		}
1063
		if (IsSubmit == 1) {
1064
			if (Formheader.isValid()) {
1065
				var pnl = Ext.ComponentQuery.query('[name=loanCard]')[0];
1066
				pnl.mask('Loading...');
1067
				var loanpayment = Ext.StoreMgr.lookup("loanpayment");
1068
				var loanregularrepayment = Ext.StoreMgr.lookup("storeloanregularrepayment");
1069
				var loanBalance = Ext.StoreMgr.lookup("loanBalance");
1070
				var payment = Ext.encode(Ext.pluck(loanpayment.data.items, 'data'));
1071
				var regular = Ext.encode(Ext.pluck(loanregularrepayment.data.items, 'data'));
1072
				var balance = Ext.encode(Ext.pluck(loanBalance.data.items, 'data'));
1073
				
1074
				var countrepay = (Ext.StoreMgr.lookup("storeloanregularrepayment").count());
1075
				var maxtenor = "";
1076
				var maxstdt = "";
1077
				var repaytype = "";
1078
				for (i = 0; i < countrepay; i++) {
1079
					var tenor = Ext.pluck(loanregularrepayment.data.items, 'data')[i].Tenor;
1080
					maxtenor = tenor; 
1081
					var stdt = Ext.pluck(loanregularrepayment.data.items, 'data')[i].StartDate;
1082
					maxstdt = stdt; 
1083
					repaytype = Ext.pluck(loanregularrepayment.data.items, 'data')[i].RepaymentType
1084
					var payment2000 = Ext.pluck(loanregularrepayment.data.items, 'data')[i].PaymentType;
1085
					if( payment2000 == "2000") { break; }
1086
				}
1087
				
1088
				var countpay = (Ext.StoreMgr.lookup("loanpayment").count());
1089
				var maxstdtpay = "";
1090

    
1091
				for (i = 0; i < countpay; i++) {
1092
					var stdtpay = Ext.pluck(loanpayment.data.items, 'data')[i].StartDate;
1093
					maxstdtpay = stdtpay; 
1094
					var payment2000_ = Ext.pluck(loanpayment.data.items, 'data')[i].PaymentType;
1095
					if( payment2000_ == "2000") { break; }	
1096
				}
1097

    
1098
				var countbalance = (Ext.StoreMgr.lookup("loanBalance").count());
1099
				var maxbalseq = "";
1100
				var maxbalstdt = "";
1101
				for (i = 0; i < countbalance; i++) {
1102
					var balseq = Ext.pluck(loanBalance.data.items, 'data')[i].Sequence;
1103
					maxbalseq = balseq;					
1104
					var balstdt = Ext.pluck(loanBalance.data.items, 'data')[i].StartDate;
1105
					maxbalstdt = balstdt;
1106
				}
1107

    
1108
				var errorsim = '';
1109
				if(countrepay == '1' && countpay != '2')
1110
				{ 					
1111
					if(repaytype != '20')
1112
					{ if(maxbalseq != maxtenor) { errorsim = 'Y'; console.log(repaytype); } } //else { errorsim = "N"; }
1113
				}
1114
				else if (countrepay == '2')
1115
				{
1116
					if(maxbalstdt != maxstdt) { errorsim = 'Y'; } //else { errorsim = "N"; }
1117
				}
1118
				
1119
				if (countpay == '2')
1120
				{ 
1121
					if(maxbalstdt != maxstdtpay) { errorsim = 'Y'; } //else { errorsim = "N"; }
1122
				}
1123

    
1124
				if(errorsim == 'Y')
1125
				{
1126
					MinovaMessage('', '000005', 'Can not save this data, please simulation first!', "E");
1127
					pnl.unmask();
1128
				}
1129
				else
1130
				{
1131
				
1132
					var PayrollControlStatus = '';
1133
					MinovaUtil.ExecuteParamQuery({ 
1134
						ID: 'LOANCEKPAYCNTRLSTAT',
1135
						EmployeeID: Ext.ComponentQuery.query('[name=EmployeeID]')[0].getValue(),
1136
						StartDate: Ext.ComponentQuery.query('[name=StartDate]')[0].getValue()
1137
					}, function (s) {
1138
						var result = Ext.decode(s.responseText);
1139
						var dt = Ext.decode(Ext.decode(result.data));
1140
						PayrollControlStatus = dt[0].PayrollControlStatus; 
1141
						pnl.unmask();
1142
					}); ////--- cek pay control status ? add By Tri nwh 20220419
1143
					
1144
					if(PayrollControlStatus == 'S' || PayrollControlStatus == 'S') 
1145
					{	
1146
						MinovaMessage('Not Null', 'LOAN3', '', "E");
1147
						pnl.unmask(); console.log('ssh'); ////--- cek pay control status ? add By Tri nwh 20220419
1148
					}
1149
					else
1150
					{
1151
						//saswanto 16feb2022
1152
						var LatNo = ''
1153
						MinovaUtil.ExecuteParamQuery({
1154
							ID: 'GetLoanNumbering',
1155
							StartDate: stdtpay
1156
						}, function (s) {
1157
							try {
1158
								var result = Ext.decode(s.responseText).data;
1159
								var dataGrid = Ext.decode(Ext.decode(result));
1160
								if (dataGrid.length > 0) {
1161
									LatNo = dataGrid[0].Column1
1162
								}
1163
							} catch (e) {}
1164
						}, function (e) {});
1165
				
1166
						Ext.ComponentQuery.query('[name=AgreementNumber]')[0].setValue(LatNo);
1167
		
1168
						Ext.Ajax.request({
1169
							async: false,
1170
							method: 'POST',
1171
							url: '/Devt/SaveLoanMaintenance',
1172
							params: {
1173
								header: Ext.encode(Formheader.getValues()),
1174
								payment: payment,
1175
								regular: regular,
1176
								balance: balance,
1177
								action: this._action
1178
							},
1179
							waitMsg: 'Saving Data...',
1180
							success: function (response) {
1181

    
1182
								var result = Ext.decode(response.responseText);
1183
								if (result.success) {
1184
									var card_ = Ext.ComponentQuery.query('[name=loanCard]')[0];
1185
									card_.getLayout().setActiveItem(0);
1186
									MinovaMessage('', '000006', '', 'S');
1187
									me.afterRenderOverview();
1188
									pnl.unmask();
1189
									//saswanto 16feb2022
1190
									MinovaUtil.ExecuteParamQuery({
1191
										ID: 'SetLoanNumbering',
1192
										StartDate: stdtpay
1193
									}, function (s) {
1194
										try {
1195
											var result = Ext.decode(s.responseText).data;
1196
											var dataGrid = Ext.decode(Ext.decode(result));
1197
											if (dataGrid.length > 0) {
1198
												LatNo = dataGrid[0].Column1
1199
											}
1200
										} catch (e) {}
1201
									}, function (e) {});
1202
						
1203
								} else {
1204
									pnl.unmask();
1205
									//MinovaMessage('', result.message.id, result.message.text, "E");
1206
									MinovaMessage('', '000005', result.message.text, "E")
1207
								}
1208
							},
1209
							failure: function (response) {
1210
								var result = Ext.decode(response.responseText);
1211
								pnl.unmask();
1212
								// MinovaMessage('', result.message.id, result.message.text, "E")
1213
								MinovaMessage('', '000005', result.message.text, "E")
1214
							}
1215
						});
1216
					}
1217
				}				
1218
			} else {
1219
				var param_ = me.cekMandatoryIsNull();
1220
				if (param_ != "") {
1221
					MinovaMessage('Not Null', '000009', param_, 'E');
1222
				} else {
1223
					// ada field yang error
1224
					var error_ = me.getErrorFields();
1225
					if (error_ != '') {
1226
						MinovaMessage('title', '000005', error_, 'E');
1227
					}
1228

    
1229
				}
1230
			}
1231
		} else {
1232
			MinovaMessageError("Error", messageID, "");
1233
		}
1234
	},
1235
	cekMandatoryIsNull: function () {
1236
		var fields_ = Ext.ComponentQuery.query('[wasValid=false][allowBlank=false]');
1237
		var param_ = '';
1238
		if (fields_.length > 0) {
1239
			for (var i = 0; i < fields_.length; i++) {
1240
				if (i == 0) {
1241
					param_ = ':' + fields_[i].fieldLabel
1242
				} else {
1243
					param_ = param_ + ',' + fields_[i].fieldLabel;
1244
				}
1245

    
1246
			}
1247
		}
1248
		return param_
1249
	},
1250
	getErrorFields: function () {
1251
		var fields_ = Ext.ComponentQuery.query('[wasValid=false]');
1252
		var param_ = '';
1253
		if (fields_.length > 0) {
1254
			for (var i = 0; i < fields_.length; i++) {
1255
				if (i == 0) {
1256
					param_ = ':' + fields_[i].fieldLabel;
1257
				} else {
1258
					param_ = param_ + ',' + fields_[i].fieldLabel;
1259
				}
1260

    
1261
			}
1262
		}
1263
		return param_
1264
	},
1265
	setKeyDisabled: function (param) {
1266
		var frmEdit = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
1267
		var fields_ = frmEdit.getFields().items;
1268
		for (var i = 0; i < fields_.length; i++) {
1269
			var field_ = frmEdit.getFields().items[i];
1270
			iskey_ = field_.IsPrimaryKey;
1271
			if (iskey_ == '1') {
1272
				field_.setReadOnly(param);
1273
			}
1274
		}
1275
	},
1276
	hitungTenor: function (editor, e) {
1277
		//alert("HITUNG TENOR");
1278
		var main_ = Ext.getCmp('gridloanregularrepayment');
1279
		var record = main_.getSelectionModel().getSelection()[0];
1280
		if (record.data.Tenor != '0' || record.data.Amount != '0') {
1281
			var loanpayment = Ext.StoreMgr.lookup("loanpayment");
1282
			var payment = Ext.pluck(loanpayment.data.items, 'data');
1283
			var countPayment = payment.length;
1284
			var Balance = 0;
1285
			if (countPayment > 0) {
1286
				for (i = 0; i < countPayment; i++) {
1287
					Balance += parseFloat(payment[i].Amount);
1288
				}
1289
			}
1290
			var Data = 0;
1291
			if (record.data.Amount == "0")
1292
				record.data.Amount = Math.round(Balance / parseFloat(record.data.Tenor) * 100) / 100;
1293
			else if (record.data.Tenor == "0")
1294
				record.data.Tenor = Math.round(Balance / parseFloat(record.data.Amount) * 100) / 100;
1295
			main_.getView().refresh();
1296
		}
1297

    
1298
		//edit amount
1299
		var Interest = 0;
1300
		var Tenor = 0;
1301
		var Koefisien = 0;
1302
		var amount1 = parseFloat(Ext.ComponentQuery.query('[name=Amount]')[0].getValue());
1303
		console.log("amount1=" + amount1);
1304
		var hasil = 0;
1305
		Ext.Ajax.request({
1306
			async: false,
1307
			method: 'POST',
1308
			url: '/UserControl/GetStore',
1309
			params: {
1310
				tableName: 'PHRBNLOANTYP',
1311
				param: 'LoanType[equal]' + Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm().getValues().LoanType + ',Tenor[equal]' + record.data.Tenor
1312
			},
1313
			success: function (response) {
1314
				results = Ext.decode(response.responseText);
1315
				Interest = record.data.RateofInterest;
1316
				console.log("Interest=" + Interest);
1317
				Tenor = record.data.Tenor;
1318
				console.log("Tenor=" + Tenor);
1319
				Koefisien = parseFloat(results.data[0].Koefisien);
1320
				console.log("Koefisien=" + Koefisien);
1321
				/* hasil = amount1 * (1 + Koefisien) / Tenor; */
1322
				hasil = ((amount1 * ((Math.pow((1 + (Interest / 100 / 12)), Tenor)) * (Interest / 100 / 12)) / ((Math.pow((1 + (Interest / 100 / 12)), Tenor)) - 1)));
1323
				console.log("hasil=" + hasil);
1324
				record.set('Amount', hasil);
1325
			}
1326
		});
1327
	},
1328
	addPayment: function () {
1329
		var store = Ext.StoreMgr.lookup('loanpayment');
1330
		idx = store.getCount();
1331
		var action = getParam('action');
1332
		
1333
		var amount = Ext.ComponentQuery.query('[name=Amount]')[0].getValue();
1334
		var data = {
1335
			StartDate: Ext.ComponentQuery.query('[name=StartDate]')[0].getValue(),
1336
			EndDate: Ext.ComponentQuery.query('[name=EndDate]')[0].getValue(),
1337
			Currency: 'IDR',
1338
			PaymentType: '1000',
1339
			Amount: amount,
1340
			EmployeeID:'',
1341
			HeaderSequence:'',
1342
			Sequence:'',
1343
			UID:'',
1344
			Notes:''
1345
			
1346
		}
1347
		store.insert(idx, data);
1348
	},
1349
	deletePayment: function () {
1350
		var me = this,
1351
		store = Ext.StoreMgr.lookup('loanpayment')
1352
			var grid = Ext.getCmp('gridloanpayment');
1353
			
1354
		Ext.MessageBox.show({
1355
			title: 'Remove tab',
1356
			msg: "This will remove. Do you want to continue?",
1357
			buttons: Ext.MessageBox.YESNO,
1358
			fn: function (choice) {
1359
				console.log(choice);
1360
				if (choice === 'yes') {
1361
					var selection = grid.getView().getSelectionModel().getSelection()[0];
1362
					if (selection) {
1363
						store.remove(selection);
1364
					}
1365
				}
1366
			}
1367
		});
1368
	},
1369
	addRepayment: function () {
1370
		var store = Ext.StoreMgr.lookup('storeloanregularrepayment');
1371
		idx = store.getCount();
1372
		var action = getParam('action');
1373
		var Sequence = 0;
1374
		
1375
		if (idx == 0) {
1376
			Sequence = 1;
1377
		} else {
1378
			Sequence = 1 + idx;
1379
		}
1380
		var Interest = 0;
1381
		var Tenor = 0;
1382
		var Koefisien = 0;
1383
		var amount1 = parseFloat(Ext.ComponentQuery.query('[name=Amount]')[0].getValue());
1384
		console.log("amount1=" + amount1);
1385
		var hasil = 0;
1386
		Ext.Ajax.request({
1387
			async: false,
1388
			method: 'POST',
1389
			url: '/UserControl/GetStore',
1390
			params: {
1391
				tableName: 'PHRBNLOANTYP',
1392
				param: 'LoanType[equal]' + Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm().getValues().LoanType
1393
			},
1394
			success: function (response) {
1395
				results = Ext.decode(response.responseText);
1396
				Interest = parseFloat(results.data[0].Interest);
1397
				Tenor = parseFloat(results.data[0].Tenor);
1398
				console.log("Tenor=" + Tenor);
1399
				Koefisien = parseFloat(results.data[0].Koefisien);
1400
				console.log("Koefisien=" + Koefisien);
1401
				hasil = amount1 * (1 + Koefisien) / Tenor;
1402
				console.log("hasil=" + hasil);
1403
			}
1404
		});
1405
		var data = {
1406
			StartDate: Ext.ComponentQuery.query('[name=StartDate]')[0].getValue(),
1407
			EndDate: Ext.ComponentQuery.query('[name=EndDate]')[0].getValue(),
1408
			//Sequence: Sequence,
1409
			RateofInterest: Interest,
1410
			Currency: 'IDR',
1411
			PaymentType: '1000',
1412
			RepaymentType: '20',
1413
			Tenor: 0,
1414
			Amount: 0
1415
		}
1416
		store.insert(idx, data);
1417
	},
1418

    
1419
	deleteRepayment: function () {
1420
		var me = this,
1421
		store = Ext.StoreMgr.lookup('storeloanregularrepayment')
1422

    
1423
			var grid = Ext.getCmp('gridloanregularrepayment');
1424

    
1425
		Ext.MessageBox.show({
1426
			title: 'Remove tab',
1427
			msg: "This will remove. Do you want to continue?",
1428
			buttons: Ext.MessageBox.YESNO,
1429
			fn: function (choice) {
1430
				console.log(choice);
1431
				if (choice === 'yes') {
1432
					var selection = grid.getView().getSelectionModel().getSelection()[0];
1433
					if (selection) {
1434
						store.remove(selection);
1435
					}
1436
				}
1437
				//delete panel.pendingClose;
1438
			}
1439
		});
1440
	},
1441
	NewSimulateLoan: function () {
1442
		var Formheader = Ext.ComponentQuery.query('[name=mainformLoan]')[0].getForm();
1443
		var loanpayment = Ext.StoreMgr.lookup("loanpayment");
1444
		var loanregularrepayment = Ext.StoreMgr.lookup("storeloanregularrepayment");
1445
		var params = {
1446
			serviceName: 'HRLoan.asmx',
1447
			methodName: 'SimulateLoan',
1448
			parameter:
1449
			'dataHeader=' + Ext.encode(Formheader.getValues())
1450
			 + '&dataPayment=' + Ext.encode(Ext.pluck(loanpayment.data.items, 'data'))
1451
			 + '&dataRepayment=' + Ext.encode(Ext.pluck(loanregularrepayment.data.items, 'data'))
1452
		};
1453
		MinovaAjaxPost('/InvokeService/Invoke/', params, function (xhr) {
1454
			result = Ext.decode(xhr.responseText);
1455
			var loanBalance = Ext.StoreMgr.lookup("loanBalance");
1456
			loanBalance.removeAll();
1457
			loanBalance.add(Ext.decode(result.data))
1458
		});
1459
	},
1460
	mapComboTenor: function () {
1461
		var loanType = Ext.ComponentQuery.query('[name=LoanType]')[0].getValue();
1462
		var storeTenor = Ext.StoreMgr.lookup("store_phrpa0034Tenor");
1463

    
1464
		storeTenor.proxy.extraParams = {
1465
			tableName: 'PDSBNLOANTENOR',
1466
			param: loanType
1467
		};
1468
		storeTenor.removeAll();
1469
		storeTenor.reload();
1470
	},
1471
	specialRep: function () {
1472
		//var paymentType = Ext.ComponentQuery.query('[id=phrpa0034PaymentType]')[0].getValue();
1473
		//var StartDate = Ext.ComponentQuery.query('[id=phrpa0034StartDate]')[0].getValue();
1474
		var paymentType = Ext.ComponentQuery.query('[id=phrpa0033PaymentType]')[0].getValue();
1475
		var repaymentType = Ext.ComponentQuery.query('[id=phrpa0034PaymentType]')[0].getValue();
1476
		var EmployeeID = getParam('EmployeeID');
1477
		var HeaderSequence = Ext.ComponentQuery.query('[name=HeaderSequence]')[0].getValue();
1478
		var LoanType = Ext.ComponentQuery.query('[name=LoanType]')[0].getValue();
1479

    
1480
		if (repaymentType == '2000') {
1481
			var StartDate = Ext.ComponentQuery.query('[id=phrpa0034StartDate]')[0].getValue();
1482
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].allowBlank = true;
1483
			Ext.ComponentQuery.query('[id=phrpa0034Amount]')[0].setReadOnly(true);
1484
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].validate();
1485
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].setReadOnly(true);
1486
			Ext.ComponentQuery.query('[id=phrpa0034RateofInterest]')[0].setValue('0');	
1487
			Ext.ComponentQuery.query('[id=phrpa0034RateofInterest]')[0].setReadOnly(true);
1488
			var Amount = 0;
1489
			
1490
			Ext.Ajax.request({
1491
			async: false,
1492
			method: 'POST',
1493
			url: '/UserControl/GetStore',
1494
			params: {
1495
				tableName: 'PDSLOANLASTBALANCEPELUNASAN',
1496
				param: EmployeeID + ',' + StartDate + ',' + HeaderSequence + ',' + LoanType
1497
			},
1498
			success: function (response) {
1499
				var results = Ext.decode(response.responseText);
1500
				var dt = results.data;
1501
				data_ = dt[0];
1502
				Amount = parseFloat(data_.LastBalance);
1503
			}
1504
			});
1505
			
1506
			Ext.ComponentQuery.query('[id=phrpa0034Amount]')[0].setValue(Amount);
1507
			
1508
		} else {
1509
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].allowBlank = false;
1510
			Ext.ComponentQuery.query('[id=phrpa0034Amount]')[0].setReadOnly(true);
1511
			Ext.ComponentQuery.query('[id=phrpa0034Tenor]')[0].validate();
1512
		}
1513
		
1514
		if (paymentType == '2000') {
1515
			var StartDate = Ext.ComponentQuery.query('[id=phrpa0033StartDate]')[0].getValue();
1516
			Ext.ComponentQuery.query('[id=phrpa0034Amount]')[0].setReadOnly(true);
1517
			var Amount = 0;
1518

    
1519
			Ext.Ajax.request({
1520
			async: false,
1521
			method: 'POST',
1522
			url: '/UserControl/GetStore',
1523
			params: {
1524
				tableName: 'PDSLOANLASTBALANCEPELUNASAN',
1525
				param: EmployeeID + ',' + StartDate + ',' + HeaderSequence + ',' + LoanType
1526
			},
1527
			success: function (response) {
1528
				var results = Ext.decode(response.responseText);
1529
				var dt = results.data;
1530
				data_ = dt[0];
1531
				Amount = parseFloat(data_.LastBalance);
1532
			}
1533
			});
1534
			
1535
			Ext.ComponentQuery.query('[id=phrpa0033Amount]')[0].setValue(Amount);
1536
			
1537
		} else {
1538
			Ext.ComponentQuery.query('[id=phrpa0033Amount]')[0].setReadOnly(true);
1539
		}
1540
	}
1541
});
(4-4/4)