Files
accounting_dev_v2/application/migrations/20260914000200_expand_company_bank_accounts.php
T
2026-09-15 10:38:43 +07:00

46 lines
2.1 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Expand_company_bank_accounts extends CI_Migration
{
private function column($name, $definition)
{
if (!$this->db->field_exists($name, 'company_bank_accounts')) {
$this->db->query("ALTER TABLE `company_bank_accounts` ADD `{$name}` {$definition}");
}
}
public function up()
{
if (!$this->db->table_exists('company_bank_accounts')) return;
$this->column('product_name', "VARCHAR(120) NULL AFTER `account_holder`");
$this->column('account_type', "ENUM('savings','deposit') NOT NULL DEFAULT 'savings' AFTER `product_name`");
$this->column('interest_rate', "DECIMAL(7,4) NOT NULL DEFAULT 0 AFTER `account_type`");
$this->column('opened_at', "DATE NULL AFTER `interest_rate`");
$this->column('maturity_date', "DATE NULL AFTER `opened_at`");
$this->column('gl_account_id', "INT NULL AFTER `currency`");
if ($this->db->table_exists('cash_accounts')) {
$this->db->query("UPDATE company_bank_accounts b
JOIN cash_accounts ca ON ca.bank_account_no=b.account_number
AND (ca.company_id=b.company_id OR ca.company_id IS NULL)
JOIN accounts a ON a.id=ca.gl_account_id AND a.company_id=b.company_id
SET b.gl_account_id=ca.gl_account_id
WHERE b.gl_account_id IS NULL");
}
if (!$this->db->query("SHOW INDEX FROM company_bank_accounts WHERE Key_name='idx_company_bank_report'")->row()) {
$this->db->query("ALTER TABLE company_bank_accounts ADD KEY idx_company_bank_report(company_id,account_type,is_active,maturity_date)");
}
if (!$this->db->query("SHOW INDEX FROM company_bank_accounts WHERE Key_name='idx_company_bank_gl'")->row()) {
$this->db->query("ALTER TABLE company_bank_accounts ADD KEY idx_company_bank_gl(company_id,gl_account_id)");
}
}
public function down()
{
throw new RuntimeException('Metadata rekening perusahaan dipertahankan untuk audit dan dokumen lama.');
}
}