diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index 9d6c95138fb3..f83112c6a7ce 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -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; } /** diff --git a/app/Ninja/Import/CSV/InvoiceTransformer.php b/app/Ninja/Import/CSV/InvoiceTransformer.php index 198b2ef139c2..6d8f2e414888 100644 --- a/app/Ninja/Import/CSV/InvoiceTransformer.php +++ b/app/Ninja/Import/CSV/InvoiceTransformer.php @@ -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')), diff --git a/app/Ninja/Import/CSV/ProductTransformer.php b/app/Ninja/Import/CSV/ProductTransformer.php index 8b372e28baa5..22e146e2c98a 100644 --- a/app/Ninja/Import/CSV/ProductTransformer.php +++ b/app/Ninja/Import/CSV/ProductTransformer.php @@ -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'), diff --git a/app/Ninja/Repositories/ProductRepository.php b/app/Ninja/Repositories/ProductRepository.php index 49659beddaea..78212cef65b5 100644 --- a/app/Ninja/Repositories/ProductRepository.php +++ b/app/Ninja/Repositories/ProductRepository.php @@ -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(); diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index cc089210b82e..1e604a5ede61 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -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; } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 9a34b368b0a7..6403e79a402e 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -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.
The file includes clients, products, invoices, quotes and payments.', 'JSON_file' => 'JSON File',