mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-08 06:24:30 -04:00
Improve expense import
This commit is contained in:
parent
d0b06b94da
commit
48d0b1b9cf
@ -54,7 +54,7 @@ class Expense extends EntityModel
|
||||
'vendor',
|
||||
'amount',
|
||||
'public_notes',
|
||||
'category',
|
||||
'expense_category',
|
||||
'expense_date',
|
||||
];
|
||||
}
|
||||
@ -63,7 +63,7 @@ class Expense extends EntityModel
|
||||
{
|
||||
return [
|
||||
'amount|total' => 'amount',
|
||||
'category' => 'category',
|
||||
'category' => 'expense_category',
|
||||
'client' => 'client',
|
||||
'vendor' => 'vendor',
|
||||
'notes|details' => 'public_notes',
|
||||
|
@ -188,4 +188,15 @@ class BaseTransformer extends TransformerAbstract
|
||||
return isset($this->maps[ENTITY_VENDOR][$name]) ? $this->maps[ENTITY_VENDOR][$name] : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return null
|
||||
*/
|
||||
public function getExpenseCategoryId($name)
|
||||
{
|
||||
$name = strtolower($name);
|
||||
return isset($this->maps[ENTITY_EXPENSE_CATEGORY][$name]) ? $this->maps[ENTITY_EXPENSE_CATEGORY][$name] : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,32 +18,11 @@ class ExpenseTransformer extends BaseTransformer
|
||||
return [
|
||||
'amount' => isset($data->amount) ? (float) $data->amount : null,
|
||||
'vendor_id' => isset($data->vendor) ? $this->getVendorId($data->vendor) : null,
|
||||
'client_id' => isset($data->client) ? $this->getClientId($data->client) : null,
|
||||
'expense_date' => isset($data->expense_date) ? date('Y-m-d', strtotime($data->expense_date)) : null,
|
||||
'public_notes' => $this->getString($data, 'public_notes'),
|
||||
'expense_category_id' => isset($data->expense_category) ? $this->getExpenseCategoryId($data->expense_category) : null,
|
||||
];
|
||||
});
|
||||
|
||||
/*
|
||||
return new Item($data, function ($data) {
|
||||
return [
|
||||
'client_id' => $this->getClientId($data->name),
|
||||
'invoice_number' => isset($data->invoice_number) ? $this->getInvoiceNumber($data->invoice_number) : null,
|
||||
'paid' => isset($data->paid) ? (float) $data->paid : null,
|
||||
'po_number' => $this->getString($data, 'po_number'),
|
||||
'terms' => $this->getString($data, 'terms'),
|
||||
'public_notes' => $this->getString($data, 'public_notes'),
|
||||
'invoice_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null,
|
||||
'invoice_items' => [
|
||||
[
|
||||
'product_key' => '',
|
||||
'notes' => $this->getString($data, 'notes'),
|
||||
'cost' => isset($data->amount) ? (float) $data->amount : null,
|
||||
'qty' => 1,
|
||||
]
|
||||
],
|
||||
];
|
||||
});
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,11 @@ class ExpenseCategoryRepository extends BaseRepository
|
||||
return 'App\Models\ExpenseCategory';
|
||||
}
|
||||
|
||||
public function all()
|
||||
{
|
||||
return ExpenseCategory::scope()->get();
|
||||
}
|
||||
|
||||
public function find($filter = null)
|
||||
{
|
||||
$query = DB::table('expense_categories')
|
||||
|
@ -17,10 +17,12 @@ use App\Ninja\Repositories\PaymentRepository;
|
||||
use App\Ninja\Repositories\ProductRepository;
|
||||
use App\Ninja\Repositories\ExpenseRepository;
|
||||
use App\Ninja\Repositories\VendorRepository;
|
||||
use App\Ninja\Repositories\ExpenseCategoryRepository;
|
||||
use App\Ninja\Serializers\ArraySerializer;
|
||||
use App\Models\Client;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Expense;
|
||||
use App\Models\ExpenseCategory;
|
||||
use App\Models\EntityModel;
|
||||
|
||||
/**
|
||||
@ -115,7 +117,8 @@ class ImportService
|
||||
ContactRepository $contactRepo,
|
||||
ProductRepository $productRepo,
|
||||
ExpenseRepository $expenseRepo,
|
||||
VendorRepository $vendorRepo
|
||||
VendorRepository $vendorRepo,
|
||||
ExpenseCategoryRepository $expenseCategoryRepo
|
||||
)
|
||||
{
|
||||
$this->fractal = $manager;
|
||||
@ -128,6 +131,7 @@ class ImportService
|
||||
$this->productRepo = $productRepo;
|
||||
$this->expenseRepo = $expenseRepo;
|
||||
$this->vendorRepo = $vendorRepo;
|
||||
$this->expenseCategoryRepo = $expenseCategoryRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -271,6 +275,18 @@ class ImportService
|
||||
private function transformRow($source, $entityType, $row)
|
||||
{
|
||||
$transformer = $this->getTransformer($source, $entityType, $this->maps);
|
||||
|
||||
// Create expesnse category
|
||||
if ($entityType == ENTITY_EXPENSE) {
|
||||
if ( ! empty($row->expense_category)) {
|
||||
$categoryId = $transformer->getExpenseCategoryId($row->expense_category);
|
||||
if ( ! $categoryId) {
|
||||
$category = $this->expenseCategoryRepo->save(['name' => $row->expense_category]);
|
||||
$this->addExpenseCategoryToMaps($category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$resource = $transformer->transform($row);
|
||||
|
||||
if (!$resource) {
|
||||
@ -708,6 +724,11 @@ class ImportService
|
||||
foreach ($vendors as $vendor) {
|
||||
$this->maps['vendor'][strtolower($vendor->name)] = $vendor->id;
|
||||
}
|
||||
|
||||
$expenseCaegories = $this->expenseCategoryRepo->all();
|
||||
foreach ($expenseCaegories as $category) {
|
||||
$this->addExpenseCategoryToMaps($category);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -747,4 +768,11 @@ class ImportService
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
private function addExpenseCategoryToMaps(ExpenseCategory $category)
|
||||
{
|
||||
if ($name = strtolower($category->name)) {
|
||||
$this->maps['expense_category'][$name] = $category->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2147,6 +2147,7 @@ $LANG = array(
|
||||
'created_expenses' => 'Successfully created :count expense(s)',
|
||||
|
||||
'translate_app' => 'Help improve our translations with :link',
|
||||
'expense_category' => 'Expense Category',
|
||||
|
||||
);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user