Some small code optimisations.

This commit is contained in:
James Cole
2015-07-09 06:13:39 +02:00
parent 0372c1aaf1
commit 84a7f825d7
7 changed files with 59 additions and 33 deletions

View File

@@ -43,6 +43,8 @@ class AuthController extends Controller
*/
public function __construct()
{
parent::__construct();
$this->middleware('guest', ['except' => 'getLogout']);
}

View File

@@ -36,6 +36,7 @@ class PasswordController extends Controller
*/
public function __construct()
{
parent::__construct();
$this->middleware('guest');
}

View File

@@ -150,14 +150,9 @@ class TransactionJournal extends Model
return $cache->get(); // @codeCoverageIgnore
}
$amount = '0';
bcscale(2);
/** @var Transaction $t */
foreach ($this->transactions as $t) {
if ($t->amount > 0) {
$amount = $t->amount;
}
}
$set = $this->transactions->sortByDesc('amount');
$amount = $set->first()->amount;
if (intval($this->tag_count) === 1) {
// get amount for single tag:
@@ -175,6 +170,49 @@ class TransactionJournal extends Model
}
/**
* @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->actual_amount);
}
return $amount;
}
if ($this->transactionType->type == 'Deposit') {
return '0';
}
return $amount;
}
/**
* @param $tag
* @param $amount
*
* @return string
*/
protected function amountByTagBalancingAct($tag, $amount)
{
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $tag->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
$amount = bcsub($amount, $transfer->actual_amount);
return $amount;
}
}
return $amount;
}
/**
* Assuming the journal has only one tag. Parameter amount is used as fallback.
*
@@ -186,28 +224,12 @@ class TransactionJournal extends Model
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';
}
return $this->amountByTagAdvancePayment($tag, $amount);
}
if ($tag->tagMode == 'balancingAct') {
if ($this->transactionType->type == 'Withdrawal') {
$transfer = $tag->transactionJournals()->transactionTypes(['Transfer'])->first();
if ($transfer) {
$amount = bcsub($amount, $transfer->actual_amount);
return $this->amountByTagBalancingAct($tag, $amount);
return $amount;
}
}
}
return $amount;

View File

@@ -94,15 +94,15 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
*
* set id of piggy bank.
*
* @param int $id
* @param int $piggyBankId
* @param int $order
*
* @return void
*/
public function setOrder($id, $order)
public function setOrder($piggyBankId, $order)
{
$piggyBank = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', Auth::user()->id)
->where('piggy_banks.id', $id)->first(['piggy_banks.*']);
->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
if ($piggyBank) {
$piggyBank->order = $order;
$piggyBank->save();

View File

@@ -58,12 +58,12 @@ interface PiggyBankRepositoryInterface
*
* set id of piggy bank.
*
* @param int $id
* @param int $piggyBankId
* @param int $order
*
* @return void
*/
public function setOrder($id, $order);
public function setOrder($piggyBankId, $order);
/**