Improve expense import

This commit is contained in:
Hillel Coren 2016-10-05 23:46:15 +03:00
parent d0b06b94da
commit 48d0b1b9cf
6 changed files with 50 additions and 26 deletions

View File

@ -54,7 +54,7 @@ class Expense extends EntityModel
'vendor', 'vendor',
'amount', 'amount',
'public_notes', 'public_notes',
'category', 'expense_category',
'expense_date', 'expense_date',
]; ];
} }
@ -63,7 +63,7 @@ class Expense extends EntityModel
{ {
return [ return [
'amount|total' => 'amount', 'amount|total' => 'amount',
'category' => 'category', 'category' => 'expense_category',
'client' => 'client', 'client' => 'client',
'vendor' => 'vendor', 'vendor' => 'vendor',
'notes|details' => 'public_notes', 'notes|details' => 'public_notes',

View File

@ -188,4 +188,15 @@ class BaseTransformer extends TransformerAbstract
return isset($this->maps[ENTITY_VENDOR][$name]) ? $this->maps[ENTITY_VENDOR][$name] : null; 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;
}
} }

View File

@ -18,32 +18,11 @@ class ExpenseTransformer extends BaseTransformer
return [ return [
'amount' => isset($data->amount) ? (float) $data->amount : null, 'amount' => isset($data->amount) ? (float) $data->amount : null,
'vendor_id' => isset($data->vendor) ? $this->getVendorId($data->vendor) : 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, 'expense_date' => isset($data->expense_date) ? date('Y-m-d', strtotime($data->expense_date)) : null,
'public_notes' => $this->getString($data, 'public_notes'), '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,
]
],
];
});
*/
} }
} }

View File

@ -12,6 +12,11 @@ class ExpenseCategoryRepository extends BaseRepository
return 'App\Models\ExpenseCategory'; return 'App\Models\ExpenseCategory';
} }
public function all()
{
return ExpenseCategory::scope()->get();
}
public function find($filter = null) public function find($filter = null)
{ {
$query = DB::table('expense_categories') $query = DB::table('expense_categories')

View File

@ -17,10 +17,12 @@ use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Repositories\ProductRepository; use App\Ninja\Repositories\ProductRepository;
use App\Ninja\Repositories\ExpenseRepository; use App\Ninja\Repositories\ExpenseRepository;
use App\Ninja\Repositories\VendorRepository; use App\Ninja\Repositories\VendorRepository;
use App\Ninja\Repositories\ExpenseCategoryRepository;
use App\Ninja\Serializers\ArraySerializer; use App\Ninja\Serializers\ArraySerializer;
use App\Models\Client; use App\Models\Client;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Expense; use App\Models\Expense;
use App\Models\ExpenseCategory;
use App\Models\EntityModel; use App\Models\EntityModel;
/** /**
@ -115,7 +117,8 @@ class ImportService
ContactRepository $contactRepo, ContactRepository $contactRepo,
ProductRepository $productRepo, ProductRepository $productRepo,
ExpenseRepository $expenseRepo, ExpenseRepository $expenseRepo,
VendorRepository $vendorRepo VendorRepository $vendorRepo,
ExpenseCategoryRepository $expenseCategoryRepo
) )
{ {
$this->fractal = $manager; $this->fractal = $manager;
@ -128,6 +131,7 @@ class ImportService
$this->productRepo = $productRepo; $this->productRepo = $productRepo;
$this->expenseRepo = $expenseRepo; $this->expenseRepo = $expenseRepo;
$this->vendorRepo = $vendorRepo; $this->vendorRepo = $vendorRepo;
$this->expenseCategoryRepo = $expenseCategoryRepo;
} }
/** /**
@ -271,6 +275,18 @@ class ImportService
private function transformRow($source, $entityType, $row) private function transformRow($source, $entityType, $row)
{ {
$transformer = $this->getTransformer($source, $entityType, $this->maps); $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); $resource = $transformer->transform($row);
if (!$resource) { if (!$resource) {
@ -708,6 +724,11 @@ class ImportService
foreach ($vendors as $vendor) { foreach ($vendors as $vendor) {
$this->maps['vendor'][strtolower($vendor->name)] = $vendor->id; $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 // do nothing
} }
private function addExpenseCategoryToMaps(ExpenseCategory $category)
{
if ($name = strtolower($category->name)) {
$this->maps['expense_category'][$name] = $category->id;
}
}
} }

View File

@ -2147,6 +2147,7 @@ $LANG = array(
'created_expenses' => 'Successfully created :count expense(s)', 'created_expenses' => 'Successfully created :count expense(s)',
'translate_app' => 'Help improve our translations with :link', 'translate_app' => 'Help improve our translations with :link',
'expense_category' => 'Expense Category',
); );