<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;
use Config\Database;

class ChangePenjualanTaxPercentToInteger extends Migration
{
    public function up()
    {
        $db = Database::connect();

        if (! $db->tableExists('penjualan') || ! $db->fieldExists('pajak_persen', 'penjualan')) {
            return;
        }

        $db->query('UPDATE penjualan SET pajak_persen = ROUND(COALESCE(pajak_persen, 0))');

        $this->forge->modifyColumn('penjualan', [
            'pajak_persen' => [
                'name'       => 'pajak_persen',
                'type'       => 'INT',
                'constraint' => 3,
                'unsigned'   => true,
                'null'       => false,
                'default'    => 11,
            ],
        ]);
    }

    public function down()
    {
        $db = Database::connect();

        if (! $db->tableExists('penjualan') || ! $db->fieldExists('pajak_persen', 'penjualan')) {
            return;
        }

        $this->forge->modifyColumn('penjualan', [
            'pajak_persen' => [
                'name'       => 'pajak_persen',
                'type'       => 'DECIMAL',
                'constraint' => '5,2',
                'null'       => false,
                'default'    => 11,
            ],
        ]);
    }
}
