182 lines
7.5 KiB
PHP
182 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\ProfessionalProject\Http\Controllers;
|
|
|
|
use App\Utils\ModuleUtil;
|
|
use App\Utils\Util;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Crm\Entities\Schedule;
|
|
use Modules\Crm\Entities\ScheduleUser;
|
|
use Modules\Essentials\Entities\Reminder;
|
|
use Modules\Essentials\Entities\ToDo;
|
|
use Modules\ProfessionalProject\Entities\PpTask;
|
|
|
|
class IntegrationController extends Controller
|
|
{
|
|
protected $commonUtil;
|
|
protected $moduleUtil;
|
|
|
|
public function __construct(Util $commonUtil, ModuleUtil $moduleUtil)
|
|
{
|
|
$this->commonUtil = $commonUtil;
|
|
$this->moduleUtil = $moduleUtil;
|
|
}
|
|
|
|
protected function authorizeProfessionalProject()
|
|
{
|
|
$business_id = request()->session()->get('user.business_id');
|
|
if (! (auth()->user()->can('superadmin')
|
|
|| $this->moduleUtil->hasThePermissionInSubscription($business_id, 'professional_project_module'))) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
}
|
|
|
|
public function createEssentialsTodo(Request $request, $task_id)
|
|
{
|
|
$this->authorizeProfessionalProject();
|
|
$business_id = $request->session()->get('user.business_id');
|
|
if (! \Module::has('Essentials') || ! \Module::isEnabled('Essentials')) {
|
|
return ['success' => false, 'msg' => __('professionalproject::lang.essentials_not_enabled')];
|
|
}
|
|
|
|
$task = PpTask::with(['assignees', 'project'])->where('business_id', $business_id)->findOrFail($task_id);
|
|
try {
|
|
$created_by = (int) $request->session()->get('user.id');
|
|
$users = $task->assignees->pluck('id')->toArray();
|
|
if (empty($users)) {
|
|
$users = [$created_by];
|
|
}
|
|
|
|
$ref_count = $this->commonUtil->setAndGetReferenceCount('essentials_todos');
|
|
$settings = $request->session()->get('business.essentials_settings');
|
|
$settings = ! empty($settings) ? json_decode($settings, true) : [];
|
|
$prefix = ! empty($settings['essentials_todos_prefix']) ? $settings['essentials_todos_prefix'] : '';
|
|
$todo_task_id = $this->commonUtil->generateReferenceNumber('essentials_todos', $ref_count, null, $prefix);
|
|
|
|
$todo = ToDo::create([
|
|
'task' => '[PP-'.$task->id.'] '.$task->title,
|
|
'date' => $task->start_date ? $task->start_date->format('Y-m-d') : now()->format('Y-m-d'),
|
|
'end_date' => $task->due_date ? $task->due_date->format('Y-m-d') : null,
|
|
'description' => $task->description,
|
|
'estimated_hours' => $task->duration,
|
|
'priority' => $task->priority === 'normal' ? 'medium' : $task->priority,
|
|
'status' => 'new',
|
|
'business_id' => $business_id,
|
|
'created_by' => $created_by,
|
|
'task_id' => $todo_task_id,
|
|
]);
|
|
|
|
$todo->users()->sync($users);
|
|
|
|
return [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
'redirect_url' => url('essentials/todo/'.$todo->id.'/edit'),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().' Line:'.$e->getLine().' Message:'.$e->getMessage());
|
|
|
|
return ['success' => false, 'msg' => __('messages.something_went_wrong')];
|
|
}
|
|
}
|
|
|
|
public function createEssentialsReminder(Request $request, $task_id)
|
|
{
|
|
$this->authorizeProfessionalProject();
|
|
$business_id = $request->session()->get('user.business_id');
|
|
if (! \Module::has('Essentials') || ! \Module::isEnabled('Essentials')) {
|
|
return ['success' => false, 'msg' => __('professionalproject::lang.essentials_not_enabled')];
|
|
}
|
|
|
|
$task = PpTask::with('project')->where('business_id', $business_id)->findOrFail($task_id);
|
|
try {
|
|
$user_id = (int) $request->session()->get('user.id');
|
|
$date = $task->due_date ? $task->due_date->format('Y-m-d') : now()->format('Y-m-d');
|
|
$time = $task->due_date ? $task->due_date->format('H:i:s') : now()->format('H:i:s');
|
|
|
|
$reminder = Reminder::create([
|
|
'business_id' => $business_id,
|
|
'user_id' => $user_id,
|
|
'name' => '[PP-'.$task->id.'] '.$task->title,
|
|
'date' => $date,
|
|
'time' => $time,
|
|
'repeat' => 'one_time',
|
|
]);
|
|
|
|
return [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
'redirect_url' => url('essentials/reminder?event_id='.$reminder->id),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().' Line:'.$e->getLine().' Message:'.$e->getMessage());
|
|
|
|
return ['success' => false, 'msg' => __('messages.something_went_wrong')];
|
|
}
|
|
}
|
|
|
|
public function createCrmFollowUp(Request $request, $task_id)
|
|
{
|
|
$this->authorizeProfessionalProject();
|
|
$business_id = $request->session()->get('user.business_id');
|
|
if (! \Module::has('Crm') || ! \Module::isEnabled('Crm')) {
|
|
return ['success' => false, 'msg' => __('professionalproject::lang.crm_not_enabled')];
|
|
}
|
|
|
|
$task = PpTask::with(['assignees', 'project'])->where('business_id', $business_id)->findOrFail($task_id);
|
|
$contact_id = optional($task->project)->contact_id;
|
|
if (empty($contact_id)) {
|
|
return ['success' => false, 'msg' => __('professionalproject::lang.crm_contact_missing')];
|
|
}
|
|
|
|
try {
|
|
$startAt = $task->due_date ?: ($task->start_date ?: now());
|
|
$endAt = (clone $startAt)->addHour();
|
|
$creatorId = (int) $request->session()->get('user.id');
|
|
|
|
$schedule = Schedule::create([
|
|
'business_id' => $business_id,
|
|
'contact_id' => $contact_id,
|
|
'title' => '[PP-'.$task->id.'] '.$task->title,
|
|
'status' => 'scheduled',
|
|
'start_datetime' => $startAt->format('Y-m-d H:i:s'),
|
|
'end_datetime' => $endAt->format('Y-m-d H:i:s'),
|
|
'description' => $task->description,
|
|
'schedule_type' => 'meeting',
|
|
'allow_notification' => 1,
|
|
'notify_via' => ['mail'],
|
|
'notify_before' => 1,
|
|
'notify_type' => 'day',
|
|
'created_by' => $creatorId,
|
|
'followup_additional_info' => [
|
|
'professional_project_task_id' => $task->id,
|
|
'professional_project_id' => $task->project_id,
|
|
],
|
|
'follow_up_by' => 'user',
|
|
]);
|
|
|
|
$userIds = $task->assignees->pluck('id')->toArray();
|
|
if (empty($userIds)) {
|
|
$userIds = [$creatorId];
|
|
}
|
|
foreach (array_unique($userIds) as $uid) {
|
|
ScheduleUser::create([
|
|
'schedule_id' => $schedule->id,
|
|
'user_id' => $uid,
|
|
]);
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'msg' => __('lang_v1.success'),
|
|
'redirect_url' => action([\Modules\Crm\Http\Controllers\ScheduleController::class, 'edit'], ['follow_up' => $schedule->id]),
|
|
];
|
|
} catch (\Exception $e) {
|
|
\Log::emergency('File:'.$e->getFile().' Line:'.$e->getLine().' Message:'.$e->getMessage());
|
|
|
|
return ['success' => false, 'msg' => __('messages.something_went_wrong')];
|
|
}
|
|
}
|
|
}
|