? Fallagassrini

Fallagassrini Bypass Shell

echo"
Fallagassrini
";
Current Path : /home1/savoy/public_html/savoyglobal.net/koaladraft/application/models/

Linux gator3171.hostgator.com 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
Upload File :
Current File : /home1/savoy/public_html/savoyglobal.net/koaladraft/application/models/accountingmodel.php

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Accountingmodel extends CI_Model
{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        // $this->load->model('hrmodel');
    }

    function search_currency($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_currency');
        $this->db->order_by('currency_id', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_currency($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        if ($postdata['default_currency'] == 1)
        {
            $tblValues = array(
                'default_currency' => 0,
            );
            $this->db->update('finance_currency', $tblValues);
        }
        $tblValues = array(
            'currency_id' => $masterid,
            'currency_name' => $postdata['currency_name'],
            'currency_code' => $postdata['currency_code'],
            'default_currency' => $postdata['default_currency'],
        );

        $this->db->insert('finance_currency', $tblValues);
        $data['resfunction'] = 'search_currency';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_currency($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        if ($postdata['default_currency'] == 1)
        {
            $tblValues = array(
                'default_currency' => 0,
            );
            $this->db->update('finance_currency', $tblValues);
        }
        $tblValues = array(
            'currency_name' => $postdata['currency_name'],
            'currency_code' => $postdata['currency_code'],
            'default_currency' => $postdata['default_currency'],
        );
        $this->db->where('currency_id', $postdata['currency_id']);
        $this->db->update('finance_currency', $tblValues);
        $data['resfunction'] = 'search_currency';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function search_fiscal_year($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_fiscal_year');
        $this->db->join('master', 'master_id=fiscal_year_id');
        $this->db->order_by('fiscal_year_id', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_fiscal_year($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $fromdate = $this->mastermodel->convertdateformat($postdata['fiscal_year_start']);
        $todate = $this->mastermodel->convertdateformat($postdata['fiscal_year_end']);

        $tblValues = array(
            'fiscal_year_id' => $masterid,
            'fiscal_year_start' => $fromdate,
            'fiscal_year_end' => $todate,
        );
        $this->db->insert('finance_fiscal_year', $tblValues);
        $data['resfunction'] = 'search_fiscal_year';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_fiscal_year($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $fromdate = $this->mastermodel->convertdateformat($postdata['fiscal_year_start']);
        $todate = $this->mastermodel->convertdateformat($postdata['fiscal_year_end']);
        $closed = $postdata['closed'];
        $tblValues = array(
            'fiscal_year_start' => $fromdate,
            'fiscal_year_end' => $todate,
            'closed' => $closed
        );
        $this->db->where('fiscal_year_id', $postdata['fiscal_year_id']);
        $this->db->update('finance_fiscal_year', $tblValues);
        $data['resfunction'] = 'search_fiscal_year';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function set_fiscal_year_current($fiscal_year_id)
    {

        $tblValues = array('default' => "1");
        $this->db->where('fiscal_year_id', $fiscal_year_id);
        $this->db->update('finance_fiscal_year', $tblValues);

        $tblValues = array('default' => "0");
        $this->db->where('fiscal_year_id!=', $fiscal_year_id);
        $this->db->update('finance_fiscal_year', $tblValues);

        $data = $this->db->query($sql);
    }

    function search_sub_ac_settings($pagenum)
    {
        $data = array();
        $q = $this->db->get('finance_sub_account_settings');
        $data['results'] = $q->row();
        return $data;
    }

    function insert_sub_ac_settings($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'employee' => $postdata['employee'],
            'profitloss' => $postdata['profitloss'],
            'salary' => $postdata['salary'],
            'student' => $postdata['student'],
            'bank_account' => $postdata['bank_account']
        );

        $this->db->insert('finance_sub_account_settings', $tblValues);
        $data['resfunction'] = 'search_sub_ac_settings';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_sub_ac_settings($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'employee' => $postdata['employee'],
            'profitloss' => $postdata['profitloss'],
            'salary' => $postdata['salary'],
            'student' => $postdata['student'],
            'bank_account' => $postdata['bank_account'],
            'employee_fee_payable' => $postdata['employee_fee_payable'],
            'allowance' => $postdata['allowance'],
            'deduction' => $postdata['deduction'],
        );
        $this->db->where('sub_account_settings_id', '1');
        $this->db->update('finance_sub_account_settings', $tblValues);
        $data['resfunction'] = 'search_sub_ac_settings';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function search_account_type($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_chart_type');
        $this->db->join('chart_account_group', 'chart_account_group.chart_group_id=finance_chart_type.chart_group', 'left');
        $this->db->order_by('chart_type_id ', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_transaction_code($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_transaction_code');
        $this->db->order_by('transaction_id ', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_transaction_code($postdata)
    {
        $data = array();
        $this->db->trans_begin();
//        $masterid = $this->mastermodel->insertmasterdata();        
        $tblValues = array(
//                        'transaction_id'=>$masterid,
            'transaction_code' => $postdata['transaction_code'],
            'transaction_name' => $postdata['transaction_name'],
        );
        $this->db->insert('finance_transaction_code', $tblValues);
        $data['resfunction'] = 'search_transaction_code';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_transaction_code($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'transaction_code' => $postdata['transaction_code'],
            'transaction_name' => $postdata['transaction_name'],
        );
        $this->db->where('transaction_id', $postdata['transaction_id']);
        $this->db->update('finance_transaction_code', $tblValues);
        $data['resfunction'] = 'search_transaction_code';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function search_main_account($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_chart_master');
        $this->db->join('finance_chart_type', 'finance_chart_type.chart_type_id=finance_chart_master.chart_type_id');
        $this->db->where('parent', '0');
        $this->db->order_by('finance_chart_master.chart_type_id', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_main_account($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $chart_type_sort = $this->mastermodel->get_single_field_value('finance_chart_type', 'sort', 'chart_type_id ', $postdata['chart_type_id']);
        $start = $chart_type_sort * 100000;
        $max_code = $start + 50000;
        $code = $this->main_account_code_increment($start, $max_code);
        $data['resfunction'] = 'search_main_account';
        if ($max_code > ($code + 1))
        {

            if ($code == 0)
            {
                $acc_code = $start + 1;
            }
            else
            {
                $acc_code = $code + 1;
            }

            $count = $this->mastermodel->get_num_rows('finance_chart_master', 'chart_account_code', $acc_code);
            if ($count == 0)
            {
                $tblValues = array(
                    'chart_account_code' => $acc_code,
                    'chart_account_name' => $postdata['chart_account_name'],
                    'chart_type_id' => $postdata['chart_type_id'],
                );
                $this->db->insert('finance_chart_master', $tblValues);
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
            else
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Account Beyond the Limit';
            return $data;
        }
    }

    function update_main_account($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'chart_account_name' => $postdata['chart_account_name']
        );
        $this->db->where('chart_master_id', $postdata['chart_master_id']);
        $this->db->update('finance_chart_master', $tblValues);
        $data['resfunction'] = 'search_main_account';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function search_sub_account($pagenum)
    {
        $data = array();
        $this->db->select('c1.chart_master_id as chart_master_id, c1.chart_account_code	as chart_account_code,
            c1.chart_account_name as chart_account_name, chart_type_name, c2.chart_account_code as main_account_code, c2.chart_account_name as main_account_name');
        $this->db->from('finance_chart_master c1');
        $this->db->join('finance_chart_type', 'finance_chart_type.chart_type_id=c1.chart_type_id');
        $this->db->join('finance_chart_master c2', 'c2.chart_master_id=c1.parent');
        $this->db->where('c1.parent !=', '0');
        $this->db->order_by('c2.chart_account_name', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_sub_account($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $account_name = $postdata['chart_account_name'];
        $acc_subexp = $postdata['parent_id'];
        $explode = explode("~", $acc_subexp);
        $acc_parent = $explode[0];
        $acc_type = $explode[1];
        $chart_type_sort = $this->mastermodel->get_single_field_value('finance_chart_type', 'sort', 'chart_type_id ', $acc_type);

        $start = ($chart_type_sort * 100000) + 50000;
        $max_code = $start + 50000;
        $code = $this->main_account_code_increment($start, $max_code);
        $data['resfunction'] = 'search_sub_account';

        if ($max_code > ($code + 1))
        {

            if ($code == 0)
            {
                $acc_code = $start + 1;
            }
            else
            {
                $acc_code = $code + 1;
            }

            $tblValues = array(
                'chart_account_code' => $acc_code,
                'chart_account_name' => $account_name,
                'chart_type_id' => $acc_type,
                'parent' => $acc_parent
            );
            $this->db->insert('finance_chart_master', $tblValues);
        }
        else
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Byond Limit';
            return $data;
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_sub_account($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $account_name = $postdata['chart_account_name'];
        $tblValues = array(
            'chart_account_code' => $postdata['chart_account_code'],
            'chart_account_name' => $account_name,
        );

        $this->db->where('chart_master_id', $postdata['chart_master_id']);
        $this->db->update('finance_chart_master', $tblValues);
        $data['resfunction'] = 'search_sub_account';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function insert_account_type($postdata)
    {
        $data = array();
        $this->db->trans_begin();
//          $chart_type_name= $postdata['chart_type_name'];
//         $sort = $postdata['sort '];
        $this->db->query("update finance_chart_type set `sort`=`sort`+1 where `sort`>=" . $postdata['sort']);

        $tblValues = array(
            'chart_type_name' => $postdata['chart_type_name'],
            'sort ' => $postdata['sort'],
            'chart_group' => $postdata['chart_account_group']
        );

        $this->db->insert('finance_chart_type', $tblValues);
        $data['resfunction'] = 'search_account_type';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_account_type($postdata)
    {

        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'chart_type_name' => $postdata['chart_type_name'],
            'sort' => $postdata['sort'],
            'chart_group' => $postdata['chart_account_group']
        );
        $this->db->where('chart_type_id', $postdata['chart_type_id']);
        $this->db->update('finance_chart_type', $tblValues);
        $data['resfunction'] = 'search_account_type';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function search_bank_account($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_bank_account');
        $this->db->order_by('bank_account_id', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_bank_account($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $account_name = $postdata['chart_account_name'];
        $acc_subexp = $postdata['parent_id'];
        $explode = explode("~", $acc_subexp);
        $acc_parent = $explode[0];
        $acc_type = $explode[1];

        $start = ($acc_type * 100000) + 50000;
        $max_code = $start + 50000;
        $code = $this->main_account_code_increment($start, $max_code);

        $data['resfunction'] = 'search_bank_account';
        if ($max_code > ($code + 1))
        {

            if ($code == 0)
            {
                $acc_code = $start + 1;
            }
            else
            {
                $acc_code = $code + 1;
            }

            $tblValues = array(
                'chart_account_code' => $acc_code,
                'chart_account_name' => $account_name,
                'chart_type_id' => $acc_type,
                'parent' => $acc_parent
            );
            $this->db->insert('finance_chart_master', $tblValues);

            $tblValues = array(
                'chart_account_code' => $acc_code,
                'bank_account_number' => $postdata['bank_account_number'],
                'bank_name' => $postdata['bank_name'],
                'bank_address' => $postdata['bank_address'],
                'currency_id' => $postdata['currency_id'],
            );
            $this->db->insert('finance_bank_account', $tblValues);
        }
        else
        {
            $data['res'] = 0;
            $data['msg'] = 'Beyond Limit';
            return $data;
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_bank_account($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'bank_account_number' => $postdata['bank_account_number'],
            'bank_name' => $postdata['bank_name'],
            'bank_address' => $postdata['bank_address'],
            'currency_id' => $postdata['currency_id'],
        );
        $this->db->where('bank_account_id', $postdata['bank_account_id']);
        $this->db->update('finance_bank_account', $tblValues);
        $data['resfunction'] = 'search_bank_account';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    /*  functions for Post Dated Cheque */

    function search_post_dated_cheque($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('finance_cheque_info', 'finance_cheque_info.journal_id=finance_journal_refs.journal_id');
        $this->db->where('tr_type', 'post_dated_cheque');
        $this->db->where('posted', '0');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_post_dated_cheque_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('finance_cheque_info', 'finance_cheque_info.journal_id=finance_journal_refs.journal_id');
        $this->db->where('tr_type', 'post_dated_cheque');
        $this->db->where('posted', '1');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_post_dated_cheque($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_post_dated_cheque';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $description = "";
            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];
//            $reference = $this->get_transaction_code($transaction_id, $payment_type);
            $reference = date('ymdHsi');
            $tblValues = array(
                'journal_id' => $masterid,
//                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => $payment_type,
                'transaction_id' => $transaction_id,
                'tr_type' => 'post_dated_cheque'
            );
            $this->db->insert('finance_journal_refs', $tblValues);

            if ($payment_type == 2)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => "",
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }

            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {
                if ($postdata['payment_to_id' . $i] != "")
                {

                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];
                    $description = $postdata['description' . $i];

                    $debit = 0;
                    $credit = 0;
                    if ($transaction_id == 1)
                    {
                        $debit = $postdata['amount' . $i];
                        $credit = 0;
                    }

                    if ($transaction_id == 2)
                    {
                        $debit = 0;
                        $credit = $postdata['amount' . $i];
                    }

                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $chart_account_code,
                        'debit' => $debit,
                        'credit' => $credit,
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . ',';
                }
                $i++;
            }

            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function post_pdc($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_post_dated_cheque';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];
            $account = explode("~", $postdata['bankacc_id']);
            $transaction_id = $postdata['transaction_id'];
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            if ($transaction_id == 1)
            {
                $description = "Post Dated Cheque - Payment Recived From " . $account_to_code . "-" . $account_to_name;
            }
            if ($transaction_id == 2)
            {
                $description = "Post Dated Cheque - Payment Made to " . $account_to_code . "-" . $account_to_name;
            }


            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];

            $reference = $this->get_transaction_code($transaction_id, $payment_type);
//echo $reference;die();
            $tblValues = array(
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'reference' => $reference,
                'posted' => '1'
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);

            $tblValues = array(
                'chart_account_code' => $account_to_code
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_cheque_info', $tblValues);

            if ($transaction_id == 2)
                $journal = $this->mastermodel->get_data_dual('finance_journal', $journal_id, 'journal_id', '0', 'debit', 'journal_id');
            if ($transaction_id == 1)
                $journal = $this->mastermodel->get_data_dual('finance_journal', $journal_id, 'journal_id', '0', 'credit', 'journal_id');

            $tot_credit = 0;
            $tot_debit = 0;
            $total = 0;
            $charttypes = "";
            foreach ($journal as $row)
            {
                if ($row['description'] == "")
                {
                    $tblValues = array(
                        'description' => $description
                    );
                    $this->db->where('id', $row['id']);
                    $this->db->update('finance_journal', $tblValues);
                }
                $tot_credit+=$row['credit'];
                $tot_debit+=$row['debit'];
                if ($row['debit'] != 0)
                {
                    $total+=$row['debit'];
                }
                else
                {
                    $total+=$row['credit'];
                }
                $charttypes.=$row['chart_account_code'] . ',';
            }

            $total_amount = $total;
            $debit = 0;
            $credit = 0;
            if ($transaction_id == 1)
            {
                $debit = 0;
                $credit = $total_amount;
                $description = "Post Dated Cheque - Payment Made to   " . $charttypes;
            }
            if ($transaction_id == 2)
            {
                $debit = $total_amount;
                $credit = 0;
                $description = "Post Dated Cheque - Payment Received From  " . $charttypes;
            }

            $tblValues = array(
                'journal_id' => $journal_id,
                'chart_account_code' => $account_to_code,
                'credit' => $credit,
                'debit' => $debit,
                'description' => $description,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Updating Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_post_dated_cheque($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_post_dated_cheque';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];

            $description = "";
            $reference = $postdata['reference'];
            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];

            $tblValues = array(
                'description' => $description,
                'currency_id' => $default_currency,
                'payment_type_id' => 2,
                'transaction_id' => $transaction_id,
                'tr_type' => 'post_dated_cheque'
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);
            $tblValues = array(
                'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                'cheque_no ' => $postdata['cheque_no'],
                'chart_account_code' => "",
                'cheque_info' => $postdata['cheque_info']
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_cheque_info', $tblValues);

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_journal');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_refs');

            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {
                if ($postdata['payment_to_id' . $i] != "")
                {

                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];
                    $description = $postdata['description' . $i];

                    $debit = 0;
                    $credit = 0;
                    if ($transaction_id == 1)
                    {
                        $debit = $postdata['amount' . $i];
                        $credit = 0;
                    }

                    if ($transaction_id == 2)
                    {
                        $debit = 0;
                        $credit = $postdata['amount' . $i];
                    }

                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $chart_account_code,
                        'debit' => $debit,
                        'credit' => $credit,
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . "( $chart_account_code ) ,";
                }
                $i++;
            }

            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Updating Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    /* functions for payment voucher */

    function search_payment_voucher_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '1');
        $this->db->where('posted', '1');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_payment_voucher($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '1');
        $this->db->where('posted', '0');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_payment_voucher($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_payment_voucher';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment From " . $account_to_code . "-" . $account_to_name;
            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];
            $reference = $this->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'transaction_id' => $postdata['transaction_id'],
            );
            $this->db->insert('finance_journal_refs', $tblValues);

            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }

            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {

                if ($postdata['payment_to_id' . $i] != "")
                {

                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];

                    if (empty($postdata['description' . $i]))
                        $description = "Payment Received From " . $account_to_name . "-" . $account_to_code;
                    else
                        $description = $postdata['description' . $i];

                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $chart_account_code,
                        'debit' => $postdata['amount' . $i],
                        'credit' => 0,
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . ',';
                }
                $i++;
            }

            $tblValues = array(
                'journal_id' => $masterid,
                'chart_account_code' => $account_to_code,
                'debit' => 0,
                'credit' => $total_amount,
                'description' => "Payment Made to " . $charttypes,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);

            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_payment_voucher($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_payment_voucher';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment From " . $account_to_code . "-" . $account_to_name;

            $reference = $postdata['reference'];
            $payment_old = $postdata['payment_old'];
            $payment_new = $postdata['payment_type_id'];

            if (($payment_new != $payment_old) && ($payment_new == 1 || $payment_old == 1 ))
            {
                $transaction_id = $postdata['transaction_id'];
                $reference = $this->get_transaction_code($transaction_id, $payment_new);
            }

            $tblValues = array(
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'reference' => $reference
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);


            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_cheque_info');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_wire_info');

            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $journal_id,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $journal_id,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }


            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_journal');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_refs');


            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {
                if ($postdata['payment_to_id' . $i] != "")
                {
                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];
                    if (empty($postdata['description' . $i]))
                        $description = "Payment Received From " . $account_to_code . "-" . $account_to_name;
                    else
                        $description = $postdata['description' . $i];

                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $chart_account_code,
                        'debit' => $postdata['amount' . $i],
                        'credit' => 0,
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . "( $chart_account_code ) ,";
                }
                $i++;
            }
            $tblValues = array(
                'journal_id' => $journal_id,
                'chart_account_code' => $account_to_code,
                'credit' => $total_amount,
                'debit' => 0,
                'description' => "Payment Made to " . $charttypes,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Updating Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    /* functions for cost of center */

    function search_cost_center_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('cost_center', 'cost_journal_id=journal_id');
        $this->db->where('transaction_id', '1');
        $this->db->where('posted', '1');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_cost_center($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('cost_center', 'cost_journal_id=journal_id');
        $this->db->where('transaction_id', '1');
        $this->db->where('posted', '0');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_cost_center($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_cost_center';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment From " . $account_to_code . "-" . $account_to_name;
            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];
            $reference = $this->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'transaction_id' => $postdata['transaction_id'],
            );
            $this->db->insert('finance_journal_refs', $tblValues);

            $tblValues = array(
                'cost_journal_id' => $masterid
            );
            $this->db->insert('cost_center', $tblValues);

            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }

            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {

                if ($postdata['payment_to_id' . $i] != "")
                {

                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];

                    if (empty($postdata['description' . $i]))
                        $description = "Payment Received From " . $account_to_name . "-" . $account_to_code . ' for expense type - ' . $this->mastermodel->get_single_field_value('expense_type', 'expense_type_name', 'expense_type_id', $postdata['expense_type' . $i]);
                    else
                        $description = $postdata['description' . $i];

                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $chart_account_code,
                        'debit' => $postdata['amount' . $i],
                        'credit' => 0,
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . ',';

                    $id = mysql_insert_id();

                    $tblValues = array(
                        'finance_journal_id' => $id,
                        'expense_type_id' => $postdata['expense_type' . $i],
                    );
                    $this->db->insert('cost_expense_type', $tblValues);
                }
                $i++;
            }

            $tblValues = array(
                'journal_id' => $masterid,
                'chart_account_code' => $account_to_code,
                'debit' => 0,
                'credit' => $total_amount,
                'description' => "Payment Made to " . $charttypes,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);

            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_cost_center($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_cost_center';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment From " . $account_to_code . "-" . $account_to_name;

            $reference = $postdata['reference'];
            $payment_old = $postdata['payment_old'];
            $payment_new = $postdata['payment_type_id'];

            if (($payment_new != $payment_old) && ($payment_new == 1 || $payment_old == 1 ))
            {
                $transaction_id = $postdata['transaction_id'];
                $reference = $this->get_transaction_code($transaction_id, $payment_new);
            }

            $tblValues = array(
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'reference' => $reference
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);


            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_cheque_info');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_wire_info');

            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $journal_id,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $journal_id,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }


            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_journal');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_refs');


            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {
                if ($postdata['payment_to_id' . $i] != "")
                {
                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];
                    if (empty($postdata['description' . $i]))
                        $description = "Payment Received From " . $account_to_code . "-" . $account_to_name . ' for expense type - ' . $this->mastermodel->get_single_field_value('expense_type', 'expense_type_name', 'expense_type_id', $postdata['expense_type' . $i]);
                    else
                        $description = $postdata['description' . $i];

                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $chart_account_code,
                        'debit' => $postdata['amount' . $i],
                        'credit' => 0,
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . "( $chart_account_code ) ,";

                    $id = mysql_insert_id();
                    $id = mysql_insert_id();

                    $tblValues = array(
                        'finance_journal_id' => $id,
                        'expense_type_id' => $postdata['expense_type' . $i],
                    );
                    $this->db->insert('cost_expense_type', $tblValues);
                    $tblValues = array(
                        'finance_journal_id' => $id,
                        'expense_type_id' => $postdata['expense_type' . $i],
                    );
                    $this->db->insert('cost_expense_type', $tblValues);
                }
                $i++;
            }
            $tblValues = array(
                'journal_id' => $journal_id,
                'chart_account_code' => $account_to_code,
                'credit' => $total_amount,
                'debit' => 0,
                'description' => "Payment Made to " . $charttypes,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Updating Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    /* Ends */
    /* functions for Receipt voucher */

    function search_receipt_voucher($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '2');
        $this->db->where('posted', '0');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_receipt_voucher_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '2');
        $this->db->where('posted', '1');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_receipt_voucher($postdata)
    {
        $data = array();
        $fisc_year_id = $this->getfiscalyearid();
        $default_currency = $this->getcurrency();
        $data['resfunction'] = 'search_receipt_voucher';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {

            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment to " . $account_to_code . "-" . $account_to_name;

            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];
            $reference = $this->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'transaction_id' => $postdata['transaction_id'],
            );
            $this->db->insert('finance_journal_refs', $tblValues);
            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }

            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {

                if ($postdata['payment_to_id' . $i] != "")
                {

                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];


                    if (empty($postdata['description' . $i]))
                        $description = "Payment made to " . $account_to_name . "-" . $account_to_code;
                    else
                        $description = $postdata['description' . $i];

                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $chart_account_code,
                        'debit' => 0,
                        'credit' => $postdata['amount' . $i],
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . "( $chart_account_code ) ,";
                }
                $i++;
            }

            $tblValues = array(
                'journal_id' => $masterid,
                'chart_account_code' => $account_to_code,
                'debit' => $total_amount,
                'credit' => 0,
                'description' => "Payment Received From " . $charttypes,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);

            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_receipt_voucher($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_receipt_voucher';
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];

            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment to " . $account_to_code . "-" . $account_to_name;

            $reference = $postdata['reference'];
            $payment_old = $postdata['payment_old'];
            $payment_new = $postdata['payment_type_id'];
            if (($payment_new != $payment_old) && ($payment_new == 1 || $payment_old == 1 ))
            {
                $transaction_id = $postdata['transaction_id'];
                $reference = $this->get_transaction_code($transaction_id, $payment_new);
            }


            $tblValues = array(
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $description,
                'payment_type_id' => $postdata['payment_type_id'],
                'reference' => $reference,
                'currency_id' => $default_currency,
            );

            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_cheque_info');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_wire_info');

            if ($postdata['payment_type_id'] == 2)
            {

                $tblValues = array(
                    'journal_id' => $journal_id,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $journal_id,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }


            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_journal');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_refs');
            $total_amount = 0;
            $i = 1;
            $charttypes = "";
            while (isset($postdata['payment_to_id' . $i]))
            {

                if ($postdata['payment_to_id' . $i] != "")
                {

                    $chart_account = explode("~", $postdata['payment_to_id' . $i]);
                    $chart_account_id = $chart_account[0];
                    $chart_account_code = $chart_account[1];
                    $chart_account_name = $chart_account[2];
                    $total_amount+=$postdata['amount' . $i];


                    if (empty($postdata['description' . $i]))
                    {
                        $description = "Payment made to " . $account_to_name . "-" . $account_to_code;
                    }
                    else
                        $description = $postdata['description' . $i];

                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $chart_account_code,
                        'debit' => 0,
                        'credit' => $postdata['amount' . $i],
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $charttypes.=$chart_account_name . ',';
                }
                $i++;
            }

            $tblValues = array(
                'journal_id' => $journal_id,
                'chart_account_code' => $account_to_code,
                'debit' => $total_amount,
                'credit' => 0,
                'description' => "Payment Received From " . $charttypes,
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Updating Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    /* functions for bank journal */

    function search_bank_journal($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '4');
        $this->db->where('posted', '0');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_bank_journal_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '4');
        $this->db->where('posted', '1');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_bank_journal($postdata)
    {
        $data = array();
        $fisc_year_id = $this->getfiscalyearid();
        $default_currency = $this->getcurrency();
        $data['resfunction'] = 'search_bank_journal';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $date = $this->mastermodel->convertdateformat($postdata['journal_date']);
            $from_account = explode("~", $postdata['from_account_id']);
            $to_account = explode("~", $postdata['to_account_id']);
            $description = "Transaction From account $from_account[2] - $from_account[1] To account $to_account[2] -$to_account[1] ";
            $transaction_id = $postdata['transaction_id'];
            $payment_type = 0;
            $reference = $this->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $date,
                'description' => $description,
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => '0',
                'transaction_id' => '4',
                'posted' => '0'
            );

            $this->db->insert('finance_journal_refs', $tblValues);

            $tblValues = array(
                'journal_id' => $masterid,
                'chart_account_code' => $from_account[1],
                'debit' => '0',
                'credit' => $postdata['amount'],
                'description' => "Transaction to account $to_account[2] -$to_account[1]  ",
                'system_date' => date('Y-m-d'),
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);

            $tblValues = array(
                'journal_id' => $masterid,
                'chart_account_code' => $to_account[1],
                'debit' => $postdata['amount'],
                'credit' => '0',
                'description' => "Transaction From account $from_account[2] - $from_account[1] ",
                'system_date' => date('Y-m-d H:i:s'),
                'fiscal_year_id' => $fisc_year_id,
            );
            $this->db->insert('finance_journal', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_bank_journal($postdata)
    {
        $data = array();
        $data['resfunction'] = 'search_bank_journal';
        $fisc_year_id = $this->getfiscalyearid();
        if ($fisc_year_id != 0)
        {
            $this->db->trans_begin();

            $date = $this->mastermodel->convertdateformat($postdata['journal_date']);
            $from_account = explode("~", $postdata['from_account_id']);
            $to_account = explode("~", $postdata['to_account_id']);
            $description = "Transaction From account $from_account[2] - $from_account[1] To account $to_account[2] -$to_account[1] ";
            $journal_id = $postdata['journal_id'];

            $tblValues = array(
                'journal_date' => $date,
                'description' => $description,
                'payment_type_id' => '0',
                'transaction_id' => '4',
                'posted' => '0'
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);

            $tblValues = array(
                'journal_id' => $journal_id,
                'chart_account_code' => $from_account[1],
                'credit' => $postdata['amount'],
                'description' => "Transaction to account $to_account[2] -$to_account[1]",
                'system_date' => date('Y-m-d'),
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->where('debit', '0');
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal', $tblValues);

            $tblValues = array(
                'journal_id' => $journal_id,
                'chart_account_code' => $to_account[1],
                'debit' => $postdata['amount'],
                'credit' => '0',
                'description' => "Transaction From account $from_account[2] - $from_account[1]",
                'system_date' => date('Y-m-d H:i:s'),
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->where('credit', '0');
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Updating Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            $data['msg'] = 'Fiscal Year Not Set.';
            return $data;
        }
    }

    /* functions for journal voucher */

    function search_journal_voucher($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '3');
        $this->db->where('posted', '0');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_journal_voucher_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->where('transaction_id', '3');
        $this->db->where('posted', '1');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_journal_voucher($postdata)
    {
        $data = array();
        $fisc_year_id = $this->getfiscalyearid();
        $default_currency = $this->getcurrency();
        $data['resfunction'] = 'search_journal_voucher';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {

            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $date = $this->mastermodel->convertdateformat($postdata['journal_voucher_date']);

            $transaction_id = $postdata['transaction_id'];
            $payment_type = 0;
            $reference = $this->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $date,
                'description' => '',
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => '0',
                'transaction_id' => '3',
                'posted' => '0'
            );

            $this->db->insert('finance_journal_refs', $tblValues);

            $i = 1;
            while (isset($postdata['account_id' . $i]))
            {
                if ($postdata['account_id' . $i] != "")
                {
                    $account = explode("~", $postdata['account_id' . $i]);

                    if ($postdata['credit' . $i] > 0)
                        $description = "Transaction From $account[2] - $account[1]";
                    if ($postdata['debit' . $i] > 0)
                        $description = "Transaction To $account[2] - $account[1]";

                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $account[1],
                        'debit' => $postdata['debit' . $i],
                        'credit' => $postdata['credit' . $i],
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                        'system_date' => date('Y-m-d')
                    );
                    $this->db->insert('finance_journal', $tblValues);
                }
                $i++;
            }


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_journal_voucher($postdata)
    {
        $data = array();
        $fisc_year_id = $this->getfiscalyearid();
        $default_currency = $this->getcurrency();
        $data['resfunction'] = 'search_journal_voucher';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {

            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];
            $date = $this->mastermodel->convertdateformat($postdata['journal_voucher_date']);

            $transaction_id = $postdata['transaction_id'];
            $payment_type = 0;
            $tblValues = array(
                'journal_date' => $date,
                'description' => '',
                'currency_id' => $default_currency,
                'payment_type_id' => $payment_type,
                'transaction_id' => '3',
                'posted' => '0'
            );
            $this->db->where('journal_id', $journal_id);
            $this->db->update('finance_journal_refs', $tblValues);

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_journal');


            $i = 1;
            while (isset($postdata['account_id' . $i]))
            {
                if ($postdata['account_id' . $i] != "")
                {
                    $account = explode("~", $postdata['account_id' . $i]);

                    if ($postdata['credit' . $i] > 0)
                        $description = "From $account[2] - $account[1]";
                    if ($postdata['debit' . $i] > 0)
                        $description = "To $account[2] - $account[1]";

                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $account[1],
                        'debit' => $postdata['debit' . $i],
                        'credit' => $postdata['credit' . $i],
                        'description' => "Transaction $description",
                        'fiscal_year_id' => $fisc_year_id,
                        'system_date' => date('Y-m-d')
                    );
                    $this->db->insert('finance_journal', $tblValues);
                }
                $i++;
            }


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    /* functions for auto journal */

    function search_auto_journal($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_auto_journal_refs');
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_auto_journal($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();

        $tblValues = array(
            'auto_journal_id' => $masterid,
            'auto_journal_title' => $postdata['auto_journal_title'],
        );
        $this->db->insert('finance_auto_journal_refs', $tblValues);

        $i = 1;
        while (isset($postdata['account_id' . $i]))
        {
            if ($postdata['account_id' . $i] != "")
            {
                $account = explode("~", $postdata['account_id' . $i]);
                $tblValues = array(
                    'auto_journal_id' => $masterid,
                    'chart_account_code' => $account[1],
                    'debit' => $postdata['debit' . $i],
                    'credit' => $postdata['credit' . $i],
                    'description' => $postdata['description' . $i],
                );
                $this->db->insert('finance_auto_journal', $tblValues);
            }
            $i++;
        }

        $data['resfunction'] = 'search_auto_journal';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Adding Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Added Successfully';
            return $data;
        }
    }

    function update_auto_journal($postdata)
    {
        $data = array();
        $this->db->trans_begin();
        $auto_journal_id = $postdata['auto_journal_id'];

        $tblValues = array(
            'auto_journal_title' => $postdata['auto_journal_title'],
        );
        $this->db->where('auto_journal_id', $auto_journal_id);
        $this->db->update('finance_auto_journal_refs', $tblValues);

        $this->db->where('auto_journal_id', $auto_journal_id);
        $this->db->delete('finance_auto_journal');

        $i = 1;
        while (isset($postdata['account_id' . $i]))
        {
            if ($postdata['account_id' . $i] != "")
            {
                $account = explode("~", $postdata['account_id' . $i]);
                $tblValues = array(
                    'auto_journal_id' => $auto_journal_id,
                    'chart_account_code' => $account[1],
                    'debit' => $postdata['debit' . $i],
                    'credit' => $postdata['credit' . $i],
                    'description' => $postdata['description' . $i],
                );
                $this->db->insert('finance_auto_journal', $tblValues);
            }
            $i++;
        }

        $data['resfunction'] = 'search_auto_journal';
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function journal_unpost($postdata)
    {
        $data = array();
        $fisc_year_id = $this->getfiscalyearid();
        $default_currency = $this->getcurrency();
        $data['resfunction'] = 'search_auto_journal';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {
            $masterid = $this->mastermodel->insertmasterdata();
            $date = $this->mastermodel->convertdateformat($postdata['journal_voucher_date']);
            $year = date("Y");

            $transaction_id = $postdata['transaction_id'];
            $payment_type = 0;
            $reference = $this->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $date,
                'description' => '',
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => '0',
                'transaction_id' => '3',
                'posted' => '0'
            );

            $this->db->insert('finance_journal_refs', $tblValues);
            $i = 1;
            while (isset($postdata['code' . $i]))
            {
                if ($postdata['code' . $i] != "")
                {
                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $postdata['code' . $i],
                        'debit' => $postdata['debit' . $i],
                        'credit' => $postdata['credit' . $i],
                        'description' => $postdata['description' . $i],
                        'fiscal_year_id' => $fisc_year_id,
                        'system_date' => date('Y-m-d')
                    );
                    $this->db->insert('finance_journal', $tblValues);
                }
                $i++;
            }

            $data['resfunction'] = 'search_auto_journal';
            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function search_trans($type, $ref, $rtype, $from, $to)
    {


        if ($rtype != 'all')
        {
            $where = "fjr.transaction_id=$rtype and";
        }
        else
        {
            $where = '';
        }
        $from = $this->mastermodel->convdatformat($from);
        $to = $this->mastermodel->convdatformat($to);
        //echo "select sum(gt.amount) as amount , gt.type_no as id,tt.transaction,r.type, r.reference as ref,gt.tran_date from gl_trans gt JOIN refs r ON r.id=gt.type_no  JOIN transaction_types tt ON tt.id=r.type where $where r.reference like '$ref%' and gt.tran_date between '$from' and '$to' and r.type!=6 and gt.amount>0 group by gt.type_no    ";

        if ($type == 'data')
        {
            $data = array();

            $res = $this->db->query("select *
                   from finance_journal_refs fjr JOIN finance_transaction_code ftc ON fjr.transaction_id=ftc.transaction_id    
                   JOIN master ON master_id=journal_id
                   where  $where fjr.reference like '$ref%' and fjr.journal_date between '$from' and '$to'   order by fjr.journal_id  desc");

            foreach ($res->result_array() as $row)
            {
                $data[] = $row;
            }
            return $data;
        }
        else if ($type == 'count')
        {
            $res = $this->db->query("select *
                   from finance_journal_refs fjr JOIN finance_transaction_code ftc ON fjr.transaction_id=ftc.transaction_id    
                   JOIN master ON master_id=journal_id
                   where  $where fjr.reference like '$ref%' and fjr.journal_date between '$from' and '$to'   order by fjr.journal_id  desc");

            return $res->num_rows();
        }
    }

    function search_student_payment($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('finance_journal', 'finance_journal.journal_id=finance_journal_refs.journal_id');
        $this->db->join('student', 'student.reg_no=finance_journal.chart_account_code');
        $this->db->where('finance_journal.debit', 0);
        $this->db->where('transaction_id', '2');
        $this->db->where('posted', '0');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_student_payment_posted($pagenum)
    {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('finance_journal', 'finance_journal.journal_id=finance_journal_refs.journal_id');
        $this->db->join('student', 'student.reg_no=finance_journal.chart_account_code');
        $this->db->where('finance_journal.debit', 0);
        $this->db->where('transaction_id', '2');
        $this->db->where('posted', '1');
        $this->db->order_by('finance_journal_refs.journal_id', 'desc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function main_account_code_increment($start, $max)
    {
        $data = "SELECT IFNULL(max(chart_account_code),0) as code FROM 
        finance_chart_master where chart_account_code between " . $start . " and  " . $max;
        $res = $this->db->query($data);
        return $res->row()->code;
    }

    function account_code_generated($start, $max)
    {
        $min = $start - 1;
        $data = "SELECT CAST((@mid:= @mid) as CHAR) as code FROM `finance_chart_master` , (SELECT @mid:=$min  ,@midmax:=$max  ) t   
            where @mid < @midmax  and `chart_account_code` between $start and $max and 
            CAST(@mid:= @mid+1  as CHAR) <> `chart_account_code`  order by `chart_account_code` ";

        $res = $this->db->query($data);
        $r = $res->result_array();
        if ($r)
        {
            $code = $r[0]['code'];
        }
        else
        {
            $data = "SELECT IFNULL(max(chart_account_code),0) as code FROM 
            finance_chart_master where chart_account_code between " . $start . " and  " . $max;
            $res = $this->db->query($data);
            $code = $res->row()->code;
            if ($code)
                $code = $code + 1;
            else
                $code = $start;
        }
        return $code;
    }

    function checkaccountisset()
    {

        $this->db->where('closed', 0);
        $this->db->where('default', 1);
        $q = $this->db->get('finance_fiscal_year');
        $res = $q->row();

        if (!isset($res->fiscal_year_id))
            $baccount = 0;
        else
        {
            $this->db->where('default_currency', 1);
            $q = $this->db->get('finance_currency');
            $res = $q->row();
            if (!isset($res->currency_id))
                $baccount = -1;
            else
                $baccount = $res->currency_id;
        }

        return $baccount;
    }

    function get_account_settings($field, $table)
    {
        $query = $this->db->query("select $field from $table ");
        $res = $query->row()->$field;
        return $res;
    }

    function getfiscalyearid()
    {
        $fisc_year_id = 0;
        $fisc_year_id = $this->mastermodel->get_single_field_value_two('finance_fiscal_year', 'fiscal_year_id', 'finance_fiscal_year.closed', '0', 'finance_fiscal_year.default', '1');
        return $fisc_year_id;
    }

    function getcurrency()
    {
        $currencyid = 0;
        $currencyid = $this->mastermodel->get_single_field_value('finance_currency', 'currency_id', 'default_currency', '1');
        return $currencyid;
    }

    function get_reference($transaction_code)
    {

        $length = strlen($transaction_code) + 1;
        $data = "SELECT MAX(CONVERT(SUBSTRING(reference," . $length . "),UNSIGNED INTEGER) ) as code FROM 
        finance_journal_refs where reference like '" . $transaction_code . "%' ";

        $res = $this->db->query($data);
        if ($res->num_rows() > 0)
        {
            $code = $res->row()->code;
            $ref = $code + 1;
        }
        else
        {
            $ref = 1;
        }

        return $transaction_code . $ref;
    }

    function get_transaction_code($transaction_id, $payment_type_id = 0)
    {
        $transaction_code = $this->gettransactioncode($transaction_id) . '/';
        if ($payment_type_id != 0)
        {
            $payment = $this->mastermodel->get_single_field_value('finance_payment_types', 'payment_type', 'payment_type_id', $payment_type_id);
            if ($payment_type_id == 1)
                $payment_type = "CASH";
            else
                $payment_type = "BANK";

            $transaction_code.=$payment_type . '/';
        }
        $year = date("Y");
        $transaction_code.= $year . '/';
        $reference = $this->get_reference($transaction_code);
        return $reference;
    }

    function gettransactioncode($tid)
    {
        return $this->mastermodel->get_single_field_value('finance_transaction_code', 'transaction_code', 'transaction_id', $tid);
    }

    function post_data($id)
    {
        $tblvalues = array(
            'posted' => '1'
        );
        $this->db->where('journal_id', $id);
        $this->db->update('finance_journal_refs', $tblvalues);
    }

    function get_count_cheque_no($cheque_no, $not_in = "")
    {
        $this->db->from('finance_cheque_info');
        $this->db->join('finance_journal_refs', 'finance_journal_refs.journal_id=finance_cheque_info.journal_id');
        $this->db->where('transaction_id', '1');
        $this->db->where("cheque_no", $cheque_no);
        if ($not_in != "")
        {
            $this->db->where('cheque_no !=', $not_in);
        }

        $count = $this->db->count_all_results();
        return $count;
    }

    function insert_student_payment($postdata)
    {
        $data = array();
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        $student_acc = $this->mastermodel->get_data_srow('finance_chart_master', $postdata['account_code'], 'chart_account_code');
        $c_account = $student_acc->chart_master_id . '~' . $student_acc->chart_account_code . '~' . $student_acc->chart_account_name;
        $data['resfunction'] = 'search_student_payment';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {

            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment to " . $account_to_code . "-" . $account_to_name;

            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];
            $reference = $this->accountingmodel->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $masterid,
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $postdata['description'],
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'transaction_id' => $postdata['transaction_id'],
            );
            $this->db->insert('finance_journal_refs', $tblValues);

            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $masterid,
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }
            $total_amount = 0;
            if ($c_account != "")
            {

                $chart_account = explode("~", $c_account);
                $chart_account_id = $chart_account[0];
                $chart_account_code = $chart_account[1];
                $chart_account_name = $chart_account[2];
                $total_amount = $postdata['amount'];


                if (empty($postdata['description']))
                    $description = "Payment made to " . $account_to_name . "-" . $account_to_code;
                else
                    $description = $postdata['description'];

                $tblValues = array(
                    'journal_id' => $masterid,
                    'chart_account_code' => $chart_account_code,
                    'debit' => 0,
                    'credit' => $total_amount,
                    'description' => $postdata['description'],
                    'fiscal_year_id' => $fisc_year_id,
                );
                $this->db->insert('finance_journal', $tblValues);
            }

            $description = "Payment Received From " . $chart_account_name . "-" . $chart_account_code;
            $tblValues = array(
                'journal_id' => $masterid,
                'chart_account_code' => $account_to_code,
                'debit' => $total_amount,
                'credit' => 0,
                'description' => $postdata['description'],
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);

//            $tblValues = array(
//                'journal_id' => $masterid,
//                'param_id' => $postdata['fee_id'],
//                'refs_type' => 'Student Fee Payment',
//                'param_table' => 'fee'
//            );
//            $this->db->insert('finance_refs', $tblValues);
//
//            $tblValues = array(
//                'journal_id' => $masterid
//            );
//            $this->db->where('fee_id', $postdata['fee_id']);
//            $this->db->update('fee', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Added Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function update_student_payment($postdata)
    {
        $data = array();
        $fisc_year_id = $this->accountingmodel->getfiscalyearid();
        $default_currency = $this->accountingmodel->getcurrency();
        $student_acc = $this->mastermodel->get_data_srow('finance_chart_master', $postdata['account_code'], 'chart_account_code');

        $c_account = $student_acc->chart_master_id . '~' . $student_acc->chart_account_code . '~' . $student_acc->chart_account_name;

        $data['resfunction'] = 'search_student_payment';
        if ($fisc_year_id != 0 && $default_currency != 0)
        {

            $this->db->trans_begin();
            $masterid = $this->mastermodel->insertmasterdata();
            $account = explode("~", $postdata['bankacc_id']);
            $account_to_id = $account[0];
            $account_to_code = $account[1];
            $account_to_name = $account[2];
            $description = "Payment to " . $account_to_code . "-" . $account_to_name;
            $journal_id = $postdata['journal_id'];
            $transaction_id = $postdata['transaction_id'];
            $payment_type = $postdata['payment_type_id'];
            $reference = $this->accountingmodel->get_transaction_code($transaction_id, $payment_type);

            $tblValues = array(
                'journal_id' => $postdata['journal_id'],
                'journal_date' => $this->mastermodel->convertdateformat($postdata['journal_date']),
                'description' => $postdata['description'],
                'reference' => $reference,
                'currency_id' => $default_currency,
                'payment_type_id' => $postdata['payment_type_id'],
                'transaction_id' => $postdata['transaction_id'],
            );
            $this->db->where('journal_id', $postdata['journal_id']);
            $this->db->update('finance_journal_refs', $tblValues);

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_cheque_info');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_wire_info');

            $this->db->where('journal_id', $journal_id);
            $this->db->delete('finance_journal');

            if ($postdata['payment_type_id'] == 2)
            {
                $tblValues = array(
                    'journal_id' => $postdata['journal_id'],
                    'cheque_date' => $this->mastermodel->convertdateformat($postdata['cheque_date']),
                    'cheque_no ' => $postdata['cheque_no'],
                    'chart_account_code' => $account_to_code,
                    'cheque_info' => $postdata['cheque_info']
                );
                $this->db->insert('finance_cheque_info', $tblValues);
            }
            else if ($postdata['payment_type_id'] == 3)
            {
                $tblValues = array(
                    'journal_id' => $postdata['journal_id'],
                    'wire_info' => $postdata['wire_info'],
                );
                $this->db->insert('finance_wire_info', $tblValues);
            }
            $total_amount = 0;
            if ($c_account != "")
            {


                $chart_account = explode("~", $c_account);
                $chart_account_id = $chart_account[0];
                $chart_account_code = $chart_account[1];
                $chart_account_name = $chart_account[2];
                $total_amount = $postdata['amount'];


                if (empty($postdata['description']))
                    $description = "Payment made to " . $account_to_name . "-" . $account_to_code;
                else
                    $description = $postdata['description'];

                $tblValues = array(
                    'journal_id' => $postdata['journal_id'],
                    'chart_account_code' => $chart_account_code,
                    'debit' => 0,
                    'credit' => $total_amount,
                    'description' => $postdata['description'],
                    'fiscal_year_id' => $fisc_year_id,
                );

                $this->db->insert('finance_journal', $tblValues);
            }

            $description = "Payment Received From " . $chart_account_name . "-" . $chart_account_code;
            $tblValues = array(
                'journal_id' => $postdata['journal_id'],
                'chart_account_code' => $account_to_code,
                'debit' => $total_amount,
                'credit' => 0,
                'description' => $postdata['description'],
                'fiscal_year_id' => $fisc_year_id
            );
            $this->db->insert('finance_journal', $tblValues);


//            $tblValues = array(
//                'journal_id' => $masterid,
//                'param_id' => $postdata['fee_id'],
//                'refs_type' => 'Student Fee Payment',
//                'param_table' => 'fee'
//            );
//            $this->db->insert('finance_refs', $tblValues);
//
//            $tblValues = array(
//                'journal_id' => $masterid
//            );
//            $this->db->where('fee_id', $postdata['fee_id']);
//            $this->db->update('fee', $tblValues);


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $data['res'] = 0;
                $data['msg'] = 'Error On Adding Record';
                return $data;
            }
            else
            {
                $this->db->trans_commit();
                $data['res'] = 1;
                $data['msg'] = 'Record Updated Successfully';
                return $data;
            }
        }
        else
        {
            $data['res'] = 0;
            if ($fisc_year_id == 0)
                $data['msg'] = 'Fiscal Year Not Set.';
            else if ($default_currency == 0)
                $data['msg'] = 'Default Currency Not Set.';
            else
                $data['msg'] = 'Fiscal Year and Default Currency Not Set.';
            return $data;
        }
    }

    function search_reconcile($account, $statement, $cheque)
    {
        $data = array();
        $data['result'] = array();
        $data['cheque_info'] = array();

        if ($statement != 'new')
        {
            $where = "fj.reconcile_date= '$statement' and";
        }
        else
        {
            $where = 'fj.reconcile_date IS NULL and';
        }


        $res = $this->db->query("select *
                    from finance_journal fj
                    join finance_journal_refs on finance_journal_refs.journal_id=fj.journal_id
                    where $where chart_account_code = '$account'  order by fj.id  desc");

        $data['count'] = $res->num_rows();
        foreach ($res->result_array() as $row)
        {
            if ($cheque == 0)
            {
                $data['result'][] = $row;
            }
            if ($row['payment_type_id'] == 2)
            {
                if (!isset($data['cheque_info'][$row['journal_id']]))
                    $data['cheque_info'][$row['journal_id']] = $this->mastermodel->get_data_srow('finance_cheque_info', $row['journal_id'], 'journal_id');
                if ($cheque == 1 && count($data['cheque_info'][$row['journal_id']]) > 0)
                {
                    $data['result'][] = $row;
                }
            }
        }

        return $data;
    }

    function set_reconsile($postdata)
    {
        $journal = array(0);
        $data = array();
        $journal = array();
        $this->db->trans_begin();
        if (isset($postdata['select_for_reconcile']))
            $journal = $postdata['select_for_reconcile'];

        $rec_date = $this->mastermodel->convertdateformat($postdata['rec_date']);
        $all_journal = explode(",", $postdata['all_journal']);

        foreach ($all_journal as $key => $value)
        {
            if ($value != 0)
            {
                if (in_array($value, $journal))
                {
                    $tblValues = array(
                        'reconcile_date' => $rec_date,
                    );
                }
                else
                {
                    $tblValues = array(
                        'reconcile_date' => null,
                    );
                }
                $this->db->where('id', (int) $value);
                $this->db->update('finance_journal', $tblValues);
            }
        }

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            $data['res'] = 0;
            $data['msg'] = 'Error On Updating Record';
            return $data;
        }
        else
        {
            $this->db->trans_commit();
            $data['res'] = 1;
            $data['msg'] = 'Record Updated Successfully';
            return $data;
        }
    }

    function get_bank_reconciliation($postdata)
    {
        $data = array();
        $from = $this->mastermodel->convdatformat($postdata['rec_from']);
        $to = $this->mastermodel->convdatformat($postdata['rec_to']);
        $account = $postdata['account_id'];
        $where = '';
        if ($account != '')
        {
            $account_details = explode("~", $postdata['account_id']);
            $where = "fj.chart_account_code like '$account_details[1]%' and ";
        }

        $data = array();
        $res = $this->db->query("select *
               from finance_journal fj
               join finance_journal_refs on finance_journal_refs.journal_id=fj.journal_id
               where $where   fj.reconcile_date between '$from' and '$to' and reconcile_date is not null  ");

        $data['cheque_info'] = array();
        foreach ($res->result_array() as $row)
        {
            $data['result'][] = $row;
            if ($row['payment_type_id'] == 2)
            {

                if (!isset($data['cheque_info'][$row['journal_id']]))
                    $data['cheque_info'][$row['journal_id']] = $this->mastermodel->get_data_srow('finance_cheque_info', $row['journal_id'], 'journal_id');
            }
        }
        return $data;
    }

    function trail_balance_report($postdata)
    {
        $data = array();
        $data['result'] = array();
        $to1 = explode('-', $postdata['trans_date']);
        $days = $this->monthdays($to1[1], $to1[2]);
        $to = $days . '-' . $to1[1] . '-' . $to1[2];
        $from = '01-' . $to1[1] . '-' . $to1[2];

        $date1 = $this->mastermodel->convdatformat($from);
        $date2 = $this->mastermodel->convdatformat($to);
        $data['date1'] = $from;
        $data['date2'] = $to;

        $qry = "SELECT sum(if(fj.credit > 0,fj.credit,if(fjm.credit > 0 ,fjm.credit,0)))as credit,
                sum(if(fj.debit > 0,fj.debit,if(fjm.debit > 0 ,fjm.debit,0)))as debit,
                sum(if(fs.journal_date < '$date1',if(fj.credit > 0,fj.credit,0),if(fsm.journal_date< '$date1',if(fjm.credit > 0 ,fjm.credit,0),0))) as opencredit,
                sum(if(fs.journal_date < '$date1',if(fj.debit > 0,fj.debit,0),if(fsm.journal_date< '$date1',if(fjm.debit > 0,fjm.debit,0),0))) as opendebit,
        
                cm.chart_master_id as main_id,cm.chart_account_name as account_name ,cm.chart_account_code as account
                FROM finance_chart_master cm
                left join finance_chart_master cp on cp.parent=cm.chart_master_id 
                left join finance_journal fj on fj.chart_account_code=cp.chart_account_code
                left join finance_journal_refs fs on fs.journal_id=fj.journal_id
                left join finance_journal fjm on fjm.chart_account_code=cm.chart_account_code
                left join finance_journal_refs fsm on fsm.journal_id=fjm.journal_id
                join finance_chart_type ct on ct.chart_type_id=cm.chart_type_id
                where cm.parent=0 
                and (fs.journal_date between '$date1' and '$date2' or fsm.journal_date between '$date1' and '$date2')
                group by main_id order by ct.chart_type_id,cm.chart_account_code asc";

//        $qry1 = "SELECT sum(if(gt.amount<0,gt.amount,if(glt.amount<0,glt.amount,0)))as credit,
//                sum(if(gt.amount>0,gt.amount,if(glt.amount>0,glt.amount,0)))as debit,
//                sum(if(gt.tran_date< '$date1',if(gt.amount<0,gt.amount,0),if(glt.tran_date< '$date1',if(glt.amount<0,glt.amount,0),0))) as opencredit,
//                sum(if(gt.tran_date< '$date1',if(gt.amount>0,gt.amount,0),if(glt.tran_date< '$date1',if(glt.amount>0,glt.amount,0),0))) as opendebit,
//                cm.account_name ,cm.account_code as account,cm.id as cgid 
//                FROM chart_master cm 
//                left join chart_master cp on cp.parent=cm.id
//                left join gl_trans gt ON gt.account=cp.account_code
//                left join gl_trans glt ON glt.account=cm.account_code
//                join char_type ct on ct.id=cm.chart_type
//                where cm.parent=0 and (gt.tran_date between '$date1' and '$date2' 
//        or glt.tran_date between '$date1' and '$date2') group by cgid order by ct.id,cm.account_code asc";

        $res = $this->db->query($qry);

        if ($res->num_rows() > 0)
        {
            foreach ($res->result_array() as $row)
            {
                $data['result'][] = $row;
            }
        }
        $res->free_result();
        return $data;
    }

    function sub_acc_trail_balance_report($postdata)
    {
        $data = array();
        $data['result'] = array();
        $to1 = explode('-', $postdata['trans_date']);
        $days = $this->monthdays($to1[1], $to1[2]);
        $to = $days . '-' . $to1[1] . '-' . $to1[2];
        $from = '01-' . $to1[1] . '-' . $to1[2];

        $date1 = $this->mastermodel->convdatformat($from);
        $date2 = $this->mastermodel->convdatformat($to);


        $acc = explode('~', $postdata['main_account_id']);
        $mainacc = $acc[0];
        $rangefrom = "";
        $rangeto = "";
        if (isset($postdata['rangefrom']))
        {
            $rangefrom = $postdata['rangefrom'];
        }
        if (isset($postdata['rangeto']))
        {
            $rangeto = $postdata['rangeto'];
        }
        $cust = "";
        $data['rfrom'] = $rangefrom;
        $data['rto'] = $rangeto;
        $data['date1'] = $from;
        $data['date2'] = $to;

        if (!empty($mainacc) && !empty($rangefrom) && !empty($rangeto))
        {
            $cust = "where cm.parent='$mainacc' and cm.chart_account_code between '$rangefrom' and '$rangeto'";
        }
        if (!empty($mainacc) && empty($rangefrom) && empty($rangeto))
        {
            $cust = "where cm.parent='$mainacc'";
        }

        if (!empty($rangefrom) && !empty($rangeto))
        {
            $cust = "where cm.chart_account_code between '$rangefrom' and '$rangeto'";
        }

        $qry = "SELECT 
                sum(if(fjr.journal_date>='$date1' and fjr.journal_date <='$date2',if(fj.credit>0,fj.credit,0),0))as credit,
                sum(if(fjr.journal_date>='$date1' and fjr.journal_date <='$date2',if(fj.debit>0,fj.debit,0),0))as debit,
                sum(if(fjr.journal_date< '$date1',if(fj.credit>0,fj.credit,0),0)) as opencredit,
                sum(if(fjr.journal_date< '$date1',if(fj.debit>0,fj.debit,0),0)) as opendebit,
                cm.chart_account_code as account,cm.chart_account_name as account_name
                FROM finance_chart_master cm 
                left join finance_journal fj ON fj.chart_account_code=cm.chart_account_code 
                left join finance_journal_refs fjr ON fjr.journal_id=fj.journal_id
                $cust group by cm.chart_account_code";
        //echo $qry;

        $res = $this->db->query($qry);

        if ($res->num_rows() > 0)
        {
            foreach ($res->result_array() as $row)
            {
                $data['result'][] = $row;
            }
        }
        $res->free_result();
        return $data;
    }

    function general_ledger($postdata)
    {
        $data = array();
        $data['result'] = array();
        $fromdate = $postdata['start_date'];
        $todate = $postdata['end_date'];
        $date1 = $this->mastermodel->convdatformat($fromdate);
        $date2 = $this->mastermodel->convdatformat($todate);
        $rangefrom = "";
        $rangeto = "";
        if (isset($postdata['rangefrom']))
        {
            $rangefrom = $postdata['rangefrom'];
        }
        if (isset($postdata['rangeto']))
        {
            $rangeto = $postdata['rangeto'];
        }
        $cust = "";
        if (!empty($rangefrom) && !empty($rangeto))
        {
            $cust = " AND cm.chart_account_code between '$rangefrom' and '$rangeto'";
        }
        $data['rfrom'] = $rangefrom;
        $data['rto'] = $rangeto;
        $data['date1'] = $fromdate;
        $data['date2'] = $todate;
        $data['acctitle'] = 'A/c Total';

        $qry = "SELECT if(r1.journal_date>='$date1' and r1.journal_date <='$date2',if(fj.credit>0,sum(fj.credit),0),if(r2.journal_date>='$date1' and r2.journal_date <='$date2',if(fjm.credit>0,sum(fjm.credit),0),0))as credit,
                if(r1.journal_date>='$date1' and r1.journal_date <='$date2',if(fj.debit>0,sum(fj.debit),0),if(r2.journal_date>='$date1' and r2.journal_date <='$date2',if(fjm.debit>0,sum(fjm.debit),0),0))as debit,
                ifnull(r1.journal_date,r2.journal_date) as tran_date,
                ifnull(fj.description,fjm.description) as memo,
                ifnull(r1.reference,r2.reference) as reference,
                cm.chart_account_name as account_name ,cm.chart_account_code as account,cm.chart_master_id as cgid
                FROM finance_chart_master cm
                left join finance_chart_master cp on cp.parent=cm.chart_master_id                
                left join finance_journal fj on fj.chart_account_code=cp.chart_account_code                
                left join finance_journal fjm on fjm.chart_account_code=cm.chart_account_code
                left join finance_journal_refs r1 on r1.journal_id=fj.journal_id
                left join finance_journal_refs r2 on r2.journal_id=fjm.journal_id                                                      
                join finance_chart_type ct on ct.chart_type_id=cm.chart_type_id
                where cm.parent=0 
                $cust
                group by reference 
                order by ct.chart_type_id,cm.chart_account_code,tran_date,
                LEFT(r1.reference,2),LEFT(r2.reference,2),RIGHT(r1.reference ,2),RIGHT(r2.reference,2),reference asc";

//echo $qry;
        $res = $this->db->query($qry);

        if ($res->num_rows() > 0)
        {
            foreach ($res->result_array() as $row)
            {
                $data['result'][] = $row;
            }
        }
        $res->free_result();
        return $data;
    }

    function opening_maintrail_balance($code, $date1, $date2)
    {

        $data = array();
        $date1 = $this->mastermodel->convdatformat($date1);

        $qry = "select sum(if(r1.journal_date< '$date1',if(fj.credit>0,fj.credit,0),if(r2.journal_date< '$date1',if(fjm.credit>0,fjm.credit,0),0))) as opencredit,
                sum(if(r1.journal_date< '$date1',if(fj.debit>0,fj.debit,0),if(r2.journal_date< '$date1',if(fjm.debit>0,fjm.debit,0),0))) as opendebit
                FROM finance_chart_master cm
                left join finance_chart_master cp on cp.parent=cm.chart_master_d
                left join finance_journal fj on fj.chart_account_code=cp.chart_account_code                
                left join finance_journal fjm on fjm.chart_account_code=cm.chart_account_code
                left join finance_journal_refs r1 on r1.journal_id=fj.journal_id
                left join finance_journal_refs r2 on r2.journal_id=fjm.journal_id    
                 where  cm.chart_account_code='$code'";



        //  $qry="SELECT IFNULL(SUM(if(gt.amount<0,gt.amount,0)),0) as opencredit, IFNULL(SUM(if(gt.amount>0,gt.amount,0)),0) as opendebit FROM gl_trans gt  where  gt.account='$code' and  gt.tran_date < '$date1' ";
        //echo $qry;
        $res = $this->db->query($qry);
        $data = $res->row();
        return $data;
    }

    function opening_trail_balance($code, $date1, $date2)
    {

        $data = array();
        $date1 = $this->mastermodel->convdatformat($date1);
        $qry = "SELECT IFNULL(SUM(if(fj.credit>0,fj.credit,0)),0) as opencredit, 
        IFNULL(SUM(if(fj.debit>0,fj.debit,0)),0) as opendebit
        FROM finance_journal fj  
        left join finance_journal_refs r1 on r1.journal_id=fj.journal_id
        where  fj.chart_account_code='$code' and  r1.journal_date < '$date1' ";

        $res = $this->db->query($qry);
        $data = $res->row();
        return $data;
    }

    function sub_general_ledger($postdata)
    {
        $data = array();
        $data['result'] = array();
        $from = $postdata['start_date'];
        $to = $postdata['end_date'];


        $date1 = $this->mastermodel->convdatformat($from);
        $date2 = $this->mastermodel->convdatformat($to);


        $acc = explode('~', $postdata['main_account_id']);
        $mainacc = $acc[0];
        $rangefrom = "";
        $rangeto = "";
        if (isset($postdata['rangefrom']))
        {
            $rangefrom = $postdata['rangefrom'];
        }
        if (isset($postdata['rangeto']))
        {
            $rangeto = $postdata['rangeto'];
        }
        $cust = "";
        $data['rfrom'] = $rangefrom;
        $data['rto'] = $rangeto;
        $data['date1'] = $from;
        $data['date2'] = $to;
        $data['acctitle'] = 'Sub A/c Total';
        $data['heading'] = "Sub Account Ledger";
//$mainacc=0;
        if (!empty($mainacc) && !empty($rangefrom) && !empty($rangeto))
        {
            $cust = "where cm.parent=$mainacc and cm.chart_account_code between '$rangefrom' and '$rangeto'";
        }
        if (!empty($mainacc) && empty($rangefrom) && empty($rangeto))
        {
            $cust = "where cm.parent=$mainacc";
        }

        if (!empty($rangefrom) && !empty($rangeto))
        {
            $cust = "where cm.chart_account_code between '$rangefrom' and '$rangeto'";
        }
//echo $mainacc;
//        $cust = "where cm.parent=0";
        $qry = "SELECT if(r1.journal_date>='$date1' and r1.journal_date <='$date2',if(fj.credit>0,fj.credit,0),if(r2.journal_date>='$date1' and r2.journal_date <='$date2',if(fjm.credit>0,fjm.credit,0),0))as credit,
                if(r1.journal_date>='$date1' and r1.journal_date <='$date2',if(fj.debit>0,fj.debit,0),if(r2.journal_date>='$date1' and r2.journal_date <='$date2',if(fjm.debit>0,fjm.debit,0),0))as debit,
                ifnull(r1.journal_date,r2.journal_date) as tran_date,
                ifnull(fj.description,fjm.description) as memo,
                ifnull(r1.reference,r2.reference) as reference,
                cm.chart_account_name as account_name ,cm.chart_account_code as account,cm.chart_master_id as cgid 
                FROM finance_chart_master cm
                left join finance_chart_master cp on cp.parent=cm.chart_master_id
                left join finance_journal fj on fj.chart_account_code=cp.chart_account_code                
                left join finance_journal fjm on fjm.chart_account_code=cm.chart_account_code
                left join finance_journal_refs r1 on r1.journal_id=fj.journal_id
                left join finance_journal_refs r2 on r2.journal_id=fjm.journal_id                                                      
                join finance_chart_type ct on ct.chart_type_id=cm.chart_type_id                        
                $cust  
                having reference!=''   
                order by ct.chart_type_id,cm.chart_account_code,tran_date,
                LEFT(r1.reference,2),LEFT(r2.reference,2),RIGHT(r1.reference ,2),RIGHT(r2.reference,2),reference asc";

//        echo $qry;
        $res = $this->db->query($qry);
        $data['transdet'] = $res->result_array();

        return $data;
    }

    function get_balance_sheet($postdata)
    {
        $data = array();
        $data['result'] = array();
        $from = $postdata['start_date'];
        $to = $postdata['end_date'];
        $date1 = $this->mastermodel->convdatformat($from);
        $date2 = $this->mastermodel->convdatformat($to);
        $data['date1'] = $from;
        $data['date2'] = $to;

        $this->db->select('*,SUM(if(debit>0 and credit=0,debit,0)) as account_amount_debit,
            SUM(if(credit>0 and debit=0,credit,0)) as account_amount_credit,finance_journal_refs.journal_id as type,chart_type_name as chart_type,
            finance_journal.chart_account_code as account, finance_chart_master.chart_account_name as  account_name, finance_chart_master.chart_type_id as chart_id', false);
        $this->db->from('finance_journal_refs');
        $this->db->join('finance_journal', 'finance_journal.journal_id=finance_journal_refs.journal_id');
        $this->db->join('finance_chart_master', 'finance_chart_master.chart_account_code=finance_journal.chart_account_code');
        $this->db->join('finance_chart_type', 'finance_chart_type.chart_type_id=finance_chart_master.chart_type_id');

        if ($from != 0)
            $this->db->where('finance_journal_refs.journal_date >=', $from);
        if ($to != 0)
            $this->db->where('finance_journal_refs.journal_date <=', $to);

        $this->db->group_by('finance_journal.chart_account_code');
        $this->db->order_by('finance_journal.chart_account_code');
        $Q = $this->db->get();
        if ($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data['result'][$row['account']] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

    function statement_of_account($postdata)
    {

//$value, $type, $cid, 
        $data = array();
        $data['result'] = array();
        $from = $postdata['start_date'];
        $to = $postdata['end_date'];


        $date1 = $this->mastermodel->convdatformat($from);
        $date2 = $this->mastermodel->convdatformat($to);
        $data['date1'] = $from;
        $data['date2'] = $to;

        $acc = explode('~', $postdata['account_id']);
        $acc_id = $acc[0];
        $acc_code = $acc[1];
        $data['acc_code'] = $acc_code;
        $data['acc_name'] = $acc[2];

        $where = "";
        if ($date2 != 0)
        {
            $where = " and fjr.journal_date <= '$date2' ";
        }
        $qry = "               
            SELECT 
            SUM(if(fjr.journal_date>='$date1' and fjr.journal_date <='$date2',if(fj.credit>0,fj.credit,0),0)) as credit,
            SUM(if(fjr.journal_date>='$date1' and fjr.journal_date <='$date2',if(fj.debit>0,fj.debit,0),0)) as debit,
            if(fjr.journal_date< '$date1',if(fj.credit>0,fj.credit,0),0) as opencredit,
            if(fjr.journal_date< '$date1',if(fj.debit>0,fj.debit,0),0) as opendebit,
            fjr.journal_id as journal_id,
            fjr.journal_date as journal_date,reference ,fj.description as description           
            FROM finance_journal fj
            INNER JOIN finance_journal_refs fjr ON fjr.journal_id=fj.journal_id
            INNER join finance_chart_master cm ON cm.chart_account_code=fj.chart_account_code
            where  cm.chart_master_id='$acc_id' $where group by fjr.journal_id order by fjr.journal_date asc ";
        $result = $this->db->query($qry);
        $data['open_credit'] = 0;
        $data['open_debit'] = 0;

        foreach ($result->result_array() as $row)
        {
//            if ($row['journal_date'] < $date1) {
            $data['open_credit']+=$row['opencredit'];
            $data['open_debit']+=$row['opendebit'];
//            }
//            echo $row['opendebit'];
            if ($row['journal_date'] >= $date1 && $row['journal_date'] <= $date2)
            {
                $data['result'][] = $row;
            }
        }
        $data['payments'] = array();
        $sql = "select *, finance_payment_types.payment_type as method,finance_journal_refs.journal_id as refs_id,
            IFNULL(rc.cheque_date,'') as ch_date,IFNULL(rc.cheque_info,'') as cheque_info,IFNULL(rc.cheque_no,'') as ch_no,
            IFNULL(fw.wire_info,0) as wire_info,
            IF(finance_journal_refs.transaction_id=1,'PV',IF(finance_journal_refs.transaction_id=2,'RV','none')) as trans_type
            from finance_journal_refs
            join finance_journal  on finance_journal.journal_id=finance_journal_refs.journal_id
            left join finance_cheque_info rc on rc.journal_id=finance_journal_refs.journal_id
            left join finance_payment_types on finance_payment_types.payment_type_id=finance_journal_refs.payment_type_id
            left join finance_wire_info fw on fw.journal_id=finance_journal_refs.journal_id
            group by finance_journal_refs.journal_id order by finance_journal_refs.journal_id ";
        $result = $this->db->query($sql);
        foreach ($result->result_array() as $row)
        {
//            echo $row['refs_id'].'/'. $row['method'].'/'.$row['ch_date'].'/'. $row['cheque_info'].'/'. $row['ch_no'].'/'. $row['wire_info'].'<br/>';
            $data['payments'][$row['refs_id']] = $row;
        }
        return $data;
    }

    function pdc_report($postdata)
    {
//    function get_pdc_report($type, $cid, $cheque_date, $tr_type, $value = "") {
        $data = array();
        $data['result'] = array();

        $acc_id = 0;
        $acc_code = "";
        if (isset($postdata['account_id']) && $postdata['account_id'] != "")
        {
            $acc = explode('~', $postdata['account_id']);
            $acc_id = $acc[0];
            $acc_code = $acc[1];
        }
        $tr_type = $postdata['transaction'];
        $cheque_date = $postdata['cheque_date'];
        $date1 = $this->mastermodel->convdatformat($cheque_date);
        $data['tr_type'] = $tr_type;
        $data['date1'] = $date1;


        $data['c_id'] = array($acc_id);
        $where = "";
        if ($tr_type != "all")
        {
            $where.=" and posted='" . $tr_type . "'";
        }
        if ($acc_code != "")
        {
            $where.=" and fj.chart_account_code='" . $acc_code . "'";
        }
        if ($cheque_date != 0)
        {
            $date1 = $this->mastermodel->convdatformat($cheque_date);
            if ($date1 != "")
            {
                $where.=" and rc.cheque_date <='" . $date1 . "'";
            }
        }
        $res = $this->db->query(" select *, r.reference as tr_no,fj.journal_id as journal_id,
                cm.chart_master_id as acc_id,cm.chart_account_code as acc_code
                from finance_journal fj
                JOIN finance_journal_refs r ON r.journal_id=fj.journal_id
                JOIN finance_cheque_info rc ON rc.journal_id=r.journal_id
                JOIN finance_transaction_code ft ON ft.transaction_id=r.transaction_id
                JOIN finance_chart_master cm on cm.chart_account_code=fj.chart_account_code          
                where  r.tr_type='post_dated_cheque' $where order by r.journal_id desc");

        $rt = $res->result_array();
        $data['transdet'] = $rt;
        $data['refs_count'] = array();
        foreach ($rt as $row)
        {
            $data['result'][] = $row;
            if (isset($data['refs_count'][$row['journal_id']]))
            {
                $data['refs_count'][$row['journal_id']]+=1;
            }
            else
            {
                $data['refs_count'][$row['journal_id']] = 1;
            }
        }


//        $res = $this->db->query(" select *  , r.reference as tr_no
//                from unposted_pay_dep pd 
//                JOIN refs r ON r.id=pd.reference 
//                JOIN receipt_cheque rc ON rc.reference=pd.reference
//                JOIN transaction_types tt ON tt.id=r.type
//                JOIN customers on customers.code=pd.account
//                LEFT JOIN customer_personal_ind ON customer_personal_ind.customer_id=customers.id
//                where  r.type='2'  and r.tr_type='post_dated_cheque' ");
//        $data['transdet']=$res->result_array();
//        var_dump($data['transdet']);
        return $data;
    }

    function monthdays($someMonth, $someYear)
    {
        return date("t", strtotime($someYear . "-" . $someMonth . "-01"));
    }

}

?>

bypass 1.0, Devloped By El Moujahidin (the source has been moved and devloped)
Email: contact@elmoujehidin.net