Exemplo n.º 1
0
 /**
  * @param TransactionJournal $journal
  *
  * @return bool
  */
 public function act(TransactionJournal $journal) : bool
 {
     // journal has this tag maybe?
     $tag = Tag::firstOrCreateEncrypted(['tag' => $this->action->action_value, 'user_id' => $journal->user->id]);
     $count = $journal->tags()->where('tag_id', $tag->id)->count();
     if ($count === 0) {
         $journal->tags()->save($tag);
         Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal->id));
         return true;
     }
     Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id));
     return true;
 }
Exemplo n.º 2
0
 /**
  * @param TransactionJournal $journal
  *
  * @return bool
  */
 public function act(TransactionJournal $journal) : bool
 {
     // if tag does not exist, no need to continue:
     $name = $this->action->action_value;
     /** @var Tag $tag */
     $tag = $journal->user->tags()->get()->filter(function (Tag $tag) use($name) {
         return $tag->tag == $name;
     })->first();
     if (!is_null($tag)) {
         Log::debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal->id));
         $journal->tags()->detach([$tag->id]);
         return true;
     }
     Log::debug(sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal->id));
     return true;
 }
Exemplo n.º 3
0
 /**
  * If a transaction journal has multiple tags, we'll have to gamble. FF3
  * does not yet block adding multiple 'special' tags so we must wing it.
  *
  * We grab the first special tag (for advancePayment and for balancingAct
  * and try to format those. If they're not present (it's all normal tags),
  * we can format like any other journal.
  *
  * @param TransactionJournal $journal
  *
  * @return string
  */
 protected function relevantTagsMulti(TransactionJournal $journal)
 {
     $firstBalancingAct = $journal->tags()->where('tagMode', 'balancingAct')->first();
     if ($firstBalancingAct) {
         return $this->formatJournalByTag($journal, $firstBalancingAct);
     }
     $firstAdvancePayment = $journal->tags()->where('tagMode', 'advancePayment')->first();
     if ($firstAdvancePayment) {
         return $this->formatJournalByTag($journal, $firstAdvancePayment);
     }
     return $this->relevantTagsNoTags($journal);
 }
Exemplo n.º 4
0
 /**
  * @param TransactionJournal $journal
  */
 protected function saveTags(TransactionJournal $journal)
 {
     if (!is_null($this->importData['tags'])) {
         foreach ($this->importData['tags'] as $tag) {
             $journal->tags()->save($tag);
         }
     }
 }
Exemplo n.º 5
0
 /**
  * @param TransactionJournal $journal
  * @param Tag                $tag
  *
  * @return bool
  */
 protected function connectBalancingAct(TransactionJournal $journal, Tag $tag) : bool
 {
     /** @var TransactionType $withdrawal */
     $withdrawal = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
     $withdrawals = $tag->transactionJournals()->where('transaction_type_id', $withdrawal->id)->count();
     /** @var TransactionType $transfer */
     $transfer = TransactionType::whereType(TransactionType::TRANSFER)->first();
     $transfers = $tag->transactionJournals()->where('transaction_type_id', $transfer->id)->count();
     // only if this is the only withdrawal.
     if ($journal->transaction_type_id == $withdrawal->id && $withdrawals < 1) {
         $journal->tags()->save($tag);
         $journal->save();
         return true;
     }
     // and only if this is the only transfer
     if ($journal->transaction_type_id == $transfer->id && $transfers < 1) {
         $journal->tags()->save($tag);
         $journal->save();
         return true;
     }
     // ignore expense
     return false;
 }
Exemplo n.º 6
0
 /**
  * @param TransactionJournal $journal
  * @param Tag                $tag
  *
  * @return bool
  */
 protected function matchAll(TransactionJournal $journal, Tag $tag)
 {
     $match = true;
     /** @var TransactionJournal $check */
     foreach ($tag->transactionjournals as $check) {
         // $checkAccount is the source_account for a withdrawal
         // $checkAccount is the destination_account for a deposit
         if ($check->transactionType->type == 'Withdrawal' && $check->source_account->id != $journal->destination_account->id) {
             $match = false;
         }
         if ($check->transactionType->type == 'Deposit' && $check->destination_account->id != $journal->destination_account->id) {
             $match = false;
         }
     }
     if ($match) {
         $journal->tags()->save($tag);
         $journal->save();
         return true;
     }
     return false;
 }
Exemplo n.º 7
0
 /**
  * @param TransactionJournal $journal
  *
  * @return bool
  */
 public function act(TransactionJournal $journal) : bool
 {
     Log::debug(sprintf('RuleAction ClearCategory removed all tags from journal %d.', $journal->id));
     $journal->tags()->detach();
     return true;
 }