From d134569b90fce08ff3f93e35d2e3f18166560b1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maximilian=20Schr=C3=B6ger?= <m.schroeger@adito.de>
Date: Fri, 12 Oct 2018 17:14:40 +0200
Subject: [PATCH] =?UTF-8?q?Vertragsnummer=20und=20Produktnummer=20m=C3=BCs?=
 =?UTF-8?q?sen=20eindeutig=20sein=20(DB-Constraint=20+=20Validierung=20in?=
 =?UTF-8?q?=20Fields)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 aliasDefinition/Data_alias/Data_alias.aod         |  4 ++--
 entity/Contract_entity/Contract_entity.aod        |  1 +
 .../entityfields/contractcode/onValidation.js     | 11 +++++++++++
 .../entityfields/productcode/onValidation.js      | 15 +++++++--------
 .../_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod   |  9 ++++++---
 language/_____LANGUAGE_de/_____LANGUAGE_de.aod    | 12 ++++++++----
 language/_____LANGUAGE_en/_____LANGUAGE_en.aod    |  9 ++++++---
 others/db_changes/masterChangelog.xml             |  2 ++
 others/db_changes/struct/contractChanges.xml      |  7 +++++++
 others/db_changes/struct/productChanges.xml       |  9 +++++++++
 10 files changed, 59 insertions(+), 20 deletions(-)
 create mode 100644 entity/Contract_entity/entityfields/contractcode/onValidation.js
 create mode 100644 others/db_changes/struct/contractChanges.xml
 create mode 100644 others/db_changes/struct/productChanges.xml

diff --git a/aliasDefinition/Data_alias/Data_alias.aod b/aliasDefinition/Data_alias/Data_alias.aod
index 4ae9c67736e..fb04b3c8a91 100644
--- a/aliasDefinition/Data_alias/Data_alias.aod
+++ b/aliasDefinition/Data_alias/Data_alias.aod
@@ -1280,7 +1280,7 @@
                 <scale v="0" />
                 <notNull v="false" />
                 <isUnique v="false" />
-                <index v="false" />
+                <index v="true" />
                 <title></title>
                 <description></description>
               </entityFieldDb>
@@ -1481,7 +1481,7 @@
                 <scale v="0" />
                 <notNull v="false" />
                 <isUnique v="false" />
-                <index v="false" />
+                <index v="true" />
                 <title></title>
                 <description></description>
               </entityFieldDb>
diff --git a/entity/Contract_entity/Contract_entity.aod b/entity/Contract_entity/Contract_entity.aod
index 4678c726e36..24d0ce85178 100644
--- a/entity/Contract_entity/Contract_entity.aod
+++ b/entity/Contract_entity/Contract_entity.aod
@@ -13,6 +13,7 @@
       <tableName>CONTRACT</tableName>
       <columnName>CONTRACTCODE</columnName>
       <caption>Contract number</caption>
+      <onValidation>%aditoprj%/entity/Contract_entity/entityfields/contractcode/onValidation.js</onValidation>
     </entityField>
     <entityField>
       <name>CONTRACTDUE</name>
diff --git a/entity/Contract_entity/entityfields/contractcode/onValidation.js b/entity/Contract_entity/entityfields/contractcode/onValidation.js
new file mode 100644
index 00000000000..5b26ae2f6e7
--- /dev/null
+++ b/entity/Contract_entity/entityfields/contractcode/onValidation.js
@@ -0,0 +1,11 @@
+import("system.translate");
+import("system.result");
+import("system.vars");
+import("system.db");
+
+var codeCount = db.cell("select count(CONTRACTCODE) from CONTRACT where CONTRACTCODE = '" + vars.get("$local.value") + "'"
+                       + " and CONTRACTID <> '" + vars.get("$field.CONTRACTID") + "'");
+if(codeCount > 0)
+{
+    result.string(translate.text("The contract number already exists!"));
+}
\ No newline at end of file
diff --git a/entity/Product_entity/entityfields/productcode/onValidation.js b/entity/Product_entity/entityfields/productcode/onValidation.js
index d04278044de..5de6f5400bb 100644
--- a/entity/Product_entity/entityfields/productcode/onValidation.js
+++ b/entity/Product_entity/entityfields/productcode/onValidation.js
@@ -1,12 +1,11 @@
-import("system.db");
-import("system.vars");
-import("system.swing");
-import("system.result");
 import("system.translate");
+import("system.result");
+import("system.vars");
+import("system.db");
 
-//Productcode has to be unique
-if(db.cell("select count(PRODUCTID) from PRODUCT where PRODUCTCODE = '" + vars.get("$local.value")
-    + "' and PRODUCTID != '" + vars.get("$field.PRODUCTID") + "'") != 0)
+var codeCount = db.cell("select count(PRODUCTCODE) from PRODUCT where PRODUCTCODE = '" + vars.get("$local.value") + "'"
+                       + " and PRODUCTID <> '" + vars.get("$field.PRODUCTID") + "'");
+if(codeCount > 0)
 {
-    result.string( translate.text("The product number exists already!") );
+    result.string(translate.text("The product number already exists!"));
 }
