firefly-iii/app/Repositories/PiggyBank/PiggyBankRepository.php

156 lines
3.6 KiB
PHP
Raw Normal View History

2015-02-25 12:32:33 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-25 12:32:33 -06:00
namespace FireflyIII\Repositories\PiggyBank;
use Auth;
2015-04-20 14:57:20 -05:00
use Carbon\Carbon;
2015-03-15 12:00:33 -05:00
use DB;
2015-02-25 12:32:33 -06:00
use FireflyIII\Models\PiggyBank;
2015-04-20 14:57:20 -05:00
use FireflyIII\Models\PiggyBankEvent;
2015-02-27 04:02:08 -06:00
use Illuminate\Support\Collection;
2015-02-25 12:32:33 -06:00
/**
* Class PiggyBankRepository
*
* @package FireflyIII\Repositories\PiggyBank
*/
class PiggyBankRepository implements PiggyBankRepositoryInterface
{
2015-04-20 14:57:20 -05:00
/**
* @param PiggyBank $piggyBank
2016-02-05 02:25:15 -06:00
* @param string $amount
2015-04-20 14:57:20 -05:00
*
* @return bool
*/
2016-02-05 02:25:15 -06:00
public function createEvent(PiggyBank $piggyBank, string $amount)
2015-04-20 14:57:20 -05:00
{
PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
return true;
}
/**
* @param PiggyBank $piggyBank
*
* @return boolean|null
2015-04-20 14:57:20 -05:00
*/
public function destroy(PiggyBank $piggyBank)
{
return $piggyBank->delete();
}
2015-04-07 10:51:22 -05:00
/**
* @param PiggyBank $piggyBank
*
* @return Collection
*/
public function getEventSummarySet(PiggyBank $piggyBank)
{
return DB::table('piggy_bank_events')->where('piggy_bank_id', $piggyBank->id)->groupBy('date')->get(['date', DB::Raw('SUM(`amount`) AS `sum`')]);
}
2015-04-20 14:57:20 -05:00
/**
* @param PiggyBank $piggyBank
*
* @return Collection
*/
public function getEvents(PiggyBank $piggyBank)
{
return $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
}
2016-01-24 13:38:58 -06:00
/**
* @return int
*/
public function getMaxOrder()
{
return intval(Auth::user()->piggyBanks()->max('order'));
}
2015-04-20 14:57:20 -05:00
/**
* @return Collection
*/
public function getPiggyBanks()
{
/** @var Collection $set */
$set = Auth::user()->piggyBanks()->orderBy('order', 'ASC')->get();
return $set;
}
2015-03-15 12:00:33 -05:00
/**
* Set all piggy banks to order 0.
*
2015-05-09 15:30:16 -05:00
* @return boolean
2015-03-15 12:00:33 -05:00
*/
public function reset()
{
2015-05-09 15:30:16 -05:00
// split query to make it work in sqlite:
$set = PiggyBank::
2015-05-14 02:51:54 -05:00
leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.id')
->where('accounts.user_id', Auth::user()->id)->get(['piggy_banks.*']);
2015-05-09 15:30:16 -05:00
foreach ($set as $e) {
$e->order = 0;
$e->save();
}
return true;
2015-03-15 12:00:33 -05:00
}
/**
*
* set id of piggy bank.
*
2015-07-08 23:13:39 -05:00
* @param int $piggyBankId
2015-03-15 12:00:33 -05:00
* @param int $order
*
* @return void
*/
2016-02-05 02:25:15 -06:00
public function setOrder(int $piggyBankId, int $order)
2015-03-15 12:00:33 -05:00
{
2015-03-15 12:08:52 -05:00
$piggyBank = PiggyBank::leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', Auth::user()->id)
2015-07-08 23:13:39 -05:00
->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']);
2015-03-15 12:00:33 -05:00
if ($piggyBank) {
$piggyBank->order = $order;
$piggyBank->save();
}
}
/**
* @param array $data
*
* @return PiggyBank
*/
public function store(array $data)
{
2015-06-23 15:13:13 -05:00
$data['remind_me'] = false;
$data['reminder_skip'] = 0;
2015-06-27 01:06:24 -05:00
$piggyBank = PiggyBank::create($data);
return $piggyBank;
}
/**
2015-05-05 03:23:01 -05:00
* @param PiggyBank $piggyBank
* @param array $data
*
* @return PiggyBank
*/
public function update(PiggyBank $piggyBank, array $data)
{
$piggyBank->name = $data['name'];
$piggyBank->account_id = intval($data['account_id']);
$piggyBank->targetamount = floatval($data['targetamount']);
$piggyBank->targetdate = $data['targetdate'];
$piggyBank->startdate = $data['startdate'];
$piggyBank->save();
return $piggyBank;
}
2015-03-29 01:14:32 -05:00
}