mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-07 17:44:29 -04:00
Support updating products when importing
This commit is contained in:
parent
0493848d7e
commit
aa7448f0b4
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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')),
|
||||
|
@ -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'),
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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',
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user