<commit_before><?php
/**
 * Document_Incoming_Invoice class
 *
 * @author <NAME> <<EMAIL>>
 */

use \Skeleton\Database\Database;

class Document_Incoming_Invoice extends Document {

	/**
	 * Local details
	 *
	 * @access private
	 * @var array $local_details
	 */
	private $local_details = [];

	/**
	 * Get details
	 *
	 * @access protected
	 */
	protected function get_details() {
		parent::get_details();
		$db = Database::Get();

		$local_details = $db->get_row('SELECT * FROM document_incoming_invoice WHERE document_id=?', [ $this->id ]);
		if ($local_details === null) {
			$this->local_details = array();
		}

		$this->local_details = $local_details;
	}

	/**
	 * Change classname
	 *
	 * @access public
	 * @param string $classname
	 */
	public function change_classname($classname) {
		if ($classname == $this->classname) {
			return $this;
		}
		$db = Database::get();
		$db->query('DELETE FROM document_incoming_invoice WHERE document_id=?', [ $this->id ]);
		return parent::change_classname($classname);
	}

	/**
	 * Delete
	 *
	 * @access public
	 */
	public function delete() {
		$db = Database::get();
		$db->query('DELETE FROM document_incoming_invoice WHERE document_id=?', [ $this->id ]);
		parent::delete();
	}

	/**
	 * Get all transfers for this invoice
	 *
	 * @access public
	 * @return array $transfers
	 */
	public function get_transfers() {
		return Transfer::get_by_object($this);
	}

	/**
	 * Validate document data
	 *
	 * @access public
	 * @param array $errors
	 * @return bool $validated
	 */
	public function validate(&$errors) {
		$errors = [];
		parent::validate($parent_errors);

		$required_fields = [ 'supplier_id', 'expiration_date', 'price_incl', 'price_excl' ];
		foreach ($required_fields as $required_field) {
			if (!isset($this->local_details[$required_field]) OR $this->local_details[$required_field] == '') {
				$errors[$required_field] = 'required';
			}
		}

		if (!empty($this->local_details['payment_structured_message'])) {
			$parts = explode('/', $this->local_details['payment_structured_message']);
			if (count($parts) != 3) {
				$errors['payment_structured_message'] = 'incorrect';
			} elseif (strlen($parts[0]) != 3 OR strlen($parts[1]) != 4 OR strlen($parts[2]) != 5) {
				$errors['payment_structured_message'] = 'incorrect';
			} else {
				$number = $this->local_details['payment_structured_message'];
				$number = str_replace('/', '', $number);
				$modulus = substr($number, -2);
				$number = substr($number, 0, 10);
				if ($number % 97 != $modulus) {
					$errors['payment_structured_message'] = 'incorrect';
				}
			}
		}

		if (!empty($this->details['supplier_id']) and !empty($this->details['supplier_identifier']) and !empty($this->details['price_excl'])) {
			$supplier = Supplier::get_by_id($this->details['supplier_id']);
			$invoices = self::get_by_supplier_supplier_identifier($supplier, $this->details['supplier_identifier']);
			foreach ($invoices as $key => $invoice) {
				if ($invoice->id == $this->id) {
					unset($invoices[$key]);
				}<commit_msg>bugfix for OGM validation<commit_after><?php
/**
 * Document_Incoming_Invoice class
 *
 * @author <NAME> <<EMAIL>>
 */

use \Skeleton\Database\Database;

class Document_Incoming_Invoice extends Document {

	/**
	 * Local details
	 *
	 * @access private
	 * @var array $local_details
	 */
	private $local_details = [];

	/**
	 * Get details
	 *
	 * @access protected
	 */
	protected function get_details() {
		parent::get_details();
		$db = Database::Get();

		$local_details = $db->get_row('SELECT * FROM document_incoming_invoice WHERE document_id=?', [ $this->id ]);
		if ($local_details === null) {
			$this->local_details = array();
		}

		$this->local_details = $local_details;
	}

	/**
	 * Change classname
	 *
	 * @access public
	 * @param string $classname
	 */
	public function change_classname($classname) {
		if ($classname == $this->classname) {
			return $this;
		}
		$db = Database::get();
		$db->query('DELETE FROM document_incoming_invoice WHERE document_id=?', [ $this->id ]);
		return parent::change_classname($classname);
	}

	/**
	 * Delete
	 *
	 * @access public
	 */
	public function delete() {
		$db = Database::get();
		$db->query('DELETE FROM document_incoming_invoice WHERE document_id=?', [ $this->id ]);
		parent::delete();
	}

	/**
	 * Get all transfers for this invoice
	 *
	 * @access public
	 * @return array $transfers
	 */
	public function get_transfers() {
		return Transfer::get_by_object($this);
	}

	/**
	 * Validate document data
	 *
	 * @access public
	 * @param array $errors
	 * @return bool $validated
	 */
	public function validate(&$errors) {
		$errors = [];
		parent::validate($parent_errors);

		$required_fields = [ 'supplier_id', 'expiration_date', 'price_incl', 'price_excl' ];
		foreach ($required_fields as $required_field) {
			if (!isset($this->local_details[$required_field]) OR $this->local_details[$required_field] == '') {
				$errors[$required_field] = 'required';
			}
		}

		if (!empty($this->local_details['payment_structured_message'])) {
			$parts = explode('/', $this->local_details['payment_structured_message']);
			if (count($parts) != 3) {
				$errors['payment_structured_message'] = 'incorrect';
			} elseif (strlen($parts[0]) != 3 OR strlen($parts[1]) != 4 OR strlen($parts[2]) != 5) {
				$errors['payment_structured_message'] = 'incorrect';
			} else {
				$number = $this->local_details['payment_structured_message'];
				$number = str_replace('/', '', $number);
				$modulus = substr($number, -2);
				$number = substr($number, 0, 10);

				$number_modulus = $number % 97;
				if ($number_modulus == 0) {
					$number_modulus = 97;
				}

				if ($number_modulus != $modulus) {
					$errors['payment_structured_message'] = 'incorrect';
				}
			}
		}

		if (!empty($this->details['supplier_id']) and !empty($this->details['supplier_identifier']) and !empty($this->details['price_excl'])) {
			$supplier = Supplier::get_by_id($this->details['supplier_id']);
			$invoices = self::get_by_supplier_supplier_identifier($supplier, $this->details['supplier_identifier']);
			foreach ($invoices as $key => $invoice) {
				if ($invoice->id == $this->id) {
					unset($invoices[$key]);
				}
{'fraction_non_alphanumeric': 0.17759121730707136, 'fraction_numerical': 0.004681950274459154, 'mean_word_length': 4.227848101265823, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, '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': '6705519', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2005, 'n_words': 394}