Database access denied

Hello there,
I am currently working on a module with a trigger to automatically write computed data to an extrafield. The extrafield in question of the type “date”. I am an admin of the dolibarr, so I don’t understand why it tells me “Access denied.”
Currently I am using a trigger for creation of a supplier bill, to read the data and do the computation. Everything works fine to this point. But when I try either “$db->begin()” or “$this->db->begin()” or “$object->db->begin()” with the trigger and click on the create button the “Access Denied” button shows up. I commented everything out, in parts of sql query and commit/rollback, out so there is only the begin() and the computation of the variables in the code.
The actual promt: “Access denied. You have tried to call a page, area or function that is disabled or has no permission to do so.”
The code:

case 'BILL_SUPPLIER_CREATE':
			    $object->db->begin();
			    $id = $object->id;
			    $ref = $object->ref;
			    $type = $object->type;
			    $creationdatestring = $_POST["re"];
			    $paymentcondition = $_POST["cond_reglement_id"];
			    setEventMessage("Object Id: ".$id." Ref: ".$ref." Type: ".$type);
			    //setEventMessage("Object Id: ".$id." Date created: ".$creationdatestring." Payment condition: ".$paymentcondition);
			    $dateinterval;
			    if($paymentcondition == "1" || $paymentcondition == "6" || $paymentcondition == "8" || $paymentcondition == "60" || $paymentcondition == "61" || $paymentcondition == "62"){
			        $dateinterval = new DateInterval("P0D");
			    }elseif($paymentcondition == "20" || $paymentcondition == "15" || $paymentcondition == "10"){
			        $dateinterval = new DateInterval("P10D");
			    }elseif($paymentcondition == "12" || $paymentcondition == "13" || $paymentcondition == "18"){
			        $dateinterval = new DateInterval("P14D");
			    }elseif($paymentcondition == "16"){
			        $dateinterval = new DateInterval("P7D");
			    }elseif($paymentcondition == "14"){
			        $dateinterval = new DateInterval("P8D");
			    }elseif($paymentcondition == "21"){
			        $dateinterval = new DateInterval("P20D");
			    }elseif($paymentcondition == "3" || $paymentcondition == "17"){
			        $dateinterval = new DateInterval("P30D");
			    }elseif($paymentcondition == "46"){
			        $dateinterval = new DateInterval("P45D");
			    }elseif ($paymentcondition == "19" || $paymentcondition == "5"){
			        $dateinterval = new DateInterval("P60D");
			    }elseif ($paymentcondition == "59"){
			        $dateinterval = new DateInterval("P90D");
			    }else{
			        $dateinterval = new DateInterval("P1D");
			    }
			    
			    $creationdate = date_create_from_format("d.m.Y", $creationdatestring);
			    
			    $payday = date_add($creationdate, $dateinterval);
			    $paydaystring = date_format($payday, "d.m.Y");
			    break;

So it now works, for some reason with “$this->db->begin()”. But know when I create a SQL query to update the date it gives me following message:
[DB_ERROR_1292] Incorrect date value: ‘2014’ for column ‘dolibarr’.‘llx_facture_fourn_extrafields’.‘pay_time’ at row 1

But the SQL does not have ‘2014’ anywhere. SQL query:
UPDATE llx_facture_fourn_extrafields SET pay_time = 2023-04-05 WHERE fk_object = 1663

Online it suggest to use values(STR_TO_DATE("04-05-2023", "%Y-%m-%d")), but that makes no real sense to me.
Any suggestions?

Hallo,
2014 is the result of the subtraction 2023-04-05, take a look at the MYSQL syntax for updating date fields

1 Like

I used the following code:

"UPDATE llx_facture_fourn_extrafields SET pay_time = (SELECT DATE_ADD((SELECT datef FROM llx_facture_fourn WHERE rowid = ".$idstring."), INTERVAL ".$dateinterval." DAY)) WHERE fk_object = ".$idstring

Still do not really understand, why it did not work with the code snippets I found online for similar problems, because this code does not seem efficent to me. But at least it works.