From 56cb5df1df1275d9adcbc9423f808e4321a48098 Mon Sep 17 00:00:00 2001
From: Neda Moeini <neda.moeini@geant.org>
Date: Thu, 9 Jan 2025 15:51:13 +0100
Subject: [PATCH] Add Sage SQL Server DB as Read-Only Database and Create
 Django Models

---
 requirements.txt                         |   1 +
 sage_validation/file_validator/models.py | 142 ++++++++++++++++++++++-
 sage_validation/settings.py              |  14 ++-
 3 files changed, 155 insertions(+), 2 deletions(-)

diff --git a/requirements.txt b/requirements.txt
index 791ff60..09b5752 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,3 +4,4 @@ mypy
 tox
 sphinx
 sphinx-autodoc-typehints
+mssql-django
diff --git a/sage_validation/file_validator/models.py b/sage_validation/file_validator/models.py
index 5fd99e9..0bc48ec 100644
--- a/sage_validation/file_validator/models.py
+++ b/sage_validation/file_validator/models.py
@@ -1 +1,141 @@
-"""Models for the file_validator app."""
+"""Models for MEO DB tables and views."""
+from django.db import models
+
+
+class PlAccountCodes(models.Model):
+    """Represents profit and loss (PL) account codes.
+
+    This model maps the PL account names to their respective codes.
+    """
+
+    objects = None
+    pl_account_name = models.TextField(db_column="PL_Account_Name")
+    pl_account_code = models.CharField(db_column="PL_Account_Code", primary_key=True, max_length=50)
+
+    class Meta:
+        """Metaclass for the PlAccountCodes model."""
+
+        managed = False
+        db_table = "PL_Account_Codes"
+
+
+class Meocostcentres(models.Model):
+    """Represents cost centres.
+
+    This model contains data related to cost centres,
+    including their names, types, and unique IDs.
+    """
+
+    cc = models.CharField(db_column="CC", max_length=3)
+    cc_name = models.CharField(db_column="CC_Name", max_length=50, blank=True, null=True)
+    cctype = models.CharField(db_column="CCType", max_length=8)
+    id = models.IntegerField(db_column="ID", primary_key=True)
+
+    class Meta:
+        """Metaclass for the Meocostcentres model."""
+
+        managed = False
+        db_table = "meoCostCentres"
+
+
+class Meonominal(models.Model):
+    """View for MEO nominal codes."""
+
+    nom = models.CharField(db_column="Nom", max_length=5)
+    nomname = models.CharField(db_column="NomName", max_length=60, blank=True, null=True)
+    nomid = models.IntegerField(db_column="NomID")
+
+    class Meta:
+        """Metaclass for the Meonominal model."""
+
+        managed = False
+        db_table = "meoNominal"
+
+
+class Meovalidsageaccounts(models.Model):
+    """View for MEO valid Sage accounts."""
+
+    accountname = models.CharField(db_column="AccountName", max_length=60)
+    accountnumber = models.CharField(db_column="AccountNumber", max_length=8, blank=True, null=True)
+    accountcostcentre = models.CharField(db_column="AccountCostCentre", max_length=3, blank=True, null=True)
+    accountdepartment = models.CharField(db_column="AccountDepartment", max_length=3, blank=True, null=True)
+
+    class Meta:
+        """Metaclass for the Meovalidsageaccounts model."""
+
+        managed = False
+        db_table = "meoValidSageAccounts"
+
+
+class Meovalidsuppliers(models.Model):
+    """View for MEO valid suppliers."""
+
+    supplieraccountnumber = models.CharField(db_column="SupplierAccountNumber", max_length=8)
+    supplieraccountname = models.CharField(db_column="SupplierAccountName", max_length=60)
+
+    class Meta:
+        """Metaclass for the Meovalidsuppliers model."""
+
+        managed = False
+        db_table = "meoValidSuppliers"
+
+
+class Meovalidvat(models.Model):
+    """View for MEO valid VAT codes."""
+
+    tax_code = models.SmallIntegerField(db_column="Tax code")
+    tax_code_label = models.CharField(db_column="tax code label", max_length=60)
+    rate = models.DecimalField(db_column="Rate", max_digits=18, decimal_places=2)
+    inputnominalaccount = models.CharField(db_column="InputNominalAccount", max_length=8, blank=True, null=True)
+
+    class Meta:
+        """Metaclass for the Meovalidvat model."""
+
+        managed = False
+        db_table = "meoValidVAT"
+
+
+class VwPlAcctCodes(models.Model):
+    """View for profit and loss account codes."""
+
+    pl_account_name = models.TextField(db_column="PL_Account_Name")
+    pl_account_code = models.CharField(db_column="PL_Account_Code", max_length=50)
+
+    class Meta:
+        """Metaclass for the VwPlAcctCodes model."""
+
+        managed = False
+        db_table = "vw_PL_Acct_Codes"
+
+
+class VwXxData(models.Model):
+    """provides a read-only view of XX data entries.
+
+    The entries are typically used for categorizing costs and activities.
+    """
+
+    description = models.CharField(db_column="Description", max_length=50)
+    xx_value = models.CharField(db_column="xx_Value", max_length=50)
+    project = models.SmallIntegerField(db_column="Project")
+    overhead = models.IntegerField(db_column="Overhead")
+
+    class Meta:
+        """Meta class for the VwXxData model."""
+
+        managed = False
+        db_table = "vw_xx-data"
+
+
+class XxData(models.Model):
+    """Model for XX data entries."""
+
+    description = models.CharField(db_column="Description", max_length=50)
+    xx_value = models.CharField(db_column="xx_Value", primary_key=True, max_length=50)
+    project = models.SmallIntegerField(db_column="Project")
+    overhead = models.IntegerField(db_column="Overhead")
+
+    class Meta:
+        """Metaclass for the XxData model."""
+
+        managed = False
+        db_table = "xx-data"
diff --git a/sage_validation/settings.py b/sage_validation/settings.py
index 5bf2163..9d50810 100644
--- a/sage_validation/settings.py
+++ b/sage_validation/settings.py
@@ -78,7 +78,19 @@ DATABASES = {
     "default": {
         "ENGINE": "django.db.backends.sqlite3",
         "NAME": BASE_DIR / "db.sqlite3",
-    }
+    },
+    "meo": {
+        'ENGINE': 'mssql',
+        'NAME': os.getenv("MSSQL_DB_NAME", ""),
+        'USER': os.getenv("MSSQL_DB_USER", ""),
+        'PASSWORD': os.getenv("MSSQL_DB_PASSWORD", ""),
+        'HOST': os.getenv("MSSQL_DB_HOST", "localhost"),
+        'PORT': os.getenv("MSSQL_DB_PORT", ""),
+
+        'OPTIONS': {
+            'driver': 'ODBC Driver 18 for SQL Server',
+        },
+    },
 }
 
 # Password validation
-- 
GitLab