? Fallagassrini

Fallagassrini Bypass Shell

echo"
Fallagassrini
";
Current Path : /home1/savoy/public_html/savoyglobal.net/rms/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/rms/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();
    }

    function get_bank_journal_data($id1, $id, $field, $isarray = FALSE) {
        $data = array();
        $this->db->where($field, $id);
        $this->db->where('journal_id', $id1);
        $Q = $this->db->get('finance_journal');
        if ($isarray)
            $row = $Q->row_array();
        else
            $row = $Q->row();
        return $row;
    }

    function set_fiscal_year_current($fiscal_year_id) {

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

        $sql = "UPDATE  finance_fiscal_year 
                LEFT JOIN  
                        master ON master_id=fiscal_year_id 
                SET default=0
                WHERE 
                        master.login_type_id =$log_in_type
                AND 
                        fiscal_year_id != $fiscal_year_id 
                ;";

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

    function set_currency_closed($currency_id) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $tblValues = array('default_currency' => "1");
        $this->db->where('currency_id', $currency_id);
        $this->db->update('finance_currency', $tblValues);

        $tblValues = array('default_currency' => "0");
        $this->db->join('master', 'master_id=' . $currency_id);
        $this->db->where('currency_id !=', $currency_id);
        $this->db->update('finance_currency', $tblValues);
    }

    function checkaccountisset() {
        $log_in_type = $_SESSION['login_type'];
        $this->db->join('master', 'master_id=fiscal_year_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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->join('master', 'master_id=currency_id');
            $this->db->where('master.login_type_id', $log_in_type);
            $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 main_account_code_increment($parent, $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 acc_code_maxi_limit($value) {

        $data = "SELECT chart_account_code FROM finance_chart_master where chart_account_code > $value and parent=0 order by chart_account_code ASC limit 0,1";
        $res = $this->db->query($data);
        if ($res->num_rows() > 0) {
            return $res->row()->chart_account_code;
        } else {
            return 99999999999999999999;
        }
    }

    function search_journal_rv($journal_id) {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal');
        $this->db->where('finance_journal.debit', 0);
        $this->db->where('journal_id', $journal_id);
        $data = $this->db->get();
        return $data;
    }

    function search_journal_pv($journal_id) {
        $data = array();
        $this->db->select('*');
        $this->db->from('finance_journal');
        $this->db->where('finance_journal.credit', 0);
        $this->db->where('journal_id', $journal_id);
        $data = $this->db->get();
        return $data;
    }

    /* function starts here */

    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();
        $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();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_fiscal_year');
        $this->db->join('master', 'master_id=fiscal_year_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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 search_transaction_code($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_transaction_code');
//        $this->db->join('finance_transaction_type','finance_transaction_type.transaction_type_id=finance_transaction_code.trans');
        $this->db->join('master', 'master_id=transaction_id ');
        $this->db->where('master.login_type_id', $log_in_type);
        $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_bank_account($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_bank_account');
        $this->db->join('master', 'master_id=bank_account_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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();
        $masterid = $this->mastermodel->insertmasterdata();

        $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($acc_parent, $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(
                'bank_account_id' => $masterid,
                '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['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_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;
        }
    }

    function search_main_account($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $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->join('master', 'master_id=chart_master_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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();
        $masterid = $this->mastermodel->insertmasterdata();
        $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('0', $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;
            }

            $tblValues = array(
                'chart_master_id' => $masterid,
                '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);
        } else {
            $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 search_sub_account($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $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->join('master', 'master_id=c1.chart_master_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('c1.parent !=', '0');
        $this->db->order_by('c2.chart_account_name', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_sub_ac_settings($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->join('master', 'master_id=sub_account_settings_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $q = $this->db->get('finance_sub_account_settings');
        $data['results'] = $q->row();
        return $data;
    }

    function search_cost_center($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('cost_center');
        $this->db->join('master', 'master_id=cost_master_id ');
        $this->db->where('master.login_type_id IN ( "' . $log_in_type . '" ,"0")');
        $this->db->order_by('cost_center_id', 'asc');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_debit_note($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_customer_payments($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
//        $this->db->join('master','master_id=journal_id');
//        $this->db->where('master.login_type_id',$log_in_type);
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_Credit_note($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
//        $this->db->join('master','master_id=journal_id');
//        $this->db->where('master.login_type_id',$log_in_type);
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_main_account_settings($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
//        $this->db->join('master','master_id=journal_id');
//        $this->db->where('master.login_type_id',$log_in_type);
        $data['results'] = $this->db->get();
        return $data;
    }

    function insert_sub_account($postdata) {
        $data = array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $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($acc_parent, $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_master_id' => $masterid,
                '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 insert_sub_ac_settings($postdata) {
        $data = array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $tblValues = array(
            'sub_account_settings_id' => $masterid,
            'customer' => $postdata['customer'],
            'employee' => $postdata['employee'],
            'profitloss' => $postdata['profitloss'],
            'salary' => $postdata['salary'],
            'vendor' => $postdata['vendor'],
            'maintenance_contract' => $postdata['maintenance_contract'],
        );

        $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 get_reference($transaction_code) {
        
        $length=strlen($transaction_code)+1;
        $data = "SELECT MAX(SUBSTRING(reference,".$length.") ) 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;
        
//        $data = "SELECT reference as code FROM 
//        finance_journal_refs where reference like '" . $transaction_code . "%' order by journal_id desc limit 1";
//        $res = $this->db->query($data);
//        if ($res->num_rows() > 0) {
//            $len = strlen($transaction_code);
//            $code = $res->row()->code;
//            $ref = substr($code, $len, strlen($code));
//            $ref = $ref + 1;
//        } else {
//            $ref = 1;
//        }
//        return $transaction_code . $ref;
    }

    /* functions for Receipt voucher */

    function search_receipt_voucher($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('transaction_id', '2');
        $this->db->where('posted', '0');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_receipt_voucher_posted($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->join('finance_journal', 'finance_journal.journal_id=finance_journal_refs.journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('finance_journal.debit', 0);
        $this->db->where('transaction_id', '2');
        $this->db->where('posted', '1');
        $this->db->order_by('finance_journal.id	', 'asc');
        $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_code = $this->gettransactioncode($postdata['transaction_id']);
//            $year = date("Y");
//            $payment = $this->mastermodel->get_single_field_value('finance_payment_types', 'payment_type', 'payment_type_id', $postdata['payment_type_id']);
//            if ($postdata['payment_type_id'] == 1)
//                $payment_type = "CASH";
//            else
//                $payment_type = "BANK";
//            $transaction_code = $transaction_code . '/' . $payment_type . '/' . $year . '/';
//            $reference = $this->get_reference($transaction_code);
            
            $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 (!empty($charttypes))
                    $charttypes.='/';

                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];
                    
                    
                    $param_table="";
                    $param_id = "";
                    $refs_type = "";
                    $refs_value = "";
                    if($postdata['param' . $i]!="")
                    {
                        $param = explode("~", $postdata['param' . $i]);                    
                        $param_table = $param[0];
                        $param_id = $param[1];
                        $refs_type = $param[2];
                        $refs_value = $param[3];
                    }
                    
                    if (empty($postdata['description' . $i]))
                        $description = "Payment made to " . $account_to_code . "-" . $account_to_name;
                    else
                        $description = $postdata['description' . $i];

                    if (!empty($refs_value) && !empty($refs_type))
                        $description.="/$refs_type/$refs_value";

                    $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);
                    $ref_id = mysql_insert_id();
                    if ($param_table != "") {


                        $tblValues = array(
                            'refs_id' => $ref_id,
                            'journal_id' => $masterid,
                            'param_id' => $param_id,
                            'refs_type' => $refs_type,
                            'param_table' => $param_table
                        );
                        $this->db->insert('finance_refs', $tblValues);
                    }
                    $charttypes.=$chart_account_name;
                }
                $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->getfiscalyearid();
        if ($fisc_year_id != 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
            );

            $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 (!empty($charttypes))
                    $charttypes.='/';
                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];

                    
                    $param_table="";
                    $param_id = "";
                    $refs_type = "";
                    $refs_value = "";
                    if($postdata['param' . $i]!="")
                    {
                        $param = explode("~", $postdata['param' . $i]);                    
                        $param_table = $param[0];
                        $param_id = $param[1];
                        $refs_type = $param[2];
                        $refs_value = $param[3];
                    }
                    
                    if (empty($postdata['description' . $i])) {
                        $description = "Payment made to " . $account_to_code . "-" . $account_to_name;
                        if (!empty($refs_value) && !empty($refs_type))
                            $description.="/$refs_type/$refs_value";
                    }
                    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);
                    $ref_id = mysql_insert_id();
                    if ($param_table != "") {

                        $tblValues = array(
                            'refs_id' => $ref_id,
                            'journal_id' => $journal_id,
                            'param_id' => $param_id,
                            'refs_type' => $refs_type,
                            'param_table' => $param_table
                        );
                        $this->db->insert('finance_refs', $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;

            $data['msg'] = 'Fiscal Year Not Set.';

            return $data;
        }
    }

    /* functions for payment voucher */

    function search_payment_voucher_posted($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('transaction_id', '1');
        $this->db->where('posted', '1');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_payment_voucher($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('transaction_id', '1');
        $this->db->where('posted', '0');
        $data['results'] = $this->db->get();
        return $data;
    }

    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 insert_payment_voucher($postdata) 
    {
        
        $fisc_year_id = $this->getfiscalyearid();
        $default_currency = $this->getcurrency();
        $data = array();
        $data['resfunction'] = 'search_payment_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 From " . $account_to_code . "-" . $account_to_name;

//            $transaction_code = $this->gettransactioncode($postdata['transaction_id']);
//            $year = date("Y");
//            $payment = $this->mastermodel->get_single_field_value('finance_payment_types', 'payment_type', 'payment_type_id', $postdata['payment_type_id']);
//            if ($postdata['payment_type_id'] == 1)
//                $payment_type = "CASH";
//            else
//                $payment_type = "BANK";
//            $transaction_code = $transaction_code . '/' . $payment_type . '/' . $year . '/';
//            $reference = $this->get_reference($transaction_code);
            
            
            $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 (!empty($charttypes))
                    $charttypes.='/';

                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];
                    
                    $param_table="";
                    $param_id = "";
                    $refs_type = "";
                    $refs_value = "";
                    if($postdata['param' . $i]!="")
                    {
                        $param = explode("~", $postdata['param' . $i]);                    
                        $param_table = $param[0];
                        $param_id = $param[1];
                        $refs_type = $param[2];
                        $refs_value = $param[3];
                    }
                    
                   
                    if (empty($postdata['description' . $i]))
                        $description = "Payment Received From " . $account_to_code . "-" . $account_to_name;
                    else
                        $description = $postdata['description' . $i];

                    if (!empty($refs_value) && !empty($refs_type))
                        $description.="/$refs_type/$refs_value";

                    $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);
                    $ref_id = mysql_insert_id();
                    if ($param_table != "") {


                        $tblValues = array(
                            'refs_id' => $ref_id,
                            'journal_id' => $masterid,
                            'param_id' => $param_id,
                            'refs_type' => $refs_type,
                            'param_table' =>$param_table
                        );
                        $this->db->insert('finance_refs', $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->getfiscalyearid();
        if ($fisc_year_id != 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,
                '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 (!empty($charttypes))
                    $charttypes.='/';
                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];

                    $param_table="";
                    $param_id = "";
                    $refs_type = "";
                    $refs_value = "";
                    if($postdata['param' . $i]!="")
                    {
                        $param = explode("~", $postdata['param' . $i]);                    
                        $param_table = $param[0];
                        $param_id = $param[1];
                        $refs_type = $param[2];
                        $refs_value = $param[3];
                    }
                    
                    if (empty($postdata['description' . $i])) {
                        $description = "Payment Received From " . $account_to_code . "-" . $account_to_name;
                        if (!empty($refs_value) && !empty($refs_type))
                            $description.="/$refs_type/$refs_value";
                    }
                    else
                        $description = $postdata['description' . $i];




                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $chart_account_code,
                        'credit' => 0,
                        'debit' => $postdata['amount' . $i],
                        'description' => $description,
                        'fiscal_year_id' => $fisc_year_id,
                    );
                    $this->db->insert('finance_journal', $tblValues);
                    $ref_id = mysql_insert_id();
                    if ($param_table != "") {


                        $tblValues = array(
                            'refs_id' => $ref_id,
                            'journal_id' => $journal_id,
                            'param_id' => $param_id,
                            'refs_type' => $refs_type,
                            'param_table' => $param_table
                        );
                        $this->db->insert('finance_refs', $tblValues);
                    }
                    $charttypes.=$chart_account_name;
                }
                $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;
            $data['msg'] = 'Fiscal Year Not Set.';
            return $data;
        }
    }

    function update_main_account($postdata) {
        $data = array();
        $this->db->trans_begin();
        $tblValues = array(
            'chart_account_name' => $postdata['chart_account_name'],
//                        'chart_type_id'=>$postdata['chart_type_id'],                            
        );
        $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 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 update_sub_ac_settings($postdata) {
        $data = array();
        $this->db->trans_begin();
        $masterid = $this->mastermodel->insertmasterdata();
        $tblValues = array(
            'customer' => $postdata['customer'],
            'employee' => $postdata['employee'],
            'profitloss' => $postdata['profitloss'],
            'salary' => $postdata['salary'],
            'vendor' => $postdata['vendor'],
            'maintenance_contract' => $postdata['maintenance_contract'],
        );
        $this->db->where('sub_account_settings_id', $postdata['sub_account_settings_id']);
        $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;
        }
    }

    /* functions for bank journal */

    function search_bank_journal($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('transaction_id', '4');
        $this->db->where('posted', '0');
        $data['results'] = $this->db->get();
        return $data;
    }

    function search_bank_journal_posted($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $this->db->where('transaction_id', '4');
        $this->db->where('posted', '1');
        $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 = "from account " . $from_account[1] . " to account " . $to_account[1];
            
//            $year = date("Y");
//            $transaction_code = "BJ/" . $year . '/';
//            $reference = $this->get_reference($transaction_code);
            
            $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' => 'From this account',
                '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' => 'To this account',
                '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) 
        {
            $data = array();
            $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 = "from account " . $from_account[1] . " to account " . $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' => 'From this account',
                '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' => 'To this account',
                '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);

            $data['resfunction'] = 'search_bank_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;
            }
        }
        else 
        {
            $data['res'] = 0;
            $data['msg'] = 'Fiscal Year Not Set.';
            return $data;
        }
    }

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

    /* functions for auto journal */

    function search_auto_journal($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_auto_journal_refs');
        $this->db->join('master', 'master_id=auto_journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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;
        }
    }

    /* functions for journal voucher */

    function search_journal_voucher($pagenum) {
        $data = array();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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();
        $log_in_type = $_SESSION['login_type'];
        $this->db->select('*');
        $this->db->from('finance_journal_refs');
        $this->db->join('master', 'master_id=journal_id');
        $this->db->where('master.login_type_id', $log_in_type);
        $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) 
        {
            $data = array();
            $this->db->trans_begin();
       
            $masterid = $this->mastermodel->insertmasterdata();
            $date = $this->mastermodel->convertdateformat($postdata['journal_voucher_date']);
            
            
//            $year = date("Y");
//            $transaction_code = "JV/" . $year . '/';
//            $reference = $this->get_reference($transaction_code);
            
            $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]);
                    $tblValues = array(
                        'journal_id' => $masterid,
                        'chart_account_code' => $account[1],
                        '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_journal_voucher';
            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();
        $data['resfunction'] = 'search_journal_voucher';
        $fisc_year_id = $this->getfiscalyearid();
        if ($fisc_year_id != 0) 
        {        
            $this->db->trans_begin();
            $journal_id = $postdata['journal_id'];        
            $date = $this->mastermodel->convertdateformat($postdata['journal_voucher_date']);

            $tblValues = array(
                'journal_date' => $date,
            );

            $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]);
                    $tblValues = array(
                        'journal_id' => $journal_id,
                        'chart_account_code' => $account[1],
                        '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_journal_voucher';
            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;
        }
    }

    function journal_unpost($postdata) {
        $data = array();
        $this->db->trans_begin();

        $fisc_year_id = 0;
        $fiscal_y = $this->mastermodel->get_single_joined_value('fiscal_year_id', 'finance_fiscal_year', 'master', 'fiscal_year_id', 'master_id', 'login_type_id', $_SESSION['login_type']);
        if ($fiscal_y)
            $fisc_year_id = $this->mastermodel->get_single_field_value('finance_fiscal_year', 'fiscal_year_id', 'closed', '1', 'fiscal_year_id');
        $masterid = $this->mastermodel->insertmasterdata();
        $date = $this->mastermodel->convertdateformat($postdata['journal_voucher_date']);
        $year = date("Y");

        $transaction_code = "JV/" . $year . '/';
        $reference = $this->get_reference($transaction_code);

        $tblValues = array(
            'journal_id' => $masterid,
            'journal_date' => $date,
            'description' => '',
            'reference' => $reference,
            'currency_id' => $this->mastermodel->get_single_field_value('finance_currency', 'currency_id', 'default_currency', '1'),
            '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;
        }
    }

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

        $log_in_type = $_SESSION['login_type'];

        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 master.login_type_id ='$log_in_type' and $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 master.login_type_id ='$log_in_type' and $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_reconcile($type, $account, $statement) {
        $log_in_type = $_SESSION['login_type'];
        if ($statement != 'new') {
//            $statement=$this->mastermodel->convdatformat($statement);
            $where = "fj.reconcile_date= '$statement' and";
        } else {
            $where = 'fj.reconcile_date IS NULL and';
        }


        
        if ($type == 'data') {


            $data = array();

            $res = $this->db->query("select *
                   from finance_journal fj 
                   JOIN master ON master_id=journal_id
                   where master.login_type_id ='$log_in_type' and $where chart_account_code = '$account'  order by fj.id  desc");

            foreach ($res->result_array() as $row) {
                $data[] = $row;
            }
            return $data;
        } else if ($type == 'count') {
            $res = $this->db->query("select *
                   from finance_journal fj 
                   JOIN master ON master_id=journal_id
                   where master.login_type_id ='$log_in_type' and $where chart_account_code = '$account'  order by fj.id  desc");

            return $res->num_rows();
        }
    }

    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) {

        $log_in_type = $_SESSION['login_type'];

        $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 master ON master_id=journal_id
               where $where master.login_type_id ='$log_in_type'  and  fj.reconcile_date between '$from' and '$to' and reconcile_date is not null  ");

        foreach ($res->result_array() as $row) {
            $data[] = $row;
        }
        return $data;
    }

    function isfiscalyearused($fiscal_yearid) {
        $this->db->where('fiscal_year_id', $fiscal_yearid);
        $Q = $this->db->get('finance_journal');
        $row = $Q->num_rows();
        return $row;
    }

    function getfiscalyearid() {
        $fisc_year_id = 0;
        $fiscal_y = $this->mastermodel->get_single_joined_value('fiscal_year_id', 'finance_fiscal_year', 'master', 'fiscal_year_id', 'master_id', 'login_type_id', $_SESSION['login_type']);
        if ($fiscal_y)
            $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;
        $currency_y = $this->mastermodel->get_single_joined_value('currency_id', 'finance_currency', 'master', 'currency_id', 'master_id', 'login_type_id', $_SESSION['login_type']);
        if ($currency_y)
            $currencyid = $this->mastermodel->get_single_field_value('finance_currency', 'currency_id', 'default_currency', '1');
        return $currencyid;
    }

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

}

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