<?php require "_header.php"; 
 require "data/combo_data.php"; 

?>
<link href="jquery/ux/css/superboxselect.css" rel="Stylesheet" type="text/css" /> 
<script type="text/javascript" src="jquery/ux/SuperBoxSelect.js"></script>

<script type="text/javascript">
var Receipt_Fields = [
	{name:'receipt_id',type: 'int'},
	{name:'receipt_no'},
	{name:'receipt_date'},
	{name:'customer_id'},
	{name:'invoice_no'},
	{name:'amount'},
	{name:'payment_mode'},
	{name:'pmode'},
	{name:'chequeno'},
	{name:'bankname'},
	{name:'branchname'},
	{name:'remark'},
	{name:'chk_date'},
	]

var labelArray;
labelArray = [  
	['Receipt No :<span class="comp">*</span>'],
	['Date:<span class="comp">*</span>'],
	['Customer :<span class="comp">*</span>'],
	['Invoice :<span class="comp">*</span>'],
	['Amount :'],
	['By Cheque ?'],
	['ChequeNo :'],
	['Bank Name :'],
	['Branch :'],
	['Remark :']];

var Receiptstore = new Ext.data.JsonStore({
    url: 'data/receipt-grid-data.php',
    root: 'data',
    fields: Receipt_Fields,
    autoLoad: true
});
var finalamount = 0;
Ext.onReady(function(){
	Ext.QuickTips.init();
	var colModel = new Ext.grid.ColumnModel([
	{
		header: "RecNo",
		width: 50,
		dataIndex: 'receipt_no',
		sortable: true  		
	},
	{
		header: "Date",
		width: 70,
		dataIndex: 'receipt_date',
		sortable: true  		
	},{
		header: "Amount",
		width: 80,
		dataIndex: 'amount',
		sortable: true  		
	},
	
	{
		header: "Mode",
		width: 50,
		dataIndex: 'pmode',
		sortable: true  
	}]);
	
	var grid = new Ext.grid.GridPanel({
   		store: Receiptstore,
        cm: colModel,
        sm: new Ext.grid.RowSelectionModel({
        	singleSelect: true,
            listeners: {
            	rowselect: function(sm, row, rec) {
					Ext.getCmp('btnSave').disable();
					Ext.getCmp('amount').reset();
					finalamount = 0;
                	Ext.getCmp("receiptform").getForm().loadRecord(rec);
					 invoiceno_store.load({params:{
									customer_id :rec.data.customer_id
									},
									'callback':function(){
										Ext.getCmp("invoicenos1").clearValue();
										Ext.getCmp("invoicenos1").setRawValue(rec.data.invoice_no);
										console.log(rec.data.invoice_no);
										}});
					
					
					if(rec.data.payment_mode == true)
						Ext.getCmp('cash').setValue(true);
					else
						Ext.getCmp('cash').setValue(false);
					
					
					Ext.getCmp('btnDelete').enable();
					
				}
         	}
   		}),
		height: 370,
		width:270,
		frame:true,
		border: true
 	});
	
	var receipt_id = new Ext.form.TextField({
		name: 'receipt_id',
		id:'receipt_id',
		value:'0',
		hidden:true
	});
	
	var receipt_no = new Ext.form.NumberField({
		name: 'receipt_no',
		id:'receipt_no',
		width: 280,	
		labelSeparator:'',
		fieldLabel:labelArray[0],
		allowBlank:false,
		readOnly:true
	});
	
	
	
	var receipt_date = new Ext.form.DateField({
	id: 'receipt_date',
	name: 'receipt_date',
	labelSeparator:'',
	width: 280,	
	fieldLabel:labelArray[1],
	format:'d/m/Y',
	allowBlank:false,
	enableKeyEvents: true,
	value:new Date()
	});
	
	var chk_date = new Ext.form.DateField({
	id: 'chk_date',
	name: 'chk_date',
	labelSeparator:'',
	width: 280,	
	disabled:true,
	fieldLabel:'Chk Date:<span class="comp">*</span>',
	format:'d/m/Y',
	allowBlank:false,
	enableKeyEvents: true,
	value:new Date()
	});
	
	
 var Customer_Fields = [
	{name:'customer_id',type: 'int'},
	{name:'customer_name'}]


	var customerstore = new Ext.data.JsonStore({
	url: 'data/getcustomer-data.php',
	root: 'data',
	fields: Customer_Fields,
	autoLoad: true
	});	

	
		
	var customer_name1 = new Ext.form.ComboBox({
		name: 'customer_name1',
		id:'customer_name1',
		labelSeparator:'',	
		fieldLabel :labelArray[2],
		forceSelection:true,				
		displayField:'customer_name',
		hiddenName: 'customer_id',
		valueField:'customer_id',
		width: 280,	
		allowBlank:false,
		triggerAction:'all',
		editable:true,
		minChars: 3,
		typeAhead: false,
		lastQuery:'',
		emptyText:'----SELECT----',
		store: customerstore,
		listeners: {
            	'select': function(cmb, rec, idx) 
					{
				Ext.getCmp("invoicenos1").clearValue();
				Ext.getCmp("amount").reset();
				Ext.getCmp("cash").reset();
				Ext.getCmp("chequeno").reset();
				Ext.getCmp("bankname").reset();
				Ext.getCmp("branchname").reset();
				Ext.getCmp("remark").reset();
				 invoiceno_store.load({params:{
									customer_id :cmb.getValue()
									}});
					}
					}
		
		});
	
	
	
	
	
	 var Invoice_Fields = [
	{name:'invoice_no',type: 'int'},
	{name:'dueamount',type: 'int'}]


	var invoiceno_store = new Ext.data.JsonStore({
	url: 'data/invoicenos-grid-data.php',
	root: 'data',
	fields: Invoice_Fields,
	autoLoad: false
	});	
   
	 
	var invoicenos1 = new Ext.ux.form.SuperBoxSelect({
		allowBlank:false,
		msgTarget: 'title',
		id:'invoicenos1',
		name:'invoicenos1',
		labelSeparator:'',
		fieldLabel:labelArray[3],
		width:280,
		resizable: false,
		autoScroll:false,
		allowBlank:true,
		store: invoiceno_store,
		mode: 'local',
		editable:false,
		displayField: 'invoice_no',
		displayFieldTpl: '{invoice_no}',
		valueField: 'invoice_no',
		navigateItemsWithTab: false,
		listeners: {
            	'additem': function(cmb, rec, idx) 
					{
						var dueamt=idx.data.dueamount;
						var total=Ext.getCmp('amount').getValue();
						Ext.getCmp('amount').setValue(total + dueamt);
						finalamount = finalamount +dueamt;
						console.log('finalamount add='+finalamount);
					},
				'removeitem':function(cmb, rec, idx){
					//alert(idx.data.dueamount);
					
					var dueamt=idx.data.dueamount;					
					var total=Ext.getCmp('amount').getValue();
					Ext.getCmp('amount').setValue(total - dueamt);
					finalamount = finalamount - dueamt;
					console.log('finalamount rem='+finalamount);
				}
		}
		
	});

	var invoicenos = new Ext.form.TextField({
		name: 'invoicenos',
		id:'invoicenos',
		hidden:true
	});

	var amount = new Ext.form.NumberField({
		name: 'amount',
		id:'amount',
		width: 280,	
		labelSeparator:'',
		fieldLabel:labelArray[4],
		allowBlank:false,
		value:0
	});
	
	
	var cash = new Ext.form.Checkbox({
	name:'cash',
	id:'cash',
	tabIndex:9,
	labelSeparator:'',	
	fieldLabel :labelArray[5]
});

var chequeno = new Ext.form.NumberField({
	name: 'chequeno',
	id:'chequeno',
	labelSeparator:'',	
	tabIndex:10,
	fieldLabel :labelArray[6] ,
	allowNegative: false,
	allowDecimals: false,
	disabled:true,
	width: 280,
	autoCreate: {tag: 'input', type: 'text',  maxlength: '15'},
	allowBlank:true
});

var banknamestore = new Ext.data.ArrayStore({		
	fields: ['bankname'],
	data:Ext.combo.bankname
});

var bankname = new Ext.form.ComboBox({
	name: 'bankname',
	id:'bankname',
	labelSeparator:'',	
	fieldLabel :labelArray[7] ,
	displayField:'bankname',
	triggerAction:'all',
	hideTrigger: true,
	tabIndex:11,
	disabled:true,
	store: banknamestore,
	mode:'local',
	width: 280,
	selectOnFocus: true,
	allowBlank:true
});
	
	var branchname = new Ext.form.TextField({
	name: 'branchname',
	id:'branchname',
	width: 280,	
	labelSeparator:'',
	fieldLabel:labelArray[8],
	disabled:true,
	allowBlank:true
	});
		
	var remark = new Ext.form.TextArea({
		name: 'remark',
		id:'remark',
		width:280,	
		labelSeparator:'',
		fieldLabel:labelArray[9],
		allowBlank:true
	});
	
	

	var receiptform = new Ext.FormPanel({	
		id: 'receiptform',
		frame: true,
		//autoHight:true,	
		url: 'save-receipt.php',
    	labelAlign: 'left',
  		bodyStyle:'padding:5px',
   		layout: 'column',
    	items: [
			{
				columnWidth: .57,
				xtype: 'fieldset',
				labelWidth:75,
				autoHeight: true,
				layout: 'form',
				bodyStyle: Ext.isIE ? 'padding:0 0 3px 1px;' : 'padding:2px 1px;',
				border: false,
				items: [
						receipt_no,receipt_date,customer_name1,invoicenos1,invoicenos,amount,cash,chequeno,bankname,branchname,remark,chk_date,receipt_id, {
								xtype:'checkbox',
								id: 'headerflag',
								name: 'headerflag',
								style:"margin-bottom:4px",
								fieldLabel: 'Print Header',
								checked :false
							}
				]
			},{
				columnWidth: .43,
				xtype: 'fieldset',
				labelWidth: 85,
				defaults: {border:false},
				defaultType: 'textfield',
				autoHeight: true,
				bodyStyle: Ext.isIE ? 'padding:0 0 2px 1px;' : 'padding:2px 1px;',
				border: false,
				items:[grid]
			}
               ]
    });
	
	var ReceiptWindow = new Ext.Window({
		title: 'Receipt',
		width:675,
		//autoHight:true,		
		height:460,	
		frame: true,
		border: false,
		modal:true,
		layout: 'fit',
		closeAction:'close',
		 constrain: true, 
		collapsible:true,
		items: receiptform,
		buttons:[
			{
            	text: 'View Receipt',
				iconCls: 'icon-view',
				tabIndex:8,
				handler: function(){
					     if(Ext.getCmp('headerflag').getValue() == true)						
							{window.location='report-receipt-result.php?receipt_no=' + Ext.getCmp('receipt_no').getValue();}
				 		else
							{window.location='report-receipt-result1.php?receipt_no=' + Ext.getCmp('receipt_no').getValue();
								}
					}
        	},
			{
				text: 'Save', 
				name:'btnSave',
				iconCls:'icon-save',
				id:'btnSave',
				handler: function(){
			  if(Ext.getCmp('amount').getValue() > finalamount)
			    {
					Ext.MessageBox.show({		title: 'Error',
												msg: 'Sorry! you cannot take more amount than current invoice bill amount.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});	
					Ext.getCmp('amount').setValue(finalamount);
					return;						
				}
			else
			  {	
					if(Ext.getCmp('receipt_id').getValue() != "0")
					 {
						 Ext.getCmp('invoicenos').setValue(Ext.getCmp('invoicenos1').getRawValue());
					 }
					 else
					   {Ext.getCmp('invoicenos').setValue(Ext.getCmp('invoicenos1').getValue());
					   }
					if(Ext.getCmp('amount').getValue() >0 && Ext.getCmp('amount').getValue() != "")
					{
					receiptform.getForm().submit({
						
										waitMsg: 'Saving...',
										success: function () {	
											Ext.MessageBox.confirm('Confirmation',' Do U Want To Print Receipt..?',printreceipt);
																			
										},
										failure: function () {
											Ext.MessageBox.show({
												title: 'Error',
												msg: 'Please fill up complete form.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});									
										}
									});	
					}
					else
					{
						Ext.MessageBox.show({
												title: 'Error',
												msg: 'Please Enter Proper Amount.',
												buttons: Ext.MessageBox.OK,
												icon:Ext.MessageBox.ERROR
											});	
						Ext.getCmp('amount').focus(true,500);					
					}
								
			  }
					}
						
			},
			{
				text: 'Delete',
				iconCls:'remove',
				disabled:true,
				id: 'btnDelete',
				name: 'btnDelete',
				hidden:true,// temp hide this becoz code remaining while deleting rec there has to be refund like process
				handler: function(){
					/*var group_id=Ext.getCmp('group_id').getValue();
					Ext.Ajax.request({
						loadMask: true,
						sync : true,
						url: 'data/getRelatedRecord.php',
						params: {action:'group',group_id:group_id},
						success: function(resp){
							var str = "";
							resbool=resp.responseText;
							if(resbool != 0){
								str = "It has related record in " + resbool +"Please Delete This Record First";
								Ext.MessageBox.show({
									title: 'Error',
									msg: str,
									buttons: Ext.MessageBox.OK,
									icon:Ext.MessageBox.ERROR
								});			
							}
							else{
								*/
								Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
								/*
							}
						}				
					});*/
				}
			},
			{
				text: 'Reset', 
				name:'btnreset',
				iconCls:'icon-reset',
				id:'btnreset',
				handler: function (){
					receiptform.getForm().reset();
					grid.getSelectionModel().clearSelections();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('customer_name1').focus(true,500);
					Ext.getCmp('receipt_id').setValue('0');	
					Ext.getCmp('btnSave').enable();
					Receiptstore.load();
					get_ReceiptNo();
					finalamount=0;
					
					
				}
			},
			{
				text:'Close', 
				name:'btnclose',
				iconCls:'icon-close',
				id:'btnclose',
				handler: function (){
					ReceiptWindow.close();
				}
			}
		]
	});
	
	
	function printreceipt(btn){
	if(btn=='yes')
	{			  			
	
		 if(Ext.getCmp('headerflag').getValue() == true)						
				{window.location='report-receipt-result.php?receipt_no=' + Ext.getCmp('receipt_no').getValue();}
		 else
				{window.location='report-receipt-result1.php?receipt_no=' + Ext.getCmp('receipt_no').getValue();}
				
	}
	else
	{
		grid.getSelectionModel().clearSelections();
		Ext.getCmp('btnDelete').disable();	
		Receiptstore.load();
		receiptform.getForm().reset();	
		get_ReceiptNo();
		Ext.getCmp('customer_name1').focus(true,500);
		finalamount=0;
	}}		
	
	
	
	function deleterecord(btn){
    	if(btn=='yes'){
	   		receiptform.getForm().submit({
				waitMsg: 'Saving...',
				params:{act:'del'},
				success: function () {	
					receiptform.getForm().reset();
					Ext.getCmp('btnDelete').disable();
					Ext.getCmp('receipt_id').setValue('0');	
					Receiptstore.load();	
					get_ReceiptNo();	
					finalamount=0;						
				}
			});
	 	}  
  	}
Ext.getCmp('cash').on('check',function (){
	if(Ext.getCmp('cash').getValue()==false){
		Ext.getCmp('bankname').reset();
		Ext.getCmp('chequeno').reset();
		Ext.getCmp('branchname').reset();
		Ext.getCmp('chk_date').reset();
		Ext.getCmp('bankname').disable();
		Ext.getCmp('chequeno').disable();
		Ext.getCmp('branchname').disable();
		Ext.getCmp('chk_date').disable();
		
	} 
	else{
        Ext.getCmp('bankname').enable();
		Ext.getCmp('chequeno').enable();
		Ext.getCmp('branchname').enable();
		Ext.getCmp('chk_date').enable();
	}
}); 	
function get_ReceiptNo(){	
	Ext.Ajax.request({
		loadMask: true,
		sync : true,
		url: 'data/getReceiptno.php',
		success: function(resp){
			var value=Ext.decode(resp.responseText).count;
			if(value==','){
			inwardform.getForm().reset();
			Ext.getCmp('customer_name1').focus(true,500);
			Ext.MessageBox.show({
					title: 'Error',
					msg: 'Receipt No Not Found for this number.',
					buttons: Ext.MessageBox.OK,
					icon:Ext.MessageBox.ERROR
				});
			} else{
				var arr_data = new Array();
				arr_data =  value.split(",");
				Ext.getCmp('receipt_no').setValue(arr_data[0]);
				
			}					
		}				
	});	}
	
	var varWidth =  (screen.width - ReceiptWindow.width)/2;
	ReceiptWindow.setPosition(varWidth,((screen.height/4)-85));
	ReceiptWindow.show();
	get_ReceiptNo();
	//Ext.getCmp('module_name1').focus(true,500);
});
</script>
<div id="main-content" align="left"></div>
<?php require "_footer.php";?>