<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateStokPenyesuaianTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'no_penyesuaian' => [
                'type'       => 'VARCHAR',
                'constraint' => 40,
            ],
            'tanggal_penyesuaian' => [
                'type' => 'DATE',
            ],
            'barang_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'kode_barang' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'nama_barang' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'satuan' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'stok_sistem' => [
                'type'       => 'INT',
                'constraint' => 11,
            ],
            'stok_fisik' => [
                'type'       => 'INT',
                'constraint' => 11,
            ],
            'selisih' => [
                'type'       => 'INT',
                'constraint' => 11,
            ],
            'jenis' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
            ],
            'alasan' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'catatan' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'user_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'status' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'default'    => 'aktif',
            ],
            'dibatalkan_oleh' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'dibatalkan_pada' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'catatan_pembatalan' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('no_penyesuaian');
        $this->forge->addKey('tanggal_penyesuaian');
        $this->forge->addKey('barang_id');
        $this->forge->addForeignKey('barang_id', 'barang', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('user_id', 'user', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->addForeignKey('dibatalkan_oleh', 'user', 'id', 'RESTRICT', 'CASCADE');
        $this->forge->createTable('stok_penyesuaian');
    }

    public function down()
    {
        $this->forge->dropTable('stok_penyesuaian');
    }
}
