HUGE HELP on executing queries to insert to database

I using GroceryCRUD Enterprise and migrating from Codeigniter 3 to 4. With the transition, I cannot make inserts to my database anymore.
public function orden_paciente()
$crud = $this->_getGroceryCrudEnterprise();

	$crud->setSubject('Orden', 'Ordenes');
	$crud->setRelation('paciente_id','pacientes','{nombre} - {fecha_nacimiento}',null,'paciente_id DESC');
	$crud->setRelationNtoN('Examen', 'ordenes_examen', 'examenes', 'ordenes_id', 'examenes_id', '{examen} - {precio}');
	//$crud->set_relation_n_n('Examen', 'ordenes_examen', 'examenes', 'ordenes_id', 'examenes_id', 'precio','priority');
	$crud->fields(['no_orden', 'paciente_id', 'Examen','status','descuento','trans', 'comentarios'])
		 ->displayAs('descuento','Decuento en %')
         ->displayAs('trans','Tipo de Transaccion');
    $crud->fieldType('trans', 'dropdown', [
        '1' => 'Efectivo',
        '2' => 'Tarjeta Credito',
        '3' => 'Mixto',
	$crud->callbackAddField('no_orden', function ($row) {
            $query = $this->db->query("SELECT max(no_orden)+1 as maxi FROM `ordenes` WHERE 1");
	            foreach ($query->result_array() as $row)
	            return '<input type="readonly" maxlength="50" value="'. $row['maxi'].'" name="no_orden" style="width:462px">';
    $crud->callbackAfterInsert(function ($stateParameters) {
        $primary_key = $stateParameters->insertId;
        $data = $stateParameters->data;
        $pa = $data['paciente_id'];
        //Calculo Automatico para total ////////////////////
        $querya = $this->db->query("SELECT descuento FROM ordenes  where ordenes_id= ". $primary_key ."");
        foreach ($querya->result_array() as $rowa)
		    $de = $rowa['descuento'];
        $query = $this->db->query("SELECT ordenes_examen.ordenes_id, sum(examenes.precio) as total FROM ordenes_examen LEFT JOIN examenes ON ordenes_examen.examenes_id= examenes.examenes_id where ordenes_id= ". $primary_key ." group by ordenes_examen.ordenes_id");
	    foreach ($query->result_array() as $row)
		    $total= $row['total']*(1-$de/100);
	    //Guardar Total en columna
	    $query2 = $this->db->query("update ordenes set total = " . $total . " where ordenes_id = " . $primary_key . " ");

return $stateParameters;

	$output = $crud->render();


This is the working code I was using with CI3, but CI4 not working anymore. Basically, once the user submits the exams, a query extracts the prices for each exam, extracts the discount field, calculates the total, and updates the order field. I am in desperate need of help as I am about to give up on this.