
Ext.ns('app');

/************************** STORE PARA DOCUMENTOS ****************************/

app.StoreDocumentos = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarDocumentos'
        }),
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        },app.documentoRecord),
		sortInfo: {field: 'id', direction: 'desc'},
        remoteSort: true
});

app.StoreDocumentosAsociados = Ext.extend(app.StoreDocumentos, {
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarDocumentosAsociados'
		})
});
app.StoreDocumentosNoAsociados = Ext.extend(app.StoreDocumentos, {
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarDocumentosNoAsociados'
        })
});

app.ColumnModelDocumentos = function(selectionModel) {
	var columns=[
		{dataIndex:'id', header:'Id', sortable: true, hidden:true, width:100},
		//{dataIndex:'id_empresa', header:'id_empresa', sortable: true, hidden:false, width:100},
		{dataIndex:'nombre', header:'Nombre', sortable: true, hidden:false, width:100},
		{dataIndex:'tags', header:'Tags', sortable: true, hidden:false, width:100},
		{dataIndex:'fichero', header:'Fichero', sortable: true, hidden:false, width:150,
			renderer:function(data, params, record){
    	    return '<a href="documentos/'+record.get('id_empresa')+'/'+data+'" target="_blank"/>'+data+'</a>';
    	    }
    	},
		{dataIndex:'fecha', header:'Fecha', sortable: true, hidden:false, width:80, align:'right'}
	];

	if(selectionModel!=null){
		columns=[selectionModel].concat(columns);
	}

	// call the superclass's constructor
	app.ColumnModelDocumentos.superclass.constructor.call(this, columns);
};

Ext.extend(app.ColumnModelDocumentos, Ext.grid.ColumnModel);


/********************* GRID PARA MOSTRAR DOCUMENTOS ASOCIADOS A UNA TABLA *******************************/

app.GridDocumentosAsociados = function(idGrid, tabla, idName) {
	app.GridDocumentosAsociados.superclass.constructor.call(this, {
		id:idGrid,
     	 autoScroll:true,
         loadMask: true,
         border:false,
         store:new app.StoreDocumentosAsociados,
         cm: new app.ColumnModelDocumentos,
         viewConfig: {
         	forceFit: true //,autoFill:true
         },
         tbar:[{
            text:'A&ntilde;adir',
            id:'add_documento_'+tabla,
            tooltip:'Subir Documento',
            hidden:true,
            iconCls:'add',
			listeners :{
            	click: function(){
            		var id = Ext.getCmp(idName).getValue();
					mostrarSubirDocumentoAsociado(idGrid, tabla, id);
            	}
            }
		},{
            text:'Eliminar',
            id:'del_documento_'+tabla,
            hidden:true,
            tooltip:'Eliminar Documento Asociado',
            iconCls:'remove',
			listeners :{
            	click: function(){
            		var grid = Ext.getCmp(idGrid)
	            	if(comprobarSeleccionado(grid)){
		             	Ext.Msg.confirm('Confirmar', 'Seguro que desea eliminar la asociaci&oacute;n con este Documento',
				    	function(btn){
			                if(btn == 'yes'){
				                var id = Ext.getCmp(idName).getValue();
				                borrarAsociacion('EliminarDocumentoAsociado',grid, tabla, id)
				            }
	            		});
            		}
            	}
            }
         },{
            text:'Asociar Documentos',
            id:'asociar_documento_'+tabla,
            tooltip:'Asociar Documentos existentes',
            hidden:true,
            iconCls:'page_add',
			listeners :{
            	click: function(){
            		var id = Ext.getCmp(idName).getValue();
					mostrarVentanaAsociar('Documentos', idGrid, tabla, id);
            	}
            }
		},{
            text:'',
            tooltip:'Actualizar Tabla',
            id:'actualizar_grid_documentos_'+tabla,
            hidden:true,
            iconCls:'refresh',
			listeners :{
            	click: function(){
            		Ext.getCmp(idGrid).getStore().reload();
            	}
            }
         }]
		,mostrarOcultar: function (id){
			var visible=(id==0 || id=='')?false:true;
			var botones = ['add_documento_','del_documento_','asociar_documento_', 'actualizar_grid_documentos_'];
			for(i=0; i<botones.length; i++)
				Ext.getCmp(botones[i]+tabla).setVisible(visible);
		},inicializar: function(){
			var id = Ext.getCmp(idName).getValue();
			var grid = Ext.getCmp(idGrid);
			this.mostrarOcultar(id);
			grid.getStore().load({params:{tabla:tabla, id: id}})
		}
	});
}

Ext.extend(app.GridDocumentosAsociados, Ext.grid.GridPanel);

/**************** STORE PARA CLIENTES **************************/

app.StoreClientes = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarClientes'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.clienteRecord),
		sortInfo: {field: 'nombre', direction: 'asc'},
        // turn on remote sorting
        remoteSort: true
});


/**************** STORE PARA PROVEEDORES **************************/

app.StoreProveedores = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarProveedores'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.proveedorRecord),
		sortInfo: {field: 'nombre', direction: 'asc'},
        // turn on remote sorting
        remoteSort: true
});

/**************** STORE PARA BANCOS **************************/

app.StoreBancos = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarBancos'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.bancoRecord),
		sortInfo: {field: 'descripcion', direction: 'asc'},
        // turn on remote sorting
        remoteSort: true
});


/**************** STORE PARA MOV. BANCOS **************************/

app.StoreMovBancos = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarMovBancos'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.movBancoRecord),
		sortInfo: {field: 'fecha', direction: 'desc'},
        // turn on remote sorting
        remoteSort: true
});

/**************** STORE PARA ARTICULOS **************************/

app.StoreArticulos = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarArticulos'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.articuloRecord),
		sortInfo: {field: 'nombre', direction: 'asc'},
        // turn on remote sorting
        remoteSort: true
});

/**************** STORE PARA PRESUPUESTOS **************************/

app.StorePresupuestos = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarPresupuestos'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.presupuestoRecord),
		sortInfo: {field: 'presupuesto', direction: 'desc'},
        // turn on remote sorting
        remoteSort: true
});

/**************** STORE PARA VENTAS **************************/

app.StoreVentas = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarVentas'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.ventaRecord),
		sortInfo: {field: 'factura', direction: 'desc'},
        // turn on remote sorting
        remoteSort: true
});

/**************** STORE PARA COBROS **************************/

app.StoreCobros = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarCobros'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.cobroRecord),
		sortInfo: {field: 'fecha', direction: 'desc'},
        // turn on remote sorting
        remoteSort: true
});


/**************** STORE PARA COMPRAS **************************/

app.StoreCompras = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarCompras'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.compraRecord),
		sortInfo: {field: 'compra', direction: 'desc'},
        // turn on remote sorting
        remoteSort: true
});

/**************** STORE PARA COBROS **************************/

app.StorePagos = Ext.extend(Ext.data.Store,{
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
            url: 'BuscarPagos'
        }),
		baseParams:{start:0, limit:25},
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'registros',
            totalProperty: 'totalCount'
        }, app.pagoRecord),
		sortInfo: {field: 'fecha', direction: 'desc'},
        // turn on remote sorting
        remoteSort: true
});


