<?php

class DebitNote {

    protected $info = array();
    private $year_id;
    private $year_id_actual;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
        $this->year_id = Session::getCurrentYearID();
        $this->year_id_actual = Session::getCurrentYearIDActual();
        $this->start_date = Session::getCurrentYearRange()['start_date'];
        $this->end_date = Session::getCurrentYearRange()['end_date'];
    }

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

    public function create() {
        $error = token_unique::is_valid_token();
        if (!empty($error))
            return $error;
//        if (!empty($this->info['unique_id']) && !empty(smc_session()['default_setting']['is_production'])) {
//            if (!isset(smc_session()['duplicate_check'][$this->info['unique_id']])) {
//                $error = array();
//                $error[0] = CommonLanguage::getMessage('duplicate_record');
//                return $error;
//            } else {
//                unset(smc_session()['duplicate_check'][$this->info['unique_id']]);
//            }
//        }
        $is_actual = $this->info['is_actual'];
//        $check = JV::checkCurrentYear($this->info['Note_Date'], $this->info['Note_Date'], $is_actual);
        $check = CommonFunctions::check_lock_data(['type' => 'note', 'start_date' => $this->info['Note_Date'], 'is_actual' => $is_actual]);

        if ($check !== '') {
            $error = array();
            $error[0] = $check;
            return $error;
        }
        //        if($this->info['Note_Date'] > $this->end_date || $this->info['Note_Date'] < $this->start_date){ 
        //            $error = array();
        //            $error[] = CommonLanguage::getMessage('current_year_modify');
        //            return $error;
        //        }
        $note_nb = $this->getmaxNoteID($this->info['Is_Debit_Note'], $is_actual);
        $rate = !empty($this->info['current_rate']) ? $this->info['current_rate'] : CurrencyFactory::getLastExRate($this->info['Currency_ID']);
        $year_id = $is_actual ? $this->year_id_actual : $this->year_id;
        $second_rate = !empty($this->info['second_currency_rate']) ? $this->info['second_currency_rate'] : CurrencyFactory::getSecondCurrencyLastExRate($this->info['Currency_ID']);
        $date_time = date('Y-m-d H:i:s');
        $total_paid_all = $this->info['Card_Amount'] + $this->info['Cash_Amount'] + $this->info['Check_Amount'];
        $this->db->query("START TRANSACTION");
        $data = [
            &$this->info['Supplier_Customer_ID'],
            &$this->info['Note_Date'],
            &$this->info['Currency_ID'],
            &$this->info['Note_Amount'],
            &$this->info['Note_Description'],
            &$this->info['Is_Debit_Note'],
            &$this->info['branch_id'],
            &$this->info['job_id'],
            &$this->info['project_id'],
            &$this->info['sales_purchase_account'],
            &$rate,
            &$second_rate,
            &$note_nb,
            &$this->info[0],
            &$this->info[0],
            &$date_time, &$date_time,
            &$is_actual,
            &$year_id, &$total_paid_all
        ];
        $this->db->prepare_Stmt("
                            INSERT
                            INTO
                                tbl_accounting_debit_credit_note
                            (
                                Supplier_Customer_ID,
                                Note_Date,
                                Currency_ID,
                                Note_Amount,
                                Note_Description,
                                Is_Debit_Note,
                                branch_id,job_id,project_id,
                                Account_ID,
                                current_rate,
                                second_currency_rate,
                                note_nb,
                                Logged_ID,created_logged_id,date_time,date_created,
                                is_actual,
                                current_year_id,paid_amount
                            )
                                VALUES
                            (
                                ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
                            )
                        ", $data);
        $note_id = $this->db->id();

        //        $lastid = $this->db->id();
        $jv = JV::returnJV($is_actual);
        $type = $this->info['Is_Debit_Note'];
        $ref = $type ? 'deb' : 'cre';
        $sup_cust_code = $this->info['Account_Code'];
        $desc = $this->info['Note_Description'];

        $remaining = $this->info['Note_Amount'] - $total_paid_all;
        //        $ids_arr = JV::getAccountIDsByCodes(['701','601']);
        if ($type == 0) {
            $d_account = $this->info['sales_purchase_account'];
            $c_account = $sup_cust_code;
            $trans_type_1 = "Credit";
            $trans_type_2 = "Debit";
        } else {
            $d_account = $sup_cust_code;
            $c_account = $this->info['sales_purchase_account'];
            $trans_type_1 = "Debit";
            $trans_type_2 = "Credit";
        }

        $data_1 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $ref . ' ' . $note_id,
            "Transaction_Account" => $d_account,
            "Transaction_Type" => 'Debit',
            "Transaction_Amount" => $this->info['Note_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $this->info[0],
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_1, $is_actual);
        $data_2 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $note_id,
            "Transaction_Account" => $c_account,
            "Transaction_Type" => 'Credit',
            "Transaction_Amount" => $this->info['Note_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $this->info[0],
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_2, $is_actual);
        $data_3 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $note_id,
            "Transaction_Account" => $sup_cust_code,
            "Transaction_Type" => $trans_type_2,
            "Transaction_Amount" => $total_paid_all,
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $this->info[0],
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_3, $is_actual);
        $data_4 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => 'credit',
            "Transaction_Account" => $this->info['Card_Account'],
            "Transaction_Type" => $trans_type_1,
            "Transaction_Amount" => $this->info['Card_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $this->info[0],
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_4, $is_actual);
        $data_5 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => 'cash',
            "Transaction_Account" => $this->info['Cash_Account'],
            "Transaction_Type" => $trans_type_1,
            "Transaction_Amount" => $this->info['Cash_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $this->info[0],
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_5, $is_actual);
        $data_6 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => 'check',
            "Transaction_Account" => $this->info['Check_Account'],
            "Transaction_Type" => $trans_type_1,
            "Transaction_Amount" => $this->info['Check_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $this->info[0],
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_6, $is_actual);
        $mydate = date('Y-m-d H:i:s');
        $arr_log = ['type' => $ref, 'main_id' => $note_id,
            'action' => 0, 'date_time' => $mydate,
            'user_id' => smc_session()['user_logged_id']];
        CommonFunctions::add_user_log($arr_log);
        if (isset($this->info['close_arr'])) {
            $rec_type = $type ? 'deb' : 'cre';
            $row_data = ['rate' => $rate];
            CommonBusiness::close_invoices($this->info['close_arr'], $note_id, $rec_type, $row_data);
        }
        $error = $this->db->error();
        if (empty($error)) {
            $this->db->query('COMMIT');
            token_unique::remove_token();
        } else {
            $this->db->query('ROLLBACK');
        }
        return array_unique($error);
    }

    public function viewDebitNotes($id = null, $date = null, $month = null, $year = null) {
        $data = array();
        $condition = '';
        if (!is_null($id))
            $condition .= ' AND n.Note_ID = "' . $id . '"';
        if (!is_null($date))
            $condition .= ' AND n.Note_Date = "' . $date . '"';
        if (!is_null($month))
            $condition .= ' AND MONTH(n.Note_Date) = "' . $month . '"';
        if (!is_null($year))
            $condition .= ' AND YEAR(n.Note_Date) = "' . $year . '"';
        $query = $this->db->query('SELECT
                                        n.*,
                                        c.*,
                                        s.*
                                    FROM
                                        tbl_accounting_debit_credit_note n
                                    LEFT JOIN
                                        tbl_common_currency c
                                    ON
                                        c.Currency_ID = n.Currency_ID
                                    LEFT JOIN
                                        tbl_stock_suppliers s
                                    ON
                                        s.Supplier_ID = n.Supplier_Customer_ID
                                    WHERE
                                        n.Is_Debit_Note = 1' . $condition);
        while ($row = $this->db->assoc($query)) {
            $data[] = $row;
        }
        return $data;
    }

    public function viewCreditNotes($id = null, $date = null, $month = null, $year = null) {
        $data = array();
        $condition = '';
        if (!is_null($id))
            $condition .= ' AND n.Note_ID = "' . $id . '"';
        if (!is_null($date))
            $condition .= ' AND n.Note_Date = "' . $date . '"';
        if (!is_null($month))
            $condition .= ' AND MONTH(n.Note_Date) = "' . $month . '"';
        if (!is_null($year))
            $condition .= ' AND YEAR(n.Note_Date) = "' . $year . '"';
        $query = $this->db->query('SELECT
                                        n.*,
                                        c.*,
                                        ct.*
                                    FROM
                                        tbl_accounting_debit_credit_note n
                                    LEFT JOIN
                                        tbl_common_currency c
                                    ON
                                        c.Currency_ID = n.Currency_ID
                                    LEFT JOIN
                                        tbl_customers ct
                                    ON
                                        ct.Customer_ID = n.Supplier_Customer_ID
                                    WHERE
                                        n.Is_Debit_Note = 0' . $condition);
        while ($row = $this->db->assoc($query)) {
            $data[] = $row;
        }
        /*
          if ($row['Supplier_Customer_ID'] == 0)
          $row['current_balance'] = $row['previous_balance'] = '---';
          else {

          $report = new Report;
          $temp_current_balance = $report->returnCustomerExactBalance($row["Supplier_Customer_ID"], $row["Currency_ID"], smc_session()['start_date'] . ' 00:00:00', $row["date_created"], 0, $row['is_actual']);

          $row['current_balance'] = CommonFunctions::formatNumber($temp_current_balance['Balance']);
          $row['previous_balance'] = CommonFunctions::formatNumber(($temp_current_balance['Balance'] - ($row['Note_Amount'] - $paid_amount)));
          }
         */
        return $data;
    }

    public function getmaxNoteID($is_debit, $actual) {
        $year = $actual == 1 ? $this->year_id_actual : $this->year_id;
        $query = $this->db->query('SELECT MAX(note_nb) AS note_nb FROM tbl_accounting_debit_credit_note 
                where current_year_id = "' . $year . '" 
                and is_actual = "' . $actual . '" 
                and Is_Debit_Note = "' . $is_debit . '"');
        $row = $this->db->assoc($query);
        return $row['note_nb'] + 1;
    }

    public function getNoteByID($id) {
        $data = array();
        $main_query = $this->db->query('select d.*,cu.*,concat(c.Account_Name," - ",c.Account_Code) as Account_Name,
                    b.branch_name,j.job_name,pr.project_name
                    from tbl_accounting_debit_credit_note d
                    join tbl_chart_of_accounts c on c.Account_ID = d.Account_ID
                    left join tbl_organization_branches b on b.branch_id = d.branch_id
                    left join tbl_accounting_jobs j on j.job_id = d.job_id
                    left join tbl_projects pr on pr.project_id = d.project_id
                    join tbl_common_currency cu on cu.Currency_ID = d.Currency_ID
                    where Note_ID = "' . $id . '"');
        $data['Main'] = $this->db->assoc($main_query);
        if ($data['Main']['Is_Debit_Note'] == 0) {
            $data['Person'] = Customer::getCustomerByID($data['Main']['Supplier_Customer_ID'], 1, $data['Main']['is_actual']);
        } else {
            $data['Person'] = Supplier::getSupplierByID($data['Main']['Supplier_Customer_ID'], 1, $data['Main']['is_actual']);
        }

        $query = $this->db->query("SELECT 
                                            t.Transaction_Amount,
                                            t.Transaction_Reference,
                                            t.Account_ID,
                                            p.Account_Name AS P_Name
                                        FROM 
                                            tbl_accounting_transactions t
                                        LEFT JOIN 
                                            tbl_chart_of_accounts p
                                        ON 
                                            t.Account_ID = p.Account_ID
                                       WHERE 
                                             t.main_id = '" . $id . "'
                                        AND 
                                            t.main_type = '" . ($data['Main']['Is_Debit_Note'] ? 'deb' : 'cre') . "'
                                        AND 
                                            t.Transaction_Reference in ('cash','check','credit')
                                        ");
        $data['Payment'] = array();
        while ($row = $this->db->assoc($query)) {
            if (stripos($row['Transaction_Reference'], 'cash') !== false)
                $data['Payment']['Cash'] = $row;
            if (stripos($row['Transaction_Reference'], 'check') !== false)
                $data['Payment']['Cheque'] = $row;
            if (stripos($row['Transaction_Reference'], 'credit') !== false)
                $data['Payment']['Credit_Card'] = $row;
        }
        //
        if ($data['Main']['Is_Debit_Note']) {
            $data['pay_inv'] = Purchase::getUnpaidInvoicesBySupplier($data['Person']['Account_ID'], $data['Main']['Currency_ID'], $id, $data['Main']['is_actual'], 'deb');
        } else {
            $data['pay_inv'] = Invoice::getUnpaidInvoicesByCustomer($data['Person']['Account_ID'], $data['Main']['Currency_ID'], $id, $data['Main']['is_actual'], 0, 'cre');
        }
        //
        return $data;
    }

    public function editdebitnote() {
        $logged_id = smc_session()['user_logged_id'];
        $is_actual = $this->info['is_actual'];
//        $check = JV::checkCurrentYear($this->info['Note_Date'], $this->info['Note_Date'], $is_actual);
        $check = CommonFunctions::check_lock_data(['type' => 'note', 'start_date' => $this->info['Note_Date'], 'is_actual' => $is_actual]);

        if ($check !== '') {
            $error = array();
            $error[0] = $check;
            return $error;
        }
        $rate = !empty($this->info['current_rate']) ? $this->info['current_rate'] : CurrencyFactory::getLastExRate($this->info['Currency_ID']);
        $second_rate = !empty($this->info['second_currency_rate']) ? $this->info['second_currency_rate'] : CurrencyFactory::getSecondCurrencyLastExRate($this->info['Currency_ID']);
        $date_time = date('Y-m-d H:i:s');
        $note_type = ($this->info['Is_Debit_Note'] ? 'deb' : 'cre');
        $year_id = $is_actual ? $this->year_id_actual : $this->year_id;
        $jv_query = $this->db->query("SELECT DISTINCT JV_ID FROM tbl_accounting_transactions  
            WHERE main_id='" . $this->info['Note_ID'] . "' AND main_type= '" . $note_type . "' 
                AND current_year_id='" . $year_id . "' and is_actual = '" . $is_actual . "'");
        $jv_assoc = $this->db->assoc($jv_query);
        if (count($jv_assoc) > 0)
            $jv = $jv_assoc['JV_ID'];
        else
            $jv = JV::returnJV($is_actual);
        $this->db->query("START TRANSACTION");
        $delete_jv = [&$this->info['Note_ID'], &$note_type];
        $this->db->prepare_Stmt('delete from tbl_accounting_transactions where main_id = ? and main_type= ? ', $delete_jv);
        $query = $this->db->query('select date_created,created_logged_id, Currency_ID,current_rate
            from tbl_accounting_debit_credit_note where Note_ID="' . $this->info['Note_ID'] . '"');
        $row_note = $this->db->assoc($query);
        $date_created = $row_note['date_created'];
        $created_logged_id = $row_note['created_logged_id'];
        $old_rate = $row_note['current_rate'];
        $old_currency = $row_note['Currency_ID'];
        $total_paid_all = $this->info['Card_Amount'] + $this->info['Cash_Amount'] + $this->info['Check_Amount'];
        $record_type = $this->info['Is_Debit_Note'] ? 'deb' : 'cre';
        CommonBusiness::return_inv_pur_close_amount($record_type, $old_currency, $old_rate, $this->info['Note_ID']);
        $data = [
            &$this->info['Supplier_Customer_ID'],
            &$this->info['Note_Date'],
            &$this->info['Currency_ID'],
            &$this->info['Note_Amount'],
            &$this->info['Note_Description'],
            &$this->info['branch_id'],
            &$this->info['job_id'],
            &$this->info['project_id'],
            &$this->info['sales_purchase_account'],
            &$rate, &$second_rate,
            &$logged_id, &$date_time, &$total_paid_all,
            &$this->info['Note_ID']
        ];
        $this->db->prepare_Stmt("update tbl_accounting_debit_credit_note
            set  Supplier_Customer_ID = ?, Note_Date = ?, Currency_ID = ?,  Note_Amount = ?,
            Note_Description = ?, branch_id = ?,job_id=?,project_id=?, Account_ID = ?,
            current_rate=?,second_currency_rate=?, Logged_ID = ?,date_time=?,paid_amount=? where Note_ID = ? ", $data);
        $note_id = $this->info['Note_ID'];
        $type = $this->info['Is_Debit_Note'];
        $ref = $type ? 'deb' : 'cre';
        $sup_cust_code = $this->info['Account_Code'];
        $desc = $this->info['Note_Description'];
        if ($type == 0) {
            $d_account = $this->info['sales_purchase_account'];
            $c_account = $sup_cust_code;
            $trans_type_1 = "Credit";
            $trans_type_2 = "Debit";
        } else {
            $d_account = $sup_cust_code;
            $c_account = $this->info['sales_purchase_account'];
            $trans_type_1 = "Debit";
            $trans_type_2 = "Credit";
        }
        $data_1 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $ref . ' ' . $note_id,
            "Transaction_Account" => $d_account,
            "Transaction_Type" => 'Debit',
            "Transaction_Amount" => $this->info['Note_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $logged_id,
            "created_logged_id" => $created_logged_id,
            "date_created" => $date_created,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_1, $is_actual);
        $data_2 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $note_id,
            "Transaction_Account" => $c_account,
            "Transaction_Type" => 'Credit',
            "Transaction_Amount" => $this->info['Note_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $logged_id,
            "created_logged_id" => $created_logged_id,
            "date_created" => $date_created,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_2, $is_actual);
        $data_3 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => $note_id,
            "Transaction_Account" => $sup_cust_code,
            "Transaction_Type" => $trans_type_2,
            "Transaction_Amount" => $total_paid_all,
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $logged_id,
            "created_logged_id" => $created_logged_id,
            "date_created" => $date_created,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_3, $is_actual);
        $data_4 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => 'credit',
            "Transaction_Account" => $this->info['Card_Account'],
            "Transaction_Type" => $trans_type_1,
            "Transaction_Amount" => $this->info['Card_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $logged_id,
            "created_logged_id" => $created_logged_id,
            "date_created" => $date_created,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_4, $is_actual);
        $data_5 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => 'cash',
            "Transaction_Account" => $this->info['Cash_Account'],
            "Transaction_Type" => $trans_type_1,
            "Transaction_Amount" => $this->info['Cash_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $logged_id,
            "created_logged_id" => $created_logged_id,
            "date_created" => $date_created,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_5, $is_actual);
        $data_6 = array(
            "JV_ID" => $jv,
            "Transaction_Reference" => 'check',
            "Transaction_Account" => $this->info['Check_Account'],
            "Transaction_Type" => $trans_type_1,
            "Transaction_Amount" => $this->info['Check_Amount'],
            "Transaction_Date" => $this->info['Note_Date'],
            "Currency_ID" => $this->info['Currency_ID'],
            "Logged_ID" => $logged_id,
            "created_logged_id" => $created_logged_id,
            "date_created" => $date_created,
            "Transaction_Description" => $desc,
            "Current_Ex_Rate" => $rate,
            "second_currency_rate" => $second_rate,
            "branch_id" => $this->info['branch_id'],
            "main_id" => $note_id,
            "main_type" => $ref,
            "job_id" => $this->info['job_id'],
            "project_id" => $this->info['project_id']
        );
        JV::addTransaction($data_6, $is_actual);
        $mydate = date('Y-m-d H:i:s');
        $arr_log = ['type' => $ref, 'main_id' => $note_id,
            'action' => 1, 'date_time' => $mydate,
            'user_id' => smc_session()['user_logged_id']];
        CommonFunctions::add_user_log($arr_log);
        if (isset($this->info['close_arr'])) {
            $rec_type = $type ? 'deb' : 'cre';
            $row_data = ['rate' => $rate];
            CommonBusiness::close_invoices($this->info['close_arr'], $note_id, $rec_type, $row_data);
        }
        $error = $this->db->error();
        if (empty($error)) {
            $this->db->query('COMMIT');
        } else {
            $this->db->query('ROLLBACK');
        }
        return array_unique($error);
    }

    public function delete_debit_credit_note($id, $is_debit) {
        if (smc_session()['year_closed'] == 1) {
            $error = array();
            $error[0] = CommonLanguage::getMessage('delete_year_alert');
            return $error;
        }
        $type = $is_debit ? 'deb' : 'cre';
        $query = $this->db->query('select Currency_ID,current_rate,Note_Date,is_actual
            from tbl_accounting_debit_credit_note where Note_ID="' . $id . '"');
        $row_note = $this->db->assoc($query);
        $old_rate = $row_note['current_rate'];
        $old_currency = $row_note['Currency_ID'];
         $check = CommonFunctions::check_lock_data(['type' => 'note', 'start_date' => $row_note['Note_Date'], 'is_actual' => $row_note['is_actual']]);
        if ($check !== '') {
            $error = array();
            $error[0] = $check;
            return $error;
        }
        $this->db->query('START TRANSACTION');
        
        CommonBusiness::return_inv_pur_close_amount($type, $old_currency, $old_rate, $id);

        $this->db->prepare_Stmt('delete from tbl_accounting_transactions 
            where main_type = "' . $type . '" and main_id = ?', [&$id]);
        $this->db->prepare_Stmt('delete from tbl_accounting_debit_credit_note where Note_ID = ?', [&$id]);
        $mydate = date('Y-m-d H:i:s');
        $arr_log = ['type' => $type, 'main_id' => $id,
            'action' => 2, 'date_time' => $mydate,
            'user_id' => smc_session()['user_logged_id']];
        CommonFunctions::add_user_log($arr_log);
        $error = $this->db->error();
        if (empty($error)) {
            $this->db->query('COMMIT');
        } else {
            $this->db->query('ROLLBACK');
        }
        return $error;
    }

}
