function processOwnToTransactionBatch(ownToResult, amountInFloat, index, usrId, usrName)
{
     dbFunction.dbUser.getUsrName(ownToResult[index].toUserId)
    .then((ownToName) => {
        var ownAmountInFloat = parseFloat(parseFloat(ownToResult[index].debtAmount).toFixed(2));
        if (amountInFloat <= ownAmountInFloat)
        {
            var newAmount = (ownAmountInFloat - amountInFloat).toFixed(2);
            if (amountInFloat == ownAmountInFloat)
            {
                newAmount = "0.00";
            }
            dbFunction.dbDebtTransaction.updateDebtTransaction(ownToResult[index].id, usrId, ownToResult[index].toUserId, newAmount)
            .then(() => {
                dbFunction.dbUser.getUsrAvailableAmount(ownToResult[index].toUserId)
                .then((value) => {
                    var remainingAmountInFloat = parseFloat(parseFloat(value).toFixed(2));
                    var newAmountAvailable = (remainingAmountInFloat + amountInFloat).toFixed(2);
                    dbFunction.dbUser.setUsrAvailableAmount(ownToResult[index].toUserId, newAmountAvailable)
                    .then(() => {
                        dbFunction.dbTransactionLog.insertLog(usrId, "Transferred " + amountInFloat.toFixed(2) + " to " + ownToName + ".");
                        dbFunction.dbTransactionLog.insertLog(ownToResult[index].toUserId, "Received " + amountInFloat.toFixed(2) + " from " + usrName  + ".")
                        .then(() => {
                            console.log("Transferred " + amountInFloat.toFixed(2) + " to " + ownToName + ".");
                            console.log("Your balance is 0.00.");
                            console.log("Owing " + newAmount + " to " + ownToName + ".");
                            process.exit(1);
                        })
                        .catch((err) => {
                            console.log("Transferred " + amountInFloat.toFixed(2) + " to " + ownToName + ".");
                            console.log("Your balance is 0.00.");
                            console.log("Owing " + newAmount + " to " + ownToName + ".");
                            process.exit(1);
                        });
                    })
                    .catch((err) => {
                        console.error(err);
                        process.exit(1);
                    });
                })
                .catch((err) => {
                    console.error(err);
                    process.exit(1);
                });
            })
            .catch((err) => {
                console.error(err);
                process.exit(1);
            });
        }
        else
        {
            let newAmount = amountInFloat - ownAmountInFloat;
            dbFunction.dbDebtTransaction.updateDebtTransaction(ownToResult[index].id, usrId, ownToResult[index].toUserId, "0.00")
            .then(() => {
                dbFunction.dbUser.getUsrAvailableAmount(ownToResult[index].toUserId)
                .then((value) => {
                    var remainingAmountInFloat = parseFloat(parseFloat(value).toFixed(2));
                    var newAmountAvailable = (remainingAmountInFloat + ownAmountInFloat).toFixed(2);
                    dbFunction.dbUser.setUsrAvailableAmount(ownToResult[index].toUserId, newAmountAvailable)
                    .then(() => {
                        dbFunction.dbTransactionLog.insertLog(usrId, "Transferred " + ownAmountInFloat.toFixed(2) + " to " + ownToName + ".");
                        dbFunction.dbTransactionLog.insertLog(ownToResult[index].toUserId, "Received " + ownAmountInFloat.toFixed(2) + " from " + usrName  + ".");
                        console.log("Transferred " + ownAmountInFloat.toFixed(2) + " to " + ownToName + ".");
                        if (index == (ownToResult.length - 1))
                        {
                            // save your own remaining
                            dbFunction.dbUser.getUsrAvailableAmount(usrId)
                            .then((value) => {
                                var remainingAmountInFloat = parseFloat(parseFloat(value).toFixed(2));
                                var newAmountAvailable = (remainingAmountInFloat + newAmount).toFixed(2);
                                dbFunction.dbUser.setUsrAvailableAmount(usrId, newAmountAvailable)
                                .then(() => {
                                    dbFunction.dbTransactionLog.insertLog(usrId, "Top up " + newAmount.toFixed(2) + ".")
                                    .then(() => {
                                        console.log("Your balance is "+ newAmountAvailable + ".");
                                        process.exit(1);
                                    })
                                    .catch((err) => {
                                        console.log("Your balance is "+ newAmountAvailable + ".");
                                        process.exit(1);
                                    });
                                })
                                .catch((err) => {
                                    console.error(err);
                                    process.exit(1);
                                });
                            })
                            .catch((err) => {
                                console.error(err);
                                process.exit(1);
                            });
                        }
                        else
                        {
                            processOwnToTransactionBatch(ownToResult, newAmount, index + 1, usrId, usrName);
                        }
                    })
                    .catch((err) => {
                        console.error(err);
                        process.exit(1);
                    });
                })
                .catch((err) => {
                    console.error(err);
                    process.exit(1);
                });
            })
            .catch((err) => {
                console.error(err);
                process.exit(1);
            });
        }
    })
    .catch(() => {
        console.error(err);
        process.exit(1);
    });
}
// process debt transaction payout for topup
process debt transaction payout for topup
process debt transaction payout for topup
function processOwnToTransactionBatch(ownToResult, amountInFloat, index, usrId, usrName)
{
     dbFunction.dbUser.getUsrName(ownToResult[index].toUserId)
    .then((ownToName) => {
        var ownAmountInFloat = parseFloat(parseFloat(ownToResult[index].debtAmount).toFixed(2));
        if (amountInFloat <= ownAmountInFloat)
        {
            var newAmount = (ownAmountInFloat - amountInFloat).toFixed(2);
            if (amountInFloat == ownAmountInFloat)
            {
                newAmount = "0.00";
            }
            dbFunction.dbDebtTransaction.updateDebtTransaction(ownToResult[index].id, usrId, ownToResult[index].toUserId, newAmount)
            .then(() => {
                dbFunction.dbUser.getUsrAvailableAmount(ownToResult[index].toUserId)
                .then((value) => {
                    var remainingAmountInFloat = parseFloat(parseFloat(value).toFixed(2));
                    var newAmountAvailable = (remainingAmountInFloat + amountInFloat).toFixed(2);
                    dbFunction.dbUser.setUsrAvailableAmount(ownToResult[index].toUserId, newAmountAvailable)
                    .then(() => {
                        dbFunction.dbTransactionLog.insertLog(usrId, "Transferred " + amountInFloat.toFixed(2) + " to " + ownToName + ".");
                        dbFunction.dbTransactionLog.insertLog(ownToResult[index].toUserId, "Received " + amountInFloat.toFixed(2) + " from " + usrName  + ".")
                        .then(() => {
                            console.log("Transferred " + amountInFloat.toFixed(2) + " to " + ownToName + ".");
                            console.log("Your balance is 0.00.");
                            console.log("Owing " + newAmount + " to " + ownToName + ".");
                            process.exit(1);
                        })
                        .catch((err) => {
                            console.log("Transferred " + amountInFloat.toFixed(2) + " to " + ownToName + ".");
                            console.log("Your balance is 0.00.");
                            console.log("Owing " + newAmount + " to " + ownToName + ".");
                            process.exit(1);
                        });
                    })
                    .catch((err) => {
                        console.error(err);
                        process.exit(1);
                    });
                })
                .catch((err) => {
                    console.error(err);
                    process.exit(1);
                });
            })
            .catch((err) => {
                console.error(err);
                process.exit(1);
            });
        }
        else
        {
            let newAmount = amountInFloat - ownAmountInFloat;
            dbFunction.dbDebtTransaction.updateDebtTransaction(ownToResult[index].id, usrId, ownToResult[index].toUserId, "0.00")
            .then(() => {
                dbFunction.dbUser.getUsrAvailableAmount(ownToResult[index].toUserId)
                .then((value) => {
                    var remainingAmountInFloat = parseFloat(parseFloat(value).toFixed(2));
                    var newAmountAvailable = (remainingAmountInFloat + ownAmountInFloat).toFixed(2);
                    dbFunction.dbUser.setUsrAvailableAmount(ownToResult[index].toUserId, newAmountAvailable)
                    .then(() => {
                        dbFunction.dbTransactionLog.insertLog(usrId, "Transferred " + ownAmountInFloat.toFixed(2) + " to " + ownToName + ".");
                        dbFunction.dbTransactionLog.insertLog(ownToResult[index].toUserId, "Received " + ownAmountInFloat.toFixed(2) + " from " + usrName  + ".");
                        console.log("Transferred " + ownAmountInFloat.toFixed(2) + " to " + ownToName + ".");
                        if (index == (ownToResult.length - 1))
                        {
                            dbFunction.dbUser.getUsrAvailableAmount(usrId)
                            .then((value) => {
                                var remainingAmountInFloat = parseFloat(parseFloat(value).toFixed(2));
                                var newAmountAvailable = (remainingAmountInFloat + newAmount).toFixed(2);
                                dbFunction.dbUser.setUsrAvailableAmount(usrId, newAmountAvailable)
                                .then(() => {
                                    dbFunction.dbTransactionLog.insertLog(usrId, "Top up " + newAmount.toFixed(2) + ".")
                                    .then(() => {
                                        console.log("Your balance is "+ newAmountAvailable + ".");
                                        process.exit(1);
                                    })
                                    .catch((err) => {
                                        console.log("Your balance is "+ newAmountAvailable + ".");
                                        process.exit(1);
                                    });
                                })
                                .catch((err) => {
                                    console.error(err);
                                    process.exit(1);
                                });
                            })
                            .catch((err) => {
                                console.error(err);
                                process.exit(1);
                            });
                        }
                        else
                        {
                            processOwnToTransactionBatch(ownToResult, newAmount, index + 1, usrId, usrName);
                        }
                    })
                    .catch((err) => {
                        console.error(err);
                        process.exit(1);
                    });
                })
                .catch((err) => {
                    console.error(err);
                    process.exit(1);
                });
            })
            .catch((err) => {
                console.error(err);
                process.exit(1);
            });
        }
    })
    .catch(() => {
        console.error(err);
        process.exit(1);
    });
}
function processOwnToTransactionBatch ( ownToResult , amountInFloat , index , usrId , usrName ) { dbFunction . dbUser . getUsrName ( ownToResult [ index ] . toUserId ) . then ( ( ownToName ) => { var ownAmountInFloat = parseFloat ( parseFloat ( ownToResult [ index ] . debtAmount ) . toFixed ( 2 ) ) ; if ( amountInFloat <= ownAmountInFloat ) { var newAmount = ( ownAmountInFloat - amountInFloat ) . toFixed ( 2 ) ; if ( amountInFloat == ownAmountInFloat ) { newAmount = "0.00" ; } dbFunction . dbDebtTransaction . updateDebtTransaction ( ownToResult [ index ] . id , usrId , ownToResult [ index ] . toUserId , newAmount ) . then ( ( ) => { dbFunction . dbUser . getUsrAvailableAmount ( ownToResult [ index ] . toUserId ) . then ( ( value ) => { var remainingAmountInFloat = parseFloat ( parseFloat ( value ) . toFixed ( 2 ) ) ; var newAmountAvailable = ( remainingAmountInFloat + amountInFloat ) . toFixed ( 2 ) ; dbFunction . dbUser . setUsrAvailableAmount ( ownToResult [ index ] . toUserId , newAmountAvailable ) . then ( ( ) => { dbFunction . dbTransactionLog . insertLog ( usrId , "Transferred " + amountInFloat . toFixed ( 2 ) + " to " + ownToName + "." ) ; dbFunction . dbTransactionLog . insertLog ( ownToResult [ index ] . toUserId , "Received " + amountInFloat . toFixed ( 2 ) + " from " + usrName + "." ) . then ( ( ) => { console . log ( "Transferred " + amountInFloat . toFixed ( 2 ) + " to " + ownToName + "." ) ; console . log ( "Your balance is 0.00." ) ; console . log ( "Owing " + newAmount + " to " + ownToName + "." ) ; process . exit ( 1 ) ; } ) . catch ( ( err ) => { console . log ( "Transferred " + amountInFloat . toFixed ( 2 ) + " to " + ownToName + "." ) ; console . log ( "Your balance is 0.00." ) ; console . log ( "Owing " + newAmount + " to " + ownToName + "." ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } else { let newAmount = amountInFloat - ownAmountInFloat ; dbFunction . dbDebtTransaction . updateDebtTransaction ( ownToResult [ index ] . id , usrId , ownToResult [ index ] . toUserId , "0.00" ) . then ( ( ) => { dbFunction . dbUser . getUsrAvailableAmount ( ownToResult [ index ] . toUserId ) . then ( ( value ) => { var remainingAmountInFloat = parseFloat ( parseFloat ( value ) . toFixed ( 2 ) ) ; var newAmountAvailable = ( remainingAmountInFloat + ownAmountInFloat ) . toFixed ( 2 ) ; dbFunction . dbUser . setUsrAvailableAmount ( ownToResult [ index ] . toUserId , newAmountAvailable ) . then ( ( ) => { dbFunction . dbTransactionLog . insertLog ( usrId , "Transferred " + ownAmountInFloat . toFixed ( 2 ) + " to " + ownToName + "." ) ; dbFunction . dbTransactionLog . insertLog ( ownToResult [ index ] . toUserId , "Received " + ownAmountInFloat . toFixed ( 2 ) + " from " + usrName + "." ) ; console . log ( "Transferred " + ownAmountInFloat . toFixed ( 2 ) + " to " + ownToName + "." ) ; if ( index == ( ownToResult . length - 1 ) ) { dbFunction . dbUser . getUsrAvailableAmount ( usrId ) . then ( ( value ) => { var remainingAmountInFloat = parseFloat ( parseFloat ( value ) . toFixed ( 2 ) ) ; var newAmountAvailable = ( remainingAmountInFloat + newAmount ) . toFixed ( 2 ) ; dbFunction . dbUser . setUsrAvailableAmount ( usrId , newAmountAvailable ) . then ( ( ) => { dbFunction . dbTransactionLog . insertLog ( usrId , "Top up " + newAmount . toFixed ( 2 ) + "." ) . then ( ( ) => { console . log ( "Your balance is " + newAmountAvailable + "." ) ; process . exit ( 1 ) ; } ) . catch ( ( err ) => { console . log ( "Your balance is " + newAmountAvailable + "." ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } else { processOwnToTransactionBatch ( ownToResult , newAmount , index + 1 , usrId , usrName ) ; } } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } ) . catch ( ( err ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; } } ) . catch ( ( ) => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; }
process debt transaction payout for topup
process debt transaction payout for topup
// save your own remaining