User calculation

Viewing 7 reply threads
  • Author
    Posts
  • April 6, 2020 at 8:17 AM #40250

    Hi,

    I need a script which makes a simple calculation on user input.
    The user inputs: amount inclusive tax and: a tax rate (from a list); the script then calculates the amount excl tax and puts it in the appropriate field.

    e.g. user input 150,00 and 21%
    output from script: 123,97

    I tried it with the Prompter class example from the documentation but without succes…
    I am not a programmer..

    Thanks in advance!

    Andreas.

    April 6, 2020 at 6:12 PM #40256

    Brendan
    Keymaster

    It sounds like something you could use the Calculation field for though. Just have two separate fields, the Tax Rate field with a Pick List of just numbers (no % character) and a Calculation field that computes the result based on the user input and the Pick List selection value.

    April 7, 2020 at 1:43 AM #40264

    Yes, I could indeed use the calculation field, but in most cases I don’t need this calculation.
    I now have an invoice with the input of services that are added together and then calculated with VAT to the total. Only in very rare cases do I have to calculate the other way round, so I have the total and have to calculate the amount without VAT.
    Is this then possible to do it with the help of a script?

    Tanks!

    Attachments:
    You must be logged in to view attached files.
    April 7, 2020 at 11:12 AM #40268

    Brendan
    Keymaster

    Yes, you could certainly do that with a Form Script.

    You would use the functions: record.getFieldValue(field_id) to get the value from your Total field, then perform your calculation on that value, then call record.setFieldValue(vat_id) to set the value on your VAT field.

    Then call form.saveAllChanges(); to save the results.

    April 7, 2020 at 1:51 PM #40269

    Thanks! I will give it a try…

    By the way, TapForms is a great application!

    With best regards, Andreas.

    April 7, 2020 at 2:07 PM #40270

    I tried the example script ‘Prompter class’ from the documentation,
    but I get ‘undefined’ for every variable….

    Is this a bug or do I something wrong?

    Thanks, Andreas.

    Attachments:
    You must be logged in to view attached files.
    April 10, 2020 at 1:33 AM #40288

    I took the script out of the automatic generated function (Nieuw_Script) and now it works fine!


    // *** Bereken het excl bedrag van het gegeven veld ***

    var output = function berekenExBtw(continued) {

    var exBtw = 0;
    var btw = 0;
    var exBedrag_ID = form.getFieldNamed('Bedrag 1').getId();
    var BTW_TYPE_ID = form.getFieldNamed('btwType').getId();
    var btwAmount = record.getFieldValue(BTW_TYPE_ID);

    if (continued == true) {
    if (inclusief) {

    if (btwAmount == '9') {btw = 9};
    if (btwAmount == '21') {btw = 21};

    exBtw = (inclusief/(btw+100))*100;

    // console.log("Het incl. bedrag is: " + inclusief + " Het excl. bedrag is: " + exBtw);

    record.setFieldValue(exBedrag_ID, exBtw);

    form.saveAllChanges();

    }

    } else {
    console.log("Cancel button pressed.");
    }
    }

    var inclusief = 0;

    let prompter = Prompter.new();
    prompter.cancelButtonTitle = 'Afbreken';
    prompter.continueButtonTitle = 'Berekenen';
    prompter.addParameter('Inclusief bedrag: ', 'inclusief')
    .show('Bereken Exclusief BTW', output);

    April 10, 2020 at 1:57 AM #40289

    Brendan
    Keymaster

    Ah yes. That was just a variable scope issue for you.

    If you had just moved the var definitions above function Nieuw_Script(), it would have worked too.

Viewing 7 reply threads

You must be logged in to reply to this topic.