<?php

namespace App\Models;

use CodeIgniter\Model;

class SwaModel extends Model
{
    function cekkuota($tujuan,$jumlah)
    {
        $this->db->transBegin();
        $this->db->query("UPDATE mst_wilayah SET counter= CASE WHEN counter+$jumlah <= kapasitas THEN counter+$jumlah ELSE counter END where wilayah_id='$tujuan'");
        $affected_rows = $this->db->affectedRows();

        $er = parse_error($this->db->error());
        if ($this->db->transStatus() === false) {
            $this->db->transRollback();
            return 9;
        } else {
            $this->db->transCommit();
            return $affected_rows;
        }

    }

    function rollbackkuota($tujuan,$koded)
    {
        $this->db->transBegin();
        $this->db->query("delete from tbl_warga where koded=:koded:,['koded'=>$koded");
        $affected_rows = $this->db->affectedRows();
        $this->cekkuota($tujuan,-$affected_rows);
                
        $er = parse_error($this->db->error());
        if ($this->db->transStatus() === false) {
            $this->db->transRollback();
            return 9;
        } else {
            $this->db->transCommit();
            return $affected_rows;
        }

    }


}
