88 lines
2.6 KiB
PHP
88 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Setting;
|
|
|
|
use App\Models\ReferenceGwp;
|
|
use App\Services\SigdCrudService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GwpService extends SigdCrudService
|
|
{
|
|
public function __construct(ReferenceGwp $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function getMatrix()
|
|
{
|
|
return $this->model->select('ghg_code', 'ar_code', 'value')
|
|
->orderBy('ghg_code')
|
|
->orderBy('ar_code')
|
|
->rowActive()
|
|
->get()
|
|
->groupBy('ghg_code')
|
|
->map(function ($item) {
|
|
return $item->keyBy('ar_code');
|
|
});
|
|
}
|
|
|
|
public function save(array $data)
|
|
{
|
|
DB::transaction(function () use ($data) {
|
|
foreach ($data['gwp'] as $ghgCode => $values) {
|
|
foreach ($values as $arCode => $value) {
|
|
$isExist = $this->model
|
|
->where('ghg_code', $ghgCode)
|
|
->where('ar_code', $arCode)
|
|
->rowActive()
|
|
->first();
|
|
|
|
if (!empty($value)) {
|
|
$newValue = getOriginalValue($value);
|
|
if ($isExist) {
|
|
$oldValue = (float) rtrim(rtrim($isExist->value, '0'), ',');
|
|
if ($oldValue != $newValue) {
|
|
$this->update($isExist, ['value' => $newValue]);
|
|
}
|
|
} else {
|
|
// Create new record
|
|
$this->create([
|
|
'ghg_code' => $ghgCode,
|
|
'ar_code' => $arCode,
|
|
'value' => $newValue,
|
|
]);
|
|
}
|
|
} else {
|
|
if ($isExist) {
|
|
$this->delete($isExist);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// private function defaultNumber($value)
|
|
// {
|
|
// $value = str_replace('.', '', $value); // Remove periods
|
|
// $value = str_replace(',', '.', $value); // Replace commas with periods
|
|
|
|
// if (is_numeric($value)) {
|
|
// return (float) $value;
|
|
// }
|
|
|
|
// return null;
|
|
// }
|
|
|
|
// private function defaultNumber($value)
|
|
// {
|
|
// $value = str_replace(',', '', $value); // Remove periods
|
|
|
|
// if (is_numeric($value)) {
|
|
// return (float) $value;
|
|
// }
|
|
|
|
// return null;
|
|
// }
|
|
}
|