controllercode
controllercode
define([
"sap/ui/core/mvc/Controller",
"sap/m/BusyDialog",
"sap/ui/core/BusyIndicator",
"sap/m/MessageBox",
"sap/m/MessageToast",
"sap/ui/model/Filter",
"sap/ui/model/FilterOperator",
"sap/ui/model/json/JSONModel",
"sap/ui/core/ValueState",
"sap/ui/core/format/DateFormat"
],
/**
* @param {typeof sap.ui.core.mvc.Controller} Controller
*/
function (Controller, BusyDialog, BusyIndicator,
MessageBox,MessageToast,Filter, FilterOperator, JSONModel, ValueState, DateFormat)
{
"use strict";
onInit: function () {
that = this;
i18nText =
that.getOwnerComponent().getModel("i18n").getResourceBundle();
oDataModel = that.getOwnerComponent().getModel(); //
Retrieves the OData model from the owner component. This assumes that the OData
model is set at the component level
oBusyDialog = new BusyDialog();
ids = ["iEmpNo", "iEmpNm", "iDeptCd", "iWlCd", "iDesig", "iPhone",
"iEmail", "iBankNo", "iBankNm"];
fieldNames = ["Empno", "Empnm", "Deptcd", "Wlcd", "Designation",
"PhoneNo", "EmailId", "Bankac", "Banknm"];
oTable = that.byId("employeeTable");
specialSymbolRegex = /[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/;
numericRegex = /^[0-9]+$/;
numericRegex1 = /[0-9]/;
dateRegex = /^\d{4}-\d{2}-\d{2}$/;
},
onSubmitEmpno: function (oEvent) {
sEmpno = oEvent.getParameter("value");
if (!sEmpno) {
MessageBox.error(i18nText.getText("MessageBox.empNoRequired"));
return;
}
that.readEmployeeData();
},
readEmployeeData: function () {
var sPath = "/Y57_EMPSet('" + sEmpno + "')"; //
Constructs the OData service path for reading employee data based on the employee
number.
oBusyDialog.open();
oDataModel.read(sPath, {
success: function (oData) {
that.clearFields();
that.updateInputFields(oData);
oBusyDialog.close();
}.bind(that),
error: function (oError) {
MessageBox.error(i18nText.getText("MessageBox.readEmpNoError") + sEmpno);
that.clearFields();
oBusyDialog.close();
}.bind(that)
});
},
updateInputFields: function (oData) {
that.byId("dpjoindt").setValue(oDateFormat.format(oData.Joiningdt));
switch (oData.Gender) {
case "M":
that.byId("GroupA").setSelectedButton(that.byId("MaleRadioButton"));
break;
case "F":
that.byId("GroupA").setSelectedButton(that.byId("FemaleRadioButton"));
break;
case "O":
that.byId("GroupA").setSelectedButton(that.byId("OtherRadioButton"));
break;
default:
that.byId("GroupA").setSelectedButton(that.byId("MaleRadioButton")); //
Handle default case if necessary
break;
}
MessageBox.success(i18nText.getText("MessageBox.readEmpNoSuccess"));
},
onPressSave: function () {
sEmpno = this.byId("iEmpNo").getValue();
if (!sEmpno) {
MessageBox.error(i18nText.getText("MessageBox.empNoRequiredSave"));
return false;
}
if (!that.validations()) {
return;
}
oBusyDialog.open();
oDataModel.read("/Y57_EMPSet('" + sEmpno + "')", {
success: function () {
oDataModel.create("/Y57_EMPSet", payloadEmpSave, {
success: function () {
MessageBox.success(i18nText.getText("MessageBox.empSave"));
that.clearFields();
}.bind(that),
error: function (oError) {
MessageBox.error(i18nText.getText("MessageBox.empSaveError") + oError.message);
}
});
},
onPressUpdate: function () {
sEmpno = this.byId("iEmpNo").getValue();
if (!sEmpno) {
MessageBox.error(i18nText.getText("MessageBox.empNoRequiredUpdate"));
return;
}
if (!that.validations()) {
return;
}
oBusyDialog.open();
oDataModel.read("/Y57_EMPSet('" + sEmpno + "')", {
success: function () {
this.updateEmployee(sEmpno);
oBusyDialog.close();
}.bind(this),
error: function () {
MessageBox.error(i18nText.getText("Messagetoast.empUpdateError"));
oBusyDialog.close();
}
});
},
updateEmployee: function (sEmpno) {
MessageBox.confirm(i18nText.getText("MessageBox.empUpdateConfirmOk") + sEmpno +
"?", {
title: "Confirm Update",
onClose: function (oAction) {
if (oAction === MessageBox.Action.OK) {
that.updateEmployeeData(sEmpno);
} else {
MessageToast.show(i18nText.getText("MessageBox.empUpdateConfirmCancel"));
}
}
});
},
updateEmployeeData: function (sEmpno) {
var sPath = "/Y57_EMPSet('" + sEmpno + "')";
var payloadEmpUpdate = that.fillFields();
oDataModel.update(sPath, payloadEmpUpdate, {
success: function () {
MessageBox.success(sEmpno + " " +
i18nText.getText("MessageBox.empUpdate"));
that.clearFields();
}.bind(that),
error: function () {
MessageBox.error(i18nText.getText("MessageBox.empUpdateError"));
}
});
},
fillFields: function () {
var oNewEmp = {};
ids.forEach(function (fieldId, index) {
oNewEmp[fieldNames[index]] = that.byId(fieldId).getValue();
}, that);
oNewEmp.Gender = that.getSelectedGender();
oNewEmp.Birthdt =
that.formatDateForOData(that.byId("dpbirdt").getDateValue());
oNewEmp.Joiningdt =
that.formatDateForOData(that.byId("dpjoindt").getDateValue());
return oNewEmp;
},
formatDateForOData: function (oDate) {
if (oDate) {
var oDateFormat = DateFormat.getDateTimeInstance({
pattern: "yyyy-MM-dd'T'HH:mm:ss"
});
return oDateFormat.format(oDate);
} else {
return "0000-00-00T00:00:00";
}
},
getSelectedGender: function () {
var RadioButtonGroup = that.byId("GroupA");
var selectedButton = RadioButtonGroup.getSelectedButton();
switch (selectedButton) {
case that.byId("MaleRadioButton"):
return "M";
case that.byId("FemaleRadioButton"):
return "F";
case that.byId("OtherRadioButton"):
return "O";
default:
return "";
}
},
clearFields: function () {
var ids = ["iEmpNo", "iEmpNm", "iDesig", "dpbirdt", "dpjoindt",
"iPhone", "iEmail", "iDeptCd", "iWlCd", "iBankNo", "iBankNm"]
ids.forEach(idsa => {
that.byId(idsa).setValue("");
that.byId(idsa).setValueState(ValueState.None);
that.byId(idsa).setValueStateText("");
});
that.byId("GroupA").setSelectedButton(that.byId("MaleRadioButton"));
},
validations: function () {
var oInputEmpNm = this.byId("iEmpNm");
that.onLiveChangeEmpnm({
getSource: function () {
return oInputEmpNm;
}
});
var oInputBirthdt = this.byId("dpbirdt");
that.onChangeBirthdate({
getSource: function () {
return oInputBirthdt;
}
});
var oInputJoindt = this.byId("dpjoindt");
that.onChangeJoiningdate({
getSource: function () {
return oInputJoindt;
}
});
var oInputDesignation = this.byId("iDesig");
that.onLiveChangeDesignation({
getSource: function () {
return oInputDesignation;
}
});
var oInputPhone = this.byId("iPhone");
that.onLiveChangePhoneNo({
getSource: function () {
return oInputPhone;
}
});
var oInputEmail = this.byId("iEmail");
that.onLiveChangeEmail({
getSource: function () {
return oInputEmail;
}
});
var oInputBankNo = this.byId("iBankNo");
that.onLiveChangeBankNo({
getSource: function () {
return oInputBankNo;
}
});
var oInputBankName = this.byId("iBankNm");
that.onLiveChangeBankName({
getSource: function () {
return oInputBankName;
}
});
if (oInputEmpNm.getValueState() === ValueState.Error) {
var errorMessage = oInputEmpNm.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputBirthdt.getValueState() === ValueState.Error) {
var errorMessage = oInputBirthdt.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputJoindt.getValueState() === ValueState.Error) {
var errorMessage = oInputJoindt.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputDesignation.getValueState() === ValueState.Error) {
var errorMessage = oInputDesignation.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputPhone.getValueState() === ValueState.Error) {
var errorMessage = oInputPhone.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputEmail.getValueState() === ValueState.Error) {
var errorMessage = oInputEmail.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputBankNo.getValueState() === ValueState.Error) {
var errorMessage = oInputBankNo.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
if (oInputBankName.getValueState() === ValueState.Error) {
var errorMessage = oInputBankName.getValueStateText();
MessageBox.error(i18nText.getText("MessageBox.validation") +
errorMessage);
return false;
}
return true;
},
onDeleteEmployee: function () {
var oSelectedItem = oTable.getSelectedItem();
oBusyDialog.open();
if (!oSelectedItem) {
MessageBox.error(i18nText.getText("MessageBox.deleteTableSelectEmp"));
oBusyDialog.close();
return;
}
MessageBox.confirm(i18nText.getText("MessageBox.deleteTableEmpConfirm") +
oEmployee.Empno + "?", {
title: "Confirm Deletion",
onClose: function (oAction) {
if (oAction === MessageBox.Action.OK) {
that.performEmployeeDeletion(sPath);
} else {
MessageBox.show(i18nText.getText("MessageBox.deleteTableEmpCancel"));
oBusyDialog.close();
}
}
});
},
performEmployeeDeletion: function (sPath) {
var oEmployee = oDataModel.getProperty(sPath);
oDataModel.remove(sPath, {
success: function () {
MessageBox.success(i18nText.getText("MessageBox.Emp") + " "
+ oEmployee.Empno + " " + i18nText.getText("MessageBox.deleteEmpSuccess"));
oTable.removeSelections();
oBusyDialog.close();
},
error: function (oError) {
MessageBox.error(i18nText.getText("MessageBox.deleteTableEmpError") +
oError.message);
oBusyDialog.close();
}
});
},
onLiveChangeEmpno: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
liveChangeInput.setValueStateText(i18nText.getText("stateText.empNoRequired"));
} else if (liveChangeValue.length !== 4 || !
numericRegex.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empNoNum"));
} else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
onLiveChangeEmpnm: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
liveChangeInput.setValueStateText(i18nText.getText("stateText.empNmRequired"));
}
else if (numericRegex1.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empNmNotNum"));
}
else if (specialSymbolRegex.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empNmNotSpSymbol"));
}
else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
onLiveChangePhoneNo: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
if (liveChangeValue) {
liveChangeInput.setValueState("Error");
liveChangeInput.setValueStateText(i18nText.getText("stateText.empPhoneNoRequired"))
;
}
if (liveChangeValue.length !== 10) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empPhoneNoDigits"));
}
else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
onLiveChangeEmail: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
var validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+
(?:\.[a-zA-Z0-9-]+)*$/;
var pattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(com|in|other|
org|net)$/;
if (!liveChangeValue) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empEmailRequired"));
}
if (!validRegex.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empEmailNotValid"));
}
if (!pattern.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empEmailEnd"));
}
else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
onChangeBirthdate: function (oEvent) {
var oDatePicker = oEvent.getSource();
var sEnteredDate = oDatePicker.getValue();
if (!sEnteredDate) {
oDatePicker.setValueState(ValueState.Error);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOBRequired"));
return;
}
if (!dateRegex.test(sEnteredDate)) {
oDatePicker.setValueState(ValueState.Error);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOBFormat"));
return;
}
CurrentDate = new Date();
var EnteredDate = new Date(sEnteredDate);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOBNotFuture"));
}
else {
function getDaysInMonth(year, month) {
return new Date(year, month, 0).getDate();
}
var enteredDay = parseInt(sEnteredDate.split('-')[2], 10);
var enteredMonth = parseInt(sEnteredDate.split('-')[1], 10);
var daysInMonth = getDaysInMonth(EnteredDate.getFullYear(),
enteredMonth);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDateMonth"));
}
else if (enteredDay > daysInMonth) {
oDatePicker.setValueState(ValueState.Error);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDateDay"));
}
else {
oDatePicker.setValueState(ValueState.Success);
oDatePicker.setValueStateText("");
}
}
},
onChangeJoiningdate: function (oEvent) {
var oDatePicker = oEvent.getSource();
var sEnteredDate = oDatePicker.getValue();
var sBirthdt = this.byId("dpbirdt").getValue();
if (!sEnteredDate) {
oDatePicker.setValueState(ValueState.Error);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOJRequired"));
return;
}
if (!dateRegex.test(sEnteredDate) && sEnteredDate.length > 0) {
oDatePicker.setValueState(ValueState.Error);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOJFormat"));
return;
}
var oEnteredDate = new Date(sEnteredDate);
var oBirthDate = new Date(sBirthdt);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOJGreater"));
}
else {
function getDaysInMonth(year, month) {
return new Date(year, month, 0).getDate();
}
var enteredDay = parseInt(sEnteredDate.split('-')[2], 10);
var enteredMonth = parseInt(sEnteredDate.split('-')[1], 10);
var daysInMonth = getDaysInMonth(oEnteredDate.getFullYear(),
enteredMonth);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOJMonths"));
}
else if (enteredDay > daysInMonth) {
oDatePicker.setValueState(ValueState.Error);
oDatePicker.setValueStateText(i18nText.getText("stateText.empDOJDay"));
}
else {
oDatePicker.setValueState(ValueState.Success);
oDatePicker.setValueStateText("");
}
}
},
onLiveChangeDesignation: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
if (numericRegex1.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empDesignationNotNum"
));
}
else if (specialSymbolRegex.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empDesignationNotSpSy
mbol"));
}
else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
onLiveChangeBankNo: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
if (!liveChangeValue) {
liveChangeInput.setValueState(ValueState.None);
liveChangeInput.setValueStateText("");
}
if ((liveChangeValue.length > 0 && liveChangeValue.length < 4) ||
liveChangeValue.length > 10) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empBankAcNoLength"));
}
else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
onLiveChangeBankName: function (oEvent) {
liveChangeInput = oEvent.getSource();
liveChangeValue = liveChangeInput.getValue();
if (numericRegex1.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empBankNmNotNum"));
}
else if (specialSymbolRegex.test(liveChangeValue)) {
liveChangeInput.setValueState(ValueState.Error);
liveChangeInput.setValueStateText(i18nText.getText("stateText.empBankNmNotSpSymbol"
));
}
else {
liveChangeInput.setValueState(ValueState.Success);
liveChangeInput.setValueStateText("");
}
},
handleEmpNoValueHelpRequest: function () {
BusyIndicator.show(0);
oDataModel.read("/y57_empnonmSet", {
success: function (oData) {
that.getView().byId("diaValueHelpEmployee").setModel(new
JSONModel(oData));
that.getView().byId("diaValueHelpEmployee").open();
BusyIndicator.hide();
}
})
},
onEmpSearch: function (oEvent) {
sSearchValue = oEvent.getParameter("query");
oBinding = that.getView().byId("diaValueHelpEmployee").getContent()
[1].getBinding("items"); // Assuming the Table is the second control in the Dialog
if (oBinding) {
var oFilterEmpno = new Filter("Empno", FilterOperator.Contains,
sSearchValue);
var oFilterEmpnm = new Filter("Empnm", FilterOperator.Contains,
sSearchValue);
if (oBinding) {
var oFilterWlCd = new Filter("Wlcd", FilterOperator.Contains,
sSearchValue);
var oFilterWlDesc = new Filter("Wldesc",
FilterOperator.Contains, sSearchValue);
if (oBinding) {
var oFilterDeptCd = new Filter("Deptcd",
FilterOperator.Contains, sSearchValue);
var oFilterDeptNm = new Filter("Deptnm",
FilterOperator.Contains, sSearchValue);
oCombinedFilter = new Filter({
filters: [oFilterDeptCd, oFilterDeptNm],
and: false // Logical OR
});
oBinding.filter([oCombinedFilter]);
}
},
onDeptCdPress: function (oEvent) {
selRow = oEvent.getSource().getBindingContext().getObject();
that.getView().byId("iDeptCd").setValue(selRow.Deptcd);
that.getView().byId("diaValueHelpDeptCd").close();
},
onCloseDeptCd: function () {
that.getView().byId("diaValueHelpDeptCd").close();
},
});
});