2014-10-29 10:30:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
2014-12-29 20:28:17 +01:00
|
|
|
namespace FireflyIII\Database\Bill;
|
2014-10-29 10:30:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-01-02 06:05:40 +01:00
|
|
|
use FireflyIII\Database\CommonDatabaseCallsInterface;
|
|
|
|
|
use FireflyIII\Database\CUDInterface;
|
2014-12-13 22:11:51 +01:00
|
|
|
use FireflyIII\Database\SwitchUser;
|
2014-11-12 22:37:09 +01:00
|
|
|
use FireflyIII\Exception\NotImplementedException;
|
2014-12-20 15:00:53 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent;
|
2014-11-12 22:37:09 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2014-11-13 11:17:39 +01:00
|
|
|
use Illuminate\Support\MessageBag;
|
2014-12-06 12:12:55 +01:00
|
|
|
|
2014-10-29 10:30:52 +01:00
|
|
|
/**
|
2014-12-29 20:28:17 +01:00
|
|
|
* Class Bill
|
2014-10-29 10:30:52 +01:00
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Database
|
|
|
|
|
*/
|
2015-01-02 06:05:40 +01:00
|
|
|
class Bill implements CUDInterface, CommonDatabaseCallsInterface, BillInterface
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
|
|
|
|
use SwitchUser;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->setUser(\Auth::user());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-20 15:00:53 +01:00
|
|
|
* @param Eloquent $model
|
2014-10-29 10:30:52 +01:00
|
|
|
*
|
2014-11-12 22:37:09 +01:00
|
|
|
* @return bool
|
2014-10-29 10:30:52 +01:00
|
|
|
*/
|
2014-12-20 15:00:53 +01:00
|
|
|
public function destroy(Eloquent $model)
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
2014-11-13 16:13:32 +01:00
|
|
|
$model->delete();
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-12 22:21:48 +01:00
|
|
|
/**
|
2014-11-12 22:37:09 +01:00
|
|
|
* @param array $data
|
2014-11-12 22:21:48 +01:00
|
|
|
*
|
2014-12-06 12:12:55 +01:00
|
|
|
* @return \Eloquent
|
2014-11-12 22:21:48 +01:00
|
|
|
*/
|
2014-11-12 22:37:09 +01:00
|
|
|
public function store(array $data)
|
2014-11-12 22:21:48 +01:00
|
|
|
{
|
2014-12-29 20:28:17 +01:00
|
|
|
$bill = new \Bill;
|
|
|
|
|
$bill->user()->associate($this->getUser());
|
|
|
|
|
$bill->name = $data['name'];
|
|
|
|
|
$bill->match = $data['match'];
|
|
|
|
|
$bill->amount_max = floatval($data['amount_max']);
|
|
|
|
|
$bill->amount_min = floatval($data['amount_min']);
|
2014-11-13 16:13:32 +01:00
|
|
|
|
|
|
|
|
$date = new Carbon($data['date']);
|
|
|
|
|
|
|
|
|
|
|
2014-12-29 20:28:17 +01:00
|
|
|
$bill->active = intval($data['active']);
|
|
|
|
|
$bill->automatch = intval($data['automatch']);
|
|
|
|
|
$bill->repeat_freq = $data['repeat_freq'];
|
2014-11-13 16:13:32 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Jump to the start of the period.
|
|
|
|
|
*/
|
2014-12-30 18:44:58 +01:00
|
|
|
$date = \DateKit::startOfPeriod($date, $data['repeat_freq']);
|
2014-12-29 20:28:17 +01:00
|
|
|
$bill->date = $date;
|
|
|
|
|
$bill->skip = intval($data['skip']);
|
2014-11-13 16:13:32 +01:00
|
|
|
|
2014-12-29 20:28:17 +01:00
|
|
|
$bill->save();
|
2014-11-13 16:13:32 +01:00
|
|
|
|
2014-12-29 20:28:17 +01:00
|
|
|
return $bill;
|
2014-11-12 22:21:48 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-29 10:30:52 +01:00
|
|
|
/**
|
2014-12-20 15:00:53 +01:00
|
|
|
* @param Eloquent $model
|
|
|
|
|
* @param array $data
|
2014-10-29 10:30:52 +01:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-12-20 15:00:53 +01:00
|
|
|
public function update(Eloquent $model, array $data)
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
2014-11-13 16:13:32 +01:00
|
|
|
$model->name = $data['name'];
|
|
|
|
|
$model->match = $data['match'];
|
|
|
|
|
$model->amount_max = floatval($data['amount_max']);
|
|
|
|
|
$model->amount_min = floatval($data['amount_min']);
|
|
|
|
|
|
|
|
|
|
$date = new Carbon($data['date']);
|
|
|
|
|
|
|
|
|
|
$model->date = $date;
|
2014-12-14 20:40:02 +01:00
|
|
|
$model->active = intval($data['active']);
|
|
|
|
|
$model->automatch = intval($data['automatch']);
|
2014-11-13 16:13:32 +01:00
|
|
|
$model->repeat_freq = $data['repeat_freq'];
|
|
|
|
|
$model->skip = intval($data['skip']);
|
|
|
|
|
$model->save();
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-11-12 22:37:09 +01:00
|
|
|
* Validates an array. Returns an array containing MessageBags
|
2014-10-29 10:30:52 +01:00
|
|
|
* errors/warnings/successes.
|
|
|
|
|
*
|
2014-12-15 19:39:01 +01:00
|
|
|
* ignored because this method will be gone soon.
|
|
|
|
|
*
|
2014-11-12 22:37:09 +01:00
|
|
|
* @param array $model
|
2014-10-29 10:30:52 +01:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2014-11-12 22:37:09 +01:00
|
|
|
public function validate(array $model)
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
2014-11-13 11:17:39 +01:00
|
|
|
$warnings = new MessageBag;
|
|
|
|
|
$successes = new MessageBag;
|
|
|
|
|
$errors = new MessageBag;
|
2015-01-18 09:49:32 +01:00
|
|
|
if (floatval($model['amount_min']) > floatval($model['amount_max'])) {
|
2014-11-13 11:17:39 +01:00
|
|
|
$errors->add('amount_max', 'Maximum amount can not be less than minimum amount.');
|
|
|
|
|
$errors->add('amount_min', 'Minimum amount can not be more than maximum amount.');
|
|
|
|
|
}
|
2014-12-29 20:28:17 +01:00
|
|
|
$object = new \Bill($model);
|
2014-12-20 15:00:53 +01:00
|
|
|
$object->isValid();
|
|
|
|
|
$errors->merge($object->getErrors());
|
2014-11-13 11:17:39 +01:00
|
|
|
|
2014-11-13 16:13:32 +01:00
|
|
|
$set = ['name', 'match', 'amount_min', 'amount_max', 'date', 'repeat_freq', 'skip', 'automatch', 'active'];
|
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
|
if (!$errors->has($entry)) {
|
|
|
|
|
$successes->add($entry, 'OK');
|
2014-11-13 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-17 10:06:12 +01:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
|
*
|
2014-11-12 22:37:09 +01:00
|
|
|
* Returns an object with id $id.
|
|
|
|
|
*
|
2014-12-19 21:18:42 +01:00
|
|
|
* @param int $objectId
|
2014-10-29 10:30:52 +01:00
|
|
|
*
|
2014-12-06 12:12:55 +01:00
|
|
|
* @return \Eloquent
|
2014-12-13 21:59:02 +01:00
|
|
|
* @throws NotImplementedException
|
2014-10-29 10:30:52 +01:00
|
|
|
*/
|
2014-12-19 21:18:42 +01:00
|
|
|
public function find($objectId)
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
2014-11-12 22:21:48 +01:00
|
|
|
throw new NotImplementedException;
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-17 10:06:12 +01:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
|
*
|
2014-11-12 22:37:09 +01:00
|
|
|
* Finds an account type using one of the "$what"'s: expense, asset, revenue, opening, etc.
|
2014-10-29 10:30:52 +01:00
|
|
|
*
|
2014-11-12 22:37:09 +01:00
|
|
|
* @param $what
|
|
|
|
|
*
|
|
|
|
|
* @return \AccountType|null
|
2014-12-13 21:59:02 +01:00
|
|
|
* @throws NotImplementedException
|
2014-10-29 10:30:52 +01:00
|
|
|
*/
|
2014-11-12 22:37:09 +01:00
|
|
|
public function findByWhat($what)
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
2014-11-12 22:21:48 +01:00
|
|
|
throw new NotImplementedException;
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-11-12 22:37:09 +01:00
|
|
|
* Returns all objects.
|
2014-11-12 22:21:48 +01:00
|
|
|
*
|
2014-11-12 22:37:09 +01:00
|
|
|
* @return Collection
|
2014-10-29 10:30:52 +01:00
|
|
|
*/
|
2014-11-12 22:37:09 +01:00
|
|
|
public function get()
|
2014-10-29 10:30:52 +01:00
|
|
|
{
|
2014-12-29 20:28:17 +01:00
|
|
|
return $this->getUser()->bills()->get();
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-17 10:06:12 +01:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2014-10-29 10:30:52 +01:00
|
|
|
* @param array $ids
|
|
|
|
|
*
|
|
|
|
|
* @return Collection
|
2014-12-13 21:59:02 +01:00
|
|
|
* @throws NotImplementedException
|
2014-10-29 10:30:52 +01:00
|
|
|
*/
|
|
|
|
|
public function getByIds(array $ids)
|
|
|
|
|
{
|
2014-11-12 22:21:48 +01:00
|
|
|
throw new NotImplementedException;
|
2014-10-29 10:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-15 07:24:01 +01:00
|
|
|
/**
|
|
|
|
|
* Returns all objects.
|
|
|
|
|
*
|
|
|
|
|
* @return Collection
|
|
|
|
|
*/
|
|
|
|
|
public function getActive()
|
|
|
|
|
{
|
2014-12-29 20:28:17 +01:00
|
|
|
return $this->getUser()->bills()->where('active', 1)->get();
|
2014-12-15 07:24:01 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-02 12:38:13 +01:00
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-17 10:06:12 +01:00
|
|
|
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
|
|
|
|
|
*
|
2015-01-02 12:38:13 +01:00
|
|
|
* @param \Bill $bill
|
|
|
|
|
*
|
|
|
|
|
* @return Carbon|null
|
|
|
|
|
*/
|
|
|
|
|
public function nextExpectedMatch(\Bill $bill)
|
|
|
|
|
{
|
|
|
|
|
$finalDate = null;
|
|
|
|
|
if ($bill->active == 0) {
|
|
|
|
|
return $finalDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* $today is the start of the next period, to make sure FF3 won't miss anything
|
|
|
|
|
* when the current period has a transaction journal.
|
|
|
|
|
*/
|
|
|
|
|
$today = \DateKit::addPeriod(new Carbon, $bill->repeat_freq, 0);
|
|
|
|
|
|
|
|
|
|
$skip = $bill->skip + 1;
|
|
|
|
|
$start = \DateKit::startOfPeriod(new Carbon, $bill->repeat_freq);
|
|
|
|
|
/*
|
|
|
|
|
* go back exactly one month/week/etc because FF3 does not care about 'next'
|
|
|
|
|
* bills if they're too far into the past.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$counter = 0;
|
|
|
|
|
while ($start <= $today) {
|
|
|
|
|
if (($counter % $skip) == 0) {
|
|
|
|
|
// do something.
|
|
|
|
|
$end = \DateKit::endOfPeriod(clone $start, $bill->repeat_freq);
|
|
|
|
|
$journalCount = $bill->transactionjournals()->before($end)->after($start)->count();
|
|
|
|
|
if ($journalCount == 0) {
|
|
|
|
|
$finalDate = clone $start;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add period for next round!
|
|
|
|
|
$start = \DateKit::addPeriod($start, $bill->repeat_freq, 0);
|
|
|
|
|
$counter++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $finalDate;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-13 16:13:32 +01:00
|
|
|
/**
|
2014-12-30 18:44:58 +01:00
|
|
|
* @param \Bill $bill
|
|
|
|
|
* @param \TransactionJournal $journal
|
2014-11-13 16:13:32 +01:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-12-29 20:28:17 +01:00
|
|
|
public function scan(\Bill $bill, \TransactionJournal $journal)
|
2014-11-13 16:13:32 +01:00
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Match words.
|
|
|
|
|
*/
|
|
|
|
|
$wordMatch = false;
|
2014-12-29 20:28:17 +01:00
|
|
|
$matches = explode(',', $bill->match);
|
2014-11-13 16:13:32 +01:00
|
|
|
$description = strtolower($journal->description);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Attach expense account to description for more narrow matching.
|
|
|
|
|
*/
|
|
|
|
|
if (count($journal->transactions) < 2) {
|
|
|
|
|
$transactions = $journal->transactions()->get();
|
|
|
|
|
} else {
|
|
|
|
|
$transactions = $journal->transactions;
|
|
|
|
|
}
|
|
|
|
|
/** @var \Transaction $transaction */
|
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
|
/** @var \Account $account */
|
|
|
|
|
$account = $transaction->account()->first();
|
|
|
|
|
/** @var \AccountType $type */
|
|
|
|
|
$type = $account->accountType()->first();
|
|
|
|
|
if ($type->type == 'Expense account' || $type->type == 'Beneficiary account') {
|
|
|
|
|
$description .= ' ' . strtolower($account->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
\Log::debug('Final description: ' . $description);
|
|
|
|
|
\Log::debug('Matches searched: ' . join(':', $matches));
|
|
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
foreach ($matches as $word) {
|
|
|
|
|
if (!(strpos($description, strtolower($word)) === false)) {
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($count >= count($matches)) {
|
|
|
|
|
$wordMatch = true;
|
|
|
|
|
\Log::debug('word match is true');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Match amount.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$amountMatch = false;
|
|
|
|
|
if (count($transactions) > 1) {
|
|
|
|
|
|
|
|
|
|
$amount = max(floatval($transactions[0]->amount), floatval($transactions[1]->amount));
|
2014-12-29 20:28:17 +01:00
|
|
|
$min = floatval($bill->amount_min);
|
|
|
|
|
$max = floatval($bill->amount_max);
|
2014-11-13 16:13:32 +01:00
|
|
|
if ($amount >= $min && $amount <= $max) {
|
|
|
|
|
$amountMatch = true;
|
|
|
|
|
\Log::debug('Amount match is true!');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If both, update!
|
|
|
|
|
*/
|
|
|
|
|
if ($wordMatch && $amountMatch) {
|
2014-12-29 20:28:17 +01:00
|
|
|
$journal->bill()->associate($bill);
|
2014-11-13 16:13:32 +01:00
|
|
|
$journal->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-29 20:28:17 +01:00
|
|
|
* @param \Bill $bill
|
2014-11-13 16:13:32 +01:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-12-29 20:28:17 +01:00
|
|
|
public function scanEverything(\Bill $bill)
|
2014-11-13 16:13:32 +01:00
|
|
|
{
|
|
|
|
|
// get all journals that (may) be relevant.
|
|
|
|
|
// this is usually almost all of them.
|
|
|
|
|
|
2014-12-13 22:54:52 +01:00
|
|
|
/** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $journalRepository */
|
|
|
|
|
$journalRepository = \App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
|
2014-11-13 16:13:32 +01:00
|
|
|
|
2014-12-29 20:28:17 +01:00
|
|
|
$set = \DB::table('transactions')->where('amount', '>', 0)->where('amount', '>=', $bill->amount_min)->where('amount', '<=', $bill->amount_max)
|
2014-11-14 14:33:41 +01:00
|
|
|
->get(['transaction_journal_id']);
|
2014-11-13 16:13:32 +01:00
|
|
|
$ids = [];
|
|
|
|
|
|
|
|
|
|
/** @var \Transaction $entry */
|
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
|
$ids[] = intval($entry->transaction_journal_id);
|
|
|
|
|
}
|
|
|
|
|
if (count($ids) > 0) {
|
|
|
|
|
$journals = $journalRepository->getByIds($ids);
|
|
|
|
|
/** @var \TransactionJournal $journal */
|
|
|
|
|
foreach ($journals as $journal) {
|
2014-12-29 20:28:17 +01:00
|
|
|
$this->scan($bill, $journal);
|
2014-11-13 16:13:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-01-02 06:16:49 +01:00
|
|
|
}
|