first change

This commit is contained in:
2025-07-27 17:40:56 +05:45
commit f8b9a6725b
3152 changed files with 229528 additions and 0 deletions

View File

@@ -0,0 +1,627 @@
<?php
namespace Modules\PMS\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Employee\Models\Employee;
use Modules\Employee\Interfaces\EmployeeInterface;
use Modules\PMS\Exports\TaskExport;
use Modules\PMS\Models\KanbanColumn;
use Modules\PMS\Models\Task;
use Modules\PMS\Repositories\KanbanColumnInterface;
use Modules\PMS\Repositories\TaskCategoryInterface;
use Modules\PMS\Repositories\TaskInterface;
use Modules\Product\Interfaces\ProductInterface;
use Modules\Template\Emails\SendMail;
use Modules\Template\Repositories\TemplateInterface;
use Modules\Template\Repositories\TemplateRepository;
use Modules\User\Services\UserService;
class TaskController extends Controller
{
private $task;
private $project;
private $employee;
private $taskCategory;
private $product;
private $kanbanColumn;
private $template;
public function __construct(
TaskInterface $task,
EmployeeInterface $employee,
TaskCategoryInterface $taskCategory,
KanbanColumnInterface $kanbanColumn,
ProductInterface $product,
TemplateInterface $template,
) {
$this->task = $task;
$this->employee = $employee;
$this->taskCategory = $taskCategory;
$this->product = $product;
$this->kanbanColumn = $kanbanColumn;
$this->template = $template;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$filters = $request->all();
$data['title'] = 'Task List';
$data['editable'] = false;
$data['tasks'] = $this->task->findAll($filters);
$data['products'] = $this->product->findAll();
$data['taskCategories'] = $this->taskCategory->findAll();
$data['memberList'] = $this->employee->pluck();
$data['statusList'] = $this->kanbanColumn->pluck(function ($query) {
$query->orderBy('position');
});
return view('pms::task.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Task';
$data['editable'] = false;
$data['status'] = $this->kanbanColumn->pluck(function ($query) {
$query->orderBy('position');
});
$data['priority'] = Task::PRIORITY;
$data['product'] = $this->product->pluck();
$data['memberList'] = $this->employee->pluck();
$data['categoryList'] = $this->taskCategory->pluck();
return view('pms::task.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$validator = Validator::make($request->all(), [
'title' => 'required|string|max:255',
]);
if ($validator->fails()) {
return to_route('task.create')->withError($validator->errors()->all());
}
$inputData = $request->except(['_token', 'file']);
$task = $this->task->create($inputData);
$statusName = $this->task->getTaskById($task->id)->statusName['status'];
if ($request->hasFile('file')) {
$path = 'uploads/task';
$inputData['file'] = uploadImage($request->file, $path);
$task->documents()->updateOrCreate([
'documentable_id' => $task->id,
'documentable_type' => 'Modules\PMS\Models\Task',
], [
'document_name' => 'Task Files',
'document_path' => $inputData['file'],
]);
}
$createdBy = UserService::getUserById($task->createdby);
$user = $task->assignedUser;
if (config('app.send_mail')) {
$templateModel = (new TemplateRepository())->where(['alias' => 'task-assignee-template'])->first();
$message = $templateModel->message;
$message = str_replace('#image', asset('storage/' . setting('logo_pic')), $message);
$message = str_replace('#task_subject', $task->title, $message);
$message = str_replace('#assigned_by', $createdBy->name, $message);
$message = str_replace('#task_desc', $task->desc, $message);
$message = str_replace('#task_deadline', $task->due_date, $message);
$data = [
'subject' => 'New Task Created',
'body' => $message,
];
Mail::to($user)->send(new SendMail($data));
}
$priorityLabel = Task::PRIORITY[$inputData['priority']] ?? 'Unknown';
$this->product->getProductById($inputData['product_id'])->client->createLog([
'title' => 'Task Created',
'data' => "Task titled '{$inputData['title']}' with an status '{$statusName}' has been created.",
]);
foreach ($inputData['assigned_id'] as $employeeId) {
$employee = $this->employee->getEmployeeById($employeeId);
if (is_iterable($employee)) {
foreach ($employee as $emp) {
$emp->createLog([
'title' => 'Task Has Been Assigned',
'data' => "Task titled '{$inputData['title']}' with status '{$statusName}' and priority '{$priorityLabel}' has been created.",
]);
}
} else {
$employee->user->createLog([
'title' => 'Task Has Been Assigned',
'data' => "Task titled '{$inputData['title']}' with status '{$statusName}' and priority '{$priorityLabel}' has been created.",
]);
}
}
return redirect()->route('task.index')->withSucess('Task Created');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'View Task';
$data['task'] = $this->task->getTaskById($id);
$data['time'] = $data['task']->latest_task_time;
$data['clientName'] = $this->product->getProductById($data['task']->product_id)->client->name;
$data['status'] = $this->kanbanColumn->pluck(function ($query) {
$query->orderBy('position');
});
$data['template'] = $this->template->pluck();
return view('pms::task.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Edit Task';
$data['editable'] = true;
$data['task'] = $this->task->getTaskById($id);
$data['status'] = $this->kanbanColumn->pluck(function ($query) {
$query->orderBy('position');
});
$data['priority'] = Task::PRIORITY;
$data['categoryList'] = $this->taskCategory->pluck();
$data['product'] = $this->product->pluck();
$data['memberList'] = $this->employee->pluck();
return view('pms::task.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
$statusName = $this->task->getTaskById($id)->statusName['status'];
$allAssignedIds = $request->assigned_id['ids'] ?? [];
$currentAssignedIds = $this->task->getTaskById($id)->assigned_id['ids'] ?? [];
$newAssignedEmployees = array_diff($allAssignedIds, $currentAssignedIds);
$validator = Validator::make($request->all(), [
'title' => 'required|string|max:255',
]);
if ($validator->fails()) {
return to_route('task.create')->withError($validator->errors()->all());
}
$inputData = $request->except(['_method', '_token']);
try {
// Updating Task
$task = $this->task->update($id, $inputData);
// Handling File Upload
if ($request->hasFile('file')) {
$path = 'uploads/task';
$inputData['file'] = uploadImage($request->file, $path);
$task->documents()->updateOrCreate(
[
'documentable_id' => $task->id,
'documentable_type' => 'Modules\Invoice\Models\ReceivedInvoice',
],
[
'document_name' => 'Task Files',
'document_path' => $inputData['file'],
]
);
// Log for file upload
foreach ($currentAssignedIds as $empId) {
$employee = Employee::find($empId);
if ($employee) {
$employee->user->createLog([
'title' => 'Document Added',
'data' => "A new document has been added to the task titled '{$task->title}'.",
]);
} else {
flash()->addError("Employee with ID {$empId} not found while creating log for file upload.");
}
}
$client = $this->product->getProductById($inputData['product_id'])->client;
$client->createLog([
'title' => 'Document Added',
'data' => "A new document has been added to the task titled '{$task->title}'.",
]);
}
// Creating Logs for Client
$priority = Task::PRIORITY[$inputData['priority']] ?? 'Unknown';
$client = $this->product->getProductById($inputData['product_id'])->client;
if ($client) {
$client->createLog([
'title' => 'Task Updated',
'data' => "Task titled '{$inputData['title']}' with status '{$statusName}' and priority '{$priority}' has been created.",
]);
} else {
flash()->addError("Client not found for product ID {$inputData['product_id']}.");
}
// Creating Logs for New Employees
if ($newAssignedEmployees) {
foreach ($newAssignedEmployees as $empId) {
$employee = Employee::find($empId);
if ($employee) {
$employee->user->createLog([
'title' => 'Task Assigned',
'data' => "Task titled '{$inputData['title']}' with status '{$statusName}' and priority '{$priority}' has been created.",
]);
} else {
flash()->addError("Employee with ID {$empId} not found while creating assignment log.");
}
}
}
// Creating Logs for Current Employees
if ($currentAssignedIds) {
foreach ($currentAssignedIds as $empId) {
$employee = Employee::find($empId);
if ($employee) {
$employee->user->createLog([
'title' => 'Task Updated',
'data' => "Task titled '{$inputData['title']}' with status '{$statusName}' and priority '{$priority}' has been created.",
]);
} else {
flash()->addError("Employee with ID {$empId} not found while creating update log.");
}
}
}
return redirect()->route('task.index')->withSuccess('Task updated successfully.');
} catch (\Throwable $th) {
flash()->addError("Error while updating task: {$th->getMessage()}");
return redirect()->route('task.index')->withErrors('Something went wrong, please try again.');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->task->delete($id);
flash()->success('Task Deleted Succesfully');
} catch (\Throwable $th) {
flash()->error($th->getMessage());
}
return response()->json(['status' => true, 'message' => 'Task Delete Succesfully']);
}
public function kanban(Request $request)
{
$data['title'] = 'Kanban Board';
$data['editable'] = false;
$data['statusList'] = $this->kanbanColumn->pluck(function ($query) {
$query->orderBy('position');
});
$data['memberList'] = $this->employee->pluck();
$data['columnsWithTasks'] = KanbanColumn::with([
'tasks' => function ($query) use ($request) {
$query->whereNull('parent_id');
if ($request->filled('search')) {
$search = $request->search;
$query->where('title', 'LIKE', "%$search%");
}
if ($request->filled('status')) {
$query->where('status', $request->status);
}
if ($request->filled('priority')) {
$query->where('priority', $request->priority);
}
if ($request->filled('date')) {
$dates = explode('to', $request->date);
$startDate = trim($dates[0]);
$endDate = isset($dates[1]) ? trim($dates[1]) : $startDate; // Handle single date or range
$query->whereBetween('start_date', [$startDate, $endDate]);
}
if ($request->filled('assigned_to')) {
$query->whereJsonContains("assigned_id->ids", (string) $request->assigned_to);
}
if (auth()->user()->hasRole('employee')) {
$assignedId = (string) auth()->user()->employee_id;
$query->whereJsonContains('assigned_id->ids', $assignedId);
}
$query->with('assignedUser:id,first_name,middle_name,last_name,profile_picture');
},
])
// ->withCount('tasks')
->orderBy('position')
->get();
// dd($data['columnsWithTasks']->toArray());
$data['columnLists'] = KanbanColumn::select(DB::raw("id, LOWER(REPLACE(name, ' ', '-')) as slug"))
->pluck('slug', 'id');
$data['taskCategories'] = $this->taskCategory->findAll();
return view('pms::task.kanban', $data);
}
public function changeStatus(Request $request)
{
try {
$taskModel = $this->task->getTaskById($request->id);
$prevTaskStatus = Task::findOrFail($request->id)->statusName['status'];
$client = $this->product->getProductById($taskModel->product_id)->client;
$taskModel->status = $request->changeStatus;
$taskModel->save();
$employeeIds = $taskModel->assigned_id['ids'];
// Creating logs for the client
$client->createLog([
'title' => 'Task Status Changed',
'data' => "Task Title: {$taskModel->title} | Status: From {$prevTaskStatus} to {$taskModel->statusName['status']}",
]);
// Creating logs for each employee assigned to the task
foreach ($employeeIds as $emp) {
$employee = Employee::findOrFail($emp);
$employee->user->createLog([
'title' => 'Task Status Changed',
'data' => "Task Title: {$taskModel->title} | Status: From {$prevTaskStatus} to {$taskModel->statusName['status']}",
]);
}
return response()->json([
'status' => true,
'msg' => 'Status Changed',
], 200);
} catch (\Throwable $th) {
return response()->json([
'status' => false,
'msg' => $th->getMessage(),
], 400);
}
}
public function fetchComment($task_id)
{
$data['task'] = $this->task->getTaskById($task_id);
return view('pms::task.partials.comment', $data);
}
public function storeComment(Request $request)
{
$request->validate([
'content' => 'required',
]);
$task = $this->task->getTaskById($request->task_id);
$task->comments()->create(['content' => $request->content, 'comment_by' => Auth::id()]);
$user = $task->assignedUser;
if (config('app.send_mail')) {
$templateModel = (new TemplateRepository())->where(['alias' => 'comment-template'])->first();
$message = $templateModel->message;
$message = str_replace('#image', asset('storage/' . setting('logo_pic')), $message);
$message = str_replace('#content', $request->content, $message);
$data = [
'subject' => 'New Comment Added on Your Task',
'body' => $message,
];
Mail::to($user)->send(new SendMail($data));
}
// Creating Logs for Client and Employees
$employeeIds = $this->task->getTaskById($request->task_id)->assigned_id['ids'];
$commentPreview = substr($request->content, 0, 50);
if (strlen($request->content) > 50) {
$commentPreview .= '...';
}
$task->product->client->createLog([
'title' => 'Comment Added in Task: "' . $task->title . '"',
'data' => 'Comment: ' . $commentPreview,
]);
foreach ($employeeIds as $emp) {
$employee = Employee::findOrFail($emp);
$employee->user->createLog([
'title' => 'New Comment Added in Task: "' . $task->title . '"',
'data' => 'Comment: ' . $commentPreview,
]);
}
return response()->json([
'status' => true,
'msg' => 'Comment Store',
], 200);
}
public function uploadFile(Request $request)
{
$task = $this->task->getTaskById($request->task_id);
$client = $task->product->client;
if ($request->hasFile('file')) {
$path = 'uploads/task';
$fileName = $request->file->getClientOriginalName();
$filePath = uploadImage($request->file, $path);
$task->documents()->create(['document_name' => $fileName, 'document_path' => $filePath]);
}
// Storing Logs for Client and Employees
$client->createLog([
'title' => "Document Uploaded",
'data' => "For Task Title '{$task->title}'",
]);
foreach ($task->assignedUser as $employee) {
$employee->user->createLog([
'title' => "Document Uploaded",
'data' => "For Task Title '{$task->title}'",
]);
}
return response()->json([
'status' => true,
'msg' => 'File Upload',
], 200);
}
public function storeSubTask(Request $request)
{
try {
$request->merge(['status' => 10]);
$request->validate([
'title' => 'required',
'start_date' => 'required',
]);
$task = $this->task->create($request->all());
// Creating Logs for Client and Employees
$employeeIds = $this->task->getTaskById($request->parent_id)->assigned_id['ids'];
$taskTitle = $this->task->getTaskById($request->parent_id)->title;
$client = $this->product->getProductById($this->task->getTaskById($request->parent_id)->product_id)->Client;
$client->createLog([
'title' => "Sub-Task Added.",
'data' => "For Task Title '{$taskTitle}'",
]);
foreach ($employeeIds as $emp) {
$employee = Employee::findOrFail($emp);
$employee->user->createLog([
'title' => "Sub-Task Added.",
'data' => "For Task Title '{$taskTitle}'",
]);
}
return redirect()->back()->withSucess('Sub Task Created');
} catch (\Throwable $th) {
return redirect()->back()->withError($th->getMessage());
}
}
public function gantChart()
{
$data['title'] = 'Gantt Chart';
return view('pms::task.gantt.chart', $data);
}
public function getGantChartTask(Request $request)
{
// try {
$filters = $request->all();
$tasks = $this->task->getAllTasks($filters);
// dd($tasks->toArray());
$finalArr = [];
foreach ($tasks as $key => $task) {
$finalArr[] = [
'taskID' => (string) $task->id,
'taskName' => $task->title . ' (' . $task->project?->project_name . ')',
'resource' => $task->title,
'startDate' => $task->start_date,
'endDate' => $task->due_date,
'duration' => null,
'percentComplete' => null,
'dependencies' => null,
];
if (isset($task['subTask']) && !empty($task['subTask'])) {
foreach ($task['subTask'] as $sub_task) {
array_push($finalArr, [
'taskID' => (string) $sub_task->id,
'taskName' => $sub_task->title,
'resource' => $sub_task->title,
'startDate' => $sub_task->start_date,
'endDate' => $sub_task->due_date,
'duration' => null,
'percentComplete' => null,
'dependencies' => (string) $task->id,
]);
}
}
}
return response()->json([
'status' => true,
'data' => $finalArr,
'msg' => 'Data fetched',
], 200);
// } catch (\Throwable $th) {
// return response()->json([
// 'status' => false,
// 'msg' => $th->getMessage(),
// ], 400);
// }
}
public function export(Request $request)
{
return Excel::download(new TaskExport($request), 'tasks.xlsx');
}
public function sendMail(Request $request)
{
try {
$task = $this->task->getTaskById($request->task_id);
$message = $request->message;
// $message = str_replace('#name', $task->name, $message);
// $message = str_replace('#email', $task->email, $message);
$data = [
'subject' => $request->subject,
'body' => $message,
];
Mail::to($task->assignedUser)->send(new SendMail($data));
return response()->json([
'status' => true,
'msg' => 'Mail send successfully',
], 200);
} catch (\Throwable $th) {
return response()->json([
'status' => false,
'msg' => $th->getMessage(),
], 500);
}
}
}