Support updating products when importing

This commit is contained in:
Hillel Coren 2017-08-03 22:31:22 +03:00
parent 0493848d7e
commit aa7448f0b4
6 changed files with 14 additions and 32 deletions

View File

@ -101,35 +101,17 @@ class BaseTransformer extends TransformerAbstract
*
* @return null
*/
public function getProductId($name)
public function getProduct($data, $key, $field, $default = false)
{
$name = strtolower(trim($name));
$productKey = trim(strtolower($data->$key));
return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null;
}
if (! isset($this->maps['product'][$productKey])) {
return $default;
}
/**
* @param $name
*
* @return null
*/
public function getProductNotes($name)
{
$name = strtolower(trim($name));
$product = $this->maps['product'][$productKey];
return isset($this->maps['product_notes'][$name]) ? $this->maps['product_notes'][$name] : null;
}
/**
* @param $name
*
* @return null
*/
public function getProductCost($name)
{
$name = strtolower(trim($name));
return isset($this->maps['product_cost'][$name]) ? $this->maps['product_cost'][$name] : null;
return $product->$field ?: $default;
}
/**

View File

@ -38,8 +38,8 @@ class InvoiceTransformer extends BaseTransformer
'invoice_items' => [
[
'product_key' => $this->getString($data, 'product'),
'notes' => $this->getString($data, 'notes') ?: $this->getProductNotes($this->getString($data, 'product')),
'cost' => $this->getFloat($data, 'amount') ?: $this->getProductCost($this->getString($data, 'product')),
'notes' => $this->getString($data, 'notes') ?: $this->getProduct($data, 'product', 'notes', ''),
'cost' => $this->getFloat($data, 'amount') ?: $this->getProduct($data, 'product', 'cost', 0),
'qty' => $this->getFloat($data, 'quantity') ?: 1,
'tax_name1' => $this->getTaxName($this->getString($data, 'tax1')),
'tax_rate1' => $this->getTaxRate($this->getString($data, 'tax1')),

View File

@ -23,6 +23,7 @@ class ProductTransformer extends BaseTransformer
return new Item($data, function ($data) {
return [
'public_id' => $this->getProduct($data, 'product_key', 'public_id'),
'product_key' => $this->getString($data, 'product_key'),
'notes' => $this->getString($data, 'notes'),
'cost' => $this->getFloat($data, 'cost'),

View File

@ -17,6 +17,7 @@ class ProductRepository extends BaseRepository
{
return Product::scope()
->withTrashed()
->where('is_deleted', '=', false)
->get();
}
@ -54,7 +55,7 @@ class ProductRepository extends BaseRepository
if ($product) {
// do nothing
} elseif ($publicId) {
$product = Product::scope($publicId)->firstOrFail();
$product = Product::scope($publicId)->withArchived()->firstOrFail();
\Log::warning('Entity not set in product repo save');
} else {
$product = Product::createNew();

View File

@ -933,9 +933,7 @@ class ImportService
private function addProductToMaps(Product $product)
{
if ($key = strtolower(trim($product->product_key))) {
$this->maps['product'][$key] = $product->id;
$this->maps['product_notes'][$key] = $product->notes;
$this->maps['product_cost'][$key] = $product->cost;
$this->maps['product'][$key] = $product;
}
}

View File

@ -1330,7 +1330,7 @@ $LANG = array(
'import_products' => 'Import Products',
'products_will_create' => 'products will be created',
'product_key' => 'Product',
'created_products' => 'Successfully created :count product(s)',
'created_products' => 'Successfully created/updated :count product(s)',
'export_help' => 'Use JSON if you plan to import the data into Invoice Ninja.<br/>The file includes clients, products, invoices, quotes and payments.',
'JSON_file' => 'JSON File',