text changes to registration mail content
[namibia] / public / js / app / admin-workspace / groups.js
1 ;(function(){
2
3         window._w.groups = {
4
5                 ti : null,
6                 itemId : null,
7
8                 onTemplateReady : function( template, event, eventData )
9                 {
10                         _w.groups.ti = _t[template];
11                         _w.groups.initGrid();
12
13                         App.DataStore.setData('pricingTypes', [
14                            {"value":"Incremental", "label":"Incremental"},
15                            {"value":"Fixed", "label":"Fixed"}
16                            ]);
17                         App.DataStore.setData('priceGuideYesNo', [
18                   {"value":"0", "label":"No"},
19                   {"value":"1", "label":"Yes"}
20                   ]);
21
22                         // Group grid
23                         if (null == App.API.taskContract('gridGroup'))
24                         {
25                                 App.API.getTask(
26                                                 'gridGroup', 'Company', 'Group.List', null,
27                                                 {}, _w.groups.loadGroupGrid, _w.taskContractError
28                                                 );
29                         }
30                         else
31                         {
32                                 _w.groups.loadGroupGrid( null, {}, {} );
33                         }
34                         if (null == App.API.taskContract('addGroup'))
35                         {
36                                 App.API.getTask(
37                                                 'addGroup', 'Company', 'Group.Create', null,
38                                                 {}, function () {}, _w.taskContractError
39                                                 );
40                         }
41                 },
42
43                 onTemplatePublished : function( template, event, eventData )
44                 {
45                         _w.groups.ti.hydrateParam('name', {
46                                 type  : 'input',
47                                 id    : 'name',
48                                 title : 'Name',
49                                 value : ''
50                         });
51                         _w.groups.ti.hydrateParam('pricingType', {
52                                 type  : 'select',
53                                 id    : 'pricingType',
54                                 title : 'Pricing Type',
55                                 value : '',
56                                 dataStoreId : 'pricingTypes'
57                         });
58                         _w.groups.ti.hydrateParam('fixedPricing', {
59                                 type  : 'input',
60                                 id    : 'fixedPricing',
61                                 title : 'Fixed Pricing',
62                                 value : ''
63                         });
64                         _w.groups.ti.hydrateParam('priceGuide', {
65                                 type  : 'select',
66                                 id    : 'priceGuide',
67                                 title : 'Price Guide',
68                                 value : '',
69                                 dataStoreId : 'priceGuideYesNo'
70                         });
71                         _w.groups._setupValidation();
72                         $('#btnNew').click(function() {
73                                 _w.groups.itemId = null;
74                                 $('#name').val('');
75                                 $('#pricingType').selectpicker('val', '');
76                                 $('#fixedPricing').val('0');
77                                 $('#priceGuide').selectpicker('val', '');
78                                 $('label.error').remove();
79                                 $('#btnSave').html(' Add ');
80                         });
81                         $('#btnNew').prop('disabled', false);
82                 },
83
84                 initGrid : function()
85                 {
86
87                         _w.groups.ti.hydrate({
88                                 // Group grid
89                                 'gridGroupTitle': 'Manage groups',
90                                 'gridGroupColumnHeaders': {
91                                         constructor: 'GridColumnHeader',
92                                         items: {
93                                                 name: {
94                                                         id: 'gridGroupColumn_name',
95                                                         title: 'NAME'
96                                                 },
97                                                 pricingType: {
98                                                         id: 'gridGroupColumn_pricingType',
99                                                         title: 'PRICING TYPE'
100                                                 },
101                                                 fixedPricing: {
102                                                         id: 'gridGroupColumn_fixedPricing',
103                                                         title: 'FIXED PRICE'
104                                                 },
105                                                 priceGuide: {
106                                                         id: 'gridGroupColumn_priceGuide',
107                                                         title: 'PRICE GUIDE'
108                                                 }
109                                         }
110                                 },
111                                 'gridGroupRowRepeater': {}
112                         });
113
114                 },
115
116                 loadGroupGrid : function( contract, data, options )
117                 {
118                         App.API.execTask(
119                                         'gridGroup', data, options,
120                                         _w.groups._onGroupGridDataReceived, _w.taskExecError
121                                         );
122                 },
123
124                 _onGroupGridDataReceived : function( response )
125                 {
126                         _w.groups.ti.hydrateParam('gridGroupRowRepeater', {});
127                         var gridData = [];
128                         for (var i = 0; i < response.Data.length; i++)
129                         {
130                                 var row = response.Data[i];
131                                 gridData.push({
132                                         prepend: {
133                                                 Amount  : 'R '
134                                         },
135                                         rowId : row.id,
136                                         onClick : function(meta) {
137                                                 _w.groups.itemId = meta.rowId;
138                                                 $('#name').val(meta.items.name);
139                                                 $('#pricingType').selectpicker('val', meta.items.pricingType);
140                                                 $('#fixedPricing').val(meta.items.fixedPricing);
141                                                 $('#priceGuide').selectpicker('val', 'Yes' == meta.items.priceGuide ? '1' : '0');
142                                                 $('#btnSave').html(' Update ');
143                                         },
144                                         items: {
145                                                 'id'                    : row.id,
146                                                 'name'                  : row.name,
147                                                 'pricingType'   : row.pricingType,
148                                                 'fixedPricing'  : row.fixedPricing ? row.fixedPricing : '0',
149                                                 'priceGuide'    : row.priceGuide ? 'Yes' : 'No'
150                                         }
151                                 });
152                         }
153                         _w.groups.ti.hydrateParam('gridGroupRowRepeater', {
154                                 constructor : 'GridDataRow',
155                                 items       : gridData
156                         });
157                 },
158
159                 _setupValidation : function ()
160                 {
161                         $('#frmGroupView').validate({
162                                 "rules": {
163                                         name                    : "required",
164                                         pricingType     : "required",
165                                         fixedPricing    : {digits: true, required: true},
166                                         priceGuide              : "required"
167                                 },
168                                 messages: {
169                                         name            : "This field is required.",
170                                         pricingType : "This field is required.",
171                                         priceGuide      : "This field is required."
172                                 },
173                                 submitHandler: function( form ) {
174                                         $('#btnSave').prop('disabled', true);
175                                         var data = {
176                                                         Group: {
177                                                                 name: $('#name').val(),
178                                                                 pricingType: $('#pricingType').val(),
179                                                                 fixedPricing: $('#fixedPricing').val(),
180                                                                 priceGuide: $('#priceGuide').val()
181                                                         }
182                                         };
183                                         if (null == _w.groups.itemId)
184                                         {
185                                                 App.API.execTask(
186                                                                 'addGroup', data, {},
187                                                                 function() {
188                                                                         $('#name').val('');
189                                                                         $('#pricingType').selectpicker('val', '');
190                                                                         $('#fixedPricing').val('0');
191                                                                         $('#priceGuide').selectpicker('val', '');
192                                                                         $('label.error').remove();
193                                                                         _w.groups.loadGroupGrid( null, {}, {} );
194                                                                         $('#btnSave').prop('disabled', false);
195                                                                         $('#btnSave').html(' Add ');
196                                                                 }, _w.taskExecError
197                                                         );
198                                         }
199                                         else
200                                         {
201                                                 App.API.getTask(
202                                                                 'updateGroup' + _w.groups.itemId, 'Company', 'Group.Update', null,
203                                                                 {id: _w.groups.itemId}, function () {
204                                                                         App.API.execTask(
205                                                                                         'updateGroup' + _w.groups.itemId, data, {},
206                                                                                         function() {
207                                                                                                 $('#name').val('');
208                                                                                                 $('#pricingType').selectpicker('val', '');
209                                                                                                 $('#fixedPricing').val('0');
210                                                                                                 $('#priceGuide').selectpicker('val', '');
211                                                                                                 $('label.error').remove();
212                                                                                                 _w.groups.loadGroupGrid( null, {}, {} );
213                                                                                                 $('#btnSave').prop('disabled', false);
214                                                                                                 $('#btnSave').html(' Add ');
215                                                                                         }, _w.taskExecError
216                                                                                 );
217                                                                 }, _w.taskContractError
218                                                                 );
219                                         }
220                                 }
221                         });
222                         $('#btnSave').prop('disabled', false);
223                 }
224
225         };
226
227 })();