2020-10-20 01:01:59 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-10-20 01:01:59 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-10-20 01:01:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2021-04-18 11:50:06 +02:00
|
|
|
use App\Models\Task;
|
2021-04-19 07:33:49 +02:00
|
|
|
use App\Models\TaskStatus;
|
2021-04-18 11:50:06 +02:00
|
|
|
|
2020-10-20 01:01:59 +02:00
|
|
|
/**
|
|
|
|
* Class for task status repository.
|
|
|
|
*/
|
|
|
|
class TaskStatusRepository extends BaseRepository
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
public function delete($task_status)
|
|
|
|
{
|
2023-08-03 00:30:17 +02:00
|
|
|
/** @var \App\Models\TaskStatus $ts **/
|
2023-08-06 09:35:19 +02:00
|
|
|
$ts = TaskStatus::query()->where('company_id', $task_status->company_id)
|
2021-04-19 07:33:49 +02:00
|
|
|
->first();
|
|
|
|
|
2021-04-19 11:59:11 +02:00
|
|
|
$new_status = $ts ? $ts->id : null;
|
2021-04-18 11:50:06 +02:00
|
|
|
|
2023-08-06 09:35:19 +02:00
|
|
|
Task::query()->where('status_id', $task_status->id)
|
2021-04-18 11:50:06 +02:00
|
|
|
->where('company_id', $task_status->company_id)
|
2021-04-19 07:33:49 +02:00
|
|
|
->update(['status_id' => $new_status]);
|
2021-04-18 11:50:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
parent::delete($task_status);
|
|
|
|
|
|
|
|
return $task_status;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-04-18 11:50:06 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
public function archive($task_status)
|
|
|
|
{
|
2023-06-29 08:52:51 +02:00
|
|
|
$task_status = TaskStatus::withTrashed()
|
|
|
|
->where('id', $task_status->id)
|
2022-07-21 06:02:29 +02:00
|
|
|
->where('company_id', $task_status->company_id)
|
2021-04-19 07:33:49 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
$new_status = $task_status ? $task_status->id : null;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-06-29 08:52:51 +02:00
|
|
|
Task::withTrashed()
|
|
|
|
->where('status_id', $task_status->id)
|
2023-05-22 13:59:07 +02:00
|
|
|
->where('company_id', $task_status->company_id)
|
|
|
|
->update(['status_id' => $new_status]);
|
2021-04-18 11:50:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
parent::archive($task_status);
|
|
|
|
|
|
|
|
return $task_status;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-05-18 01:12:12 +02:00
|
|
|
|
|
|
|
public function reorder(TaskStatus $task_status)
|
|
|
|
{
|
2023-05-22 13:59:07 +02:00
|
|
|
|
2023-08-06 09:35:19 +02:00
|
|
|
TaskStatus::query()->where('company_id', $task_status->company_id)
|
2023-05-22 13:59:07 +02:00
|
|
|
->where('id', '!=', $task_status->id)
|
|
|
|
->orderByRaw('ISNULL(status_order), status_order ASC')
|
|
|
|
->cursor()
|
2023-10-26 04:57:44 +02:00
|
|
|
->each(function ($ts, $key) use ($task_status) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-22 13:59:07 +02:00
|
|
|
if($ts->status_order < $task_status->status_order) {
|
|
|
|
$ts->status_order--;
|
|
|
|
$ts->save();
|
|
|
|
} elseif($ts->status_order >= $task_status->status_order) {
|
2024-01-14 05:05:00 +01:00
|
|
|
$ts->status_order++;
|
2023-05-22 13:59:07 +02:00
|
|
|
$ts->save();
|
2024-09-04 00:13:53 +02:00
|
|
|
} else {
|
|
|
|
$ts->status_order = 99999;
|
|
|
|
$ts->save();
|
2023-05-22 13:59:07 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-22 13:59:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2023-08-06 09:35:19 +02:00
|
|
|
TaskStatus::query()->where('company_id', $task_status->company_id)
|
2023-05-22 13:59:07 +02:00
|
|
|
->orderByRaw('ISNULL(status_order), status_order ASC')
|
|
|
|
->cursor()
|
|
|
|
->each(function ($ts, $key) {
|
2024-01-14 05:05:00 +01:00
|
|
|
$ts->status_order = $key + 1;
|
2023-05-22 13:59:07 +02:00
|
|
|
$ts->save();
|
|
|
|
});
|
2023-05-18 01:12:12 +02:00
|
|
|
|
|
|
|
}
|
2020-10-20 01:01:59 +02:00
|
|
|
}
|