JS Statements
There are certain occassions when javascript must be exectued on the client side to calculate values and alter grouped inputs. For example if a user selects a people_id on a customer_invoice record, it should prepopulate the billing address from their primary address. This is achieved by a JS Statement.
These JS statments are defined on the FIYGE backend, in the following manner.
function jsStatment(
setFormValues,
data,
relatedData,
fieldName,
insertListItem,
value,
accessToken
) {
// User defined rules using the inputs above.
}
setFormValues(obj): this is a function that is called with an object of key/value pairs. The key is the field name of the input you want to change, and the value is the new value to change it to.data: this is the data record that is being modified.relatedData: this is the data record that was selected by an input. Ex: If you are assigning a people record to an invoice, this would contain the entire data record of the people record.fieldName: this is the dot notation of the current field being updated. Ex:invoices.people_id.insertListItem(fieldName, obj): in the case of an array that we want to modify by inserting a new item, we will call this function with the fieldName of the corresponding array and the new object that we want to insert. Ensure the object contains all mandatory fields that are needed to be saved.value: the current value that is being updated.accessToken: the current user's access token that can be used to make additional requests.
An example below on the invoices.orginazation_id input, in this function we get the currency_id from the relatedData (data of the orginization that was selected), extract the currency_id and set that on the current invoice record.
const __currency_id = relatedData?.__currency_id;
const currency_id = relatedData?.currency_id;
const valuesToSet = {__currency_id, currency_id};
setFormValues(valuesToSet)
In this more complex example on invoice.invoice_lines.line_taxes.tax_id, we extract the id of the tax_id that has been selected and check if it exists on the existing data record under line_taxes_summary. If it exists we do nothing, if it does not exist, we insert a new element in the array line_taxes_summary with the selected tax_id information
const tax_id = relatedData?.["id"]
// look for a row entry in data?.line_taxes_summary
const existing_line_taxes = data?.line_taxes_summary
const existing_index = existing_line_taxes.findIndex(item => item.tax_id === tax_id);
if(existing_index > -1) {
} else {
const newLineTaxSummaryItem = {
tax_amount: 0,
tax_id: relatedData?.id,
__tax_id: relatedData?.tax_code,
tax_id_model: "taxes"
}
insertListItem("line_taxes_summary", newLineTaxSummaryItem);
}
JS Formulas
In JS Statements, we are modifying other inputs based on the change event of a given input. In JS Formulas we are modifying a given input based on the change of other inputs. For example if we want to calculate the discount amount of an invoice based off a given discount percent and a total, we will use JS Formulas. We can call the formula on our frontend by using the function constructor.
In scope of a JS Formula is the data of the record and the fieldName
function jsFormula(data, fieldName) {
// user defined formula
}
In the below example we are calculating the value for invoices.discount_amount. This value is calculated by taking the total_before_discount and discount_percent and multipying them and dividing by 100.
const total_before_discount = Number(data?.["total_before_discount"]) || 0;
const discount_percent = Number(data?.["discount_percent"]) || 0;
const discount_total = total_before_discount*discount_percent/100
if(discount_total == 0 || discount_total){
return discount_total
}
return 0