<commit_before><?php

/*
 * fakturabankscheme:
 *  FakturabankSchemeID : table index
 *  FakturabankRemoteSchemeID : id of scheme used in fakturabank
 *  SchemeType: type of scheme, i.e. NO:ORG
 *
 *
 * accountplanscheme:
 *  AccountPlanSchemeID: table index
 *  AccountPlanID: reference to accountplan table
 *  FakturabankSchemeID: reference to fakturabankscheme table
 *  SchemeValue: lookup value of a give fakturabankscheme.
 *               i.e. for NO:ORG fakturabankscheme this could be 12345678
 *
 */

class lodo_accountplan_scheme {
    /**
     * List available scheme types. I.e. list every entry in fakturabankscheme
     */
    static function listAvailableTypes() {
        global $_lib;

        $l = array();

        $r = $_lib['db']->db_query("SELECT * FROM fakturabankscheme");
        while( ($row = $_lib['db']->db_fetch_assoc($r)) ) {
            $l[] = $row;
        }

        return $l;
    }

    /**
     * Add a new scheme type
     *
     * $remoteid is the ID used in fakturabank
     * $type is the scheme type, i.e. NO:ORG
     */
    static function addSchemeType($remoteid, $type) {
        global $_lib;

        $q = sprintf("INSERT INTO `fakturabankscheme` (`FakturabankRemoteSchemeID`, `SchemeType`) VALUES (%d, %d);",
                     $remoteid, $type);

        $_lib['db']->db_query($q);
        return $_lib['db']->db_insert_id();
    }

    /**
     * Delete a scheme
     *
     * FakturabankSchemeID is the index in table to delete
     */
    static function deleteSchemeType($FakturabankSchemeID) {
        global $_lib;

        $q = sprintf("DELETE FROM fakturabankscheme WHERE FakturabankSchemeID = %d",
                     $FakturabankSchemeID);
        $_lib['db']->db_query($q);
    }

    /**
     * Find an accountplanid associated with a scheme type and value
     * i.e. findAccountPlan("NO:ORG", "12345678")
     */
    static function findAccountPlan($schemetype, $schemevalue) {
        global $_lib;

        $schemetype = trim($schemetype);
        $schemevalue = trim($schemevalue);

        if($schemevalue == "")
            return null;

        $q = sprintf("SELECT a.AccountPlanID as AccountPlanID
                        FROM accountplanscheme a,
                             fakturabankscheme f
                        WHERE
                          a.SchemeValue = '%s'
                          AND a.FakturabankSchemeID = f.FakturabankSchemeID
                          AND f.SchemeType = '%s'",
                     $_lib['db']->db_escape($schemevalue),
                     $_lib['db']->db_escape($schemetype));
        $r = $_lib['db']->db_query($q);
        $row = $_lib['db']->db_fetch_assoc($r);

        if($row)
            return $row['AccountPlanID'];
        else
            return null;
    }

    /**
     * Find an accountplanid given a scheme type and value + the type of the account.
     * findAccoutnPlanType("NO:ORG", "12345678", "supplier")
     */
    static function findAccountPlanType($schemetype, $schemevalue, $accountType) {
        global $_lib;

        $schemetype = trim($schemetype);
        $schemevalue = trim($schemevalue);

        if($schemevalue == "")
            return null;

        $q = sprintf("SELECT a.AccountPlanID as AccountPlanID
                        FROM accountplanscheme a,
                             fakturabankscheme f,
                             accountplan ap
                        WHERE
                          a.SchemeValue = '%s'
                          AND a.FakturabankSchemeID = f.FakturabankSchemeID
                          AND a.AccountPlanID = ap.AccountPlanID
                          AND f.SchemeType = '%s'
                          AND ap.AccountPlanType = '%s'",
                     $_lib['db']->db_escape($schemevalue),
                     $_lib['db']->db_escape($schemetype),
                     $_lib['db']->db_escape($accountType)
            );

        $r = $_lib['db']->db_query($q);
        $row = $_lib['db']->db_fetch_assoc($r);

        if($row)
            return $row['AccountPlanID'];
        else
            return null;
    }

    function refreshSchemes() {
        global $_lib;
        includelogic("fakturabank/fakturabank");
        $fakturabank = new lodo_fakturabank_fakturabank();

        $page = "identificators.json";
        $url = $fakturabank->construct_fakturabank_url($page);
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);

        $decoded_json = json_decode($result);

