<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Accounts 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'])) {
                    $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);
                    redirect("accounts");
                }
                loadView("accounts/add", $data);
                break;
            case 'edit':
                if (isset($_POST['submit'])) {
                    $id = $this->uri->segment(3);
                    $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);
                    redirect("accounts");
                }
                loadView("accounts/edit", $data);
                break;
            case 'delete':
                $id = $this->uri->segment(3);
                $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');
                redirect("accounts");
                break;
            case 'getdetails':
                $id = $this->uri->segment(3);
                $this->db->where("account_id", $id);
                $data['Account'] = $this->db->get("tbl_accounts")->row();
                $this->load->view("accounts/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");
                break;
            case 'listvouchers':
                $data['pageTitle'] = "Journal Voucher";
                loadView("accounts/listvouchers", $data);
                break;
            case 'addvoucher':
                $data['pageTitle'] = "Journal Voucher";
                if (isset($_POST['submit']) && !isVoucherExists($_POST['voucher_no'])) {
                    $TableData = array(
                        'voucher_no' => filter_var($_POST['voucher_no']),
                        //'voucher_ref' => filter_var($_POST['voucher_ref']), // If the voucher is raised against other transactions
                        'voucher_date' => NepaliToEnglishDate(filter_var($_POST['voucher_date'])),
                        'voucher_state' => 'Entered',  // Different stages of voucher Entered/Reversed/Cancel/Deleted/Locked/Draft etc.
                        'voucher_type' => 'Journal',  //Different types of Vouchers like Cash/Bank/Payment/Receipt/advance etc.
                        'fiscalyear_id' => $this->session->userdata("FiscalYearID"),
                        'branch_id' => $this->session->userdata("BranchID"),
                        'created_by' => $this->session->userdata("loggedUser"),
                        'created_on' => date('Y-m-d H:i:s'),
                        'remarks' => filter_var($_POST['remarks']),
                        'status' => 1
                    );
                    $this->db->insert('tbl_vouchers', $TableData);
                    $voucher_id = $this->db->insert_id();
                    $account_ids = $_POST['account_ids'];
                    $narrations = $_POST['narration'];
                    $debits = $_POST['debit'];
                    $credits = $_POST['credit'];
                    $entry_no = 0;
                    for ($entry_no = 0; $entry_no < sizeof($account_ids); $entry_no++) {
                        $TableData = array();

                        $TableData = array(
                            'voucher_id' => $voucher_id,
                            'entry_no' => $entry_no,
                            'transaction_date' => NepaliToEnglishDate(filter_var($_POST['voucher_date'])),
                            'account_id' => $account_ids[$entry_no],
                            'narration' => $narrations[$entry_no],
                            'dr' => $debits[$entry_no],
                            'cr' => $credits[$entry_no],
                            //    'cheque' => filter_var($_POST['cheque']),
                            //    'cheque_details' => filter_var($_POST['cheque_details']),
                            'currency' => 'NPR',
                            //    'exrate' => filter_var($_POST['exrate']),
                            //    'fcdr' => filter_var($_POST['fcdr']),
                            //    'fccr' => filter_var($_POST['fccr']),
                            'branch_id' => $this->session->userdata("BranchID"),
                            'fiscalyear_id' => $this->session->userdata("FiscalYearID"),
                            'created_on' => date('Y-m-d H:i:s'),
                            'created_by' => $this->session->userdata("loggedUser"),
                            //    'remarks' => filter_var($_POST['remarks']),
                            'status' => 1
                        );
                        $this->db->insert('tbl_voucherdetails', $TableData);
                    }
                    redirect("accounts/listvouchers");
                    break;
                }

                loadView("accounts/addvoucher", $data);
                break;
            case 'voucherdetails':
                $voucher_id = $this->uri->segment(3);
                $Voucher = $this->db->where("voucher_id", $voucher_id)->get("tbl_vouchers")->row();
                $Voucher->Details = $this->db->where("voucher_id", $voucher_id)->get("tbl_voucherdetails")->result();
                $data['Voucher'] = $Voucher;
                $this->load->view("accounts/voucherdetails", $data);
                break;
            case 'show_voucher':
                $data['pageTitle']="Voucher Entries";
                $voucher_id = $this->uri->segment(3);
                $Voucher = $this->db->where("voucher_id", $voucher_id)->get("tbl_vouchers")->row();
                $Voucher->Details = $this->db->where("voucher_id", $voucher_id)->get("tbl_voucherdetails")->result();
                $data['Voucher'] = $Voucher;
                loadview("accounts/show_voucher", $data);
                break;
            case 'reversal':
                $voucher_id = $this->uri->segment(3);
                if (isVoucherReverseable($voucher_id)) :
                    $VoucherDetails = $this->db->where("voucher_id", $voucher_id)->get("tbl_voucherdetails")->result();
                    $entry_no = 0;
                    foreach ($VoucherDetails as $VoucherDetail) :
                        $TableData = array(
                            'voucher_id' => $voucher_id,
                            'entry_no' => $entry_no,
                            'transaction_date' => $VoucherDetail->transaction_date,
                            'account_id' => $VoucherDetail->account_id,
                            'narration' => "Reversal of Voucher #" . getFieldfromValue("tbl_vouchers", "voucher_no", "voucher_id", $VoucherDetail->voucher_id),
                            'dr' => $VoucherDetail->cr,
                            'cr' => $VoucherDetail->dr,
                            //    'cheque' => filter_var($_POST['cheque']),
                            //    'cheque_details' => filter_var($_POST['cheque_details']),
                            'currency' => 'NPR',
                            //    'exrate' => filter_var($_POST['exrate']),
                            //    'fcdr' => filter_var($_POST['fcdr']),
                            //    'fccr' => filter_var($_POST['fccr']),
                            'branch_id' => $this->session->userdata("BranchID"),
                            'fiscalyear_id' => $this->session->userdata("FiscalYearID"),
                            'created_on' => date('Y-m-d H:i:s'),
                            'created_by' => $this->session->userdata("loggedUser"),
                            //    'remarks' => filter_var($_POST['remarks']),
                            'status' => 1
                        );
                        $this->db->insert('tbl_voucherdetails', $TableData);
                        $entry_no++;
                    endforeach;
                    $this->db->where("voucher_id", $voucher_id)->set("voucher_state", 'Reversed')->update("tbl_vouchers");
                endif;
                redirect("accounts/listvouchers");
                break;
            case 'ledger':
                $data['pageTitle'] = "Ledger";
                $AccountGroups = $this->db->where("status", 1)->get("tbl_acgroups")->result();
                foreach ($AccountGroups as $AccountGroup) {
                    $AccountGroup->AccountCategories = $this->db->where("status", 1)->where("acgroup_id", $AccountGroup->acgroup_id)->get("tbl_accategories")->result();
                }
                $data['AccountGroups'] = $AccountGroups;
                loadView("accounts/ledger", $data);
                break;
            case 'ledgerall':
                $data['pageTitle'] = "Ledger";
                $Accounts = $this->db->where("status", 1)->get("tbl_accounts")->result();
                foreach ($Accounts as $Account) :


                endforeach;
                $AccountGroups = $this->db->where("status", 1)->get("tbl_acgroups")->result();
                $data['Accounts'] = $Accounts;
                $data['AccountGroups'] = $AccountGroups;
                loadView("accounts/ledgerall", $data);
                break;
            case 'getledgersummary':
                $account_id = $this->uri->segment(3);
                $data['pageTitle'] = "Ledger";
                $Transactions = $this->db->where("status", 1)->where("account_id", $account_id)->get("tbl_voucherdetails")->result();
                $Account = $this->db->where("status", 1)->where("account_id", $account_id)->get("tbl_accounts")->row();
                foreach ($Transactions as $T) {
                    //$T->AccountCategory=$this->db->where("status",1)->where("accategory_id",$AccountGroup->acgroup_id)->get("tbl_accategories")->result();
                    $T->Voucher = $this->db->where("status", 1)->where("voucher_id", $T->voucher_id)->get("tbl_vouchers")->row();
                }
                $data['Transactions'] = $Transactions;
                $data['Account'] = $Account;
                $data['account_id'] = $account_id;
                $this->load->view("accounts/ledgersummary", $data);
                break;
            case 'trialbalance':
                $data['pageTitle'] = "Trial Balance";
                $AccountGroups = $this->db->where("status", 1)->get("tbl_acgroups")->result();
                foreach ($AccountGroups as $AccountGroup) {
                    $AccountGroup->AccountCategories = $this->db->where("status", 1)->where("acgroup_id", $AccountGroup->acgroup_id)->get("tbl_accategories")->result();
                    foreach ($AccountGroup->AccountCategories as $AccountCategory) {
                        $AccountCategory->Accounts = $this->db->where("status", 1)->where("accategory_id", $AccountCategory->accategory_id)->get("tbl_accounts")->result();
                    }
                }
                $data['AccountGroups'] = $AccountGroups;
                loadView("accounts/trialbalance", $data);
                break;
            default:
                loadView("accounts/list", $data);
        }
    }
}