From cc25d27624367bdd01ba99a285c08fad35dbf5bd Mon Sep 17 00:00:00 2001 From: BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> Date: Wed, 20 Oct 2021 11:42:33 +0200 Subject: [PATCH] WIP: Adding registry view --- .../controller/web/WebRegistryController.java | 40 +++++++++++++++++++ .../sil/erp/back/db/DatabaseSQLiteImpl.java | 37 +++++++++++++++++ .../fr/unistra/sil/erp/back/db/IDatabase.java | 6 +++ .../sil/erp/back/model/RegistryEntry.java | 8 ++-- src/main/resources/messages.properties | 16 ++++++++ src/main/resources/messages_en.properties | 16 ++++++++ src/main/resources/messages_fr.properties | 16 ++++++++ src/main/resources/templates/registry.html | 27 +++++++++++++ src/main/resources/templates/stocks.html | 30 ++++++++++++++ 9 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 src/main/java/fr/unistra/sil/erp/back/controller/web/WebRegistryController.java create mode 100644 src/main/resources/templates/registry.html create mode 100644 src/main/resources/templates/stocks.html diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/web/WebRegistryController.java b/src/main/java/fr/unistra/sil/erp/back/controller/web/WebRegistryController.java new file mode 100644 index 0000000..05c62b6 --- /dev/null +++ b/src/main/java/fr/unistra/sil/erp/back/controller/web/WebRegistryController.java @@ -0,0 +1,40 @@ +/* + * CONTRAT DE LICENCE DE LOGICIEL LIBRE CECILL-B + * https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html + */ +package fr.unistra.sil.erp.back.controller.web; + +import fr.unistra.sil.erp.back.DatabaseSystem; +import fr.unistra.sil.erp.back.db.DatabaseConnectionException; +import fr.unistra.sil.erp.back.model.RegistryEntry; +import fr.unistra.sil.erp.back.model.Stock; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; + +/** + * Web controller for the registry display page. + * @author antoine.beauvais + */ +@Controller +public class WebRegistryController { + + /** + * Returns the registry view for the Web interface. + * @param model the model to populate the template with. + * @return the view. + */ + @GetMapping("registry") + public String stocks(Model model) { + try { + List<RegistryEntry> lr = DatabaseSystem.getInstance().getRegistry(); + model.addAttribute("stocks", lr); + } catch (DatabaseConnectionException ex) { + Logger.getLogger(WebRegistryController.class.getName()).log(Level.SEVERE, null, ex); + } + return "registry"; + } + +} diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java index 2479947..20a2f40 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java @@ -6,6 +6,7 @@ package fr.unistra.sil.erp.back.db; import fr.unistra.sil.erp.back.model.Category; import fr.unistra.sil.erp.back.model.Item; +import fr.unistra.sil.erp.back.model.RegistryEntry; import fr.unistra.sil.erp.back.model.Stock; import java.sql.Connection; import java.sql.DriverManager; @@ -13,7 +14,9 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -63,6 +66,14 @@ public class DatabaseSQLiteImpl implements IDatabase { private static final String SQL_GETSTOCKFORITEM = SQL_GETSTOCKS + " WHERE item = ?"; + /** + * Query used to retrieve the registry. + */ + private static final String SQL_GETREGISTRY = + "SELECT r.id AS id, r.dt AS date, r.type AS type, " + + "r.account_id AS account, r.debit AS debit, r.credit AS credit, " + + "r.remarks AS remarks FROM Registry r"; + private static final String SQL_UPDATESTOCK = "UPDATE stocks SET quantity = ? WHERE item = ?"; @@ -333,5 +344,31 @@ public class DatabaseSQLiteImpl implements IDatabase { throw new DatabaseUpdateException("Failed to execute query."); } } + + @Override + public List<RegistryEntry> getRegistry() throws DatabaseConnectionException { + ResultSet rs = this.query(SQL_GETREGISTRY); + if(rs == null) + return null; + + List<RegistryEntry> res = new ArrayList<>(); + try { + while(rs.next()) + { + RegistryEntry r = new RegistryEntry(rs.getInt("id"), + rs.getInt("type"), new Date(rs.getInt("date") * 1000), + rs.getInt("account"), + rs.getBigDecimal("debit"), rs.getBigDecimal("credit"), + rs.getString("remarks")); + res.add(r); + } + } catch (SQLException ex) { + Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( + Level.SEVERE, "Failed to fetch results.", ex); + return null; + } + + return res; + } } diff --git a/src/main/java/fr/unistra/sil/erp/back/db/IDatabase.java b/src/main/java/fr/unistra/sil/erp/back/db/IDatabase.java index 0f9666b..96ec66b 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/IDatabase.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/IDatabase.java @@ -6,6 +6,7 @@ package fr.unistra.sil.erp.back.db; import fr.unistra.sil.erp.back.model.Category; import fr.unistra.sil.erp.back.model.Item; +import fr.unistra.sil.erp.back.model.RegistryEntry; import fr.unistra.sil.erp.back.model.Stock; import java.util.List; @@ -65,4 +66,9 @@ public interface IDatabase { public void updateStock(int id, int quantity) throws DatabaseConnectionException, DatabaseUpdateException; + /** + * Returns all registry entries. + */ + public List<RegistryEntry> getRegistry() throws DatabaseConnectionException; + } diff --git a/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java b/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java index d3c10b2..73fabde 100644 --- a/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java +++ b/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java @@ -5,7 +5,7 @@ package fr.unistra.sil.erp.back.model; import java.math.BigDecimal; -import java.time.LocalDateTime; +import java.util.Date; /** * Entry in the Registry. @@ -30,7 +30,7 @@ public class RegistryEntry { /** * The date and time for this transaction. */ - private final LocalDateTime dt; + private final Date dt; /** * The ID of the associated account. @@ -62,7 +62,7 @@ public class RegistryEntry { * @param credit money for the credit. * @param remarks arbitrary remarks. */ - public RegistryEntry(int id, int transactionType, LocalDateTime dt, + public RegistryEntry(int id, int transactionType, Date dt, int accountId, BigDecimal debit, BigDecimal credit, String remarks) { @@ -97,7 +97,7 @@ public class RegistryEntry { * Returns the transaction's date and time. * @return the date and time. */ - public LocalDateTime getDatetime() + public Date getDatetime() { return this.dt; } diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 3f1dcdc..25749d6 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -20,3 +20,19 @@ web.ui.documents=Documents web.ui.price=Price #Price for subscribers. web.ui.subscriberPrice=Subscriber Price +#Stock +web.ui.stock=Stock +#Stocks +web.ui.stocks=Stocks +#Type transaction (achat, vente...) +web.ui.stock.type=Type +#Date label. +web.ui.stock.date=Date +#Financial Account +web.ui.stock.account=Account +#Debit amount label. +web.ui.stock.debit=Debit +#Credit amount label. +web.ui.stock.credit=Credit +#Arbitrary remarks for stock +web.ui.stock.remarks=Remarks diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 3f1dcdc..25749d6 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -20,3 +20,19 @@ web.ui.documents=Documents web.ui.price=Price #Price for subscribers. web.ui.subscriberPrice=Subscriber Price +#Stock +web.ui.stock=Stock +#Stocks +web.ui.stocks=Stocks +#Type transaction (achat, vente...) +web.ui.stock.type=Type +#Date label. +web.ui.stock.date=Date +#Financial Account +web.ui.stock.account=Account +#Debit amount label. +web.ui.stock.debit=Debit +#Credit amount label. +web.ui.stock.credit=Credit +#Arbitrary remarks for stock +web.ui.stock.remarks=Remarks diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index f9f08b4..d62bf94 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -20,3 +20,19 @@ web.ui.documents=Documents web.ui.price=Prix #Price for subscribers. web.ui.subscriberPrice=Prix adh\u00e9rent +#Stock +web.ui.stock=Stock +#Stocks +web.ui.stocks=Stocks +#Type transaction (achat, vente...) +web.ui.stock.type=Type +#Date label. +web.ui.stock.date=Date +#Financial Account +web.ui.stock.account=Compte +#Debit amount label. +web.ui.stock.debit=D\u00e9bit +#Credit amount label. +web.ui.stock.credit=Cr\u00e9dit +#Arbitrary remarks for stock +web.ui.stock.remarks=Commentaires diff --git a/src/main/resources/templates/registry.html b/src/main/resources/templates/registry.html new file mode 100644 index 0000000..7b92937 --- /dev/null +++ b/src/main/resources/templates/registry.html @@ -0,0 +1,27 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> + <head th:replace="fragments/head :: sharedHead (pageName=#{web.ui.products})"> + </head> + + <body> + <div th:replace="fragments/navbar :: topNavBar"></div> + <article> + <h1 th:text="#{web.ui.products}"></h1> + <div class="mainFrame" id="productsArea"> + <table class="mainList" id="productsList"> + <tr> + <th th:text="#{web.ui.product}"></th> + <th th:text="#{web.ui.price}"></th> + <th th:text="#{web.ui.subscriberPrice}"></th> + </tr> + + <tr th:each="item: ${items}"> + <td th:text="${item.name}"></td> + <td th:text="${item.price}"></td> + <td th:text="${item.subscriberPrice}"></td> + </tr> + </table> + </div> + </article> + </body> +</html> \ No newline at end of file diff --git a/src/main/resources/templates/stocks.html b/src/main/resources/templates/stocks.html new file mode 100644 index 0000000..cf26c47 --- /dev/null +++ b/src/main/resources/templates/stocks.html @@ -0,0 +1,30 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> + <head th:replace="fragments/head :: sharedHead (pageName=#{web.ui.products})"> + </head> + + <body> + <div th:replace="fragments/navbar :: topNavBar"></div> + <article> + <h1 th:text="#{web.ui.products}"></h1> + <div class="mainFrame" id="productsArea"> + <table class="mainList" id="stocksList"> + <tr> + <th th:text="#{web.ui.stock.date}"></th> + <th th:text="#{web.ui.stock.type}"></th> + <th th:text="#{web.ui.stock.account}"></th> + <th th:text="#{web.ui.stock.debit}"></th> + <th th:text="#{web.ui.stock.credit}"></th> + <th th:text="#{web.ui.stock.remarks}"></th> + </tr> + + <tr th:each="stock: ${stocks}"> + <td th:text="${stock.date}"></td> + <td th:text="${stock.type}"></td> + <td th:text="${stock.account}"></td> + </tr> + </table> + </div> + </article> + </body> +</html> \ No newline at end of file -- GitLab