/** * @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; }
/** * @param Tag $tag * @param $amount * * @return string */ protected function amountByTagAdvancePayment(Tag $tag, $amount) { if ($this->transactionType->type == 'Withdrawal') { $others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get(); foreach ($others as $other) { $amount = bcsub($amount, $other->amount_positive); } return $amount; } if ($this->transactionType->type == 'Deposit') { return '0'; } return $amount; }
/** * Assuming the journal has only one tag. Parameter amount is used as fallback. * * @param Tag $tag * @param string $amount * * @return string */ protected function amountByTag(Tag $tag, $amount) { if ($tag->tagMode == 'advancePayment') { if ($this->transactionType->type == 'Withdrawal') { $others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get(); foreach ($others as $other) { $amount = bcsub($amount, $other->actual_amount); } return $amount; } if ($this->transactionType->type == 'Deposit') { return '0'; } } if ($tag->tagMode == 'balancingAct') { if ($this->transactionType->type == 'Withdrawal') { $transfer = $tag->transactionJournals()->transactionTypes(['Transfer'])->first(); if ($transfer) { $amount = bcsub($amount, $transfer->actual_amount); return $amount; } } } return $amount; }