automatic calculation instead of after update/insert code

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
facos79
Veteran Member
Posts: 119
Joined: 2014-10-29 12:31

automatic calculation instead of after update/insert code

Post by facos79 » 2023-10-12 16:26

Hi everyone, I have a problem: I have a code that works well if inserted in the after update/insert section hook page.
However, on this page the values are updated only when I update the fields.
Can anyone help me convert the code so I can use it directly in automatic calculations?

Code: Select all

$tb = sqlValue("SELECT `Tot_blocchi` FROM `Acquisto_peso` WHERE `id`=" . $data['Totfat']);

$sum = sqlValue("SELECT SUM(`Lordo`) FROM `Movimenti_acquisto_peso` WHERE `Totfat`=" . $data['Totfat']);

$rr = $sum - $tb ;

sql("UPDATE `Movimenti_acquisto_peso` SET `Differenza`='" . $rr . "' WHERE `id`=" . $data['selectedID'], $eo);

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1709
Joined: 2018-04-01 10:12

Re: automatic calculation instead of after update/insert code

Post by pbottcher » 2023-10-12 20:29

Hi,

you may try:

Code: Select all

SELECT 
SUM(`Movimenti_acquisto_peso`.`Lordo`) - `Acquisto_peso`.Tot_blocchi` 
FROM `Movimenti_acquisto_peso`, `Movimenti_acquisto_peso` 
WHERE  `Movimenti_acquisto_peso`.`Totfat`= `Movimenti_acquisto_peso`.`Totfat` 
AND  `Acquisto_peso`.`id` = `Movimenti_acquisto_peso`.`Totfat` 
AND `Movimenti_acquisto_peso`.`id` = %ID%
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

facos79
Veteran Member
Posts: 119
Joined: 2014-10-29 12:31

Re: automatic calculation instead of after update/insert code

Post by facos79 » 2023-10-12 21:43

Hi, thanks for the help.
The code doesn't work. It shows no results.
I tried to insert the following code. The formula works. But there is a problem: the formula is applied to all the rows without distinction, however I would like it to be applied to all the rows that have the same value contained in the 'Block' cell
See attached photo.

Code: Select all

SELECT (`Movimenti_acquisto_peso`.`Totfat` - (SELECT SUM(`Movimenti_acquisto_peso`.`Lordo`))) FROM `Movimenti_acquisto_peso` 
WHERE `Movimenti_acquisto_peso`.`id`=%PKFIELD%
Attachments
Catturaappgini12.JPG
Catturaappgini12.JPG (76.69 KiB) Viewed 3293 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1709
Joined: 2018-04-01 10:12

Re: automatic calculation instead of after update/insert code

Post by pbottcher » 2023-10-14 11:26

Hi,

without having the table really visibile it is hard to understand. You may try:

Code: Select all

SELECT SUM(`m2`.`Lordo`)-acquisto_peso.Tot_blocchi 
FROM `Movimenti_acquisto_peso` 
LEFT JOIN Movimenti_acquisto_peso m2 on m2.Totfat = Movimenti_acquisto_peso.Totfat 
LEFT JOIN acquisto_peso on acquisto_peso.id=m2.Totfat 
WHERE `Movimenti_acquisto_peso`.`id`=%ID%
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply