<?php
class ModelPaymentMatera extends Model {
	
	public function getFullAccountDB() {
		$sql = "SELECT * FROM oc_matera_config";

		$query = $this->db->query($sql);

		return $query->row;
	}
	
	public function updateDataDB($data = null, $fullAccount = null) {

		$cnpj = preg_replace('/[^a-zA-Z0-9]/', '', $data['matera_account_info']['cnpj']);
		$oldCnpj = preg_replace('/[^a-zA-Z0-9]/', '', $fullAccount['cnpj']);
		$cep = preg_replace('/[^a-zA-Z0-9]/', '', $data['matera_billing_address']['cep']);
		$accountInfo = $data['matera_account_info'];
		$billingAddressInfo = $data['matera_billing_address'];
		$materaTerm = $data['matera_term'] ?? '';

		$sql = "UPDATE oc_matera_config SET"
		." `cnpj`                 = '" . $cnpj . "',"
		." `account_id`           = '" . $accountInfo['account_id'] . "',"
		." `social_reason`        = '" . $accountInfo['razao_social'] . "',"
		." `email`                = '" . $accountInfo['email'] . "',"
		." `tax_type`             = " . (int)$accountInfo['ec_tx_tipo'] . ","
		." `tax_value`            = " . (float)$accountInfo['ec_tx_valor'] . ","
		." `tax_percent`          = " . (float)$accountInfo['ec_tx_percentual'] . ","
		." `tax_percent_min`      = " . (float)$accountInfo['ec_tx_percentual_vlr_min'] . ","
		." `tax_percent_max`      = " . (float)$accountInfo['ec_tx_percentual_vlr_max'] . ","
		." `address`              = '" . addslashes($billingAddressInfo['logradouro']) . "',"
		." `address_number`       = " . (int)$billingAddressInfo['numero'] . ","
		." `address_city`         = '" . addslashes($billingAddressInfo['cidade']) . "',"
		." `address_state`        = '" . $billingAddressInfo['estado'] . "',"
		." `address_district`     = '" . addslashes($billingAddressInfo['bairro']) . "',"
		." `address_cep`          = '" . $cep . "'";
		
		if ($materaTerm) {
			$sql .= ", `term` = '$materaTerm'";
		}

		if (isset($accountInfo['ec_tx_faixa_valor'])) {
			$sql .= ", `tax_range_value` = '" . json_encode($accountInfo['ec_tx_faixa_valor']) . "'";
		}
		
		$sql .= " WHERE `cnpj` = '$oldCnpj'";

		$query = $this->db->query($sql);

		return $query;
	}

	public function saveDataDB($data = null) {

		$cnpj = preg_replace('/[^a-zA-Z0-9]/', '', $data['matera_account_info']['cnpj']);
		$cep = preg_replace('/[^a-zA-Z0-9]/', '', $data['matera_billing_address']['cep']);
		$accountInfo = $data['matera_account_info'];
		$billingAddressInfo = $data['matera_billing_address'];
		$materaTerm = $data['matera_term'] ?? '';
		
		$sql = "INSERT INTO oc_matera_config SET"
        ." `cnpj`                 = '" . $cnpj . "',"
		." `account_id`           = '" . $accountInfo['account_id'] . "',"
		." `social_reason`        = '" . $accountInfo['razao_social'] . "',"
		." `email`                = '" . $accountInfo['email'] . "',"
		." `tax_type`             = " . (int)$accountInfo['ec_tx_tipo'] . ","
		." `tax_value`            = " . (float)$accountInfo['ec_tx_valor'] . ","
		." `tax_percent`          = " . (float)$accountInfo['ec_tx_percentual'] . ","
		." `tax_percent_min`      = " . (float)$accountInfo['ec_tx_percentual_vlr_min'] . ","
		." `tax_percent_max`      = " . (float)$accountInfo['ec_tx_percentual_vlr_max'] . ","
		." `address`              = '" . addslashes($billingAddressInfo['logradouro']) . "',"
		." `address_number`       = " . (int)$billingAddressInfo['numero'] . ","
		." `address_city`         = '" . addslashes($billingAddressInfo['cidade']) . "',"
		." `address_state`        = '" . $billingAddressInfo['estado'] . "',"
		." `address_district`     = '" . addslashes($billingAddressInfo['bairro']) . "',"
		." `address_cep`          = '" . $cep . "',"
		." `term`                 = '" . $materaTerm . "'";
	
		if (isset($accountInfo['ec_tx_faixa_valor'])) {
			$sql .= ", `tax_range_value` = '" . json_encode($accountInfo['ec_tx_faixa_valor']) . "'";
		}
			
		  $query = $this->db->query($sql);

		return $query;
	}

	public function deleteDataDB() {
		$sql = "DELETE FROM oc_matera_config";

		$query = $this->db->query($sql);

		return $query->row;
	}
}