mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
Reorganize project part 1
This commit is contained in:
26
services/ApplicationService.php
Normal file
26
services/ApplicationService.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
class ApplicationService
|
||||
{
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public static function IsDemoInstallation()
|
||||
{
|
||||
return file_exists(__DIR__ . '/../data/demo.txt');
|
||||
}
|
||||
|
||||
private static $InstalledVersion;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function GetInstalledVersion()
|
||||
{
|
||||
if (self::$InstalledVersion == null)
|
||||
{
|
||||
self::$InstalledVersion = preg_replace("/\r|\n/", '', file_get_contents(__DIR__ . '/../version.txt'));
|
||||
}
|
||||
|
||||
return self::$InstalledVersion;
|
||||
}
|
||||
}
|
57
services/BatteriesService.php
Normal file
57
services/BatteriesService.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
class BatteriesService
|
||||
{
|
||||
public static function GetCurrent()
|
||||
{
|
||||
$sql = 'SELECT * from batteries_current';
|
||||
return DatabaseService::ExecuteDbQuery(DatabaseService::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public static function GetNextChargeTime(int $batteryId)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$battery = $db->batteries($batteryId);
|
||||
$batteryLastLogRow = DatabaseService::ExecuteDbQuery(DatabaseService::GetDbConnectionRaw(), "SELECT * from batteries_current WHERE battery_id = $batteryId LIMIT 1")->fetch(PDO::FETCH_OBJ);
|
||||
|
||||
if ($battery->charge_interval_days > 0)
|
||||
{
|
||||
return date('Y-m-d H:i:s', strtotime('+' . $battery->charge_interval_days . ' day', strtotime($batteryLastLogRow->last_tracked_time)));
|
||||
}
|
||||
else
|
||||
{
|
||||
return date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function GetBatteryDetails(int $batteryId)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$battery = $db->batteries($batteryId);
|
||||
$batteryChargeCylcesCount = $db->battery_charge_cycles()->where('battery_id', $batteryId)->count();
|
||||
$batteryLastChargedTime = $db->battery_charge_cycles()->where('battery_id', $batteryId)->max('tracked_time');
|
||||
|
||||
return array(
|
||||
'battery' => $battery,
|
||||
'last_charged' => $batteryLastChargedTime,
|
||||
'charge_cycles_count' => $batteryChargeCylcesCount
|
||||
);
|
||||
}
|
||||
|
||||
public static function TrackChargeCycle(int $batteryId, string $trackedTime)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$logRow = $db->battery_charge_cycles()->createRow(array(
|
||||
'battery_id' => $batteryId,
|
||||
'tracked_time' => $trackedTime
|
||||
));
|
||||
$logRow->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
82
services/DatabaseService.php
Normal file
82
services/DatabaseService.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
class DatabaseService
|
||||
{
|
||||
private static $DbConnectionRaw;
|
||||
/**
|
||||
* @return PDO
|
||||
*/
|
||||
public static function GetDbConnectionRaw($doMigrations = false)
|
||||
{
|
||||
if ($doMigrations === true)
|
||||
{
|
||||
self::$DbConnectionRaw = null;
|
||||
}
|
||||
|
||||
if (self::$DbConnectionRaw == null)
|
||||
{
|
||||
$pdo = new PDO('sqlite:' . __DIR__ . '/../data/grocy.db');
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
if ($doMigrations === true)
|
||||
{
|
||||
self::ExecuteDbStatement($pdo, "CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL PRIMARY KEY UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')))");
|
||||
GrocyDbMigrator::MigrateDb($pdo);
|
||||
|
||||
if (ApplicationService::IsDemoInstallation())
|
||||
{
|
||||
GrocyDemoDataGenerator::PopulateDemoData($pdo);
|
||||
}
|
||||
}
|
||||
|
||||
self::$DbConnectionRaw = $pdo;
|
||||
}
|
||||
|
||||
return self::$DbConnectionRaw;
|
||||
}
|
||||
|
||||
private static $DbConnection;
|
||||
/**
|
||||
* @return LessQL\Database
|
||||
*/
|
||||
public static function GetDbConnection($doMigrations = false)
|
||||
{
|
||||
if ($doMigrations === true)
|
||||
{
|
||||
self::$DbConnection = null;
|
||||
}
|
||||
|
||||
if (self::$DbConnection == null)
|
||||
{
|
||||
self::$DbConnection = new LessQL\Database(self::GetDbConnectionRaw($doMigrations));
|
||||
}
|
||||
|
||||
return self::$DbConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public static function ExecuteDbStatement(PDO $pdo, string $sql)
|
||||
{
|
||||
if ($pdo->exec($sql) === false)
|
||||
{
|
||||
throw new Exception($pdo->errorInfo());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean|PDOStatement
|
||||
*/
|
||||
public static function ExecuteDbQuery(PDO $pdo, string $sql)
|
||||
{
|
||||
if (self::ExecuteDbStatement($pdo, $sql) === true)
|
||||
{
|
||||
return $pdo->query($sql);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
59
services/HabitsService.php
Normal file
59
services/HabitsService.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
class HabitsService
|
||||
{
|
||||
const HABIT_TYPE_MANUALLY = 'manually';
|
||||
const HABIT_TYPE_DYNAMIC_REGULAR = 'dynamic-regular';
|
||||
|
||||
public static function GetCurrentHabits()
|
||||
{
|
||||
$sql = 'SELECT * from habits_current';
|
||||
return DatabaseService::ExecuteDbQuery(DatabaseService::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public static function GetNextHabitTime(int $habitId)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$habit = $db->habits($habitId);
|
||||
$habitLastLogRow = DatabaseService::ExecuteDbQuery(DatabaseService::GetDbConnectionRaw(), "SELECT * from habits_current WHERE habit_id = $habitId LIMIT 1")->fetch(PDO::FETCH_OBJ);
|
||||
|
||||
switch ($habit->period_type)
|
||||
{
|
||||
case self::HABIT_TYPE_MANUALLY:
|
||||
return date('Y-m-d H:i:s');
|
||||
case self::HABIT_TYPE_DYNAMIC_REGULAR:
|
||||
return date('Y-m-d H:i:s', strtotime('+' . $habit->period_days . ' day', strtotime($habitLastLogRow->last_tracked_time)));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function GetHabitDetails(int $habitId)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$habit = $db->habits($habitId);
|
||||
$habitTrackedCount = $db->habits_log()->where('habit_id', $habitId)->count();
|
||||
$habitLastTrackedTime = $db->habits_log()->where('habit_id', $habitId)->max('tracked_time');
|
||||
|
||||
return array(
|
||||
'habit' => $habit,
|
||||
'last_tracked' => $habitLastTrackedTime,
|
||||
'tracked_count' => $habitTrackedCount
|
||||
);
|
||||
}
|
||||
|
||||
public static function TrackHabit(int $habitId, string $trackedTime)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$logRow = $db->habits_log()->createRow(array(
|
||||
'habit_id' => $habitId,
|
||||
'tracked_time' => $trackedTime
|
||||
));
|
||||
$logRow->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
48
services/SessionService.php
Normal file
48
services/SessionService.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
class SessionService
|
||||
{
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public static function IsValidSession($sessionKey)
|
||||
{
|
||||
if ($sessionKey === null || empty($sessionKey))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return file_exists(__DIR__ . "/../data/sessions/$sessionKey.txt");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function CreateSession()
|
||||
{
|
||||
if (!file_exists(__DIR__ . '/../data/sessions'))
|
||||
{
|
||||
mkdir(__DIR__ . '/../data/sessions');
|
||||
}
|
||||
|
||||
$now = time();
|
||||
foreach (new FilesystemIterator(__DIR__ . '/../data/sessions') as $file)
|
||||
{
|
||||
if ($now - $file->getCTime() >= 2678400) //31 days
|
||||
{
|
||||
unlink(__DIR__ . '/../data/sessions/' . $file->getFilename());
|
||||
}
|
||||
}
|
||||
|
||||
$newSessionKey = uniqid() . uniqid() . uniqid();
|
||||
file_put_contents(__DIR__ . "/../data/sessions/$newSessionKey.txt", '');
|
||||
return $newSessionKey;
|
||||
}
|
||||
|
||||
public static function RemoveSession($sessionKey)
|
||||
{
|
||||
unlink(__DIR__ . "/../data/sessions/$sessionKey.txt");
|
||||
}
|
||||
}
|
191
services/StockService.php
Normal file
191
services/StockService.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
class StockService
|
||||
{
|
||||
const TRANSACTION_TYPE_PURCHASE = 'purchase';
|
||||
const TRANSACTION_TYPE_CONSUME = 'consume';
|
||||
const TRANSACTION_TYPE_INVENTORY_CORRECTION = 'inventory-correction';
|
||||
|
||||
public static function GetCurrentStock()
|
||||
{
|
||||
$sql = 'SELECT * from stock_current';
|
||||
return DatabaseService::ExecuteDbQuery(DatabaseService::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public static function GetMissingProducts()
|
||||
{
|
||||
$sql = 'SELECT * from stock_missing_products';
|
||||
return DatabaseService::ExecuteDbQuery(DatabaseService::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public static function GetProductDetails(int $productId)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$product = $db->products($productId);
|
||||
$productStockAmount = $db->stock()->where('product_id', $productId)->sum('amount');
|
||||
$productLastPurchased = $db->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_PURCHASE)->max('purchased_date');
|
||||
$productLastUsed = $db->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->max('used_date');
|
||||
$quPurchase = $db->quantity_units($product->qu_id_purchase);
|
||||
$quStock = $db->quantity_units($product->qu_id_stock);
|
||||
|
||||
return array(
|
||||
'product' => $product,
|
||||
'last_purchased' => $productLastPurchased,
|
||||
'last_used' => $productLastUsed,
|
||||
'stock_amount' => $productStockAmount,
|
||||
'quantity_unit_purchase' => $quPurchase,
|
||||
'quantity_unit_stock' => $quStock
|
||||
);
|
||||
}
|
||||
|
||||
public static function AddProduct(int $productId, int $amount, string $bestBeforeDate, $transactionType)
|
||||
{
|
||||
if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
$stockId = uniqid();
|
||||
|
||||
$logRow = $db->stock_log()->createRow(array(
|
||||
'product_id' => $productId,
|
||||
'amount' => $amount,
|
||||
'best_before_date' => $bestBeforeDate,
|
||||
'purchased_date' => date('Y-m-d'),
|
||||
'stock_id' => $stockId,
|
||||
'transaction_type' => $transactionType
|
||||
));
|
||||
$logRow->save();
|
||||
|
||||
$stockRow = $db->stock()->createRow(array(
|
||||
'product_id' => $productId,
|
||||
'amount' => $amount,
|
||||
'best_before_date' => $bestBeforeDate,
|
||||
'purchased_date' => date('Y-m-d'),
|
||||
'stock_id' => $stockId,
|
||||
));
|
||||
$stockRow->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Transaction type $transactionType is not valid (StockService.AddProduct)");
|
||||
}
|
||||
}
|
||||
|
||||
public static function ConsumeProduct(int $productId, int $amount, bool $spoiled, $transactionType)
|
||||
{
|
||||
if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$productStockAmount = $db->stock()->where('product_id', $productId)->sum('amount');
|
||||
$potentialStockEntries = $db->stock()->where('product_id', $productId)->orderBy('best_before_date', 'ASC')->orderBy('purchased_date', 'ASC')->fetchAll(); //First expiring first, then first in first out
|
||||
|
||||
if ($amount > $productStockAmount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($potentialStockEntries as $stockEntry)
|
||||
{
|
||||
if ($amount == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if ($amount >= $stockEntry->amount) //Take the whole stock entry
|
||||
{
|
||||
$logRow = $db->stock_log()->createRow(array(
|
||||
'product_id' => $stockEntry->product_id,
|
||||
'amount' => $stockEntry->amount * -1,
|
||||
'best_before_date' => $stockEntry->best_before_date,
|
||||
'purchased_date' => $stockEntry->purchased_date,
|
||||
'used_date' => date('Y-m-d'),
|
||||
'spoiled' => $spoiled,
|
||||
'stock_id' => $stockEntry->stock_id,
|
||||
'transaction_type' => $transactionType
|
||||
));
|
||||
$logRow->save();
|
||||
|
||||
$amount -= $stockEntry->amount;
|
||||
$stockEntry->delete();
|
||||
}
|
||||
else //Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
|
||||
{
|
||||
$logRow = $db->stock_log()->createRow(array(
|
||||
'product_id' => $stockEntry->product_id,
|
||||
'amount' => $amount * -1,
|
||||
'best_before_date' => $stockEntry->best_before_date,
|
||||
'purchased_date' => $stockEntry->purchased_date,
|
||||
'used_date' => date('Y-m-d'),
|
||||
'spoiled' => $spoiled,
|
||||
'stock_id' => $stockEntry->stock_id,
|
||||
'transaction_type' => $transactionType
|
||||
));
|
||||
$logRow->save();
|
||||
|
||||
$restStockAmount = $stockEntry->amount - $amount;
|
||||
$amount = 0;
|
||||
|
||||
$stockEntry->update(array(
|
||||
'amount' => $restStockAmount
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Transaction type $transactionType is not valid (StockService.ConsumeProduct)");
|
||||
}
|
||||
}
|
||||
|
||||
public static function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate)
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
$productStockAmount = $db->stock()->where('product_id', $productId)->sum('amount');
|
||||
|
||||
if ($newAmount > $productStockAmount)
|
||||
{
|
||||
$amountToAdd = $newAmount - $productStockAmount;
|
||||
self::AddProduct($productId, $amountToAdd, $bestBeforeDate, self::TRANSACTION_TYPE_INVENTORY_CORRECTION);
|
||||
}
|
||||
else if ($newAmount < $productStockAmount)
|
||||
{
|
||||
$amountToRemove = $productStockAmount - $newAmount;
|
||||
self::ConsumeProduct($productId, $amountToRemove, false, self::TRANSACTION_TYPE_INVENTORY_CORRECTION);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function AddMissingProductsToShoppingList()
|
||||
{
|
||||
$db = DatabaseService::GetDbConnection();
|
||||
|
||||
$missingProducts = self::GetMissingProducts();
|
||||
foreach ($missingProducts as $missingProduct)
|
||||
{
|
||||
$product = $db->products()->where('id', $missingProduct->id)->fetch();
|
||||
$amount = ceil($missingProduct->amount_missing / $product->qu_factor_purchase_to_stock);
|
||||
|
||||
$alreadyExistingEntry = $db->shopping_list()->where('product_id', $missingProduct->id)->fetch();
|
||||
if ($alreadyExistingEntry) //Update
|
||||
{
|
||||
$alreadyExistingEntry->update(array(
|
||||
'amount_autoadded' => $amount
|
||||
));
|
||||
}
|
||||
else //Insert
|
||||
{
|
||||
$shoppinglistRow = $db->shopping_list()->createRow(array(
|
||||
'product_id' => $missingProduct->id,
|
||||
'amount_autoadded' => $amount
|
||||
));
|
||||
$shoppinglistRow->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user