Some foreach loop cleaning up.

This commit is contained in:
James Cole 2015-04-11 12:27:56 +02:00
parent 5c06b45eb1
commit f6999f355b
2 changed files with 9 additions and 46 deletions

View File

@ -45,15 +45,10 @@ class JsonController extends Controller
foreach ($ranges as $range) { foreach ($ranges as $range) {
// paid a bill in this range? // paid a bill in this range?
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']); $amount += $repository->getJournalsInRange($bill, $range['start'], $range['end'])->sum('amount');
foreach ($journals as $journal) {
$amount += $journal->amount;
}
} }
} }
unset($journals, $journal); unset($ranges, $bill, $range, $bills);
/** /**
* Find credit card accounts and possibly unpaid credit card bills. * Find credit card accounts and possibly unpaid credit card bills.
@ -66,12 +61,7 @@ class JsonController extends Controller
if ($balance == 0) { if ($balance == 0) {
// find a transfer TO the credit card which should account for // find a transfer TO the credit card which should account for
// anything paid. If not, the CC is not yet used. // anything paid. If not, the CC is not yet used.
$journals = $accountRepository->getTransfersInRange($creditCard, $start, $end); $amount += $accountRepository->getTransfersInRange($creditCard, $start, $end)->sum('amount');
foreach ($journals as $journal) {
$amount += floatval($journal->amount);
}
} }
} }
@ -86,26 +76,22 @@ class JsonController extends Controller
*/ */
public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository) public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{ {
$start = Session::get('start');
$end = Session::get('end');
$amount = 0; $amount = 0;
$bills = $repository->getActiveBills(); $bills = $repository->getActiveBills();
$unpaid = new Collection; // bills $unpaid = new Collection; // bills
/** @var Bill $bill */ /** @var Bill $bill */
foreach ($bills as $bill) { foreach ($bills as $bill) {
$ranges = $repository->getRanges($bill, $start, $end); $ranges = $repository->getRanges($bill, clone Session::get('start'), clone Session::get('end'));
foreach ($ranges as $range) { foreach ($ranges as $range) {
// paid a bill in this range?
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']); $journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);
if ($journals->count() == 0) { if ($journals->count() == 0) {
$unpaid->push([$bill, $range['start']]); $unpaid->push([$bill, $range['start']]);
} }
} }
} }
unset($bill, $range, $ranges); unset($bill, $bills, $range, $ranges);
$creditCards = $accountRepository->getCreditCards(); $creditCards = $accountRepository->getCreditCards();
foreach ($creditCards as $creditCard) { foreach ($creditCards as $creditCard) {
@ -119,8 +105,6 @@ class JsonController extends Controller
$unpaid->push([$fakeBill, $date]); $unpaid->push([$fakeBill, $date]);
} }
} }
// loop unpaid:
/** @var Bill $entry */ /** @var Bill $entry */
foreach ($unpaid as $entry) { foreach ($unpaid as $entry) {
$current = ($entry[0]->amount_max + $entry[0]->amount_min) / 2; $current = ($entry[0]->amount_max + $entry[0]->amount_min) / 2;
@ -139,12 +123,7 @@ class JsonController extends Controller
{ {
$start = Session::get('start'); $start = Session::get('start');
$end = Session::get('end'); $end = Session::get('end');
$amount = 0; $amount = $reportQuery->incomeByPeriod($start, $end, true)->sum('queryAmount');
$set = $reportQuery->incomeByPeriod($start, $end, true);
foreach ($set as $entry) {
$amount += $entry->queryAmount;
}
return Response::json(['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); return Response::json(['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
} }
@ -158,13 +137,7 @@ class JsonController extends Controller
{ {
$start = Session::get('start'); $start = Session::get('start');
$end = Session::get('end'); $end = Session::get('end');
$amount = 0; $amount = $reportQuery->journalsByExpenseAccount($start, $end, true)->sum('queryAmount');
$set = $reportQuery->journalsByExpenseAccount($start, $end, true);
foreach ($set as $entry) {
$amount += $entry->queryAmount;
}
return Response::json(['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); return Response::json(['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
} }

View File

@ -62,12 +62,7 @@ class BillRepository implements BillRepositoryInterface
public function getActiveBills() public function getActiveBills()
{ {
/** @var Collection $set */ /** @var Collection $set */
$set = Auth::user()->bills()->orderBy('name', 'ASC')->where('active', 1)->get(); $set = Auth::user()->bills()->orderBy('name', 'ASC')->where('active', 1)->get()->sortBy('name');
$set->sort(
function (Bill $bill) {
return $bill->name;
}
);
return $set; return $set;
} }
@ -78,12 +73,7 @@ class BillRepository implements BillRepositoryInterface
public function getBills() public function getBills()
{ {
/** @var Collection $set */ /** @var Collection $set */
$set = Auth::user()->bills()->orderBy('name', 'ASC')->get(); $set = Auth::user()->bills()->orderBy('name', 'ASC')->get()->sortBy('name');
$set->sort(
function (Bill $bill) {
return $bill->name;
}
);
return $set; return $set;
} }