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', '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() public function getEntityType()
{ {
return ENTITY_PRODUCT; return ENTITY_PRODUCT;

View File

@ -15,21 +15,38 @@ class BaseTransformer extends TransformerAbstract
protected function hasClient($name) protected function hasClient($name)
{ {
$name = strtolower($name); $name = trim(strtolower($name));
return isset($this->maps[ENTITY_CLIENT][$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) protected function getString($data, $field)
{ {
return (isset($data->$field) && $data->$field) ? $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) protected function getClientId($name)
{ {
$name = strtolower($name); $name = strtolower($name);
return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null; 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) protected function getCountryId($name)
{ {
$name = strtolower($name); $name = strtolower($name);
@ -53,7 +70,7 @@ class BaseTransformer extends TransformerAbstract
if ( ! $date instanceof DateTime) { if ( ! $date instanceof DateTime) {
$date = DateTime::createFromFormat($format, $date); $date = DateTime::createFromFormat($format, $date);
} }
return $date ? $date->format('Y-m-d') : null; return $date ? $date->format('Y-m-d') : null;
} }
@ -87,11 +104,11 @@ class BaseTransformer extends TransformerAbstract
return isset($this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber])? $this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber] : null; return isset($this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber])? $this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber] : null;
} }
protected function getVendorId($name) protected function getVendorId($name)
{ {
$name = strtolower($name); $name = strtolower($name);
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;
} }
} }

View File

@ -11,6 +11,13 @@ class ProductRepository extends BaseRepository
return 'App\Models\Product'; return 'App\Models\Product';
} }
public function all()
{
return Product::scope()
->withTrashed()
->get();
}
public function find($accountId) public function find($accountId)
{ {
return DB::table('products') return DB::table('products')
@ -30,11 +37,11 @@ class ProductRepository extends BaseRepository
'products.deleted_at' 'products.deleted_at'
); );
} }
public function save($data, $product = null) public function save($data, $product = null)
{ {
$publicId = isset($data['public_id']) ? $data['public_id'] : false; $publicId = isset($data['public_id']) ? $data['public_id'] : false;
if ($product) { if ($product) {
// do nothing // do nothing
} elseif ($publicId) { } elseif ($publicId) {
@ -50,4 +57,4 @@ class ProductRepository extends BaseRepository
return $product; return $product;
} }
} }

View File

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

View File

@ -1314,6 +1314,13 @@ $LANG = array(
'wait_for_upload' => 'Please wait for the document upload to complete.', 'wait_for_upload' => 'Please wait for the document upload to complete.',
'upgrade_for_permissions' => 'Upgrade to our Enterprise plan to enable permissions.', 'upgrade_for_permissions' => 'Upgrade to our Enterprise plan to enable permissions.',
'enable_second_tax_rate' => 'Enable specifying a <b>second tax rate</b>', '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 @parent
<style type="text/css"> <style type="text/css">
.contact-file, .import-file {
.task-file,
.payment-file {
display: none; display: none;
} }
</style> </style>
@ -34,7 +32,7 @@
@foreach (\App\Services\ImportService::$entityTypes as $entityType) @foreach (\App\Services\ImportService::$entityTypes as $entityType)
{!! Former::file("{$entityType}_file") {!! Former::file("{$entityType}_file")
->addGroupClass("{$entityType}-file") !!} ->addGroupClass("import-file {$entityType}-file") !!}
@endforeach @endforeach
{!! Former::actions( Button::info(trans('texts.upload'))->submit()->large()->appendIcon(Icon::create('open'))) !!} {!! Former::actions( Button::info(trans('texts.upload'))->submit()->large()->appendIcon(Icon::create('open'))) !!}
@ -67,13 +65,17 @@
trans('texts.payments') => array('name' => ENTITY_PAYMENT, 'value' => 1), trans('texts.payments') => array('name' => ENTITY_PAYMENT, 'value' => 1),
])->check(ENTITY_CLIENT)->check(ENTITY_TASK)->check(ENTITY_INVOICE)->check(ENTITY_PAYMENT) !!} ])->check(ENTITY_CLIENT)->check(ENTITY_TASK)->check(ENTITY_INVOICE)->check(ENTITY_PAYMENT) !!}
{!! Former::actions( Button::primary(trans('texts.download'))->submit()->large()->appendIcon(Icon::create('download-alt'))) !!} {!! Former::actions( Button::primary(trans('texts.download'))->submit()->large()->appendIcon(Icon::create('download-alt'))) !!}
</div> </div>
</div> </div>
{!! Former::close() !!} {!! Former::close() !!}
<script type="text/javascript"> <script type="text/javascript">
$(function() {
setFileTypesVisible();
});
function setEntityTypesVisible() { function setEntityTypesVisible() {
var selector = '.entity-types input[type=checkbox]'; var selector = '.entity-types input[type=checkbox]';
if ($('#format').val() === 'JSON') { if ($('#format').val() === 'JSON') {
@ -103,4 +105,4 @@
</script> </script>
@stop @stop

View File

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