Support importing/exporting expense payment fields

This commit is contained in:
Hillel Coren 2018-04-15 13:02:40 +03:00
parent 579f078f57
commit 36f2ddb3f5
5 changed files with 41 additions and 2 deletions

View File

@ -489,6 +489,21 @@ class Utils
return intval($value);
}
public static function lookupIdInCache($name, $type)
{
$cache = Cache::get($type);
$data = $cache->filter(function ($item) use ($name) {
return strtolower($item->name) == trim(strtolower($name));
});
if ($record = $data->first()) {
return $record->id;
} else {
return null;
}
}
public static function getFromCache($id, $type)
{
$cache = Cache::get($type);

View File

@ -66,6 +66,9 @@ class Expense extends EntityModel
'private_notes',
'expense_category',
'expense_date',
'payment_type',
'payment_date',
'transaction_reference',
];
}
@ -78,7 +81,10 @@ class Expense extends EntityModel
'vendor' => 'vendor',
'notes|details^private' => 'public_notes',
'notes|details^public' => 'private_notes',
'date' => 'expense_date',
'date^payment' => 'expense_date',
'payment type' => 'payment_type',
'payment date' => 'payment_date',
'reference' => 'transaction_reference',
];
}

View File

@ -2,6 +2,7 @@
namespace App\Ninja\Import\CSV;
use Utils;
use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item;
@ -18,14 +19,20 @@ class ExpenseTransformer extends BaseTransformer
public function transform($data)
{
return new Item($data, function ($data) {
$clientId = isset($data->client) ? $this->getClientId($data->client) : null;
return [
'amount' => $this->getFloat($data, 'amount'),
'vendor_id' => isset($data->vendor) ? $this->getVendorId($data->vendor) : null,
'client_id' => isset($data->client) ? $this->getClientId($data->client) : null,
'client_id' => $clientId,
'expense_date' => isset($data->expense_date) ? date('Y-m-d', strtotime($data->expense_date)) : null,
'public_notes' => $this->getString($data, 'public_notes'),
'private_notes' => $this->getString($data, 'private_notes'),
'expense_category_id' => isset($data->expense_category) ? $this->getExpenseCategoryId($data->expense_category) : null,
'payment_type_id' => isset($data->payment_type) ? Utils::lookupIdInCache($data->payment_type, 'paymentTypes') : null,
'payment_date' => isset($data->payment_date) ? date('Y-m-d', strtotime($data->payment_date)) : null,
'transaction_reference' => $this->getString($data, 'transaction_reference'),
'should_be_invoiced' => $clientId ? true : false,
];
});
}

View File

@ -59,6 +59,15 @@ class ExpensePresenter extends EntityPresenter
return $this->entity->expense_category ? $this->entity->expense_category->name : '';
}
public function payment_type()
{
if (! $this->payment_type_id) {
return '';
}
return Utils::getFromCache($this->payment_type_id, 'paymentTypes')->name;
}
public function calendarEvent($subColors = false)
{
$data = parent::calendarEvent();

View File

@ -12,6 +12,7 @@
<td>{{ trans('texts.status') }}</td>
<td>{{ trans('texts.public_notes') }}</td>
<td>{{ trans('texts.private_notes') }}</td>
<td>{{ trans('texts.payment_type') }}</td>
<td>{{ trans('texts.payment_date') }}</td>
<td>{{ trans('texts.transaction_reference') }}</td>
</tr>
@ -31,6 +32,7 @@
<td>{{ $expense->statusLabel() }}</td>
<td>{{ $expense->public_notes }}</td>
<td>{{ $expense->private_notes }}</td>
<td>{{ $expense->present()->payment_type }}</td>
<td>{{ $expense->present()->payment_date }}</td>
<td>{{ $expense->transaction_reference }}</td>
</tr>