137 lines
4.7 KiB
PHP
137 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\User;
|
|
use App\Models\Administration;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
|
|
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
|
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
|
|
|
$user = Auth::user();
|
|
|
|
// Lấy tất cả card nhận và gửi của user này
|
|
$administrations = Administration::where('msnv', $user->msnv)
|
|
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
|
->orderBy('date', 'desc')
|
|
->get();
|
|
|
|
return view('user.dashboard', compact('administrations', 'selectedMonth', 'user'));
|
|
}
|
|
|
|
public function sendThankcards()
|
|
{
|
|
$users = User::where('status', User::STATUS_ACTIVE)->where('msnv', '!=', Auth::user()->msnv)->get();
|
|
return view('user.send', compact('users'));
|
|
}
|
|
|
|
public function storeThankcards(Request $request)
|
|
{
|
|
$request->validate([
|
|
'receiver' => 'required|exists:user,msnv',
|
|
'amount' => 'required|integer|min:1|max:' . Administration::MAX_SEND_CARD_PER_MONTH
|
|
]);
|
|
|
|
$sender = Auth::user();
|
|
$receiverMsnv = $request->receiver;
|
|
$amount = $request->amount;
|
|
|
|
if ($sender->flag_send == User::FLAG_SEND_DISABLED) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Bạn hiện không có quyền gửi Thank Card.'
|
|
], 422);
|
|
}
|
|
|
|
$startOfMonth = Carbon::now()->startOfMonth();
|
|
$endOfMonth = Carbon::now()->endOfMonth();
|
|
|
|
// Tính tổng số lượng card ĐÃ gửi cho người này trong tháng
|
|
$cardsSentToThisUserThisMonth = Administration::where('msnv', $sender->msnv)
|
|
->where('receiver', $receiverMsnv)
|
|
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
|
->sum('sent');
|
|
|
|
if ($cardsSentToThisUserThisMonth + $amount > Administration::MAX_SEND_CARD_PER_MONTH) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => "Một tháng bạn chỉ được gửi tối đa " . Administration::MAX_SEND_CARD_PER_MONTH . " card cho một người. Bạn đã gửi {$cardsSentToThisUserThisMonth} card cho nhân viên này."
|
|
], 422);
|
|
}
|
|
|
|
// Validate số lượng thẻ còn lại
|
|
if ($sender->card < $amount) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Số lượng card của bạn không đủ. Vui lòng liên hệ Admin.'
|
|
], 422);
|
|
}
|
|
|
|
DB::transaction(function () use ($sender, $receiverMsnv, $amount) {
|
|
// 1. Insert thông tin người nhận
|
|
Administration::create([
|
|
'msnv' => $receiverMsnv,
|
|
'received' => $amount,
|
|
'sender' => $sender->msnv,
|
|
'sent' => 0,
|
|
'receiver' => null,
|
|
'date' => Carbon::today()
|
|
]);
|
|
|
|
// 2. Insert thông tin người gửi
|
|
Administration::create([
|
|
'msnv' => $sender->msnv,
|
|
'received' => 0,
|
|
'sender' => null,
|
|
'sent' => $amount,
|
|
'receiver' => $receiverMsnv,
|
|
'date' => Carbon::today()
|
|
]);
|
|
|
|
// 3. Trừ số card của người gửi
|
|
$sender->card -= $amount;
|
|
$sender->save();
|
|
|
|
// TODO: Bắn tin nhắn CO tới người nhận (Chatwork/System Notification)
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Đã gửi Thank card thành công!'
|
|
]);
|
|
}
|
|
|
|
public function changePasswordForm()
|
|
{
|
|
return view('user.change_password');
|
|
}
|
|
|
|
public function updatePassword(Request $request)
|
|
{
|
|
$request->validate([
|
|
'password' => 'required|min:6|confirmed'
|
|
]);
|
|
|
|
$user = Auth::user();
|
|
$user->pass = Hash::make($request->password);
|
|
$user->first_login = User::FIRST_LOGIN_FALSE;
|
|
$user->save();
|
|
|
|
if ($user->role == User::ROLE_ADMIN) {
|
|
return redirect()->route('admin.dashboard')->with('success', 'Đổi mật khẩu thành công!');
|
|
}
|
|
|
|
return redirect()->route('user.dashboard')->with('success', 'Đổi mật khẩu thành công!');
|
|
}
|
|
}
|