Project

General

Profile

Bug #3106 » costpattern.js

Tri Rizqiaty, 10/14/2024 09:44 AM

 
1
Ext.define('MinovaES.controller.orm.ctrlCustom.training.costpattern', {
2
	extend: 'Ext.app.ViewController',
3
	alias: 'controller.costpattern',
4
	ObjectID: undefined,
5
	init: function () {
6
		this.control({
7
			'[name=mainCostpattern]': {
8
                afterrender: this.afterrenderCostPattern
9
            },			
10
			'[name=saveCP]': {
11
				click: this.save
12
			},
13
			'[action=addEmp]': {
14
				click: this.addGrid
15
			},
16
			'[action=deleteData]': {
17
				click: this.deleteGrid
18
			},
19
			'[name=PHROM0033]': {
20
				afterrender: this.afterrenderGrid
21
			},
22
			'[name=PDSTRCOSTITEMSUM]': {
23
				afterrender: this.afterrenderGridSum
24
			},
25
			'[name=gridPHROM0033]': {
26
				edit: this.editGridCostPattern
27
			},
28
			'[id=PHROM0033CalculateAttendance]': {
29
                change: this.calAtt
30
            },
31
			'[id=PHROM0033CalculateDay]': {
32
				change: this.calDay
33
			}
34
		});
35
	},
36
	afterrenderGrid: function(){
37
		var me = this;
38
		var overView = Ext.StoreMgr.lookup("storePHROM0033");
39
        overView.proxy.url = '/UserControl/GetStore';
40
        var pnlORM = Ext.ComponentQuery.query('[name=mainORM]')[0];
41
        var obj = pnlORM.getRecordSelect();
42
        var objid = obj.data.ObjectId;
43
		var objclass = obj.data.ObjectType;
44
        overView.proxy.extraParams = {
45
            tableName: 'PHROM0033',
46
            param: 'ObjectID[=]' + objid + ',ObjectClass[equal]' + objclass
47
        };
48
        overView.removeAll();
49
        overView.reload();
50
        overView.loadPage(1);		
51
	},
52
	afterrenderGridSum: function(){		
53
		var me = this;		
54
		var pnlORM = Ext.ComponentQuery.query('[name=mainORM]')[0];
55
		var gridSum = Ext.ComponentQuery.query('[name=PDSTRCOSTITEMSUM]')[0];
56
		var strSum = Ext.StoreMgr.lookup("PDSTRCOSTITEMSUM");
57
        var obj = pnlORM.getRecordSelect();
58
        var objid = obj.data.ObjectId;
59
		var objclass = obj.data.ObjectType;
60
		var total = 0;
61
		var totalCost = 0;
62
		Ext.Ajax.request({
63
			async: false,
64
			method: 'POST',
65
			url: '/UserControl/GetStore',
66
			params: {
67
				tableName: 'PHROM0033',
68
				param: 'ObjectID[equal]' + objid + ',ObjectClass[equal]' + objclass
69
			},
70
			success: function (response) {
71
				var results = Ext.decode(response.responseText);
72
		   		var dt = results.data;
73
				for(var i = 0; i<dt.length; i++)
74
                {
75
					total = parseFloat(dt[i].Total);
76
					var cur = dt[i].Currency;
77
					var strSum = gridSum.store;
78
					var idx = strSum.findExact('Currency', cur)
79
					if(idx >= 0)
80
					{
81
						var recsum = strSum.findRecord('Currency', cur)
82
						totalCost = total + parseFloat(recsum.data.Amount);
83
						recsum.data.Amount = totalCost;
84
						gridSum.view.refresh();
85
					}
86
					else
87
					{
88
						strSum.add({
89
							Currency: cur,
90
							Amount: total
91
						});
92
						gridSum.view.refresh();
93
					}
94
				}
95
			}
96
		});
97
		var overView = Ext.StoreMgr.lookup("storePHROM0033");
98
		overView.reload();
99
	},
100
	afterrenderCostPattern: function(){
101
		var me = this;
102
		var pnl = Ext.ComponentQuery.query('[name=PanelOrmMdDetail]')[0];
103
		pnl.setHeight(600);		
104
	},
105
	addGrid: function(){
106
		var store = Ext.StoreMgr.lookup('storePHROM0033');
107
		var pnlORM = Ext.ComponentQuery.query('[name=mainORM]')[0];
108
        var obj = pnlORM.getRecordSelect();
109
		var objid = obj.data.ObjectId;           
110
		var objClass = obj.data.ObjectType;		
111
		idx = store.getCount();
112
		var Sequence = 0;
113
		if (idx == 0) {
114
			Sequence = 1;
115
		} else {
116
			Sequence = 1 + idx;
117
		}		
118
		var seq = 'Sequence';
119
		var SequenceValue = Sequence;		
120
		data = {
121
			Currency: '',
122
			ObjectClass: objClass,
123
			CostType: '',
124
			DayNumber: '',
125
			CalculateEvent: '',
126
			CalculateDay: '',
127
			CalculateAttendance: '',
128
			AttendanceNumber: '',
129
			Notes: '',
130
			Total: '0',
131
			UnitPrice: '0',
132
			ObjectID: objid,
133
			StartDate: obj.data.StartDate,
134
			EndDate: obj.data.EndDate,
135
			CreateDate: '',
136
			CreateBy: '',
137
			ChangeDate: '',
138
			ChangeBy: '',
139
			Action: 0			
140
		};
141
		data[seq] = SequenceValue;
142
		store.insert(idx, data);
143
	},
144
	calAtt: function(){		
145
		var attVal = Ext.ComponentQuery.query('[id=PHROM0033CalculateAttendance]')[0];
146
		var obj = Ext.ComponentQuery.query('[id=PHROM0033ObjectID]')[0];
147
		var objClass = Ext.ComponentQuery.query('[id=PHROM0033ObjectClass]')[0];
148
		var q = Ext.ComponentQuery.query('[id=PHROM0033AttendanceNumber]')[0];
149
		if(attVal.getValue() == "1")
150
		{
151
			var tablename = "";			
152
			if(objClass.getValue() == "DT") 
153
			{ 
154
				tablename = "PHROM0031";
155
			} else if(objClass.getValue() == "DE") 
156
			{
157
				tablename = "PHROM0036";
158
			}		
159
			Ext.Ajax.request({
160
				async: false,
161
				method: 'POST',
162
				url: '/UserControl/GetStore',
163
				params: {
164
					tableName: tablename,
165
					param: 'ObjectID[equal]' + obj.getValue() + ',ObjectClass[equal]' + objClass.getValue()
166
				},
167
				success: function (response) {
168
					var results = Ext.decode(response.responseText);
169
					var attNumb = results.totalRecords;
170
					q.setValue(attNumb);
171
				}
172
			});
173
		} else if(attVal.getValue() == "0")
174
		{
175
			q.setValue(0);
176
		}			
177
	},
178
	calDay: function(){
179
		var attVal = Ext.ComponentQuery.query('[id=PHROM0033CalculateDay]')[0];
180
		var obj = Ext.ComponentQuery.query('[id=PHROM0033ObjectID]')[0];
181
		var objClass = Ext.ComponentQuery.query('[id=PHROM0033ObjectClass]')[0];
182
		var q = Ext.ComponentQuery.query('[id=PHROM0033DayNumber]')[0];
183
		if(attVal.getValue() == "1")
184
		{
185
			var tablename = "";			
186
			if(objClass.getValue() == "DT") 
187
			{ 
188
				tablename = "PHROM0028";
189
			} else if(objClass.getValue() == "DE") 
190
			{
191
				tablename = "PHROM0035";
192
			}		
193
			Ext.Ajax.request({
194
				async: false,
195
				method: 'POST',
196
				url: '/UserControl/GetStore',
197
				params: {
198
					tableName: tablename,
199
					param: 'ObjectID[equal]' + obj.getValue() + ',ObjectClass[equal]' + objClass.getValue()
200
				},
201
				success: function (response) {
202
					var results = Ext.decode(response.responseText);
203
					var attNumb = results.data;					
204
					var dur = 0;
205
					Ext.each(attNumb, function(d) {						
206
						dur = dur + parseFloat(d.Duration);	
207
					});
208
					q.setValue(dur);
209
				}
210
			});
211
		} else if(attVal.getValue() == "0")
212
		{
213
			q.setValue(0);
214
		}	
215
	},
216
	deleteGrid: function()
217
	{
218
		var me = this;
219
		var grid = Ext.getCmp('gridPHROM0033');		
220
        var selection = grid.getView().getSelectionModel()
221
        var dt = selection.getSelection()[0];
222
        if (dt) {
223
            Ext.MessageBox.show({
224
                title: 'Remove tab',
225
                msg: MinovaMessageData('000007', ''),
226
                buttons: Ext.MessageBox.YESNO,
227
                fn: function (choice) {                    
228
                    if (choice === 'yes') {						
229
                        var data = Ext.pluck(selection.selected.items, 'data');
230
						var action = undefined;
231
						$.grep(data, function (r) {
232
						   action = r.Action;
233
						})
234
						if(action == undefined)
235
						{
236
							Ext.Ajax.request({
237
								async: false,
238
								method: 'POST',
239
								url: '/Devt/SaveTable',
240
								params: {
241
									tableName: 'PHROM0033',
242
									data: Ext.encode(data),
243
									action: 2                                
244
								},
245
								waitMsg: 'Saving Data...',
246
								success: function (response) {
247
									var result = Ext.decode(response.responseText);
248
									if (result.success) {
249
										MinovaMessage('', 'BSDEL', '', 'S');
250
										var storeGrid = Ext.StoreMgr.lookup("storePHROM0033");
251
										/*storeGrid.reload();*/
252
										storeGrid.remove(dt);										
253
									}
254
									else {
255
										pnl.unmask();
256
										MinovaMessage('', '000005', result.message.text, "E")
257
									}
258
								},
259
								failure: function (response) {
260
									var result = Ext.decode(response.responseText);
261
									MinovaMessage('', '000005', result, "E")
262
									
263
								}
264
							});
265
						} else if (action == 0)
266
						{
267
							var storeGrid = Ext.StoreMgr.lookup("storePHROM0033");							
268
							storeGrid.remove(dt);
269
							MinovaMessage('', 'BSDEL', '', 'S');
270
						}	
271
						var grid33 = Ext.ComponentQuery.query('[name=gridPHROM0033]')[0];		
272
						var gridSum = Ext.ComponentQuery.query('[name=PDSTRCOSTITEMSUM]')[0];							
273
						me.loadSummariesCost(grid33, gridSum);
274
                    }                    
275
                }
276
            });
277
        }				
278
	},
279
	editGridCostPattern: function(editor, e) {	
280
		var me = this;	
281
		var rec = e.record;
282
		var curency = rec.data.Currency;
283
		var calAtt = rec.data.CalculateAttendance;
284
		var calEvent = rec.data.CalculateEvent;
285
		var calDay = rec.data.CalculateDay;
286
		var unitPrice = rec.data.UnitPrice;
287
		var attNumb = rec.data.AttendanceNumber;
288
		var dayNumb = rec.data.DayNumber;
289
		if(curency == null || 
290
		   curency == undefined || 
291
		   curency == "")
292
		{
293
			alert("Currency is mandatory field ");
294
			return;
295
		}		
296
		if(calAtt == "1" && calDay == "1")
297
		{
298
			rec.data.Total = parseFloat(unitPrice) * parseFloat(attNumb) * parseFloat(dayNumb);
299
		} else if(calAtt == "1")
300
		{
301
			rec.data.Total = parseFloat(unitPrice) * parseFloat(attNumb);
302
		} else if(calDay == "1")
303
		{
304
			rec.data.Total = parseFloat(unitPrice) * parseFloat(dayNumb);
305
		} else if(calAtt == "0" && calDay == "0" && calEvent == "1")
306
		{
307
			rec.data.Total = parseFloat(unitPrice);
308
		} else 
309
		{
310
			rec.data.Total = parseFloat(unitPrice) * 1;
311
		}
312
		var grid = Ext.ComponentQuery.query('[name=gridPHROM0033]')[0];
313
		var gridSum = Ext.ComponentQuery.query('[name=PDSTRCOSTITEMSUM]')[0];						
314
		grid.view.refresh();
315
		me.loadSummariesCost(grid, gridSum);			
316
	},
317
	loadSummariesCost: function(grid, gridSum)
318
	{		
319
		var total = 0;
320
		var totalCost = 0;
321
		gridSum.store.removeAll();
322
		if(grid.store.count() == 0) gridSum.store.removeAll();
323
		Ext.each(grid.store.getRange(), function(r) {
324
			var cur = r.data.Currency;
325
			total = parseFloat(r.data.Total);
326
			var strSum = gridSum.store;
327
			var idx = strSum.findExact('Currency', cur)
328
			if(idx >= 0)
329
			{
330
				var recsum = strSum.findRecord('Currency', cur)
331
				totalCost = total + parseFloat(recsum.data.Amount);
332
				recsum.data.Amount = totalCost;
333
				gridSum.view.refresh();
334
			}
335
			else
336
			{
337
				strSum.add({
338
					Currency: cur,
339
					Amount: total
340
				});
341
				gridSum.view.refresh();
342
			}
343
		});		
344
	},
345
	save: function()
346
	{
347
		var pnl = Ext.ComponentQuery.query('[name=PHROM0033]')[0];
348
        pnl.mask('Loading...');
349
        var storeGrid = Ext.StoreMgr.lookup("storePHROM0033");
350
        var storeCount_ = storeGrid.getCount();
351
        if (storeCount_ > 0) {            
352
            var storeGrid = Ext.StoreMgr.lookup("storePHROM0033");
353
            var dt = Ext.pluck(storeGrid.data.items, 'data');
354
            dataAdd = $.grep(dt, function (r) {
355
                return r.Action == '0'
356
            });
357
            dataEdit = $.grep(dt, function (r) {
358
                return r.Action != '0'
359
            });
360
            Ext.Ajax.request({
361
                async: false,
362
                method: 'POST',
363
                url: '/Devt/SaveEditTable',
364
                params: {
365
                    tableName: 'PHROM0033',
366
                    dataAdd: Ext.encode(dataAdd),
367
                    dataEdit: Ext.encode(dataEdit)
368
                },
369
                waitMsg: 'Saving Data...',
370
                success: function (response) {
371
                    var result = Ext.decode(response.responseText);
372
                    if (result.success) {
373
                        MinovaMessage('', '000006', '', 'S');
374
                        storeGrid.reload();
375
                    }
376
                    else {
377
                        pnl.unmask();
378
                        MinovaMessage('', '000005', result.message.text, "E")
379
                    }
380
                },
381
                failure: function (response) {
382
                    var result = Ext.decode(response.responseText);
383
                    MinovaMessage('', '000005', result, "E")
384
                }
385
            });
386
        }
387
        pnl.unmask();
388
	}
389
});
(4-4/13)