<?php require "_header.php";?>
<?php require "includeGridFilters.php";?>

<link rel="stylesheet" type="text/css" href="icons.css" />
<script language="javascript" type="text/javascript">  


var store = new Ext.data.JsonStore({
    url:  'data/view-voucher-data.php',
	remoteSort: true,
	
    root: 'certificates',
	idProperty:'voucher_id',
    totalProperty: 'count',
  	sortInfo: {
            field: 'voucher_id',
            direction: 'ASC'
        },
 	 fields: [{
			 name:'voucher_id',
			 type:'int'
			 },
			 {	name:'receipt_no',
			 	type:'int'
			 },
			  {	name:'voucher_no',
			 	type:'int'
			 },
			 {	name:'voucher_type',
			 	type:'list'
		     },
			 {	name:'voucher_check',
			 	type:'int'
			 },
			 {	name:'voucher_date',
			 	 type: 'date',
                 dateFormat: 'Y-m-d'
			 },
			 {	name:'voucher_totalamount',
			 	type:'float'
			 },
			 {	name:'voucher_creditdetail',
			 	type:'string'
			 },
			 {	name:'voucher_debitdetail',
			 	type:'string'
			 },
			 {	name:'account_id',
			 	type:'int'
			 },
			 {	name:'voucher_creditamount',
			 	type:'int'
			 },
			 {	name:'voucher_debitamount',
			 	type:'int'
			 }],
	        writer:new Ext.data.JsonWriter()
});


var filters = new Ext.ux.grid.GridFilters({
		filters:[
					{
						type:'list',
						dataIndex: 'voucher_check',
						options : [{id:'1',text:'Check'},{id:'0',text:'Cash'}],
						phpMode: true
					}, {
						type:'list',
						dataIndex: 'voucher_type',
						options : ['Contra','General','Receipt','Payment'],
						phpMode: true
					},{
						type: 'date',
						dataIndex: 'voucher_date'
					},{
						type: 'int',
						dataIndex: 'receipt_no'
					},{
						type: 'int',
						dataIndex: 'voucher_no'
					},{
						type: 'int',
						dataIndex: 'voucher_totalamount'
					}
				]
    });

Ext.onReady(function() {
	var xg = Ext.grid;
	 var sm2 = new xg.CheckboxSelectionModel({
        listeners: {
            selectionchange: function(sm) {
                if (sm.getCount() ) {
                    grid.removeButton.enable();
                } else {
                    grid.removeButton.disable();
                }
            }
        }
    });  
	var encode = false;
    var local = false;
	function confirmDeleteUsers(){
    if(grid.selModel.getCount() == 1) // only one president is selected here
    {
      Ext.MessageBox.confirm('Confirmation','You are about to delete a voucher. Continue?', deleteUsers);
    } else if(grid.selModel.getCount() > 1){
      Ext.MessageBox.confirm('Confirmation','Delete these voucher?', deleteUsers);
    } 
} 
function discheck(v)
{
 if(v==0)
 {
 return "Cash";
 }
 else
 {
   return "Check/DD";
 }
}

function deleteUsers(btn){
    if(btn=='yes'){
         var selections = grid.selModel.getSelections();
         var prez = [];
		 for(i = 0; i< grid.selModel.getCount(); i++){
          prez.push(selections[i].json.voucher_id);
		}
         var encoded_array = Ext.encode(prez);
         Ext.Ajax.request({  
            waitMsg: 'Please Wait',
            url: 'data/voucher-grid-delete.php', 
            params: { 
               task: "DELETEDIST", 
               ids:  encoded_array
			   }, 
            success: function(response){
              var result=eval(response.responseText);
              switch(result){
              case 1: 
                store.reload();
				Ext.MessageBox.show({
									   title: 'Success',
									   msg: 'voucher Deleted Successfully.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.INFO
				                      });
				
                break;
              default:
			     Ext.MessageBox.show({
									   title: 'Warning',
									   msg: 'Could not delete the entire selection.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.WARNING
				                      });
                
                break;
              }
            },
            failure: function(response){
              var result=response.responseText;
			  Ext.MessageBox.show({
									   title: 'Error',
									   msg: 'Could not delete the entire selection.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.ERROR
				                      });
                   
              }
         });
      }  
  }
	var createColModel = function (finish, start) {
	  var columns=[
				  sm2,
				  {header: "Edit",width:50,renderer: function(value, metaData, record, rowIndex, colIndex, store) {
	    	   		return '<a href="addedit-voucher.php?ID='+ record.get('voucher_id') +'">Edit</a>';}
				},
		
                 { header: "voucher_id",width:50, dataIndex: 'voucher_id', sortable: true, hidden:true },
				 { header: "Date",width:100,dataIndex: 'voucher_date',align:'center', sortable: true, renderer: Ext.util.Format.dateRenderer('d/m/Y')},
				 { header: "Receipt Type",width:80,dataIndex: 'voucher_type', sortable: true },
				 { header: "Check/Case",width:100,dataIndex: 'voucher_check', sortable: true,renderer:discheck},
				 { header: "Receipt No",width:70,dataIndex: 'receipt_no', sortable: true},
				 { header: "Voucher No",width:70,dataIndex: 'voucher_no', sortable: true},
				 { header: "Total account",width:92,dataIndex: 'voucher_totalamount', sortable: true},
				 { header: "Credit Detail",width:100,dataIndex: 'voucher_creditdetail', sortable: true},
				 { header: "Debit Detail",width:100,dataIndex: 'voucher_debitdetail', sortable: true}
            ];
return new Ext.grid.ColumnModel({
            columns: columns.slice(start || 0, finish),
            defaults: {
                sortable: true
            }
        });
    };
	    var grid = new Ext.grid.GridPanel({
		store: store,
		selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
        colModel: createColModel(40),
		enableColLock:false,
		 stripeRows: true,
		 plugins: [filters],
		sm: sm2,
		listeners: {
            render: {
                fn: function(){
                    store.reload({
                        params: {
                            start: 0,
                            limit: 50
                        }
                    });
                }
            }
        }, 
		 tbar: [
           {
		    	iconCls: 'icon-add',
           		text: 'Add Voucher',
				//ref: '../removeButton',
            	handler: function() {
               location.href="addedit-voucher.php"; }
      		},'-',{ 
            text: 'Delete Voucher',
            handler: confirmDeleteUsers,   // Confirm before deleting
            iconCls:'remove',
			ref: '../removeButton',
			disabled: true
      		}],
         width: 651,
         height: 360,
        loadMask: true,
        columnLines:true,
        bbar: new Ext.PagingToolbar({
            pageSize: 50,
            store: store,
            displayInfo: true,
			plugins: [filters],
            displayMsg: 'Displaying data {0} - {1} of {2}',
            emptyMsg: "No Data found"
        })
				
    });
	grid.getBottomToolbar().add([
       '->',
       {
            text: 'Clear Filter Data',
            handler: function () {
                grid.filters.clearFilters();
            } 
        }  
    ]);
		
//   store.load({ params: { start: 0, limit: 10} });
    var win = new Ext.Window({
        title: 'View  Voucher Information',
        height: 360,
        width: 820,
		modal:true,
        layout: 'fit',
		border: false,
		closeAction:'close',
		collapsible:'true',
		
        items: grid
    }).show();
	
	var varWidth =  (screen.width - win.width)/2;
	win.setPosition(varWidth,((screen.height/4)-100));
	
});
    
</script>
<div align="center"><div id="main-content" style="width:600px" align="left"></div></div> 
<?php require "_footer.php";?>