Enabled importing products

This commit is contained in:
Hillel Coren 2016-05-31 23:15:55 +03:00
parent c122987bc7
commit 42ac9507e5
7 changed files with 105 additions and 40 deletions

View File

@ -16,6 +16,24 @@ class Product extends EntityModel
'default_tax_rate_id',
];
public static function getImportColumns()
{
return [
'product_key',
'notes',
'cost',
];
}
public static function getImportMap()
{
return [
'product|item' => 'product_key',
'notes|description|details' => 'notes',
'cost|amount|price' => 'cost',
];
}
public function getEntityType()
{
return ENTITY_PRODUCT;

View File

@ -15,21 +15,38 @@ class BaseTransformer extends TransformerAbstract
protected function hasClient($name)
{
$name = strtolower($name);
$name = trim(strtolower($name));
return isset($this->maps[ENTITY_CLIENT][$name]);
}
protected function hasProduct($key)
{
$key = trim(strtolower($key));
return isset($this->maps[ENTITY_PRODUCT][$key]);
}
protected function getString($data, $field)
{
return (isset($data->$field) && $data->$field) ? $data->$field : '';
}
protected function getNumber($data, $field)
{
return (isset($data->$field) && $data->$field) ? $data->$field : 0;
}
protected function getClientId($name)
{
$name = strtolower($name);
return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null;
}
protected function getProductId($name)
{
$name = strtolower($name);
return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null;
}
protected function getCountryId($name)
{
$name = strtolower($name);

View File

@ -11,6 +11,13 @@ class ProductRepository extends BaseRepository
return 'App\Models\Product';
}
public function all()
{
return Product::scope()
->withTrashed()
->get();
}
public function find($accountId)
{
return DB::table('products')

View File

@ -13,6 +13,7 @@ use App\Ninja\Repositories\ContactRepository;
use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Repositories\ProductRepository;
use App\Ninja\Serializers\ArraySerializer;
use App\Models\Client;
use App\Models\Invoice;
@ -23,6 +24,7 @@ class ImportService
protected $invoiceRepo;
protected $clientRepo;
protected $contactRepo;
protected $productRepo;
protected $processedRows = array();
public static $entityTypes = [
@ -31,6 +33,8 @@ class ImportService
ENTITY_INVOICE,
ENTITY_PAYMENT,
ENTITY_TASK,
ENTITY_PRODUCT,
ENTITY_EXPENSE,
];
public static $sources = [
@ -45,7 +49,14 @@ class ImportService
IMPORT_ZOHO,
];
public function __construct(Manager $manager, ClientRepository $clientRepo, InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ContactRepository $contactRepo)
public function __construct(
Manager $manager,
ClientRepository $clientRepo,
InvoiceRepository $invoiceRepo,
PaymentRepository $paymentRepo,
ContactRepository $contactRepo,
ProductRepository $productRepo
)
{
$this->fractal = $manager;
$this->fractal->setSerializer(new ArraySerializer());
@ -54,6 +65,7 @@ class ImportService
$this->invoiceRepo = $invoiceRepo;
$this->paymentRepo = $paymentRepo;
$this->contactRepo = $contactRepo;
$this->productRepo = $productRepo;
}
public function import($source, $files)
@ -216,8 +228,11 @@ class ImportService
'invoice_number' => 'required|unique:invoices,invoice_number,,id,account_id,'.Auth::user()->account_id,
'discount' => 'positive',
];
} else {
return true;
}
if ($entityType === ENTITY_PRODUCT) {
$rules = [
'product_key' => 'required',
];
}
$validator = Validator::make($data, $rules);
@ -251,6 +266,14 @@ class ImportService
}
}
$productMap = [];
$products = $this->productRepo->all();
foreach ($products as $product) {
if ($key = strtolower(trim($product->product_key))) {
$productMap[$key] = $product->id;
}
}
$countryMap = [];
$countryMap2 = [];
$countries = Cache::get('countries');
@ -269,6 +292,7 @@ class ImportService
ENTITY_CLIENT => $clientMap,
ENTITY_INVOICE => $invoiceMap,
ENTITY_INVOICE.'_'.ENTITY_CLIENT => $invoiceClientMap,
ENTITY_PRODUCT => $productMap,
'countries' => $countryMap,
'countries2' => $countryMap2,
'currencies' => $currencyMap,
@ -280,13 +304,9 @@ class ImportService
$data = [];
foreach ($files as $entityType => $filename) {
if ($entityType === ENTITY_CLIENT) {
$columns = Client::getImportColumns();
$map = Client::getImportMap();
} else {
$columns = Invoice::getImportColumns();
$map = Invoice::getImportMap();
}
$class = "App\\Models\\" . ucwords($entityType);
$columns = $class::getImportColumns();
$map = $class::getImportMap();
// Lookup field translations
foreach ($columns as $key => $value) {
@ -452,12 +472,8 @@ class ImportService
private function convertToObject($entityType, $data, $map)
{
$obj = new stdClass();
if ($entityType === ENTITY_CLIENT) {
$columns = Client::getImportColumns();
} else {
$columns = Invoice::getImportColumns();
}
$class = "App\\Models\\" . ucwords($entityType);
$columns = $class::getImportColumns();
foreach ($columns as $column) {
$obj->$column = false;

View File

@ -1314,6 +1314,13 @@ $LANG = array(
'wait_for_upload' => 'Please wait for the document upload to complete.',
'upgrade_for_permissions' => 'Upgrade to our Enterprise plan to enable permissions.',
'enable_second_tax_rate' => 'Enable specifying a <b>second tax rate</b>',
'payment_file' => 'Payment File',
'expense_file' => 'Expense File',
'product_file' => 'Product File',
'import_products' => 'Import Products',
'products_will_create' => 'products will be created.',
'product_key' => 'Product',
'created_products' => 'Successfully created :count product(s)',
);

View File

@ -4,9 +4,7 @@
@parent
<style type="text/css">
.contact-file,
.task-file,
.payment-file {
.import-file {
display: none;
}
</style>
@ -34,7 +32,7 @@
@foreach (\App\Services\ImportService::$entityTypes as $entityType)
{!! Former::file("{$entityType}_file")
->addGroupClass("{$entityType}-file") !!}
->addGroupClass("import-file {$entityType}-file") !!}
@endforeach
{!! Former::actions( Button::info(trans('texts.upload'))->submit()->large()->appendIcon(Icon::create('open'))) !!}
@ -74,6 +72,10 @@
<script type="text/javascript">
$(function() {
setFileTypesVisible();
});
function setEntityTypesVisible() {
var selector = '.entity-types input[type=checkbox]';
if ($('#format').val() === 'JSON') {

View File

@ -7,13 +7,11 @@
{!! Former::open('/import_csv')->addClass('warn-on-exit') !!}
@if (isset($data[ENTITY_CLIENT]))
@include('accounts.partials.map', $data[ENTITY_CLIENT])
@endif
@if (isset($data[ENTITY_INVOICE]))
@include('accounts.partials.map', $data[ENTITY_INVOICE])
@endif
@foreach (App\Services\ImportService::$entityTypes as $entityType)
@if (isset($data[$entityType]))
@include('accounts.partials.map', $data[$entityType])
@endif
@endforeach
{!! Former::actions(
Button::normal(trans('texts.cancel'))->large()->asLinkTo(URL::to('/settings/import_export'))->appendIcon(Icon::create('remove-circle')),