Make to order line to future date based on lead time at branch level
Hi Experts,
This is my first C assignment. I have req where we need to calculate Lead time based on Item branch, right now it has been written on Item Master. F4101 has only 1 index where as new req is on item branch which two indices. Pls advise in fixing this. I wrote the logic bt need some guidance if I'm doing it right? Pls help.
=========================================
Original Logic
=========================================
/* Move dates forward based on lead time (PRP2) in F4101 */
jdeStrcpy((JCHAR *)(dsD4201610.szSalesOrderEntryVersion),(const JCHAR *)(lpDS->szCMVersion));
jdeCallObject(_J("RetrieveSOEProcessingOptions"),
NULL, lpBhvrCom,lpVoid,(LPVOID)&dsD4201610,
(CALLMAP *)NULL,(int)0,(JCHAR *)NULL,
(JCHAR *)NULL,(int)0);
if (dsD4201610.cTHCMTODateLogic == _J('1'))
{
idReturnValue = I4200310_CalculateRequestedDate(lpBhvrCom, lpVoid, lpDS,
lpds4200310B);
}
/**********************************************************************
* Function: I4200310_CalculateRequestedDate
* Notes: Retrieve the record from F4101 for the given Item Number.
*
* Returns:
*
* Parameters: szIdentifier2ndItem - item number to use for fetch
* hRequestF4101 - valid request handle used to open F4101
*
**************************************************************************/
ID I4200310_CalculateRequestedDate (LPBHVRCOM lpBhvrCom,LPVOID lpVoid,
LPDSD4200310F lpDS, LPDS4200310B lpds4200310B)
{
/************************************************************************
* Variable declarations
************************************************************************/
HREQUEST hRequestF4101 = (HREQUEST) NULL ;
HUSER hUser = (HUSER) NULL;
ID idJDBReturn = JDEDB_PASSED ;
NID szF4101TableID = NID_F4101;
ID idF4101IndexID2 = ID_F4101_2ND_ITEM_NUMBER;
NID szColArrayF4101[3] = {NID_LITM, NID_PRP2};
JCHAR szTHC_Branch[13] = {0};
JCHAR szTHCItemNo [26] = {0};
JCHAR szTHCPurchasingReportCode2[4] = {0};
long lDaysToAdvance = 0;
long lDateDifference = 0;
MATH_NUMERIC mnNumberOfDaysPerWeek7 = {0};
MATH_NUMERIC mnDays = {0};
MATH_NUMERIC mnWeeks = {0};
JDEDATE jdTempDate = {0};
ID idReturnValue = ER_SUCCESS;
/************************************************************************
* Declare structures
************************************************************************/
/*F4101 dsF4101 = {0};*/ /* Item Master Table */
KEY2_F4101 dsF4101Key2 = {0}; /* Item Master Key */
DSDE0022 dsDE0022;
/************************************************************************
* Business Function structures
************************************************************************/
DS_D5542310_COLUMN_F4101 dsColArrayF4101 = {0};
DSD5542010A dsD5542010A = {0};
LPF42UI01 lpdsF42UI01 = lpds4200310B->lpdsF42UI01;
DSD0000580 ds0000580ConvertStringToMathNumeric = {0}; /* Convert String to math Numeric - B4000580 */
/************************************************************************
* Check for NULL pointers
************************************************************************/
/************************************************************************
* Initialize Behavior Routine
************************************************************************/
/* Initialize hUser */
idJDBReturn = JDB_InitBhvr((void *)lpBhvrCom, &hUser, (JCHAR *) NULL, JDEDB_COMMIT_AUTO);
if (idJDBReturn != JDEDB_PASSED)
{
jdeSetGBRError(lpBhvrCom, lpVoid, 0L, _J("078S"));
return ER_ERROR;
}
/************************************************************************
* Variable initializations
************************************************************************/
ParseNumericString(&mnNumberOfDaysPerWeek7,_J("7"));
/************************************************************************
* Main Processing
/************************************************************************
/* Initialize Data Structure */
/*memset ((void *)(&dsF4101),(int)(_J('\0')), sizeof(F4101)); */
memset ((void *)(&dsF4101Key2),(int)(_J('\0')), sizeof(KEY2_F4101));
memset((void *)(&dsColArrayF4101), (int)(_J('\0')), sizeof(DS_D5542310_COLUMN_F4101));
memset((void *)(&dsDE0022),(int)(_J('\0')),sizeof(dsDE0022));
memset((void *)(&ds0000580ConvertStringToMathNumeric),
(int)(_J('\0')),sizeof(ds0000580ConvertStringToMathNumeric));
/* Retrieve correct item number */
if (IsStringBlank(lpDS->szBusinessUnit))
{
jdeStrcpy((JCHAR *)(szTHC_Branch),(const JCHAR *)(lpds4200310B->lpdsF42UI01->zhmcu));
}
else
{
jdeStrcpy((JCHAR *)(szTHC_Branch),(const JCHAR *)(lpDS->szBusinessUnit));
}
if (IsStringBlank(lpDS->szLineType))
{
jdeStrcpy((JCHAR *)(dsD5542010A.szItemNoUnknownFormat),(const JCHAR *)(lpDS->szItemNo));
jdeStrcpy((JCHAR *)(dsD5542010A.szCostCenter),(const JCHAR *)(szTHC_Branch));
MathCopy(&dsD5542010A.mnAddressNumber,&lpds4200310B->lpdsF42UI01->zhan8);
idReturnValue = jdeCallObject(_J("THCVerifyItem"),
NULL, lpBhvrCom,lpVoid,(LPVOID)&dsD5542010A,
(CALLMAP *)NULL,(int)0,(JCHAR *)NULL,
(JCHAR *)NULL,(int)0);
if (idReturnValue == ER_SUCCESS)
{
jdeStrcpy((JCHAR *)szTHCItemNo,(const JCHAR *)(dsD5542010A.szIdentifier2ndItem));
}
}
/* Open Item Master Table */
idJDBReturn = JDB_OpenTable(hUser, szF4101TableID, idF4101IndexID2,
szColArrayF4101, (ushort)(2),
(JCHAR *) NULL, &hRequestF4101);
if (idJDBReturn == JDEDB_FAILED) /* if Open Table failed */
{
memset((void *) (&dsDE0022), (int) ('\0'), sizeof(dsDE0022));
jdeStrcpy((JCHAR *)dsDE0022.szDescription, (const JCHAR *) _J("F4211"));
jdeSetGBRErrorSubText(lpBhvrCom, lpVoid, (ID) 0, _J("078D"), &dsDE0022);
JDB_FreeBhvr(hUser);
return ER_ERROR;
}
else /* if Open Table Passed */ /* if(idJDBReturn == JDEDB_PASSED) */
{
/* Load the F4101 primary key */
jdeStrcpy ((JCHAR *)dsF4101Key2.imlitm, (const JCHAR *)szTHCItemNo);
/* Fetch the F4101 with only 1 field using the internal data structure */
idJDBReturn = JDB_FetchKeyed(hRequestF4101, (ID) 0, (void *)(&dsF4101Key2),
(short)(1), (void *)(&dsColArrayF4101),
(int) (FALSE));
if( idJDBReturn == JDEDB_PASSED ) /* if Fetch Success */
{
jdeStrcpy((JCHAR *)szTHCPurchasingReportCode2, dsColArrayF4101.imprp2);
/* Convert the PRP2 to MathNumeric */
jdeStrncpy(ds0000580ConvertStringToMathNumeric.szInputString,
(const JCHAR *)szTHCPurchasingReportCode2, DIM(ds0000580ConvertStringToMathNumeric.szInputString)-1);
jdeCallObject(_J("ConvertStringToMathNumeric"),
NULL,
lpBhvrCom, lpVoid,
(LPVOID)&ds0000580ConvertStringToMathNumeric,
(CALLMAP *) NULL,
(int) 0, (JCHAR *) NULL,
(JCHAR *) NULL, (int) 0);
MathCopy(&mnWeeks, &ds0000580ConvertStringToMathNumeric.mnOutputMathNumeric);
}
} /* Endif Open Table Passed */
if (MathZeroTest(&mnWeeks) != 0)
{
/*If Hdr requested date and Dtl requested date do not match, this means the requested date has been changed by the user */
if ((memcmp((void *)&lpdsF42UI01->zhdrqj, (void *)&lpDS->jdRequestedDate,sizeof(JDEDATE)) != 0))
{
/* Do Nothing */
}
else /* If Hdr requested date is equal to Dtl requested date....*/
{
/* If number of days to advance is greater than zero, advance requested date */
MathMultiply(&mnDays, &mnWeeks, &mnNumberOfDaysPerWeek7);
MathNumericToLong(&mnDays, &lDaysToAdvance);
AdvanceDate (&jdTempDate, &lpdsF42UI01->zhtrdj, 0, lDaysToAdvance);
/* If calculated date is greater than detail requested date - advance the requested date */
lDateDifference = DateDifference(&jdTempDate, &lpDS->jdRequestedDate);
if (lDateDifference < 0)
{
memcpy((void *)&lpDS->jdRequestedDate, (const void *)&jdTempDate, sizeof(JDEDATE));
}
}
}
/************************************************************************
* Function Clean Up
************************************************************************/
if (hUser != (HUSER) NULL)
{
JDB_FreeBhvr (hUser);
}
if (hRequestF4101 != (HREQUEST)NULL)
{
JDB_CloseTable(hRequestF4101);
}
return (idReturnValue);
}
============================================
NEW Logic on F4102
==============================================
jdeStrcpy((JCHAR *)(dsD4201610.szSalesOrderEntryVersion),(const JCHAR *)(lpDS->szCMVersion));
jdeCallObject(_J("RetrieveSOEProcessingOptions"),
NULL, lpBhvrCom,lpVoid,(LPVOID)&dsD4201610,
(CALLMAP *)NULL,(int)0,(JCHAR *)NULL,
(JCHAR *)NULL,(int)0);
if (dsD4201610.cTHCMTODateLogic == _J('1'))
{
idReturnValue = I4200310_CalculateRequestedDate(lpBhvrCom, lpVoid, lpDS,
lpds4200310B);
}
/**********************************************************************
* Function: I4200310_CalculateRequestedDate
* Notes: Retrieve the record from F4102 for the given Item Branch.
*
* Returns:
*
* Parameters: szIdentifier2ndItem - item number to use for fetch
szBranch - Branch to use for fetch
* hRequestF4102 - valid request handle used to open F4101
*
**************************************************************************/
ID I4200310_CalculateRequestedDate (LPBHVRCOM lpBhvrCom,LPVOID lpVoid,
LPDSD4200310F lpDS, LPDS4200310B lpds4200310B)
{
/************************************************************************
* Variable declarations
************************************************************************/
HREQUEST hRequestF4102 = (HREQUEST) NULL ;
HUSER hUser = (HUSER) NULL;
ID idJDBReturn = JDEDB_PASSED ;
NID szF4102TableID = NID_F4102;
ID idF4102IndexID2 = ID_F4102_2ND_ITEM_NUMBER;
ID idF4102IndexID2 = ID_F4102_Branch;
NID szColArrayF4102[4] = {NID_LITM, NID_MCU, NID_PRP2};
JCHAR szTHC_Branch[13] = {0};
JCHAR szTHCItemNo [26] = {0};
JCHAR szTHCPurchasingReportCode2[4] = {0};
long lDaysToAdvance = 0;
long lDateDifference = 0;
MATH_NUMERIC mnNumberOfDaysPerWeek7 = {0};
MATH_NUMERIC mnDays = {0};
MATH_NUMERIC mnWeeks = {0};
JDEDATE jdTempDate = {0};
ID idReturnValue = ER_SUCCESS;
/************************************************************************
* Declare structures
************************************************************************/
/*F4102 dsF4102 = {0};*/ /* Item Master Table */
KEY2_F4102 dsF4102Key2 = {0}; /* Item Master Key */
DSDE0022 dsDE0022;
/************************************************************************
* Business Function structures
************************************************************************/
DS_D5542310_COLUMN_F4101 dsColArrayF4101 = {0};
DSD5542010A dsD5542010A = {0};
LPF42UI01 lpdsF42UI01 = lpds4200310B->lpdsF42UI01;
DSD0000580 ds0000580ConvertStringToMathNumeric = {0}; /* Convert String to math Numeric - B4000580 */
/************************************************************************
* Check for NULL pointers
************************************************************************/
/************************************************************************
* Initialize Behavior Routine
************************************************************************/
/* Initialize hUser */
idJDBReturn = JDB_InitBhvr((void *)lpBhvrCom, &hUser, (JCHAR *) NULL, JDEDB_COMMIT_AUTO);
if (idJDBReturn != JDEDB_PASSED)
{
jdeSetGBRError(lpBhvrCom, lpVoid, 0L, _J("078S"));
return ER_ERROR;
}
/************************************************************************
* Variable initializations
************************************************************************/
ParseNumericString(&mnNumberOfDaysPerWeek7,_J("7"));
/************************************************************************
* Main Processing
/************************************************************************
/* Initialize Data Structure */
/*memset ((void *)(&dsF4101),(int)(_J('\0')), sizeof(F4101)); */
memset ((void *)(&dsF4102Key2),(int)(_J('\0')), sizeof(KEY2_F4102));
memset((void *)(&dsColArrayF4102), (int)(_J('\0')), sizeof(DS_D5542310_COLUMN_F4102));
memset((void *)(&dsDE0022),(int)(_J('\0')),sizeof(dsDE0022));
memset((void *)(&ds0000580ConvertStringToMathNumeric),
(int)(_J('\0')),sizeof(ds0000580ConvertStringToMathNumeric));
/* Retrieve correct item number */
if (IsStringBlank(lpDS->szBusinessUnit))
{
jdeStrcpy((JCHAR *)(szTHC_Branch),(const JCHAR *)(lpds4200310B->lpdsF42UI01->zhmcu));
}
else
{
jdeStrcpy((JCHAR *)(szTHC_Branch),(const JCHAR *)(lpDS->szBusinessUnit));
}
if (IsStringBlank(lpDS->szLineType))
{
jdeStrcpy((JCHAR *)(dsD5542010A.szItemNoUnknownFormat),(const JCHAR *)(lpDS->szItemNo));
jdeStrcpy((JCHAR *)(dsD5542010A.szCostCenter),(const JCHAR *)(szTHC_Branch));
MathCopy(&dsD5542010A.mnAddressNumber,&lpds4200310B->lpdsF42UI01->zhan8);
idReturnValue = jdeCallObject(_J("THCVerifyItem"),
NULL, lpBhvrCom,lpVoid,(LPVOID)&dsD5542010A,
(CALLMAP *)NULL,(int)0,(JCHAR *)NULL,
(JCHAR *)NULL,(int)0);
if (idReturnValue == ER_SUCCESS)
{
jdeStrcpy((JCHAR *)szTHCItemNo,(const JCHAR *)(dsD5542010A.szIdentifier2ndItem));
jdeStrcpy((JCHAR *)szTHC_Branch,(const JCHAR *)(dsD5542010A.szBranch))
}
}
/* Open Item Branch Table */
idJDBReturn = JDB_OpenTable(hUser, szF4102TableID, idF4102IndexID2,
szColArrayF4102, (ushort)(2),
(JCHAR *) NULL, &hRequestF4102);
if (idJDBReturn == JDEDB_FAILED) /* if Open Table failed */
{
memset((void *) (&dsDE0022), (int) ('\0'), sizeof(dsDE0022));
jdeStrcpy((JCHAR *)dsDE0022.szDescription, (const JCHAR *) _J("F4211"));
jdeSetGBRErrorSubText(lpBhvrCom, lpVoid, (ID) 0, _J("078D"), &dsDE0022);
JDB_FreeBhvr(hUser);
return ER_ERROR;
}
else /* if Open Table Passed */ /* if(idJDBReturn == JDEDB_PASSED) */
{
/* Load the F4102 primary key */
jdeStrcpy ((JCHAR *)dsF4101Key2.iblitm, (const JCHAR *)szTHCItemNo);
jdeStrcpy ((JCHAR *)dsF4101Key2.ibmcu, (const JCHAR *)szTHC_Branch);
/* Fetch the F4101 with only 1 field using the internal data structure */
idJDBReturn = JDB_FetchKeyed(hRequestF4102, (ID) 0, (void *)(&dsF4102Key2),
(short)(1), (void *)(&dsColArrayF4102),
(int) (FALSE));
if( idJDBReturn == JDEDB_PASSED ) /* if Fetch Success */
{
jdeStrcpy((JCHAR *)szTHCPurchasingReportCode2, dsColArrayF4102.ibprp2);
/* Convert the PRP2 to MathNumeric */
jdeStrncpy(ds0000580ConvertStringToMathNumeric.szInputString,
(const JCHAR *)szTHCPurchasingReportCode2, DIM(ds0000580ConvertStringToMathNumeric.szInputString)-1);
jdeCallObject(_J("ConvertStringToMathNumeric"),
NULL,
lpBhvrCom, lpVoid,
(LPVOID)&ds0000580ConvertStringToMathNumeric,
(CALLMAP *) NULL,
(int) 0, (JCHAR *) NULL,
(JCHAR *) NULL, (int) 0);
MathCopy(&mnWeeks, &ds0000580ConvertStringToMathNumeric.mnOutputMathNumeric);
}
} /* Endif Open Table Passed */
if (MathZeroTest(&mnWeeks) != 0)
{
/*If Hdr requested date and Dtl requested date do not match, this means the requested date has been changed by the user */
if ((memcmp((void *)&lpdsF42UI01->zhdrqj, (void *)&lpDS->jdRequestedDate,sizeof(JDEDATE)) != 0))
{
/* Do Nothing */
}
else /* If Hdr requested date is equal to Dtl requested date....*/
{
/* If number of days to advance is greater than zero, advance requested date */
MathMultiply(&mnDays, &mnWeeks, &mnNumberOfDaysPerWeek7);
MathNumericToLong(&mnDays, &lDaysToAdvance);
AdvanceDate (&jdTempDate, &lpdsF42UI01->zhtrdj, 0, lDaysToAdvance);
/* If calculated date is greater than detail requested date - advance the requested date */
lDateDifference = DateDifference(&jdTempDate, &lpDS->jdRequestedDate);
if (lDateDifference < 0)
{
memcpy((void *)&lpDS->jdRequestedDate, (const void *)&jdTempDate, sizeof(JDEDATE));
}
}
}
/************************************************************************
* Function Clean Up
************************************************************************/
if (hUser != (HUSER) NULL)
{
JDB_FreeBhvr (hUser);
}
if (hRequestF4102 != (HREQUEST)NULL)
{
JDB_CloseTable(hRequestF4102);
}
return (idReturnValue);
}
/* ===================================================== */