<?php
require "_header.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
?>
<?php
if ($_REQUEST["ID"] != "" && isset($_REQUEST["ID"])) {
	$feesreceive_id = Escape($_REQUEST["ID"]);
	$mode = "edit";
	if ($EDIT == "false") {
		redirectToPage();
	}
	$limit = 1;
	$start = 0;
	$condition = " and feesreceive_id = '$feesreceive_id' ";
	$order_field = "";
	$order_type = "";

	$objStudentFeesRecMgmt = new StudentFeesRecMgmt();
	$out = $objStudentFeesRecMgmt->getAllFeesReceipt($condition);
	if (dbRows($out)) {
		$row = dbArray($out);
	}
} else {
	$mode = "add";
	if ($ADD == "false") {
		redirectToPage();
	}
	$feesreceive_id = 0;
}
?>
<div id="main-content">
	<script type="text/javascript" src="jquery/ux/CheckColumn.js"></script>
	<link href="jquery/ux/css/superboxselect.css" rel="Stylesheet" type="text/css" />
	<link href="jquery/ux/css/summary.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="jquery/ux/SuperBoxSelect.js"></script>
	<script type="text/javascript" src="jquery/ux/GroupSummary.js"></script>
	<script type="text/javascript" src="jquery/ux/RowExpander.js"></script>
	<?php if ($_SESSION['VedantnrCollegeId'] == '1') {
	}
	?>
	<style type="text/css">
		.x-grid3-row-over .x-grid3-cell-inner {
			font-weight: bold;
		}

		.row-preview {
			padding: 2px 2px 5px 25px;
			border-top: dotted 1px #CCCCCC;
		}

		.tplmain {
			width: 170px;
		}

		.tplleft {
			width: 120px;
		}

		.tplright {
			width: 50px;
			text-align: right;
		}

		.tblstyle {
			text-align: center;
			background-color: #CED9E7;
			border: 1px solid #99BBE8;
			color: #000;
		}

		.tblstyle th {
			text-align: center;
			font-weight: bold;
			color: #15428B;
		}

		.tblstyle td {
			padding: 2px;
		}
	</style>
	<script type="text/javascript">
		Ext.onReady(function () {
			Ext.QuickTips.init();
			var student_id, student_name;
			var labelArray;
			labelArray = [
				['Enrollment No<span class="comp">*</span> '],
				['Course<span class="comp">*</span> '],
				['Batch Year<span class="comp">*</span> '],
				['Course Year<span class="comp">*</span> '],
				['Semester<span class="comp">*</span> '],
				['Student<span class="comp">*</span> '],
				['Total Amount '],
				['Discount '],
				['Receipt No '],
				['Receipt Date<span class="comp">*</span> '],
				['Paid by cheque '],
				['Cheque No<span class="comp">*</span> '],
				['Bank Name<span class="comp">*</span> '],
				['Comment Show '],
				['Comment ']
			];


			var feesreceive_id = '<?php echo $feesreceive_id ?>';
			var formMode = '<?php echo $mode; ?>';
			var branchstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'branch_name'],
				data: Ext.semester.branch
			});

			var batchyearstore = new Ext.data.ArrayStore({
				fields: ['year_id', 'year_name'],
				data: Ext.semester.yearname
			});

			var semesterstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'semester_id', 'semester_name'],
				data: Ext.student.semester
			});

			var yearstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
				data: Ext.year.year
			});

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

			var studentstore = new Ext.data.JsonStore({
				url: 'data/getstudent-feesrecive.php',
				root: 'data',
				totalProperty: 'total',
				fields: [{
					name: 'student_id',
					type: 'int'
				}, {
					name: 'student_name',
					type: 'string'
				}, {
					name: 'student_gturegno',
					type: 'string'
				}]
			});

			var DebitDetail = new Ext.form.Label({
				name: 'DebitDetail',
				id: 'DebitDetail'
			});

			/* START GRID */
			var summary = new Ext.grid.GroupSummary();

			var feeshead_fields = [{
				name: 'student_id',
				type: 'int'
			}, {
				name: 'selected',
				type: 'bool'
			}, {
				name: 'head_id',
				type: 'int'
			},
			{
				name: 'head_name',
				type: 'string'
			}, {
				name: 'branch_id',
				type: 'int'
			}, {
				name: 'branch_name',
				type: 'string'
			}, {
				name: 'year_id',
				type: 'int'
			}, {
				name: 'year_name',
				type: 'string'
			}, {
				name: 'branchyear_id',
				type: 'int'
			}, {
				name: 'branchyear_name',
				type: 'string'
			}, {
				name: 'semester_id',
				type: 'int'
			}, {
				name: 'semester_name',
				type: 'string'
			}, {
				name: 'fees_amount',
				type: 'float'
			}, {
				name: 'fees_received_amount',
				type: 'float'
			}, {
				name: 'fees_due_amount',
				type: 'float'
			},
			{
				name: "fees_type",
				type: "string"
			}]

			var reader = new Ext.data.JsonReader({
				idProperty: 'branch_name',
				root: 'data',
				fields: feeshead_fields
			});

			var feeshead_grid_store = new Ext.data.GroupingStore({
				url: 'data/view-feesreceive-data.php',
				reader: reader,
				successProperty: 'success',
				sortInfo: {
					field: 'student_id',
					autoLoad: true,
					direction: 'ASC'
				},
				groupField: 'fees_type'
			});

			var feeshead_colModel = new Ext.grid.ColumnModel([
				new Ext.grid.RowNumberer,
				{
					id: 'head_id',
					dataIndex: 'head_id',
					hidden: true
				},
				{
					id: 'fees_type',
					dataIndex: 'fees_type',
					hidden: true
				},
				{
					id: 'branch_name',
					header: "Course Name",
					width: 190,
					dataIndex: 'branch_name',
					align: 'left',
					sortable: true,
					hidden: true
				}, {
					header: "Branch ID",
					dataIndex: 'branch_id',
					sortable: true,
					hidden: true
				}, {
					header: "Semester ID",
					dataIndex: 'semester_id',
					sortable: true,
					hidden: true

				}, {
					xtype: 'checkcolumn',
					id: 'present',
					dataIndex: 'selected',
					width: 25
				}, {
					header: "Head Name",
					dataIndex: 'head_name',
					sortable: true,
					width: 170,
					summaryType: 'count',
					summaryRenderer: function (v, params, data) {
						return ((v === 0 || v > 1) ? + v + ' Fees' : '1 Fees');
					}
				}, {
					header: 'Receiveble Amount',
					id: 'fees_amount',
					dataIndex: 'fees_amount',
					width: 110,
					align: 'left',
					summaryType: 'sum',
					summaryRenderer: function (v, params, data) {
						return v;
					},
					groupName: 'fees_amount'
				}, {
					header: 'Received Amount',
					id: 'fees_received_amount',
					dataIndex: 'fees_received_amount',
					width: 100,
					align: 'left',
					summaryType: 'sum',
					summaryRenderer: function (v, params, data) {
						return v;
					},
					groupName: 'fees_received_amount'
				}, {
					header: 'Due Amount',
					id: 'fees_due_amount',
					dataIndex: 'fees_due_amount',
					width: 100,
					align: 'right',
					renderer: change,
					editor: {
						xtype: 'numberfield',
						allowBlank: true,
						style: 'text-align: right;'
					},
					summaryType: 'sum',
					summaryRenderer: function (v, params, data, record) {
						Ext.getCmp('receive_amt').setValue(v);
						return v;
					},
					groupName: 'fees_due_amount'
				}
			]);
