Working on importing expenses

This commit is contained in:
Hillel Coren 2016-10-02 10:10:28 +03:00
parent 47137e60df
commit 7bd4026e4c
4 changed files with 102 additions and 8 deletions

View File

@ -31,6 +31,7 @@ class Expense extends EntityModel
'client_id', 'client_id',
'vendor_id', 'vendor_id',
'expense_currency_id', 'expense_currency_id',
'expense_date',
'invoice_currency_id', 'invoice_currency_id',
'amount', 'amount',
'foreign_amount', 'foreign_amount',
@ -46,6 +47,29 @@ class Expense extends EntityModel
'tax_name2', 'tax_name2',
]; ];
public static function getImportColumns()
{
return [
'client',
'vendor',
'amount',
'public_notes',
'category',
'expense_date',
];
}
public static function getImportMap()
{
return [
'amount|total' => 'amount',
'category' => 'category',
'client' => 'client',
'vendor' => 'vendor',
'notes|details' => 'public_notes',
'date' => 'expense_date',
];
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */

View File

@ -0,0 +1,49 @@
<?php namespace App\Ninja\Import\CSV;
use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item;
/**
* Class InvoiceTransformer
*/
class ExpenseTransformer extends BaseTransformer
{
/**
* @param $data
* @return bool|Item
*/
public function transform($data)
{
return new Item($data, function ($data) {
return [
'amount' => isset($data->amount) ? (float) $data->amount : null,
'vendor_id' => isset($data->vendor) ? $this->getVendorId($data->vendor) : null,
'expense_date' => isset($data->expense_date) ? date('Y-m-d', strtotime($data->expense_date)) : null,
'public_notes' => $this->getString($data, 'public_notes'),
];
});
/*
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

@ -15,9 +15,12 @@ use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Repositories\ProductRepository; use App\Ninja\Repositories\ProductRepository;
use App\Ninja\Repositories\ExpenseRepository;
use App\Ninja\Repositories\VendorRepository;
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\EntityModel; use App\Models\EntityModel;
/** /**
@ -110,7 +113,9 @@ class ImportService
InvoiceRepository $invoiceRepo, InvoiceRepository $invoiceRepo,
PaymentRepository $paymentRepo, PaymentRepository $paymentRepo,
ContactRepository $contactRepo, ContactRepository $contactRepo,
ProductRepository $productRepo ProductRepository $productRepo,
ExpenseRepository $expenseRepo,
VendorRepository $vendorRepo
) )
{ {
$this->fractal = $manager; $this->fractal = $manager;
@ -121,6 +126,8 @@ class ImportService
$this->paymentRepo = $paymentRepo; $this->paymentRepo = $paymentRepo;
$this->contactRepo = $contactRepo; $this->contactRepo = $contactRepo;
$this->productRepo = $productRepo; $this->productRepo = $productRepo;
$this->expenseRepo = $expenseRepo;
$this->vendorRepo = $vendorRepo;
} }
/** /**
@ -462,12 +469,11 @@ class ImportService
$title = strtolower($headers[$i]); $title = strtolower($headers[$i]);
$mapped[$i] = ''; $mapped[$i] = '';
if ($hasHeaders) { foreach ($map as $search => $column) {
foreach ($map as $search => $column) { if ($this->checkForMatch($title, $search)) {
if ($this->checkForMatch($title, $search)) { $hasHeaders = true;
$mapped[$i] = $column; $mapped[$i] = $column;
break; break;
}
} }
} }
} }
@ -668,6 +674,8 @@ class ImportService
'currencies' => [], 'currencies' => [],
'client_ids' => [], 'client_ids' => [],
'invoice_ids' => [], 'invoice_ids' => [],
'vendors' => [],
'expense_categories' => [],
]; ];
$clients = $this->clientRepo->all(); $clients = $this->clientRepo->all();
@ -695,6 +703,11 @@ class ImportService
foreach ($currencies as $currency) { foreach ($currencies as $currency) {
$this->maps['currencies'][strtolower($currency->code)] = $currency->id; $this->maps['currencies'][strtolower($currency->code)] = $currency->id;
} }
$vendors = $this->vendorRepo->all();
foreach ($vendors as $vendor) {
$this->maps['vendor'][strtolower($vendor->name)] = $vendor->id;
}
} }
/** /**
@ -729,4 +742,9 @@ class ImportService
$this->maps['product'][$key] = $product->id; $this->maps['product'][$key] = $product->id;
} }
} }
private function addExpenseToMaps(Expense $expense)
{
// do nothing
}
} }

View File

@ -2142,7 +2142,10 @@ $LANG = array(
'enable_recurring' => 'Enable Recurring', 'enable_recurring' => 'Enable Recurring',
'disable_recurring' => 'Disable Recurring', 'disable_recurring' => 'Disable Recurring',
'text' => 'Text', 'text' => 'Text',
'expense_will_create' => 'expense will be created',
'expenses_will_create' => 'expenses will be created',
'created_expenses' => 'Successfully created :count expense(s)',
); );
return $LANG; return $LANG;