dda5daed2d1098f56ee55a0d5f59fb979cf6a7f8
[tine20] / tine20 / Timetracker / js / TimeaccountEditDialog.js
1 /**
2  * Tine 2.0
3  * 
4  * @package     Timetracker
5  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
6  * @author      Cornelius Weiss <c.weiss@metaways.de>
7  * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
8  *
9  */
10  
11 Ext.namespace('Tine.Timetracker');
12
13 Tine.Timetracker.TimeaccountEditDialog = Ext.extend(Tine.widgets.dialog.EditDialog, {
14     
15     /**
16      * @private
17      */
18     windowNamePrefix: 'TimeaccountEditWindow_',
19     appName: 'Timetracker',
20     recordClass: Tine.Timetracker.Model.Timeaccount,
21     recordProxy: Tine.Timetracker.timeaccountBackend,
22     loadRecord: false,
23     tbarItems: [{xtype: 'widget-activitiesaddbutton'}],
24     
25     /**
26      * overwrite update toolbars function (we don't have record grants yet)
27      */
28     updateToolbars: function() {
29
30     },
31     
32     onRecordLoad: function() {
33         // make sure grants grid is initialised
34         this.getGrantsGrid();
35         
36         var grants = this.record.get('grants') || [];
37         this.grantsStore.loadData({results: grants});
38         Tine.Timetracker.TimeaccountEditDialog.superclass.onRecordLoad.call(this);
39         
40     },
41     
42     onRecordUpdate: function() {
43         Tine.Timetracker.TimeaccountEditDialog.superclass.onRecordUpdate.call(this);
44         this.record.set('grants', '');
45         
46         var grants = [];
47         this.grantsStore.each(function(_record){
48             grants.push(_record.data);
49         });
50         
51         this.record.set('grants', grants);
52     },
53     
54     /**
55      * returns dialog
56      * 
57      * NOTE: when this method gets called, all initalisation is done.
58      */
59     getFormItems: function() {
60         
61         var secondRow = [{
62             fieldLabel: this.app.i18n._('Unit'),
63             name: 'price_unit'
64         }, {
65             xtype: 'numberfield',
66             fieldLabel: this.app.i18n._('Unit Price'),
67             name: 'price',
68             allowNegative: false
69             //decimalSeparator: ','
70         }, {
71             fieldLabel: this.app.i18n._('Budget'),
72             name: 'budget'
73         }, {
74             fieldLabel: this.app.i18n._('Status'),
75             name: 'is_open',
76             xtype: 'combo',
77             mode: 'local',
78             forceSelection: true,
79             triggerAction: 'all',
80             store: [[0, this.app.i18n._('closed')], [1, this.app.i18n._('open')]]
81         }, {
82             fieldLabel: this.app.i18n._('Billed'),
83             name: 'status',
84             xtype: 'combo',
85             mode: 'local',
86             forceSelection: true,
87             triggerAction: 'all',
88             value: 'not yet billed',
89             store: [
90                 ['not yet billed', this.app.i18n._('not yet billed')], 
91                 ['to bill', this.app.i18n._('to bill')],
92                 ['billed', this.app.i18n._('billed')]
93             ],
94             listeners: {
95                 scope: this,
96                 select: this.onBilledChange.createDelegate(this)
97             }
98         }, {
99             //disabled: true,
100             //emptyText: this.app.i18n._('not cleared yet...'),
101             fieldLabel: this.app.i18n._('Cleared In'),
102             name: 'billed_in',
103             xtype: 'textfield'
104         }, {
105             fieldLabel: this.app.i18n._('Booking deadline'),
106             name: 'deadline',
107             xtype: 'combo',
108             mode: 'local',
109             forceSelection: true,
110             triggerAction: 'all',
111             value: 'none',
112             store: [
113                 ['none', this.app.i18n._('none')], 
114                 ['lastweek', this.app.i18n._('last week')]
115             ]
116         }, {
117             xtype: 'extuxclearabledatefield',
118             name: 'cleared_at',
119             fieldLabel: this.app.i18n._('Cleared at')
120         }];
121         
122         if (Tine.Tinebase.appMgr.get('Sales')) {
123             secondRow.push({
124                 xtype: 'tinerelationpickercombo',
125                 fieldLabel: this.app.i18n._('Cost Center'),
126                 editDialog: this,
127                 allowBlank: true,
128                 app: 'Sales',
129                 recordClass: Tine.Sales.Model.CostCenter,
130                 relationType: 'COST_CENTER',
131                 relationDegree: 'sibling',
132                 modelUnique: true
133             });
134         }
135         
136         secondRow.push({
137             columnWidth: 1/3,
138             editDialog: this,
139             xtype: 'tinerelationpickercombo',
140             fieldLabel: this.app.i18n._('Responsible person'),
141             allowBlank: true,
142             app: 'Addressbook',
143             recordClass: Tine.Addressbook.Model.Contact,
144             relationType: 'RESPONSIBLE',
145             relationDegree: 'sibling',
146             modelUnique: true
147         });
148         
149         secondRow.push({
150             hideLabel: true,
151             boxLabel: this.app.i18n._('Timesheets are billable'),
152             name: 'is_billable',
153             xtype: 'checkbox',
154             columnWidth: .666
155         });
156         
157         return {
158             xtype: 'tabpanel',
159             border: false,
160             plain:true,
161             activeTab: 0,
162             plugins: [{
163                 ptype : 'ux.tabpanelkeyplugin'
164             }],
165             defaults: {
166                 hideMode: 'offsets'
167             },
168             items:[{
169                 title: this.app.i18n.ngettext('Time Account', 'Time Accounts', 1),
170                 autoScroll: true,
171                 border: false,
172                 frame: true,
173                 layout: 'border',
174                 items: [{
175                     region: 'center',
176                     xtype: 'columnform',
177                     labelAlign: 'top',
178                     formDefaults: {
179                         xtype:'textfield',
180                         anchor: '100%',
181                         labelSeparator: '',
182                         columnWidth: .333
183                     },
184                     items: [[{
185                         fieldLabel: this.app.i18n._('Number'),
186                         name: 'number',
187                         allowBlank: false
188                         }, {
189                         columnWidth: .666,
190                         fieldLabel: this.app.i18n._('Title'),
191                         name: 'title',
192                         allowBlank: false
193                         }], [{
194                         columnWidth: 1,
195                         fieldLabel: this.app.i18n._('Description'),
196                         xtype: 'textarea',
197                         name: 'description',
198                         height: 150
199                         }], secondRow] 
200                 }, {
201                     // activities and tags
202                     layout: 'accordion',
203                     animate: true,
204                     region: 'east',
205                     width: 210,
206                     split: true,
207                     collapsible: true,
208                     collapseMode: 'mini',
209                     header: false,
210                     margins: '0 5 0 5',
211                     border: true,
212                     items: [/*new Ext.Panel({
213                         // @todo generalise!
214                         title: this.app.i18n._('Description'),
215                         iconCls: 'descriptionIcon',
216                         layout: 'form',
217                         labelAlign: 'top',
218                         border: false,
219                         items: [{
220                             style: 'margin-top: -4px; border 0px;',
221                             labelSeparator: '',
222                             xtype:'textarea',
223                             name: 'description',
224                             hideLabel: true,
225                             grow: false,
226                             preventScrollbars:false,
227                             anchor:'100% 100%',
228                             emptyText: this.app.i18n._('Enter description')                            
229                         }]
230                     }),*/
231                     new Tine.widgets.activities.ActivitiesPanel({
232                         app: 'Timetracker',
233                         showAddNoteForm: false,
234                         border: false,
235                         bodyStyle: 'border:1px solid #B5B8C8;'
236                     }),
237                     new Tine.widgets.tags.TagPanel({
238                         app: 'Timetracker',
239                         border: false,
240                         bodyStyle: 'border:1px solid #B5B8C8;'
241                     })]
242                 }]
243             },{
244                 title: this.app.i18n._('Access'),
245                 layout: 'fit',
246                 items: [this.getGrantsGrid()]
247             }, new Tine.widgets.activities.ActivitiesTabPanel({
248                 app: this.appName,
249                 record_id: (! this.copyRecord) ? this.record.id : null,
250                 record_model: this.appName + '_Model_' + this.recordClass.getMeta('modelName')
251             })]
252         };
253     },
254     
255     getGrantsGrid: function() {
256         if (! this.grantsGrid) {
257             this.grantsStore =  new Ext.data.JsonStore({
258                 root: 'results',
259                 totalProperty: 'totalcount',
260                 // use account_id here because that simplifies the adding of new records with the search comboboxes
261                 id: 'account_id',
262                 fields: Tine.Timetracker.Model.TimeaccountGrant
263             });
264             
265             var columns = [
266                 new Ext.ux.grid.CheckColumn({
267                     header: this.app.i18n._('Book Own'),
268                     dataIndex: 'bookOwnGrant',
269                     tooltip: _('The grant to add Timesheets to this Timeaccount'),
270                     width: 55
271                 }),
272                 new Ext.ux.grid.CheckColumn({
273                     header: this.app.i18n._('View All'),
274                     tooltip: _('The grant to view Timesheets of other users'),
275                     dataIndex: 'viewAllGrant',
276                     width: 55
277                 }),
278                 new Ext.ux.grid.CheckColumn({
279                     header: this.app.i18n._('Book All'),
280                     tooltip: _('The grant to add Timesheets for other users'),
281                     dataIndex: 'bookAllGrant',
282                     width: 55
283                 }),
284                 new Ext.ux.grid.CheckColumn({
285                     header:this.app.i18n. _('Manage Clearing'),
286                     tooltip: _('The grant to manage clearing of Timesheets'),
287                     dataIndex: 'manageBillableGrant',
288                     width: 55
289                 }),
290                 new Ext.ux.grid.CheckColumn({
291                     header:this.app.i18n. _('Export'),
292                     tooltip: _('The grant to export Timesheets of Timeaccount'),
293                     dataIndex: 'exportGrant',
294                     width: 55
295                 }),
296                 new Ext.ux.grid.CheckColumn({
297                     header: this.app.i18n._('Manage All'),
298                     tooltip: _('Includes all other grants'),
299                     dataIndex: 'adminGrant',
300                     width: 55
301                 })
302             ];
303             
304             this.grantsGrid = new Tine.widgets.account.PickerGridPanel({
305                 selectType: 'both',
306                 title:  this.app.i18n._('Permissions'),
307                 store: this.grantsStore,
308                 hasAccountPrefix: true,
309                 configColumns: columns,
310                 selectAnyone: false,
311                 selectTypeDefault: 'group',
312                 recordClass: Tine.Tinebase.Model.Grant
313             });
314         }
315         return this.grantsGrid;
316     },
317     
318     /**
319      * is called is billed field changes
320      */
321     onBilledChange: function(combo, record, index) {
322         if (combo.getValue() == 'billed') {
323             if (! this.getForm().findField('cleared_at').getValue()) {
324                 this.getForm().findField('cleared_at').setValue(new Date());
325             }
326         }
327     }
328 });
329
330 /**
331  * Timetracker Edit Popup
332  */
333 Tine.Timetracker.TimeaccountEditDialog.openWindow = function (config) {
334     var id = (config.record && config.record.id) ? config.record.id : 0;
335     var window = Tine.WindowFactory.getWindow({
336         width: 800,
337         height: 500,
338         name: Tine.Timetracker.TimeaccountEditDialog.prototype.windowNamePrefix + id,
339         contentPanelConstructor: 'Tine.Timetracker.TimeaccountEditDialog',
340         contentPanelConstructorConfig: config
341     });
342     return window;
343 };