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