// GraalJS Script (engine type: 2)
//
// Notes:
// "Fiscal Month-Day" (date ID) is stored as a string.
// "Fiscal Month-Day" (date ID) can also be stored as a numerical value.
// Sorting by date becomes easier, if included in the quote ID, slip ID, etc.
//
// Notes (ja):
// "会計月日"(日付ID)は文字列として文字型データに格納されます。
// "会計月日"(日付ID)は数値として数値型データに格納することも可能です。
// 見積書や伝票などの管理IDに含めると、日付順ソートが容易になります。
//
// [M] numMonthSerial (Serial Number, 1 means the first month of the fiscal year.)
// 01 02 03 04 05 06 07 08 09 10 11 12
// EndMonth= 1: 12 1 2 3 4 5 6 7 8 9 10 11
// EndMonth= 3: 10 11 12 1 2 3 4 5 6 7 8 9
// EndMonth=12: 1 2 3 4 5 6 7 8 9 10 11 12
//////// START "main()" ////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA = configs.get( "conf_DataIdA" ) + ""; // required
const strEndMonth = configs.get( "conf_EndMonth" ) + ""; // required
const dataIdC = configs.get( "conf_DataIdC" ) + ""; // required
let numEndMonth = 0;
const regMonthInt = /^([1-9]|1[0-2])$/; // RegExp: 1,2,,,11,12
if( regMonthInt.test( strEndMonth ) ){
numEndMonth = parseInt(strEndMonth, 10);
}else{
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {B:EndMonth} must be 1 to 12 \n" );
}
//// == Data Retrieving / ワークフローデータの参照 ==
if( engine.findDataByNumber( dataIdA ) === null ){
throw new Error( "\n AutomatedTask UnexpectedDateError:" +
" Date {A} is null \n" );
}
const strDateA = engine.findDataByNumber( dataIdA ) + ""; // e.g. 2020-04-30
//// == Calculating / 演算 ==
let year = 0;
let monthIndex = 0;
let day = 0;
let arrDateParts = strDateA.split("-");
year = parseInt(arrDateParts[0], 10);
monthIndex = parseInt(arrDateParts[1], 10) - 1; // 3
day = parseInt(arrDateParts[2], 10); // 30
let numMdd = monthIndex * 100 + day; // e.g. 330
numMdd = (numMdd + 1200 - (numEndMonth * 100) ) % 1200 + 100;
//// == Data Updating / ワークフローデータへの代入 ==
if( engine.findDataDefinitionByNumber( dataIdC ).matchDataType("DECIMAL") ){
engine.setDataByNumber( dataIdC, new java.math.BigDecimal(numMdd) );
}else{
engine.setDataByNumber( dataIdC, (numMdd + "") );
}
} //////// END "main()" ////////////////////////////////////////////////////////////////