mirror of
https://github.com/grocy/grocy.git
synced 2025-09-18 02:31:44 +00:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
96209c852c | ||
|
8e40c50cc1 | ||
|
4b0f0141c9 | ||
|
d1bd21a601 | ||
|
c6925ba4c3 | ||
|
52e311d847 | ||
|
f2f18d260d | ||
|
1d293741ba | ||
|
5db288fc3c | ||
|
d628f9b3ca | ||
|
fe8a6d96e4 |
53
Grocy.php
53
Grocy.php
@@ -6,17 +6,21 @@ class Grocy
|
||||
/**
|
||||
* @return PDO
|
||||
*/
|
||||
public static function GetDbConnectionRaw()
|
||||
public static function GetDbConnectionRaw($doMigrations = false)
|
||||
{
|
||||
if ($doMigrations === true)
|
||||
{
|
||||
self::$DbConnectionRaw = null;
|
||||
}
|
||||
|
||||
if (self::$DbConnectionRaw == null)
|
||||
{
|
||||
$newDb = !file_exists(__DIR__ . '/data/grocy.db');
|
||||
$pdo = new PDO('sqlite:' . __DIR__ . '/data/grocy.db');
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
if ($newDb)
|
||||
if ($doMigrations === true)
|
||||
{
|
||||
$pdo->exec("CREATE TABLE migrations (migration INTEGER NOT NULL UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')), PRIMARY KEY(migration)) WITHOUT ROWID");
|
||||
Grocy::ExecuteDbStatement($pdo, "CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')), PRIMARY KEY(migration)) WITHOUT ROWID");
|
||||
GrocyDbMigrator::MigrateDb($pdo);
|
||||
|
||||
if (self::IsDemoInstallation())
|
||||
@@ -35,22 +39,59 @@ class Grocy
|
||||
/**
|
||||
* @return LessQL\Database
|
||||
*/
|
||||
public static function GetDbConnection()
|
||||
public static function GetDbConnection($doMigrations = false)
|
||||
{
|
||||
if ($doMigrations === true)
|
||||
{
|
||||
self::$DbConnection = null;
|
||||
}
|
||||
|
||||
if (self::$DbConnection == null)
|
||||
{
|
||||
self::$DbConnection = new LessQL\Database(self::GetDbConnectionRaw());
|
||||
self::$DbConnection = new LessQL\Database(self::GetDbConnectionRaw($doMigrations));
|
||||
}
|
||||
|
||||
return self::$DbConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public static function ExecuteDbStatement(PDO $pdo, string $sql)
|
||||
{
|
||||
if ($pdo->exec(utf8_encode($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(utf8_encode($sql));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
|
@@ -71,18 +71,46 @@ class GrocyDbMigrator
|
||||
INSERT INTO products (name, description, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('DefaultProduct1', 'This is the first default product, edit or delete it', 1, 1, 1, 1);
|
||||
INSERT INTO products (name, description, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('DefaultProduct2', 'This is the second default product, edit or delete it', 1, 1, 1, 1);"
|
||||
);
|
||||
|
||||
self::ExecuteMigrationWhenNeeded($pdo, 7, "
|
||||
CREATE VIEW stock_missing_products
|
||||
AS
|
||||
SELECT p.id, MAX(p.name) AS name, p.min_stock_amount - IFNULL(SUM(s.amount), 0) AS amount_missing
|
||||
FROM products p
|
||||
LEFT JOIN stock s
|
||||
ON p.id = s.product_id
|
||||
WHERE p.min_stock_amount != 0
|
||||
GROUP BY p.id
|
||||
HAVING IFNULL(SUM(s.amount), 0) < p.min_stock_amount;"
|
||||
);
|
||||
|
||||
self::ExecuteMigrationWhenNeeded($pdo, 8, "
|
||||
CREATE VIEW stock_current
|
||||
AS
|
||||
SELECT product_id, SUM(amount) AS amount, MIN(best_before_date) AS best_before_date
|
||||
from stock
|
||||
GROUP BY product_id
|
||||
ORDER BY MIN(best_before_date) ASC;"
|
||||
);
|
||||
|
||||
self::ExecuteMigrationWhenNeeded($pdo, 9, "
|
||||
CREATE TABLE shopping_list (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||
product_id INTEGER NOT NULL UNIQUE,
|
||||
amount INTEGER NOT NULL DEFAULT 0,
|
||||
amount_autoadded INTEGER NOT NULL DEFAULT 0,
|
||||
row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime'))
|
||||
)"
|
||||
);
|
||||
}
|
||||
|
||||
private static function ExecuteMigrationWhenNeeded(PDO $pdo, int $migrationId, string $sql)
|
||||
{
|
||||
if ($pdo->query("SELECT COUNT(*) FROM migrations WHERE migration = $migrationId")->fetchColumn() == 0)
|
||||
$rowCount = Grocy::ExecuteDbQuery($pdo, 'SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
{
|
||||
if ($pdo->exec(utf8_encode($sql)) === false)
|
||||
{
|
||||
throw new Exception($pdo->errorInfo());
|
||||
}
|
||||
|
||||
$pdo->exec('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
Grocy::ExecuteDbStatement($pdo, $sql);
|
||||
Grocy::ExecuteDbStatement($pdo, 'INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,52 +4,56 @@ class GrocyDemoDataGenerator
|
||||
{
|
||||
public static function PopulateDemoData(PDO $pdo)
|
||||
{
|
||||
$sql = "
|
||||
UPDATE locations SET name = 'Vorratskammer', description = '' WHERE id = 1;
|
||||
INSERT INTO locations (name) VALUES ('S<><53>igkeitenschrank'); --2
|
||||
INSERT INTO locations (name) VALUES ('Konservenschrank'); --3
|
||||
INSERT INTO locations (name) VALUES ('K<>hlschrank'); --4
|
||||
|
||||
UPDATE quantity_units SET name = 'St<53>ck' WHERE id = 1;
|
||||
INSERT INTO quantity_units (name) VALUES ('Packung'); --2
|
||||
INSERT INTO quantity_units (name) VALUES ('Glas'); --3
|
||||
INSERT INTO quantity_units (name) VALUES ('Dose'); --4
|
||||
INSERT INTO quantity_units (name) VALUES ('Becher'); --5
|
||||
INSERT INTO quantity_units (name) VALUES ('Bund'); --6
|
||||
|
||||
DELETE FROM products WHERE id IN (1, 2);
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Gummib<69>rchen', 2, 2, 2, 1); --3
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Chips', 2, 2, 2, 1); --4
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Eier', 1, 2, 1, 10); --5
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Nudeln', 1, 2, 2, 1); --6
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Essiggurken', 3, 3, 3, 1); --7
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Gulaschsuppe', 3, 4, 4, 1); --8
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Joghurt', 4, 5, 5, 1); --9
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('K<>se', 4, 2, 2, 1); --10
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Aufschnitt', 4, 2, 2, 1); --11
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Paprika', 4, 1, 1, 1); --12
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Gurke', 4, 1, 1, 1); --13
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Radieschen', 4, 6, 6, 1); --14
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Tomate', 4, 1, 1, 1); --15
|
||||
";
|
||||
|
||||
if ($pdo->exec(utf8_encode($sql)) === false)
|
||||
$rowCount = Grocy::ExecuteDbQuery($pdo, 'SELECT COUNT(*) FROM migrations WHERE migration = -1')->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
{
|
||||
throw new Exception($pdo->errorInfo());
|
||||
}
|
||||
$sql = "
|
||||
UPDATE locations SET name = 'Vorratskammer', description = '' WHERE id = 1;
|
||||
INSERT INTO locations (name) VALUES ('S<><53>igkeitenschrank'); --2
|
||||
INSERT INTO locations (name) VALUES ('Konservenschrank'); --3
|
||||
INSERT INTO locations (name) VALUES ('K<>hlschrank'); --4
|
||||
|
||||
GrocyLogicStock::AddProduct(3, 5, date('Y-m-d', strtotime('+180 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(4, 5, date('Y-m-d', strtotime('+180 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(5, 5, date('Y-m-d', strtotime('+20 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(6, 5, date('Y-m-d', strtotime('+600 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(7, 5, date('Y-m-d', strtotime('+800 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(8, 5, date('Y-m-d', strtotime('+900 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(9, 5, date('Y-m-d', strtotime('+14 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(10, 5, date('Y-m-d', strtotime('+21 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(11, 5, date('Y-m-d', strtotime('+10 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(12, 5, date('Y-m-d', strtotime('+2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(13, 5, date('Y-m-d', strtotime('-2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(14, 5, date('Y-m-d', strtotime('+2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(15, 5, date('Y-m-d', strtotime('-2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
UPDATE quantity_units SET name = 'St<EFBFBD>ck' WHERE id = 1;
|
||||
INSERT INTO quantity_units (name) VALUES ('Packung'); --2
|
||||
INSERT INTO quantity_units (name) VALUES ('Glas'); --3
|
||||
INSERT INTO quantity_units (name) VALUES ('Dose'); --4
|
||||
INSERT INTO quantity_units (name) VALUES ('Becher'); --5
|
||||
INSERT INTO quantity_units (name) VALUES ('Bund'); --6
|
||||
|
||||
DELETE FROM products WHERE id IN (1, 2);
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock, min_stock_amount) VALUES ('Gummib<69>rchen', 2, 2, 2, 1, 8); --3
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock, min_stock_amount) VALUES ('Chips', 2, 2, 2, 1, 10); --4
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Eier', 1, 2, 1, 10); --5
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Nudeln', 1, 2, 2, 1); --6
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Essiggurken', 3, 3, 3, 1); --7
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Gulaschsuppe', 3, 4, 4, 1); --8
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Joghurt', 4, 5, 5, 1); --9
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('K<>se', 4, 2, 2, 1); --10
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Aufschnitt', 4, 2, 2, 1); --11
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Paprika', 4, 1, 1, 1); --12
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Gurke', 4, 1, 1, 1); --13
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Radieschen', 4, 6, 6, 1); --14
|
||||
INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Tomate', 4, 1, 1, 1); --15
|
||||
|
||||
INSERT INTO migrations (migration) VALUES (-1);
|
||||
";
|
||||
|
||||
Grocy::ExecuteDbStatement($pdo, $sql);
|
||||
|
||||
GrocyLogicStock::AddProduct(3, 5, date('Y-m-d', strtotime('+180 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(4, 5, date('Y-m-d', strtotime('+180 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(5, 5, date('Y-m-d', strtotime('+20 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(6, 5, date('Y-m-d', strtotime('+600 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(7, 5, date('Y-m-d', strtotime('+800 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(8, 5, date('Y-m-d', strtotime('+900 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(9, 5, date('Y-m-d', strtotime('+14 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(10, 5, date('Y-m-d', strtotime('+21 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(11, 5, date('Y-m-d', strtotime('+10 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(12, 5, date('Y-m-d', strtotime('+2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(13, 5, date('Y-m-d', strtotime('-2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(14, 5, date('Y-m-d', strtotime('+2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddProduct(15, 5, date('Y-m-d', strtotime('-2 days')), GrocyLogicStock::TRANSACTION_TYPE_PURCHASE);
|
||||
GrocyLogicStock::AddMissingProductsToShoppingList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,8 +8,14 @@ class GrocyLogicStock
|
||||
|
||||
public static function GetCurrentStock()
|
||||
{
|
||||
$db = Grocy::GetDbConnectionRaw();
|
||||
return $db->query('SELECT product_id, SUM(amount) AS amount, MIN(best_before_date) AS best_before_date from stock GROUP BY product_id ORDER BY MIN(best_before_date) ASC')->fetchAll(PDO::FETCH_OBJ);
|
||||
$sql = 'SELECT * from stock_current';
|
||||
return Grocy::ExecuteDbQuery(Grocy::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public static function GetMissingProducts()
|
||||
{
|
||||
$sql = 'SELECT * from stock_missing_products';
|
||||
return Grocy::ExecuteDbQuery(Grocy::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public static function GetProductDetails(int $productId)
|
||||
@@ -154,4 +160,32 @@ class GrocyLogicStock
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function AddMissingProductsToShoppingList()
|
||||
{
|
||||
$db = Grocy::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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -46,4 +46,15 @@ class GrocyPhpHelper
|
||||
|
||||
return $returnArray;
|
||||
}
|
||||
|
||||
public static function SumArrayValue($array, $propertyName)
|
||||
{
|
||||
$sum = 0;
|
||||
foreach($array as $object)
|
||||
{
|
||||
$sum += $object->{$propertyName};
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
}
|
||||
|
@@ -7,5 +7,5 @@ mkdir "%releasePath%"
|
||||
for /f "tokens=*" %%a in ('type version.txt') do set version=%%a
|
||||
|
||||
del "%releasePath%\grocy_%version%.zip"
|
||||
"build_tools\7za.exe" a -r "%releasePath%\grocy_%version%.zip" "%projectPath%\*" -xr!.* -xr!build_tools -xr!build.bat -xr!composer.json -xr!composer.lock -xr!composer.phar -xr!grocy.phpproj -xr!grocy.phpproj.user -xr!grocy.sln
|
||||
"build_tools\7za.exe" d "%releasePath%\grocy_%version%.zip" data\add_before_end_body.html data\demo.txt data\config.php data\grocy.db data\.gitignore bower.json
|
||||
"build_tools\7za.exe" a -r "%releasePath%\grocy_%version%.zip" "%projectPath%\*" -xr!.* -xr!build_tools -xr!build.bat -xr!composer.json -xr!composer.lock -xr!composer.phar -xr!grocy.phpproj -xr!grocy.phpproj.user -xr!grocy.sln -xr!bower.json
|
||||
"build_tools\7za.exe" d "%releasePath%\grocy_%version%.zip" data\*.*
|
||||
|
8
grocy.js
8
grocy.js
@@ -86,7 +86,8 @@ Grocy.GetUriParam = function(key)
|
||||
var currentUri = decodeURIComponent(window.location.search.substring(1));
|
||||
var vars = currentUri.split('&');
|
||||
|
||||
for (i = 0; i < vars.length; i++) {
|
||||
for (i = 0; i < vars.length; i++)
|
||||
{
|
||||
var currentParam = vars[i].split('=');
|
||||
|
||||
if (currentParam[0] === key)
|
||||
@@ -95,3 +96,8 @@ Grocy.GetUriParam = function(key)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Grocy.Wait = function(ms)
|
||||
{
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
@@ -25,6 +25,8 @@
|
||||
<Compile Include="index.php" />
|
||||
<Compile Include="views\consumption.php" />
|
||||
<Compile Include="views\inventory.php" />
|
||||
<Compile Include="views\shoppinglistform.php" />
|
||||
<Compile Include="views\shoppinglist.php" />
|
||||
<Compile Include="views\purchase.php" />
|
||||
<Compile Include="views\quantityunitform.php" />
|
||||
<Compile Include="views\locationform.php" />
|
||||
@@ -51,6 +53,8 @@
|
||||
<Content Include="views\consumption.js" />
|
||||
<Content Include="views\dashboard.js" />
|
||||
<Content Include="views\inventory.js" />
|
||||
<Content Include="views\shoppinglistform.js" />
|
||||
<Content Include="views\shoppinglist.js" />
|
||||
<Content Include="views\purchase.js" />
|
||||
<Content Include="views\quantityunitform.js" />
|
||||
<Content Include="views\locationform.js" />
|
||||
|
48
index.php
48
index.php
@@ -36,11 +36,14 @@ $db = Grocy::GetDbConnection();
|
||||
|
||||
$app->get('/', function(Request $request, Response $response) use($db)
|
||||
{
|
||||
$db = Grocy::GetDbConnection(true); //For database schema migration
|
||||
|
||||
return $this->renderer->render($response, '/layout.php', [
|
||||
'title' => 'Dashboard',
|
||||
'contentPage' => 'dashboard.php',
|
||||
'products' => $db->products(),
|
||||
'currentStock' => GrocyLogicStock::GetCurrentStock()
|
||||
'currentStock' => GrocyLogicStock::GetCurrentStock(),
|
||||
'missingProducts' => GrocyLogicStock::GetMissingProducts()
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -71,6 +74,18 @@ $app->get('/inventory', function(Request $request, Response $response) use($db)
|
||||
]);
|
||||
});
|
||||
|
||||
$app->get('/shoppinglist', function(Request $request, Response $response) use($db)
|
||||
{
|
||||
return $this->renderer->render($response, '/layout.php', [
|
||||
'title' => 'Shopping list',
|
||||
'contentPage' => 'shoppinglist.php',
|
||||
'listItems' => $db->shopping_list(),
|
||||
'products' => $db->products(),
|
||||
'quantityunits' => $db->quantity_units(),
|
||||
'missingProducts' => GrocyLogicStock::GetMissingProducts()
|
||||
]);
|
||||
});
|
||||
|
||||
$app->get('/products', function(Request $request, Response $response) use($db)
|
||||
{
|
||||
return $this->renderer->render($response, '/layout.php', [
|
||||
@@ -167,7 +182,30 @@ $app->get('/quantityunit/{quantityunitId}', function(Request $request, Response
|
||||
}
|
||||
});
|
||||
|
||||
$app->group('/api', function() use($db, $app)
|
||||
$app->get('/shoppinglist/{itemId}', function(Request $request, Response $response, $args) use($db)
|
||||
{
|
||||
if ($args['itemId'] == 'new')
|
||||
{
|
||||
return $this->renderer->render($response, '/layout.php', [
|
||||
'title' => 'Add shopping list item',
|
||||
'contentPage' => 'shoppinglistform.php',
|
||||
'products' => $db->products(),
|
||||
'mode' => 'create'
|
||||
]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->renderer->render($response, '/layout.php', [
|
||||
'title' => 'Edit shopping list item',
|
||||
'contentPage' => 'shoppinglistform.php',
|
||||
'listItem' => $db->shopping_list($args['itemId']),
|
||||
'products' => $db->products(),
|
||||
'mode' => 'edit'
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
$app->group('/api', function() use($db)
|
||||
{
|
||||
$this->get('/get-objects/{entity}', function(Request $request, Response $response, $args) use($db)
|
||||
{
|
||||
@@ -257,6 +295,12 @@ $app->group('/api', function() use($db, $app)
|
||||
{
|
||||
echo json_encode(GrocyLogicStock::GetCurrentStock());
|
||||
});
|
||||
|
||||
$this->get('/stock/add-missing-products-to-shoppinglist', function(Request $request, Response $response)
|
||||
{
|
||||
GrocyLogicStock::AddMissingProductsToShoppingList();
|
||||
echo json_encode(array('success' => true));
|
||||
});
|
||||
})->add(function($request, $response, $next)
|
||||
{
|
||||
$response = $next($request, $response);
|
||||
|
14
style.css
14
style.css
@@ -118,3 +118,17 @@
|
||||
.error-bg {
|
||||
background-color: #f2dede !important;
|
||||
}
|
||||
|
||||
.info-bg {
|
||||
background-color: #afd9ee !important;
|
||||
}
|
||||
|
||||
.discrete-content-separator {
|
||||
padding-top: 5px;
|
||||
padding-bottom: 5px;
|
||||
}
|
||||
|
||||
.discrete-content-separator-2x {
|
||||
padding-top: 10px;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
@@ -1 +1 @@
|
||||
1.0.1
|
||||
1.2.0
|
@@ -57,6 +57,7 @@ $('#product_id').on('change', function(e)
|
||||
$('#selected-product-last-used-timeago').text($.timeago(productDetails.last_used || ''));
|
||||
$('#amount').attr('max', productDetails.stock_amount);
|
||||
$('#consumption-form').validator('update');
|
||||
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
|
||||
|
||||
Grocy.EmptyElementWhenMatches('#selected-product-last-purchased-timeago', 'NaN years ago');
|
||||
Grocy.EmptyElementWhenMatches('#selected-product-last-used-timeago', 'NaN years ago');
|
||||
|
@@ -1,11 +1,12 @@
|
||||
<div class="col-sm-3 col-sm-offset-3 col-md-3 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">Consumption</h1>
|
||||
|
||||
<form id="consumption-form">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="product_id">Product <i class="fa fa-barcode"></i></label>
|
||||
<select data-instockproduct="instockproduct" class="form-control combobox" id="product_id" name="product_id" required>
|
||||
<select class="form-control combobox" id="product_id" name="product_id" required>
|
||||
<option value=""></option>
|
||||
<?php foreach ($products as $product) : ?>
|
||||
<option data-additional-searchdata="<?php echo $product->barcode; ?>" value="<?php echo $product->id; ?>"><?php echo $product->name; ?></option>
|
||||
@@ -15,7 +16,7 @@
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="amount">Amount</label>
|
||||
<label for="amount">Amount <span id="amount_qu_unit" class="small text-muted"></span></label>
|
||||
<input type="number" class="form-control" id="amount" name="amount" value="1" min="1" required>
|
||||
<div class="help-block with-errors"></div>
|
||||
</div>
|
||||
@@ -29,6 +30,7 @@
|
||||
<button id="save-consumption-button" type="submit" class="btn btn-default">OK</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-sm-6 col-md-5 col-lg-3 main well">
|
||||
|
@@ -1,7 +1,7 @@
|
||||
$(function()
|
||||
{
|
||||
$('#current-stock-table').DataTable({
|
||||
'paging': false,
|
||||
'pageLength': 50,
|
||||
'order': [[2, 'asc']]
|
||||
});
|
||||
});
|
||||
|
@@ -2,13 +2,18 @@
|
||||
|
||||
<h1 class="page-header">Dashboard</h1>
|
||||
|
||||
<h3>Stock overview</h3>
|
||||
<h3>Stock overview <span class="text-muded small"><strong><?php echo count($currentStock) ?></strong> products with <strong><?php echo GrocyPhpHelper::SumArrayValue($currentStock, 'amount'); ?></strong> units in stock</span></h3>
|
||||
|
||||
<div>
|
||||
<p class="btn btn-lg btn-warning no-real-button"><strong><?php echo count(GrocyPhpHelper::FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime('+5 days')), '<')); ?></strong> products expiring within the next 5 days</p>
|
||||
<p class="btn btn-lg btn-danger no-real-button"><strong><?php echo count(GrocyPhpHelper::FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime('-1 days')), '<')); ?></strong> products are already expired</p>
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<p class="btn btn-lg btn-warning no-real-button"><strong><?php echo count(GrocyPhpHelper::FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime('+5 days')), '<')); ?></strong> products expiring within the next 5 days</p>
|
||||
<p class="btn btn-lg btn-danger no-real-button"><strong><?php echo count(GrocyPhpHelper::FindAllObjectsInArrayByPropertyValue($currentStock, 'best_before_date', date('Y-m-d', strtotime('-1 days')), '<')); ?></strong> products are already expired</p>
|
||||
<p class="btn btn-lg btn-info no-real-button"><strong><?php echo count($missingProducts); ?></strong> products are below defined min. stock amount</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="discrete-content-separator-2x"></div>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table id="current-stock-table" class="table table-striped">
|
||||
<thead>
|
||||
@@ -20,7 +25,7 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($currentStock as $currentStockEntry) : ?>
|
||||
<tr class="<?php if ($currentStockEntry->best_before_date < date('Y-m-d', strtotime('-1 days'))) echo 'error-bg'; else if ($currentStockEntry->best_before_date < date('Y-m-d', strtotime('+5 days'))) echo 'warning-bg'; ?>">
|
||||
<tr class="<?php if ($currentStockEntry->best_before_date < date('Y-m-d', strtotime('-1 days'))) echo 'error-bg'; else if ($currentStockEntry->best_before_date < date('Y-m-d', strtotime('+5 days'))) echo 'warning-bg'; else if (GrocyPhpHelper::FindObjectInArrayByPropertyValue($missingProducts, 'id', $currentStockEntry->product_id) !== null) echo 'info-bg'; ?>">
|
||||
<td>
|
||||
<?php echo GrocyPhpHelper::FindObjectInArrayByPropertyValue($products, 'id', $currentStockEntry->product_id)->name; ?>
|
||||
</td>
|
||||
|
@@ -33,6 +33,7 @@
|
||||
}
|
||||
|
||||
toastr.success('Stock amount of ' + productDetails.product.name + ' is now ' + jsonForm.new_amount.toString() + ' ' + productDetails.quantity_unit_stock.name);
|
||||
Grocy.Wait(1000);
|
||||
|
||||
if (addBarcode !== undefined)
|
||||
{
|
||||
@@ -133,6 +134,8 @@ $(function()
|
||||
message: '<strong>' + input + '</strong> could not be resolved to a product, how do you want to proceed?',
|
||||
title: 'Create or assign product',
|
||||
onEscape: function() { },
|
||||
size: 'large',
|
||||
backdrop: true,
|
||||
buttons: {
|
||||
cancel: {
|
||||
label: 'Cancel',
|
||||
@@ -140,22 +143,44 @@ $(function()
|
||||
callback: function() { }
|
||||
},
|
||||
addnewproduct: {
|
||||
label: 'Add as new product',
|
||||
className: 'btn-success',
|
||||
label: 'Add as new <u><strong>p</strong></u>roduct',
|
||||
className: 'btn-success add-new-product-dialog-button',
|
||||
callback: function()
|
||||
{
|
||||
window.location.href = '/product/new?prefillname=' + encodeURIComponent(input) + '&returnto=' + encodeURIComponent(window.location.pathname);
|
||||
}
|
||||
},
|
||||
addbarcode: {
|
||||
label: 'Add as barcode to existing product',
|
||||
className: 'btn-info',
|
||||
label: 'Add as <u><strong>b</strong></u>arcode to existing product',
|
||||
className: 'btn-info add-new-barcode-dialog-button',
|
||||
callback: function()
|
||||
{
|
||||
window.location.href = '/inventory?addbarcodetoselection=' + encodeURIComponent(input);
|
||||
}
|
||||
},
|
||||
addnewproductwithbarcode: {
|
||||
label: '<u><strong>A</strong></u>dd as new product + prefill barcode',
|
||||
className: 'btn-warning add-new-product-with-barcode-dialog-button',
|
||||
callback: function()
|
||||
{
|
||||
window.location.href = '/product/new?prefillbarcode=' + encodeURIComponent(input) + '&returnto=' + encodeURIComponent(window.location.pathname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).on('keypress', function(e)
|
||||
{
|
||||
if (e.key === 'B' || e.key === 'b')
|
||||
{
|
||||
$('.add-new-barcode-dialog-button').click();
|
||||
}
|
||||
if (e.key === 'p' || e.key === 'P')
|
||||
{
|
||||
$('.add-new-product-dialog-button').click();
|
||||
}
|
||||
if (e.key === 'a' || e.key === 'A')
|
||||
{
|
||||
$('.add-new-product-with-barcode-dialog-button').click();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -176,6 +201,13 @@ $(function()
|
||||
{
|
||||
return 'Wrong date format, needs to be YYYY-MM-DD';
|
||||
}
|
||||
else if (moment($el.val()).isValid())
|
||||
{
|
||||
if (moment($el.val()).isBefore(moment(), 'day'))
|
||||
{
|
||||
return 'This value cannot be before today.';
|
||||
}
|
||||
}
|
||||
},
|
||||
'notequal': function($el)
|
||||
{
|
||||
@@ -231,6 +263,7 @@ $(function()
|
||||
{
|
||||
$('#addbarcodetoselection').text(addBarcode);
|
||||
$('#flow-info-addbarcodetoselection').removeClass('hide');
|
||||
$('#barcode-lookup-disabled-hint').removeClass('hide');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -258,11 +291,13 @@ $('#best_before_date').on('keypress', function(e)
|
||||
|
||||
$('.datepicker').datepicker('hide');
|
||||
|
||||
if (value.length === 0)
|
||||
//If input is empty and any arrow key is pressed, set date to today
|
||||
if (value.length === 0 && (e.keyCode === 38 || e.keyCode === 40 || e.keyCode === 37 || e.keyCode === 39))
|
||||
{
|
||||
element.val(moment().format('YYYY-MM-DD'));
|
||||
dateObj = moment(new Date(), 'YYYY-MM-DD', true);
|
||||
}
|
||||
else if (dateObj.isValid())
|
||||
|
||||
if (dateObj.isValid())
|
||||
{
|
||||
if (e.keyCode === 38) //Up
|
||||
{
|
||||
|
@@ -1,10 +1,11 @@
|
||||
<div class="col-sm-4 col-sm-offset-3 col-md-3 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">Inventory</h1>
|
||||
|
||||
<form id="inventory-form">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="product_id">Product <i class="fa fa-barcode"></i></label>
|
||||
<label for="product_id">Product <i class="fa fa-barcode"></i><span id="barcode-lookup-disabled-hint" class="small text-muted hide"> Barcode lookup is disabled</span></label>
|
||||
<select class="form-control combobox" id="product_id" name="product_id" required>
|
||||
<option value=""></option>
|
||||
<?php foreach ($products as $product) : ?>
|
||||
@@ -36,6 +37,7 @@
|
||||
<button id="save-inventory-button" type="submit" class="btn btn-default">OK</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-sm-6 col-md-5 col-lg-3 main well">
|
||||
|
@@ -52,6 +52,9 @@
|
||||
<li data-nav-for-page="inventory.php">
|
||||
<a class="discrete-link" href="/inventory"><i class="fa fa-list fa-fw"></i> Inventory</a>
|
||||
</li>
|
||||
<li data-nav-for-page="shoppinglist.php">
|
||||
<a class="discrete-link" href="/shoppinglist"><i class="fa fa-shopping-bag fa-fw"></i> Shopping list</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
@@ -88,6 +91,9 @@
|
||||
<li data-nav-for-page="inventory.php">
|
||||
<a class="discrete-link" href="/inventory"><i class="fa fa-list fa-fw"></i> Inventory</a>
|
||||
</li>
|
||||
<li data-nav-for-page="shoppinglist.php">
|
||||
<a class="discrete-link" href="/shoppinglist"><i class="fa fa-shopping-bag fa-fw"></i> Shopping list</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<ul class="nav nav-sidebar">
|
||||
|
@@ -75,6 +75,13 @@ $(function()
|
||||
$('#name').val(prefillName);
|
||||
$('#name').focus();
|
||||
}
|
||||
|
||||
var prefillBarcode = Grocy.GetUriParam('prefillbarcode');
|
||||
if (prefillBarcode !== undefined)
|
||||
{
|
||||
$('#barcode-taginput').tagsManager('pushTag', prefillBarcode);
|
||||
$('#name').focus();
|
||||
}
|
||||
});
|
||||
|
||||
$('.input-group-qu').on('change', function(e)
|
||||
|
@@ -35,6 +35,7 @@
|
||||
}
|
||||
|
||||
toastr.success('Added ' + amount + ' ' + productDetails.quantity_unit_stock.name + ' of ' + productDetails.product.name + ' to stock');
|
||||
Grocy.Wait(1000);
|
||||
|
||||
if (addBarcode !== undefined)
|
||||
{
|
||||
@@ -81,7 +82,7 @@ $('#product_id').on('change', function(e)
|
||||
$('#selected-product-last-purchased-timeago').text($.timeago(productDetails.last_purchased || ''));
|
||||
$('#selected-product-last-used').text((productDetails.last_used || 'never').substring(0, 10));
|
||||
$('#selected-product-last-used-timeago').text($.timeago(productDetails.last_used || ''));
|
||||
$('#new_amount_qu_unit').text(productDetails.quantity_unit_stock.name);
|
||||
$('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name);
|
||||
|
||||
if (productDetails.product.default_best_before_days.toString() !== '0')
|
||||
{
|
||||
@@ -138,6 +139,8 @@ $(function()
|
||||
message: '<strong>' + input + '</strong> could not be resolved to a product, how do you want to proceed?',
|
||||
title: 'Create or assign product',
|
||||
onEscape: function() { },
|
||||
size: 'large',
|
||||
backdrop: true,
|
||||
buttons: {
|
||||
cancel: {
|
||||
label: 'Cancel',
|
||||
@@ -145,22 +148,44 @@ $(function()
|
||||
callback: function() { }
|
||||
},
|
||||
addnewproduct: {
|
||||
label: 'Add as new product',
|
||||
className: 'btn-success',
|
||||
label: 'Add as new <u><strong>p</strong></u>roduct',
|
||||
className: 'btn-success add-new-product-dialog-button',
|
||||
callback: function()
|
||||
{
|
||||
window.location.href = '/product/new?prefillname=' + encodeURIComponent(input) + '&returnto=' + encodeURIComponent(window.location.pathname);
|
||||
}
|
||||
},
|
||||
addbarcode: {
|
||||
label: 'Add as barcode to existing product',
|
||||
className: 'btn-info',
|
||||
label: 'Add as <u><strong>b</strong></u>arcode to existing product',
|
||||
className: 'btn-info add-new-barcode-dialog-button',
|
||||
callback: function()
|
||||
{
|
||||
window.location.href = '/purchase?addbarcodetoselection=' + encodeURIComponent(input);
|
||||
}
|
||||
},
|
||||
addnewproductwithbarcode: {
|
||||
label: '<u><strong>A</strong></u>dd as new product + prefill barcode',
|
||||
className: 'btn-warning add-new-product-with-barcode-dialog-button',
|
||||
callback: function()
|
||||
{
|
||||
window.location.href = '/product/new?prefillbarcode=' + encodeURIComponent(input) + '&returnto=' + encodeURIComponent(window.location.pathname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).on('keypress', function(e)
|
||||
{
|
||||
if (e.key === 'B' || e.key === 'b')
|
||||
{
|
||||
$('.add-new-barcode-dialog-button').click();
|
||||
}
|
||||
if (e.key === 'p' || e.key === 'P')
|
||||
{
|
||||
$('.add-new-product-dialog-button').click();
|
||||
}
|
||||
if (e.key === 'a' || e.key === 'A')
|
||||
{
|
||||
$('.add-new-product-with-barcode-dialog-button').click();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -181,6 +206,13 @@ $(function()
|
||||
{
|
||||
return 'Wrong date format, needs to be YYYY-MM-DD';
|
||||
}
|
||||
else if (moment($el.val()).isValid())
|
||||
{
|
||||
if (moment($el.val()).isBefore(moment(), 'day'))
|
||||
{
|
||||
return 'This value cannot be before today.';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -229,6 +261,7 @@ $(function()
|
||||
{
|
||||
$('#addbarcodetoselection').text(addBarcode);
|
||||
$('#flow-info-addbarcodetoselection').removeClass('hide');
|
||||
$('#barcode-lookup-disabled-hint').removeClass('hide');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -256,11 +289,13 @@ $('#best_before_date').on('keypress', function(e)
|
||||
|
||||
$('.datepicker').datepicker('hide');
|
||||
|
||||
if (value.length === 0)
|
||||
//If input is empty and any arrow key is pressed, set date to today
|
||||
if (value.length === 0 && (e.keyCode === 38 || e.keyCode === 40 || e.keyCode === 37 || e.keyCode === 39))
|
||||
{
|
||||
element.val(moment().format('YYYY-MM-DD'));
|
||||
dateObj = moment(new Date(), 'YYYY-MM-DD', true);
|
||||
}
|
||||
else if (dateObj.isValid())
|
||||
|
||||
if (dateObj.isValid())
|
||||
{
|
||||
if (e.keyCode === 38) //Up
|
||||
{
|
||||
|
@@ -1,10 +1,11 @@
|
||||
<div class="col-sm-4 col-sm-offset-3 col-md-3 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">Purchase</h1>
|
||||
|
||||
<form id="purchase-form">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="product_id">Product <i class="fa fa-barcode"></i></label>
|
||||
<label for="product_id">Product <i class="fa fa-barcode"></i><span id="barcode-lookup-disabled-hint" class="small text-muted hide"> Barcode lookup is disabled</span></label>
|
||||
<select class="form-control combobox" id="product_id" name="product_id" required>
|
||||
<option value=""></option>
|
||||
<?php foreach ($products as $product) : ?>
|
||||
@@ -27,7 +28,7 @@
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="amount">Amount <span id="new_amount_qu_unit" class="small text-muted"></span></label>
|
||||
<label for="amount">Amount <span id="amount_qu_unit" class="small text-muted"></span></label>
|
||||
<input type="number" class="form-control" id="amount" name="amount" value="1" min="1" required>
|
||||
<div class="help-block with-errors"></div>
|
||||
</div>
|
||||
@@ -35,6 +36,7 @@
|
||||
<button id="save-purchase-button" type="submit" class="btn btn-default">OK</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-sm-6 col-md-5 col-lg-3 main well">
|
||||
|
38
views/shoppinglist.js
Normal file
38
views/shoppinglist.js
Normal file
@@ -0,0 +1,38 @@
|
||||
$(document).on('click', '.shoppinglist-delete-button', function(e)
|
||||
{
|
||||
Grocy.FetchJson('/api/delete-object/shopping_list/' + $(e.target).attr('data-shoppinglist-id'),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/shoppinglist';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$(document).on('click', '#add-products-below-min-stock-amount', function(e)
|
||||
{
|
||||
Grocy.FetchJson('/api/stock/add-missing-products-to-shoppinglist',
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/shoppinglist';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$(function()
|
||||
{
|
||||
$('#shoppinglist-table').DataTable({
|
||||
'pageLength': 50,
|
||||
'order': [[1, 'asc']],
|
||||
'columnDefs': [
|
||||
{ 'orderable': false, 'targets': 0 }
|
||||
]
|
||||
});
|
||||
});
|
45
views/shoppinglist.php
Normal file
45
views/shoppinglist.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header">
|
||||
Shopping List
|
||||
<a class="btn btn-default" href="/shoppinglist/new" role="button">
|
||||
<i class="fa fa-plus"></i> Add
|
||||
</a>
|
||||
<a id="add-products-below-min-stock-amount" class="btn btn-info" href="#" role="button">
|
||||
<i class="fa fa-plus"></i> Add products that are below defined min. stock amount
|
||||
</a>
|
||||
</h1>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table id="shoppinglist-table" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Product</th>
|
||||
<th>Amount</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($listItems as $listItem) : ?>
|
||||
<tr>
|
||||
<td class="fit-content">
|
||||
<a class="btn btn-info" href="/shoppinglist/<?php echo $listItem->id; ?>" role="button">
|
||||
<i class="fa fa-pencil"></i>
|
||||
</a>
|
||||
<a class="btn btn-danger shoppinglist-delete-button" href="#" role="button" data-shoppinglist-id="<?php echo $listItem->id; ?>">
|
||||
<i class="fa fa-trash"></i>
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<?php echo GrocyPhpHelper::FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->name; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php echo $listItem->amount + $listItem->amount_autoadded . ' ' . GrocyPhpHelper::FindObjectInArrayByPropertyValue($quantityunits, 'id', GrocyPhpHelper::FindObjectInArrayByPropertyValue($products, 'id', $listItem->product_id)->qu_id_purchase)->name; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div>
|
147
views/shoppinglistform.js
Normal file
147
views/shoppinglistform.js
Normal file
@@ -0,0 +1,147 @@
|
||||
$('#save-shoppinglist-button').on('click', function(e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.PostJson('/api/add-object/shopping_list', $('#shoppinglist-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/shoppinglist';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.PostJson('/api/edit-object/shopping_list/' + Grocy.EditObjectId, $('#shoppinglist-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = '/shoppinglist';
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$('#product_id').on('change', function(e)
|
||||
{
|
||||
var productId = $(e.target).val();
|
||||
|
||||
if (productId)
|
||||
{
|
||||
Grocy.FetchJson('/api/stock/get-product-details/' + productId,
|
||||
function (productDetails)
|
||||
{
|
||||
$('#selected-product-name').text(productDetails.product.name);
|
||||
$('#selected-product-stock-amount').text(productDetails.stock_amount || '0');
|
||||
$('#selected-product-stock-qu-name').text(productDetails.quantity_unit_stock.name);
|
||||
$('#selected-product-stock-qu-name2').text(productDetails.quantity_unit_stock.name);
|
||||
$('#selected-product-last-purchased').text((productDetails.last_purchased || 'never').substring(0, 10));
|
||||
$('#selected-product-last-purchased-timeago').text($.timeago(productDetails.last_purchased || ''));
|
||||
$('#selected-product-last-used').text((productDetails.last_used || 'never').substring(0, 10));
|
||||
$('#selected-product-last-used-timeago').text($.timeago(productDetails.last_used || ''));
|
||||
$('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name);
|
||||
|
||||
Grocy.EmptyElementWhenMatches('#selected-product-last-purchased-timeago', 'NaN years ago');
|
||||
Grocy.EmptyElementWhenMatches('#selected-product-last-used-timeago', 'NaN years ago');
|
||||
|
||||
if ($('#product_id').hasClass('suppress-next-custom-validate-event'))
|
||||
{
|
||||
$('#product_id').removeClass('suppress-next-custom-validate-event');
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.FetchJson('/api/get-objects/shopping_list',
|
||||
function (currentShoppingListItems)
|
||||
{
|
||||
if (currentShoppingListItems.filter(function (e) { return e.product_id === productId; }).length > 0)
|
||||
{
|
||||
$('#product_id').val('');
|
||||
$('#product_id_text_input').val('');
|
||||
$('#product_id_text_input').addClass('has-error');
|
||||
$('#product_id_text_input').parent('.input-group').addClass('has-error');
|
||||
$('#product_id_text_input').closest('.form-group').addClass('has-error');
|
||||
$('#product-error').text('This product is already on the shopping list.');
|
||||
$('#product-error').show();
|
||||
$('#product_id_text_input').focus();
|
||||
}
|
||||
else
|
||||
{
|
||||
$('#product_id_text_input').removeClass('has-error');
|
||||
$('#product_id_text_input').parent('.input-group').removeClass('has-error');
|
||||
$('#product_id_text_input').closest('.form-group').removeClass('has-error');
|
||||
$('#product-error').hide();
|
||||
}
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$(function()
|
||||
{
|
||||
$('.combobox').combobox({
|
||||
appendId: '_text_input'
|
||||
});
|
||||
|
||||
$('#product_id_text_input').on('change', function(e)
|
||||
{
|
||||
var input = $('#product_id_text_input').val().toString();
|
||||
var possibleOptionElement = $("#product_id option[data-additional-searchdata*='" + input + "']").first();
|
||||
|
||||
if (possibleOptionElement.length > 0 && possibleOptionElement.text().length > 0) {
|
||||
$('#product_id').val(possibleOptionElement.val());
|
||||
$('#product_id').data('combobox').refresh();
|
||||
$('#product_id').trigger('change');
|
||||
}
|
||||
});
|
||||
|
||||
$('#product_id_text_input').focus();
|
||||
$('#product_id_text_input').trigger('change');
|
||||
|
||||
if (Grocy.EditMode === 'edit')
|
||||
{
|
||||
$('#product_id').addClass('suppress-next-custom-validate-event');
|
||||
$('#product_id').trigger('change');
|
||||
}
|
||||
|
||||
$('#shoppinglist-form').validator();
|
||||
$('#shoppinglist-form').validator('validate');
|
||||
|
||||
$('#amount').on('focus', function(e)
|
||||
{
|
||||
if ($('#product_id_text_input').val().length === 0)
|
||||
{
|
||||
$('#product_id_text_input').focus();
|
||||
}
|
||||
});
|
||||
|
||||
$('#shoppinglist-form input').keydown(function(event)
|
||||
{
|
||||
if (event.keyCode === 13) //Enter
|
||||
{
|
||||
if ($('#shoppinglist-form').validator('validate').has('.has-error').length !== 0) //There is at least one validation error
|
||||
{
|
||||
event.preventDefault();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
45
views/shoppinglistform.php
Normal file
45
views/shoppinglistform.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<div class="col-sm-3 col-sm-offset-3 col-md-3 col-md-offset-2 main">
|
||||
|
||||
<h1 class="page-header"><?php echo $title; ?></h1>
|
||||
|
||||
<script>Grocy.EditMode = '<?php echo $mode; ?>';</script>
|
||||
|
||||
<?php if ($mode == 'edit') : ?>
|
||||
<script>Grocy.EditObjectId = <?php echo $listItem->id; ?>;</script>
|
||||
<?php endif; ?>
|
||||
|
||||
<form id="shoppinglist-form">
|
||||
|
||||
<div class="form-group">
|
||||
<label for="product_id">Product <i class="fa fa-barcode"></i></label>
|
||||
<select class="form-control combobox" id="product_id" name="product_id" value="<?php if ($mode == 'edit') echo $listItem->product_id; ?>" required>
|
||||
<option value=""></option>
|
||||
<?php foreach ($products as $product) : ?>
|
||||
<option <?php if ($mode == 'edit' && $product->id == $listItem->product_id) echo 'selected="selected"'; ?> data-additional-searchdata="<?php echo $product->barcode; ?>" value="<?php echo $product->id; ?>"><?php echo $product->name; ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<div id="product-error" class="help-block with-errors"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="amount">Amount <span id="amount_qu_unit" class="small text-muted"></span></label>
|
||||
<input type="number" class="form-control" id="amount" name="amount" value="<?php if ($mode == 'edit') echo $listItem->amount; else echo '1'; ?>" min="1" required>
|
||||
<div class="help-block with-errors"></div>
|
||||
</div>
|
||||
|
||||
<button id="save-shoppinglist-button" type="submit" class="btn btn-default">Save</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-sm-6 col-md-5 col-lg-3 main well">
|
||||
<h3>Product overview <strong><span id="selected-product-name"></span></strong></h3>
|
||||
<h4><strong>Stock quantity unit:</strong> <span id="selected-product-stock-qu-name"></span></h4>
|
||||
|
||||
<p>
|
||||
<strong>Stock amount:</strong> <span id="selected-product-stock-amount"></span> <span id="selected-product-stock-qu-name2"></span><br />
|
||||
<strong>Last purchased:</strong> <span id="selected-product-last-purchased"></span> <time id="selected-product-last-purchased-timeago" class="timeago timeago-contextual"></time><br />
|
||||
<strong>Last used:</strong> <span id="selected-product-last-used"></span> <time id="selected-product-last-used-timeago" class="timeago timeago-contextual"></time>
|
||||
</p>
|
||||
</div>
|
Reference in New Issue
Block a user