<?php

class VAT {

    protected $info = array();
    private $db;
    private $year_id;
    private $vat_id;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
        $this->year_id = Session::getCurrentYearID();
        $this->vat_id = 0;
    }

    public function setData(array $data) {
        $this->info = $data;
        return $this;
    }

    private function viewDateOfLastClosingVAT() {
        $cond = '';
        if (!empty($this->vat_id))
            $cond = ' where Closing_VAT_ID <> "' . $this->vat_id . '" ';
        $query = $this->db->query('SELECT 
                                       Closing_VAT_Date 
                                    FROM 
                                        tbl_accounting_closing_vat
                                        ' . $cond . ' 
                                    order by  Closing_VAT_ID desc limit 1
                ');
        $row = $this->db->assoc($query);
        if (empty($row))
            return date('Y-m-d', strtotime(smc_session()['start_date'] . ' -1 day'));
        return $row['Closing_VAT_Date'];
    }

    private function viewVATByCodeType($code_type, $jv, $ref, $date, $logged_id, $default_currency, $desc) {
        $is_actual = 0;
        $condition = '';
        $total = 0.0;
        $last_date = self::viewDateOfLastClosingVAT();
        if (!empty($last_date))
            $condition .= ' AND t.Transaction_Date > "' . $last_date . '" ';
        $len = strlen($code_type);
        $query = $this->db->query('SELECT 
                                        t.Transaction_Amount,
                                        a.Account_Code,
                                        a.Account_ID,
                                        t.Transaction_Type,
                                        t.Currency_ID
                                    FROM 
                                        tbl_accounting_transactions t join tbl_chart_of_accounts a
                                        on a.Account_ID = t.Account_ID
                                    WHERE 
                                        current_year_id = "' . $this->year_id . '"
                                    AND
                                        LEFT(a.Account_Code,' . $len . ') = "' . $code_type . '"
                                        AND t.Transaction_Date <= "' . $date . '"
                ' . $condition);
        while ($row = $this->db->assoc($query)) {
            //in future use exact account balance
            $amount = CurrencyFactory::returnExchangedValueBetweenTwoCurrencies($row['Currency_ID'], $default_currency, $row['Transaction_Amount']);
            if ($row['Transaction_Type'] == 'Debit') {
                $data_1 = array(
                    "JV_ID" => $jv,
                    "Transaction_Reference" => $ref,
                    "Transaction_Account" => $row['Account_ID'],
                    "Transaction_Type" => "Credit",
                    "Transaction_Amount" => $amount,
                    "Transaction_Date" => $date,
                    "Currency_ID" => $default_currency,
                    "Logged_ID" => $logged_id,
                    "Transaction_Description" => $desc,
                    "Current_Ex_Rate" => "0",
                    "main_id" => $this->vat_id,
                    "main_type" => "clv"
                );
                JV::addTransaction($data_1, $is_actual);
            } else if ($row['Transaction_Type'] == 'Credit') {
                $data_2 = array(
                    "JV_ID" => $jv,
                    "Transaction_Reference" => $ref,
                    "Transaction_Account" => $row['Account_ID'],
                    "Transaction_Type" => "Debit",
                    "Transaction_Amount" => $amount,
                    "Transaction_Date" => $date,
                    "Currency_ID" => $default_currency,
                    "Logged_ID" => $logged_id,
                    "Transaction_Description" => $desc,
                    "Current_Ex_Rate" => "0",
                    "main_id" => $this->vat_id,
                    "main_type" => "clv"
                );
                JV::addTransaction($data_2, $is_actual);
//                echo "<pre>";
//                die(print_r($data_2));
                $amount = $amount * (-1);
            }
            $total += $amount;
        }
        if ($total < 0)
            $total = $total * (-1);
        return $total;
    }

    private function closingVATFirstStep($first_account, $jv, $ref, $date, $logged_id, $default_currency, $desc) {
        $first_code_by_id = JV::getAccountCodeByID($first_account);
        $total = self::viewVATByCodeType($first_code_by_id, $jv, $ref, $date, $logged_id, $default_currency, $desc);
        //4427 ---> for sales (usually must be credit so close should be credit)
        // same as sales account ex:660 sales including vat=> 600 sales credit and 60 sales vat credit
        //let total return operation ==> test to set debit or credit
//        $first_code_by_id = JV::getAccountCodeByID($first_account);
        if ($first_code_by_id == '4427')
            $transaction_type = 'Credit';
        else
            $transaction_type = 'Debit';
        $data_1 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $ref,
            "Transaction_Account" => $first_account,
            "Transaction_Type" => $transaction_type,
            "Transaction_Amount" => $total,
            "Transaction_Date" => $date,
            "Currency_ID" => $default_currency,
            "Logged_ID" => $logged_id,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_1, 0);
        return $total;
    }

    private function closingVATSecondStep(Array $arr) {
        $data_1 = array(
            "JV_ID" => $arr[3],
            "Transaction_Reference" => $arr[4],
            "Transaction_Account" => $arr[0],
            "Transaction_Type" => "Credit",
            "Transaction_Amount" => $arr[2],
            "Transaction_Date" => $arr[5],
            "Currency_ID" => $arr[7],
            "Logged_ID" => $arr[6],
            "Transaction_Description" => $arr[8],
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_1, 0);
        $data_2 = array(
            "JV_ID" => $arr[3],
            "Transaction_Reference" => $arr[4],
            "Transaction_Account" => $arr[1],
            "Transaction_Type" => "Debit",
            "Transaction_Amount" => $arr[2],
            "Transaction_Date" => $arr[5],
            "Currency_ID" => $arr[7],
            "Logged_ID" => $arr[6],
            "Transaction_Description" => $arr[8],
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_2, 0);
    }

    private function closingVATThirdStep(array $data) {
        $data_1 = array(
            "JV_ID" => $data['jv'],
            "Transaction_Reference" => $data['ref'],
            "Transaction_Account" => $data['first_account'],
            "Transaction_Type" => "Credit",
            "Transaction_Amount" => $data['first_amount'],
            "Transaction_Date" => $data['date'],
            "Currency_ID" => $data['currency'],
            "Logged_ID" => $data['logged'],
            "Transaction_Description" => $data['desc'],
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_1, 0);
        $data_2 = array(
            "JV_ID" => $data['jv'],
            "Transaction_Reference" => $data['ref'],
            "Transaction_Account" => $data['second_account'],
            "Transaction_Type" => "Credit",
            "Transaction_Amount" => $data['second_amount'],
            "Transaction_Date" => $data['date'],
            "Currency_ID" => $data['currency'],
            "Logged_ID" => $data['logged'],
            "Transaction_Description" => $data['desc'],
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_2, 0);
        $data_3 = array(
            "JV_ID" => $data['jv'],
            "Transaction_Reference" => $data['ref'],
            "Transaction_Account" => $data['third_account'],
            "Transaction_Type" => "Credit",
            "Transaction_Amount" => $data['third_amount'],
            "Transaction_Date" => $data['date'],
            "Currency_ID" => $data['currency'],
            "Logged_ID" => $data['logged'],
            "Transaction_Description" => $data['desc'],
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_3, 0);
        $data_4 = array(
            "JV_ID" => $data['jv'],
            "Transaction_Reference" => $data['ref'],
            "Transaction_Account" => $data['fourth_account'],
            "Transaction_Type" => "Debit",
            "Transaction_Amount" => $data['total'],
            "Transaction_Date" => $data['date'],
            "Currency_ID" => $data['currency'],
            "Logged_ID" => $data['logged'],
            "Transaction_Description" => $data['desc'],
            "Current_Ex_Rate" => "0",
            "main_id" => $this->vat_id,
            "main_type" => "clv"
        );
        JV::addTransaction($data_4, 0);
    }

    private function closeVATFourthStep($trans_array) {
        for ($i = 0; $i < count($trans_array['Transaction_Account']); $i++) {
            $data_4 = array(
                "JV_ID" => $trans_array['Transaction_JV'],
                "Transaction_Reference" => $trans_array['Transaction_Reference'],
                "Transaction_Account" => $trans_array['Transaction_Account'][$i],
                "Transaction_Type" => $trans_array['Transaction_Type'][$i],
                "Transaction_Amount" => $trans_array['Transaction_Amount'][$i],
                "Transaction_Date" => $trans_array['Transaction_Date'],
                "Currency_ID" => $trans_array['Currency_ID'],
                "Logged_ID" => $trans_array['Logged_ID'],
                "Transaction_Description" => $trans_array['Transaction_Description'],
                "Current_Ex_Rate" => "0",
                "main_id" => $this->vat_id,
                "main_type" => "clv"
            );
            JV::addTransaction($data_4, 0);
        }
    }

    public function closeVAT($date, $logged_id) {
        $this->db->query("START TRANSACTION");
        //accounts vat for next level start with 29
        $ref = str_replace('-', '', $date);
        $default_currency = CurrencyFactory::returnLocalCurrency();
        $close_total = 0;
        $closing_array = array(
            'Closing_VAT_Amount' => $close_total,
            'Currency_ID' => $default_currency,
            'Closing_VAT_Date' => $date,
            'Logged_ID' => $logged_id
        );
        self::registerClosingVAT($closing_array);
        $this->vat_id = $this->db->id();
        $jv = JV::returnJV(0);
        $desc = 'Closing VAT for ' . $date;
        $org_type = self::viewOrgVATType();
//        getAccountIDsByCodes
        $vat_codes_arr = ["44210", "44213", "44216", "44261", "44263", "44265", "44211", "44214", "44217", "44262",
            "44264", "44266", "4426", "685", "4427", "4425", "44252", "44251"];
        $vat_ids = JV::getAccountIDsByCodes($vat_codes_arr);
        if ($org_type == 1) {
            $vat_1 = $vat_ids["44210"];
            $vat_2 = $vat_ids["44213"];
            $vat_3 = $vat_ids["44216"];

            $close_1 = $vat_ids["44261"];
            $close_2 = $vat_ids["44263"];
            $close_3 = $vat_ids["44265"];
        } else if ($org_type == 2) {
            $vat_1 = $vat_ids["44211"];
            $vat_2 = $vat_ids["44214"];
            $vat_3 = $vat_ids["44217"];

            $close_1 = $vat_ids["44262"];
            $close_2 = $vat_ids["44264"];
            $close_3 = $vat_ids["44266"];
        } else if ($org_type == 3) {
            $vat_1 = $vat_ids["44211"];
            $vat_2 = $vat_ids["44214"];
            $vat_3 = $vat_ids["44217"];

            $close_1 = $vat_ids["44261"];
            $close_2 = $vat_ids["44263"];
            $close_3 = $vat_ids["44265"];
        }
        //first step
        $total_44210 = self::closingVATFirstStep($vat_1, $jv, $ref, $date, $logged_id, $default_currency, $desc);

//                $error = $this->db->error();
//        if(empty($error)){
//            $this->db->query('COMMIT');
//        }else{
//            $this->db->query('ROLLBACK');
//        }
//        return array_unique($error);
        $total_44213 = self::closingVATFirstStep($vat_2, $jv, $ref, $date, $logged_id, $default_currency, $desc);
        $total_44216 = self::closingVATFirstStep($vat_3, $jv, $ref, $date, $logged_id, $default_currency, $desc);
//        $error = $this->db->error();
//        if(empty($error)){
//            $this->db->query('COMMIT');
//        }else{
//            $this->db->query('ROLLBACK');
//        }
//        return array_unique($error);
        //second step
        $arr_1 = array($vat_1, $close_1, $total_44210, $jv, $ref, $date, $logged_id, $default_currency, $desc);
        self::closingVATSecondStep($arr_1);
        $arr_2 = array($vat_2, $close_2, $total_44213, $jv, $ref, $date, $logged_id, $default_currency, $desc);
        self::closingVATSecondStep($arr_2);
        $arr_3 = array($vat_3, $close_3, $total_44216, $jv, $ref, $date, $logged_id, $default_currency, $desc);
        self::closingVATSecondStep($arr_3);

        //third step
        $total = $total_44210 + $total_44213 + $total_44216;
        if ($org_type == 1 or $org_type == 2) {
            if ($org_type == 1) {
                $data = array('first_account' => $close_1,
                    'first_amount' => $total_44210,
                    'second_account' => $close_2,
                    'second_amount' => $total_44213,
                    'third_account' => $close_3,
                    'third_amount' => $total_44216,
                    'fourth_account' => '4426',
                    'total' => $total,
                    'jv' => $jv,
                    'ref' => $ref,
                    'date' => $date,
                    'logged' => $logged_id,
                    'currency' => $default_currency,
                    'desc' => $desc
                );
                self::closingVATThirdStep($data);
            } elseif ($org_type == 2) {
                //            $total = $total_44210 + $total_44213 + $total_44216;
                $percent_1 = self::viewSalesTaxablePercent();
                $percent = round($percent_1, 3);
                $rem_percent = 1 - $percent;
                $total_44263_rem = $total_44213 * $rem_percent;
                $total_44265_rem = $total_44216 * $rem_percent;
                $total_44263 = $total_44213 * $percent;
                $total_44265 = $total_44216 * $percent;
                //if type of org is production and partially=> {
                //$total_44210_new = $total_44210 * $percent;
                ////$total_44210_rem = $total_44210 * $rem_percent;
                // $total_4426 = $total_44265 + $total_44263 + $total_44210_new;
                //$total_685 = $total_44263_rem + $total_44265_rem + $total_44210_rem;
                //}
                $total_4426 = $total_44265 + $total_44263 + $total_44210;
                $total_685 = $total_44263_rem + $total_44265_rem;
                $data = array(
                    "Transaction_JV" => $jv,
                    "Transaction_Reference" => $ref,
                    "Transaction_Account" => array(
                        $close_1,
                        $close_2,
                        $close_3,
                        $vat_ids['4426'],
                        $vat_ids['685']
                    ),
                    "Transaction_Type" => array(
                        "Credit",
                        "Credit",
                        "Credit",
                        "Debit",
                        "Debit"
                    ),
                    "Transaction_Amount" => array(
                        $total_44210,
                        $total_44213,
                        $total_44216,
                        $total_4426,
                        $total_685
                    ),
                    "Transaction_Date" => $date,
                    "Currency_ID" => $default_currency,
                    "Logged_ID" => $logged_id,
                    "Transaction_Description" => $desc
                );
                self::closeVATFourthStep($data);
            }
            $total_4427 = self::closingVATFirstStep($vat_ids['4427'], $jv, $ref, $date, $logged_id, $default_currency, $desc);

            $fourth_array = array(
                "Transaction_JV" => $jv,
                "Transaction_Reference" => $ref,
                "Transaction_Account" => array(
                    $vat_ids['4426'],
                    $vat_ids['4425'],
                    $vat_ids['4427'],
                    $vat_ids['4425']
                ),
                "Transaction_Type" => array(
                    "Credit",
                    "Debit",
                    "Debit",
                    "Credit"
                ),
                "Transaction_Amount" => array(
                    $total,
                    $total,
                    $total_4427,
                    $total_4427
                ),
                "Transaction_Date" => $date,
                "Currency_ID" => $default_currency,
                "Logged_ID" => $logged_id,
                "Transaction_Description" => $desc
            );
            self::closeVATFourthStep($fourth_array);

            $close_total = $total + $total_4427;
            $diff_4425 = $total - $total_4427;

            if ($diff_4425 > 0) {
                $final_4425 = array(
                    "Transaction_JV" => $jv,
                    "Transaction_Reference" => $ref,
                    "Transaction_Account" => array(
                        $vat_ids['4425'],
                        $vat_ids['44252']
                    ),
                    "Transaction_Type" => array(
                        "Credit",
                        "Debit"
                    ),
                    "Transaction_Amount" => array(
                        $diff_4425,
                        $diff_4425
                    ),
                    "Transaction_Date" => $date,
                    "Currency_ID" => $default_currency,
                    "Logged_ID" => $logged_id,
                    "Transaction_Description" => $desc
                );
                self::closeVATFourthStep($final_4425);
            } elseif ($diff_4425 < 0) {
                $final_4425 = array(
                    "Transaction_JV" => $jv,
                    "Transaction_Reference" => $ref,
                    "Transaction_Account" => array(
                        $vat_ids['4425'],
                        $vat_ids['44251']
                    ),
                    "Transaction_Type" => array(
                        "Debit",
                        "Credit"
                    ),
                    "Transaction_Amount" => array(
                        $diff_4425 * (-1),
                        $diff_4425 * (-1)
                    ),
                    "Transaction_Date" => $date,
                    "Currency_ID" => $default_currency,
                    "Logged_ID" => $logged_id,
                    "Transaction_Description" => $desc
                );
                self::closeVATFourthStep($final_4425);
            }
        } else {
            $data_1 = array(
                "JV_ID" => $jv,
                "Transaction_Reference" => $ref,
                "Transaction_Account" => $close_1,
                "Transaction_Type" => "Credit",
                "Transaction_Amount" => $total_44210,
                "Transaction_Date" => $date,
                "Currency_ID" => "84",
                "Logged_ID" => $logged_id,
                "Transaction_Description" => $desc,
                "Current_Ex_Rate" => "0",
                "main_id" => $this->vat_id,
                "main_type" => "clv"
            );
            JV::addTransaction($data_1, 0);
            $data_2 = array(
                "JV_ID" => $jv,
                "Transaction_Reference" => $ref,
                "Transaction_Account" => $close_2,
                "Transaction_Type" => "Credit",
                "Transaction_Amount" => $total_44213,
                "Transaction_Date" => $date,
                "Currency_ID" => "84",
                "Logged_ID" => $logged_id,
                "Transaction_Description" => $desc,
                "Current_Ex_Rate" => "0",
                "main_id" => $this->vat_id,
                "main_type" => "clv"
            );
            JV::addTransaction($data_2, 0);
            $data_3 = array(
                "JV_ID" => $jv,
                "Transaction_Reference" => $ref,
                "Transaction_Account" => $close_3,
                "Transaction_Type" => "Credit",
                "Transaction_Amount" => $total_44216,
                "Transaction_Date" => $date,
                "Currency_ID" => "84",
                "Logged_ID" => $logged_id,
                "Transaction_Description" => $desc,
                "Current_Ex_Rate" => "0",
                "main_id" => $this->vat_id,
                "main_type" => "clv"
            );
            JV::addTransaction($data_3, 0);
            $data_t = array(
                "JV_ID" => $jv,
                "Transaction_Reference" => $ref,
                "Transaction_Account" => $vat_ids["44252"],
                "Transaction_Type" => "Debit",
                "Transaction_Amount" => $total,
                "Transaction_Date" => $date,
                "Currency_ID" => "84",
                "Logged_ID" => $logged_id,
                "Transaction_Description" => $desc,
                "Current_Ex_Rate" => "0",
                "main_id" => $this->vat_id,
                "main_type" => "clv"
            );
            JV::addTransaction($data_t, 0);
        }


        $mydata = [&$close_total, &$this->vat_id];
//       $error = $this->db->error();
//        die(print_r($error));
        $this->db->prepare_Stmt("UPDATE tbl_accounting_closing_vat 
                                SET   
                                    Closing_VAT_Amount = ?
                                WHERE 
                                    Closing_VAT_ID = ? 
                ", $mydata);
        $error = $this->db->error();
        if (empty($error)) {
            $this->db->query('COMMIT');
        } else {
            $this->db->query('ROLLBACK');
        }
        return array_unique($error);
    }

    private function viewSalesTaxablePercent() {
        $currency = CurrencyFactory::returnLocalCurrency();
        $taxable_amount = 0.0;
        $not_taxable_amount = 0.0;
        $total = 0.0;
        $last_date = self::viewDateOfLastClosingVAT();
        $condition = '';
        if (!is_null($last_date))
            $condition .= ' AND t.Transaction_Date > "' . $last_date . '"';
        $taxable_query = $this->db->query('SELECT 
                                                t.Transaction_Amount,
                                                t.Currency_ID
                                            FROM 
                                                tbl_accounting_transactions t join tbl_chart_of_accounts a
                                                on t.Account_ID = a.Account_ID
                                            WHERE  
                                                t.current_year_id = "' . $this->year_id . '"
                                            AND
                                                LEFT(a.Account_Code,4) = 7001
                        ' . $condition);
        while ($row = $this->db->assoc($taxable_query)) {
            $amount = CurrencyFactory::returnExchangedValueBetweenTwoCurrencies($row['Currency_ID'], $currency, $row['Transaction_Amount']);
            $taxable_amount += $amount;
        }

        $not_taxable_query = $this->db->query('SELECT 
                                                t.Transaction_Amount,
                                                t.Currency_ID
                                            FROM 
                                                tbl_accounting_transactions t join tbl_chart_of_accounts a
                                                on t.Account_ID = a.Account_ID
                                            WHERE
                                                t.current_year_id = "' . $this->year_id . '"
                                            AND
                                                LEFT(a.Account_Code,4) = 7002
                        ' . $condition);
        while ($row2 = $this->db->assoc($not_taxable_query)) {
            $amount = CurrencyFactory::returnExchangedValueBetweenTwoCurrencies($row2['Currency_ID'], $currency, $row2['Transaction_Amount']);
            $not_taxable_amount += $amount;
        }
        //total sold
        $total = $taxable_amount + $not_taxable_amount;
        $query = $this->db->query('SELECT 
                                        t.Transaction_Amount,
                                        t.Currency_ID,
                                        t.JV_ID
                                    FROM 
                                        tbl_accounting_transactions t join tbl_chart_of_accounts a
                                                on t.Account_ID = a.Account_ID
                                    WHERE  
                                        t.current_year_id = "' . $this->year_id . '"
                                    AND
                                        LEFT(a.Account_Code,1) = 7
                                    AND 
                                       LEFT(a.Account_Code,4) <> 7002
                                    AND 
                                       LEFT(a.Account_Code,4) <> 7001 
                ' . $condition);
        while ($row3 = $this->db->assoc($query)) {
            $amount = CurrencyFactory::returnExchangedValueBetweenTwoCurrencies($row3['Currency_ID'], $currency, $row3['Transaction_Amount']);
            $query2 = $this->db->query('SELECT 
                                            * 
                                        FROM 
                                            tbl_accounting_transactions t join tbl_chart_of_accounts a
                                                on t.Account_ID = a.Account_ID
                                        WHERE 
                                            t.current_year_id = "' . $this->year_id . '"
                                        AND
                                            t.JV_ID = "' . $row3['JV_ID'] . '"
                                        AND 
                                            LEFT(a.Account_Code,4) = 4427
                    ');
            $row_test = $this->db->assoc($query2);
            if (count($row_test) > 0)
                $taxable_amount += $amount;
            $total += $amount;
        }
        if ($total > 0)
            $percent = $taxable_amount / $total;
        else
            $percent = 0;
        return $percent;
    }

    private function registerClosingVAT(array $data) {
        $mydata = [
            &$data['Closing_VAT_Amount'],
            &$data['Currency_ID'],
            &$data['Closing_VAT_Date'],
            &$data['Logged_ID']
        ];
        $this->db->prepare_Stmt("INSERT INTO
                                        tbl_accounting_closing_vat (
                                            Closing_VAT_Amount,
                                            Currency_ID,
                                            Closing_VAT_Date,
                                            Logged_ID
                                        )
                                        VALUES (
                                            ?,?,?,?
                                        )
                ", $mydata);
    }

    public function viewVATTransactions($date) {
        $data = array();
        $condition = ' AND current_year_id = "' . $this->year_id . '"';
        $org_type = self::viewOrgVATType();
        $vat_codes = ["44210", "44213", "44216", "44211", "44214", "44217"];
        $vat_ids = JV::getAccountIDsByCodes($vat_codes);
        if ($org_type == 1) {
            $vat_1 = 44210;
            $vat_2 = 44213;
            $vat_3 = 44216;
//            $vat_1 = $vat_ids["44210"];
//            $vat_2 = $vat_ids["44213"];
//            $vat_3 = $vat_ids["44216"];
        } else {
            $vat_1 = 44211;
            $vat_2 = 44214;
            $vat_3 = 44217;
//            $vat_1 = $vat_ids["44211"];
//            $vat_2 = $vat_ids["44214"];
//            $vat_3 = $vat_ids["44217"];
        }
        $last_date = self::viewDateOfLastClosingVAT();

        if (!is_null($last_date))
            $condition .= ' AND Transaction_Date > "' . $last_date . '"';
        $query = $this->db->query('SELECT 
                                        *
                                    FROM
                                        transactions_info
                                    WHERE 
                                        
                                            ( LEFT(Transaction_Account,5) = "' . $vat_1 . '"
                                            OR
                                            LEFT(Transaction_Account,5) = "' . $vat_2 . '"
                                            OR
                                            LEFT(Transaction_Account,5) = "' . $vat_3 . '"
                                            OR
                                            LEFT(Transaction_Account,4) = 4427 )
                                            AND Transaction_Date <= "' . $date . '"
                                        
                ' . $condition);
        while ($row = $this->db->assoc($query))
            $data[] = $row;
        return $data;
    }

    private function viewOrgVATType() {
        $query = $this->db->query('SELECT VAT_Type_ID FROM tbl_organization WHERE Organization_ID = 1');
        $row = $this->db->assoc($query);
        return $row['VAT_Type_ID'];
    }

}
