Files
firefly-iii/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php

134 lines
3.6 KiB
PHP
Raw Normal View History

2014-07-31 22:01:52 +02:00
<?php
namespace Firefly\Storage\Piggybank;
use Carbon\Carbon;
2014-07-31 22:01:52 +02:00
/**
* Class EloquentLimitRepository
*
* @package Firefly\Storage\Limit
*/
class EloquentPiggybankRepository implements PiggybankRepositoryInterface
{
2014-08-10 15:01:46 +02:00
/**
* @return mixed
*/
2014-07-31 22:01:52 +02:00
public function count()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->count();
}
2014-08-10 15:01:46 +02:00
/**
* @param $piggyBankId
*
* @return mixed
*/
2014-07-31 22:01:52 +02:00
public function find($piggyBankId)
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']);
2014-07-31 22:01:52 +02:00
}
public function countRepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('repeats', 1)->count();
}
public function countNonrepeating()
{
return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->where('repeats', 0)->count();
}
2014-08-10 15:01:46 +02:00
/**
* @return mixed
*/
2014-07-31 22:01:52 +02:00
public function get()
{
return \Piggybank::with('account')->leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
'accounts.user_id', \Auth::user()->id
)->get(['piggybanks.*']);
}
2014-08-10 15:01:46 +02:00
/**
* @param $data
*
* @return \Piggybank
*/
2014-07-31 22:01:52 +02:00
public function store($data)
{
var_dump($data);
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
$piggyBank = new \Piggybank($data);
2014-07-31 22:01:52 +02:00
$piggyBank->account()->associate($account);
$today = new Carbon;
2014-07-31 22:01:52 +02:00
if ($piggyBank->validate()) {
echo 'Valid, but some more checking!';
if($piggyBank->targetdate < $today) {
$piggyBank->errors()->add('targetdate','Target date cannot be in the past.');
echo 'errrrrrr on target date';
return $piggyBank;
}
// first period for reminder is AFTER target date.
// just flash a warning
die('Here be a check. Sorry for the kill-switch. Will continue tomorrow.');
2014-07-31 22:01:52 +02:00
$piggyBank->save();
}
return $piggyBank;
}
2014-08-10 15:01:46 +02:00
/**
* @param $data
*
* @return mixed
*/
public function update($data)
{
$piggyBank = $this->find($data['id']);
if ($piggyBank) {
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$account = $accounts->find($data['account_id']);
// update piggybank accordingly:
$piggyBank->name = $data['name'];
$piggyBank->target = floatval($data['target']);
$piggyBank->account()->associate($account);
if ($piggyBank->validate()) {
$piggyBank->save();
}
}
return $piggyBank;
}
2014-08-10 15:01:46 +02:00
/**
* @param \Piggybank $piggyBank
* @param $amount
*
* @return mixed|void
*/
public function updateAmount(\Piggybank $piggyBank, $amount)
{
$piggyBank->amount = floatval($amount);
if ($piggyBank->validate()) {
$piggyBank->save();
}
}
2014-07-31 22:01:52 +02:00
}