328 lines
12 KiB
PHP
328 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Activity;
|
|
|
|
use App\Enums\ActivityType;
|
|
use App\Models\ActivityApprovalKonsolidasi;
|
|
use App\Models\ActivityForm;
|
|
use App\Models\ActivityFormDetail;
|
|
use App\Models\ReferenceUnitConversion;
|
|
use App\Models\SettingFormDetail;
|
|
use App\Services\SigdCrudService;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class FormService extends SigdCrudService
|
|
{
|
|
public function __construct(ActivityForm $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function getActivityForm($sector, $code, $inventoryYear, $agency = null)
|
|
{
|
|
$query = ActivityForm::where('sector', $sector)
|
|
->where('form_code', $code)
|
|
->where('inventory_year', $inventoryYear)
|
|
->rowActive()
|
|
->orderBy('inventory_year', 'desc');
|
|
|
|
if ($agency && $agency != 'none') {
|
|
if ($agency === 'all') {
|
|
// return $query->whereNotNull('agency_id')->get();
|
|
$produsen = $this->getDataApproval($sector, $code, $inventoryYear)
|
|
->where('status', 1)->pluck('agency_id')->toArray();
|
|
return $query->whereIn('agency_id', $produsen);
|
|
}
|
|
|
|
return $query->where('agency_id', $agency)->first();
|
|
}
|
|
|
|
return $query->whereNull('agency_id')->first();
|
|
}
|
|
|
|
|
|
public function getActivityFormDetails($formId, $agency = null)
|
|
{
|
|
if ($agency == 'all') {
|
|
$results = ActivityFormDetail::whereIn('form_id', $formId)
|
|
->rowActive()->groupBy('activity_year', 'activity_code', 'activity_unit_code')
|
|
->selectRaw('activity_year, activity_code, activity_unit_code, sum(activity_value) as activity_value')
|
|
->orderBy('activity_year', 'desc')->get();
|
|
|
|
return $results->groupBy('activity_year');
|
|
}
|
|
|
|
return ActivityFormDetail::where('form_id', $formId)
|
|
->rowActive()->orderBy('activity_year', 'desc')
|
|
->get()->groupBy('activity_year');
|
|
}
|
|
|
|
public function convertUnit($fromUnit, $toUnit, $value)
|
|
{
|
|
$conversion = ReferenceUnitConversion::where('from_unit_code', $fromUnit)
|
|
->where('to_unit_code', $toUnit)->rowActive()
|
|
->first();
|
|
|
|
if ($conversion) {
|
|
$convertedValue = $value * $conversion->value;
|
|
|
|
return $convertedValue;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
public function save($sector, $code, $inventoryYear, $data, $fromUnitCodes = null, $instansi = null)
|
|
{
|
|
$activityYears = activityYearRange($inventoryYear);
|
|
$query = ActivityForm::where('sector', $sector)
|
|
->where('form_code', $code)
|
|
->where('inventory_year', $inventoryYear)
|
|
->rowActive();
|
|
|
|
if ($instansi && $instansi != 'all' && $instansi != 'none') {
|
|
$query->where('agency_id', $instansi);
|
|
} else {
|
|
$query->whereNull('agency_id');
|
|
}
|
|
|
|
$activityForm = $query->first();
|
|
|
|
try {
|
|
if (!$activityForm) {
|
|
$instansiId = $instansi === 'all' || $instansi === 'none' ? null : $instansi;
|
|
$dataForm = [
|
|
'sector' => $sector,
|
|
'form_code' => $code,
|
|
'inventory_year' => $inventoryYear,
|
|
'verification_status' => 'unverified',
|
|
'validation_status' => 'unvalidated',
|
|
'lock_status' => 'open',
|
|
|
|
// new column
|
|
'agency_id' => $instansiId,
|
|
];
|
|
|
|
$activityForm = $this->create($dataForm);
|
|
logUserActivity(ActivityType::INSERT_ACTIVITY_DATA, $activityForm->id);
|
|
} else {
|
|
$this->update($activityForm, []);
|
|
logUserActivity(ActivityType::UPDATE_ACTIVITY_DATA, $activityForm->id);
|
|
}
|
|
|
|
foreach ($activityYears as $year) {
|
|
// if (isset($data[$year]) && collect($data[$year])->filter(function ($value) {
|
|
// return $value !== null;
|
|
// })->isNotEmpty()) {
|
|
if (isset($data[$year])) {
|
|
$this->saveDetail($code, $activityForm, $year, $data[$year], $fromUnitCodes);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function saveDetail($formCode, $activityForm, $year, $data, $fromUnitCodes = null)
|
|
{
|
|
foreach ($data as $activityCodeUnit => $activityValue) {
|
|
list($activityCode, $unitCode) = explode('-', $activityCodeUnit);
|
|
|
|
$activityFormDetail = ActivityFormDetail::where('form_id', $activityForm->id)
|
|
->where('activity_year', $year)
|
|
->where('activity_code', $activityCode)
|
|
->where('activity_unit_code', $unitCode)
|
|
->rowActive()
|
|
->first();
|
|
|
|
// Default Unit Code = $unitCode
|
|
// New Unit Code = $fromUnitCodes[$activityCode]
|
|
$newUnitCode = $fromUnitCodes != null ? $fromUnitCodes[$activityCode] : $unitCode;
|
|
if ($unitCode != $newUnitCode) {
|
|
$this->settingUnitCode($formCode, $activityCode, $unitCode, $newUnitCode);
|
|
}
|
|
|
|
if (!empty($activityValue)) {
|
|
$newValue = getOriginalValue($activityValue);
|
|
// Convert to Default Unit Code
|
|
// $newValue = $fromUnitCodes == null ? $newValue : $this->convertUnit($fromUnitCodes[$activityCode], $unitCode, $newValue);
|
|
|
|
if ($activityFormDetail) {
|
|
$oldValue = getFormattedValue($activityFormDetail->activity_value);
|
|
|
|
if ($oldValue != $activityValue) {
|
|
$this->update(
|
|
$activityFormDetail,
|
|
[
|
|
'activity_value' => $newValue,
|
|
'activity_unit_code' => $newUnitCode
|
|
]
|
|
);
|
|
}
|
|
} else {
|
|
$dataFormDetail = [
|
|
'form_id' => $activityForm->id,
|
|
'activity_year' => $year,
|
|
'activity_code' => $activityCode,
|
|
'activity_unit_code' => $newUnitCode,
|
|
'activity_value' => $newValue
|
|
];
|
|
|
|
$this->create($dataFormDetail, ActivityFormDetail::class);
|
|
}
|
|
} else {
|
|
if ($activityFormDetail) {
|
|
$this->delete($activityFormDetail);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function settingUnitCode($formCode, $activityCode, $oldUnitCode, $newUnitCode)
|
|
{
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$activityFormDetail = SettingFormDetail::where('form_code', $formCode)
|
|
->where('activity_code', $activityCode)->where('unit_code', $oldUnitCode)
|
|
->rowActive()->first();
|
|
|
|
if ($activityFormDetail) {
|
|
$activityFormDetail->update([
|
|
'unit_code' => $newUnitCode,
|
|
'updated_by' => auth()->user()->id ?? null
|
|
]);
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
throw new Exception('Failed to update unit code: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function lockForm($id)
|
|
{
|
|
$activityForm = ActivityForm::findOrFail($id);
|
|
$this->update($activityForm, ['lock_status' => 'locked']);
|
|
|
|
logUserActivity(ActivityType::LOCK_ACTIVITY_FORM, $activityForm->id);
|
|
}
|
|
|
|
public function unlockForm($id)
|
|
{
|
|
$activityForm = ActivityForm::findOrFail($id);
|
|
$this->update($activityForm, ['lock_status' => 'open']);
|
|
|
|
logUserActivity(ActivityType::UNLOCK_ACTIVITY_FORM, $activityForm->id);
|
|
}
|
|
|
|
public function copyData($sector, $code, $fromYear, $toYear)
|
|
{
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$activityYears = activityYearRange($toYear);
|
|
$activityForm = ActivityForm::where('inventory_year', $fromYear)
|
|
->where('sector', $sector)->where('form_code', $code)->rowActive()->first();
|
|
|
|
if ($activityForm) {
|
|
$existingForm = ActivityForm::where('inventory_year', $toYear)
|
|
->where('sector', $activityForm->sector)
|
|
->where('form_code', $activityForm->form_code)
|
|
->rowActive()->first();
|
|
|
|
if ($existingForm) {
|
|
$existingForm->update([
|
|
'updated_by' => Auth::user()->name,
|
|
]);
|
|
|
|
$newActivityForm = $existingForm;
|
|
} else {
|
|
$dataForm = [
|
|
'sector' => $sector,
|
|
'form_code' => $code,
|
|
'inventory_year' => $toYear,
|
|
'verification_status' => 'unverified',
|
|
'validation_status' => 'unvalidated',
|
|
'lock_status' => 'open',
|
|
];
|
|
|
|
$newActivityForm = $this->create($dataForm);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public function approvalKonsolidasi($form, $sector, $code, $inventoryYear, $agencyId)
|
|
{
|
|
$hasData = $this->getDataApproval($sector, $code, $inventoryYear, true)->where([
|
|
'form_id' => $form->id,
|
|
'agency_id' => $agencyId,
|
|
])->rowActive()->first();
|
|
|
|
$newStatus = 1;
|
|
if ($hasData) {
|
|
$newStatus = $hasData->status == 1 ? 0 : 1;
|
|
}
|
|
|
|
$dataForm = [
|
|
'form_id' => $form->id,
|
|
'form_sector' => $sector,
|
|
'form_code' => $code,
|
|
'inventory_year' => $inventoryYear,
|
|
'agency_id' => $agencyId,
|
|
'status' => $newStatus,
|
|
];
|
|
|
|
$this->createOrUpdate([
|
|
'form_id' => $form->id,
|
|
'form_sector' => $sector,
|
|
'form_code' => $code,
|
|
'inventory_year' => $inventoryYear,
|
|
'agency_id' => $agencyId,
|
|
], $dataForm, ActivityApprovalKonsolidasi::class);
|
|
|
|
$activityType = $newStatus == 1 ? ActivityType::APPROVAL_KONSOLIDASI : ActivityType::DISSAPPROVAL_KONSOLIDASI;
|
|
logUserActivity($activityType, $form->id);
|
|
}
|
|
}
|