<commit_before><?
#should split to factory pattern with incoming and outgoing invoices.
includelogic('invoice/invoice');
includelogic('fakturabank/fakturabankvoting');
includelogic('exchange/exchange');

class lodo_fakturabank_fakturabank {
    private $host           = '';
    private $protocol       = '';
    private $username       = '';
    private $password       = '';
    private $login          = false;
    private $timeout        = 30; 
    private $retrievestatus = '';
    private $credentials    = '';
    private $OrgNumber      = '';
    public  $startexectime  = '';
    public  $stopexectime   = '';
    public  $diffexectime   = '';
    public  $error          = '';
    public  $success        = false;
    private $ArrayTag       = array(
                                 'Invoice'          => true,
                                 'cac:InvoiceLine'  => true,
                                 'InvoiceLine'      => true,
                                 'TaxSubtotal'      => true
                                );
    private $attributesOfInterest = array(
									 'schemeID',
									 'currencyID'
								 );

    function __construct() {
        global $_lib;
        $this->startexectime  = microtime();

        $this->username         = $_lib['sess']->get_person('FakturabankUsername');
        $this->password         = $_lib['sess']->get_person('<PASSWORD>Password');
        $this->retrievestatus   = $_lib['setup']->get_value('fakturabank.status');

        $this->host = $GLOBALS['_SETUP']['FB_SERVER'];
        $this->protocol = $GLOBALS['_SETUP']['FB_SERVER_PROTOCOL'];

        if(is_array($args)) {
            foreach($args as $key => $value) {
                $this->{$key} = $value;
            }
        }

        if(!$this->username || !$this->username) {
            $_lib['message']->add("Fakturabank brukernavn og passord er ikke definert p&aring; brukeren din");
        } else {
            $this->login = true;
        }

        $old_pattern    = array("/[^0-9]/", "/_+/", "/_$/");
        $new_pattern    = array("", "", "");
        $this->OrgNumber= strtolower(preg_replace($old_pattern, $new_pattern , $_lib['sess']->get_companydef('OrgNumber'))); 

        $this->credentials = "$this->username:$this->password";
    }

    function __destruct() {
        $this->stopexectime   = microtime();
        $this->diffexectime   = $this->stopexectime - $this->startexectime;
    }
    
    ####################################################################################################
    #Get a list of all outgoing invoices from fakturabank
    public function outgoing() {
        global $_lib;
        #https://fakturabank.no/invoices/outgoing.xml?orgnr=981951271

        $page       = "invoices/outgoing.xml";
        $params     = "?orgnr=$this->OrgNumber";
        $params     .= "&supplier_status=created"; #Only retrieve with status 'created'
        $params     .= "&order=invoiceno";
        
        $url    = "$this->protocol://$this->host/$page$params";
        $_lib['sess']->debug($url);

        $invoicesO = $this->retrieve($page, $url);
		$validated_invoices = $this->validate_outgoing($invoicesO);
		$this->save_outgoing_w_voting($validated_invoices);
        return $validated_invoices;
    }

    ####################################################################################################
    #Get a list of all incoming invoices from fakturabank
    public function incoming() {
        global $_lib;
        #https://fakturabank.no/invoices?orgnr=981951271

        $page       = "invoices";
        $params     = "?orgnr=" . $this->OrgNumber . '&order=issue_date';
        if($this->retrievestatus) $params .= '&customer_status=' . $this->retrievestatus;
        $url    = "$this->protocol://$this->host/$page$params";
        $_lib['message']->add($url);<commit_msg>adjust to changes in fb: specify num rows<commit_after><?
#should split to factory pattern with incoming and outgoing invoices.
includelogic('invoice/invoice');
includelogic('fakturabank/fakturabankvoting');
includelogic('exchange/exchange');

class lodo_fakturabank_fakturabank {
    private $host           = '';
    private $protocol       = '';
    private $username       = '';
    private $password       = '';
    private $login          = false;
    private $timeout        = 30; 
    private $retrievestatus = '';
    private $credentials    = '';
    private $OrgNumber      = '';
    public  $startexectime  = '';
    public  $stopexectime   = '';
    public  $diffexectime   = '';
    public  $error          = '';
    public  $success        = false;
    private $ArrayTag       = array(
                                 'Invoice'          => true,
                                 'cac:InvoiceLine'  => true,
                                 'InvoiceLine'      => true,
                                 'TaxSubtotal'      => true
                                );
    private $attributesOfInterest = array(
									 'schemeID',
									 'currencyID'
								 );

    function __construct() {
        global $_lib;
        $this->startexectime  = microtime();

        $this->username         = $_lib['sess']->get_person('FakturabankUsername');
        $this->password         = $_lib['sess']->get_person('<PASSWORD>');
        $this->retrievestatus   = $_lib['setup']->get_value('fakturabank.status');

        $this->host = $GLOBALS['_SETUP']['FB_SERVER'];
        $this->protocol = $GLOBALS['_SETUP']['FB_SERVER_PROTOCOL'];

        if(is_array($args)) {
            foreach($args as $key => $value) {
                $this->{$key} = $value;
            }
        }

        if(!$this->username || !$this->username) {
            $_lib['message']->add("Fakturabank brukernavn og passord er ikke definert p&aring; brukeren din");
        } else {
            $this->login = true;
        }

        $old_pattern    = array("/[^0-9]/", "/_+/", "/_$/");
        $new_pattern    = array("", "", "");
        $this->OrgNumber= strtolower(preg_replace($old_pattern, $new_pattern , $_lib['sess']->get_companydef('OrgNumber'))); 

        $this->credentials = "$this->username:$this->password";
    }

    function __destruct() {
        $this->stopexectime   = microtime();
        $this->diffexectime   = $this->stopexectime - $this->startexectime;
    }
    
    ####################################################################################################
    #Get a list of all outgoing invoices from fakturabank
    public function outgoing() {
        global $_lib;
        #https://fakturabank.no/invoices/outgoing.xml?orgnr=981951271

        $page       = "invoices/outgoing.xml";

        $params     = "?rows=1000&orgnr=$this->OrgNumber"; // add top limit rows=1000, otherwise we only get one record
        $params     .= "&supplier_status=created"; #Only retrieve with status 'created'
        $params     .= "&order=invoiceno";
        
        $url    = "$this->protocol://$this->host/$page$params";
        $_lib['sess']->debug($url);

        $invoicesO = $this->retrieve($page, $url);
		$validated_invoices = $this->validate_outgoing($invoicesO);
		$this->save_outgoing_w_voting($validated_invoices);
        return $validated_invoices;
    }

    ####################################################################################################
    #Get a list of all incoming invoices from fakturabank
    public function incoming() {
        global $_lib;
        #https://fakturabank.no/invoices?orgnr=981951271

        $page       = "invoices";
        $params     = "?rows=1000&orgnr=" . $this->OrgNumber . '&order=issue_date'; // add top limit rows=1000, otherwise we only get one record
        if($this->retrievestatus) $params .= '&customer_status=' . $this->retrievestatus;
        $url    = "$this->protocol://$this->host/$page$params";
        $_lib['message']->add($url);
{'fraction_non_alphanumeric': 0.19584231208011155, 'fraction_numerical': 0.007605526682722779, 'mean_word_length': 2.2725010369141434, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4815847', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2295, 'n_words': 396}