sigd/app/Services/Tool/LockActivityService.php

140 lines
4.3 KiB
PHP

<?php
namespace App\Services\Tool;
use App\Enums\ActivityType;
use App\Enums\SigdStatus;
use App\Models\ActivityCopy;
use App\Models\ActivityForm;
use App\Models\ActivityFormDetail;
use App\Models\ActivityLock;
use App\Services\SigdCrudService;
use Illuminate\Support\Facades\DB;
class LockActivityService extends SigdCrudService
{
public function __construct(ActivityLock $model)
{
$this->model = $model;
}
public function lock(int $year)
{
$dataForm = [
'inventory_year' => $year,
'lock_status' => 'locked',
];
$this->createOrUpdate([
'inventory_year' => $year,
'row_status' => 1,
], $dataForm);
logUserActivity(ActivityType::LOCK_ACTIVITY_FORM);
}
public function unlock(int $year)
{
$dataForm = [
'inventory_year' => $year,
'lock_status' => 'open',
];
$this->createOrUpdate([
'inventory_year' => $year,
'row_status' => 1,
], $dataForm);
logUserActivity(ActivityType::UNLOCK_ACTIVITY_FORM);
}
public function isYearLocked(int $year): bool
{
$lock = ActivityLock::where('inventory_year', $year)->first();
return $lock ? $lock->lock_status : 'open';
}
public function copyActivityToNextYear($year)
{
try {
$executeTime = now();
$this->copyAllData($year, $year + 1);
$this->logCopyActivity($year, $year + 1, $executeTime);
} catch (\Exception $e) {
throw $e;
}
}
public function logCopyActivity($year, $nextYear, $time)
{
$copyRequest = [
'from_year' => $year,
'to_year' => $nextYear,
'executed_time' => $time,
'finished_time' => now(),
'status' => SigdStatus::SELESAI,
];
$this->create($copyRequest, ActivityCopy::class);
}
public function copyAllData($fromYear, $toYear)
{
DB::beginTransaction();
try {
$activityYears = activityYearRange($toYear);
$activityForms = ActivityForm::where('inventory_year', $fromYear)
->whereNot('sector', 'folu')->rowActive()->get();
// Delete existing ActivityForm and ActivityFormDetail for the toYear
ActivityForm::where('inventory_year', $toYear)
->whereNot('sector', 'folu')->rowActive()
->each(function ($form) {
$formDetail = ActivityFormDetail::where('form_id', $form->id)->get();
foreach ($formDetail as $item) {
$this->delete($item);
};
$this->delete($form);
});
foreach ($activityForms as $activityForm) {
$dataForm = [
'sector' => $activityForm->sector,
'form_code' => $activityForm->form_code,
'inventory_year' => $toYear,
'verification_status' => 'unverified',
'validation_status' => 'unvalidated',
'lock_status' => 'open',
'agency_id' => $activityForm->agency_id, // new column
];
$newActivityForm = $this->create($dataForm, ActivityForm::class);
logUserActivity(ActivityType::COPY_ACTIVITY_FORM, $newActivityForm->id);
$activityFormDetails = ActivityFormDetail::where('form_id', $activityForm->id)
->whereIn('activity_year', $activityYears)->rowActive()->get();
foreach ($activityFormDetails as $detail) {
$dataFormDetail = [
'form_id' => $newActivityForm->id,
'activity_year' => $detail->activity_year,
'activity_code' => $detail->activity_code,
'activity_unit_code' => $detail->activity_unit_code,
'activity_value' => $detail->activity_value,
];
$newActivityFormDetail = $this->create($dataFormDetail, ActivityFormDetail::class);
}
}
DB::commit();
return true;
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
}