dictControlModel = new \App\Models\DictControlModel(); $this->controlTestModel = new \App\Models\ControlTestModel(); $this->resultModel = new \App\Models\ResultModel(); $this->commentModel = new \App\Models\ResultCommentModel(); } public function getControls() { $date = $this->request->getGet('date'); $deptid = $this->request->getGet('deptid'); $controls = $this->dictControlModel->getActiveByDate($date, $deptid); return $this->response->setJSON([ 'status' => 'success', 'message' => 'Controls fetched successfully', 'data' => $controls ]); } public function getTests() { $controlid = $this->request->getGet('controlid'); $tests = $this->controlTestModel->getByControl($controlid); return $this->response->setJSON([ 'status' => 'success', 'message' => 'Tests fetched successfully', 'data' => $tests ]); } public function saveDaily() { $post = $this->request->getPost(); $resultData = [ 'control_ref_id' => $post['controlid'] ?? 0, 'test_ref_id' => $post['testid'] ?? 0, 'resdate' => $post['resdate'] ?? date('Y-m-d'), 'resvalue' => $post['resvalue'] ?? '', 'rescomment' => $post['rescomment'] ?? '', ]; $success = $this->resultModel->saveResult($resultData); if ($success) { return $this->response->setJSON([ 'status' => 'success', 'message' => 'Result saved successfully' ]); } return $this->response->setJSON([ 'status' => 'error', 'message' => 'Failed to save result' ])->setStatusCode(500); } public function saveMonthly() { $post = $this->request->getPost(); $controlid = $post['controlid'] ?? 0; $testid = $post['testid'] ?? 0; $dates = $post['dates'] ?? ''; $resvalues = $post['resvalue'] ?? []; $success = true; foreach ($resvalues as $day => $value) { if (!empty($value)) { $resultData = [ 'control_ref_id' => $controlid, 'test_ref_id' => $testid, 'resdate' => $dates . '-' . str_pad($day, 2, '0', STR_PAD_LEFT), 'resvalue' => $value, 'rescomment' => '', ]; if (!$this->resultModel->saveResult($resultData)) { $success = false; } } } if ($success) { return $this->response->setJSON([ 'status' => 'success', 'message' => 'Monthly data saved successfully' ]); } return $this->response->setJSON([ 'status' => 'error', 'message' => 'Failed to save some entries' ])->setStatusCode(500); } public function saveComment() { $post = $this->request->getPost(); $commentData = [ 'control_ref_id' => $post['controlid'] ?? 0, 'test_ref_id' => $post['testid'] ?? 0, 'commonth' => $post['commonth'] ?? '', 'comtext' => $post['comtext'] ?? '', ]; $success = $this->commentModel->saveComment($commentData); if ($success) { return $this->response->setJSON([ 'status' => 'success', 'message' => 'Comment saved successfully' ]); } return $this->response->setJSON([ 'status' => 'error', 'message' => 'Failed to save comment' ])->setStatusCode(500); } }