Included bill controller.

This commit is contained in:
James Cole
2015-04-05 18:20:06 +02:00
parent ed8e392616
commit 9136e592d3
7 changed files with 506 additions and 123 deletions

View File

@@ -2,9 +2,12 @@
namespace FireflyIII\Repositories\Bill;
use Auth;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
use Log;
use Navigation;
@@ -15,6 +18,62 @@ use Navigation;
*/
class BillRepository implements BillRepositoryInterface
{
/**
* @param Bill $bill
*
* @return mixed
*/
public function destroy(Bill $bill)
{
return $bill->delete();
}
/**
* @return Collection
*/
public function getBills()
{
return Auth::user()->bills()->orderBy('name', 'ASC')->get();
}
/**
* @param Bill $bill
*
* @return Collection
*/
public function getJournals(Bill $bill)
{
return $bill->transactionjournals()->withRelevantData()
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get();
}
/**
* @param Bill $bill
*
* @return Collection
*/
public function getPossiblyRelatedJournals(Bill $bill)
{
$set = \DB::table('transactions')->where('amount', '>', 0)->where('amount', '>=', $bill->amount_min)->where('amount', '<=', $bill->amount_max)->get(
['transaction_journal_id']
);
$ids = [];
/** @var Transaction $entry */
foreach ($set as $entry) {
$ids[] = intval($entry->transaction_journal_id);
}
$journals = new Collection;
if (count($ids) > 0) {
$journals = Auth::user()->transactionjournals()->whereIn('id', $ids)->get();
}
return $journals;
}
/**
* Every bill repeats itself weekly, monthly or yearly (or whatever). This method takes a date-range (usually the view-range of Firefly itself)
* and returns date ranges that fall within the given range; those ranges are the bills expected. When a bill is due on the 14th of the month and
@@ -58,6 +117,21 @@ class BillRepository implements BillRepositoryInterface
return $validRanges;
}
/**
* @param Bill $bill
*
* @return Carbon|null
*/
public function lastFoundMatch(Bill $bill)
{
$last = $bill->transactionjournals()->orderBy('date', 'DESC')->first();
if ($last) {
return $last->date;
}
return null;
}
/**
* @param Bill $bill
*