mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-07 11:14:40 -04:00
Check for duplicates in the JSON import
This commit is contained in:
parent
3d507fad68
commit
91e3f5d7b9
@ -27,7 +27,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function hasClient($name)
|
public function hasClient($name)
|
||||||
{
|
{
|
||||||
$name = trim(strtolower($name));
|
$name = trim(strtolower($name));
|
||||||
return isset($this->maps[ENTITY_CLIENT][$name]);
|
return isset($this->maps[ENTITY_CLIENT][$name]);
|
||||||
@ -37,7 +37,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $key
|
* @param $key
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function hasProduct($key)
|
public function hasProduct($key)
|
||||||
{
|
{
|
||||||
$key = trim(strtolower($key));
|
$key = trim(strtolower($key));
|
||||||
return isset($this->maps[ENTITY_PRODUCT][$key]);
|
return isset($this->maps[ENTITY_PRODUCT][$key]);
|
||||||
@ -48,7 +48,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getString($data, $field)
|
public function getString($data, $field)
|
||||||
{
|
{
|
||||||
return (isset($data->$field) && $data->$field) ? $data->$field : '';
|
return (isset($data->$field) && $data->$field) ? $data->$field : '';
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function getNumber($data, $field)
|
public function getNumber($data, $field)
|
||||||
{
|
{
|
||||||
return (isset($data->$field) && $data->$field) ? $data->$field : 0;
|
return (isset($data->$field) && $data->$field) ? $data->$field : 0;
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $field
|
* @param $field
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
protected function getFloat($data, $field)
|
public function getFloat($data, $field)
|
||||||
{
|
{
|
||||||
return (isset($data->$field) && $data->$field) ? Utils::parseFloat($data->$field) : 0;
|
return (isset($data->$field) && $data->$field) ? Utils::parseFloat($data->$field) : 0;
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getClientId($name)
|
public function getClientId($name)
|
||||||
{
|
{
|
||||||
$name = strtolower(trim($name));
|
$name = strtolower(trim($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;
|
||||||
@ -87,7 +87,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getProductId($name)
|
public function getProductId($name)
|
||||||
{
|
{
|
||||||
$name = strtolower(trim($name));
|
$name = strtolower(trim($name));
|
||||||
return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null;
|
return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null;
|
||||||
@ -97,7 +97,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getCountryId($name)
|
public function getCountryId($name)
|
||||||
{
|
{
|
||||||
$name = strtolower(trim($name));
|
$name = strtolower(trim($name));
|
||||||
return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null;
|
return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null;
|
||||||
@ -107,7 +107,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getCountryIdBy2($name)
|
public function getCountryIdBy2($name)
|
||||||
{
|
{
|
||||||
$name = strtolower(trim($name));
|
$name = strtolower(trim($name));
|
||||||
return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null;
|
return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null;
|
||||||
@ -117,7 +117,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getFirstName($name)
|
public function getFirstName($name)
|
||||||
{
|
{
|
||||||
$name = Utils::splitName($name);
|
$name = Utils::splitName($name);
|
||||||
return $name[0];
|
return $name[0];
|
||||||
@ -128,7 +128,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param string $format
|
* @param string $format
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getDate($date, $format = 'Y-m-d')
|
public function getDate($date, $format = 'Y-m-d')
|
||||||
{
|
{
|
||||||
if ( ! $date instanceof DateTime) {
|
if ( ! $date instanceof DateTime) {
|
||||||
$date = DateTime::createFromFormat($format, $date);
|
$date = DateTime::createFromFormat($format, $date);
|
||||||
@ -141,7 +141,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getLastName($name)
|
public function getLastName($name)
|
||||||
{
|
{
|
||||||
$name = Utils::splitName($name);
|
$name = Utils::splitName($name);
|
||||||
return $name[1];
|
return $name[1];
|
||||||
@ -151,7 +151,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $number
|
* @param $number
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getInvoiceNumber($number)
|
public function getInvoiceNumber($number)
|
||||||
{
|
{
|
||||||
return str_pad(trim($number), 4, '0', STR_PAD_LEFT);
|
return str_pad(trim($number), 4, '0', STR_PAD_LEFT);
|
||||||
}
|
}
|
||||||
@ -160,7 +160,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $invoiceNumber
|
* @param $invoiceNumber
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getInvoiceId($invoiceNumber)
|
public function getInvoiceId($invoiceNumber)
|
||||||
{
|
{
|
||||||
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
||||||
$invoiceNumber = strtolower($invoiceNumber);
|
$invoiceNumber = strtolower($invoiceNumber);
|
||||||
@ -171,7 +171,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $invoiceNumber
|
* @param $invoiceNumber
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function hasInvoice($invoiceNumber)
|
public function hasInvoice($invoiceNumber)
|
||||||
{
|
{
|
||||||
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
||||||
$invoiceNumber = strtolower($invoiceNumber);
|
$invoiceNumber = strtolower($invoiceNumber);
|
||||||
@ -182,7 +182,7 @@ class BaseTransformer extends TransformerAbstract
|
|||||||
* @param $invoiceNumber
|
* @param $invoiceNumber
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
protected function getInvoiceClientId($invoiceNumber)
|
public function getInvoiceClientId($invoiceNumber)
|
||||||
{
|
{
|
||||||
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
||||||
$invoiceNumber = strtolower($invoiceNumber);
|
$invoiceNumber = strtolower($invoiceNumber);
|
||||||
|
@ -25,6 +25,7 @@ use App\Models\Vendor;
|
|||||||
use App\Models\Expense;
|
use App\Models\Expense;
|
||||||
use App\Models\ExpenseCategory;
|
use App\Models\ExpenseCategory;
|
||||||
use App\Models\EntityModel;
|
use App\Models\EntityModel;
|
||||||
|
use App\Ninja\Import\BaseTransformer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ImportService
|
* Class ImportService
|
||||||
@ -142,17 +143,22 @@ class ImportService
|
|||||||
*/
|
*/
|
||||||
public function importJSON($file)
|
public function importJSON($file)
|
||||||
{
|
{
|
||||||
$this->init();
|
$this->initMaps();
|
||||||
|
|
||||||
$file = file_get_contents($file);
|
$file = file_get_contents($file);
|
||||||
$json = json_decode($file, true);
|
$json = json_decode($file, true);
|
||||||
$json = $this->removeIdFields($json);
|
$json = $this->removeIdFields($json);
|
||||||
|
$transformer = new BaseTransformer($this->maps);
|
||||||
|
|
||||||
$this->checkClientCount(count($json['clients']));
|
$this->checkClientCount(count($json['clients']));
|
||||||
|
|
||||||
foreach ($json['products'] as $jsonProduct) {
|
foreach ($json['products'] as $jsonProduct) {
|
||||||
|
if ($transformer->hasProduct($jsonProduct['product_key'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (EntityModel::validate($jsonProduct, ENTITY_PRODUCT) === true) {
|
if (EntityModel::validate($jsonProduct, ENTITY_PRODUCT) === true) {
|
||||||
$product = $this->productRepo->save($jsonProduct);
|
$product = $this->productRepo->save($jsonProduct);
|
||||||
|
$this->addProductToMaps($product);
|
||||||
$this->addSuccess($product);
|
$this->addSuccess($product);
|
||||||
} else {
|
} else {
|
||||||
$this->addFailure(ENTITY_PRODUCT, $jsonProduct);
|
$this->addFailure(ENTITY_PRODUCT, $jsonProduct);
|
||||||
@ -164,6 +170,7 @@ class ImportService
|
|||||||
|
|
||||||
if (EntityModel::validate($jsonClient, ENTITY_CLIENT) === true) {
|
if (EntityModel::validate($jsonClient, ENTITY_CLIENT) === true) {
|
||||||
$client = $this->clientRepo->save($jsonClient);
|
$client = $this->clientRepo->save($jsonClient);
|
||||||
|
$this->addClientToMaps($client);
|
||||||
$this->addSuccess($client);
|
$this->addSuccess($client);
|
||||||
} else {
|
} else {
|
||||||
$this->addFailure(ENTITY_CLIENT, $jsonClient);
|
$this->addFailure(ENTITY_CLIENT, $jsonClient);
|
||||||
@ -174,6 +181,7 @@ class ImportService
|
|||||||
$jsonInvoice['client_id'] = $client->id;
|
$jsonInvoice['client_id'] = $client->id;
|
||||||
if (EntityModel::validate($jsonInvoice, ENTITY_INVOICE) === true) {
|
if (EntityModel::validate($jsonInvoice, ENTITY_INVOICE) === true) {
|
||||||
$invoice = $this->invoiceRepo->save($jsonInvoice);
|
$invoice = $this->invoiceRepo->save($jsonInvoice);
|
||||||
|
$this->addInvoiceToMaps($invoice);
|
||||||
$this->addSuccess($invoice);
|
$this->addSuccess($invoice);
|
||||||
} else {
|
} else {
|
||||||
$this->addFailure(ENTITY_INVOICE, $jsonInvoice);
|
$this->addFailure(ENTITY_INVOICE, $jsonInvoice);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user