
Java Code for Examples
package com.sap.expense.customer.examples;
import com.sap.expense.customer.businessLogic.*;
import com.sap.expense.model.modules.*;
import com.sap.expense.model.dboReadOnly.*;
import com.sap.expense.tools.NFHelper;
import com.sap.expense.tools.CalendarHelper;
import com.sap.mycats.basics.tools.badi.*;
import com.sap.mycats.basics.tools.*;
import java.util.Hashtable;
public class ZTravelExpenseEnhancement implements ITravelExpenseEnhancement {
public Hashtable customize(String strOperation, Hashtable htbToCustomize) {
Hashtable htbResult = null;
if(OP_RECEIPT_SET_DEFAULTS.equals(strOperation)) {
htbResult = setReceiptDefaults(htbToCustomize);
} else if(OP_RECEIPT_LIST_SUMMARY.equals(strOperation)) {
htbResult = setReceiptListSummary(htbToCustomize);
} else if(OP_TRIP_VALIDATION.equals(strOperation)) {
htbResult = validateTrip(htbToCustomize);
} else if(OP_TRIP_ADD_COLUMNS.equals(strOperation)) {
htbResult = populateTripAddColumns(htbToCustomize);
} else if(OP_TRIP_CUSTOMER_DATA_SAVE.equals(strOperation)) {
htbResult = saveCustomerData(htbToCustomize);
} else if(OP_TRIP_CUSTOMER_DATA_LOAD.equals(strOperation)) {
htbResult = loadCustomerData(htbToCustomize);
} else if(OP_TEXT_SET_DEFAULTS.equals(strOperation)) {
htbResult = setDefaultText(htbToCustomize);
}
return htbResult;
}
/**
* Apply additional default rules to Receipt record.
* Can modify field values based on field names.
* @param htbToCustomize hashtable containing Receipt record to modify.
*/
private Hashtable setReceiptDefaults(Hashtable htbToCustomize) {
ReceiptReadOnly dboReceipt = new ReceiptReadOnly();
try {
ITable tblReceipts = dboReceipt.htbRecordToTable((Hashtable) htbToCustomize.get(IReceipt.TN_RECEIPTS));
if(tblReceipts != null) {
IRecord rcdToCustomize = tblReceipts.getRecord(0);
// Set Shorttxt field
rcdToCustomize.setField( "SHORTTXT", "Custom");
// Update record in ITable
tblReceipts.updateRecord(0, rcdToCustomize);
// Get Key
String strReceiptGuid = rcdToCustomize.getFieldAsString(IReceipt.FN_RECEIPTGUID);
// Create Hashtable of receipts
Hashtable htbReceipts = new Hashtable();
// Add record to hashtable
htbReceipts.put(strReceiptGuid, rcdToCustomize);
// Update htbToCustomize
htbToCustomize.put(IReceipt.TN_RECEIPTS, htbReceipts);
}
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
return htbToCustomize;
}
/**
* Additional Receipt list summary line (Exchange rate average).
*/
private Hashtable setReceiptListSummary (Hashtable htbToCustomize) {
ReceiptReadOnly dboReceipt = new ReceiptReadOnly();
try {
ITable tblReceipts = dboReceipt.htbRecordToTable((Hashtable) htbToCustomize.get(IReceipt.TN_RECEIPTS));
double dblSumRate = 0;
String strRate = null;
if(tblReceipts != null) {
int iCount = tblReceipts.getRowCount();
// Loop thru ITable to get average exch rate
for(int i=0; i <iCount; i++){
dblSumRate = dblSumRate + tblReceipts.getRecord(i).getFieldAsFloat(IReceipt.FN_EXCH_RATE);
}
dblSumRate = dblSumRate / iCount;
// Format result for UI
NFHelper nfHelper = new NFHelper();
int iDec = IReceipt.CONST_INT_MAX_DECIMALS_EXCH_RATE;
strRate = nfHelper.formatNumberForUI((float) dblSumRate, iDec);
}
htbToCustomize.put(OP_RECEIPT_LIST_SUMMARY, strRate);
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
return htbToCustomize;
}
/**
* Validates than if trip's PD_MEALS is on, there are not receipts of expense type HOTL, ABZA or AUTO.
* If there are HOTL or ABZA receipts, sets a warning message.
* If there are AUTO receipts, set an error message.
* @param htbToValidate The Hashtable to validate.
* @return Hashtable validated Hashtable
*/
private Hashtable validateTrip(Hashtable htbTrip) {
// Create hashtable for errors and warnings
Hashtable htbErrMsgs = new Hashtable();
Hashtable htbWarningMsgs = new Hashtable();
// Get trip record from Hashtable
IRecordReadWrite rcdTrip = (IRecordReadWrite) htbTrip.get(ITrip.TN_TRIPS);
// Get receipts ITable from Hashtable
ReceiptReadOnly dboReceipt = new ReceiptReadOnly();
ITable tblReceipts = null;
try{
tblReceipts = dboReceipt.htbRecordToTable((Hashtable) htbTrip.get(IReceipt.TN_RECEIPTS));
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
if(rcdTrip != null && tblReceipts != null){
IRecordReadWrite rcdReceipt;
ExpenseTypeReadOnly dboExpType = new ExpenseTypeReadOnly();
String strExpType;
String strPdMeals = rcdTrip.getFieldAsString(ITrip.FN_PD_MEALS);
if("X".equals(strPdMeals)){
int iCount = tblReceipts.getRowCount();
for (int i=0; i < iCount; i++){
rcdReceipt = tblReceipts.getRecord(i);
// Get Expense Type
strExpType = rcdReceipt.getFieldAsString(IReceipt.FN_EXP_TYPE);
if("HOTL".equals(strExpType)){
htbWarningMsgs.put(String.valueOf(i), "WARNING1: Customer exit validation for Hotel!");
}
if("ABZA".equals(strExpType)){
htbWarningMsgs.put(String.valueOf(i), "WARNING2: Customer exit validation for Dinner deduction!");
}
if("AUTO".equals(strExpType)){
htbErrMsgs.put(String.valueOf(i), "ERROR1: Customer exit validation for Test Auto!");
}
}
}
}
// If there were some warnings, add htbWarningMsgs in htbTrip
if(!htbWarningMsgs.isEmpty()){
htbTrip.put(Z_WARNING_MESSAGE, htbWarningMsgs);
}
// If there were some errors, add htbErrMsgs in htbTrip
if(!htbErrMsgs.isEmpty()){
htbTrip.put(Z_ERROR_MESSAGE, htbErrMsgs);
}
return htbTrip;
}
/**
* Additional Trip List Columns.
* The following record must be defined in the customer file
* C:\me\wwwroot\te_customer\extensions\ci_records.xml
* <record name="ZTRAVEL_HEADER">
* <fields>
* <field name="TRIPGUID" type="E_TRIPGUID"/>
* <field name="TOT_MILES" type="E_M_TOTAL"/>
* <field name="WBS_ELEMENT" type="E_WBS_ELEMT"/>
* <field name="NO_RECEIPTS" type="Z_NO_RECEIPTS"/>
* </fields>
* </record>
*/
private Hashtable populateTripAddColumns(Hashtable htbToCustomize) {
try {
// Get trip table from Hashtable
ITableReadWrite tblTrips = (ITableReadWrite) htbToCustomize.get(ITrip.TN_TRIPS);
ITableReadWrite zTable = new TableFactory().createTable("ZTRAVEL_HEADER");
IRecordReadWrite rcdTrip;
IRecordReadWrite zRecord = new RecordFactory().createRecord("ZTRAVEL_HEADER");
if(tblTrips != null){
int iCount = tblTrips.getRowCount();
for(int i=0; i<iCount; i++){
rcdTrip = tblTrips.getRecordReadWrite(i);
zRecord.setCorrespondingFields(rcdTrip);
// Get tripGuid
String strTripGuid = rcdTrip.getFieldAsString(ITrip.FN_TRIPGUID);
// Get Total miles
MileReadOnly dboMile = new MileReadOnly();
String strTotalMiles = String.valueOf(dboMile.getTotalMilesFromDB(strTripGuid));
zRecord.setField("TOT_MILES", strTotalMiles);
// Get WBS of first cost line
CostDistReadOnly dboCost = new CostDistReadOnly();
// Get costGuid
String strCostGuid = rcdTrip.getFieldAsString(ITrip.FN_COSTGUID);
ITable tblCosts = dboCost.getTable(strCostGuid);
String strWbs = "";
if(tblCosts != null){
IRecord rcdCost = tblCosts.getRecord(0);
strWbs = rcdCost.getFieldAsString(ICostDist.FN_WBS_ELEMT);
}
zRecord.setField("WBS_ELEMENT", strWbs);
// Get number of receipts
ReceiptReadOnly dboReceipt = new ReceiptReadOnly();
ITable tblReceipts = dboReceipt.getTableByTripGuid(strTripGuid);
int iRecCount = 0;
if(tblReceipts != null) iRecCount = tblReceipts.getRowCount();
zRecord.setField("NO_RECEIPTS", String.valueOf(iRecCount));
// Add record to table
zTable.appendRecord(zRecord);
}
// Create customized Hashtable
htbToCustomize.put(ITrip.TN_TRIPS, zTable);
}
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
return htbToCustomize;
}
/**
* Add customer data to persisted Hashtable
* @param htbToCustomize hashtable containing Customer Data record to save.
* @return Hashtable containing Customer Data from UI.
*/
private Hashtable saveCustomerData(Hashtable htbToCustomize) {
try {
// Create CustomerData record
CustomerDataReadOnly dboCustomreData = new CustomerDataReadOnly();
IRecord rcdTmp = dboCustomreData.getNewRecord();
// Get array of field names and get values from context
int iFieldCount = rcdTmp.getRepositoryType().getFieldCount();
String strFName, strFValue;
UIContextIO uiIO = new UIContextIO();
NFHelper nfHelper = new NFHelper();
boolean isEmpty = true;
for(int i=0; i<iFieldCount; i++){
// Get field name
strFName = rcdTmp.getRepositoryType().getFieldType(i).getName();
// Get value from context
strFValue = uiIO.getValue(strFName);
// Set value in record
if(strFValue != null && strFValue.length() > 0){
rcdTmp.setField(strFName, nfHelper.formatNumberForDB(strFValue));
isEmpty = false;
}
}
if(!isEmpty){
htbToCustomize.put(ICustomerData.TN_CUSTOMER_DATA, rcdTmp);
return htbToCustomize;
} else {
htbToCustomize.remove(ICustomerData.TN_CUSTOMER_DATA);
return htbToCustomize;
}
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
}
/**
* Send persisted customer data to context
* @param htbToCustomize hashtable containing Customer data to send.
* @return null
*/
private Hashtable loadCustomerData(Hashtable htbToCustomize) {
try {
// Get Customer Data from Hashtable
IRecord rcdCustomerData = (IRecord) htbToCustomize.get(ICustomerData.TN_CUSTOMER_DATA);
if(rcdCustomerData != null){
// Get array of field names and send values to context
int iFieldCount = rcdCustomerData.getRepositoryType().getFieldCount();
String strFName;
UIContextIO uiIO = new UIContextIO();
NFHelper nfHelper = new NFHelper();
for(int i=0; i<iFieldCount; i++){
// Get field name
strFName = rcdCustomerData.getRepositoryType().getFieldType(i).getName();
// Set value to context
uiIO.setValue(strFName, rcdCustomerData.getFieldAsDisplayString(strFName, true));
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
}
/**
* Set customer exit key of TRAVEL_TEXT_TEMPLATE_NAMES record.
* @param htbToCustomize hashtable containing trip information and text template name record.
*/
private Hashtable setDefaultText(Hashtable htbToCustomize) {
try {
IRecord rcdTemplateName = (IRecord) htbToCustomize.get(OP_TEXT_SET_DEFAULTS);
if(rcdTemplateName != null) {
// Get the Editor Type
String strEditorType = rcdTemplateName.getFieldAsString(ITemplateTextName.FN_EDITOR_TYPE);
// Depending on Editor Type, apply logic
if(IGeneral.EDITOR_TYPE_TRIP.equals(strEditorType)) {
// Get Trip record
IRecord rcdTrip = (IRecord) htbToCustomize.get(ITrip.TN_TRIPS);
if(rcdTrip != null){
String strUserExit = "";
// Get trip's country and employee's country
String strTripCountry = rcdTrip.getFieldAsString(ITrip.FN_COUNTRY);
EmployeeReadOnly dboEmployee = new EmployeeReadOnly();
IRecord rcdEmployee = dboEmployee.getTable().getRecord(0);
String strEmployeeCountry = rcdEmployee.getFieldAsString(IEmployee.FN_COUNTRY);
// If trip is domestic
if(strEmployeeCountry.equals(strTripCountry)){
MileReadOnly dboMile = new MileReadOnly();
ITable tblMiles = dboMile.htbRecordToTable((
Hashtable) htbToCustomize.get(IMiles.TN_TRAVEL_MILES));float fltTotMiles = 0;
int iCount = 0;
if(tblMiles != null) iCount = tblMiles.getRowCount();
for(int i=0; i<iCount ; i++){
fltTotMiles = fltTotMiles + tblMiles.getRecord(i).getFieldAsFloat(IMiles.FN_M_TOTAL);
}
// If Total miles > 0, USER_EXIT = CAR else USER_EXIT = NOCAR
if(fltTotMiles > 0) strUserExit = "CAR";
else strUserExit = "NOCAR";
}
rcdTemplateName.setField(ITemplateTextName.FN_USER_EXIT, strUserExit);
}
} else if(IGeneral.EDITOR_TYPE_RECEIPT.equals(strEditorType)) {
// Get Trip record
IRecord rcdTrip = (IRecord) htbToCustomize.get(ITrip.TN_TRIPS);
if(rcdTrip != null){
String strTripCountry = rcdTrip.getFieldAsString(ITrip.FN_COUNTRY);
// If trip's country is FRANCE
if("FR".equals(strTripCountry)){
AddInfoReadOnly dboAddInfo = new AddInfoReadOnly();
ITable tblAddInfo = dboAddInfo.htbRecordToTable((Hashtable) htbToCustomize.get(IAddInfo.TN_RECEIPTS_ADDINFO));
if(tblAddInfo != null){
IRecord rcdAddInfo = tblAddInfo.getRecord(0);
String strCategory = rcdAddInfo.getFieldAsString(IAddInfo.FN_P_CTG);
String strProvider = rcdAddInfo.getFieldAsString(IAddInfo.FN_P_PRV);
// If the airline used if AIR FRANCE
if("F".equals(strCategory) && "AF".equals(strProvider)){
rcdTemplateName.setField(ITemplateTextName.FN_USER_EXIT, "DOMFL");
}
}
}
}
} else if(IGeneral.EDITOR_TYPE_MILE.equals(strEditorType)) {
// To be implemented
}
htbToCustomize.put(OP_TEXT_SET_DEFAULTS, rcdTemplateName);
}
} catch (Exception e) {
e.printStackTrace();
throw new Error (e.toString());
}
return htbToCustomize;
}
}