150 lines
5.9 KiB
PHP
150 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Tool;
|
|
|
|
use App\Enums\ActivityType;
|
|
use App\Models\ActivityCopy;
|
|
use App\Models\ActivityForm;
|
|
use App\Models\ActivityFormDetail;
|
|
use App\Services\SigdCrudService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CopyActivityService extends SigdCrudService
|
|
{
|
|
public function __construct(ActivityCopy $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
// CopyDataAll if exist replace
|
|
// public function copyAllData($fromYear, $toYear)
|
|
// {
|
|
// DB::beginTransaction();
|
|
|
|
// try {
|
|
// $activityYears = activityYearRange($toYear);
|
|
// $activityForms = ActivityForm::where('inventory_year', $fromYear)
|
|
// ->whereNot('sector', 'folu')->rowActive()->get();
|
|
|
|
// foreach ($activityForms as $activityForm) {
|
|
// $existingForm = ActivityForm::where('inventory_year', $toYear)
|
|
// ->where('form_code', $activityForm->form_code)
|
|
// ->whereNot('sector', 'folu')->rowActive()->first();
|
|
|
|
// if ($existingForm) {
|
|
// $existingForm->update([
|
|
// 'updated_by' => Auth::user()->name,
|
|
// ]);
|
|
|
|
// $newActivityForm = $existingForm;
|
|
// } else {
|
|
// $dataForm = [
|
|
// 'sector' => $activityForm->sector,
|
|
// 'form_code' => $activityForm->form_code,
|
|
// 'inventory_year' => $toYear,
|
|
// 'verification_status' => 'unverified',
|
|
// 'validation_status' => 'unvalidated',
|
|
// 'lock_status' => 'open',
|
|
// ];
|
|
|
|
// $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) {
|
|
// $existingDetail = ActivityFormDetail::where('form_id', $newActivityForm->id)
|
|
// ->where('activity_year', $detail->activity_year)
|
|
// ->where('activity_code', $detail->activity_code)
|
|
// ->rowActive()->first();
|
|
|
|
// if ($existingDetail) {
|
|
// $existingDetail->update([
|
|
// 'activity_value' => $detail->activity_value,
|
|
// 'updated_by' => Auth::user()->name,
|
|
// ]);
|
|
// } else {
|
|
// $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;
|
|
// }
|
|
// }
|
|
}
|