\ No newline at end of file
diff --git a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
index b51d31b0de6..85d20120736 100644
--- a/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
+++ b/language/_____LANGUAGE_EXTRA/_____LANGUAGE_EXTRA.aod
@@ -378,9 +378,6 @@
     <entry>
       <key>Contract number</key>
     </entry>
-    <entry>
-      <key>The product number exists already!</key>
-    </entry>
     <entry>
       <key>Min. stock</key>
     </entry>
@@ -531,6 +528,12 @@
     <entry>
       <key>Reference</key>
     </entry>
+    <entry>
+      <key>The contract number already exists!</key>
+    </entry>
+    <entry>
+      <key>The product number already exists!</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
   <sqlModels>
diff --git a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
index 1e584c40ab0..bc03fe3d328 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -495,10 +495,6 @@
       <key>Contract number</key>
       <value>Vertragsnummer</value>
     </entry>
-    <entry>
-      <key>The product number exists already!</key>
-      <value>Die Produktnummer exisitiert bereits!</value>
-    </entry>
     <entry>
       <key>Min. stock</key>
       <value>Min.-Bestand</value>
@@ -698,6 +694,14 @@
       <key>Reference</key>
       <value>Referenz</value>
     </entry>
+    <entry>
+      <key>The contract number already exists!</key>
+      <value>Die Vertragsnummer existiert bereits!</value>
+    </entry>
+    <entry>
+      <key>The product number already exists!</key>
+      <value>Die Produktnummer existiert bereits!</value>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
index 4c60221d567..23910819b7d 100644
--- a/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
+++ b/language/_____LANGUAGE_en/_____LANGUAGE_en.aod
@@ -384,9 +384,6 @@
     <entry>
       <key>Contract number</key>
     </entry>
-    <entry>
-      <key>The product number exists already!</key>
-    </entry>
     <entry>
       <key>Min. stock</key>
     </entry>
@@ -539,6 +536,12 @@
     <entry>
       <key>Reference</key>
     </entry>
+    <entry>
+      <key>The contract number already exists!</key>
+    </entry>
+    <entry>
+      <key>The product number already exists!</key>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/others/db_changes/masterChangelog.xml b/others/db_changes/masterChangelog.xml
index 20602decea6..ee76531830e 100644
--- a/others/db_changes/masterChangelog.xml
+++ b/others/db_changes/masterChangelog.xml
@@ -13,6 +13,8 @@
     <include file="struct/create_prod2prod.xml"/>
     <include file="struct/create_productprice.xml"/>
     <include file="struct/create_stock.xml"/>
+    <include file="struct/contractChanges.xml"/>
+    <include file="struct/productChanges.xml"/>
     <include file="misc/1535533490181_persChanges.xml"/>
     <include file="struct/1535612802325_addressChange.xml"/>
     <include file="data/example_org/ORG_privat.xml"/>
diff --git a/others/db_changes/struct/contractChanges.xml b/others/db_changes/struct/contractChanges.xml
new file mode 100644
index 00000000000..f7f2e2c301f
--- /dev/null
+++ b/others/db_changes/struct/contractChanges.xml
@@ -0,0 +1,7 @@
+<?xml version="1.1" encoding="UTF-8" standalone="no"?>
+<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
+    <changeSet author="m.schroeger" id="eae40a20-2a4a-4a96-99f6-b66fb9f5e6c9">
+        <addUniqueConstraint tableName="CONTRACT" columnNames="CONTRACTCODE" constraintName="unique_CONTRACT_CONTRACTCODE"/>
+
+    </changeSet>
+</databaseChangeLog>
\ No newline at end of file
diff --git a/others/db_changes/struct/productChanges.xml b/others/db_changes/struct/productChanges.xml
new file mode 100644
index 00000000000..1c5f418ec68
--- /dev/null
+++ b/others/db_changes/struct/productChanges.xml
@@ -0,0 +1,9 @@
+<?xml version="1.1" encoding="UTF-8" standalone="no"?>
+<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
+    <changeSet author="m.schroeger" id="9d7715ea-da67-4579-b591-ab4cd871e320">
+        <addUniqueConstraint tableName="PRODUCT" columnNames="PRODUCTCODE" constraintName="unique_PRODUCT_PRODUCTCODE"/>
+        <rollback>
+            <dropUniqueConstraint tableName="PRODUCT" constraintName="unique_PRODUCT_PRODUCTCODE"/>
+        </rollback>
+    </changeSet>
+</databaseChangeLog>
\ No newline at end of file
-- 
GitLab