2018-04-11 19:49:35 +02:00
< ? php
namespace Grocy\Controllers ;
2026-04-24 19:59:42 +02:00
use DI\Container ;
2021-06-12 17:21:12 +02:00
use Grocy\Helpers\Grocycode ;
2026-04-20 22:46:47 +02:00
use Grocy\Services\LocalizationService ;
2020-10-31 10:25:33 -05:00
use Grocy\Services\RecipesService ;
2026-04-20 22:46:47 +02:00
use Grocy\Services\StockService ;
use Grocy\Services\UserfieldsService ;
2026-04-24 19:59:42 +02:00
use Grocy\Services\UsersService ;
2023-05-13 14:43:51 +02:00
use Psr\Http\Message\ResponseInterface as Response ;
use Psr\Http\Message\ServerRequestInterface as Request ;
2020-10-31 10:25:33 -05:00
2018-04-11 19:49:35 +02:00
class StockController extends BaseController
{
2021-07-16 17:32:08 +02:00
use GrocycodeTrait ;
2025-01-12 13:58:47 +01:00
public function __construct ( Container $container )
{
parent :: __construct ( $container );
try
{
2026-04-20 22:46:47 +02:00
$externalBarcodeLookupPluginName = StockService :: GetInstance () -> GetExternalBarcodeLookupPluginName ();
2025-01-12 13:58:47 +01:00
}
catch ( \Exception )
{
$externalBarcodeLookupPluginName = '' ;
}
finally
{
$this -> View -> set ( 'ExternalBarcodeLookupPluginName' , $externalBarcodeLookupPluginName );
}
}
2023-05-13 14:43:51 +02:00
public function Consume ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'consume' , [
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> where ( 'id IN (SELECT product_id from stock_current WHERE amount_aggregated > 0)' ) -> orderBy ( 'name' ),
'barcodes' => $this -> DB -> product_barcodes_comma_separated (),
'recipes' => $this -> DB -> recipes () -> where ( 'type' , RecipesService :: RECIPE_TYPE_NORMAL ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved ()
2018-04-11 19:49:35 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function Inventory ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'inventory' , [
'products' => $this -> DB -> products () -> where ( 'active = 1 AND no_own_stock = 0' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'barcodes' => $this -> DB -> product_barcodes_comma_separated (),
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'stock' )
2018-04-11 19:49:35 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function Journal ( Request $request , Response $response , array $args )
2019-12-19 12:48:36 -06:00
{
2021-07-13 19:29:23 +02:00
if ( isset ( $request -> getQueryParams ()[ 'months' ]) && filter_var ( $request -> getQueryParams ()[ 'months' ], FILTER_VALIDATE_INT ) !== false )
{
$months = $request -> getQueryParams ()[ 'months' ];
$where = " row_created_timestamp > DATE(DATE('now', 'localtime'), '- $months months') " ;
}
else
{
// Default 6 months
$where = " row_created_timestamp > DATE(DATE('now', 'localtime'), '-6 months') " ;
}
if ( isset ( $request -> getQueryParams ()[ 'product' ]) && filter_var ( $request -> getQueryParams ()[ 'product' ], FILTER_VALIDATE_INT ) !== false )
{
$productId = $request -> getQueryParams ()[ 'product' ];
$where .= " AND product_id = $productId " ;
}
2026-04-20 22:46:47 +02:00
$usersService = UsersService :: GetInstance ();
2020-11-17 19:11:02 +01:00
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'stockjournal' , [
'stockLog' => $this -> DB -> uihelper_stock_journal () -> where ( $where ) -> orderBy ( 'row_created_timestamp' , 'DESC' ),
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-17 19:11:02 +01:00
'users' => $usersService -> GetUsersAsDto (),
2022-03-30 17:32:53 +02:00
'transactionTypes' => GetClassConstants ( '\Grocy\Services\StockService' , 'TRANSACTION_TYPE_' ),
2026-04-20 22:46:47 +02:00
'userfieldsStock' => UserfieldsService :: GetInstance () -> GetFields ( 'stock' ),
'userfieldValuesStock' => UserfieldsService :: GetInstance () -> GetAllValues ( 'stock' )
2019-12-19 12:48:36 -06:00
]);
}
2023-05-13 14:43:51 +02:00
public function LocationContentSheet ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'locationcontentsheet' , [
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'currentStockLocationContent' => StockService :: GetInstance () -> GetCurrentStockLocationContent ( isset ( $request -> getQueryParams ()[ 'include_out_of_stock' ]))
2018-04-11 19:49:35 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function LocationEditForm ( Request $request , Response $response , array $args )
2018-10-20 14:55:49 +02:00
{
2020-08-31 20:40:31 +02:00
if ( $args [ 'locationId' ] == 'new' )
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'locationform' , [
2020-08-31 20:40:31 +02:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'locations' )
2020-08-31 20:40:31 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'locationform' , [
'location' => $this -> DB -> locations ( $args [ 'locationId' ]),
2020-08-31 20:40:31 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'locations' )
2020-08-31 20:40:31 +02:00
]);
}
2018-10-20 14:55:49 +02:00
}
2023-05-13 14:43:51 +02:00
public function LocationsList ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2023-05-13 14:24:52 +02:00
if ( isset ( $request -> getQueryParams ()[ 'include_disabled' ]))
{
2026-04-20 22:46:47 +02:00
$locations = $this -> DB -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
else
{
2026-04-20 22:46:47 +02:00
$locations = $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'locations' , [
2023-05-13 14:24:52 +02:00
'locations' => $locations ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'locations' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'locations' )
2018-04-11 19:49:35 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function Overview ( Request $request , Response $response , array $args )
2020-03-25 19:34:56 +01:00
{
2026-04-20 22:46:47 +02:00
$usersService = UsersService :: GetInstance ();
2025-01-17 17:22:37 +01:00
$userSettings = $usersService -> GetUserSettings ( GROCY_USER_ID );
$nextXDays = $userSettings [ 'stock_due_soon_days' ];
2020-03-25 19:34:56 +01:00
2025-01-15 21:44:45 +01:00
$where = 'is_in_stock_or_below_min_stock = 1' ;
2025-01-17 17:22:37 +01:00
if ( boolval ( $userSettings [ 'stock_overview_show_all_out_of_stock_products' ]))
2025-01-15 21:44:45 +01:00
{
$where = '1=1' ;
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'stockoverview' , [
'currentStock' => $this -> DB -> uihelper_stock_current_overview () -> where ( $where ),
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'currentStockLocations' => StockService :: GetInstance () -> GetCurrentStockLocations (),
2020-08-31 20:40:31 +02:00
'nextXDays' => $nextXDays ,
2026-04-20 22:46:47 +02:00
'productGroups' => $this -> DB -> product_groups () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'products' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'products' )
2018-09-24 13:02:52 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function ProductBarcodesEditForm ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-08-31 20:40:31 +02:00
$product = null ;
if ( isset ( $request -> getQueryParams ()[ 'product' ]))
{
2026-04-20 22:46:47 +02:00
$product = $this -> DB -> products ( $request -> getQueryParams ()[ 'product' ]);
2020-08-31 20:40:31 +02:00
}
if ( $args [ 'productBarcodeId' ] == 'new' )
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productbarcodeform' , [
2020-08-31 20:40:31 +02:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'barcodes' => $this -> DB -> product_barcodes () -> orderBy ( 'barcode' ),
2020-08-31 20:40:31 +02:00
'product' => $product ,
2026-04-20 22:46:47 +02:00
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_barcodes' )
2020-08-31 20:40:31 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productbarcodeform' , [
2020-08-31 20:40:31 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'barcode' => $this -> DB -> product_barcodes ( $args [ 'productBarcodeId' ]),
2020-08-31 20:40:31 +02:00
'product' => $product ,
2026-04-20 22:46:47 +02:00
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_barcodes' )
2020-08-31 20:40:31 +02:00
]);
}
2018-04-11 19:49:35 +02:00
}
2023-05-13 14:43:51 +02:00
public function ProductEditForm ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
if ( $args [ 'productId' ] == 'new' )
{
2026-04-20 22:46:47 +02:00
$quantityunits = $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' );
2026-02-04 21:49:46 +01:00
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productform' , [
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' ),
'barcodes' => $this -> DB -> product_barcodes () -> orderBy ( 'barcode' ),
2026-02-04 21:49:46 +01:00
'quantityunitsAll' => $quantityunits ,
'quantityunitsReferenced' => $quantityunits ,
2026-04-20 22:46:47 +02:00
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productgroups' => $this -> DB -> product_groups () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'products' ),
'products' => $this -> DB -> products () -> where ( 'parent_product_id IS NULL and active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2019-09-14 17:34:36 +02:00
'isSubProductOfOthers' => false ,
2018-04-11 19:49:35 +02:00
'mode' => 'create'
]);
}
else
{
2026-04-20 22:46:47 +02:00
$product = $this -> DB -> products ( $args [ 'productId' ]);
2019-09-14 17:34:36 +02:00
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productform' , [
2020-08-31 20:40:31 +02:00
'product' => $product ,
2026-04-20 22:46:47 +02:00
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'barcodes' => $this -> DB -> product_barcodes () -> orderBy ( 'barcode' ),
'quantityunitsAll' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunitsReferenced' => $this -> DB -> quantity_units () -> where ( 'id IN (SELECT to_qu_id FROM cache__quantity_unit_conversions_resolved WHERE product_id = :1) OR NOT EXISTS(SELECT 1 FROM stock_log WHERE product_id = :1)' , $product -> id ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productgroups' => $this -> DB -> product_groups () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'products' ),
'products' => $this -> DB -> products () -> where ( 'id != :1 AND parent_product_id IS NULL and active = 1' , $product -> id ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'isSubProductOfOthers' => $this -> DB -> products () -> where ( 'parent_product_id = :1' , $product -> id ) -> count () !== 0 ,
2019-09-15 16:40:54 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'quConversions' => $this -> DB -> quantity_unit_conversions () -> where ( 'product_id' , $product -> id ),
'productBarcodeUserfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_barcodes' ),
'productBarcodeUserfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'product_barcodes' )
2018-04-11 19:49:35 +02:00
]);
}
}
2023-05-13 14:43:51 +02:00
public function ProductGrocycodeImage ( Request $request , Response $response , array $args )
2021-06-12 17:21:12 +02:00
{
2021-07-13 19:29:23 +02:00
$gc = new Grocycode ( Grocycode :: PRODUCT , $args [ 'productId' ]);
2021-07-16 17:32:08 +02:00
return $this -> ServeGrocycodeImage ( $request , $response , $gc );
2021-06-12 17:21:12 +02:00
}
2023-05-13 14:43:51 +02:00
public function ProductGroupEditForm ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-08-31 20:40:31 +02:00
if ( $args [ 'productGroupId' ] == 'new' )
2018-04-11 19:49:35 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productgroupform' , [
2019-04-22 22:16:35 +02:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_groups' )
2018-04-11 19:49:35 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productgroupform' , [
'group' => $this -> DB -> product_groups ( $args [ 'productGroupId' ]),
2019-04-22 22:16:35 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_groups' )
2018-04-11 19:49:35 +02:00
]);
}
}
2023-05-13 14:43:51 +02:00
public function ProductGroupsList ( Request $request , Response $response , array $args )
2020-03-25 19:34:56 +01:00
{
2023-05-13 14:24:52 +02:00
if ( isset ( $request -> getQueryParams ()[ 'include_disabled' ]))
{
2026-04-20 22:46:47 +02:00
$productGroups = $this -> DB -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
else
{
2026-04-20 22:46:47 +02:00
$productGroups = $this -> DB -> product_groups () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'productgroups' , [
2023-05-13 14:24:52 +02:00
'productGroups' => $productGroups ,
2026-04-20 22:46:47 +02:00
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_groups' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'product_groups' )
2020-08-31 20:40:31 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function ProductsList ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
2026-04-20 22:46:47 +02:00
$products = $this -> DB -> products ();
2021-07-06 20:08:02 +02:00
if ( ! isset ( $request -> getQueryParams ()[ 'include_disabled' ]))
2020-12-07 19:48:33 +01:00
{
2021-07-06 20:08:02 +02:00
$products = $products -> where ( 'active = 1' );
2020-12-07 19:48:33 +01:00
}
2021-07-06 20:08:02 +02:00
if ( isset ( $request -> getQueryParams ()[ 'only_in_stock' ]))
2020-12-07 19:48:33 +01:00
{
2021-07-06 20:08:02 +02:00
$products = $products -> where ( 'id IN (SELECT product_id from stock_current WHERE amount_aggregated > 0)' );
2020-12-07 19:48:33 +01:00
}
2023-04-13 20:28:28 +02:00
if ( isset ( $request -> getQueryParams ()[ 'only_out_of_stock' ]))
{
$products = $products -> where ( 'id NOT IN (SELECT product_id from stock_current WHERE amount_aggregated > 0)' );
}
2020-12-07 19:48:33 +01:00
2021-07-06 20:08:02 +02:00
$products = $products -> orderBy ( 'name' , 'COLLATE NOCASE' );
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'products' , [
2020-12-07 19:48:33 +01:00
'products' => $products ,
2026-04-20 22:46:47 +02:00
'locations' => $this -> DB -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productGroups' => $this -> DB -> product_groups () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'shoppingLocations' => $this -> DB -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'products' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'products' )
2020-08-31 20:40:31 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function Purchase ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'purchase' , [
'products' => $this -> DB -> products () -> where ( 'active = 1 AND no_own_stock = 0' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'barcodes' => $this -> DB -> product_barcodes_comma_separated (),
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'stock' )
2020-08-31 20:40:31 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function QuantityUnitConversionEditForm ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
$product = null ;
if ( isset ( $request -> getQueryParams ()[ 'product' ]))
2020-03-25 19:34:56 +01:00
{
2026-04-20 22:46:47 +02:00
$product = $this -> DB -> products ( $request -> getQueryParams ()[ 'product' ]);
2020-03-25 19:34:56 +01:00
}
2020-08-31 20:40:31 +02:00
$defaultQuUnit = null ;
if ( isset ( $request -> getQueryParams ()[ 'qu-unit' ]))
2020-03-25 19:34:56 +01:00
{
2026-04-20 22:46:47 +02:00
$defaultQuUnit = $this -> DB -> quantity_units ( $request -> getQueryParams ()[ 'qu-unit' ]);
2020-03-25 19:34:56 +01:00
}
2020-08-31 20:40:31 +02:00
if ( $args [ 'quConversionId' ] == 'new' )
2018-09-24 13:02:52 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunitconversionform' , [
2019-04-22 22:16:35 +02:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'quantity_unit_conversions' ),
'quantityunits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'product' => $product ,
'defaultQuUnit' => $defaultQuUnit
2018-09-24 13:02:52 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunitconversionform' , [
'quConversion' => $this -> DB -> quantity_unit_conversions ( $args [ 'quConversionId' ]),
2019-04-22 22:16:35 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'quantity_unit_conversions' ),
'quantityunits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'product' => $product ,
'defaultQuUnit' => $defaultQuUnit
2018-09-24 13:02:52 +02:00
]);
}
}
2023-05-13 14:43:51 +02:00
public function QuantityUnitEditForm ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
if ( $args [ 'quantityunitId' ] == 'new' )
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunitform' , [
2019-04-22 22:16:35 +02:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'quantity_units' ),
'pluralCount' => LocalizationService :: GetInstance () -> GetPluralCount (),
'pluralRule' => LocalizationService :: GetInstance () -> GetPluralDefinition ()
2018-04-11 19:49:35 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
$quantityUnit = $this -> DB -> quantity_units ( $args [ 'quantityunitId' ]);
2019-09-15 16:40:54 +02:00
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunitform' , [
2020-08-31 20:40:31 +02:00
'quantityUnit' => $quantityUnit ,
2019-04-22 22:16:35 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'quantity_units' ),
'pluralCount' => LocalizationService :: GetInstance () -> GetPluralCount (),
'pluralRule' => LocalizationService :: GetInstance () -> GetPluralDefinition (),
'defaultQuConversions' => $this -> DB -> quantity_unit_conversions () -> where ( 'from_qu_id = :1 AND product_id IS NULL' , $quantityUnit -> id ),
'quantityUnits' => $this -> DB -> quantity_units ()
2018-04-11 19:49:35 +02:00
]);
}
2020-08-31 20:40:31 +02:00
}
2023-05-13 14:43:51 +02:00
public function QuantityUnitPluralFormTesting ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunitpluraltesting' , [
'quantityUnits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' )
2020-08-31 20:40:31 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function QuantityUnitsList ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
2023-05-13 14:24:52 +02:00
if ( isset ( $request -> getQueryParams ()[ 'include_disabled' ]))
{
2026-04-20 22:46:47 +02:00
$quantityUnits = $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
else
{
2026-04-20 22:46:47 +02:00
$quantityUnits = $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunits' , [
2023-05-13 14:24:52 +02:00
'quantityunits' => $quantityUnits ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'quantity_units' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'quantity_units' )
2020-08-31 20:40:31 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function ShoppingList ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
$listId = 1 ;
if ( isset ( $request -> getQueryParams ()[ 'list' ]))
{
$listId = $request -> getQueryParams ()[ 'list' ];
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglist' , [
'listItems' => $this -> DB -> uihelper_shopping_list () -> where ( 'shopping_list_id = :1' , $listId ) -> orderBy ( 'product_name' , 'COLLATE NOCASE' ),
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> DB -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'missingProducts' => StockService :: GetInstance () -> GetMissingProducts (),
'shoppingLists' => $this -> DB -> shopping_lists_view () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'selectedShoppingListId' => $listId ,
2026-04-20 22:46:47 +02:00
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'productUserfields' => UserfieldsService :: GetInstance () -> GetFields ( 'products' ),
'productUserfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'products' ),
'productGroupUserfields' => UserfieldsService :: GetInstance () -> GetFields ( 'product_groups' ),
'productGroupUserfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'product_groups' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_list' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'shopping_list' )
2020-08-31 20:40:31 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function ShoppingListEditForm ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
if ( $args [ 'listId' ] == 'new' )
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglistform' , [
2020-11-17 19:11:02 +01:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_lists' )
2020-08-31 20:40:31 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglistform' , [
'shoppingList' => $this -> DB -> shopping_lists ( $args [ 'listId' ]),
2020-11-17 19:11:02 +01:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_lists' )
2020-08-31 20:40:31 +02:00
]);
}
2018-04-11 19:49:35 +02:00
}
2023-05-13 14:43:51 +02:00
public function ShoppingListItemEditForm ( Request $request , Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
if ( $args [ 'itemId' ] == 'new' )
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglistitemform' , [
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'barcodes' => $this -> DB -> product_barcodes_comma_separated (),
'shoppingLists' => $this -> DB -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-09 22:15:25 +01:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'quantityUnits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_list' )
2018-04-11 19:49:35 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglistitemform' , [
'listItem' => $this -> DB -> shopping_list ( $args [ 'itemId' ]),
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'barcodes' => $this -> DB -> product_barcodes_comma_separated (),
'shoppingLists' => $this -> DB -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-09 22:15:25 +01:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'quantityUnits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved (),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_list' )
2018-04-11 19:49:35 +02:00
]);
}
}
2018-10-27 17:26:00 +02:00
2023-05-13 14:43:51 +02:00
public function ShoppingListSettings ( Request $request , Response $response , array $args )
2019-04-20 17:04:40 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglistsettings' , [
'shoppingLists' => $this -> DB -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' )
2022-04-03 13:00:14 +02:00
]);
2020-08-31 20:40:31 +02:00
}
2023-05-13 14:43:51 +02:00
public function ShoppingLocationEditForm ( Request $request , Response $response , array $args )
2020-08-31 20:40:31 +02:00
{
if ( $args [ 'shoppingLocationId' ] == 'new' )
2019-04-20 17:04:40 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglocationform' , [
2020-08-31 20:40:31 +02:00
'mode' => 'create' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_locations' )
2019-04-20 17:04:40 +02:00
]);
}
else
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglocationform' , [
'shoppingLocation' => $this -> DB -> shopping_locations ( $args [ 'shoppingLocationId' ]),
2020-08-31 20:40:31 +02:00
'mode' => 'edit' ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_locations' )
2019-04-20 17:04:40 +02:00
]);
}
}
2023-05-13 14:43:51 +02:00
public function ShoppingLocationsList ( Request $request , Response $response , array $args )
2020-02-01 12:54:05 +01:00
{
2023-05-13 14:24:52 +02:00
if ( isset ( $request -> getQueryParams ()[ 'include_disabled' ]))
{
2026-04-20 22:46:47 +02:00
$shoppingLocations = $this -> DB -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
else
{
2026-04-20 22:46:47 +02:00
$shoppingLocations = $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' );
2023-05-13 14:24:52 +02:00
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'shoppinglocations' , [
2023-05-13 14:24:52 +02:00
'shoppinglocations' => $shoppingLocations ,
2026-04-20 22:46:47 +02:00
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'shopping_locations' ),
'userfieldValues' => UserfieldsService :: GetInstance () -> GetAllValues ( 'shopping_locations' )
2020-08-31 20:40:31 +02:00
]);
2020-02-01 12:54:05 +01:00
}
2023-05-13 14:43:51 +02:00
public function StockEntryEditForm ( Request $request , Response $response , array $args )
2018-10-27 17:26:00 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'stockentryform' , [
'stockEntry' => $this -> DB -> stock () -> where ( 'id' , $args [ 'entryId' ]) -> fetch (),
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => UserfieldsService :: GetInstance () -> GetFields ( 'stock' )
2018-10-27 17:26:00 +02:00
]);
}
2019-08-10 16:34:29 +02:00
2023-05-13 14:43:51 +02:00
public function StockEntryGrocycodeImage ( Request $request , Response $response , array $args )
2021-06-12 17:21:12 +02:00
{
2026-04-20 22:46:47 +02:00
$stockEntry = $this -> DB -> stock () -> where ( 'id' , $args [ 'entryId' ]) -> fetch ();
2021-06-12 17:21:12 +02:00
$gc = new Grocycode ( Grocycode :: PRODUCT , $stockEntry -> product_id , [ $stockEntry -> stock_id ]);
2021-07-16 17:32:08 +02:00
return $this -> ServeGrocycodeImage ( $request , $response , $gc );
2021-06-12 17:21:12 +02:00
}
2023-05-13 14:43:51 +02:00
public function StockEntryGrocycodeLabel ( Request $request , Response $response , array $args )
2021-06-12 17:21:12 +02:00
{
2026-04-20 22:46:47 +02:00
$stockEntry = $this -> DB -> stock () -> where ( 'id' , $args [ 'entryId' ]) -> fetch ();
return $this -> RenderPage ( $response , 'stockentrylabel' , [
2021-06-12 17:21:12 +02:00
'stockEntry' => $stockEntry ,
2026-04-20 22:46:47 +02:00
'product' => $this -> DB -> products ( $stockEntry -> product_id ),
2021-06-12 17:21:12 +02:00
]);
}
2023-05-13 14:43:51 +02:00
public function StockSettings ( Request $request , Response $response , array $args )
2019-08-10 16:34:29 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'stocksettings' , [
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productGroups' => $this -> DB -> product_groups () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' )
2019-08-10 16:34:29 +02:00
]);
}
2019-09-15 16:40:54 +02:00
2023-05-13 14:43:51 +02:00
public function Stockentries ( Request $request , Response $response , array $args )
2020-08-17 14:47:33 -05:00
{
2026-04-20 22:46:47 +02:00
$usersService = UsersService :: GetInstance ();
2020-11-15 19:53:44 +01:00
$nextXDays = $usersService -> GetUserSettings ( GROCY_USER_ID )[ 'stock_due_soon_days' ];
2020-08-17 14:47:33 -05:00
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'stockentries' , [
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'shoppinglocations' => $this -> DB -> shopping_locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'stockEntries' => $this -> DB -> uihelper_stock_entries () -> orderBy ( 'product_id' ),
'currentStockLocations' => StockService :: GetInstance () -> GetCurrentStockLocations (),
2020-08-31 20:40:31 +02:00
'nextXDays' => $nextXDays ,
2026-04-20 22:46:47 +02:00
'userfieldsProducts' => UserfieldsService :: GetInstance () -> GetFields ( 'products' ),
'userfieldValuesProducts' => UserfieldsService :: GetInstance () -> GetAllValues ( 'products' ),
'userfieldsStock' => UserfieldsService :: GetInstance () -> GetFields ( 'stock' ),
'userfieldValuesStock' => UserfieldsService :: GetInstance () -> GetAllValues ( 'stock' )
2020-08-31 20:40:31 +02:00
]);
2020-08-17 14:47:33 -05:00
}
2023-05-13 14:43:51 +02:00
public function Transfer ( Request $request , Response $response , array $args )
2019-09-15 16:40:54 +02:00
{
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'transfer' , [
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> where ( 'no_own_stock = 0 AND id IN (SELECT product_id from stock_current WHERE amount_aggregated > 0)' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'barcodes' => $this -> DB -> product_barcodes_comma_separated (),
'locations' => $this -> DB -> locations () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> DB -> cache__quantity_unit_conversions_resolved ()
2020-08-31 20:40:31 +02:00
]);
2019-09-15 16:40:54 +02:00
}
2019-09-18 20:21:09 +02:00
2023-05-13 14:43:51 +02:00
public function JournalSummary ( Request $request , Response $response , array $args )
2020-09-06 13:18:51 +02:00
{
2026-04-20 22:46:47 +02:00
$entries = $this -> DB -> uihelper_stock_journal_summary ();
2020-09-06 13:18:51 +02:00
if ( isset ( $request -> getQueryParams ()[ 'product_id' ]))
{
$entries = $entries -> where ( 'product_id' , $request -> getQueryParams ()[ 'product_id' ]);
}
if ( isset ( $request -> getQueryParams ()[ 'user_id' ]))
{
$entries = $entries -> where ( 'user_id' , $request -> getQueryParams ()[ 'user_id' ]);
}
if ( isset ( $request -> getQueryParams ()[ 'transaction_type' ]))
{
$entries = $entries -> where ( 'transaction_type' , $request -> getQueryParams ()[ 'transaction_type' ]);
}
2020-11-17 19:11:02 +01:00
2026-04-20 22:46:47 +02:00
$usersService = UsersService :: GetInstance ();
return $this -> RenderPage ( $response , 'stockjournalsummary' , [
2020-11-17 19:11:02 +01:00
'entries' => $entries ,
2026-04-20 22:46:47 +02:00
'products' => $this -> DB -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-17 19:11:02 +01:00
'users' => $usersService -> GetUsersAsDto (),
'transactionTypes' => GetClassConstants ( '\Grocy\Services\StockService' , 'TRANSACTION_TYPE_' )
2020-09-06 13:18:51 +02:00
]);
}
2022-12-04 19:02:15 +01:00
2023-05-13 14:43:51 +02:00
public function QuantityUnitConversionsResolved ( Request $request , Response $response , array $args )
2022-12-04 19:02:15 +01:00
{
$product = null ;
if ( isset ( $request -> getQueryParams ()[ 'product' ]))
{
2026-04-20 22:46:47 +02:00
$product = $this -> DB -> products ( $request -> getQueryParams ()[ 'product' ]);
$quantityUnitConversionsResolved = $this -> DB -> cache__quantity_unit_conversions_resolved () -> where ( 'product_id' , $product -> id );
2022-12-04 19:02:15 +01:00
}
else
{
2026-04-20 22:46:47 +02:00
$quantityUnitConversionsResolved = $this -> DB -> cache__quantity_unit_conversions_resolved () -> where ( 'product_id IS NULL' );
2022-12-04 19:02:15 +01:00
}
2026-04-20 22:46:47 +02:00
return $this -> RenderPage ( $response , 'quantityunitconversionsresolved' , [
2022-12-04 19:02:15 +01:00
'product' => $product ,
2026-04-20 22:46:47 +02:00
'quantityUnits' => $this -> DB -> quantity_units () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-12-04 19:02:15 +01:00
'quantityUnitConversionsResolved' => $quantityUnitConversionsResolved
]);
}
2018-04-11 19:49:35 +02:00
}