2017-04-16 23:11:03 +02:00
< ? php
2018-04-10 20:30:11 +02:00
class StockService
2017-04-16 23:11:03 +02:00
{
2017-04-19 21:09:28 +02:00
const TRANSACTION_TYPE_PURCHASE = 'purchase' ;
const TRANSACTION_TYPE_CONSUME = 'consume' ;
const TRANSACTION_TYPE_INVENTORY_CORRECTION = 'inventory-correction' ;
2017-04-16 23:11:03 +02:00
public static function GetCurrentStock ()
{
2017-04-21 13:21:09 +02:00
$sql = 'SELECT * from stock_current' ;
2018-04-10 20:30:11 +02:00
return DatabaseService :: ExecuteDbQuery ( DatabaseService :: GetDbConnectionRaw (), $sql ) -> fetchAll ( PDO :: FETCH_OBJ );
2017-04-21 13:21:09 +02:00
}
public static function GetMissingProducts ()
{
$sql = 'SELECT * from stock_missing_products' ;
2018-04-10 20:30:11 +02:00
return DatabaseService :: ExecuteDbQuery ( DatabaseService :: GetDbConnectionRaw (), $sql ) -> fetchAll ( PDO :: FETCH_OBJ );
2017-04-16 23:11:03 +02:00
}
public static function GetProductDetails ( int $productId )
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-04-16 23:11:03 +02:00
$product = $db -> products ( $productId );
$productStockAmount = $db -> stock () -> where ( 'product_id' , $productId ) -> sum ( 'amount' );
2017-04-22 21:51:07 +02:00
$productLastPurchased = $db -> stock_log () -> where ( 'product_id' , $productId ) -> where ( 'transaction_type' , self :: TRANSACTION_TYPE_PURCHASE ) -> max ( 'purchased_date' );
2017-04-19 21:09:28 +02:00
$productLastUsed = $db -> stock_log () -> where ( 'product_id' , $productId ) -> where ( 'transaction_type' , self :: TRANSACTION_TYPE_CONSUME ) -> max ( 'used_date' );
2017-04-16 23:11:03 +02:00
$quPurchase = $db -> quantity_units ( $product -> qu_id_purchase );
$quStock = $db -> quantity_units ( $product -> qu_id_stock );
return array (
'product' => $product ,
'last_purchased' => $productLastPurchased ,
'last_used' => $productLastUsed ,
'stock_amount' => $productStockAmount ,
'quantity_unit_purchase' => $quPurchase ,
'quantity_unit_stock' => $quStock
);
}
2017-04-19 21:09:28 +02:00
public static function AddProduct ( int $productId , int $amount , string $bestBeforeDate , $transactionType )
{
2017-04-20 17:10:21 +02:00
if ( $transactionType === self :: TRANSACTION_TYPE_CONSUME || $transactionType === self :: TRANSACTION_TYPE_PURCHASE || $transactionType === self :: TRANSACTION_TYPE_INVENTORY_CORRECTION )
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-04-20 17:10:21 +02:00
$stockId = uniqid ();
$logRow = $db -> stock_log () -> createRow ( array (
'product_id' => $productId ,
'amount' => $amount ,
'best_before_date' => $bestBeforeDate ,
'purchased_date' => date ( 'Y-m-d' ),
'stock_id' => $stockId ,
'transaction_type' => $transactionType
));
$logRow -> save ();
$stockRow = $db -> stock () -> createRow ( array (
'product_id' => $productId ,
'amount' => $amount ,
'best_before_date' => $bestBeforeDate ,
'purchased_date' => date ( 'Y-m-d' ),
'stock_id' => $stockId ,
));
$stockRow -> save ();
return true ;
}
else
{
2018-04-10 20:30:11 +02:00
throw new Exception ( " Transaction type $transactionType is not valid (StockService.AddProduct) " );
2017-04-20 17:10:21 +02:00
}
2017-04-19 21:09:28 +02:00
}
public static function ConsumeProduct ( int $productId , int $amount , bool $spoiled , $transactionType )
2017-04-16 23:11:03 +02:00
{
2017-04-20 17:10:21 +02:00
if ( $transactionType === self :: TRANSACTION_TYPE_CONSUME || $transactionType === self :: TRANSACTION_TYPE_PURCHASE || $transactionType === self :: TRANSACTION_TYPE_INVENTORY_CORRECTION )
2017-04-16 23:11:03 +02:00
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-04-16 23:11:03 +02:00
2017-04-20 17:10:21 +02:00
$productStockAmount = $db -> stock () -> where ( 'product_id' , $productId ) -> sum ( 'amount' );
2017-04-22 21:15:30 +02:00
$potentialStockEntries = $db -> stock () -> where ( 'product_id' , $productId ) -> orderBy ( 'best_before_date' , 'ASC' ) -> orderBy ( 'purchased_date' , 'ASC' ) -> fetchAll (); //First expiring first, then first in first out
2017-04-16 23:11:03 +02:00
2017-04-20 17:10:21 +02:00
if ( $amount > $productStockAmount )
2017-04-16 23:11:03 +02:00
{
2017-04-20 17:10:21 +02:00
return false ;
2017-04-16 23:11:03 +02:00
}
2017-04-20 17:10:21 +02:00
foreach ( $potentialStockEntries as $stockEntry )
2017-04-16 23:11:03 +02:00
{
2017-04-20 17:10:21 +02:00
if ( $amount == 0 )
{
break ;
}
if ( $amount >= $stockEntry -> amount ) //Take the whole stock entry
{
$logRow = $db -> stock_log () -> createRow ( array (
'product_id' => $stockEntry -> product_id ,
'amount' => $stockEntry -> amount * - 1 ,
'best_before_date' => $stockEntry -> best_before_date ,
'purchased_date' => $stockEntry -> purchased_date ,
'used_date' => date ( 'Y-m-d' ),
'spoiled' => $spoiled ,
'stock_id' => $stockEntry -> stock_id ,
'transaction_type' => $transactionType
));
$logRow -> save ();
$amount -= $stockEntry -> amount ;
$stockEntry -> delete ();
}
else //Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
{
$logRow = $db -> stock_log () -> createRow ( array (
'product_id' => $stockEntry -> product_id ,
'amount' => $amount * - 1 ,
'best_before_date' => $stockEntry -> best_before_date ,
'purchased_date' => $stockEntry -> purchased_date ,
'used_date' => date ( 'Y-m-d' ),
'spoiled' => $spoiled ,
'stock_id' => $stockEntry -> stock_id ,
'transaction_type' => $transactionType
));
$logRow -> save ();
$restStockAmount = $stockEntry -> amount - $amount ;
$amount = 0 ;
$stockEntry -> update ( array (
'amount' => $restStockAmount
));
}
2017-04-16 23:11:03 +02:00
}
2017-04-20 17:10:21 +02:00
return true ;
}
else
{
2018-04-10 20:30:11 +02:00
throw new Exception ( " Transaction type $transactionType is not valid (StockService.ConsumeProduct) " );
2017-04-20 17:10:21 +02:00
}
}
public static function InventoryProduct ( int $productId , int $newAmount , string $bestBeforeDate )
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-04-20 17:10:21 +02:00
$productStockAmount = $db -> stock () -> where ( 'product_id' , $productId ) -> sum ( 'amount' );
if ( $newAmount > $productStockAmount )
{
$amountToAdd = $newAmount - $productStockAmount ;
self :: AddProduct ( $productId , $amountToAdd , $bestBeforeDate , self :: TRANSACTION_TYPE_INVENTORY_CORRECTION );
}
else if ( $newAmount < $productStockAmount )
{
$amountToRemove = $productStockAmount - $newAmount ;
self :: ConsumeProduct ( $productId , $amountToRemove , false , self :: TRANSACTION_TYPE_INVENTORY_CORRECTION );
2017-04-16 23:11:03 +02:00
}
return true ;
}
2017-04-21 15:36:04 +02:00
public static function AddMissingProductsToShoppingList ()
{
2018-04-10 20:30:11 +02:00
$db = DatabaseService :: GetDbConnection ();
2017-04-21 15:36:04 +02:00
$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 ();
}
}
}
2017-04-16 23:11:03 +02:00
}