Gửi email thông báo cho admin khi tạo đơn, trả email thông báo đơn được duyệt hoặc từ chối cho user

This commit is contained in:
Truong Vo 2024-08-12 09:33:51 +07:00
parent 72b586e94a
commit 2fb2d44d6c
6 changed files with 206 additions and 4 deletions

View File

@ -19,4 +19,9 @@ class CategoryController extends Controller
$data = Category::where('c_type', '=', $request->type)->where('c_active', '=', 1)->select('id', 'c_code', 'c_name', 'c_value', 'c_type')->get();
return AbstractController::ResultSuccess($data);
}
public static function getListMasterByCodeAndType($type, $code)
{
$data = Category::where('c_type', '=', $type)->where('c_code', '=', $code)->where('c_active', '=', 1)->select('id', 'c_code', 'c_name', 'c_value', 'c_type')->first();
return $data;
}
}

View File

@ -4,6 +4,8 @@ namespace Modules\Admin\app\Http\Controllers;
use App\Helper\Cache\CurrentMonthTimekeeping;
use App\Http\Controllers\Controller;
use App\Mail\ContactMail;
use App\Mail\TicketMail;
use App\Models\Notes;
use App\Traits\AnalyzeData;
use App\Traits\HasFilterRequest;
@ -13,6 +15,7 @@ use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Modules\Admin\app\Models\Admin;
use Modules\Admin\app\Models\Category;
use Modules\Admin\app\Models\Ticket;
@ -188,7 +191,6 @@ class TicketController extends Controller
public function createTicket(Request $request)
{
// Define validation rules
$rules = [
'start_date' => 'required|date',
@ -213,6 +215,15 @@ class TicketController extends Controller
// return $user;
$dataMasterStartPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $startPeriod);
$dataMasterEndPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $endPeriod);
$dataMasterType = CategoryController::getListMasterByCodeAndType("REASON", $type);
$formattedStartDate = Carbon::createFromFormat('d-m-Y', $startDate)->format('d/m/Y');
$formattedEndDate = Carbon::createFromFormat('d-m-Y', $endDate)->format('d/m/Y');
$user = auth('admins')->user();
$ticket = Ticket::create([
'start_date' => Carbon::create($startDate)->setTimezone(env('TIME_ZONE')),
'start_period' => $startPeriod,
@ -225,7 +236,22 @@ class TicketController extends Controller
]);
// Send notification email to admin (list)
$admins = Admin::where('permission', 'admin')->get();
foreach ($admins as $key => $value) {
if ($value->email == "admin@apactech.io") {
continue;
}
$data = array(
"email_template" => "email.notification_tickets",
"email" => $user->email,
"name" => $user->name,
"date" => $dataMasterStartPeriod->c_name . " (" . $formattedStartDate . ") - " . $dataMasterEndPeriod->c_name . " (" . $formattedEndDate . ")",
"type" => $dataMasterType->c_name,
"note" => $reason,
"link" => "tickets-management" //link đến page admin
);
Mail::to($value->email)->send(new TicketMail($data));
}
return response()->json(['data' => $ticket, 'status' => true]);
}
@ -288,9 +314,21 @@ class TicketController extends Controller
// Refuse
// Update updated_by and admin_note in tickets table
$startDate = $ticket->start_date; //Start day
$startPeriod = $ticket->start_period; //The session begins
$endDate = $ticket->end_date; //End date
$endPeriod = $ticket->end_period; //Session ends
$type = $ticket->type;
$dataMasterStartPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $startPeriod);
$dataMasterEndPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $endPeriod);
$dataMasterType = CategoryController::getListMasterByCodeAndType("REASON", $type);
$formattedStartDate = Carbon::createFromFormat('Y-m-d', $startDate)->format('d/m/Y');
$formattedEndDate = Carbon::createFromFormat('Y-m-d', $endDate)->format('d/m/Y');
// Send notification email to users
$user = Admin::find($ticket->user_id);
if ($action == "confirm") {
foreach ($results as $result) {
list($year, $month, $day) = explode('-', $result['date']);
@ -340,9 +378,32 @@ class TicketController extends Controller
$ticket['updated_by'] = $admin->name;
$ticket['admin_note'] = $admin_note;
$ticket['status'] = 'CONFIRMED';
$ticket->save();
$this->createOrUpdateRecordForCurrentMonth($month, $year);
// $admins = Admin::where('permission', 'admin')->get();
// foreach ($admins as $key => $value) {
// if ($value->email == "admin@apactech.io") {
// continue;
// }
// }
// Send notification email to users
$data = array(
"email_template" => "email.notification_tickets_user",
"user_name" => $user->name,
"email" => $user->email,
"name" => $admin->name, //name admin duyệt
"date" => $dataMasterStartPeriod->c_name . " (" . $formattedStartDate . ") - " . $dataMasterEndPeriod->c_name . " (" . $formattedEndDate . ")",
"type" => $dataMasterType->c_name,
"note" => $ticket->reason,
"admin_note" => $admin_note,
"link" => "tickets", //link đến page admin
"status" => "confirmed"
);
Mail::to($user->email)->send(new TicketMail($data));
return response()->json(['message' => "confirmed", 'status' => true]);
}
@ -351,6 +412,20 @@ class TicketController extends Controller
$ticket['admin_note'] = $admin_note;
$ticket['status'] = 'REFUSED';
$ticket->save();
$data = array(
"email_template" => "email.notification_tickets_user",
"user_name" => $user->name,
"email" => $user->email,
"name" => $admin->name, //name admin duyệt
"date" => $dataMasterStartPeriod->c_name . " (" . $formattedStartDate . ") - " . $dataMasterEndPeriod->c_name . " (" . $formattedEndDate . ")",
"type" => $dataMasterType->c_name,
"note" => $ticket->reason,
"admin_note" => $admin_note,
"link" => "tickets", //link đến page admin
"status" => "refused"
);
Mail::to($user->email)->send(new TicketMail($data));
return response()->json(['message' => "refused", 'status' => true]);
}

