1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Services/ImportService.php

1058 lines
30 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-11-18 15:40:50 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Services;
use App\Models\Client;
use App\Models\Contact;
2017-01-30 20:40:43 +01:00
use App\Models\EntityModel;
use App\Models\Expense;
use App\Models\ExpenseCategory;
use App\Models\Invoice;
2017-03-07 10:09:53 +01:00
use App\Models\Payment;
use App\Models\Product;
2017-01-30 20:40:43 +01:00
use App\Models\Vendor;
use App\Models\AccountGatewayToken;
2017-01-30 20:40:43 +01:00
use App\Ninja\Import\BaseTransformer;
2015-11-18 15:40:50 +01:00
use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\CustomerRepository;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\ContactRepository;
use App\Ninja\Repositories\ExpenseCategoryRepository;
use App\Ninja\Repositories\ExpenseRepository;
2015-11-18 15:40:50 +01:00
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\PaymentRepository;
2016-05-31 22:15:55 +02:00
use App\Ninja\Repositories\ProductRepository;
2016-10-02 09:10:28 +02:00
use App\Ninja\Repositories\VendorRepository;
2017-07-31 18:22:29 +02:00
use App\Ninja\Repositories\TaxRateRepository;
2015-11-18 15:40:50 +01:00
use App\Ninja\Serializers\ArraySerializer;
2017-01-30 20:40:43 +01:00
use Auth;
use Cache;
use Excel;
use Exception;
2017-04-30 10:58:56 +02:00
use File;
2017-01-30 20:40:43 +01:00
use League\Fractal\Manager;
use parsecsv;
use Session;
use stdClass;
use Utils;
use Carbon;
2015-11-18 15:40:50 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class ImportService.
*/
2015-11-18 15:40:50 +01:00
class ImportService
{
/**
* @var
*/
2015-11-18 15:40:50 +01:00
protected $transformer;
/**
* @var InvoiceRepository
*/
2015-11-18 15:40:50 +01:00
protected $invoiceRepo;
/**
* @var ClientRepository
*/
2015-11-18 15:40:50 +01:00
protected $clientRepo;
/**
* @var CustomerRepository
*/
protected $customerRepo;
/**
* @var ContactRepository
*/
protected $contactRepo;
/**
* @var ProductRepository
*/
2016-05-31 22:15:55 +02:00
protected $productRepo;
/**
* @var array
*/
2016-06-02 21:03:59 +02:00
protected $processedRows = [];
/**
* @var array
*/
2016-06-02 21:03:59 +02:00
private $maps = [];
/**
* @var array
*/
2016-06-02 21:03:59 +02:00
public $results = [];
2015-11-18 15:40:50 +01:00
/**
* @var array
*/
2015-11-18 15:40:50 +01:00
public static $entityTypes = [
2016-06-02 21:03:59 +02:00
IMPORT_JSON,
2015-11-18 15:40:50 +01:00
ENTITY_CLIENT,
ENTITY_CONTACT,
2015-11-18 15:40:50 +01:00
ENTITY_INVOICE,
ENTITY_PAYMENT,
2015-11-18 15:40:50 +01:00
ENTITY_TASK,
2016-05-31 22:15:55 +02:00
ENTITY_PRODUCT,
ENTITY_EXPENSE,
ENTITY_CUSTOMER,
2015-11-18 15:40:50 +01:00
];
/**
* @var array
*/
2015-11-18 15:40:50 +01:00
public static $sources = [
IMPORT_CSV,
2016-06-02 21:03:59 +02:00
IMPORT_JSON,
2015-11-18 15:40:50 +01:00
IMPORT_FRESHBOOKS,
2015-12-08 11:10:20 +01:00
IMPORT_HIVEAGE,
2015-12-10 11:57:51 +01:00
IMPORT_INVOICEABLE,
2017-05-22 09:46:37 +02:00
IMPORT_INVOICEPLANE,
2015-12-14 21:07:48 +01:00
IMPORT_NUTCACHE,
2015-12-10 14:35:40 +01:00
IMPORT_RONIN,
IMPORT_STRIPE,
2015-12-13 21:12:54 +01:00
IMPORT_WAVE,
IMPORT_ZOHO,
2015-11-18 15:40:50 +01:00
];
/**
* ImportService constructor.
*
2017-01-30 20:40:43 +01:00
* @param Manager $manager
* @param ClientRepository $clientRepo
* @param CustomerRepository $customerRepo
* @param InvoiceRepository $invoiceRepo
* @param PaymentRepository $paymentRepo
* @param ContactRepository $contactRepo
* @param ProductRepository $productRepo
*/
2016-05-31 22:15:55 +02:00
public function __construct(
Manager $manager,
ClientRepository $clientRepo,
CustomerRepository $customerRepo,
2016-05-31 22:15:55 +02:00
InvoiceRepository $invoiceRepo,
PaymentRepository $paymentRepo,
ContactRepository $contactRepo,
2016-10-02 09:10:28 +02:00
ProductRepository $productRepo,
ExpenseRepository $expenseRepo,
2016-10-05 22:46:15 +02:00
VendorRepository $vendorRepo,
2017-07-31 18:22:29 +02:00
ExpenseCategoryRepository $expenseCategoryRepo,
TaxRateRepository $taxRateRepository
2017-01-30 17:05:31 +01:00
) {
2015-11-18 15:40:50 +01:00
$this->fractal = $manager;
$this->fractal->setSerializer(new ArraySerializer());
$this->clientRepo = $clientRepo;
$this->customerRepo = $customerRepo;
2015-11-18 15:40:50 +01:00
$this->invoiceRepo = $invoiceRepo;
$this->paymentRepo = $paymentRepo;
$this->contactRepo = $contactRepo;
2016-05-31 22:15:55 +02:00
$this->productRepo = $productRepo;
2016-10-02 09:10:28 +02:00
$this->expenseRepo = $expenseRepo;
$this->vendorRepo = $vendorRepo;
2016-10-05 22:46:15 +02:00
$this->expenseCategoryRepo = $expenseCategoryRepo;
2017-07-31 18:22:29 +02:00
$this->taxRateRepository = $taxRateRepository;
2015-11-18 15:40:50 +01:00
}
/**
* @param $file
2017-01-30 20:40:43 +01:00
*
* @throws Exception
2017-01-30 20:40:43 +01:00
*
* @return array
*/
2017-04-30 10:58:56 +02:00
public function importJSON($fileName, $includeData, $includeSettings)
2016-06-02 21:03:59 +02:00
{
$this->initMaps();
2017-04-02 17:10:24 +02:00
$this->checkForFile($fileName);
$file = file_get_contents($fileName);
2016-06-02 21:03:59 +02:00
$json = json_decode($file, true);
$json = $this->removeIdFields($json);
$transformer = new BaseTransformer($this->maps);
2016-06-02 21:03:59 +02:00
$this->checkClientCount(count($json['clients']));
2017-03-22 10:50:55 +01:00
if ($includeSettings) {
// remove blank id values
$settings = [];
foreach ($json as $field => $value) {
if (strstr($field, '_id') && ! $value) {
// continue;
} else {
$settings[$field] = $value;
}
2016-12-25 11:32:11 +01:00
}
2017-04-02 17:10:24 +02:00
2017-03-22 10:50:55 +01:00
$account = Auth::user()->account;
$account->fill($settings);
$account->save();
2017-03-31 17:02:56 +02:00
$emailSettings = $account->account_email_settings;
2017-07-12 09:52:49 +02:00
$emailSettings->fill(isset($settings['account_email_settings']) ? $settings['account_email_settings'] : $settings);
2017-03-31 17:02:56 +02:00
$emailSettings->save();
2016-12-25 11:32:11 +01:00
}
2017-03-22 10:50:55 +01:00
if ($includeData) {
foreach ($json['products'] as $jsonProduct) {
if ($transformer->hasProduct($jsonProduct['product_key'])) {
continue;
}
$productValidate = EntityModel::validate($jsonProduct, ENTITY_PRODUCT);
if ($productValidate === true) {
2017-03-22 10:50:55 +01:00
$product = $this->productRepo->save($jsonProduct);
$this->addProductToMaps($product);
$this->addSuccess($product);
} else {
$jsonProduct['type'] = ENTITY_PRODUCT;
$jsonProduct['error'] = $productValidate;
2017-03-22 10:50:55 +01:00
$this->addFailure(ENTITY_PRODUCT, $jsonProduct);
continue;
}
2016-06-02 21:03:59 +02:00
}
2017-03-22 10:50:55 +01:00
foreach ($json['clients'] as $jsonClient) {
$clientValidate = EntityModel::validate($jsonClient, ENTITY_CLIENT);
if ($clientValidate === true) {
2017-03-22 10:50:55 +01:00
$client = $this->clientRepo->save($jsonClient);
$this->addClientToMaps($client);
$this->addSuccess($client);
2016-06-02 21:03:59 +02:00
} else {
$jsonClient['type'] = ENTITY_CLIENT;
$jsonClient['error'] = $clientValidate;
2017-03-22 10:50:55 +01:00
$this->addFailure(ENTITY_CLIENT, $jsonClient);
2016-06-02 21:03:59 +02:00
continue;
}
2017-03-22 10:50:55 +01:00
foreach ($jsonClient['invoices'] as $jsonInvoice) {
$jsonInvoice['client_id'] = $client->id;
$invoiceValidate = EntityModel::validate($jsonInvoice, ENTITY_INVOICE);
if ($invoiceValidate === true) {
2017-03-22 10:50:55 +01:00
$invoice = $this->invoiceRepo->save($jsonInvoice);
$this->addInvoiceToMaps($invoice);
$this->addSuccess($invoice);
2016-06-02 21:03:59 +02:00
} else {
$jsonInvoice['type'] = ENTITY_INVOICE;
$jsonInvoice['error'] = $invoiceValidate;
2017-03-22 10:50:55 +01:00
$this->addFailure(ENTITY_INVOICE, $jsonInvoice);
2016-06-02 21:03:59 +02:00
continue;
}
2017-03-22 10:50:55 +01:00
foreach ($jsonInvoice['payments'] as $jsonPayment) {
$jsonPayment['invoice_id'] = $invoice->public_id;
$paymentValidate = EntityModel::validate($jsonPayment, ENTITY_PAYMENT);
if ($paymentValidate === true) {
2017-03-22 10:50:55 +01:00
$jsonPayment['client_id'] = $client->id;
$jsonPayment['invoice_id'] = $invoice->id;
$payment = $this->paymentRepo->save($jsonPayment);
$this->addSuccess($payment);
} else {
$jsonPayment['type'] = ENTITY_PAYMENT;
$jsonPayment['error'] = $paymentValidate;
2017-03-22 10:50:55 +01:00
$this->addFailure(ENTITY_PAYMENT, $jsonPayment);
continue;
}
}
2016-06-02 21:03:59 +02:00
}
}
}
2017-04-30 10:58:56 +02:00
File::delete($fileName);
2017-04-02 17:10:24 +02:00
2016-06-02 21:03:59 +02:00
return $this->results;
}
/**
* @param $array
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-06-02 21:03:59 +02:00
public function removeIdFields($array)
{
foreach ($array as $key => $val) {
if (is_array($val)) {
$array[$key] = $this->removeIdFields($val);
} elseif ($key === 'id') {
unset($array[$key]);
}
}
2017-01-30 20:40:43 +01:00
2016-06-02 21:03:59 +02:00
return $array;
}
/**
* @param $source
* @param $files
2017-01-30 20:40:43 +01:00
*
* @return array
*/
2016-06-02 21:03:59 +02:00
public function importFiles($source, $files)
2015-11-18 15:40:50 +01:00
{
2015-12-21 20:57:55 +01:00
$results = [];
2015-11-18 15:40:50 +01:00
$imported_files = null;
2016-06-02 21:03:59 +02:00
$this->initMaps();
2015-11-24 20:45:38 +01:00
2015-11-18 15:40:50 +01:00
foreach ($files as $entityType => $file) {
2015-12-21 20:57:55 +01:00
$results[$entityType] = $this->execute($source, $entityType, $file);
2015-11-18 15:40:50 +01:00
}
2015-12-31 16:09:45 +01:00
2015-12-21 20:57:55 +01:00
return $results;
2015-11-18 15:40:50 +01:00
}
/**
* @param $source
* @param $entityType
* @param $file
2017-01-30 20:40:43 +01:00
*
* @return array
*/
2017-04-30 10:58:56 +02:00
private function execute($source, $entityType, $fileName)
2015-11-18 15:40:50 +01:00
{
2015-12-21 20:57:55 +01:00
$results = [
RESULT_SUCCESS => [],
RESULT_FAILURE => [],
];
2015-11-24 20:45:38 +01:00
// Convert the data
$row_list = [];
2017-04-02 17:10:24 +02:00
$this->checkForFile($fileName);
2016-06-02 21:03:59 +02:00
2017-04-02 17:10:24 +02:00
Excel::load($fileName, function ($reader) use ($source, $entityType, &$row_list, &$results) {
2015-11-24 20:45:38 +01:00
$this->checkData($entityType, count($reader->all()));
2016-06-02 21:03:59 +02:00
$reader->each(function ($row) use ($source, $entityType, &$row_list, &$results) {
2017-01-08 15:13:23 +01:00
if ($this->isRowEmpty($row)) {
return;
}
2016-06-02 21:03:59 +02:00
$data_index = $this->transformRow($source, $entityType, $row);
2015-12-31 16:09:45 +01:00
if ($data_index !== false) {
if ($data_index !== true) {
// Wasn't merged with another row
$row_list[] = ['row' => $row, 'data_index' => $data_index];
}
2015-12-21 20:57:55 +01:00
} else {
$results[RESULT_FAILURE][] = $row;
2015-11-25 10:35:24 +01:00
}
2015-11-24 20:45:38 +01:00
});
});
2015-12-31 16:09:45 +01:00
// Save the data
2015-12-31 16:09:45 +01:00
foreach ($row_list as $row_data) {
2016-06-02 21:03:59 +02:00
$result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index']);
if ($result) {
$results[RESULT_SUCCESS][] = $result;
} else {
$results[RESULT_FAILURE][] = $row_data['row'];
}
}
2015-11-18 17:08:37 +01:00
2017-04-30 10:58:56 +02:00
File::delete($fileName);
2017-04-02 17:10:24 +02:00
2015-12-21 20:57:55 +01:00
return $results;
2015-11-24 20:45:38 +01:00
}
2015-11-18 18:16:23 +01:00
/**
* @param $source
* @param $entityType
* @param $row
2017-01-30 20:40:43 +01:00
*
* @return bool|mixed
*/
2016-06-02 21:03:59 +02:00
private function transformRow($source, $entityType, $row)
2015-11-24 20:45:38 +01:00
{
2016-06-02 21:03:59 +02:00
$transformer = $this->getTransformer($source, $entityType, $this->maps);
2017-03-27 22:05:18 +02:00
$resource = $transformer->transform($row);
if (! $resource) {
return false;
}
$data = $this->fractal->createData($resource)->toArray();
2016-10-05 22:46:15 +02:00
// Create expesnse category
if ($entityType == ENTITY_EXPENSE) {
2017-01-30 17:05:31 +01:00
if (! empty($row->expense_category)) {
2016-10-05 22:46:15 +02:00
$categoryId = $transformer->getExpenseCategoryId($row->expense_category);
2017-01-30 17:05:31 +01:00
if (! $categoryId) {
2016-10-05 22:46:15 +02:00
$category = $this->expenseCategoryRepo->save(['name' => $row->expense_category]);
$this->addExpenseCategoryToMaps($category);
2017-03-27 22:05:18 +02:00
$data['expense_category_id'] = $category->id;
2016-10-05 22:46:15 +02:00
}
}
2017-01-30 17:05:31 +01:00
if (! empty($row->vendor) && ($vendorName = trim($row->vendor))) {
if (! $transformer->getVendorId($vendorName)) {
2017-01-08 15:02:52 +01:00
$vendor = $this->vendorRepo->save(['name' => $vendorName, 'vendor_contact' => []]);
2016-10-05 22:56:48 +02:00
$this->addVendorToMaps($vendor);
2017-03-27 22:05:18 +02:00
$data['vendor_id'] = $vendor->id;
2016-10-05 22:56:48 +02:00
}
}
2016-10-05 22:46:15 +02:00
}
2015-11-25 10:35:24 +01:00
// if the invoice number is blank we'll assign it
2017-01-30 20:40:43 +01:00
if ($entityType == ENTITY_INVOICE && ! $data['invoice_number']) {
2015-11-24 21:41:08 +01:00
$account = Auth::user()->account;
$invoice = Invoice::createNew();
2017-01-04 09:11:32 +01:00
$data['invoice_number'] = $account->getNextNumber($invoice);
2015-11-24 21:41:08 +01:00
}
2015-12-31 16:09:45 +01:00
2016-08-07 21:42:32 +02:00
if (EntityModel::validate($data, $entityType) !== true) {
return false;
}
2015-12-31 16:09:45 +01:00
if ($entityType == ENTITY_INVOICE) {
if (empty($this->processedRows[$data['invoice_number']])) {
$this->processedRows[$data['invoice_number']] = $data;
2015-12-31 16:09:45 +01:00
} else {
// Merge invoice items
$this->processedRows[$data['invoice_number']]['invoice_items'] = array_merge($this->processedRows[$data['invoice_number']]['invoice_items'], $data['invoice_items']);
2015-12-31 16:09:45 +01:00
return true;
}
2015-12-31 16:09:45 +01:00
} else {
$this->processedRows[] = $data;
}
2015-12-31 16:09:45 +01:00
end($this->processedRows);
2015-12-31 16:09:45 +01:00
return key($this->processedRows);
}
2015-12-31 16:09:45 +01:00
/**
* @param $source
* @param $entityType
* @param $row
* @param $data_index
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-06-02 21:03:59 +02:00
private function saveData($source, $entityType, $row, $data_index)
{
$data = $this->processedRows[$data_index];
if ($entityType == ENTITY_INVOICE) {
$data['is_public'] = true;
}
2017-03-07 10:09:53 +01:00
2015-11-24 20:45:38 +01:00
$entity = $this->{"{$entityType}Repo"}->save($data);
2015-11-25 10:35:24 +01:00
2016-06-02 21:03:59 +02:00
// update the entity maps
if ($entityType != ENTITY_CUSTOMER) {
$mapFunction = 'add' . ucwords($entity->getEntityType()) . 'ToMaps';
if (method_exists($this, $mapFunction)) {
$this->$mapFunction($entity);
}
}
2016-06-02 21:03:59 +02:00
2015-11-24 20:45:38 +01:00
// if the invoice is paid we'll also create a payment record
2015-12-10 14:35:40 +01:00
if ($entityType === ENTITY_INVOICE && isset($data['paid']) && $data['paid'] > 0) {
2017-03-27 22:05:18 +02:00
$this->createPayment($source, $row, $data['client_id'], $entity->id, $entity->public_id);
2015-11-24 20:45:38 +01:00
}
return $entity;
2015-11-24 20:45:38 +01:00
}
/**
* @param $entityType
* @param $count
2017-01-30 20:40:43 +01:00
*
* @throws Exception
*/
2015-11-24 20:45:38 +01:00
private function checkData($entityType, $count)
{
2016-07-19 17:57:01 +02:00
if (Utils::isNinja() && $count > MAX_IMPORT_ROWS) {
throw new Exception(trans('texts.limit_import_rows', ['count' => MAX_IMPORT_ROWS]));
}
2015-11-24 20:45:38 +01:00
if ($entityType === ENTITY_CLIENT) {
$this->checkClientCount($count);
}
}
/**
* @param $count
2017-01-30 20:40:43 +01:00
*
* @throws Exception
*/
2015-11-25 10:35:24 +01:00
private function checkClientCount($count)
{
$totalClients = $count + Client::scope()->withTrashed()->count();
if ($totalClients > Auth::user()->getMaxNumClients()) {
throw new Exception(trans('texts.limit_clients', ['count' => Auth::user()->getMaxNumClients()]));
}
}
/**
* @param $source
* @param $entityType
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2015-11-24 20:45:38 +01:00
public static function getTransformerClassName($source, $entityType)
{
return 'App\\Ninja\\Import\\'.$source.'\\'.ucwords($entityType).'Transformer';
}
/**
* @param $source
* @param $entityType
* @param $maps
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-12-08 11:10:20 +01:00
public static function getTransformer($source, $entityType, $maps)
2015-11-24 20:45:38 +01:00
{
$className = self::getTransformerClassName($source, $entityType);
2015-12-08 11:10:20 +01:00
return new $className($maps);
2015-11-24 20:45:38 +01:00
}
/**
* @param $source
* @param $data
* @param $clientId
* @param $invoiceId
*/
2017-03-27 22:05:18 +02:00
private function createPayment($source, $row, $clientId, $invoiceId, $invoicePublicId)
2015-11-24 20:45:38 +01:00
{
2016-06-02 21:03:59 +02:00
$paymentTransformer = $this->getTransformer($source, ENTITY_PAYMENT, $this->maps);
2015-11-24 20:45:38 +01:00
2017-03-07 10:09:53 +01:00
$row->client_id = $clientId;
$row->invoice_id = $invoiceId;
2015-11-24 20:45:38 +01:00
2017-03-07 10:09:53 +01:00
if ($resource = $paymentTransformer->transform($row)) {
2015-11-24 20:45:38 +01:00
$data = $this->fractal->createData($resource)->toArray();
2017-03-27 22:05:18 +02:00
$data['invoice_id'] = $invoicePublicId;
2017-03-07 10:09:53 +01:00
if (Payment::validate($data) === true) {
2017-03-27 22:05:18 +02:00
$data['invoice_id'] = $invoiceId;
2017-03-07 10:09:53 +01:00
$this->paymentRepo->save($data);
}
2015-11-24 20:45:38 +01:00
}
2015-11-18 15:40:50 +01:00
}
/**
* @param array $files
2017-01-30 20:40:43 +01:00
*
* @throws Exception
2017-01-30 20:40:43 +01:00
*
* @return array
*/
public function mapCSV(array $files)
2015-11-18 15:40:50 +01:00
{
2015-11-24 20:45:38 +01:00
$data = [];
foreach ($files as $entityType => $filename) {
$class = 'App\\Models\\' . ucwords($entityType);
2016-05-31 22:15:55 +02:00
$columns = $class::getImportColumns();
$map = $class::getImportMap();
2015-11-24 20:45:38 +01:00
// Lookup field translations
foreach ($columns as $key => $value) {
unset($columns[$key]);
2017-07-07 14:12:57 +02:00
$label = $value;
// disambiguate some of the labels
if ($entityType == ENTITY_INVOICE) {
if ($label == 'name') {
$label = 'client_name';
} elseif ($label == 'notes') {
$label = 'product_notes';
} elseif ($label == 'terms') {
$label = 'invoice_terms';
}
}
$columns[$value] = trans("texts.{$label}");
2015-11-24 20:45:38 +01:00
}
array_unshift($columns, ' ');
$data[$entityType] = $this->mapFile($entityType, $filename, $columns, $map);
if ($entityType === ENTITY_CLIENT) {
if (count($data[$entityType]['data']) + Client::scope()->count() > Auth::user()->getMaxNumClients()) {
throw new Exception(trans('texts.limit_clients', ['count' => Auth::user()->getMaxNumClients()]));
}
}
}
return $data;
2015-11-18 15:40:50 +01:00
}
2015-11-18 18:16:23 +01:00
/**
* @param $entityType
* @param $filename
* @param $columns
* @param $map
2017-01-30 20:40:43 +01:00
*
* @return array
*/
2015-11-24 20:45:38 +01:00
public function mapFile($entityType, $filename, $columns, $map)
2015-11-18 18:16:23 +01:00
{
2017-04-02 15:54:07 +02:00
$data = $this->getCsvData($filename);
2015-11-18 18:16:23 +01:00
$headers = false;
$hasHeaders = false;
$mapped = [];
2015-11-18 18:16:23 +01:00
2017-04-02 15:54:07 +02:00
if (count($data) > 0) {
$headers = $data[0];
2015-11-18 18:16:23 +01:00
foreach ($headers as $title) {
if (strpos(strtolower($title), 'name') > 0) {
$hasHeaders = true;
break;
}
}
2017-01-30 20:40:43 +01:00
for ($i = 0; $i < count($headers); $i++) {
2015-11-18 18:16:23 +01:00
$title = strtolower($headers[$i]);
$mapped[$i] = '';
2016-10-02 09:10:28 +02:00
foreach ($map as $search => $column) {
if ($this->checkForMatch($title, $search)) {
$hasHeaders = true;
$mapped[$i] = $column;
break;
2015-11-18 18:16:23 +01:00
}
}
}
}
$data = [
2015-11-24 20:45:38 +01:00
'entityType' => $entityType,
2017-04-02 15:54:07 +02:00
'data' => $data,
2015-11-18 18:16:23 +01:00
'headers' => $headers,
'hasHeaders' => $hasHeaders,
'columns' => $columns,
'mapped' => $mapped,
'warning' => false,
];
2015-11-18 18:16:23 +01:00
// check that dates are valid
if (count($data['data']) > 1) {
$row = $data['data'][1];
foreach ($mapped as $index => $field) {
if (! strstr($field, 'date')) {
continue;
}
try {
$date = new Carbon($row[$index]);
} catch(Exception $e) {
$data['warning'] = 'invalid_date';
}
}
}
2015-11-18 18:16:23 +01:00
return $data;
}
2017-04-02 17:10:24 +02:00
private function getCsvData($fileName)
2017-04-02 15:54:07 +02:00
{
2017-04-02 17:10:24 +02:00
$this->checkForFile($fileName);
2017-10-31 18:40:33 +01:00
$file = file_get_contents($fileName);
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file));
2017-04-02 15:54:07 +02:00
if (count($data) > 0) {
$headers = $data[0];
// Remove Invoice Ninja headers
if (count($headers) && count($data) > 4) {
$firstCell = $headers[0];
if (strstr($firstCell, APP_NAME)) {
array_shift($data); // Invoice Ninja...
array_shift($data); // <blank line>
array_shift($data); // Enitty Type Header
}
}
}
return $data;
}
/**
* @param $column
* @param $pattern
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-11-25 10:35:24 +01:00
private function checkForMatch($column, $pattern)
{
if (strpos($column, 'sec') === 0) {
return false;
}
if (strpos($pattern, '^')) {
list($include, $exclude) = explode('^', $pattern);
$includes = explode('|', $include);
$excludes = explode('|', $exclude);
} else {
$includes = explode('|', $pattern);
$excludes = [];
}
foreach ($includes as $string) {
if (strpos($column, $string) !== false) {
$excluded = false;
foreach ($excludes as $exclude) {
if (strpos($column, $exclude) !== false) {
$excluded = true;
break;
}
}
2017-01-30 20:40:43 +01:00
if (! $excluded) {
2015-11-25 10:35:24 +01:00
return true;
}
}
}
return false;
}
/**
* @param array $maps
* @param $headers
2017-01-30 20:40:43 +01:00
*
* @return array
*/
2017-04-02 15:54:07 +02:00
public function importCSV(array $maps, $headers, $timestamp)
2015-11-18 18:16:23 +01:00
{
2015-12-21 20:57:55 +01:00
$results = [];
2015-11-25 10:35:24 +01:00
2015-11-24 20:45:38 +01:00
foreach ($maps as $entityType => $map) {
2017-04-02 15:54:07 +02:00
$results[$entityType] = $this->executeCSV($entityType, $map, $headers[$entityType], $timestamp);
2015-11-25 10:35:24 +01:00
}
2015-12-21 20:57:55 +01:00
return $results;
2015-11-25 10:35:24 +01:00
}
/**
* @param $entityType
* @param $map
* @param $hasHeaders
2017-01-30 20:40:43 +01:00
*
* @return array
*/
2017-04-02 15:54:07 +02:00
private function executeCSV($entityType, $map, $hasHeaders, $timestamp)
2015-11-25 10:35:24 +01:00
{
2015-12-21 20:57:55 +01:00
$results = [
RESULT_SUCCESS => [],
RESULT_FAILURE => [],
];
2015-11-25 10:35:24 +01:00
$source = IMPORT_CSV;
2017-04-30 10:58:56 +02:00
$path = env('FILE_IMPORT_PATH') ?: storage_path() . '/import';
$fileName = sprintf('%s/%s_%s_%s.csv', $path, Auth::user()->account_id, $timestamp, $entityType);
2017-04-02 17:10:24 +02:00
$data = $this->getCsvData($fileName);
2015-11-25 10:35:24 +01:00
$this->checkData($entityType, count($data));
2016-06-02 21:03:59 +02:00
$this->initMaps();
2015-11-25 10:35:24 +01:00
// Convert the data
$row_list = [];
2015-11-25 10:35:24 +01:00
foreach ($data as $row) {
if ($hasHeaders) {
$hasHeaders = false;
continue;
}
$row = $this->convertToObject($entityType, $row, $map);
2017-01-08 15:13:23 +01:00
if ($this->isRowEmpty($row)) {
continue;
}
2016-06-02 21:03:59 +02:00
$data_index = $this->transformRow($source, $entityType, $row);
2015-12-31 16:09:45 +01:00
if ($data_index !== false) {
2015-12-31 16:09:45 +01:00
if ($data_index !== true) {
// Wasn't merged with another row
$row_list[] = ['row' => $row, 'data_index' => $data_index];
}
} else {
$results[RESULT_FAILURE][] = $row;
}
}
2015-12-31 16:09:45 +01:00
// Save the data
2015-12-31 16:09:45 +01:00
foreach ($row_list as $row_data) {
2016-06-02 21:03:59 +02:00
$result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index']);
2015-12-31 16:09:45 +01:00
2015-12-21 20:57:55 +01:00
if ($result) {
$results[RESULT_SUCCESS][] = $result;
} else {
$results[RESULT_FAILURE][] = $row;
2015-11-25 10:35:24 +01:00
}
2015-11-18 18:16:23 +01:00
}
2015-11-25 10:35:24 +01:00
2017-04-30 10:58:56 +02:00
File::delete($fileName);
2015-11-25 10:35:24 +01:00
2015-12-21 20:57:55 +01:00
return $results;
2015-11-24 20:45:38 +01:00
}
2015-11-18 18:16:23 +01:00
/**
* @param $entityType
* @param $data
* @param $map
2017-01-30 20:40:43 +01:00
*
* @return stdClass
*/
2015-11-24 20:45:38 +01:00
private function convertToObject($entityType, $data, $map)
{
$obj = new stdClass();
$class = 'App\\Models\\' . ucwords($entityType);
2016-05-31 22:15:55 +02:00
$columns = $class::getImportColumns();
2015-11-18 18:16:23 +01:00
2015-11-24 20:45:38 +01:00
foreach ($columns as $column) {
$obj->$column = false;
}
2015-11-18 18:16:23 +01:00
2015-11-24 20:45:38 +01:00
foreach ($map as $index => $field) {
if (! $field) {
2015-11-18 18:16:23 +01:00
continue;
}
2015-11-25 10:35:24 +01:00
if (isset($obj->$field) && $obj->$field) {
continue;
}
2017-11-01 18:06:00 +01:00
if (isset($data[$index])) {
$obj->$field = $data[$index];
}
2015-11-18 18:16:23 +01:00
}
2015-11-24 20:45:38 +01:00
return $obj;
2015-11-18 18:16:23 +01:00
}
2016-06-02 21:03:59 +02:00
/**
* @param $entity
*/
2016-06-02 21:03:59 +02:00
private function addSuccess($entity)
{
$this->results[$entity->getEntityType()][RESULT_SUCCESS][] = $entity;
}
/**
* @param $entityType
* @param $data
*/
2016-06-02 21:03:59 +02:00
private function addFailure($entityType, $data)
{
$this->results[$entityType][RESULT_FAILURE][] = $data;
}
private function init()
{
EntityModel::$notifySubscriptions = false;
2016-12-25 11:32:11 +01:00
foreach ([ENTITY_CLIENT, ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_QUOTE, ENTITY_PRODUCT] as $entityType) {
2016-06-02 21:03:59 +02:00
$this->results[$entityType] = [
RESULT_SUCCESS => [],
RESULT_FAILURE => [],
];
}
}
private function initMaps()
{
$this->init();
$this->maps = [
'client' => [],
'contact' => [],
'customer' => [],
2016-06-02 21:03:59 +02:00
'invoice' => [],
'invoice_client' => [],
'product' => [],
'countries' => [],
'countries2' => [],
'currencies' => [],
'client_ids' => [],
'invoice_ids' => [],
2016-10-02 09:10:28 +02:00
'vendors' => [],
'expense_categories' => [],
2017-07-31 18:22:29 +02:00
'tax_rates' => [],
'tax_names' => [],
2016-06-02 21:03:59 +02:00
];
$clients = $this->clientRepo->all();
foreach ($clients as $client) {
$this->addClientToMaps($client);
}
$customers = $this->customerRepo->all();
foreach ($customers as $customer) {
$this->addCustomerToMaps($customer);
}
$contacts = $this->contactRepo->all();
foreach ($contacts as $contact) {
$this->addContactToMaps($contact);
}
2016-06-02 21:03:59 +02:00
$invoices = $this->invoiceRepo->all();
foreach ($invoices as $invoice) {
$this->addInvoiceToMaps($invoice);
}
$products = $this->productRepo->all();
foreach ($products as $product) {
$this->addProductToMaps($product);
}
$countries = Cache::get('countries');
foreach ($countries as $country) {
$this->maps['countries'][strtolower($country->name)] = $country->id;
$this->maps['countries2'][strtolower($country->iso_3166_2)] = $country->id;
}
$currencies = Cache::get('currencies');
foreach ($currencies as $currency) {
$this->maps['currencies'][strtolower($currency->code)] = $currency->id;
}
2016-10-02 09:10:28 +02:00
$vendors = $this->vendorRepo->all();
foreach ($vendors as $vendor) {
2016-10-05 22:56:48 +02:00
$this->addVendorToMaps($vendor);
2016-10-02 09:10:28 +02:00
}
2016-10-05 22:46:15 +02:00
$expenseCaegories = $this->expenseCategoryRepo->all();
foreach ($expenseCaegories as $category) {
$this->addExpenseCategoryToMaps($category);
}
2017-07-31 18:22:29 +02:00
$taxRates = $this->taxRateRepository->all();
foreach ($taxRates as $taxRate) {
$name = trim(strtolower($taxRate->name));
$this->maps['tax_rates'][$name] = $taxRate->rate;
$this->maps['tax_names'][$name] = $taxRate->name;
}
2016-06-02 21:03:59 +02:00
}
/**
* @param Invoice $invoice
*/
private function addInvoiceToMaps(Invoice $invoice)
2016-06-02 21:03:59 +02:00
{
if ($number = strtolower(trim($invoice->invoice_number))) {
$this->maps['invoices'][$number] = $invoice;
2016-06-02 21:03:59 +02:00
$this->maps['invoice'][$number] = $invoice->id;
$this->maps['invoice_client'][$number] = $invoice->client_id;
$this->maps['invoice_ids'][$invoice->public_id] = $invoice->id;
}
}
/**
* @param Client $client
*/
private function addClientToMaps(Client $client)
2016-06-02 21:03:59 +02:00
{
if ($name = strtolower(trim($client->name))) {
$this->maps['client'][$name] = $client->id;
2017-03-07 10:09:53 +01:00
$this->maps['client_ids'][$client->public_id] = $client->id;
}
2017-04-24 09:35:30 +02:00
if (count($client->contacts) && $name = strtolower(trim($client->contacts[0]->email))) {
2017-03-07 10:09:53 +01:00
$this->maps['client'][$name] = $client->id;
2016-06-02 21:03:59 +02:00
$this->maps['client_ids'][$client->public_id] = $client->id;
}
}
/**
* @param Customer $customer
*/
private function addCustomerToMaps(AccountGatewayToken $customer)
{
$this->maps['customer'][$customer->token] = $customer;
$this->maps['customer'][$customer->contact->email] = $customer;
}
/**
* @param Product $product
*/
private function addContactToMaps(Contact $contact)
{
if ($key = strtolower(trim($contact->email))) {
$this->maps['contact'][$key] = $contact;
}
}
/**
* @param Product $product
*/
private function addProductToMaps(Product $product)
2016-06-02 21:03:59 +02:00
{
if ($key = strtolower(trim($product->product_key))) {
$this->maps['product'][$key] = $product;
2016-06-02 21:03:59 +02:00
}
}
2016-10-02 09:10:28 +02:00
private function addExpenseToMaps(Expense $expense)
{
// do nothing
}
2016-10-05 22:46:15 +02:00
2016-10-05 22:56:48 +02:00
private function addVendorToMaps(Vendor $vendor)
{
$this->maps['vendor'][strtolower($vendor->name)] = $vendor->id;
}
2016-10-05 22:46:15 +02:00
private function addExpenseCategoryToMaps(ExpenseCategory $category)
{
if ($name = strtolower($category->name)) {
$this->maps['expense_category'][$name] = $category->id;
}
}
2017-01-08 15:13:23 +01:00
private function isRowEmpty($row)
{
$isEmpty = true;
foreach ($row as $key => $val) {
if (trim($val)) {
$isEmpty = false;
}
}
return $isEmpty;
}
2017-04-02 15:54:07 +02:00
public function presentResults($results, $includeSettings = false)
{
$message = '';
$skipped = [];
if ($includeSettings) {
$message = trans('texts.imported_settings') . '<br/>';
}
foreach ($results as $entityType => $entityResults) {
if ($count = count($entityResults[RESULT_SUCCESS])) {
$message .= trans("texts.created_{$entityType}s", ['count' => $count]) . '<br/>';
}
if (count($entityResults[RESULT_FAILURE])) {
$skipped = array_merge($skipped, $entityResults[RESULT_FAILURE]);
}
}
if (count($skipped)) {
$message .= '<p/>' . trans('texts.failed_to_import') . '<br/>';
foreach ($skipped as $skip) {
$message .= json_encode($skip) . '<br/>';
}
}
return $message;
}
2017-04-02 17:10:24 +02:00
private function checkForFile($fileName)
{
$counter = 0;
while (! file_exists($fileName)) {
$counter++;
if ($counter > 60) {
throw new Exception('File not found: ' . $fileName);
}
sleep(2);
}
return true;
}
2015-11-18 15:40:50 +01:00
}