update controller 2:23am | 3/4/2025

 avatar
jthrx
php
12 days ago
5.5 kB
4
Indexable
public function update_request_details() {
        // Log the received POST data for debugging
        log_message('debug', 'POST data: ' . print_r($this->input->post(), true));
    
        // Check if the necessary data is provided
        if ($this->input->post('request_id') && $this->input->post('medicine_data') && $this->input->post('maintenance_data')) {
            $request_id = $this->input->post('request_id');
            $medicine_data = $this->input->post('medicine_data');
            $maintenance_data = $this->input->post('maintenance_data');
    
            // Log received data to verify
            log_message('debug', 'Medicine Data: ' . print_r($medicine_data, true));
            log_message('debug', 'Maintenance Data: ' . print_r($maintenance_data, true));
    
            // Process the medicine data
            if (!empty($medicine_data)) {
                foreach ($medicine_data as $medicine) {
                    $medicine_id = isset($medicine['id']) ? $medicine['id'] : null; // Safely access 'id'
                    $medicine_name = $medicine['name'];
                    $medicine_quantity = $medicine['quantity'];
                    $medicine_price = $medicine['price'];
                    $medicine_amount = $medicine['amount'];
    
                    // Check if medicine_id exists
                    if ($medicine_id) {
                        $medicine_update = array(
                            'medicine_prescribed' => $medicine_name,
                            'amount_per_piece' => $medicine_price,
                            'medicine_quantity' => $medicine_quantity,
                            'amount' => $medicine_amount
                        );
    
                        // Update the medicine_details table using request_id and medicine_id
                        $this->db->where('request_id', $request_id); // Ensure it matches the correct request
                        $this->db->where('id', $medicine_id); // Update specific medicine by its ID
                        // $this->db->update('medicine_details', $medicine_update);
                        $update_result = $this->db->update('medicine_details', $medicine_update);


                            // Check if rows were affected
                        if ($this->db->affected_rows() > 0) {
                            log_message('debug', 'Update was successful.');
                        } else {
                            log_message('error', 'No rows were affected.');
                        }
                        // Log the query and result to verify
                        log_message('debug', 'Medicine Update Query: ' . $this->db->last_query());
                        log_message('debug', 'Medicine Update Result: ' . ($update_result ? 'Success' : 'Failure'));
                    }
                }
            }
    
            // Process the maintenance data
            if (!empty($maintenance_data)) {
                foreach ($maintenance_data as $maintenance) {
                    $maintenance_id = isset($maintenance['id']) ? $maintenance['id'] : null; // Safely access 'id'
                    $maintenance_name = $maintenance['name'];
                    $maintenance_quantity = $maintenance['quantity'];
                    $maintenance_price = $maintenance['price'];
                    $maintenance_amount = $maintenance['amount'];
    
                    // Check if maintenance_id exists
                    if ($maintenance_id) {
                        $maintenance_update = array(
                            'maintenance' => $maintenance_name,
                            'amount_per_piece' => $maintenance_price,
                            'maintenance_quantity' => $maintenance_quantity,
                            'amount' => $maintenance_amount
                        );
    
                        // Update the maintenance_details table using request_id and maintenance_id
                        $this->db->where('request_id', $request_id); // Ensure it matches the correct request
                        $this->db->where('id', $maintenance_id); // Update specific maintenance by its ID
                        // $this->db->update('maintenance_details', $maintenance_update);
                        $update_result = $this->db->update('maintenance_details', $maintenance_update);

                         // Check if rows were affected
                         if ($this->db->affected_rows() > 0) {
                            log_message('debug', 'Update was successful.');
                        } else {
                            log_message('error', 'No rows were affected.');
                        }
                        // Log the query and result to verify
                        log_message('debug', 'Maintenance Update Query: ' . $this->db->last_query());
                        log_message('debug', 'Maintenance Update Result: ' . ($update_result ? 'Success' : 'Failure'));
                    }
                }
            }
    
            // Return a success response
            echo json_encode(array('status' => 'success', 'message' => 'Request processed successfully.'));
        } else {
            // Return an error if the data is missing
            log_message('error', 'Missing request_id or details');
            echo json_encode(array('status' => 'error', 'message' => 'Missing request ID or details.'));
        }
    }
Editor is loading...
Leave a Comment