58
BACKEND/app/Mail/TicketMail.php Executable file
View File

@ -0,0 +1,58 @@
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Address;
class TicketMail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
// replyTo: [
// new Address($this->data["email"], $this->data["name"]),
// ],
subject: '[Ticket] Ticket Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: ($this->data["email_template"]),
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

BIN
BACKEND/public/logo.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

@ -0,0 +1,29 @@
<img src="{{ asset(\App\Models\Setting::getByCache()->value('logo')) . 'logo.jpg' }}" alt="Logo" border="0"
width="100" style="display: block; width: 100px; max-width: 100px; min-width: 48px;">
<p>Dear Admin,</p>
<p></p>
<p>Employee {{ $data['name'] }} has sent a request ticket, the specific content is as follows:</p>
<p></p>
<p>Name: {{ $data['name'] }}</p>
<p>Date: {{ $data['date'] }}</p>
<p>Type: {{ $data['type'] }}</p>
<p>Note: {{ $data['note'] }}</p>
<p style="margin:0 0 16px;padding:0">
<a href='{{ asset(\App\Models\Setting::getByCache()->value('logo')) . $data['link'] }}'
style="
color: #fff;
border-radius: 10px;
background-color: rgba(68,115,196);
background-image: linear-gradient(to top left,rgba(0,0,0,.2),rgba(0,0,0,.2) 30%,rgba(0,0,0,0));
text-decoration: none;
display: inline-block;
font-weight: 600;
font-size: 16px;
line-height: 150%;
text-align: center;
margin: 0;
padding: 10px 12px;
">
Check now</a>
</p>

View File

@ -0,0 +1,35 @@
<img src="{{ asset(\App\Models\Setting::getByCache()->value('logo')) . 'logo.jpg' }}" alt="Logo" border="0"
width="100" style="display: block; width: 100px; max-width: 100px; min-width: 48px;">
<p>Dear {{ $data['user_name'] }},</p>
<p></p>
<p>Admin {{ $data['name'] }} has {{ $data['status'] }} your request</p>
<p></p>
<p>Your ticket information:</p>
<p>Name: {{ $data['user_name'] }}</p>
<p>Date: {{ $data['date'] }}</p>
<p>Type: {{ $data['type'] }}</p>
<p>Note: {{ $data['note'] }}</p>
<p>Content of admin's response:</p>
<p>{{ $data['admin_note'] }}</p>
<p style="margin:0 0 16px;padding:0">
<a href='{{ asset(\App\Models\Setting::getByCache()->value('logo')) . $data['link'] }}'
style="
color: #fff;
border-radius: 10px;
background-color: rgba(68,115,196);
background-image: linear-gradient(to top left,rgba(0,0,0,.2),rgba(0,0,0,.2) 30%,rgba(0,0,0,0));
text-decoration: none;
display: inline-block;
font-weight: 600;
font-size: 16px;
line-height: 150%;
text-align: center;
margin: 0;
padding: 10px 12px;
">
Check now</a>
</p>
<p>If you have any questions, please contact the administrator directly</p>
<p>E-mail: admin@apactech.io</p>