///For Edit Col Model////
var feeshead_editcolModel = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer,
			{
					id: 'head_id',
					dataIndex: 'head_id',
					hidden: true
				},
				{
					id: 'fees_type',
					dataIndex: 'fees_type',
					hidden: true
				},
				{
					id: 'branch_name',
					header: "Course Name",
					width: 190,
					dataIndex: 'branch_name',
					align: 'left',
					sortable: true,
					hidden: true
				}, {
					header: "Branch ID",
					dataIndex: 'branch_id',
					sortable: true,
					hidden: true
				}, {
					header: "Semester ID",
					dataIndex: 'semester_id',
					sortable: true,
					hidden: true

				}, {
					xtype: 'checkcolumn',
					id: 'present',
					dataIndex: 'selected',
					width: 25
				}, {
					header: "Head Name",
					dataIndex: 'head_name',
					sortable: true,
					width: 170,
					summaryType: 'count',
					summaryRenderer: function (v, params, data) {
						return ((v === 0 || v > 1) ? + v + ' Fees' : '1 Fees');
					}
				}, {
					header: 'Receiveble Amount',
					id: 'fees_amount',
					dataIndex: 'fees_amount',
					width: 110,
					align: 'left',
					summaryType: 'sum',
					summaryRenderer: function (v, params, data) {
						return v;
					},
					groupName: 'fees_amount'
				}, {
					header: 'Received Amount',
					id: 'fees_received_amount',
					dataIndex: 'fees_received_amount',
					width: 100,
					align: 'left',
					summaryType: 'sum',
					editor: {
					xtype: 'numberfield',
					allowBlank: false,
					style: 'text-align: right;'
				},
					summaryRenderer: function (v, params, data) {
						return v;
					},
					groupName: 'fees_received_amount'
				}
		]);

		///////////////////////////
			var grid = new Ext.grid.EditorGridPanel({
				ds: feeshead_grid_store,
				cm: feeshead_colModel,
				height: 280,
				title: '',
				border: true,
				loadMask: true,
				columnLines: true,
				stripeRows: true,
				clicksToEdit: 1,
				enableHdMenu: false,
				//	tbar: [{ text: 'Fees Types' }],
				view: new Ext.grid.GroupingView({
					forceFit: true,
					showGroupName: false,
					enableNoGroups: false,
					hideGroupedColumn: true
				}),
				plugins: summary
			});

			grid.getStore().on('update', function (store, record, action) {
				var receive_amt = Ext.getCmp('receive_amt').getValue();
				var temp = record.data.fees_amount - record.data.fees_received_amount;
				var temp1 = record.data.fees_due_amount;

				if (record.data.selected == true) {
					if (temp1 > 0) {
						record.set('fees_due_amount', temp1);
					} else {
						record.set('fees_due_amount', temp);
					}
				} else {
					record.set('fees_due_amount', 0);
				}
			});

			grid.on('render', function () {
				grid.tip = new Ext.ToolTip({
					view: grid.getView(),
					target: grid.getView().mainBody,
					delegate: '.x-grid3-row',
					trackMouse: true,
					renderTo: document.body,
					listeners: {
						beforeshow: function updateTipBody(tip) {
							tip.body.dom.innerHTML = "Click On Due Amount Column To Edit Amount.";
						}
					}
				});
			});
			//jignal
			//jignal
			grid.on("validateedit", function notmaximum(e) {
				var value = e.value;//get edited value
				var currentval = e.record.get(e.field);//get the current value
				if (e.field == "fees_due_amount") {
					if (value > currentval) {
						e.cancel = true; // cancel the editing
						Ext.MessageBox.show({
							title: 'Error',
							msg: 'Due Amount should not be Greater than original amount',
							buttons: Ext.MessageBox.OK,
							icon: Ext.MessageBox.ERROR
						});
					}
				}
			});
			Ext.override(Ext.Editor, {
				onBlur: function () {
					if (this.allowBlur === true && this.editing) {
						this.completeEdit();
					}
				}
			});
			/* END GRID */
			/* START BELOW GRID */
			var rowExpander = new Ext.grid.RowExpander({
				tpl: new Ext.Template(
					'<div class="row-preview "><p>{feeshead_detail}</p></div>'
				)
			});

			var receivedfees_fields = [{
				name: 'feesreceive_id',
				type: 'int'
			}, {
				name: 'branch_id',
				type: 'int'
			}, {
				name: 'year_id',
				type: 'int'
			}, {
				name: 'branchyear_id',
				type: 'int'
			}, {
				name: 'semester_id',
				type: 'int'
			}, {
				name: 'year_name',
				type: 'string'
			}, {
				name: 'branchyear_name',
				type: 'string'
			}, {
				name: 'semester_name',
				type: 'string'
			}, {
				name: 'receipt_no',
				type: 'int'
			}, {
				name: 'receipt_date',
				type: 'date',
				dateFormat: 'Y-m-d'
			}, {
				name: 'discount',
				type: 'float'
			}, {
				name: 'receive_amt',
				type: 'float'
			}, {
				name: 'feeshead_detail',
				type: 'string'
			}]

			var reader1 = new Ext.data.JsonReader({
				idProperty: 'feesreceive_id',
				root: 'data1',
				fields: receivedfees_fields
			});

			var receivedfees_grid_store = new Ext.data.Store({
				url: 'data/view-receivedfees-data.php',
				reader: reader1
			});

			var receivedfees_colModel = new Ext.grid.ColumnModel([
				rowExpander,
				{
					header: 'Year',
					id: 'year_id',
					align: 'right',
					dataIndex: 'year_name',
					width: 80
				},
				{
					header: "Course Year",
					id: 'beanchyear_name',
					dataIndex: 'beanchyear_name',
					width: 60,
					align: 'center'
				}, {
					header: "Semester",
					id: 'semester_name',
					dataIndex: 'semester_name',
					width: 60,
					align: 'center'
				}, {
					header: "Receipt No",
					dataIndex: 'receipt_no',
					width: 80,
					align: 'center'
				}, {
					header: "Receipt Date",
					dataIndex: 'receipt_date',
					renderer: Ext.util.Format.dateRenderer('d/m/Y'),
					width: 80
				}, {
					header: 'Discount',
					id: 'discount',
					align: 'right',
					dataIndex: 'discount',
					width: 90
				}, {
					header: 'Rec. Amount',
					id: 'receive_amt',
					align: 'right',
					dataIndex: 'receive_amt',
					width: 90
				},
				{
					header: "Duplicate Receipt",
					width: 120,
					renderer: function (value, metaData, record, rowIndex, colIndex, store) {
						return '<a href="duplicate-fees-receipt-report.php?feesreceive_id=' + record.get('feesreceive_id') + '">Print</a>';

					}
				}
			]);

			var grid1 = new Ext.grid.GridPanel({
				ds: receivedfees_grid_store,
				cm: receivedfees_colModel,
				//	store: feeshead_grid_store,
				loadMask: true,
				columnLines: true,
				stripeRows: true,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect: true,
					listeners: {
						rowselect: function (sm, row, rec) {
							Ext.getCmp("receiveform").getForm().loadRecord(rec);
							Ext.getCmp('discount').enable();

							if (flgEdit == "true") {
								Ext.getCmp('btnSave').enable();
							} else {
								Ext.getCmp('btnSave').disable();
								alert('You do not have access to edit the record.');
							}

							feeshead_grid_store.removeAll();
							feeshead_grid_store.load({
								params: {
									task: 'getdata',
									student_id: Ext.getCmp('student_name').getValue(),
									branch_id: Ext.getCmp('branch_name').getValue(),
									semester_id: Ext.getCmp('semester_name').getValue(),
									recipt_no: Ext.getCmp('receipt_no').getValue(),
									head_name: Ext.getCmp('head_name').getValue(),
									student_gturegno: Ext.getCmp('student_gturegno').getValue(),
								}
							});
						}
					}
				}),
				height: 180,
				title: '',
				border: true,
				tbar: [{ text: 'Fees Receipt' }],
				enableHdMenu: false,
				plugins: rowExpander
			});

			/* END BELOW GRID */

			var receiveform = new Ext.FormPanel({
				id: 'receiveform',
				frame: true,
				url: 'save-feesrecive.php?ID=<?php print $feesreceive_id; ?>',
				labelAlign: 'left',
				bodyStyle: 'padding:5px',
				layout: 'column',
				items: [{
					columnWidth: 0.40,
					xtype: 'fieldset',
					id: 'feeshead_add_edit_form',
					labelWidth: 100,
					title: '',
					defaults: { border: false },
					autoHeight: true,
					border: false,
					items: [{
						xtype: 'textfield',
						name: 'student_gturegno',
						id: 'student_gturegno',
						tabIndex: 2,
						fieldLabel: labelArray[0],
						allowBlank: true,
						allowNegative: false,
						allowDecimals: false,
						width: 210,
						listeners: {
							'blur': function () {
								var gturegno = this.getValue();
								if (gturegno != "") {
									Ext.Ajax.request({
										loadMask: true,
										sync: true,
										url: 'data/getgtunodata.php',
										params: { gturegno: gturegno },
										success: function (resp) {
											var value = Ext.decode(resp.responseText).count;
											if (value == ',,,') {
												grid.stopEditing();
												Ext.getCmp('student_name').reset();
												Ext.getCmp('student_gturegno').reset();
												Ext.getCmp('cheque_no').reset();
												Ext.getCmp('cash').reset();
												Ext.getCmp('bank_name').reset();
												Ext.getCmp('receive_amt').reset();
												Ext.getCmp('comment').reset();
												Ext.getCmp('receipt_date').reset();
												Ext.getCmp('receipt_no').reset();
												Ext.getCmp('discount').reset();


												feeshead_grid_store.removeAll();
												receivedfees_grid_store.removeAll();
												Ext.MessageBox.show({
													title: 'Message',
													msg: 'Enrollment No ' + gturegno + ' Does Not Exist...',
													buttons: Ext.MessageBox.OK,
													icon: Ext.MessageBox.INFO
												});
											} else {
												var arr_data = new Array();
												arr_data = value.split(",");
												student_id = arr_data[0];
												student_name = arr_data[1];
												branch_id = arr_data[2];
												year_id = arr_data[3];
												branchyear_id = arr_data[4];
												semester_id = arr_data[5];

												Ext.getCmp('student_name').setValue(student_id);
												Ext.getCmp('student_name').setRawValue(student_name);
												Ext.getCmp('branch_name').setValue(branch_id);
												Ext.getCmp('year_name').setValue(year_id);
												Ext.getCmp('branchyear_name').setValue(branchyear_id);
												Ext.getCmp('semester_name').setValue(semester_id);
												Ext.getCmp('comment').setValue(comment);

												get_feeshead_data();
												get_receivedfees_data();
											}
										}
									});
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'branch_name',
						id: 'branch_name',
						hiddenName: 'branch_hidden',
						fieldLabel: labelArray[1],
						displayField: 'branch_name',
						valueField: 'branch_id',
						editable: true,
						tabIndex: 4,
						width: 210,
						triggerAction: 'all',
						mode: 'local',
						allowBlank: false,
						store: branchstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								Ext.getCmp('semester_name').reset();
								Ext.getCmp('student_name').reset();
								Ext.getCmp('student_gturegno').reset();
								Ext.getCmp('cheque_no').reset();
								Ext.getCmp('cash').reset();
								Ext.getCmp('bank_name').reset();
								Ext.getCmp('receive_amt').reset();
								Ext.getCmp('comment').reset();
								Ext.getCmp('receipt_date').reset();
								Ext.getCmp('receipt_no').reset();
								Ext.getCmp('discount').reset();

								studentstore.removeAll();
								grid.stopEditing();
								studentstore.removeAll();
								grid.stopEditing();
								semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
								yearstore.filter('branch_id', cmb.getValue(), false, true, true);
								feeshead_grid_store.removeAll();
								receivedfees_grid_store.removeAll();
								var semesterCombo = Ext.getCmp('semester_name');
								if (cmb.getValue() == 3 || cmb.getValue() == 5) {
									semesterCombo.enable();
								} else {
									semesterCombo.disable();
									semesterCombo.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'year_name',
						id: 'year_name',
						hiddenName: 'year_hidden',
						fieldLabel: labelArray[2],
						displayField: 'year_name',
						valueField: 'year_id',
						lastQuery: '',
						triggerAction: 'all',
						mode: 'local',
						width: 210,
						tabIndex: 5,
						allowBlank: true,
						store: batchyearstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								if (Ext.getCmp('branch_name').getValue() != "") {
									Ext.getCmp('semester_name').reset();
									studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'year_id': Ext.getCmp('year_name').getValue() } });
								} else {
									this.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'branchyear_name',
						id: 'branchyear_name',
						hiddenName: 'branchyear_hidden',
						tabIndex: 6,
						width: 210,
						fieldLabel: labelArray[3],
						displayField: 'branchyear_name',
						valueField: 'branchyear_id',
						editable: true,
						triggerAction: 'all',
						mode: 'local',
						lastQuery: '',
						allowBlank: true,
						store: yearstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								var branch = Ext.getCmp('branch_name').getValue();
								var branchyear = Ext.getCmp('branchyear_name').getValue();
								if (Ext.getCmp('branch_name').getValue() != "") {
									get_ReceiptNo();
									grid.stopEditing();
									feeshead_grid_store.removeAll();
									receivedfees_grid_store.removeAll();
									Ext.getCmp('student_name').reset();
									Ext.getCmp('student_gturegno').reset();
									Ext.getCmp('cheque_no').reset();
									Ext.getCmp('cash').reset();
									Ext.getCmp('bank_name').reset();
									Ext.getCmp('receive_amt').reset();
									Ext.getCmp('comment').reset();
									Ext.getCmp('receipt_date').reset();
									Ext.getCmp('receipt_no').reset();

									studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'year_id': Ext.getCmp('year_name').getValue(), 'branchyear_id': Ext.getCmp('branchyear_name').getValue() } });
								} else {
									alert('Select Branch First')
									this.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'semester_name',
						id: 'semester_name',
						hiddenName: 'semester_hidden',
						tabIndex: 7,
						width: 210,
						fieldLabel: labelArray[4],
						displayField: 'semester_name',
						valueField: 'semester_id',
						disabled: true,
						triggerAction: 'all',
						mode: 'local',
						lastQuery: '',
						allowBlank: true,
						store: semesterstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								var branch = Ext.getCmp('branch_name').getValue();
								var semeseter = Ext.getCmp('semester_name').getValue();
								if (Ext.getCmp('branch_name').getValue() != "") {
									get_ReceiptNo();
									grid.stopEditing();
									feeshead_grid_store.removeAll();
									receivedfees_grid_store.removeAll();
									Ext.getCmp('student_name').reset();
									Ext.getCmp('student_gturegno').reset();
									Ext.getCmp('cheque_no').reset();
									Ext.getCmp('cash').reset();
									Ext.getCmp('bank_name').reset();
									Ext.getCmp('receive_amt').reset();
									Ext.getCmp('comment').reset();
									Ext.getCmp('receipt_date').reset();
									Ext.getCmp('receipt_no').reset();

									studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'year_id': Ext.getCmp('year_name').getValue(), 'semester_id': Ext.getCmp('semester_name').getValue() } });
								} else {
									alert('Select Branch First')
									this.reset();
								}
							}
						}
					}, {
						xtype: 'combo',
						name: 'student_name',
						id: 'student_name',
						hiddenName: 'student_hidden',
						fieldLabel: labelArray[5],
						displayField: 'student_name',
						valueField: 'student_id',
						editable: true,
						tabIndex: 8,
						width: 210,
						triggerAction: 'all',
						mode: 'local',
						allowBlank: true,
						forceSelection: false,
						store: studentstore,
						listeners: {
							'select': function (cmb, rec, idx) {
								if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('year_name').getValue() != "") {
									Ext.getCmp('student_gturegno').setValue(studentstore.getAt(idx).data.student_gturegno);
									get_feeshead_data();
									get_receivedfees_data();
									get_ReceiptNo();
									student_id = this.getValue();
									student_name = this.getRawValue();
								} else if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('branchyear_name').getValue() != "") {
									Ext.getCmp('student_gturegno').setValue(studentstore.getAt(idx).data.student_gturegno);

									get_feeshead_data();
									get_receivedfees_data();
									get_ReceiptNo();
									student_id = this.getValue();
									student_name = this.getRawValue();
								} else if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('semester_name').getValue() != "") {
									Ext.getCmp('student_gturegno').setValue(studentstore.getAt(idx).data.student_gturegno);

									get_feeshead_data();
									get_receivedfees_data();
									get_ReceiptNo();
									student_id = this.getValue();
									student_name = this.getRawValue();
								} else {
									alert('Select Branch And Semester First')
									this.reset();
								}
							},
							'focus': function (cmb, rec, idx) {
								student_id = this.getValue();
								student_name = this.getRawValue();
							},
							'blur': function (cmb, rec, idx) {
								this.setValue(student_id);
								this.setRawValue(student_name);
							}
						}
					}, {
						xtype: 'numberfield',
						name: 'receive_amt',
						id: 'receive_amt',
						fieldLabel: labelArray[6],
						width: 210,
						tabIndex: 9,
						allowBlank: true,
						allowNegative: false,
						maxValue: 99999.993,
						decimalPrecision: 0,
						autoCreate: { tag: 'input', type: 'text', maxlength: '7' }
						//,readOnly:true
					}, {
						xtype: 'numberfield',
						name: 'discount',
						id: 'discount',
						fieldLabel: labelArray[7],
						width: 210,
						tabIndex: 10,
						disabled: false,
						allowBlank: true,
						allowNegative: false,
						allowDecimals: true,
						maxValue: 99999.993,
						decimalPrecision: 4,
						autoCreate: { tag: 'input', type: 'text', maxlength: '8' },
						//,readOnly:true,
						listeners:
						{
							onBlur: function () {
								alert("hello");
							}
						}
					}, {
						xtype: 'numberfield',
						id: 'receipt_no',
						name: 'receipt_no',
						width: 210,
						tabIndex: 11,
						fieldLabel: labelArray[8],
						//readOnly:true
					}, {
						xtype: 'datefield',
						id: 'receipt_date',
						name: 'receipt_date',
						width: 210,
						tabIndex: 12,
						fieldLabel: labelArray[9],
						enableKeyEvents: true,
						allowBlank: true,
						maxValue: new Date(),
						value: new Date()
					}, {
						xtype: 'checkbox',
						name: 'cash',
						id: 'cash',
						tabIndex: 13,
						fieldLabel: labelArray[10]
					}, {
						xtype: 'numberfield',
						name: 'cheque_no',
						id: 'cheque_no',
						tabIndex: 14,
						fieldLabel: labelArray[11],
						allowNegative: false,
						allowDecimals: false,
						disabled: true,
						width: 210,
						autoCreate: { tag: 'input', type: 'text', maxlength: '15' },
						allowBlank: true
					}, {
						xtype: 'combo',
						name: 'bank_name',
						id: 'bank_name',
						fieldLabel: labelArray[12],
						displayField: 'bank_name',
						triggerAction: 'all',
						hideTrigger: true,
						tabIndex: 15,
						disabled: true,
						store: banknamestore,
						mode: 'local',
						width: 210,
						selectOnFocus: true,
						allowBlank: true
					}, {
						xtype: 'checkbox',
						name: 'comment_show',
						id: 'comment_show',
						tabIndex: 16,
						fieldLabel: labelArray[13]
					}, {
						xtype: 'textfield',
						id: 'comment',
						name: 'comment',
						width: 210,
						tabIndex: 17,
						disabled: true,
						fieldLabel: labelArray[14]
					}, {
						xtype: 'textfield',
						name: 'feesreceive_id',
						id: 'feesreceive_id',
						hidden: true
					}, DebitDetail
					]
				}, {
					columnWidth: 0.60,
					xtype: 'container',
					border: false,
					layout: 'column',
					defaultType: 'field',
					items: [{
						xtype: 'container',
						columnWidth: 1,
						items: [grid]
					}, {
						xtype: 'container',
						columnWidth: 1,
						items: [grid1]
					}]
				}]
			});

			function change(val) {
				if (val > 0) {
					return '<span style="color:red;">' + val + '</span>';
				}
				else if (val <= 0) {
					return '<span style="color:green;">' + val + '</span>';
				}
				return val;
			}

			function get_receivedfees_data() {
				var student_id = Ext.getCmp('student_name').getValue();
				//gtu no
				var student_gturegno = Ext.getCmp('student_gturegno').getValue();
				var branch_id = Ext.getCmp('branch_name').getValue();
				var year_id = Ext.getCmp('year_name').getValue();
				var branchyear_id = Ext.getCmp('branchyear_name').getValue();
				var semester_id = Ext.getCmp('semester_name').getValue();

				receivedfees_grid_store.removeAll();
				receivedfees_grid_store.load({
					params: {
						student_id: student_id,
						branch_id: branch_id,
						year_id: year_id,
						branchyear_id: branchyear_id,
						semester_id: semester_id,
						//discount: discount,
						student_gturegno: student_gturegno,
					}
				});
			}

			receiveform.getForm().findField('discount').on('blur', function () {
				var discount = receiveform.getForm().findField('discount').getValue();
				var amount = receiveform.getForm().findField('receive_amt').getValue();
				var t = 0;
				for (k = 0; k < feeshead_grid_store.getCount(); k++) {
					var record = feeshead_grid_store.getAt(k);
					t = t + record.data.fees_due_amount;
				}
				amount = t - discount;
				var total = amount - discount;
				Ext.getCmp('receive_amt').enable();
				Ext.getCmp('receive_amt').setValue(total);
			});

			function get_feeshead_data() {
		
				//feeshead_grid_store.removeAll();
				feeshead_grid_store.load({
					params: {
						task: 'getdata',
						student_id: Ext.getCmp('student_name').getValue(),
						branch_id: Ext.getCmp('branch_name').getValue(),
						year_id: Ext.getCmp('year_name').getValue(),
						branchyear_id: Ext.getCmp('branchyear_name').getValue(),
						semester_id: Ext.getCmp('semester_name').getValue(),
						receipt_no: Ext.getCmp('receipt_no').getValue(),
						student_gturegno: Ext.getCmp('student_gturegno').getValue(),
					}
				});
			}

			function get_ReceiptNo() {
				var branch_id = Ext.getCmp('branch_name').getValue();
				var year_id = Ext.getCmp('year_name').getValue();
				var branchyear_id = Ext.getCmp('branchyear_name').getValue();
				var semester_id = Ext.getCmp('semester_name').getValue();

				Ext.Ajax.request({
					loadMask: true,
					sync: true,
					params: { branch_id: branch_id, year_id: year_id, branchyear_id: branchyear_id, semester_id: semester_id },
					url: 'data/getReceiptno.php',
					success: function (resp) {
						var value = Ext.decode(resp.responseText).count;
						Ext.getCmp('receipt_no').setValue(value);
					}
				});
			}

			function toggleflag(btn) {
				flag = true;
			}

			function deleteallowances(btn) {
				if (btn == 'yes') {
					window.location = 'fees-receipt-report.php?&feesreceive_id=' + Ext.getCmp('feesreceive_id').getValue();
				} else {
					receiveform.getForm().reset();
					feeshead_grid_store.removeAll();
					receivedfees_grid_store.removeAll();
					Ext.getCmp("feeshead_add_edit_form").enable();
					Ext.getCmp('branch_name').focus(true, 500);
					//get_ReceiptNo();	
				}
			}
			receiveform.getForm().on({
				actioncomplete: function (form, action) {
					if (action.type == 'submit') {
						if (action.result.success == "true" && action.result.feesreceive_id > 0) {

							Ext.getCmp('feesreceive_id').setValue(action.result.feesreceive_id);


							Ext.MessageBox.confirm('Confirmation', ' Do U Want To Print Receipt..?', deleteallowances);



						} else {
							resetform();
							/* lastreceivedfees_store.reload();
							 grid1.reconfigure(lastreceivedfees_store,receivedfees_colModel);*/
						}
					}
				}
			});
			var SetupWindow = new Ext.Window({
				title: 'Fees Receipt Master',
				width: 1100,
				height: 550,
				frame: true,
				border: false,
				modal: true,
				layout: 'fit',
				closeAction: 'close',
				collapsible: true,
				items: receiveform,
				buttons: [{
					text: 'Save',
					tabIndex: 13,
					name: 'btnSave',
					iconCls: 'icon-save',
					id: 'btnSave',
					handler: function () {
						var flag = 0;
						var student_id = Ext.getCmp('student_name').getValue();
						Ext.Ajax.request({
							loadMask: true,
							sync: true,
							url: 'data/checknewadmission.php',
							params: { student_id: student_id },
							success: function (resp) {
								var feesflag = Ext.decode(resp.responseText).feesflag;
								feesflag = 1;

								if (feesflag == '0') {
									Ext.MessageBox.show({
										title: 'Message',
										msg: 'Please Contact to director to verify the documents of this student!',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.INFO
									});
									Ext.getCmp("btnreset").handler.call(Ext.getCmp("btnreset").scope);
								} else {
									if (Ext.getCmp('receive_amt').getValue() > 0) {
										grid.stopEditing();

										var recivedamt = new Array();
										var headid = new Array();

										var l = 0;
										for (var i = 0, len = grid.store.getCount(); i < len; i++) {
											record = grid.store.getAt(i);
											headid[l] = record.data.head_id;
											recivedamt[l] = record.data.fees_due_amount;

											l++;
										}


										receiveform.getForm().submit({
											waitMsg: 'Saving...',
											timeout: 3000,
											params: {

												headid: Ext.util.JSON.encode(headid), // Send head_names as well
												recivedamt: Ext.util.JSON.encode(recivedamt)
											},
											sync: true,
											success: function (resp) {

												Ext.MessageBox.confirm('Confirmation', ' Do U Want To Print Receipt..?', deleteallowances);
											},
											failure: function () {
												Ext.MessageBox.show({
													title: 'Error',
													msg: 'Please fill up complete form.',
													buttons: Ext.MessageBox.OK,
													icon: Ext.MessageBox.ERROR
												});
											}
										});
									} else {
										alert("No Debit Amount For this Record...");
									}
								}
							}
						});
					}
				}, {
					text: 'Reset',
					name: 'btnreset',
					tabIndex: 14,
					iconCls: 'icon-reset',
					id: 'btnreset',
					handler: function () {
						if (formMode == "add") {
							resetform();
						}
						receiveform.getForm().reset();
						grid.stopEditing();
						feeshead_grid_store.removeAll();
						receivedfees_grid_store.removeAll();
						Ext.getCmp("feeshead_add_edit_form").enable();
						get_ReceiptNo();
						if (flgAdd == "true") {
							Ext.getCmp('btnSave').enable();
						}
						Ext.getCmp('branch_name').focus(true, 500);

					}
				}, {
					text: 'Close',
					name: 'btnclose',
					tabIndex: 15,
					iconCls: 'icon-close',
					id: 'btnclose',
					handler: function () {
						SetupWindow.close();
					}
				}]
			});

			Ext.getCmp('cash').on('check', function () {
				if (Ext.getCmp('cash').getValue() == false) {
					Ext.getCmp('bank_name').reset();
					Ext.getCmp('cheque_no').reset();
					Ext.getCmp('bank_name').disable();
					Ext.getCmp('cheque_no').disable();
				} else {
					Ext.getCmp('bank_name').enable();
					Ext.getCmp('cheque_no').enable();
				}
			});


			Ext.getCmp('comment_show').on('check', function () {
				if (Ext.getCmp('comment_show').getValue() == false) {
					Ext.getCmp('comment').reset();
					Ext.getCmp('comment').disable();
				} else {
					Ext.getCmp('comment').enable();
				}
			});


			///(shital)receipt_date setup comment as per harshadbhai's requirement(they need old date fees recepit)// 
			/*	Ext.getCmp('receipt_date').on('select', function () {
					if (Ext.getCmp('receipt_no').getValue() != "") {
						Ext.Ajax.request({
							loadMask: true,
							sync: true,
							url: 'data/getfeesreceiptdaterange.php',
							params: {
								year_id: Ext.getCmp('year_name').getValue(),
								branchyear_id: Ext.getCmp('branchyear_name').getValue(),
								semester_id: Ext.getCmp('semester_name').getValue(),
								feesrec_no: Ext.getCmp('receipt_no').getValue(),
								date: Ext.getCmp('receipt_date').getValue(),
							},
							waitMsg: 'Loading',
							success: function (resp) {
								if (Ext.decode(resp.responseText).valid == "true") {
									flag = true;
								} else {
									flag = false;
									var date = Ext.decode(resp.responseText).valid;
									var feesrec_no = Ext.getCmp('receipt_no').getValue();
									feesrec_no = feesrec_no - 1;
									Ext.MessageBox.alert("Error", "Recipt Date Should Not Less than " + date + " See Receipt No " + feesrec_no, toggleflag);
									Ext.getCmp('receipt_date').reset();
								}
							}
						});
					} else {
						alert("Select Semester First");
						Ext.getCmp('receipt_date').reset();
					}
				}); */

			feeshead_grid_store.on({
				'load': {
					fn: function () {
						if (formMode == "add") {
							grid.reconfigure(feeshead_grid_store, feeshead_colModel);

							grid1.reconfigure(receivedfees_grid_store, receivedfees_colModel);
						}
						else if (formMode == "edit") {
							get_receivedfees_data();

						}

						if (feeshead_grid_store.getCount() <= 0) {
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Fees Setup Not found For This Selection.',
								buttons: Ext.MessageBox.OK,
								icon: Ext.MessageBox.ERROR
							});
						} else {
							grid.startEditing(0, 8);
						}
					}
				}
			});
			receivedfees_grid_store.on({
				'load': {
					fn: function (store, options) {
						if (formMode == "add") {
							grid1.reconfigure(receivedfees_grid_store, receivedfees_colModel);
						}
						var maxmonthid = 1;
						if (receivedfees_grid_store.getCount() > 0) {
							receivedfees_grid_store.sort("feesrec_to", "DESC");
							maxmonthid = parseInt(receivedfees_grid_store.getAt(0).data.feesrec_to);
							/* var fieldname = receivedfees_grid_store.getSortState().field ;
							 var direction = receivedfees_grid_store.getSortState().direction;
							 receivedfees_grid_store.sort(fieldname,direction);*/
						}
						if (formMode == "add") {

							grid1();
						}
					}
				}
			});
			function afterEdit(e) {

var t = 0;
for (k = 0; k < feeshead_grid_store.getCount(); k++) {
	var record = feeshead_grid_store.getAt(k);
	t = t + record.data.feeshead_receiving;

}
feeshead_form.getForm().findField('total_amount').setValue(t);
};
				
