Items to be replaced:
- Portal Address
- Capture Address
/*===================================================================================*/
/*=========================== FUNCTIONS =============================================*/
/*===================================================================================*/
/**
* Function to now date with special string format
* @return : formatted date in string
**/
var getFormattedDate = function () {
var now = new Date();
var year = now.getFullYear();
var month = (now.getMonth() + 1).toString().padStart(2, '0'); // Mois commence à 0, donc +1
var day = now.getDate().toString().padStart(2, '0');
var hour = now.getHours().toString().padStart(2, '0');
var minute = now.getMinutes().toString().padStart(2, '0');
//var seconde = maintenant.getSeconds().toString().padStart(2, '0');
return year + month + day +hour + minute;// + seconde;
};
/**
* Function to get VAT account associated with current line handled
* @parameters : pLine : current account entry line handled
* @parameters : associatedTVAWithCharge : object containing account number in key and vat account in relation to in value
* @parameters : listTVAAccountInAccountEntry : array of vat account used in current account entry
* @return : VAT account in relation to current line given in parameter
**/
var getVATAccountFromLine = function(pLine, associatedTVAWithCharge, listTVAAccountInAccountEntry){
if(pLine == null || pLine.accountNumber == null || associatedTVAWithCharge == null)
return null;
//Handle case of juste one VAT on accountEntry
if(listTVAAccountInAccountEntry != null && listTVAAccountInAccountEntry.length == 1)
return listTVAAccountInAccountEntry[0];
//Handle case of matching charge and tva amount
if(associatedTVAWithCharge[pLine.accountNumber] != undefined)
return associatedTVAWithCharge[pLine.accountNumber];
return null;
};
/**
* Function to get all VATAccount in Capture filtered by buyer owners of current accountEntry
* @parameters : pParameters : object with all informations to get needed values
* @parameters : ws : Webservice object already initialized to be used to get informations from Capture webservice
* @return : array of all VATAccount object
**/
var getAllVATAccounts = function(pParameters, ws){
var resA = ws.send("/ws/account/list/"+FOLDER.id+"/VATAccount/_/_/_/_/_", "GET", {"Authorization" : "Bearer "+ CAPTURE_WS_TOKEN});
resA = JSON.parse(resA.StringResponse);
return resA != null && resA.items != null && resA.items.length > 0 ? resA.items : [];
};
/**
* Function to get index value from id and idUnit
* @parameters : pParameters : object with all informations to get needed values
* @parameters : idIndex : string id of index
* @parameters : idUnit : id unit
* @return : value of index found by id and idunit
**/
var getIndexFromIDAndIDUnit = function(pParameters, idIndex, idUnit){
var listIndex = getIndexesFromID(pParameters, idIndex);
if(listIndex != null){
for(var i=0;i<listIndex.length;i++){
if(listIndex[i].idUnit == idUnit)
return getTextFromIndexInfo(listIndex[i]);
}
}
return null;
};
/**
* Function to get amount ex vat(HT) id unit from charge amount line
* @parameters : pParameters : object with all informations to get needed values
* @parameters : pChargeAmount : amount of current charge line
* @return : amount ex vat(HT) id unit
**/
var getHTIDUnit = function(pParameters, pChargeAmount){
var listHT = getIndexesFromID(pParameters, "AMOUNT_EX_VAT_INDEX");
if(listHT == null)
return null;
for(var lh=0;lh<listHT.length;lh++){
var textAmount = getTextFromIndexInfo(listHT[lh]);
if(textAmount != null){
var parseValue = parseFloat(textAmount.replace(',', '.'));
if(parseValue == pChargeAmount)
return listHT[lh].idUnit;
}
}
return -1;
}
/**
* Function to get vat account number if corresponding with chargeAmount spent in parameter
* step 1 : getIDUnit from chargeAmount
* step 2 : find vatIndexInfo with the same idUnit
* step 3 : find line matching with vatIndexInfo amount
* @parameters : pParameters : object with all informations to get needed values
* @parameters : pLines : lines of current account entry
* @parameters : pChargeAmount : amount of current charge line
* @return : VAT account number in relation with current charge amount
**/
var getVATAccountNumberFromCharge = function(pParameters, pLines, pChargeAmount){
var idUnitHT = getHTIDUnit(pParameters, pChargeAmount);
if(idUnitHT == -1)
return null;
var vatInfoValue = getIndexFromIDAndIDUnit(pParameters, "VAT_AMOUNT_INDEX", idUnitHT);
if(vatInfoValue != null){
var vatValue = parseFloat(vatInfoValue.replace(',', '.'));
for(var ll=0;ll<pLines.length;ll++){
if(pLines[ll].amount == vatValue)
return pLines[ll].accountNumber;
}
}
return null;
}
/**
* Function to get VATAccount if account number spend in parameters is matching with one element of list
* @parameters : pList : array of all VATAccount
* @parameters : pAccountNumber : account number
* @return : VAT account matching with account number or null
**/
var getAccountFromList = function(pList, pAccountNumber){
if(pAccountNumber == null || pAccountNumber == "")
return null;
for(var va=0;va<pList.length;va++){
if(pList[va].accountNumber == pAccountNumber)
return pList[va];
}
return null;
};
/**
* Function to get VATAccount in relation with currentLine handled
* @parameters : pLine : current line handled
* @parameters : pLines : lines of current account entry
* @parameters : pParameters : object with all informations to get needed values
* @parameters : pVATAccounts : array of all VATAccount
* @return : VAT account found or null
**/
var tryToGetVATAccount = function(pLine, pLines, pParameters, pVATAccounts){
if(pVATAccounts.length == 0)
return null;
// Handle VATLine case
var result = getAccountFromList(pVATAccounts, pLine.accountNumber);
if(result != null)
return result;
//Handle ChargeLine case
var vatAccountNumber = getVATAccountNumberFromCharge(pParameters, pLines, pLine.amount);
return getAccountFromList(pVATAccounts, vatAccountNumber);
};
/**
* Function to get an object account number -> VATAccount in relation ex ["665450"] -> {type : "VATAccount", "accountNumber": "445650", ...}
* @parameters : pLines : lines of current account entry
* @parameters : pParameters : object with all informations to get needed values
* @parameters : ws : Webservice object already initialized to be used to get informations from Capture webservice
* @return : object containing key (account number), value(VAT account in relation)
**/
var getAssociatedTVAAccount = function(pLines, pParameters, ws){
if(pLines == null || pLines.length == 0)
return {};
var result = {};
var accounts = getAllVATAccounts(pParameters, ws);
for(var l = 0;l<pLines.length;l++){
if(!pLines[l].genericAccountNumber)
result[pLines[l].accountNumber] = tryToGetVATAccount(pLines[l], pLines, pParameters, accounts);
}
return result;
};
/**
* Function to get only VAT account used on current account entry.
* This function use object associatedAccount because it already filtered, so we save time on loop
* @parameters : pAssociatedAccount : object containing account number in key and vat account in relation to in value
* @return : array of VAT account used on current accountr entry
**/
var getTVAAccountFromAccountEntry = function(pAssociatedAccount){
var result = [];
for(var key in pAssociatedAccount){
if(pAssociatedAccount[key] != null && pAssociatedAccount[key].type == "VATAccount")
result.push(pAssociatedAccount[key]);
}
return result;
}
/**
* Function to get value of additional info from list and code
* @parameters : pInfos : array with additionals infos
* @parameters : pCode : code of additional info needed
* @return : value of additional info
**/
var getAdditionalInfosValue = function(pInfos, pCode){
if(pInfos == null || pCode == null || pInfos.length == 0)
return "";
for(var ii=0;ii<pInfos.length;ii++){
if(pInfos[ii].id == pCode)
return pInfos[ii].value;
}
return "";
};
/**
* Function to get code tva information in relation with current line
* option 1 : if line has vatAccountAssociated get it
* option 2 : try to get vat account in deduction with amount or number of vat account in current account entry
* @parameters : pLine : current account entry line handled
* @parameters : associatedTVAWithCharge : object containing account number in key and vat account in relation to in value
* @parameters : listTVAAccountInAccountEntry : array of vat account used in current account entry
* @return : TVA code associated with current line
**/
var getCodeTVAValue = function(pLine, associatedTVAWithCharge, listTVAAccountInAccountEntry){
if(!pLine.genericAccountNumber){
var vatAccount = null;
if(pLine.vatAccountAssociated)
vatAccount = pLine.vatAccountAssociated;
else
vatAccount = getVATAccountFromLine(pLine, associatedTVAWithCharge, listTVAAccountInAccountEntry);
if(vatAccount != null)
return getAdditionalInfosValue(vatAccount.additionnalInfos, "TVACODE");
return '00';
}
return '';
};
/**
* Get details for an account entry
* @parameters : pParameters : object with all informations to get needed values
* @parameters : ws : Webservice object already initialized to be used to get informations from Capture webservice
* @return : string which is account entries formatted
**/
var getAccountEntryDetails = function(pParameters, ws){
var res = ws.send("/ws/document/"+INTERNAL_DOCUMENT_ID, "GET", {"Authorization" : "Bearer "+ CAPTURE_WS_TOKEN});
res = JSON.parse(res.StringResponse);
// Pour déboguage au besoin
//JSE_File.writeFile("C:\\ExportTest\\FichierComplet.json",res.StringResponse);
var lignesCompta = res.accountEntries[0].lineAccountEntries;
var associatedTVAWithCharge = getAssociatedTVAAccount(lignesCompta, pParameters, ws);
var listTVAAccountInAccountEntry = getTVAAccountFromAccountEntry(associatedTVAWithCharge);
var ledgerCode = ACCOUNT_ENTRY.ledger.code;
var idDms = ID_DMS;
var url = "https://*******.com/previewDocument/idDocument/"+idDms;
var documentDate = getTextFromIndexInfo(getFirstIndexFromID(pParameters,"DATE_INDEX"));
var invoiceNumber = getTextFromIndexInfo(getFirstIndexFromID(pParameters,"INVOICE_NUMBER_INDEX"));
var typeFacture = getTextFromIndexInfo(getFirstIndexFromID(pParameters,"TYPEDOC_INDEX"));
var dueDate = getTextFromIndexInfo(getFirstIndexFromID(pParameters,"DUE_DATE_INDEX"));
var result = "";
for(var i = 0; i < lignesCompta.length; i++){
var codeTVAInfo = getCodeTVAValue(lignesCompta[i], associatedTVAWithCharge, listTVAAccountInAccountEntry);
var compteTiersGen = lignesCompta[i].accountNumber;
var meaningTest = (lignesCompta[i].meaning == 'DEBIT') ? 'D' : 'C';
result += "BEGIN\t"
+ledgerCode+"\t"
+documentDate+"\t"
+invoiceNumber+"\t"
+documentDate+"\t"
+compteTiersGen+"\t"
+typeFacture+"\t"
+""+"\t"
+lignesCompta[i].labelLine+"\t"
+"" +"\t"
+dueDate+'\t'
+meaningTest+'\t'
+lignesCompta[i].amount.toFixed(2).replace('.',',')+'\t'
+codeTVAInfo+"\t"
+idDms+'\t'
+url+'\t'
+""+'\r\n';
}
return result;
};
/*===================================================================================*/
/*=========================== MAIN CODE =============================================*/
/*===================================================================================*/
var finalPathFile = "C:\\Export\\ExportFile_"+getFormattedDate()+".csv";
var ws = new JSE_WebService("******.smartcapture.*****.com", "https", 443);
var result = "";
if(ACCOUNT_ENTRY_NUMBER == 1)
result = "COMPTA CJO DATE_ECRIT PIECE DATE_PIECE COMPTE DOC INFO1 LIBELLE INFO2 DATE_ECH SENS MONTANT TVA ID_ECRITURE_EXT URL INFO3\r\n";
else
result = JSE_File.readFile(finalPathFile)
result += getAccountEntryDetails(pParameters, ws);
JSE_File.writeFile(finalPathFile, result);
return true;