summaryrefslogtreecommitdiff
path: root/core/src/Model
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-04-08 08:49:10 -0700
committerrtk0c <[email protected]>2021-04-08 08:49:10 -0700
commitce8660cc5bfc12e6e3f75d4cce22492783ca9066 (patch)
treef5c1088def48e60570a1225d6440f304b37b5b5c /core/src/Model
parent2f4b9db39239ed5150094a81743beea42a3eedc2 (diff)
Initial work on table visualizer
Diffstat (limited to 'core/src/Model')
-rw-r--r--core/src/Model/Filter.cpp1
-rw-r--r--core/src/Model/Filter.hpp5
-rw-r--r--core/src/Model/Items.cpp9
-rw-r--r--core/src/Model/Items.hpp6
-rw-r--r--core/src/Model/Project.cpp8
-rw-r--r--core/src/Model/Project.hpp3
-rw-r--r--core/src/Model/TransactionDatabase.cpp161
-rw-r--r--core/src/Model/TransactionDatabase.hpp42
-rw-r--r--core/src/Model/fwd.hpp6
9 files changed, 207 insertions, 34 deletions
diff --git a/core/src/Model/Filter.cpp b/core/src/Model/Filter.cpp
new file mode 100644
index 0000000..1e4b31b
--- /dev/null
+++ b/core/src/Model/Filter.cpp
@@ -0,0 +1 @@
+#include "Filter.hpp"
diff --git a/core/src/Model/Filter.hpp b/core/src/Model/Filter.hpp
new file mode 100644
index 0000000..53995c1
--- /dev/null
+++ b/core/src/Model/Filter.hpp
@@ -0,0 +1,5 @@
+#pragma once
+
+class TableRowsFilter {
+ // TODO
+};
diff --git a/core/src/Model/Items.cpp b/core/src/Model/Items.cpp
index 2951666..02a4516 100644
--- a/core/src/Model/Items.cpp
+++ b/core/src/Model/Items.cpp
@@ -8,6 +8,13 @@ void ProductItem::SetDescription(std::string description) {
mDescription = std::move(description);
}
+int ProductItem::GetPrice() const {
+ return mPrice;
+}
+void ProductItem::SetPrice(int price) {
+ mPrice = price;
+}
+
int ProductItem::GetStock() const {
return mStock;
}
@@ -19,12 +26,14 @@ void ProductItem::SetStock(int stock) {
Json::Value ProductItem::Serialize() const {
Json::Value elm;
elm["Description"] = mDescription;
+ elm["Price"] = mPrice;
elm["Stock"] = mStock;
return elm;
}
void ProductItem::Deserialize(const Json::Value& elm) {
mDescription = elm["Description"].asString();
+ mPrice = elm["Price"].asInt();
mStock = elm["Stock"].asInt();
}
diff --git a/core/src/Model/Items.hpp b/core/src/Model/Items.hpp
index 14b62f3..1289be6 100644
--- a/core/src/Model/Items.hpp
+++ b/core/src/Model/Items.hpp
@@ -175,6 +175,7 @@ public:
class ProductItem : public ItemBase<ProductItem> {
private:
std::string mDescription;
+ int mPrice = 0;
int mStock = 0;
public:
@@ -182,6 +183,11 @@ public:
const std::string& GetDescription() const;
void SetDescription(std::string description);
+ /// Get the price of this item in US cents.
+ int GetPrice() const;
+ void SetPrice(int price);
+ /// Get the current number of this product in warehouse.
+ /// This is a housekeeping field and shouldn't be editable by the user from the UI.
int GetStock() const;
void SetStock(int stock);
diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp
index c20e0c8..2a79e3f 100644
--- a/core/src/Model/Project.cpp
+++ b/core/src/Model/Project.cpp
@@ -82,6 +82,14 @@ void Project::SetName(std::string name) {
mName = std::move(name);
}
+const TransactionDatabase& Project::GetDatabase() const {
+ return mDb;
+}
+
+TransactionDatabase& Project::GetDatabase() {
+ return mDb;
+}
+
Json::Value Project::Serialize() {
Json::Value root(Json::objectValue);
diff --git a/core/src/Model/Project.hpp b/core/src/Model/Project.hpp
index 8d437ea..dca10d0 100644
--- a/core/src/Model/Project.hpp
+++ b/core/src/Model/Project.hpp
@@ -39,6 +39,9 @@ public:
const std::string& GetName() const;
void SetName(std::string name);
+ const TransactionDatabase& GetDatabase() const;
+ TransactionDatabase& GetDatabase();
+
Json::Value Serialize();
void WriteToDisk();
};
diff --git a/core/src/Model/TransactionDatabase.cpp b/core/src/Model/TransactionDatabase.cpp
index c28db0d..766727d 100644
--- a/core/src/Model/TransactionDatabase.cpp
+++ b/core/src/Model/TransactionDatabase.cpp
@@ -4,38 +4,157 @@
#include <filesystem>
#include <stdexcept>
-#include <string>
namespace fs = std::filesystem;
-static bool TableExists(sqlite3* db, const char* table, const char* column = nullptr) {
- return sqlite3_table_column_metadata(db, nullptr, table, column, nullptr, nullptr, nullptr, nullptr, nullptr) == SQLITE_OK;
+SalesTable::SalesTable(TransactionDatabase& db)
+ // language=SQLite
+ : GetRowsStatement(db.GetSQLite(), R"""(
+SELECT * FROM Sales WHERE rowid >= ? AND rowid < ?
+)""")
+ // language=SQLite
+ // TODO
+ , FilterRowsStatement(db.GetSQLite(), R"""(
+)""") {
+}
+
+int SalesTable::GetEntryCont() const {
+ // TODO
+ return 0;
+}
+
+DeliveryTable::DeliveryTable(TransactionDatabase& db) {
+}
+
+PurchasesTable::PurchasesTable(TransactionDatabase& db) {
+}
+
+static std::string GetDatabaseFilePath(const Project& project) {
+ auto dbsDir = project.GetPath() / "databases";
+ fs::create_directories(dbsDir);
+
+ auto dbFile = dbsDir / "transactions.sqlite3";
+ return dbFile.string();
}
TransactionDatabase::TransactionDatabase(Project& project)
: mProject{ &project }
- , mDatabase{ nullptr } {
+ , mDb(GetDatabaseFilePath(project), SQLite::OPEN_READWRITE)
+ , mSales(*this)
+ , mPurchases(*this)
+ , mDeliveries(*this) {
+ // Schema
+ // - Customer: the customer item ID
+ // - Deadline: unix epoch time of order deadline
+ // - DeliveryTime: the time this order was completed (through a set of deliveries)
+ if (!mDb.tableExists("Sales")) {
+ // language=SQLite
+ mDb.exec(R"""(
+CREATE TABLE Sales(
+ INT PRIMARY KEY,
+ Customer INT,
+ Deadline DATETIME,
+ DeliveryTime DATETIME
+);
+)""");
+ }
+
+ if (!mDb.tableExists("SalesItems")) {
+ // language=SQLite
+ mDb.exec(R"""(
+CREATE TABLE SalesItems(
+ SaleId INT,
+ ItemId INT,
+ Count INT
+);
+)""");
+ }
- fs::path dbDir = project.GetPath() / "databases";
- fs::create_directories(dbDir);
+ // Schema
+ // - Factory: the factory id,
+ // - OrderTime: the time this order was made
+ // - DeliveryTime: the time this order was completed (through a set of deliveries)
+ if (!mDb.tableExists("Purchases")) {
+ // language=SQLite
+ mDb.exec(R"""(
+CREATE TABLE Purchases(
+ INT PRIMARY KEY,
+ Factory INT,
+ OrderTime DATETIME,
+ DeliveryTime DATETIME
+);
+)""");
+ }
+
+ if (!mDb.tableExists("PurchasesItems")) {
+ // language=SQLite
+ mDb.exec(R"""(
+CREATE TABLE PurchasesItems(
+ PurchaseId INT,
+ ItemId INT,
+ Count INT
+);
+)""");
+ }
- fs::path dbPath = dbDir / "transactions.sqlite3";
-#if PLATFORM_WIN32
- if (int rc = sqlite3_open16(dbPath.c_str(), &mDatabase); rc) {
-#else
- if (int rc = sqlite3_open(transactionDbPath.c_str(), &mDatabase); rc) {
-#endif
- sqlite3_close(mDatabase);
+ // Schema
+ // - SendTime: unix epoch time of sending to delivery
+ // - ArriveTime: unix epoch time of delivery arrived at warehouse; 0 if not arrived yet
+ // - AssociatedOrder: rowid of the order that this delivery is completing (which table: Outgoing=true -> Sales, Outgoing=false -> Purchases)
+ // - Outgoing: true if the delivery is from warehouse to customer; false if the delivery is from factory to warehouse
+ if (!mDb.tableExists("Deliveries")) {
+ // language=SQLite
+ mDb.exec(R"""(
+CREATE TABLE Deliveries(
+ INT PRIMARY KEY,
+ SendTime DATETIME,
+ ArriveTime DATETIME,
+ AssociatedOrder INT,
+ Outgoing BOOLEAN
+);
+)""");
+ }
- std::string message;
- message += "Failed to open SQLite database for transactions. Error code: ";
- message += rc;
- message += ".";
- throw std::runtime_error(message);
+ if (!mDb.tableExists("DeliveriesItems")) {
+ // language=SQLite
+ mDb.exec(R"""(
+CREATE TABLE DeliveriesItems(
+ DeliveryId INT,
+ ItemId INT,
+ Count INT
+);
+)""");
}
}
-TransactionDatabase::~TransactionDatabase() {
- sqlite3_close(mDatabase);
- mDatabase = nullptr;
+const SQLite::Database& TransactionDatabase::GetSQLite() const {
+ return mDb;
+}
+
+SQLite::Database& TransactionDatabase::GetSQLite() {
+ return mDb;
+}
+
+const SalesTable& TransactionDatabase::GetSales() const {
+ return mSales;
+}
+
+SalesTable& TransactionDatabase::GetSales() {
+ return mSales;
+}
+
+const PurchasesTable& TransactionDatabase::GetPurchases() const {
+ return mPurchases;
+}
+
+PurchasesTable& TransactionDatabase::GetPurchases() {
+ return mPurchases;
+}
+
+const DeliveryTable& TransactionDatabase::GetDeliveries() const {
+ return mDeliveries;
+}
+
+DeliveryTable& TransactionDatabase::GetDeliveries() {
+ return mDeliveries;
}
diff --git a/core/src/Model/TransactionDatabase.hpp b/core/src/Model/TransactionDatabase.hpp
index 191a8b8..9c869c4 100644
--- a/core/src/Model/TransactionDatabase.hpp
+++ b/core/src/Model/TransactionDatabase.hpp
@@ -2,33 +2,49 @@
#include "cplt_fwd.hpp"
-#include <sqlite3.h>
+#include <SQLiteCpp/Database.h>
+#include <SQLiteCpp/Statement.h>
#include <cstdint>
-struct DeliveryId {
- int64_t id;
-};
+class SalesTable {
+public:
+ SQLite::Statement GetRowsStatement;
+ SQLite::Statement FilterRowsStatement;
-class DeliveryTable {
-};
+public:
+ SalesTable(TransactionDatabase& db);
-class OrdersTable {
+ int GetEntryCont() const;
};
class PurchasesTable {
+public:
+ PurchasesTable(TransactionDatabase& db);
+};
+
+class DeliveryTable {
+public:
+ DeliveryTable(TransactionDatabase& db);
};
class TransactionDatabase {
private:
Project* mProject;
- sqlite3* mDatabase;
+ SQLite::Database mDb;
+ SalesTable mSales;
+ PurchasesTable mPurchases;
+ DeliveryTable mDeliveries;
public:
TransactionDatabase(Project& project);
- ~TransactionDatabase();
- TransactionDatabase(const TransactionDatabase&) = delete;
- TransactionDatabase& operator=(const TransactionDatabase&) = delete;
- TransactionDatabase(TransactionDatabase&&) = default;
- TransactionDatabase& operator=(TransactionDatabase&&) = default;
+ const SQLite::Database& GetSQLite() const;
+ SQLite::Database& GetSQLite();
+
+ const SalesTable& GetSales() const;
+ SalesTable& GetSales();
+ const PurchasesTable& GetPurchases() const;
+ PurchasesTable& GetPurchases();
+ const DeliveryTable& GetDeliveries() const;
+ DeliveryTable& GetDeliveries()
};
diff --git a/core/src/Model/fwd.hpp b/core/src/Model/fwd.hpp
index 2d8d2ec..e153923 100644
--- a/core/src/Model/fwd.hpp
+++ b/core/src/Model/fwd.hpp
@@ -1,5 +1,8 @@
#pragma once
+// Filter.hpp
+class TableRowsFilter;
+
// GlobalStates.hpp
class GlobalStates;
@@ -16,4 +19,7 @@ class CustomerItem;
class Project;
// TransactionDatabase.hpp
+class SalesTable;
+class PurchasesTable;
+class DeliveryTable;
class TransactionDatabase;