<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * The manager's actual typed-in allowance/deduction entries for a
 * payslip — not a computed tax/SSNIT formula (Ghana's statutory rates
 * change periodically and weren't safe to hardcode; see the plan's
 * payroll section). A payslip can have as many or as few lines as the
 * manager adds; `type` distinguishes which side of net pay a line adds
 * to or subtracts from.
 */
class CreatePayslipLineItemsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'payslip_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'type' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'comment'    => 'allowance, deduction',
            ],
            'label' => [
                'type'       => 'VARCHAR',
                'constraint' => 150,
            ],
            'amount' => [
                'type'       => 'DECIMAL',
                'constraint' => '10,2',
            ],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('payslip_id');
        $this->forge->addForeignKey('payslip_id', 'payslips', 'id', '', 'CASCADE');
        $this->forge->createTable('payslip_line_items');
    }

    public function down()
    {
        $this->forge->dropTable('payslip_line_items', true);
    }
}
