db_onedev = $this->load->database("onedev", true); $this->db = $this->load->database("onedev", true); } function index() { $this->load->library("Resultcalc"); $id = 946; $rows = $this->resultcalc->auto($id); echo "
 $rows";
   }


   function testing(){
        $sql = "
                    SELECT M_DoctorID as id, M_DoctorCode as code,CONCAT(M_DoctorPrefix,M_DoctorPrefix2,' ',M_DoctorName,' ',M_DoctorSufix,M_DoctorSufix2,M_DoctorSufix3) as name
                    FROM m_doctor
                    WHERE 
                    M_DoctorIsActive	 = 'Y' LIMIT 5
                ";
                //echo $sql;
                $query = $this->db_onedev->query($sql);
                $datas = $query->result_array();
                echo json_encode($datas);
    }

    function updatedoublerecord($orderHeaderID) {
        // Step 1: Retrieve active records with the specified order header ID
        $this->db->where('T_OrderDetailT_OrderHeaderID', $orderHeaderID);
        $this->db->where('T_OrderDetailIsActive', 'Y');
        $query = $this->db->get('t_orderdetail');
        $records = $query->result();

        if (empty($records)) {
            return 0; // No active records found
        }

        // Group records by T_OrderDetailT_TestID for processing
        $groupedRecords = [];
        foreach ($records as $record) {
            $groupedRecords[$record->T_OrderDetailT_TestID][] = $record;
        }

        // Step 2: Initialize list to track records eligible for deactivation
        $deactivateIDs = [];

        foreach ($groupedRecords as $testID => $testRecords) {
            $activeRecord = null;

            // Step 3: Apply selection criteria to find one record to keep as active
            foreach ($testRecords as $record) {
                $this->db->where('So_ResultEntryT_OrderDetailID', $record->T_OrderDetailID);
                $this->db->where('So_ResultEntryStatus !=', 'NEW');
                $entryQuery = $this->db->get('so_resultentry');

                // Check if no result entries with status != 'NEW' exist for this record
                if ($entryQuery->num_rows() == 0) {
                    // If no activeRecord yet, apply selection conditions
                    if (!$activeRecord && is_null($record->T_OrderDetailResult)) {
                        $activeRecord = $record;
                    }
                }
            }

            // If no record with NULL T_OrderDetailResult, check for T_OrderDetailVerification = 'N'
            if (!$activeRecord) {
                foreach ($testRecords as $record) {
                    if ($record->T_OrderDetailVerification === 'N') {
                        $activeRecord = $record;
                        break;
                    }
                }
            }

            // If all records have T_OrderDetailVerification = 'Y', check for T_OrderDetailValidation = 'N'
            if (!$activeRecord) {
                foreach ($testRecords as $record) {
                    if ($record->T_OrderDetailValidation === 'N') {
                        $activeRecord = $record;
                        break;
                    }
                }
            }

            // Collect IDs for deactivation, excluding the active record
            foreach ($testRecords as $record) {
                if (!$activeRecord || $record->T_OrderDetailID != $activeRecord->T_OrderDetailID) {
                    $deactivateIDs[] = $record->T_OrderDetailID;
                }
            }

            // Update the chosen active record
            if ($activeRecord) {
                $this->db->where('T_OrderDetailID', $activeRecord->T_OrderDetailID);
                $this->db->update('t_orderdetail', [
                    'T_OrderDetailIsActive' => 'Y',
                    'T_OrderDetailUserID' => 777
                ]);
            }
        }

        // Step 4: Deactivate all other records for this order header and test ID combination
        if (!empty($deactivateIDs)) {
            $this->db->where_in('T_OrderDetailID', $deactivateIDs);
            $this->db->update('t_orderdetail', ['T_OrderDetailIsActive' => 'N']);
        }

        return 1; // Update completed
    }
}