/**
 * Copyright Intermesh
 *
 * This file is part of Group-Office. You should have received a copy of the
 * Group-Office license along with Group-Office. See the file /LICENSE.TXT
 *
 * If you have questions write an e-mail to info@intermesh.nl
 *
 * @version $Id: ExpensesGrid.js 17180 2013-10-31 10:40:50Z mschering $
 * @copyright Copyright Intermesh
 * @author Merijn Schering <mschering@intermesh.nl>
 * @author Michael de Hart <msdhart@intermesh.nl>
 */


GO.projects2.ExpensesGrid = Ext.extend(GO.grid.GridPanel,{

	initComponent : function(){
		
		this.summary = new Ext.grid.GridSummary();

		this.store = new GO.data.JsonStore({
			url:GO.url("projects2/expense/store"),
			fields:['id','project_id','nett','vat','date','description','expense_budget']
		})
		this.store.on('load', function(me, records, options){ 
			if(records.length===0)
				this.summary.view.summary.update();
		},this);

		Ext.apply(this,{
			plugins: [this.summary],
			title:GO.projects2.lang.expenses,
			disabled:true,
			standardTbar:true,
			store: this.store,
			border: false,
			paging:true,
			editDialogClass: GO.projects2.ExpenseDialog,
			listeners:{
				show:function(){
					this.store.load();
				},
				scope:this
			},
			cm:new Ext.grid.ColumnModel({
				defaults:{
					sortable:true
				},
				columns:[{
					header: GO.projects2.lang.expenseBudget,
					dataIndex: 'expense_budget'
				},{
					header: GO.lang.strDescription,
					dataIndex: 'description'
				},{
					header: GO.projects2.lang.amount,
					dataIndex: 'nett',
					summaryType: 'sum',
					editor: new GO.form.NumberField({
						allowBlank: false
					}),
					align:'right'
//					renderer:function(v){
//						var number = GO.util.unlocalizeNumber(v);
//						return GO.util.numberFormat(number);
//					}
				},{
					header: GO.projects2.lang.vat,
					dataIndex: 'vat',
					
					editor: new GO.form.NumberField({
						allowBlank: false
					}),
					align:'right'
				},{
					header: GO.lang.strDate,
					dataIndex: 'date',
					editor: new Ext.form.DateField({
						format:GO.settings.date_format
					}),

					renderer: function(value){
						return typeof(value.dateFormat)=='undefined' ? value : value.dateFormat(GO.settings.date_format);
					},
					summaryRenderer:function(){
						return '&nbsp;';
					}
				}]
			})
		});
		
		GO.projects2.ExpensesGrid.superclass.initComponent.call(this);		
	},

	
	setProjectId : function(project_id){
		this.store.baseParams.project_id=project_id;
		this.setDisabled(GO.util.empty(project_id));
	},
	btnAdd : function(){
		if(this.editDialogClass){
			this.showEditDialog(0,{
				values:{
					project_id:this.store.baseParams.project_id
					}
			});
		}
	}
});
