firefly-iii/app/Import/Converter/BillName.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2016-07-16 01:25:39 -05:00
<?php
/**
* BillName.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Import\Converter;
use FireflyIII\Exceptions\FireflyException;
2016-07-23 14:37:06 -05:00
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Log;
2016-07-16 01:25:39 -05:00
/**
* Class BillName
*
* @package FireflyIII\Import\Converter
*/
class BillName extends BasicConverter implements ConverterInterface
{
/**
* @param $value
*
* @return Bill
2016-07-16 01:25:39 -05:00
* @throws FireflyException
*/
public function convert($value)
{
2016-07-23 14:37:06 -05:00
$value = trim($value);
Log::debug('Going to convert using BillName', ['value' => $value]);
if (strlen($value) === 0) {
$this->setCertainty(0);
2016-08-26 01:21:31 -05:00
2016-07-23 14:37:06 -05:00
return new Bill;
}
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class, [$this->user]);
if (isset($this->mapping[$value])) {
Log::debug('Found bill in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]);
$bill = $repository->find(intval($this->mapping[$value]));
if (!is_null($bill->id)) {
Log::debug('Found bill by ID', ['id' => $bill->id]);
$this->setCertainty(100);
2016-08-26 01:21:31 -05:00
2016-07-23 14:37:06 -05:00
return $bill;
}
}
// not mapped? Still try to find it first:
$bill = $repository->findByName($value);
if (!is_null($bill->id)) {
Log::debug('Found bill by name ', ['id' => $bill->id]);
$this->setCertainty(100);
2016-08-26 01:21:31 -05:00
2016-07-23 14:37:06 -05:00
return $bill;
}
// create new bill. Use a lot of made up values.
$bill = $repository->store(
[
'name' => $value,
'match' => $value,
'amount_min' => 1,
2016-08-26 01:21:31 -05:00
'user' => $this->user->id,
2016-07-23 14:37:06 -05:00
'amount_max' => 10,
'date' => date('Ymd'),
'repeat_freq' => 'monthly',
'skip' => 0,
'automatch' => 0,
'active' => 1,
]
);
2016-08-11 11:44:11 -05:00
if (is_null($bill->id)) {
$this->setCertainty(0);
Log::info('Could not store new bill by name', $bill->getErrors()->toArray());
return new Bill;
}
$this->setCertainty(100);
2016-07-23 14:37:06 -05:00
return $bill;
2016-07-16 01:25:39 -05:00
}
2016-08-12 08:10:03 -05:00
}