Fix reversalInvoice button
[tine20] / tine20 / Sales / js / InvoiceGridPanel.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) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8 Ext.ns('Tine.Sales');
9
10 /**
11  * Invoice grid panel
12  * 
13  * @namespace   Tine.Sales
14  * @class       Tine.Sales.InvoiceGridPanel
15  * @extends     Tine.widgets.grid.GridPanel
16  * 
17  * <p>Invoice Grid Panel</p>
18  * <p><pre>
19  * </pre></p>
20  * 
21  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
22  * @author      Alexander Stintzing <a.stintzing@metaways.de>    
23  * 
24  * @param       {Object} config
25  * @constructor
26  * Create a new Tine.Sales.InvoiceGridPanel
27  */
28 Tine.Sales.InvoiceGridPanel = Ext.extend(Tine.widgets.grid.GridPanel, {
29     
30     initComponent: function() {
31         this.initDetailsPanel();
32         Tine.Sales.InvoiceGridPanel.superclass.initComponent.call(this);
33     },
34     
35     /**
36      * is called when the component is rendered
37      * @param {} ct
38      * @param {} position
39      */
40     onRender : function(ct, position) {
41         this.billMask = new Ext.LoadMask(ct, {msg: this.app.i18n._('Rebilling Invoice...')});
42         Tine.Sales.ContractGridPanel.superclass.onRender.call(this, ct, position);
43     },
44     
45     /**
46      * @todo: make this generally available (here its more general: Tine.HumanResources.EmployeeGridPanel)
47      * 
48      * returns additional toobar items
49      * 
50      * @return {Array} of Ext.Action
51      */
52     getActionToolbarItems: function() {
53         this.actions_export = new Ext.Action({
54             // _('Export Invoices')
55             text: this.app.i18n._hidden('Export Invoices'),
56             iconCls: 'action_export',
57             scope: this,
58             requiredGrant: 'exportGrant',
59             disabled: true,
60             allowMultiple: true,
61             menu: {
62                 items: [
63                     new Tine.widgets.grid.ExportButton({
64                         text: this.app.i18n._('Export selected invoices as ODS'),
65                         singularText: this.app.i18n._('Export selected invoice as ODS'),
66                         pluralText: this.app.i18n._('Export selected invoices as ODS'),
67                         format: 'ods',
68                         iconCls: 'tinebase-action-export-ods',
69                         exportFunction: 'Sales.exportInvoices',
70                         gridPanel: this
71                     }),
72                     new Tine.widgets.grid.ExportButton({
73                         text: this.app.i18n._('Export as ...'),
74                         iconCls: 'tinebase-action-export-xls',
75                         exportFunction: 'Sales.exportInvoices',
76                         showExportDialog: true,
77                         gridPanel: this
78                     })
79                 ]
80             }
81         });
82
83         var exportButton = Ext.apply(new Ext.SplitButton(this.actions_export), {
84             scale: 'medium',
85             rowspan: 2,
86             iconAlign: 'top',
87             arrowAlign:'right'
88         });
89         
90         this.actions_reversal = new Ext.Action({
91             text: this.app.i18n._('Create Reversal Invoice'),
92             iconCls: 'action_reversal',
93             scope: this,
94             disabled: true,
95             allowMultiple: false,
96             handler: this.onReverseInvoice,
97             actionUpdater: function(action, grants, records) {
98                 if (records.length == 1 && records[0].get('type') == 'INVOICE' && records[0].get('number')) {
99                     action.enable();
100                 } else {
101                     action.disable();
102                 }
103             }
104         });
105
106         var reversalButton = Ext.apply(new Ext.Button(this.actions_reversal), {
107             scale: 'medium',
108             rowspan: 2,
109             iconAlign: 'top'
110         });
111         
112         this.actions_rebill = new Ext.Action({
113             text: this.app.i18n._('Rebill Invoice'),
114             iconCls: 'action_rebill',
115             scope: this,
116             disabled: true,
117             allowMultiple: false,
118             handler: this.onRebillInvoice,
119             actionUpdater: function(action, grants, records) {
120                 if (records.length == 1 && records[0].get('type') == 'INVOICE' && records[0].get('cleared') != 'CLEARED'  && records[0].get('is_auto')) {
121                     action.enable();
122                 } else {
123                     action.disable();
124                 }
125             }
126         });
127
128         var rebillButton = Ext.apply(new Ext.Button(this.actions_rebill), {
129             scale: 'medium',
130             rowspan: 2,
131             iconAlign: 'top'
132         });
133         
134         var additionalActions = [this.actions_export, this.actions_reversal, this.actions_rebill];
135         this.actionUpdater.addActions(additionalActions);
136         return [exportButton, reversalButton, rebillButton];
137     },
138     
139     /**
140      * is called on reversal invoice action
141      * 
142      * @param {Ext.Action} action
143      * @param {Object} event
144      */
145     onReverseInvoice: function(action, event) {
146         var rows = this.getGrid().getSelectionModel().getSelections();
147         if (rows.length == 1) {
148             var record = rows[0];
149             
150             var cfg = {
151                 record: record,
152                 createReversal: true
153             };
154             
155             Tine.Sales.InvoiceEditDialog.openWindow(cfg);
156         }
157     },
158     
159     /**
160      * 
161      */
162     onRebillInvoice: function() {
163         var rows = this.getGrid().getSelectionModel().getSelections();
164         
165         if (rows.length != 1) {
166             return;
167         }
168         
169         this.billMask.show();
170         
171         var that = this;
172         
173         var req = Ext.Ajax.request({
174             url : 'index.php',
175             params : { 
176                 method: 'Sales.rebillInvoice', 
177                 id:     rows[0].id 
178             },
179             success : function(result, request) {
180                 that.billMask.hide();
181                 that.getGrid().store.reload();
182             },
183             failure : function(exception) {
184                 that.billMask.hide();
185                 Tine.Tinebase.ExceptionHandler.handleRequestException(exception);
186             },
187             scope: that
188         });
189     },
190     
191     /**
192      * add custom items to context menu
193      * 
194      * @return {Array}
195      */
196     getContextMenuItems: function() {
197         var items = [
198             '-',
199             this.actions_export,
200             this.actions_reversal,
201             this.actions_rebill
202             ];
203         
204         return items;
205     },
206     
207     /**
208      * @private
209      */
210     initDetailsPanel: function() {
211         this.detailsPanel = new Tine.Sales.InvoiceDetailsPanel({
212             grid: this,
213             app: this.app
214         });
215     }
216 });