Question

We're building a SaaS product that helps bus operators generate bills and send invoices to their customers. The problem is, each operator (our customer) has his/her unique formula to calculate the tariff.

We cannot generalize and bring a canonical formula in place. But I can tell you this, all the formulae accept the same input variables and produce a single output variable called tariff.

We're building this in PHP and I first thought of letting the customers write their formula in a textarea and then use eval() to execute it. But it sounds stupid.

Is there a way to build an interactive formula builder that customers can use to build their formula? If so, will I be able to store the final formula in the database? Or is there a way to use it without eval()?

Était-ce utile?

La solution

I first thought of letting the customers write their formula in a textarea and then use eval() to execute it

That is highly error-prone and likely to lead to annoyed and frustrated users.

There are many approaches to this type of problem, but a Rules Engine seems particularly well suited. A Rules Engine allows you to specify this type of processing using meta data rather than hard-coding it. Some Rules Engines even provide end-user friendly user interfaces.

I have had good experience with Drools, though I have not looked at how it might integrate with PHP.

A Rules Engine is far more complex than just calling eval() on a user-supplied function, but it is far more likely to lead to maintainable software, and to happy customers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top