firefly-iii/app/Import/Object/ImportBill.php

259 lines
6.8 KiB
PHP
Raw Normal View History

2017-06-14 13:13:19 -05:00
<?php
/**
* ImportBill.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-06-14 13:13:19 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Import\Object;
2017-06-21 13:04:35 -05:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
2017-09-14 23:26:09 -05:00
use Steam;
2017-06-21 13:04:35 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class ImportBill.
2017-06-21 13:04:35 -05:00
*/
2017-06-14 13:13:19 -05:00
class ImportBill
{
2017-09-14 23:26:09 -05:00
/** @var string */
private $amount = '1';
2017-06-21 13:04:35 -05:00
/** @var Bill */
private $bill;
2017-06-14 13:13:19 -05:00
/** @var array */
private $id = [];
/** @var array */
private $name = [];
2017-06-21 13:04:35 -05:00
/** @var BillRepositoryInterface */
private $repository;
2017-11-15 05:25:49 -06:00
/** @var User */
2017-06-21 13:04:35 -05:00
private $user;
/**
* ImportBill constructor.
*/
public function __construct()
{
$this->bill = new Bill;
$this->repository = app(BillRepositoryInterface::class);
Log::debug('Created ImportBill.');
}
/**
* @return Bill
*/
public function getBill(): Bill
{
2017-11-15 05:25:49 -06:00
if (null === $this->bill->id) {
2017-06-21 13:04:35 -05:00
$this->store();
}
return $this->bill;
}
2017-06-14 13:13:19 -05:00
2017-09-14 23:26:09 -05:00
/**
* @param string $amount
*/
public function setAmount(string $amount)
{
$this->amount = Steam::positive($amount);
}
2017-06-14 13:13:19 -05:00
/**
* @param array $id
*/
public function setId(array $id)
{
$this->id = $id;
}
/**
* @param array $name
*/
public function setName(array $name)
{
$this->name = $name;
}
2017-06-21 13:04:35 -05:00
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* @return Bill
*/
private function findExistingObject(): Bill
{
Log::debug('In findExistingObject() for Bill');
// 1: find by ID, or name
2017-11-15 05:25:49 -06:00
if (3 === count($this->id)) {
2017-06-21 13:04:35 -05:00
Log::debug(sprintf('Finding bill with ID #%d', $this->id['value']));
/** @var Bill $bill */
$bill = $this->repository->find(intval($this->id['value']));
2017-11-15 05:25:49 -06:00
if (null !== $bill->id) {
2017-06-21 13:04:35 -05:00
Log::debug(sprintf('Found unmapped bill by ID (#%d): %s', $bill->id, $bill->name));
return $bill;
}
Log::debug('Found nothing.');
}
// 2: find by name
2017-11-15 05:25:49 -06:00
if (3 === count($this->name)) {
2017-06-21 13:04:35 -05:00
/** @var Collection $bills */
$bills = $this->repository->getBills();
$name = $this->name['value'];
Log::debug(sprintf('Finding bill with name %s', $name));
$filtered = $bills->filter(
function (Bill $bill) use ($name) {
if ($bill->name === $name) {
Log::debug(sprintf('Found unmapped bill by name (#%d): %s', $bill->id, $bill->name));
return $bill;
}
return null;
}
);
2017-11-15 05:25:49 -06:00
if (1 === $filtered->count()) {
2017-06-21 13:04:35 -05:00
return $filtered->first();
}
Log::debug('Found nothing.');
}
// 4: do not search by account number.
Log::debug('Found NO existing bills.');
return new Bill;
}
/**
* @return Bill
*/
private function findMappedObject(): Bill
{
Log::debug('In findMappedObject() for Bill');
$fields = ['id', 'name'];
foreach ($fields as $field) {
$array = $this->$field;
Log::debug(sprintf('Find mapped bill based on field "%s" with value', $field), $array);
// check if a pre-mapped object exists.
$mapped = $this->getMappedObject($array);
2017-11-15 05:25:49 -06:00
if (null !== $mapped->id) {
2017-06-21 13:04:35 -05:00
Log::debug(sprintf('Found bill #%d!', $mapped->id));
return $mapped;
}
}
Log::debug('Found no bill on mapped data or no map present.');
return new Bill;
}
/**
* @param array $array
*
* @return Bill
*/
private function getMappedObject(array $array): Bill
{
Log::debug('In getMappedObject() for Bill');
2017-11-15 05:25:49 -06:00
if (0 === count($array)) {
2017-06-21 13:04:35 -05:00
Log::debug('Array is empty, nothing will come of this.');
return new Bill;
}
2017-11-15 05:25:49 -06:00
if (array_key_exists('mapped', $array) && null === $array['mapped']) {
2017-06-21 13:04:35 -05:00
Log::debug(sprintf('No map present for value "%s". Return NULL.', $array['value']));
return new Bill;
}
Log::debug('Finding a mapped bill based on', $array);
2017-07-23 01:32:51 -05:00
$search = intval($array['mapped']);
$bill = $this->repository->find($search);
2017-06-21 13:04:35 -05:00
2017-11-15 05:25:49 -06:00
if (null === $bill->id) {
2017-07-10 12:52:31 -05:00
Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search));
2017-06-21 13:04:35 -05:00
2017-07-10 12:52:31 -05:00
return new Bill;
}
Log::debug(sprintf('Found bill! #%d ("%s"). Return it', $bill->id, $bill->name));
return $bill;
2017-06-21 13:04:35 -05:00
}
/**
* @return bool
*/
private function store(): bool
{
// 1: find mapped object:
$mapped = $this->findMappedObject();
2017-11-15 05:25:49 -06:00
if (null !== $mapped->id) {
2017-06-21 13:04:35 -05:00
$this->bill = $mapped;
return true;
}
// 2: find existing by given values:
$found = $this->findExistingObject();
2017-11-15 05:25:49 -06:00
if (null !== $found->id) {
2017-06-21 13:04:35 -05:00
$this->bill = $found;
return true;
}
$name = $this->name['value'] ?? '';
2017-11-15 05:25:49 -06:00
if (0 === strlen($name)) {
2017-06-21 13:04:35 -05:00
return true;
}
$data = [
2017-09-14 23:26:09 -05:00
'name' => $name,
'match' => $name,
'amount_min' => bcmul($this->amount, '0.9'),
'amount_max' => bcmul($this->amount, '1.1'),
'user_id' => $this->user->id,
'date' => date('Y-m-d'),
'repeat_freq' => 'monthly',
'skip' => '0',
'automatch' => '0',
'active' => '1',
2017-06-21 13:04:35 -05:00
];
2017-09-14 23:26:09 -05:00
Log::debug('Found no bill so must create one ourselves. Assume default values.', $data);
2017-06-21 13:04:35 -05:00
$this->bill = $this->repository->store($data);
Log::debug(sprintf('Successfully stored new bill #%d: %s', $this->bill->id, $this->bill->name));
return true;
}
2017-07-07 01:09:42 -05:00
}