| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Modifier.php | 
					
						
							|  |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See the LICENSE file for details. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Search; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use Log; | 
					
						
							|  |  |  | use Steam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Modifier | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * @param string      $amount | 
					
						
							|  |  |  |      * @param int         $expected | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function amountCompare(Transaction $transaction, string $amount, int $expected): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $amount            = Steam::positive($amount); | 
					
						
							|  |  |  |         $transactionAmount = Steam::positive($transaction->transaction_amount); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $compare = bccomp($amount, $transactionAmount); | 
					
						
							|  |  |  |         Log::debug(sprintf('%s vs %s is %d', $amount, $transactionAmount, $compare)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $compare === $expected; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |     public static function apply(array $modifier, Transaction $transaction): bool | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |         switch ($modifier['type']) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 throw new FireflyException(sprintf('Search modifier "%s" is not (yet) supported. Sorry!', $modifier['type'])); | 
					
						
							| 
									
										
										
										
											2017-02-19 12:12:24 +01:00
										 |  |  |             case 'amount': | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |             case 'amount_is': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::amountCompare($transaction, $modifier['value'], 0); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |                 Log::debug(sprintf('Amount is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-02-19 12:12:24 +01:00
										 |  |  |             case 'amount_min': | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |             case 'amount_less': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::amountCompare($transaction, $modifier['value'], 1); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |                 Log::debug(sprintf('Amount less than %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-02-19 12:12:24 +01:00
										 |  |  |             case 'amount_max': | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |             case 'amount_more': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::amountCompare($transaction, $modifier['value'], -1); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |                 Log::debug(sprintf('Amount more than %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'source': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::stringCompare($transaction->account_name, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |                 Log::debug(sprintf('Source is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'destination': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::stringCompare($transaction->opposing_account_name, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |                 Log::debug(sprintf('Destination is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'category': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::category($transaction, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |                 Log::debug(sprintf('Category is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-02-19 07:41:12 +01:00
										 |  |  |             case 'budget': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::budget($transaction, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-02-19 07:41:12 +01:00
										 |  |  |                 Log::debug(sprintf('Budget is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |             case 'bill': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::stringCompare(strval($transaction->bill_name), $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |                 Log::debug(sprintf('Bill is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'type': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::stringCompare($transaction->transaction_type_type, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |                 Log::debug(sprintf('Transaction type is %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'date': | 
					
						
							| 
									
										
										
										
											2017-02-19 12:17:07 +01:00
										 |  |  |             case 'on': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::sameDate($transaction->date, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-07-15 17:19:12 +02:00
										 |  |  |                 Log::debug(sprintf('Date same as %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'date_before': | 
					
						
							| 
									
										
										
										
											2017-02-19 12:17:07 +01:00
										 |  |  |             case 'before': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::dateBefore($transaction->date, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-07-15 17:19:12 +02:00
										 |  |  |                 Log::debug(sprintf('Date before %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 'date_after': | 
					
						
							| 
									
										
										
										
											2017-02-19 12:17:07 +01:00
										 |  |  |             case 'after': | 
					
						
							| 
									
										
										
										
											2017-02-25 17:39:50 +01:00
										 |  |  |                 $res = self::dateAfter($transaction->date, $modifier['value']); | 
					
						
							| 
									
										
										
										
											2017-07-15 17:19:12 +02:00
										 |  |  |                 Log::debug(sprintf('Date before %s? %s', $modifier['value'], var_export($res, true))); | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |         return $res; | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 09:07:14 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * @param string $compare | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function dateAfter(Carbon $date, string $compare): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $compareDate = new Carbon($compare); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $date->greaterThanOrEqualTo($compareDate); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * @param string $compare | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function dateBefore(Carbon $date, string $compare): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $compareDate = new Carbon($compare); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $date->lessThanOrEqualTo($compareDate); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * @param string $compare | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function sameDate(Carbon $date, string $compare): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $compareDate = new Carbon($compare); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $compareDate->isSameDay($date); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |      * @param string $haystack | 
					
						
							|  |  |  |      * @param string $needle | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |     public static function stringCompare(string $haystack, string $needle): bool | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |         $res = !(strpos(strtolower($haystack), strtolower($needle)) === false); | 
					
						
							|  |  |  |         Log::debug(sprintf('"%s" is in "%s"? %s', $needle, $haystack, var_export($res, true))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $res; | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 07:41:12 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * @param string      $search | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private static function budget(Transaction $transaction, string $search): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $journalBudget = ''; | 
					
						
							|  |  |  |         if (!is_null($transaction->transaction_journal_budget_name)) { | 
					
						
							|  |  |  |             $journalBudget = Steam::decrypt(intval($transaction->transaction_journal_budget_encrypted), $transaction->transaction_journal_budget_name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $transactionBudget = ''; | 
					
						
							|  |  |  |         if (!is_null($transaction->transaction_budget_name)) { | 
					
						
							|  |  |  |             $journalBudget = Steam::decrypt(intval($transaction->transaction_budget_encrypted), $transaction->transaction_budget_name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return self::stringCompare($journalBudget, $search) || self::stringCompare($transactionBudget, $search); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * @param string      $search | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |     private static function category(Transaction $transaction, string $search): bool | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |         $journalCategory = ''; | 
					
						
							|  |  |  |         if (!is_null($transaction->transaction_journal_category_name)) { | 
					
						
							|  |  |  |             $journalCategory = Steam::decrypt(intval($transaction->transaction_journal_category_encrypted), $transaction->transaction_journal_category_name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $transactionCategory = ''; | 
					
						
							|  |  |  |         if (!is_null($transaction->transaction_category_name)) { | 
					
						
							|  |  |  |             $journalCategory = Steam::decrypt(intval($transaction->transaction_category_encrypted), $transaction->transaction_category_name); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 07:38:51 +01:00
										 |  |  |         return self::stringCompare($journalCategory, $search) || self::stringCompare($transactionCategory, $search); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-07-07 08:09:42 +02:00
										 |  |  | } |