adjusts validation for discount (purchase invoices) and budget (TA)
[tine20] / tine20 / Sales / js / SupplierEditDialog.js
1 /*
2  * Tine 2.0
3  * 
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Alexander Stintzing <a.stintzing@metaways.de>
6  * @copyright   Copyright (c) 2013 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8 Ext.ns('Tine.Sales');
9
10 /**
11  * @namespace   Tine.Sales
12  * @class       Tine.Sales.SupplierEditDialog
13  * @extends     Tine.widgets.dialog.EditDialog
14  * 
15  * <p>Supplier Compose Dialog</p>
16  * <p></p>
17  * 
18  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
19  * @author      Alexander Stintzing <a.stintzing@metaways.de>
20  * 
21  * @param       {Object} config
22  * @constructor
23  * Create a new Tine.Sales.SupplierEditDialog
24  */
25 Tine.Sales.SupplierEditDialog = Ext.extend(Tine.widgets.dialog.EditDialog, {
26     windowWidth: 900,
27     windowHeight: 800,
28     
29     initComponent: function() {
30         this.tbarItems = [{xtype: 'widget-activitiesaddbutton'}];
31         Tine.Sales.SupplierEditDialog.superclass.initComponent.call(this);
32     },
33     
34     /**
35      * 
36      */
37     isValid: function() {
38         var isValid = true,
39             form = this.getForm();
40         
41         isValid = Tine.Sales.SupplierEditDialog.superclass.isValid.call(this)
42             
43         if (Ext.isEmpty(form.findField('adr_street').getValue()) && Ext.isEmpty(form.findField('adr_pobox').getValue())) {
44             isValid = false;
45             var msg = this.app.i18n._('Either street or postbox is required!');
46             form.markInvalid( {'adr_street': msg, 'adr_pobox': msg});
47         }
48
49         return isValid;
50     },
51     
52     /**
53      * executed after record got updated from proxy
54      */
55     onRecordLoad: function() {
56         // interrupt process flow until dialog is rendered
57         if (! this.rendered) {
58             this.onRecordLoad.defer(250, this);
59             return;
60         }
61         
62         Tine.Sales.SupplierEditDialog.superclass.onRecordLoad.call(this);
63         
64         if (this.copyRecord) {
65             this.doCopyRecord();
66             this.window.setTitle(this.app.i18n._('Copy Supplier'));
67         } else {
68             if (! this.record.id) {
69                 this.window.setTitle(this.app.i18n._('Add New Supplier'));
70             } else {
71                 this.window.setTitle(String.format(this.app.i18n._('Edit Supplier "{0}"'), this.record.getTitle()));
72                 //this.getForm().findField('number').disable();
73             }
74         }
75     },
76     
77     /**
78      * duplicate(s) found exception handler
79      * 
80      * @todo: make this globally, smoothly the virtual fields (modelconfig) don't fit anywhere
81      * 
82      * @param {Object} exception
83      */
84     onDuplicateException: function(exception) {
85         this.onRecordUpdate();
86         exception.clientRecord = this.record.data;
87         Tine.Sales.SupplierEditDialog.superclass.onDuplicateException.call(this, exception);
88     },
89     
90     /**
91      * Fill address with contact data, if not set already
92      * 
93      * @param {} combo
94      * @param {} record
95      * @param {} index
96      */
97     onSelectContactPerson: function(combo, record, index) {
98         var form = this.getForm();
99         if (record.get('adr_one_street') && ! form.findField('adr_street').getValue()) {
100             var ar = ['street', 'postalcode', 'region', 'locality', 'countryname'];
101             for (var index = 0; index < ar.length; index++) {
102                 form.findField('adr_' + ar[index]).setValue(record.get('adr_one_' + ar[index]));
103             }
104         }
105     },
106     
107     /**
108      * returns dialog
109      * 
110      * NOTE: when this method gets called, all initalisation is done.
111      * 
112      * @return {Object}
113      * @private
114      */
115     getFormItems: function() {
116         var formFieldDefaults = {
117             xtype:'textfield',
118             anchor: '100%',
119             labelSeparator: '',
120             columnWidth: .5
121         };
122         
123         this.clipboardButton = new Ext.Button({
124            columnWidth: 5/100,
125            iconCls: 'clipboard',
126            tooltip: Ext.util.Format.htmlEncode(this.app.i18n._('Copy address to the clipboard')),
127            fieldLabel: '&nbsp;',
128            lazyLoading: false,
129            listeners: {
130                 scope: this,
131                 click: function() {
132                     this.onRecordUpdate();
133                     Tine.Sales.addToClipboard(this.record);
134                 }
135            }
136         });
137         
138         var currency = Tine.Sales.registry.get('config').ownCurrency.value;
139         
140         return {
141             xtype: 'tabpanel',
142             defaults: {
143                 hideMode: 'offsets'
144             },
145             border: false,
146             plain: true,
147             activeTab: 0,
148             items: [{
149             title: this.app.i18n._('Supplier'),
150             autoScroll: true,
151             border: false,
152             frame: true,
153             layout: 'border',
154             items: [{
155                 region: 'center',
156                 layout: 'hfit',
157                 border: false,
158                 items: [{
159                     xtype: 'fieldset',
160                     layout: 'hfit',
161                     autoHeight: true,
162                     title: this.app.i18n._('Core Data'),
163                     items: [{
164                         xtype: 'columnform',
165                         labelAlign: 'top',
166                         formDefaults: formFieldDefaults,
167                         items: [[{
168                                 fieldLabel: this.app.i18n._('Supplier Number'),
169                                 name: 'number',
170                                 allowBlank: true,
171                                 columnWidth: .250,
172                                 minValue: 1,
173                                 maxValue: 4294967296,
174                                 xtype: 'uxspinner',
175                                 strategy: new Ext.ux.form.Spinner.NumberStrategy({
176                                     incrementValue : 1,
177                                     alternateIncrementValue: 1,
178                                     minValue: 1,
179                                     maxValue: 4294967296,
180                                     allowDecimals : false
181                                 })
182                             }, {
183                                 columnWidth: .750,
184                                 allowBlank: false,
185                                 fieldLabel: this.app.i18n._('Name'),
186                                 name: 'name',
187                                 xtype: 'tine.widget.field.AutoCompleteField',
188                                 recordClass: this.recordClass
189                             }], [Tine.widgets.form.RecordPickerManager.get('Addressbook', 'Contact', {
190                                     columnWidth: 1/2,
191                                     blurOnSelect: true,
192                                     name: 'cpextern_id',
193                                     allowBlank: true,
194                                     fieldLabel: this.app.i18n._('Contact Person (external)'),
195                                     listeners: {
196                                         scope: this,
197                                         select: this.onSelectContactPerson
198                                     }
199                             }), Tine.widgets.form.RecordPickerManager.get('Addressbook', 'Contact', {
200                                     columnWidth: 1/2,
201                                     blurOnSelect: true,
202                                     name: 'cpintern_id',
203                                     allowBlank: true,
204                                     fieldLabel: this.app.i18n._('Contact Person (internal)')
205                                 })
206                         ]]
207                     }]
208                 }, {
209                     xtype: 'fieldset',
210                     layout: 'hfit',
211                     autoHeight: true,
212                     title: this.app.i18n._('Accounting'),
213                     items: [{
214                         xtype: 'columnform',
215                         labelAlign: 'top',
216                         formDefaults: formFieldDefaults,
217                         items: [
218                             [{
219                                 name: 'vatid',
220                                 fieldLabel: this.app.i18n._('VAT No.')
221                             }, {
222                                 name: 'credit_term',
223                                 fieldLabel: this.app.i18n._('Credit Term (days)'),
224                                 xtype: 'uxspinner',
225                                 strategy: new Ext.ux.form.Spinner.NumberStrategy({
226                                     incrementValue : 1,
227                                     alternateIncrementValue: 1,
228                                     minValue: 0,
229                                     maxValue: 1024,
230                                     allowDecimals : false
231                                 })
232                             }], [{
233                                 name: 'currency',
234                                 fieldLabel: this.app.i18n._('Currency'),
235                                 value: currency,
236                                 allowBlank: false
237                             }, {
238                                 name: 'currency_trans_rate',
239                                 fieldLabel: this.app.i18n._('Currency Translation Rate'),
240                                 xtype: 'uxspinner',
241                                 strategy: new Ext.ux.form.Spinner.NumberStrategy({
242                                     incrementValue : 0.01,
243                                     alternateIncrementValue: 0.1,
244                                     minValue: 0.01,
245                                     allowDecimals : true
246                                 }),
247                                 decimalPrecision: 2,
248                                 decimalSeparator: Tine.Tinebase.registry.get('decimalSeparator'),
249                                 regex: /^[0-9]+\.?[0-9]*$/
250                             }], [{
251                                 name: 'iban',
252                                 fieldLabel: this.app.i18n._('IBAN')
253                             }, {
254                                 name: 'bic',
255                                 fieldLabel: this.app.i18n._('BIC')
256                             }]]
257                     }]
258                 }, {
259                     xtype: 'fieldset',
260                     layout: 'hfit',
261                     autoHeight: true,
262                     title: this.app.i18n._('Postal Address'),
263                     items: [{
264                         xtype: 'columnform',
265                         labelAlign: 'top',
266                         formDefaults: formFieldDefaults,
267                         items: [
268                             [{
269                                 name: 'adr_street',
270                                 fieldLabel: this.app.i18n._('Street'),
271                                 columnWidth: 47/100
272                             }, {
273                                 name: 'adr_pobox',
274                                 fieldLabel: this.app.i18n._('Postbox'),
275                                 columnWidth: 47/100
276                                 
277                             }], [{
278                                 name: 'adr_postalcode',
279                                 allowBlank: false,
280                                 fieldLabel: this.app.i18n._('Postalcode'),
281                                 columnWidth: 47/100
282                             }, {
283                                 name: 'adr_locality',
284                                 allowBlank: false,
285                                 fieldLabel: this.app.i18n._('Locality'),
286                                 columnWidth: 47/100
287                             }], [{
288                                 name: 'adr_region',
289                                 fieldLabel: this.app.i18n._('Region'),
290                                 columnWidth: 47/100
291                             }, {
292                                 xtype: 'widget-countrycombo',
293                                 name: 'adr_countryname',
294                                 fieldLabel: this.app.i18n._('Country'),
295                                 columnWidth: 47/100
296                             }
297                             ], [{
298                                 name: 'adr_prefix1',
299                                 fieldLabel: this.app.i18n._('Prefix'),
300                                 columnWidth: 47/100
301                             }, {
302                                 name: 'adr_prefix2',
303                                 fieldLabel: this.app.i18n._('Additional Prefix'),
304                                 columnWidth: 48/100
305                             }, this.clipboardButton]
306                         ]
307                     }]
308                 }, {
309                     xtype: 'fieldset',
310                     layout: 'hfit',
311                     autoHeight: true,
312                     title: this.app.i18n._('Miscellaneous'),
313                     items: [{
314                         xtype: 'columnform',
315                         labelAlign: 'top',
316                         formDefaults: formFieldDefaults,
317                         items: [[
318                             {
319                                 columnWidth: 1/3,
320                                 fieldLabel: this.app.i18n._('Web'),
321                                 xtype: 'mirrortextfield',
322                                 name: 'url',
323                                 maxLength: 128,
324                                 listeners: {
325                                     scope: this,
326                                     focus: function (field) {
327                                         if (! field.getValue()) {
328                                             field.setValue('http://www.');
329                                             field.selectText.defer(100, field, [7, 11]);
330                                         }
331                                     },
332                                     blur: function (field) {
333                                         if (field.getValue() === 'http://www.') {
334                                             field.setValue(null);
335                                             field.validate();
336                                         }
337                                         if (field.getValue().indexOf('http://http://') == 0 || field.getValue().indexOf('http://https://') == 0) {
338                                             field.setValue(field.getValue().substr(7));
339                                             field.validate();
340                                         }
341                                         if (field.getValue().indexOf('http://www.http://') == 0 || field.getValue().indexOf('http://www.https://') == 0) {
342                                             field.setValue(field.getValue().substr(11));
343                                             field.validate();
344                                         }
345                                     }
346                                 }
347                             }
348                         ]]
349                     }]
350                 }]
351             }, {
352                 // activities and tags
353                 layout: 'accordion',
354                 animate: true,
355                 region: 'east',
356                 width: 210,
357                 split: true,
358                 collapsible: true,
359                 collapseMode: 'mini',
360                 header: false,
361                 margins: '0 5 0 5',
362                 border: true,
363                 items: [
364                     new Ext.Panel({
365                         title: this.app.i18n._('Description'),
366                         iconCls: 'descriptionIcon',
367                         layout: 'form',
368                         labelAlign: 'top',
369                         border: false,
370                         items: [{
371                             style: 'margin-top: -4px; border 0px;',
372                             labelSeparator: '',
373                             xtype: 'textarea',
374                             name: 'description',
375                             hideLabel: true,
376                             grow: false,
377                             preventScrollbars: false,
378                             anchor: '100% 100%',
379                             emptyText: this.app.i18n._('Enter description'),
380                             requiredGrant: 'editGrant'
381                         }]
382                     }),
383                     new Tine.widgets.activities.ActivitiesPanel({
384                         app: 'Sales',
385                         showAddNoteForm: false,
386                         border: false,
387                         bodyStyle: 'border:1px solid #B5B8C8;'
388                     }),
389                     new Tine.widgets.tags.TagPanel({
390                         app: 'Sales',
391                         border: false,
392                         bodyStyle: 'border:1px solid #B5B8C8;'
393                     })
394                 ]
395             }]
396         },
397            new Tine.widgets.activities.ActivitiesTabPanel({
398                 app: this.appName,
399                 record_id: this.record.id,
400                 record_model: 'Sales_Model_Supplier'
401             })
402         ]};
403     }
404 });