| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * FromAccountIs.php | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-13 07:49:58 +02:00
										 |  |  | namespace FireflyIII\TransactionRules\Triggers; | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 08:09:53 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2016-09-21 20:30:09 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2016-09-21 20:58:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class FromAccountIs | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-09-13 07:49:58 +02:00
										 |  |  |  * @package FireflyIII\TransactionRules\Triggers | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-02-17 21:55:00 +01:00
										 |  |  | final class FromAccountIs extends AbstractTrigger implements TriggerInterface | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-02-17 19:07:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 18:00:13 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * A trigger is said to "match anything", or match any given transaction, | 
					
						
							|  |  |  |      * when the trigger value is very vague or has no restrictions. Easy examples | 
					
						
							|  |  |  |      * are the "AmountMore"-trigger combined with an amount of 0: any given transaction | 
					
						
							|  |  |  |      * has an amount of more than zero! Other examples are all the "Description"-triggers | 
					
						
							|  |  |  |      * which have hard time handling empty trigger values such as "" or "*" (wild cards). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * If the user tries to create such a trigger, this method MUST return true so Firefly III | 
					
						
							|  |  |  |      * can stop the storing / updating the trigger. If the trigger is in any way restrictive | 
					
						
							|  |  |  |      * (even if it will still include 99.9% of the users transactions), this method MUST return | 
					
						
							|  |  |  |      * false. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param null $value | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function willMatchEverything($value = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!is_null($value)) { | 
					
						
							| 
									
										
										
										
											2017-10-06 15:41:21 +02:00
										 |  |  |             $res = strval($value) === ''; | 
					
						
							|  |  |  |             if ($res === true) { | 
					
						
							|  |  |  |                 Log::error(sprintf('Cannot use %s with "" as a value.', self::class)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $res; | 
					
						
							| 
									
										
										
										
											2016-02-17 18:00:13 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-21 20:30:09 +02:00
										 |  |  |         Log::error(sprintf('Cannot use %s with a null value.', self::class)); | 
					
						
							| 
									
										
										
										
											2016-02-17 18:00:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-02-17 19:07:20 +01:00
										 |  |  |      * @param TransactionJournal $journal | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function triggered(TransactionJournal $journal): bool | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-20 08:09:53 +02:00
										 |  |  |         $name = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Account $account */ | 
					
						
							| 
									
										
										
										
											2017-03-04 07:26:03 +01:00
										 |  |  |         foreach ($journal->sourceAccountList() as $account) { | 
					
						
							| 
									
										
										
										
											2016-05-20 08:09:53 +02:00
										 |  |  |             $name .= strtolower($account->name); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-20 11:02:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 07:02:39 +01:00
										 |  |  |         $search = strtolower($this->triggerValue); | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 20:58:12 +02:00
										 |  |  |         if ($name === $search) { | 
					
						
							|  |  |  |             Log::debug(sprintf('RuleTrigger FromAccountIs for journal #%d: "%s" is "%s", return true.', $journal->id, $name, $search)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-03-19 16:51:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 20:58:12 +02:00
										 |  |  |         Log::debug(sprintf('RuleTrigger FromAccountIs for journal #%d: "%s" is NOT "%s", return false.', $journal->id, $name, $search)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-13 07:47:26 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-28 21:50:20 +01:00
										 |  |  | } |