2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< ? php  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								namespace  Grocy\Services ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class  StockService  extends  BaseService  
						 
					
						
							
								
									
										
										
										
											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' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  GetCurrentStock () 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 13:21:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$sql  =  'SELECT * from stock_current' ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  $this -> DatabaseService -> ExecuteDbQuery ( $sql ) -> fetchAll ( \PDO :: FETCH_OBJ ); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 13:21:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  GetMissingProducts () 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 13:21:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$sql  =  'SELECT * from stock_missing_products' ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  $this -> DatabaseService -> ExecuteDbQuery ( $sql ) -> fetchAll ( \PDO :: FETCH_OBJ ); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-08-04 14:25:32 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  GetExpiringProducts ( int  $days  =  5 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$currentStock  =  $this -> GetCurrentStock (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  FindAllObjectsInArrayByPropertyValue ( $currentStock ,  'best_before_date' ,  date ( 'Y-m-d' ,  strtotime ( " + $days  days " )),  '<' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  GetProductDetails ( int  $productId ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( ! $this -> ProductExists ( $productId )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( 'Product does not exist' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$product  =  $this -> Database -> products ( $productId ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$productStockAmount  =  $this -> Database -> stock () -> where ( 'product_id' ,  $productId ) -> sum ( 'amount' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$productLastPurchased  =  $this -> Database -> stock_log () -> where ( 'product_id' ,  $productId ) -> where ( 'transaction_type' ,  self :: TRANSACTION_TYPE_PURCHASE ) -> max ( 'purchased_date' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$productLastUsed  =  $this -> Database -> stock_log () -> where ( 'product_id' ,  $productId ) -> where ( 'transaction_type' ,  self :: TRANSACTION_TYPE_CONSUME ) -> max ( 'used_date' ); 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-07 20:11:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$nextBestBeforeDate  =  $this -> Database -> stock () -> where ( 'product_id' ,  $productId ) -> min ( 'best_before_date' ); 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$quPurchase  =  $this -> Database -> quantity_units ( $product -> qu_id_purchase ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$quStock  =  $this -> Database -> quantity_units ( $product -> qu_id_stock ); 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-04 07:45:24 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$lastPrice  =  null ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$lastLogRow  =  $this -> Database -> stock_log () -> where ( 'product_id = :1 AND transaction_type = :2' ,  $productId ,  self :: TRANSACTION_TYPE_PURCHASE ) -> orderBy ( 'row_created_timestamp' ,  'DESC' ) -> limit ( 1 ) -> fetch (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  ( $lastLogRow  !==  null  &&  ! empty ( $lastLogRow )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$lastPrice  =  $lastLogRow -> price ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  array ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											'product'  =>  $product , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											'last_purchased'  =>  $productLastPurchased , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											'last_used'  =>  $productLastUsed , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											'stock_amount'  =>  $productStockAmount , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											'quantity_unit_purchase'  =>  $quPurchase , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											'quantity_unit_stock'  =>  $quStock , 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-07 20:11:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											'last_price'  =>  $lastPrice , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											'next_best_before_date'  =>  $nextBestBeforeDate 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  GetProductPriceHistory ( int  $productId ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  ( ! $this -> ProductExists ( $productId )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( 'Product does not exist' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$returnData  =  array (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$rows  =  $this -> Database -> stock_log () -> where ( 'product_id = :1 AND transaction_type = :2' ,  $productId ,  self :: TRANSACTION_TYPE_PURCHASE ) -> whereNOT ( 'price' ,  null ) -> orderBy ( 'purchased_date' ,  'DESC' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										foreach  ( $rows  as  $row ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$returnData []  =  array ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'date'  =>  $row -> purchased_date , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'price'  =>  $row -> price 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  $returnData ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public  function  AddProduct ( int  $productId ,  int  $amount ,  string  $bestBeforeDate ,  $transactionType ,  $purchasedDate ,  $price ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-19 21:09:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( ! $this -> ProductExists ( $productId )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( 'Product does not exist' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											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 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$stockId  =  uniqid (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$logRow  =  $this -> Database -> stock_log () -> createRow ( array ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'product_id'  =>  $productId , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'amount'  =>  $amount , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'best_before_date'  =>  $bestBeforeDate , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'purchased_date'  =>  $purchasedDate , 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'stock_id'  =>  $stockId , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'transaction_type'  =>  $transactionType , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'price'  =>  $price 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											)); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$logRow -> save (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$stockRow  =  $this -> Database -> stock () -> createRow ( array ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'product_id'  =>  $productId , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'amount'  =>  $amount , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												'best_before_date'  =>  $bestBeforeDate , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'purchased_date'  =>  $purchasedDate , 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'stock_id'  =>  $stockId , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												'price'  =>  $price 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											)); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$stockRow -> save (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  true ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										else 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  ConsumeProduct ( int  $productId ,  int  $amount ,  bool  $spoiled ,  $transactionType ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( ! $this -> ProductExists ( $productId )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( 'Product does not exist' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											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-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$productStockAmount  =  $this -> Database -> stock () -> where ( 'product_id' ,  $productId ) -> sum ( 'amount' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$potentialStockEntries  =  $this -> Database -> 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
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													$logRow  =  $this -> Database -> stock_log () -> createRow ( array ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
														'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 , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
														'transaction_type'  =>  $transactionType , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														'price'  =>  $stockEntry -> price 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													)); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													$logRow -> save (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													$amount  -=  $stockEntry -> amount ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													$stockEntry -> delete (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												else  //Stock entry amount is > than needed amount -> split the stock entry resp. update the amount
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													$logRow  =  $this -> Database -> stock_log () -> createRow ( array ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
														'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 , 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
														'transaction_type'  =>  $transactionType , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														'price'  =>  $stockEntry -> price 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													)); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													$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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  InventoryProduct ( int  $productId ,  int  $newAmount ,  string  $bestBeforeDate ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( ! $this -> ProductExists ( $productId )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( 'Product does not exist' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$productStockAmount  =  $this -> Database -> stock () -> where ( 'product_id' ,  $productId ) -> sum ( 'amount' ); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  ( $newAmount  >  $productStockAmount ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$productDetails  =  $this -> GetProductDetails ( $productId ); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$amountToAdd  =  $newAmount  -  $productStockAmount ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-26 20:27:38 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$this -> AddProduct ( $productId ,  $amountToAdd ,  $bestBeforeDate ,  self :: TRANSACTION_TYPE_INVENTORY_CORRECTION ,  date ( 'Y-m-d' ),  $productDetails [ 'last_price' ]); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-20 17:10:21 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										else  if  ( $newAmount  <  $productStockAmount ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											$amountToRemove  =  $productStockAmount  -  $newAmount ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$this -> 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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  AddMissingProductsToShoppingList () 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 15:36:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$missingProducts  =  $this -> GetMissingProducts (); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 15:36:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										foreach  ( $missingProducts  as  $missingProduct ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$product  =  $this -> Database -> products () -> where ( 'id' ,  $missingProduct -> id ) -> fetch (); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 15:36:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$amount  =  ceil ( $missingProduct -> amount_missing  /  $product -> qu_factor_purchase_to_stock ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											$alreadyExistingEntry  =  $this -> Database -> shopping_list () -> where ( 'product_id' ,  $missingProduct -> id ) -> fetch (); 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 15:36:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  ( $alreadyExistingEntry )  //Update
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												$alreadyExistingEntry -> update ( array ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													'amount_autoadded'  =>  $amount 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												)); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											else  //Insert
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-11 19:49:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												$shoppinglistRow  =  $this -> Database -> shopping_list () -> createRow ( array ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 15:36:04 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													'product_id'  =>  $missingProduct -> id , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													'amount_autoadded'  =>  $amount 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												)); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												$shoppinglistRow -> save (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-15 08:29:26 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									public  function  ClearShoppingList () 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$this -> Database -> shopping_list () -> delete (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 14:25:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									private  function  ProductExists ( $productId ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$productRow  =  $this -> Database -> products () -> where ( 'id = :1' ,  $productId ) -> fetch (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  $productRow  !==  null ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 19:47:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									private  function  LoadBarcodeLookupPlugin () 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-24 19:41:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$pluginName  =  defined ( 'GROCY_STOCK_BARCODE_LOOKUP_PLUGIN' )  ?  GROCY_STOCK_BARCODE_LOOKUP_PLUGIN  :  '' ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 19:47:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( empty ( $pluginName )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( 'No barcode lookup plugin defined' ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-24 19:41:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										$path  =  GROCY_DATAPATH  .  " /plugins/ $pluginName .php " ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-22 19:47:46 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ( file_exists ( $path )) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											require_once  $path ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  new  $pluginName ( $this -> Database -> locations () -> fetchAll (),  $this -> Database -> quantity_units () -> fetchAll ()); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										else 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											throw  new  \Exception ( " Plugin  $pluginName  was not found " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public  function  ExternalBarcodeLookup ( $barcode ,  $addFoundProduct ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$plugin  =  $this -> LoadBarcodeLookupPlugin (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										$pluginOutput  =  $plugin -> Lookup ( $barcode ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  ( $pluginOutput  !==  null )  // Lookup was successful
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  ( $addFoundProduct  ===  true ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// Add product to database and include new product id in output
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												$newRow  =  $this -> Database -> products () -> createRow ( $pluginOutput ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												$newRow -> save (); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												$pluginOutput [ 'id' ]  =  $newRow -> id ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  $pluginOutput ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-16 23:11:03 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}