Commit 10ddf0d7 authored by Ajit Deshmukh's avatar Ajit Deshmukh

Upload New File

parent c4c3bdc2
package ibase.webitm.ejb.dis;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import javax.naming.InitialContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import ibase.ejb.CommonDBAccessEJB;
import ibase.system.config.AppConnectParm;
import ibase.system.config.ConnDriver;
import ibase.utility.BaseLogger;
import ibase.utility.E12GenericUtility;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.MasterStatefulLocal;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.ejb.XML2DBEJB;
import ibase.webitm.utility.GenericWorkflowClass;
import ibase.webitm.utility.ITMException;
public class ReqForQuotationAprv extends ValidatorEJB {
E12GenericUtility genericUtility = new E12GenericUtility();
public String updateWFStatus() throws RemoteException, ITMException {
BaseLogger.log("3", null, null, "ReqForQuotationAprv.updateWFStatus calling......");
return "";
}
public String updateWFStatus(String domString, Connection conn, String xtraParams)
throws RemoteException, ITMException {
BaseLogger.log("3", null, null, "ReqForQuotationAprv.updateWFStatus() calling......03/FEB/26 " + domString);
BaseLogger.log("3", null, null, "ReqForQuotationAprv.updateWFStatus() xtraParams:: " + xtraParams);
String retVal = "";
String refSer = "";
String tranID = "";
String signStatus = "";
String enterprises1 = "";
boolean isLocalConn = false;
PreparedStatement rfqPstmt = null;
PreparedStatement mgmntApprvUpdatePs = null;
ResultSet mgmntApprvUpdateRs = null;
ResultSet rfqRs = null;
PreparedStatement rfqDetPstmt = null;
ResultSet rfqDetRs = null;
PreparedStatement pStmt = null;
ResultSet rs = null;
PreparedStatement rateComparePstmt = null;
ResultSet rateCompareRs = null;
PreparedStatement checkAppr2StatusPs = null;
ResultSet checkAppr2StatusRs = null;
PreparedStatement updateWfStatusPstmt = null;
ResultSet updateWfStatusRs = null;
String empCode = "";
String entityCode = "", reportTo = "", allocatedTo;
LocalDate tranDate = LocalDate.now();// for tranDate
LocalTime tranTime = LocalTime.now();
String tranDateStr = formatDateTime(tranDate, tranTime);
String reqId = "";
String custCode = "";
String quotType = "";
String itemSer = "";
String validUpto = "";
String executed = "";
String tranIdOrg = "";
String activeYn = "";
String itmVer = "";
String chgUser = "";
String chgTerm = "";
String confirmed = "";
String empCodeAprv = "";
String enterprises = "";
String applDb = "";
String execRights = "";
int domId = 0;
String tranIdRef = "";
String quantity = "";
String lineNo = "";
String itemCode = "";
int rate = 0;
String wrkflwInit = "";
// InitialContext ctx = null;
String retString = "";
String executionRights = "";
String objName = "";
String roleCodeSign = "";
StringBuffer processDynXmlString = new StringBuffer();
XML2DBEJB xml2dbObj = new XML2DBEJB();
Boolean tranFlag = false;
String processRequestResult = "";
try {
UserInfoBean userInfo = new UserInfoBean();
userInfo = getUserInfo();
CommonDBAccessEJB commonDBAccessEJB = new CommonDBAccessEJB();
AppConnectParm appConnect = new AppConnectParm();
InitialContext initialContext = new InitialContext(appConnect.getProperty());
System.out.println("ReqForQuotationAprv.updateWFStatus() domString test[" + domString + "]!!!!!!!");
if (conn == null) {
// ConnDriver connDriver = new ConnDriver();
// conn = connDriver.getConnectDB(userInfo.getTransDB());
conn = getConnection();
}
Document dom = genericUtility.parseString(domString);
signStatus = checkNull(genericUtility.getColumnValue("sign_status", dom, "1"));
refSer = checkNull(genericUtility.getColumnValue("ref_ser", dom, "1"));
tranID = checkNull(genericUtility.getColumnValue("ref_id", dom));
signStatus = checkNull(genericUtility.getColumnValue("sign_status", dom, "1"));
empCode = checkNull(genericUtility.getColumnValue("emp_code", dom));
allocatedTo = checkNull(genericUtility.getColumnValue("allocated_to", dom, "1"));
objName = checkNull(genericUtility.getColumnValue("obj_name", dom, "1"));
roleCodeSign = checkNull(genericUtility.getColumnValue("role_code__sign", dom, "1"));
if (roleCodeSign.equalsIgnoreCase("VHB_MNGMNT") && signStatus.equalsIgnoreCase("S")) {
String updateWfStatusSql = "update SALES_QUOT_REQ set WF_STATUS = 'A' where tran_id = ?";
updateWfStatusPstmt = conn.prepareStatement(updateWfStatusSql);
updateWfStatusPstmt.setString(1, tranID);
int updateWfStatusCount = updateWfStatusPstmt.executeUpdate();
System.out.println("updateWfStatusCount::::: =[" + updateWfStatusCount + "]");
}
System.out.println("enterprises::::: =[" + enterprises1 + "]" + " tran_id::" + tranID);
userInfo = commonDBAccessEJB.createUserInfo(empCode);
System.out.println("signStatus::" + signStatus + " enterprises::::: =[" + enterprises1 + "]"
+ " tran_id::" + tranID + " empCode::" + empCode);
// Changes on 06-Feb-26 by Ajit [Start]
String rateCompareSql = "SELECT RATE_QUOT,PTR,RATE_APPRV,LINE_NO FROM SALES_QUOT_REQ_DET WHERE tran_id = ?";
rateComparePstmt = conn.prepareStatement(rateCompareSql);
rateComparePstmt.setString(1, tranID);
rateCompareRs = rateComparePstmt.executeQuery();
while (rateCompareRs.next()) {
BigDecimal rateQuot = rateCompareRs.getBigDecimal("RATE_QUOT");
BigDecimal rateApprv = rateCompareRs.getBigDecimal("RATE_APPRV");
BigDecimal PTR = rateCompareRs.getBigDecimal("PTR");
boolean directsquotCreationFlag = true;
int lineNoOfDet = rateCompareRs.getInt("LINE_NO");
// Added a condition if PTR and special rate is equal directly create the sales
// quotation transaction
if (PTR.compareTo(rateQuot) == 0) {
directsquotCreationFlag = false;
}
if (rateApprv.compareTo(rateQuot) > 0) {
tranFlag = true;
}
BaseLogger.log("3", null, null, "rateQuot:: " + rateQuot + " PTR::" + PTR + " tranFlag:: " + tranFlag);
if (!directsquotCreationFlag) {
BaseLogger.log("3", null, null, "Direct Sales Quotation Creation");
tranFlag = false;
}
}
if (rateComparePstmt != null) {
rateComparePstmt.close();
rateComparePstmt = null;
}
if (rateCompareRs != null) {
rateCompareRs.close();
rateCompareRs = null;
}
BaseLogger.log("3", null, null, " tranFlag:: " + tranFlag);
if (tranFlag) {
String checkAppr2StatusSql = "SELECT WF_STATUS FROM SALES_QUOT_REQ WHERE tran_id = ?";
checkAppr2StatusPs = conn.prepareStatement(checkAppr2StatusSql);
checkAppr2StatusPs.setString(1, tranID);
checkAppr2StatusRs = checkAppr2StatusPs.executeQuery();
while (checkAppr2StatusRs.next()) {
String wfStatus = checkNull(checkAppr2StatusRs.getString("WF_STATUS"));
if (wfStatus.equalsIgnoreCase("A")) {
tranFlag = false;
}
}
}
if (checkAppr2StatusRs != null) {
checkAppr2StatusRs.close();
checkAppr2StatusRs = null;
}
if (checkAppr2StatusPs != null) {
checkAppr2StatusPs.close();
checkAppr2StatusPs = null;
}
BaseLogger.log("3", null, null, "in ReqForQuotationAprv tranFlag::" + tranFlag);
if (!tranFlag) {
BaseLogger.log("3", null, null, "in ReqForQuotationAprv tranFlag true condition::");
String rfqSql = "select CUST_CODE ,QUOT_TYPE ,ITEM_SER, VALID_UPTO "
+ "FROM SALES_QUOT_REQ where tran_id = ?";
rfqPstmt = conn.prepareStatement(rfqSql);
rfqPstmt.setString(1, tranID);
rfqRs = rfqPstmt.executeQuery();
while (rfqRs.next()) {
// Directly fetching and using values from result set without declaring
// variables
custCode = checkNull(rfqRs.getString("CUST_CODE"));
quotType = checkNull(rfqRs.getString("QUOT_TYPE"));
itemSer = checkNull(rfqRs.getString("ITEM_SER"));
validUpto = checkNull(rfqRs.getString("VALID_UPTO"));
// Do something with these values directly (e.g., use them in logic, or store in
// a collection)
System.out.println("custCode: " + custCode + ", " + "quotType: " + quotType + ", " + "validUpto: "
+ validUpto + ", " + "itemSer: " + itemSer);
}
// Start building the XML string
processDynXmlString.append("<DocumentRoot><description>Datawindow Root</description><group0>");
processDynXmlString.append(
"<description>Group0 description</description><Header0><description>Header0 members</description>");
processDynXmlString.append("<objName><![CDATA[salesquot]]></objName>");
processDynXmlString.append("<pageContext><![CDATA[1]]></pageContext>");
processDynXmlString.append("<objContext><![CDATA[1]]></objContext>");
processDynXmlString.append("<editFlag><![CDATA[A]]></editFlag>");
processDynXmlString.append("<focusedColumn><![CDATA[]]></focusedColumn>");
processDynXmlString.append("<action><![CDATA[SAVE]]></action>");
processDynXmlString.append("<elementName><![CDATA[]]></elementName>");
processDynXmlString.append("<keyValue><![CDATA[1]]></keyValue>");
processDynXmlString.append("<saveLevel><![CDATA[1]]></saveLevel>");
processDynXmlString.append("<forcedSave><![CDATA[false]]></forcedSave>");
processDynXmlString.append("<Detail1 dbID='' domID='1' objContext='1' objName='salesquot'>");
processDynXmlString
.append("<attribute IS_CHANGE='Y' pkNames='TRAN_ID' selected='N' status='N' updateFlag='A'/>");
// processDynXmlString.append("<tran_id/>");
processDynXmlString.append("<inq_date><![CDATA[" + dateFormatter(tranDateStr) + "]]></inq_date>");
processDynXmlString.append("<cust_code><![CDATA[" + custCode + "]]></cust_code>");
processDynXmlString.append("<quot_type><![CDATA[" + quotType + "]]></quot_type>");
processDynXmlString.append("<item_ser><![CDATA[" + itemSer.trim() + "]]></item_ser>");
processDynXmlString.append("<eff_date><![CDATA[" + dateFormatter(tranDateStr) + "]]></eff_date>");
processDynXmlString.append("<valid_upto><![CDATA[" + dateFormatter(validUpto) + "]]></valid_upto>");
processDynXmlString.append("<curr_code><![CDATA[INR]]></curr_code>");
processDynXmlString.append("<confirmed><![CDATA[N]]></confirmed>");
processDynXmlString.append("</Detail1>");
String rfqDet1Sql = "SELECT LINE_NO, ITEM_CODE, QTY_QUOT, RATE_QUOT FROM SALES_QUOT_REQ_DET WHERE tran_id = ?";
rfqDetPstmt = conn.prepareStatement(rfqDet1Sql);
rfqDetPstmt.setString(1, tranID);
rfqDetRs = rfqDetPstmt.executeQuery();
while (rfqDetRs.next()) {
domId++;
BaseLogger.log("3", getUserInfo(), null, "domId :: " + domId);
lineNo = checkNull(rfqDetRs.getString("LINE_NO"));
itemCode = checkNull(rfqDetRs.getString("ITEM_CODE"));
quantity = checkNull(rfqDetRs.getString("QTY_QUOT"));
rate = rfqDetRs.getInt("RATE_QUOT");
String resvisedRateSql = "select PHY_ATTRIB_13,PHY_ATTRIB_14,PHY_ATTRIB_15 from item where item_code = ?";
PreparedStatement resvisedRatePstmt = conn.prepareStatement(resvisedRateSql);
resvisedRatePstmt.setString(1, itemCode);
ResultSet resvisedRateRs = resvisedRatePstmt.executeQuery();
while (resvisedRateRs.next()) {
String distMarginStr = checkNull(resvisedRateRs.getString("PHY_ATTRIB_13"));
String stockMarginStr = checkNull(resvisedRateRs.getString("PHY_ATTRIB_14"));
String retailMarginStr = checkNull(resvisedRateRs.getString("PHY_ATTRIB_15"));
int distMargin = 0;
int stockMargin = 0;
int retailMargin = 0;
try {
distMargin = (distMarginStr != null && distMarginStr.trim().length() > 0)
? Integer.parseInt(distMarginStr.trim())
: 0;
stockMargin = (stockMarginStr != null && stockMarginStr.trim().length() > 0)
? Integer.parseInt(stockMarginStr.trim())
: 0;
retailMargin = (retailMarginStr != null && retailMarginStr.trim().length() > 0)
? Integer.parseInt(retailMarginStr.trim())
: 0;
} catch (Exception e) {
BaseLogger.log("3", null, null, "Error parsing margin values");
}
BigDecimal rateBD = new BigDecimal(rate);
BigDecimal hundred = new BigDecimal(100);
BigDecimal retailMarginBD = new BigDecimal(retailMargin);
BigDecimal stockMarginBD = new BigDecimal(stockMargin);
BigDecimal distMarginBD = new BigDecimal(distMargin);
BaseLogger.log("3", null, null, "distMargin::" + distMargin + " stockMargin::" + stockMargin
+ " revisedPtd:: " + retailMargin + "rate:: " + rate + "]");
BigDecimal revisedPtr = rateBD.divide(hundred.multiply(hundred.subtract(retailMarginBD)), 6,
RoundingMode.HALF_UP);
BigDecimal revisedPts = rateBD.divide(hundred.multiply(hundred.subtract(stockMarginBD)), 6,
RoundingMode.HALF_UP);
BigDecimal revisedPtd = rateBD.divide(hundred.multiply(hundred.subtract(distMarginBD)), 6,
RoundingMode.HALF_UP);
BaseLogger.log("3", null, null, "revisedPtr::" + revisedPtr + " revisedPts::" + revisedPts
+ " revisedPtd::" + revisedPtd);
BaseLogger.log("3", null, null, "revisedPtr::" + revisedPtr + " revisedPts::" + revisedPts
+ " revisedPtd:: " + revisedPtd + "]");
processDynXmlString.append(
"<Detail2 dbID=':' domID='" + domId + "' objName=\"salesquot\" objContext=\"2\">");
processDynXmlString.append(
"<attribute pkNames=\"quot_no:line_no:\" selected=\"N\" updateFlag=\"A\" status=\"N\" />");
processDynXmlString.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
processDynXmlString.append("<item_code><![CDATA[").append(itemCode).append("]]></item_code>");
processDynXmlString.append("<quantity><![CDATA[").append(quantity).append("]]></quantity>");
processDynXmlString.append("<rate><![CDATA[").append(rate).append("]]></rate>");
processDynXmlString.append("<ptr><![CDATA[").append(revisedPtr).append("]]></ptr>");
processDynXmlString.append("<pts><![CDATA[").append(revisedPts).append("]]></pts>");
processDynXmlString.append("<ptd><![CDATA[").append(revisedPtd).append("]]></ptd>");
processDynXmlString.append("</Detail2>");
}
}
if (rfqDetRs != null) {
rfqDetRs.close();
rfqDetRs = null;
}
if (rfqDetPstmt != null) {
rfqDetPstmt.close();
rfqDetPstmt = null;
}
// Detail3 XML Building start - 19 March
/*
* BaseLogger.log("3", null, null, "quotType::" + quotType.toString() + "]");
* BaseLogger.log("3", null, null, "custCode::" + custCode.toString() + "]");
*
* if(quotType.trim().equalsIgnoreCase("G")) { String fetchHospitalMappingSql =
* "select CUST_CODE__STK, CUST_CODE__DIST from CUSTOMER_PREF_DIST where CUST_CODE = ?"
* ; PreparedStatement fetchHospitalMappingPs =
* conn.prepareStatement(fetchHospitalMappingSql);
* fetchHospitalMappingPs.setString(1, custCode); ResultSet
* fetchHospitalMappingRs = fetchHospitalMappingPs.executeQuery();
* while(fetchHospitalMappingRs.next()) {
* processDynXmlString.append("<Detail3 dbID=':' domID='"
* +domId+"' objName=\"salesquot\" objContext=\"3\">"); processDynXmlString.
* append("<attribute pkNames=\"quot_no:line_no:\" selected=\"N\" updateFlag=\"A\" status=\"N\" />"
* ); processDynXmlString.append("<line_no><![CDATA[").append(lineNo).append(
* "]]></line_no>");
* processDynXmlString.append("<cust_code><![CDATA[").append(custCode).append(
* "]]></cust_code>");
* processDynXmlString.append("<cust_code__stk><![CDATA[").append(checkNull(
* fetchHospitalMappingRs.getString("CUST_CODE__STK"))).append(
* "]]></cust_code__stk>");
* processDynXmlString.append("<cust_code__dist><![CDATA[").append(checkNull(
* fetchHospitalMappingRs.getString("CUST_CODE__DIST"))).append(
* "]]></cust_code__dist>"); processDynXmlString.append("</Detail3>"); } }
*/
// Detail3 XML Building end - 19 March
processDynXmlString.append("</Header0></group0></DocumentRoot>");
// Changes on 06-Feb-26 by Ajit [End]
// Now, processDynXmlString contains the dynamically generated XML with the
// values from your query
System.out.println(processDynXmlString.toString());
// String tranIDNew = commonDBAccessEJB.generateSeq("asset_assign_seq", 10, userInfo.getTransDB());
// BaseLogger.log("3", null, null, " generated tranID :: " + tranIDNew); // tran_id
// processDynXmlString.toString().replace("{{tranId}}", tranIdNew);
String salesQuotXml = processDynXmlString.toString();
BaseLogger.log("3", null, null, "userInfo::" + userInfo.toString() + "]");
BaseLogger.log("3", null, null, "salesQuotXml::" + salesQuotXml.toString() + "]");
String tranDb = userInfo.getTransDB();
BaseLogger.log("3", null, null, "tranDb::" + tranDb.toString() + "]");
MasterStatefulLocal masterStateful = (MasterStatefulLocal) initialContext
.lookup("ibase/MasterStatefulEJB/local");
processRequestResult = masterStateful.processRequest(userInfo, salesQuotXml, false, conn);
BaseLogger.log("3", null, null, "processRequest processRequestResultD" + processRequestResult + "]");
if (processRequestResult != null && processRequestResult.trim().length() > 0) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(processRequestResult));
Document doc = builder.parse(is);
// 🔹 Check SUCCESS
NodeList detailNodeList = doc.getElementsByTagName("Detail");
if (detailNodeList != null && detailNodeList.getLength() > 0) {
String detailValue = detailNodeList.item(0).getTextContent();
if ("Success".equalsIgnoreCase(detailValue)) {
BaseLogger.log("3", null, null, "Transaction SUCCESS");
// ✅ Success logic
String tranIdOfSalesQuotaion = doc.getElementsByTagName("TranID").item(0)
.getTextContent();
BaseLogger.log("3", null, null,
"Generated Sales Quotaion TranID: " + tranIdOfSalesQuotaion);
BaseLogger.log("3", null, null, "RFQ TranID: " + tranID);
String UpdateRfqIdInSQuotSql = "update sales_quot set TRAN_ID__RFQ = ? where QUOT_NO = ?";
PreparedStatement UpdateRfqIdInSQuotPs = conn.prepareStatement(UpdateRfqIdInSQuotSql);
UpdateRfqIdInSQuotPs.setString(1, tranID);
UpdateRfqIdInSQuotPs.setString(2, tranIdOfSalesQuotaion);
int UpdateRfqIdInSQuotCount = UpdateRfqIdInSQuotPs.executeUpdate();
BaseLogger.log("3", null, null,
"Sales Quotation TRAN_ID__RFQ upadate count: " + UpdateRfqIdInSQuotCount);
//Commect out the code of Confirmed the transaction on Approval of WF
/*
* String updateConfirm =
* "update sales_quot_req set confirmed = 'Y' where tran_id = ?";
* PreparedStatement updateConfirmPs = conn.prepareStatement(updateConfirm);
* updateConfirmPs.setString(1, tranID); int updateConfirmCount =
* updateConfirmPs.executeUpdate(); BaseLogger.log("3", null, null,
* "RFQ Confirmed Column update count: " + updateConfirmCount);
*
* if (updateConfirmPs != null) { updateConfirmPs.close(); updateConfirmPs =
* null; }
*/
}
} else {
BaseLogger.log("3", null, null, "Sales Quotation Generation Failed: ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
retString = processRequestResult;
}
} catch (Exception e) {
e.printStackTrace();
throw new ITMException(e);
} finally {
try {
BaseLogger.log("3", null, null, "Finally block executed - connection NOT closed (container managed)");
// Close only statements/resultsets if still open
if (rfqRs != null) rfqRs.close();
if (rfqPstmt != null) rfqPstmt.close();
if (rfqDetRs != null) rfqDetRs.close();
if (rfqDetPstmt != null) rfqDetPstmt.close();
//if (conn != null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
BaseLogger.log("3", null, null, "returning retString [" + retString + "]");
return retString;
}
public static String formatDateTime(LocalDate tranDate, LocalTime tranTime) {
LocalDateTime tranDateTime = LocalDateTime.of(tranDate, tranTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = tranDateTime.format(formatter);
return formattedDateTime;
}
public static String dateFormatter(String inputDateString) {
String result = "";
String inputFormat = "yyyy-MM-dd HH:mm:ss";
String outputFormat = "dd/MM/yy HH:mm:ss";
try {
// Parse input date string
SimpleDateFormat inputFormatter = new SimpleDateFormat(inputFormat);
Date date = inputFormatter.parse(inputDateString);
// Format the date in the desired output format
SimpleDateFormat outputFormatter = new SimpleDateFormat(outputFormat);
String outputDateString = outputFormatter.format(date);
System.out.println("Input Date: " + inputDateString);
System.out.println("Formatted Date: " + outputDateString);
result = outputDateString;
} catch (Exception e) {
BaseLogger.log("3", null, null, "Exception in dateFormatter. [" + E12GenericUtility.getStackTrace(e) + "]");
}
return result;
}
private String checkNull(String input) {
if (input == null) {
input = "";
}
return input;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment