diff --git a/application/controllers/mockup/fo/resultemailv7/Done.php b/application/controllers/mockup/fo/resultemailv7/Done.php index 6aa04e5f..bbc0b8c2 100644 --- a/application/controllers/mockup/fo/resultemailv7/Done.php +++ b/application/controllers/mockup/fo/resultemailv7/Done.php @@ -371,5 +371,62 @@ class Done extends MY_Controller } } + function cancel_email() + { + try { + if (!$this->isLogin) { + $this->sys_error("Invalid Token"); + return; + } + + $prm = $this->sys_input; + $userid = isset($this->sys_user['M_UserID']) ? $this->sys_user['M_UserID'] : 0; + + $order_header_id = isset($prm['T_OrderHeaderID']) ? (int) $prm['T_OrderHeaderID'] : 0; + $recepient_email = isset($prm['T_SendEmailRecepient']) ? trim($prm['T_SendEmailRecepient']) : ''; + + if ($order_header_id == 0 || $recepient_email == '') { + $this->sys_error("Order ID dan Email wajib diisi"); + return; + } + + // Hanya boleh cancel jika status masih S (Sending/queued) + $check = $this->db_onedev->query(" + SELECT T_SendEmailID, T_SendEmailStatus + FROM t_send_email + WHERE T_SendEmailT_OrderHeaderID = ? + AND T_SendEmailRecepient = ? + AND T_SendEmailIsActive = 'Y' + LIMIT 1 + ", [$order_header_id, $recepient_email])->row_array(); + + if (!$check) { + $this->sys_error("Data email tidak ditemukan"); + return; + } + + if ($check['T_SendEmailStatus'] !== 'S') { + $this->sys_error("Tidak bisa dicancel, status saat ini: " . $check['T_SendEmailStatus']); + return; + } + + $this->db_onedev->query(" + UPDATE t_send_email + SET T_SendEmailStatus = 'C', + T_SendEmailLastUpdated = NOW(), + T_SendEmailLastUpdatedUserID = ? + WHERE T_SendEmailID = ? + ", [$userid, $check['T_SendEmailID']]); + + $this->sys_ok([ + "status" => "OK", + "message" => "Email berhasil dicancel" + ]); + + } catch (Exception $e) { + $this->sys_error($e->getMessage()); + } + } + } \ No newline at end of file