if(formMode == "edit"){
   
   var name = new Array()
	name ='<?php echo $row["student_name"] ; ?>'.split(" ");
	
   receiveform.getForm().findField('receipt_no').setValue('<?php echo $row["receipt_no"] ; ?>');
   receiveform.getForm().findField('receipt_date').setValue('<?php echo $row["receipt_date"] ; ?>');
   receiveform.getForm().findField('student_gturegno').setValue('<?php echo $row["student_gturegno"] ; ?>');

   receiveform.getForm().findField('branch_name').setValue('<?php echo $row["branch_id"] ; ?>');
 
   receiveform.getForm().findField('student_name').setValue('<?php echo $row["student_name"] ; ?>');
 
   receiveform.getForm().findField('year_name').setValue('<?php echo $row["year_id"] ; ?>');
   receiveform.getForm().findField('branchyear_name').setValue('<?php echo $row["branchyear_id"] ; ?>');
   receiveform.getForm().findField('semester_name').setValue('<?php echo $row["semester_id"] ; ?>');
  receiveform.getForm().findField('receive_amt').setValue('<?php echo $row["receive_amt"] ; ?>');
 
   receiveform.getForm().findField('branch_name').readOnly = true;


   if('<?php echo $row["feesrec_cash"] ; ?>' == "No"){ 
	   receiveform.getForm().findField('cash').setValue(true);
	   receiveform.getForm().findField('cheque_no').setValue('<?php echo $row["cheque_no"] ; ?>');
	   receiveform.getForm().findField('bank_name').setValue('<?php echo $row["bank_name"] ; ?>');
   }	
   
   receiveform.getForm().findField('comment').setValue('<?php echo $row["comment"] ; ?>');
   student_id = '<?php echo $row["student_id"] ; ?>' ;

   
   grid.reconfigure(feeshead_grid_store, feeshead_editcolModel);
	get_feeshead_data();  
	get_receivedfees_data();
	grid.on('afteredit', afterEdit, this);
}
			var varWidth = (screen.width - SetupWindow.width) / 2;
			SetupWindow.setPosition(varWidth, ((screen.height / 4) - 190));
			SetupWindow.show();

			if (flgAdd == "true") {
				Ext.getCmp('btnSave').enable();
			} else {
				Ext.getCmp('btnSave').disable();
			}
			Ext.getCmp('branch_name').focus(true, 500);

		});
	</script>
</div>
<?php require "_footer.php"; ?>