| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * TransactionTransformer.php | 
					
						
							|  |  |  |  * Copyright (c) 2018 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Transformers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2018-02-11 07:46:34 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							|  |  |  | use League\Fractal\Resource\Collection as FractalCollection; | 
					
						
							|  |  |  | use League\Fractal\Resource\Item; | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  | use League\Fractal\TransformerAbstract; | 
					
						
							| 
									
										
										
										
											2018-05-29 07:25:04 +02:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  | use Symfony\Component\HttpFoundation\ParameterBag; | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class TransactionTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class TransactionTransformer extends TransformerAbstract | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * List of resources possible to include | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $availableIncludes = ['attachments', 'user', 'tags', 'journal_meta', 'piggy_bank_events']; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * List of resources to automatically include | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $defaultIncludes = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |     /** @var ParameterBag */ | 
					
						
							|  |  |  |     protected $parameters; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * TransactionTransformer constructor. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-02-11 20:45:33 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param ParameterBag $parameters | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(ParameterBag $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->parameters = $parameters; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Include attachments. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includeAttachments(Transaction $transaction): FractalCollection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->collection($transaction->transactionJournal->attachments, new AttachmentTransformer($this->parameters), 'attachments'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Include meta data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includeJournalMeta(Transaction $transaction): FractalCollection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $meta = $transaction->transactionJournal->transactionJournalMeta()->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->collection($meta, new JournalMetaTransformer($this->parameters), 'journal_meta'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Include piggy bank events | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includePiggyBankEvents(Transaction $transaction): FractalCollection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $events = $transaction->transactionJournal->piggyBankEvents()->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->collection($events, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Include tags | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includeTags(Transaction $transaction): FractalCollection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $set = $transaction->transactionJournal->tags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->collection($set, new TagTransformer($this->parameters), 'tags'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Include the user. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return \League\Fractal\Resource\Item | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function includeUser(Transaction $transaction): Item | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->item($transaction->transactionJournal->user, new UserTransformer($this->parameters), 'users'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |      * Transform the journal. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-02-11 07:46:34 +01:00
										 |  |  |      * @param Transaction $transaction | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-02-11 07:46:34 +01:00
										 |  |  |     public function transform(Transaction $transaction): array | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |         $categoryId   = null; | 
					
						
							|  |  |  |         $categoryName = null; | 
					
						
							|  |  |  |         $budgetId     = null; | 
					
						
							|  |  |  |         $budgetName   = null; | 
					
						
							| 
									
										
										
										
											2018-04-24 19:26:16 +02:00
										 |  |  |         $categoryId   = $transaction->transaction_category_id ?? $transaction->transaction_journal_category_id; | 
					
						
							|  |  |  |         $categoryName = $transaction->transaction_category_name ?? $transaction->transaction_journal_category_name; | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($transaction->transaction_type_type === TransactionType::WITHDRAWAL) { | 
					
						
							| 
									
										
										
										
											2018-04-24 19:26:16 +02:00
										 |  |  |             $budgetId   = $transaction->transaction_budget_id ?? $transaction->transaction_journal_budget_id; | 
					
						
							|  |  |  |             $budgetName = $transaction->transaction_budget_name ?? $transaction->transaction_journal_budget_name; | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |         /** @var Note $dbNote */ | 
					
						
							|  |  |  |         $dbNote = $transaction->transactionJournal->notes()->first(); | 
					
						
							| 
									
										
										
										
											2018-03-10 22:38:20 +01:00
										 |  |  |         $notes  = null; | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         if (null !== $dbNote) { | 
					
						
							| 
									
										
										
										
											2018-02-18 16:35:26 +01:00
										 |  |  |             $notes = $dbNote->text; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 07:46:34 +01:00
										 |  |  |         $data = [ | 
					
						
							| 
									
										
										
										
											2018-03-11 13:31:22 +01:00
										 |  |  |             'id'                      => (int)$transaction->id, | 
					
						
							|  |  |  |             'updated_at'              => $transaction->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'created_at'              => $transaction->created_at->toAtomString(), | 
					
						
							|  |  |  |             'description'             => $transaction->description, | 
					
						
							| 
									
										
										
										
											2018-03-11 15:54:33 +01:00
										 |  |  |             'transaction_description' => $transaction->transaction_description, | 
					
						
							| 
									
										
										
										
											2018-03-11 13:31:22 +01:00
										 |  |  |             'date'                    => $transaction->date->format('Y-m-d'), | 
					
						
							|  |  |  |             'type'                    => $transaction->transaction_type_type, | 
					
						
							|  |  |  |             'identifier'              => $transaction->identifier, | 
					
						
							|  |  |  |             'journal_id'              => (int)$transaction->journal_id, | 
					
						
							|  |  |  |             'reconciled'              => (bool)$transaction->reconciled, | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |             'amount'                  => round($transaction->transaction_amount, (int)$transaction->transaction_currency_dp), | 
					
						
							| 
									
										
										
										
											2018-03-11 13:31:22 +01:00
										 |  |  |             'currency_id'             => $transaction->transaction_currency_id, | 
					
						
							|  |  |  |             'currency_code'           => $transaction->transaction_currency_code, | 
					
						
							|  |  |  |             'currency_symbol'         => $transaction->transaction_currency_symbol, | 
					
						
							|  |  |  |             'currency_dp'             => $transaction->transaction_currency_dp, | 
					
						
							|  |  |  |             'foreign_amount'          => null, | 
					
						
							|  |  |  |             'foreign_currency_id'     => $transaction->foreign_currency_id, | 
					
						
							|  |  |  |             'foreign_currency_code'   => $transaction->foreign_currency_code, | 
					
						
							|  |  |  |             'foreign_currency_symbol' => $transaction->foreign_currency_symbol, | 
					
						
							|  |  |  |             'foreign_currency_dp'     => $transaction->foreign_currency_dp, | 
					
						
							|  |  |  |             'bill_id'                 => $transaction->bill_id, | 
					
						
							|  |  |  |             'bill_name'               => $transaction->bill_name, | 
					
						
							|  |  |  |             'category_id'             => $categoryId, | 
					
						
							|  |  |  |             'category_name'           => $categoryName, | 
					
						
							|  |  |  |             'budget_id'               => $budgetId, | 
					
						
							|  |  |  |             'budget_name'             => $budgetName, | 
					
						
							|  |  |  |             'notes'                   => $notes, | 
					
						
							|  |  |  |             'links'                   => [ | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							| 
									
										
										
										
											2018-02-11 07:46:34 +01:00
										 |  |  |                     'uri' => '/transactions/' . $transaction->id, | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |         // expand foreign amount:
 | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |         if (null !== $transaction->transaction_foreign_amount) { | 
					
						
							|  |  |  |             $data['foreign_amount'] = round($transaction->transaction_foreign_amount, (int)$transaction->foreign_currency_dp); | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         // switch on type for consistency
 | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |         switch ($transaction->transaction_type_type) { | 
					
						
							|  |  |  |             case TransactionType::WITHDRAWAL: | 
					
						
							| 
									
										
										
										
											2018-05-29 07:25:04 +02:00
										 |  |  |                 Log::debug(sprintf('%d is a withdrawal', $transaction->journal_id)); | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |                 $data['source_id']        = $transaction->account_id; | 
					
						
							|  |  |  |                 $data['source_name']      = $transaction->account_name; | 
					
						
							|  |  |  |                 $data['source_iban']      = $transaction->account_iban; | 
					
						
							|  |  |  |                 $data['source_type']      = $transaction->account_type; | 
					
						
							|  |  |  |                 $data['destination_id']   = $transaction->opposing_account_id; | 
					
						
							|  |  |  |                 $data['destination_name'] = $transaction->opposing_account_name; | 
					
						
							|  |  |  |                 $data['destination_iban'] = $transaction->opposing_account_iban; | 
					
						
							|  |  |  |                 $data['destination_type'] = $transaction->opposing_account_type; | 
					
						
							| 
									
										
										
										
											2018-05-29 07:25:04 +02:00
										 |  |  |                 Log::debug(sprintf('source_id / account_id is %d', $transaction->account_id)); | 
					
						
							|  |  |  |                 Log::debug(sprintf('source_name / account_name is "%s"', $transaction->account_name)); | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |             case TransactionType::DEPOSIT: | 
					
						
							|  |  |  |             case TransactionType::TRANSFER: | 
					
						
							|  |  |  |             case TransactionType::OPENING_BALANCE: | 
					
						
							|  |  |  |             case TransactionType::RECONCILIATION: | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |                 $data['source_id']        = $transaction->opposing_account_id; | 
					
						
							|  |  |  |                 $data['source_name']      = $transaction->opposing_account_name; | 
					
						
							|  |  |  |                 $data['source_iban']      = $transaction->opposing_account_iban; | 
					
						
							|  |  |  |                 $data['source_type']      = $transaction->opposing_account_type; | 
					
						
							|  |  |  |                 $data['destination_id']   = $transaction->account_id; | 
					
						
							|  |  |  |                 $data['destination_name'] = $transaction->account_name; | 
					
						
							|  |  |  |                 $data['destination_iban'] = $transaction->account_iban; | 
					
						
							|  |  |  |                 $data['destination_type'] = $transaction->account_type; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2018-02-17 10:47:06 +01:00
										 |  |  |                 // @codeCoverageIgnoreStart
 | 
					
						
							|  |  |  |                 throw new FireflyException( | 
					
						
							|  |  |  |                     sprintf('Transaction transformer cannot handle transactions of type "%s"!', $transaction->transaction_type_type) | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |             // @codeCoverageIgnoreEnd
 | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // expand description.
 | 
					
						
							| 
									
										
										
										
											2018-04-24 19:26:16 +02:00
										 |  |  |         if (\strlen((string)$transaction->transaction_description) > 0) { | 
					
						
							| 
									
										
										
										
											2018-02-13 21:04:15 +01:00
										 |  |  |             $data['description'] = $transaction->transaction_description . ' (' . $transaction->description . ')'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 07:46:34 +01:00
										 |  |  |         return $data; | 
					
						
							| 
									
										
										
										
											2018-02-10 10:58:06 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |