Files
accounting_dev_v2/application/controllers/Accountingrepair.php
T
2026-09-11 18:06:17 +07:00

28 lines
1.6 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Accountingrepair extends CI_Controller
{
public function __construct() { parent::__construct(); if(!is_cli()) show_404(); $this->load->library('AuditService'); }
public function duplicate_refs()
{
if (strtolower((string)app_env('APP_ENV', ENVIRONMENT)) === 'production') { echo "Ditolak: repair ini hanya untuk development.\n"; return; }
$groups=$this->db->select('no_ref, COUNT(*) total')->from('journals')->group_by('no_ref')->having('COUNT(*) >',1)->get()->result();
$changed=0; $this->db->trans_begin();
try {
foreach($groups as $group) {
$rows=$this->db->query('SELECT id,no_ref FROM journals WHERE no_ref=? ORDER BY id ASC FOR UPDATE',array($group->no_ref))->result();
array_shift($rows);
foreach($rows as $row) {
$suffix='-D'.$row->id; $new=substr($row->no_ref,0,50-strlen($suffix)).$suffix;
$this->db->where('id',$row->id)->update('journals',array('no_ref'=>$new));
$this->auditservice->record('development_repair','journal',$row->id,'renumber_duplicate_reference',array('no_ref'=>$row->no_ref),array('no_ref'=>$new),null);
echo $row->no_ref.' -> '.$new."\n"; $changed++;
}
}
if(!$this->db->trans_status()) throw new RuntimeException('Repair database gagal.');
$this->db->trans_commit(); echo "Selesai: {$changed} referensi diperbaiki.\n";
} catch(Throwable $e) { $this->db->trans_rollback(); echo 'Gagal: '.$e->getMessage()."\n"; }
}
}