The phone running the script would be inordinately hard to make happen. Most phones can’t be programmed to do things; especially things like interacting with databases or moving stock levels around.
Making the PBX do things based on the input from the phone, on the other hand, is the bread and butter of what lots of us do here. Anyone that’s integrated a CRM or Time Tracking system to a phone has done basically what you are trying to do.
There are lots of ways to reach into another application. You can look back into the “TimeTrex” interface that I wrote a few years ago as an example. From the phone, you dial a special code (*11 and *12, IIRC) and the custom context set up the application by getting all of the information and firing up a System() function that just went to town.
There are options in the Custom Context to allow for specific extensions to do specific things. @lgaetz has written extensively on this kind of stuff and there are several example projects out in the GitHub repo that should be a good starting point for where you want to go. Remember, we’re here for you if you have questions, so try it and see what happens.