        foreach($decoded_json as $json_node) {
            $found = false;
            foreach($this->globalSchemes as $existing) {
                if($existing["FakturabankRemoteSchemeID"] == $json_node->identificator->id) {
                    $found = true;
                    break;
                }
            }
<commit_msg>Change fetching identificator schemes back to regular curl<commit_after><?php

/*
 * fakturabankscheme:
 *  FakturabankSchemeID : table index
 *  FakturabankRemoteSchemeID : id of scheme used in fakturabank
 *  SchemeType: type of scheme, i.e. NO:ORG
 *
 *
 * accountplanscheme:
 *  AccountPlanSchemeID: table index
 *  AccountPlanID: reference to accountplan table
 *  FakturabankSchemeID: reference to fakturabankscheme table
 *  SchemeValue: lookup value of a give fakturabankscheme.
 *               i.e. for NO:ORG fakturabankscheme this could be 12345678
 *
 */

class lodo_accountplan_scheme {
    /**
     * List available scheme types. I.e. list every entry in fakturabankscheme
     */
    static function listAvailableTypes() {
        global $_lib;

        $l = array();

        $r = $_lib['db']->db_query("SELECT * FROM fakturabankscheme");
        while( ($row = $_lib['db']->db_fetch_assoc($r)) ) {
            $l[] = $row;
        }

        return $l;
    }

    /**
     * Add a new scheme type
     *
     * $remoteid is the ID used in fakturabank
     * $type is the scheme type, i.e. NO:ORG
     */
    static function addSchemeType($remoteid, $type) {
        global $_lib;

        $q = sprintf("INSERT INTO `fakturabankscheme` (`FakturabankRemoteSchemeID`, `SchemeType`) VALUES (%d, %d);",
                     $remoteid, $type);

        $_lib['db']->db_query($q);
        return $_lib['db']->db_insert_id();
    }

    /**
     * Delete a scheme
     *
     * FakturabankSchemeID is the index in table to delete
     */
    static function deleteSchemeType($FakturabankSchemeID) {
        global $_lib;

        $q = sprintf("DELETE FROM fakturabankscheme WHERE FakturabankSchemeID = %d",
                     $FakturabankSchemeID);
        $_lib['db']->db_query($q);
    }

    /**
     * Find an accountplanid associated with a scheme type and value
     * i.e. findAccountPlan("NO:ORG", "12345678")
     */
    static function findAccountPlan($schemetype, $schemevalue) {
        global $_lib;

        $schemetype = trim($schemetype);
        $schemevalue = trim($schemevalue);

        if($schemevalue == "")
            return null;

        $q = sprintf("SELECT a.AccountPlanID as AccountPlanID
                        FROM accountplanscheme a,
                             fakturabankscheme f
                        WHERE
                          a.SchemeValue = '%s'
                          AND a.FakturabankSchemeID = f.FakturabankSchemeID
                          AND f.SchemeType = '%s'",
                     $_lib['db']->db_escape($schemevalue),
                     $_lib['db']->db_escape($schemetype));
        $r = $_lib['db']->db_query($q);
        $row = $_lib['db']->db_fetch_assoc($r);

        if($row)
            return $row['AccountPlanID'];
        else
            return null;
    }

    /**
     * Find an accountplanid given a scheme type and value + the type of the account.
     * findAccoutnPlanType("NO:ORG", "12345678", "supplier")
     */
    static function findAccountPlanType($schemetype, $schemevalue, $accountType) {
        global $_lib;

        $schemetype = trim($schemetype);
        $schemevalue = trim($schemevalue);

        if($schemevalue == "")
            return null;

        $q = sprintf("SELECT a.AccountPlanID as AccountPlanID
                        FROM accountplanscheme a,
                             fakturabankscheme f,
                             accountplan ap
                        WHERE
                          a.SchemeValue = '%s'
                          AND a.FakturabankSchemeID = f.FakturabankSchemeID
                          AND a.AccountPlanID = ap.AccountPlanID
                          AND f.SchemeType = '%s'
                          AND ap.AccountPlanType = '%s'",
                     $_lib['db']->db_escape($schemevalue),
                     $_lib['db']->db_escape($schemetype),
                     $_lib['db']->db_escape($accountType)
            );

        $r = $_lib['db']->db_query($q);
        $row = $_lib['db']->db_fetch_assoc($r);

        if($row)
            return $row['AccountPlanID'];
        else
            return null;
    }

    function refreshSchemes() {
        global $_lib;
        includelogic("fakturabank/fakturabank");
        $fakturabank = new lodo_fakturabank_fakturabank();

        $page = "rest/identificators.json";
        $url = $fakturabank->construct_fakturabank_url($page);
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);

        $decoded_json = json_decode($result);

        foreach($decoded_json as $json_node) {
            $found = false;
            foreach($this->globalSchemes as $existing) {
                if($existing["FakturabankRemoteSchemeID"] == $json_node->identificator->id) {
                    $found = true;
                    break;
                }
            }

{'fraction_non_alphanumeric': 0.11813627254509018, 'fraction_numerical': 0.0052104208416833666, 'mean_word_length': 1.8234794908062235, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1357588', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2972, 'n_words': 625}