<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Accountheads extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper("accounts");
        checkLogin();
    }
    public function _remap($alias = "", $params = array())
    {
        $data['dataValue'] = $this->session;
        $data['pageTitle'] = "Account Head";
        switch ($alias) {
            case 'add':
                if (isset($_POST['submit'])) {
                    if (!$this->checkifAccountExists($_POST['account_name'])) {
                        $TableData = array(
                            'accategory_id' => filter_var($_POST['accategory_id']),
                            //   'account_code' => generateAccountCode(filter_var($_POST['account_code'])),
                            'account_name' => filter_var($_POST['account_name']),
                            // 'account_type' => filter_var($_POST['account_type']),
                            // 'account_plcategory' => filter_var($_POST['account_plcategory']),
                            // 'account_currency' => filter_var($_POST['account_currency']),
                            // 'account_partyname' => filter_var($_POST['account_partyname']),
                            // 'account_partyaddress' => filter_var($_POST['account_partyaddress']),
                            // 'account_partypan' => filter_var($_POST['account_partypan']),
                            // 'account_partycontact' => filter_var($_POST['account_partycontact']),
                            // 'account_partyemail' => filter_var($_POST['account_partyemail']),
                            // 'account_partycontactperson' => filter_var($_POST['account_partycontactperson']),
                            // 'account_partycontactpersoncontact' => filter_var($_POST['account_partycontactpersoncontact']),
                            'created_on' => date('Y-m-d H:i:s'),
                            'created_by' => 'admin',
                            'remarks' => filter_var($_POST['remarks']),
                            'status' => 1,
                        );
                        $this->db->insert('tbl_accounts', $TableData);
                        $account_id = $this->db->insert_id();
                        $voucherData = array(
                            "voucher_id" => 0,
                            "transaction_date" => date("Y-m-d"),
                            "account_id" => $account_id,
                            "Dr" => 0,
                            "Cr" => 0,
                            "fiscalyear_id" => $this->session->userdata['FiscalYearID'],
                            "created_on" => date('Y-m-d H:i:s'),
                            "branch_id" => $this->session->userdata("BranchID"),
                            "created_by" => $this->session->userdata("loggedUser"),
                            "remarks" => "Opening Balance Entry",
                            "status" => 1
                        );
                        if ($_POST['opening_balance_drcr'] == "DR") $voucherData['Dr'] = $_POST['opening_balance'];
                        if ($_POST['opening_balance_drcr'] == "CR") $voucherData['Cr'] = $_POST['opening_balance'];
                        $this->db->insert('tbl_voucherdetails', $voucherData);
                        redirect("accounts/accountheads/list");
                    } else {
                        echo "Account Head Name Already Exists";
                        die;
                    }
                }
                loadView("accounts/accountheads/add", $data);
                break;
            case 'edit':
                $id = $this->uri->segment(4);
                if (isset($_POST['submit'])) {
                    $TableData = array(
                        'accategory_id' => filter_var($_POST['accategory_id']),
                        //'account_code' => filter_var($_POST['account_code']),
                        'account_name' => filter_var($_POST['account_name']),
                        'account_type' => filter_var($_POST['account_type']),
                        'account_plcategory' => filter_var($_POST['account_plcategory']),
                        'account_currency' => filter_var($_POST['account_currency']),
                        'account_partyname' => filter_var($_POST['account_partyname']),
                        'account_partyaddress' => filter_var($_POST['account_partyaddress']),
                        'account_partypan' => filter_var($_POST['account_partypan']),
                        'account_partycontact' => filter_var($_POST['account_partycontact']),
                        'account_partyemail' => filter_var($_POST['account_partyemail']),
                        'account_partycontactperson' => filter_var($_POST['account_partycontactperson']),
                        'account_partycontactpersoncontact' => filter_var($_POST['account_partycontactpersoncontact']),
                        'created_on' => date('Y-m-d H:i:s'),
                        'created_by' => 'admin',
                        'remarks' => filter_var($_POST['remarks']),
                        'status' => 1,
                    );
                    $this->db->where('account_id', $id);
                    $this->db->update('tbl_accounts', $TableData);
                    $voucherData = array(
                        "Dr" => 0,
                        "Cr" => 0,
                    );
                    if ($_POST['opening_balance_drcr'] == "DR") $voucherData['Dr'] = $_POST['opening_balance'];
                    if ($_POST['opening_balance_drcr'] == "CR") $voucherData['Cr'] = $_POST['opening_balance'];
                    $v = $this->db->query("select * from tbl_voucherdetails where account_id =\"$id\" and voucher_id=\"0\" ")->row();
                    if ($v) {
                        $this->db->where('account_id', $id);
                        $this->db->where('voucher_id', 0);
                        $this->db->update('tbl_voucherdetails', $voucherData);
                    } else {
                        $voucherData = array(
                            "voucher_id" => 0,
                            "transaction_date" => date("Y-m-d"),
                            "account_id" => $id,
                            "Dr" => 0,
                            "Cr" => 0,
                            "fiscalyear_id" => $this->session->userdata['FiscalYearID'],
                            "created_on" => date('Y-m-d H:i:s'),
                            "branch_id" => $this->session->userdata("BranchID"),
                            "created_by" => $this->session->userdata("loggedUser"),
                            "remarks" => "Opening Balance Entry",
                            "status" => 1
                        );
                        if ($_POST['opening_balance_drcr'] == "DR") $voucherData['Dr'] = $_POST['opening_balance'];
                        if ($_POST['opening_balance_drcr'] == "CR") $voucherData['Cr'] = $_POST['opening_balance'];
                        $this->db->insert('tbl_voucherdetails', $voucherData);
                    }
                    redirect("accounts/accountheads/list");
                }
                $data['Account'] = $this->db->query("select * from tbl_accounts where account_id =\"$id\" ")->row();
                $data['Account']->OpeningEntry = $this->db->query("select * from tbl_voucherdetails where account_id =\"$id\" and voucher_id=\"0\"")->row();
                loadView("accounts/accountheads/edit", $data);
                break;
            case 'delete':
                $id = $this->uri->segment(4);
                if (!$this->myaccounts->hasTransaction("account", $id)) {
                $this->db->set('status', "-1");
                $this->db->set('remarks', "Deleted on" . date("Y-m-d H:i:s") . " by " . $this->session->userdata("loggedUser"));
                $this->db->where('account_id', $id);
                $this->db->update('tbl_accounts');
                }
                else
                {
                    echo "Transaction Exists!!! Can't Delete";die;
                }
                redirect("accounts/accountheads/list");
                break;
            case 'getdetails':
                $id = $this->uri->segment(4);
                $this->db->where("account_id", $id);
                $data['Account'] = $this->db->get("tbl_accounts")->row();
                $this->load->view("accounts/accountheads/details", $data);
                break;
            case 'renumerate':
                $Accounts = $this->db->where("status=1")->get("tbl_accounts")->result();
                foreach ($Accounts as $Account) :
                    $AccountCode = generateAccountCode($Account->accategory_id);
                    $this->db->where("account_id", $Account->account_id)->set("account_code", $AccountCode)->update("tbl_accounts");
                endforeach;
                redirect("accounts/accountheads/");
                break;
            default:
                loadView("accounts/accountheads/list", $data);
        }
    }
    function checkifAccountExists($account_name)
    {
        return ($this->db->query("select * from tbl_accounts where UPPER(account_name)='" . strtoupper($account_name) . "'")->num_rows() > 0) ? true : false;
    }
}