From 6e89fcd0cb8e2638d0f5f157afbdb45f88b98643 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 7 Jul 2017 13:25:51 +0300 Subject: [PATCH] Lookup product details when importing invoice CSV --- app/Ninja/Import/BaseTransformer.php | 24 +++++++++++++++++++++ app/Ninja/Import/CSV/InvoiceTransformer.php | 4 ++-- app/Services/ImportService.php | 2 ++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/Ninja/Import/BaseTransformer.php b/app/Ninja/Import/BaseTransformer.php index d718fd6824d8..d136b159f1ff 100644 --- a/app/Ninja/Import/BaseTransformer.php +++ b/app/Ninja/Import/BaseTransformer.php @@ -108,6 +108,30 @@ class BaseTransformer extends TransformerAbstract return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null; } + /** + * @param $name + * + * @return null + */ + public function getProductNotes($name) + { + $name = strtolower(trim($name)); + + 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; + } + /** * @param $name * diff --git a/app/Ninja/Import/CSV/InvoiceTransformer.php b/app/Ninja/Import/CSV/InvoiceTransformer.php index 146f7a5846a6..0692badfb3fc 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'), - 'cost' => $this->getFloat($data, 'amount'), + 'notes' => $this->getString($data, 'notes') ?: $this->getProductNotes($this->getString($data, 'product')), + 'cost' => $this->getFloat($data, 'amount') ?: $this->getProductCost($this->getString($data, 'product')), 'qty' => $this->getFloat($data, 'quantity') ?: 1, ], ], diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 18cb4edc84b9..ec278af94a01 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -911,6 +911,8 @@ class ImportService { 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; } }