This topic is locked

Help Requested - JavaScript OnLoad Event Question

1/10/2022 9:47:06 PM
PHPRunner General questions
O
OzCPA author

I have extremely limited experience with PHPRunner. I have created a test application (which will be added to after I get the basics sorted out) with one database and two tables; details are as follows:

DATABASE = TEST
TABLES = MASTER and DETAILS
FIELDS in MASTER table = Name [varchar 40], ChargeRate [decimal]
FIELDS in DETAILS table = Date (date), Name (varchar 40; read only; lookup to Master.Name), JobDetails (varchar 60), JobDuration (decimal), ChargeRate (decimal; read only; lookup to Master.ChargeRate), Fee (decimal; calculated)

What I am trying to achieve (using JavScript OnLoad Event if possible) is that for each record that I add (or modify) in the DETAILS table the process is as follows:

  1. I enter Date
  2. I select a dropdown of Name via a lookup in MASTER.Name
  3. I enter JobDetails
  4. I enter JobDuration
  5. The system auto-populates the ChargeRate field by a lookup to Master.Rate for the Name selected in Step 2 above
  6. The system calculates the Fee field by multiplying the JobDuration by the ChargeRate

I have designed my simple test system and get everything to work EXCEPT FOR Steps 5 and 6 (above).

I would be extremely grateful if someone would be kind enough to assist me by letting me know the actual "code" to use in the JavaScript OnLoad Events (based on the provided database, table, and field names).

I have looked at the documentation and tried various things for days, but I have really no idea what I am doing and can't get it to work.

MANY THANKS

fhumanes 1/15/2022

Hello,

I think it can be inspired by the example I did: https://fhumanes.com/blog/guias-desarrollo/guia-7-phprunner-sincronizar-actualizacion-entre-maestro-y-detalle/

If you do not get to solve it, you write me to my email and you tell me what version of Phprunner you use and I make an example (FREE) set to the information you have facilitated.

Greetings,
fernando

fernandohumanes@gmail.com

T
thamestrader 1/15/2022

I have done something very similar in my applications.I tend to use the Before record added to do any processing to obtain data from other tables and to produce calcuated fields. I have found that, and this may have changed with later versions, that calculated fields need to be defined on the page i.e. calc_field as " ".

Looking at how I use JSONLOAD Event - that tends to be for altering Whether/ How / Format of a field on the page, rather than affecting the actual contents of the field, the PHP code has to be in the Before Display Event and the Javascript code in the JSONLOAD Event.

I have found with the various code or application generators I have used that the key to success is understanding the products 'in built' processing cycle and then designing my solutions to match that cycle, whenever I'm finding something is difficult or generating a lot of code (defeating the object) its useally because my design is not in sync with the inbuilt cycle.

My understanding of PHPRunner is that with Master Detail relationship designs the inbuilt cycle is to search for something, produce a List page for the results, select a record and then include functionality to add a detail, find all the details, update the record selected etc. In the event that the search doesn't find a record then provide the option to add a new master record. This is prodably a rather simplistic definition - other users will no doubt be able to improve on this.

You might find that a slight redesign of the application may make this simpler to code. Using the inbuilt Master/Detail functionality. Create a list page for the Names table, use the basic search to display all the names that 'match', with a large file of names this will be quicker that paging through a dropdown list, and opens up a range of search features with zero coding. Once the list page displays the names that match, include a button to add a Detail record, using custom view that will automatically lookup fields, use the Before Display Event to perform the data editting before its displayed and the Before record added to perform any data editing required based on data keyed in.

Something else to consider is to have a unique (auto-increment) numeric record ID field on each table, and use this to link master and detail, rather than by Name given there could be many records for Smith, Brown, Jones etc

You might also find one of the templates for Invoicing provides a solution for as the underlying concept seems similar. I've never used any templates as I've found it easier to go my own way.

Good luck its a great product.

UPDATE: I tend to think of the PHPRunner generated applications as 'Client-Server' because the application contains PHP and SQL code that runs on the Server; which in turn outputs HTML and Javascript code that is transmitted to the Clients web browser to be processed. I found it useful to determine which functionality was server and which was client as it helped in deciding which PHPRunner event should be used.

T
thamestrader 1/16/2022

Further to my earlier post, I had an odd hour to spare so I made a working solution that does what you require, using out of the box functionality, and 13 lines of code to do the lookups and calculation required. Those 13 lines also include the code to check for a charge of duration on an edit, that requires a lookup for the charge rate (it might have changed) and the recalculation of the fee.

I can't attach an export of the project to this post or a PDF of the PHPRunner screen prints showing the set up.

The lookups are in the ADD Page
Process Record Values

// Get Name and Rate from Master
$data = $pageObject->getMasterRecord();
if ($data["Rec_ID"] > 0) {
$values["J_Name"] = $data["Name"];
$values["J_ChargeRate"] = $data["ChargeRate"];}

The Fee calculation is in the Before record Added Event

$values["Fee"] = $values["J_ChargeRate"] * $values["Duration"];

To satisfy your requirement thats all the code needed, I included the following code to for completeness.

The EDIT Page has the follwoing code in the Before Record Updated Event

if ($oldvalues["Duration"] <> $values["Duration"]) {
// Get Rate from Master
$data = $pageObject->getMasterRecord();
if ($data["Rec_ID"] > 0) {
$values["J_ChargeRate"] = $data["ChargeRate"];}
$values["Fee"] = $values["J_ChargeRate"] * $values["Duration"];}
else {
$values["J_ChargeRate"] = $oldvalues["J_ChargeRate"];}

Hope this helps - I'm happy to send you the PDF screen prints or project export if theres a way to do it.