2018-04-11 19:49:35 +02:00
< ? php
namespace Grocy\Controllers ;
2021-06-12 17:21:12 +02:00
use Grocy\Helpers\Grocycode ;
2020-10-31 10:25:33 -05:00
use Grocy\Services\RecipesService ;
2018-04-11 19:49:35 +02:00
class StockController extends BaseController
{
2021-07-16 17:32:08 +02:00
use GrocycodeTrait ;
2020-02-11 17:42:03 +01:00
public function Consume ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'consume' , [
2021-07-06 19:31:55 +02:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> where ( 'id IN (SELECT product_id from stock_current WHERE amount_aggregated > 0)' ) -> orderBy ( 'name' ),
2020-12-20 16:00:14 +01:00
'barcodes' => $this -> getDatabase () -> product_barcodes_comma_separated (),
2020-11-17 19:11:02 +01:00
'recipes' => $this -> getDatabase () -> recipes () -> where ( 'type' , RecipesService :: RECIPE_TYPE_NORMAL ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-09 19:25:46 +01:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved ()
2018-04-11 19:49:35 +02:00
]);
}
2020-02-11 17:42:03 +01:00
public function Inventory ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'inventory' , [
2022-04-01 18:49:17 +02:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1 AND no_own_stock = 0' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-12-20 16:00:14 +01:00
'barcodes' => $this -> getDatabase () -> product_barcodes_comma_separated (),
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-03-30 17:32:53 +02:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'stock' )
2018-04-11 19:49:35 +02:00
]);
}
2020-08-31 20:40:31 +02:00
public function Journal ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $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 " ;
}
2020-11-17 19:11:02 +01:00
$usersService = $this -> getUsersService ();
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'stockjournal' , [
2021-07-13 19:29:23 +02:00
'stockLog' => $this -> getDatabase () -> uihelper_stock_journal () -> where ( $where ) -> orderBy ( 'row_created_timestamp' , 'DESC' ),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'users' => $usersService -> GetUsersAsDto (),
2022-03-30 17:32:53 +02:00
'transactionTypes' => GetClassConstants ( '\Grocy\Services\StockService' , 'TRANSACTION_TYPE_' ),
'userfieldsStock' => $this -> getUserfieldsService () -> GetFields ( 'stock' ),
'userfieldValuesStock' => $this -> getUserfieldsService () -> GetAllValues ( 'stock' )
2019-12-19 12:48:36 -06:00
]);
}
2020-08-31 20:40:31 +02:00
public function LocationContentSheet ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'locationcontentsheet' , [
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-04-01 21:55:06 +02:00
'currentStockLocationContent' => $this -> getStockService () -> GetCurrentStockLocationContent ( isset ( $request -> getQueryParams ()[ 'include_out_of_stock' ]))
2018-04-11 19:49:35 +02:00
]);
}
2020-08-31 20:40:31 +02:00
public function LocationEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-10-20 14:55:49 +02:00
{
2020-08-31 20:40:31 +02:00
if ( $args [ 'locationId' ] == 'new' )
{
return $this -> renderPage ( $response , 'locationform' , [
'mode' => 'create' ,
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'locations' )
]);
}
else
{
return $this -> renderPage ( $response , 'locationform' , [
'location' => $this -> getDatabase () -> locations ( $args [ 'locationId' ]),
'mode' => 'edit' ,
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'locations' )
]);
}
2018-10-20 14:55:49 +02:00
}
2020-02-11 17:42:03 +01:00
public function LocationsList ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'locations' , [
2020-11-17 19:11:02 +01:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'locations' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'locations' )
2018-04-11 19:49:35 +02:00
]);
}
2020-08-31 20:40:31 +02:00
public function Overview ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2020-03-25 19:34:56 +01:00
{
2020-08-31 20:40:31 +02:00
$usersService = $this -> getUsersService ();
2020-11-15 19:53:44 +01:00
$nextXDays = $usersService -> GetUserSettings ( GROCY_USER_ID )[ 'stock_due_soon_days' ];
2020-03-25 19:34:56 +01:00
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'stockoverview' , [
'currentStock' => $this -> getStockService () -> GetCurrentStockOverview (),
2020-11-17 19:11:02 +01:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'currentStockLocations' => $this -> getStockService () -> GetCurrentStockLocations (),
'nextXDays' => $nextXDays ,
2020-11-17 19:11:02 +01:00
'productGroups' => $this -> getDatabase () -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'products' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'products' )
2018-09-24 13:02:52 +02:00
]);
}
2020-08-31 20:40:31 +02:00
public function ProductBarcodesEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $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' ]))
{
$product = $this -> getDatabase () -> products ( $request -> getQueryParams ()[ 'product' ]);
}
if ( $args [ 'productBarcodeId' ] == 'new' )
{
2020-11-10 18:11:33 +01:00
return $this -> renderPage ( $response , 'productbarcodeform' , [
2020-08-31 20:40:31 +02:00
'mode' => 'create' ,
'barcodes' => $this -> getDatabase () -> product_barcodes () -> orderBy ( 'barcode' ),
'product' => $product ,
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'product_barcodes' )
2020-08-31 20:40:31 +02:00
]);
}
else
{
2020-11-10 18:11:33 +01:00
return $this -> renderPage ( $response , 'productbarcodeform' , [
2020-08-31 20:40:31 +02:00
'mode' => 'edit' ,
'barcode' => $this -> getDatabase () -> product_barcodes ( $args [ 'productBarcodeId' ]),
'product' => $product ,
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'product_barcodes' )
2020-08-31 20:40:31 +02:00
]);
}
2018-04-11 19:49:35 +02:00
}
2020-02-11 17:42:03 +01:00
public function ProductEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
if ( $args [ 'productId' ] == 'new' )
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'productform' , [
2020-08-31 20:40:31 +02:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' ),
'barcodes' => $this -> getDatabase () -> product_barcodes () -> orderBy ( 'barcode' ),
2020-11-17 19:11:02 +01:00
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-03-13 17:09:07 +01:00
'quantityunitsStock' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productgroups' => $this -> getDatabase () -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'products' ),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> 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
{
2020-03-01 23:47:47 +07:00
$product = $this -> getDatabase () -> products ( $args [ 'productId' ]);
2019-09-14 17:34:36 +02:00
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'productform' , [
2020-08-31 20:40:31 +02:00
'product' => $product ,
2020-11-17 19:11:02 +01:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'barcodes' => $this -> getDatabase () -> product_barcodes () -> orderBy ( 'barcode' ),
2020-11-17 19:11:02 +01:00
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-03-13 17:09:07 +01:00
'quantityunitsStock' => $this -> getDatabase () -> quantity_units () -> where ( 'id IN (SELECT to_qu_id FROM 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' ),
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productgroups' => $this -> getDatabase () -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'products' ),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'id != :1 AND parent_product_id IS NULL and active = 1' , $product -> id ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-03-01 23:47:47 +07:00
'isSubProductOfOthers' => $this -> getDatabase () -> products () -> where ( 'parent_product_id = :1' , $product -> id ) -> count () !== 0 ,
2019-09-15 16:40:54 +02:00
'mode' => 'edit' ,
2020-11-17 19:11:02 +01:00
'quConversions' => $this -> getDatabase () -> quantity_unit_conversions (),
'productBarcodeUserfields' => $this -> getUserfieldsService () -> GetFields ( 'product_barcodes' ),
'productBarcodeUserfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'product_barcodes' )
2018-04-11 19:49:35 +02:00
]);
}
}
2021-06-12 17:21:12 +02:00
public function ProductGrocycodeImage ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
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
}
2020-08-31 20:40:31 +02:00
public function ProductGroupEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $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
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'productgroupform' , [
2019-04-22 22:16:35 +02:00
'mode' => 'create' ,
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'product_groups' )
2018-04-11 19:49:35 +02:00
]);
}
else
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'productgroupform' , [
'group' => $this -> getDatabase () -> product_groups ( $args [ 'productGroupId' ]),
2019-04-22 22:16:35 +02:00
'mode' => 'edit' ,
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'product_groups' )
2018-04-11 19:49:35 +02:00
]);
}
}
2020-08-31 20:40:31 +02:00
public function ProductGroupsList ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2020-03-25 19:34:56 +01:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'productgroups' , [
2020-11-17 19:11:02 +01:00
'productGroups' => $this -> getDatabase () -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'product_groups' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'product_groups' )
]);
}
public function ProductsList ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
2021-07-06 20:08:02 +02:00
$products = $this -> getDatabase () -> products ();
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
}
2021-07-06 20:08:02 +02:00
$products = $products -> orderBy ( 'name' , 'COLLATE NOCASE' );
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'products' , [
2020-12-07 19:48:33 +01:00
'products' => $products ,
2020-11-17 19:11:02 +01:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productGroups' => $this -> getDatabase () -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-12-19 10:51:07 +01:00
'shoppingLocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'products' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'products' )
]);
}
public function Purchase ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
return $this -> renderPage ( $response , 'purchase' , [
2022-04-01 18:49:17 +02:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1 AND no_own_stock = 0' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-12-20 16:00:14 +01:00
'barcodes' => $this -> getDatabase () -> product_barcodes_comma_separated (),
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-03-30 17:32:53 +02:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'stock' )
2020-08-31 20:40:31 +02:00
]);
}
public function QuantityUnitConversionEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
$product = null ;
if ( isset ( $request -> getQueryParams ()[ 'product' ]))
2020-03-25 19:34:56 +01:00
{
2020-08-31 20:40:31 +02:00
$product = $this -> getDatabase () -> 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
{
2020-08-31 20:40:31 +02:00
$defaultQuUnit = $this -> getDatabase () -> 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
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'quantityunitconversionform' , [
2019-04-22 22:16:35 +02:00
'mode' => 'create' ,
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'quantity_unit_conversions' ),
2020-11-17 19:11:02 +01:00
'quantityunits' => $this -> getDatabase () -> quantity_units () -> 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
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'quantityunitconversionform' , [
'quConversion' => $this -> getDatabase () -> quantity_unit_conversions ( $args [ 'quConversionId' ]),
2019-04-22 22:16:35 +02:00
'mode' => 'edit' ,
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'quantity_unit_conversions' ),
2020-11-17 19:11:02 +01:00
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'product' => $product ,
'defaultQuUnit' => $defaultQuUnit
2018-09-24 13:02:52 +02:00
]);
}
}
2020-02-11 17:42:03 +01:00
public function QuantityUnitEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
if ( $args [ 'quantityunitId' ] == 'new' )
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'quantityunitform' , [
2019-04-22 22:16:35 +02:00
'mode' => 'create' ,
2020-03-01 23:47:47 +07:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'quantity_units' ),
'pluralCount' => $this -> getLocalizationService () -> GetPluralCount (),
'pluralRule' => $this -> getLocalizationService () -> GetPluralDefinition ()
2018-04-11 19:49:35 +02:00
]);
}
else
{
2020-03-01 23:47:47 +07:00
$quantityUnit = $this -> getDatabase () -> quantity_units ( $args [ 'quantityunitId' ]);
2019-09-15 16:40:54 +02:00
2020-03-01 23:47:47 +07: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' ,
2020-03-01 23:47:47 +07:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'quantity_units' ),
'pluralCount' => $this -> getLocalizationService () -> GetPluralCount (),
'pluralRule' => $this -> getLocalizationService () -> GetPluralDefinition (),
'defaultQuConversions' => $this -> getDatabase () -> quantity_unit_conversions () -> where ( 'from_qu_id = :1 AND product_id IS NULL' , $quantityUnit -> id ),
'quantityUnits' => $this -> getDatabase () -> quantity_units ()
2018-04-11 19:49:35 +02:00
]);
}
2020-08-31 20:40:31 +02:00
}
public function QuantityUnitPluralFormTesting ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
return $this -> renderPage ( $response , 'quantityunitpluraltesting' , [
2020-11-17 19:11:02 +01:00
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' )
2020-08-31 20:40:31 +02:00
]);
}
public function QuantityUnitsList ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
return $this -> renderPage ( $response , 'quantityunits' , [
2020-11-17 19:11:02 +01:00
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'quantity_units' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'quantity_units' )
]);
}
public function ShoppingList ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
$listId = 1 ;
if ( isset ( $request -> getQueryParams ()[ 'list' ]))
{
$listId = $request -> getQueryParams ()[ 'list' ];
}
return $this -> renderPage ( $response , 'shoppinglist' , [
2020-12-19 17:55:49 +01:00
'listItems' => $this -> getDatabase () -> uihelper_shopping_list () -> where ( 'shopping_list_id = :1' , $listId ),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'missingProducts' => $this -> getStockService () -> GetMissingProducts (),
2020-11-17 19:11:02 +01:00
'shoppingLists' => $this -> getDatabase () -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'selectedShoppingListId' => $listId ,
2020-11-13 17:30:57 +01:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
2020-11-17 19:11:02 +01:00
'productUserfields' => $this -> getUserfieldsService () -> GetFields ( 'products' ),
'productUserfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'products' ),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_list' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'shopping_list' )
2020-08-31 20:40:31 +02:00
]);
}
public function ShoppingListEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
if ( $args [ 'listId' ] == 'new' )
{
return $this -> renderPage ( $response , 'shoppinglistform' , [
2020-11-17 19:11:02 +01:00
'mode' => 'create' ,
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_lists' )
2020-08-31 20:40:31 +02:00
]);
}
else
{
return $this -> renderPage ( $response , 'shoppinglistform' , [
'shoppingList' => $this -> getDatabase () -> shopping_lists ( $args [ 'listId' ]),
2020-11-17 19:11:02 +01:00
'mode' => 'edit' ,
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_lists' )
2020-08-31 20:40:31 +02:00
]);
}
2018-04-11 19:49:35 +02:00
}
2020-02-11 17:42:03 +01:00
public function ShoppingListItemEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
if ( $args [ 'itemId' ] == 'new' )
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'shoppinglistitemform' , [
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-07-12 19:00:40 +02:00
'barcodes' => $this -> getDatabase () -> product_barcodes_comma_separated (),
2020-11-17 19:11:02 +01:00
'shoppingLists' => $this -> getDatabase () -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-09 22:15:25 +01:00
'mode' => 'create' ,
2020-11-17 19:11:02 +01:00
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_list' )
2018-04-11 19:49:35 +02:00
]);
}
else
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'shoppinglistitemform' , [
2020-08-31 20:40:31 +02:00
'listItem' => $this -> getDatabase () -> shopping_list ( $args [ 'itemId' ]),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-07-12 19:00:40 +02:00
'barcodes' => $this -> getDatabase () -> product_barcodes_comma_separated (),
2020-11-17 19:11:02 +01:00
'shoppingLists' => $this -> getDatabase () -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-09 22:15:25 +01:00
'mode' => 'edit' ,
2020-11-17 19:11:02 +01:00
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnitConversionsResolved' => $this -> getDatabase () -> quantity_unit_conversions_resolved (),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_list' )
2018-04-11 19:49:35 +02:00
]);
}
}
2018-10-27 17:26:00 +02:00
2020-08-31 20:40:31 +02:00
public function ShoppingListSettings ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2019-04-20 17:04:40 +02:00
{
2022-04-03 13:00:14 +02:00
return $this -> renderPage ( $response , 'shoppinglistsettings' , [
'shoppingLists' => $this -> getDatabase () -> shopping_lists () -> orderBy ( 'name' , 'COLLATE NOCASE' )
]);
2020-08-31 20:40:31 +02:00
}
public function ShoppingLocationEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
if ( $args [ 'shoppingLocationId' ] == 'new' )
2019-04-20 17:04:40 +02:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'shoppinglocationform' , [
'mode' => 'create' ,
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_locations' )
2019-04-20 17:04:40 +02:00
]);
}
else
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'shoppinglocationform' , [
'shoppinglocation' => $this -> getDatabase () -> shopping_locations ( $args [ 'shoppingLocationId' ]),
'mode' => 'edit' ,
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_locations' )
2019-04-20 17:04:40 +02:00
]);
}
}
2020-08-31 20:40:31 +02:00
public function ShoppingLocationsList ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2020-02-01 12:54:05 +01:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'shoppinglocations' , [
2020-11-17 19:11:02 +01:00
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'shopping_locations' ),
'userfieldValues' => $this -> getUserfieldsService () -> GetAllValues ( 'shopping_locations' )
]);
2020-02-01 12:54:05 +01:00
}
2020-08-31 20:40:31 +02:00
public function StockEntryEditForm ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-10-27 17:26:00 +02:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'stockentryform' , [
'stockEntry' => $this -> getDatabase () -> stock () -> where ( 'id' , $args [ 'entryId' ]) -> fetch (),
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2022-04-06 19:08:17 +02:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'userfields' => $this -> getUserfieldsService () -> GetFields ( 'stock' )
2018-10-27 17:26:00 +02:00
]);
}
2019-08-10 16:34:29 +02:00
2021-06-12 17:21:12 +02:00
public function StockEntryGrocycodeImage ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
$stockEntry = $this -> getDatabase () -> stock () -> where ( 'id' , $args [ 'entryId' ]) -> fetch ();
$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
}
public function StockEntryGrocycodeLabel ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
$stockEntry = $this -> getDatabase () -> stock () -> where ( 'id' , $args [ 'entryId' ]) -> fetch ();
return $this -> renderPage ( $response , 'stockentrylabel' , [
'stockEntry' => $stockEntry ,
'product' => $this -> getDatabase () -> products ( $stockEntry -> product_id ),
]);
}
2020-08-31 20:40:31 +02:00
public function StockSettings ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2019-08-10 16:34:29 +02:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'stocksettings' , [
2020-11-17 19:11:02 +01:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'productGroups' => $this -> getDatabase () -> product_groups () -> orderBy ( 'name' , 'COLLATE NOCASE' )
2019-08-10 16:34:29 +02:00
]);
}
2019-09-15 16:40:54 +02:00
2020-08-31 20:40:31 +02:00
public function Stockentries ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2020-08-17 14:47:33 -05:00
{
2020-08-31 20:40:31 +02:00
$usersService = $this -> getUsersService ();
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
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'stockentries' , [
2020-11-17 19:11:02 +01:00
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityunits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'shoppinglocations' => $this -> getDatabase () -> shopping_locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-08-31 20:40:31 +02:00
'stockEntries' => $this -> getDatabase () -> stock () -> orderBy ( 'product_id' ),
'currentStockLocations' => $this -> getStockService () -> GetCurrentStockLocations (),
'nextXDays' => $nextXDays ,
2022-03-30 17:32:53 +02:00
'userfieldsProducts' => $this -> getUserfieldsService () -> GetFields ( 'products' ),
'userfieldValuesProducts' => $this -> getUserfieldsService () -> GetAllValues ( 'products' ),
'userfieldsStock' => $this -> getUserfieldsService () -> GetFields ( 'stock' ),
'userfieldValuesStock' => $this -> getUserfieldsService () -> GetAllValues ( 'stock' )
2020-08-31 20:40:31 +02:00
]);
2020-08-17 14:47:33 -05:00
}
2020-08-31 20:40:31 +02:00
public function Transfer ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2019-09-15 16:40:54 +02:00
{
2020-08-31 20:40:31 +02:00
return $this -> renderPage ( $response , 'transfer' , [
2022-04-01 18:49:17 +02:00
'products' => $this -> getDatabase () -> 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' ),
2020-12-20 16:00:14 +01:00
'barcodes' => $this -> getDatabase () -> product_barcodes_comma_separated (),
2020-11-17 19:11:02 +01:00
'locations' => $this -> getDatabase () -> locations () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'quantityUnits' => $this -> getDatabase () -> quantity_units () -> orderBy ( 'name' , 'COLLATE NOCASE' ),
2020-11-09 19:25:46 +01:00
'quantityUnitConversionsResolved' => $this -> getDatabase () -> 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
2020-09-06 13:18:51 +02:00
public function JournalSummary ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
{
$entries = $this -> getDatabase () -> uihelper_stock_journal_summary ();
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
$usersService = $this -> getUsersService ();
2020-09-06 13:18:51 +02:00
return $this -> renderPage ( $response , 'stockjournalsummary' , [
2020-11-17 19:11:02 +01:00
'entries' => $entries ,
'products' => $this -> getDatabase () -> products () -> where ( 'active = 1' ) -> orderBy ( 'name' , 'COLLATE NOCASE' ),
'users' => $usersService -> GetUsersAsDto (),
'transactionTypes' => GetClassConstants ( '\Grocy\Services\StockService' , 'TRANSACTION_TYPE_' )
2020-09-06 13:18:51 +02:00
]);
}
2018-04-11 19:49:35 +02:00
}