2014-07-31 22:01:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Firefly\Storage\Piggybank;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2014-08-02 07:34:38 +02:00
|
|
|
)->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']);
|
2014-07-31 22:01:52 +02:00
|
|
|
}
|
|
|
|
|
2014-08-13 20:36:32 +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;
|
|
|
|
|
|
|
|
|
2014-08-13 20:36:32 +02:00
|
|
|
$piggyBank = new \Piggybank($data);
|
2014-07-31 22:01:52 +02:00
|
|
|
$piggyBank->account()->associate($account);
|
|
|
|
if ($piggyBank->validate()) {
|
|
|
|
$piggyBank->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $piggyBank;
|
|
|
|
}
|
2014-08-02 07:34:38 +02:00
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-08-02 07:34:38 +02:00
|
|
|
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
|
|
|
|
*/
|
2014-08-02 07:34:38 +02:00
|
|
|
public function updateAmount(\Piggybank $piggyBank, $amount)
|
|
|
|
{
|
|
|
|
$piggyBank->amount = floatval($amount);
|
|
|
|
if ($piggyBank->validate()) {
|
|
|
|
$piggyBank->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-31 22:01:52 +02:00
|
|
|
}
|