#include "TransactionsModel.hpp" #include "Model/Project.hpp" #include #include namespace fs = std::filesystem; SalesTable::SalesTable(TransactionModel& db) // language=SQLite : GetRowCountStatement(db.GetSQLite(), "SELECT Count(*) FROM Sales") // language=SQLite , GetRowsStatement(db.GetSQLite(), "SELECT * FROM Sales WHERE rowid >= ? AND rowid < ?") // language=SQLite // TODO , FilterRowsStatement(db.GetSQLite(), R"""( )""") { } DeliveryTable::DeliveryTable(TransactionModel& db) { } PurchasesTable::PurchasesTable(TransactionModel& 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(); } /// Wrapper for SQLite::Database that creates the default tables TransactionModel::DatabaseWrapper::DatabaseWrapper(TransactionModel& self) : mSqlite(GetDatabaseFilePath(*self.mProject), SQLite::OPEN_READWRITE) { // If this table doesn't exist, the database probably just got initialized if (mSqlite.tableExists("Sales")) { return; } // 'Sales' 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) // 'Purchases' schema // - Factory: the factory id, // - OrderTime: the time this order was made // - DeliveryTime: the time this order was completed (through a set of deliveries) // 'Deliveries' 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 // language=SQLite mSqlite.exec(R"""( CREATE TABLE IF NOT EXISTS Sales( INT PRIMARY KEY, Customer INT, Deadline DATETIME, DeliveryTime DATETIME ); CREATE TABLE IF NOT EXISTS SalesItems( SaleId INT, ItemId INT, Count INT ); CREATE TABLE IF NOT EXISTS Purchases( INT PRIMARY KEY, Factory INT, OrderTime DATETIME, DeliveryTime DATETIME ); CREATE TABLE IF NOT EXISTS PurchasesItems( PurchaseId INT, ItemId INT, Count INT ); CREATE TABLE IF NOT EXISTS Deliveries( INT PRIMARY KEY, SendTime DATETIME, ArriveTime DATETIME, AssociatedOrder INT, Outgoing BOOLEAN ); CREATE TABLE IF NOT EXISTS DeliveriesItems( DeliveryId INT, ItemId INT, Count INT ); )"""); } TransactionModel::TransactionModel(Project& project) : mProject{ &project } , mDbWrapper(*this) , mSales(*this) , mPurchases(*this) , mDeliveries(*this) { } const SQLite::Database& TransactionModel::GetSQLite() const { return mDbWrapper.mSqlite; } SQLite::Database& TransactionModel::GetSQLite() { return mDbWrapper.mSqlite; } const SalesTable& TransactionModel::GetSales() const { return mSales; } SalesTable& TransactionModel::GetSales() { return mSales; } const PurchasesTable& TransactionModel::GetPurchases() const { return mPurchases; } PurchasesTable& TransactionModel::GetPurchases() { return mPurchases; } const DeliveryTable& TransactionModel::GetDeliveries() const { return mDeliveries; } DeliveryTable& TransactionModel::GetDeliveries() { return mDeliveries; }