2015-11-24 20:45:38 +01:00
|
|
|
<?php namespace app\Services;
|
2015-11-18 15:40:50 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
use stdClass;
|
2015-11-18 15:40:50 +01:00
|
|
|
use Excel;
|
|
|
|
use Cache;
|
|
|
|
use Exception;
|
2015-11-18 17:08:37 +01:00
|
|
|
use Auth;
|
2015-11-18 18:16:23 +01:00
|
|
|
use parsecsv;
|
|
|
|
use Session;
|
|
|
|
use Validator;
|
2015-11-18 15:40:50 +01:00
|
|
|
use League\Fractal\Manager;
|
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
|
|
|
use App\Ninja\Serializers\ArraySerializer;
|
2015-11-18 17:08:37 +01:00
|
|
|
use App\Models\Client;
|
2015-11-24 20:45:38 +01:00
|
|
|
use App\Models\Invoice;
|
2015-11-18 15:40:50 +01:00
|
|
|
|
|
|
|
class ImportService
|
|
|
|
{
|
|
|
|
protected $transformer;
|
|
|
|
protected $invoiceRepo;
|
|
|
|
protected $clientRepo;
|
|
|
|
|
|
|
|
public static $entityTypes = [
|
|
|
|
ENTITY_CLIENT,
|
|
|
|
ENTITY_INVOICE,
|
|
|
|
ENTITY_TASK,
|
|
|
|
];
|
|
|
|
|
|
|
|
public static $sources = [
|
|
|
|
IMPORT_CSV,
|
|
|
|
IMPORT_FRESHBOOKS,
|
|
|
|
//IMPORT_HARVEST,
|
|
|
|
//IMPORT_HIVEAGE,
|
|
|
|
//IMPORT_INVOICEABLE,
|
|
|
|
//IMPORT_NUTCACHE,
|
|
|
|
//IMPORT_RONIN,
|
|
|
|
//IMPORT_WAVE,
|
|
|
|
//IMPORT_ZOHO,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(Manager $manager, ClientRepository $clientRepo, InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo)
|
|
|
|
{
|
|
|
|
$this->fractal = $manager;
|
|
|
|
$this->fractal->setSerializer(new ArraySerializer());
|
|
|
|
|
|
|
|
$this->clientRepo = $clientRepo;
|
|
|
|
$this->invoiceRepo = $invoiceRepo;
|
|
|
|
$this->paymentRepo = $paymentRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function import($source, $files)
|
|
|
|
{
|
|
|
|
$imported_files = null;
|
2015-11-24 20:45:38 +01:00
|
|
|
|
2015-11-18 15:40:50 +01:00
|
|
|
foreach ($files as $entityType => $file) {
|
|
|
|
$this->execute($source, $entityType, $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function execute($source, $entityType, $file)
|
|
|
|
{
|
2015-11-25 10:35:24 +01:00
|
|
|
$skipped = [];
|
2015-11-24 20:45:38 +01:00
|
|
|
|
2015-11-25 10:35:24 +01:00
|
|
|
Excel::load($file, function ($reader) use ($source, $entityType, $skipped) {
|
2015-11-24 20:45:38 +01:00
|
|
|
$this->checkData($entityType, count($reader->all()));
|
|
|
|
$maps = $this->createMaps();
|
2015-11-18 15:40:50 +01:00
|
|
|
|
2015-11-24 21:54:04 +01:00
|
|
|
$reader->each(function ($row) use ($source, $entityType, $maps) {
|
2015-11-25 10:35:24 +01:00
|
|
|
$result = $this->saveData($source, $entityType, $row, $maps);
|
|
|
|
|
|
|
|
if ( ! $result) {
|
|
|
|
$skipped[] = $row;
|
|
|
|
}
|
2015-11-24 20:45:38 +01:00
|
|
|
});
|
|
|
|
});
|
2015-11-18 17:08:37 +01:00
|
|
|
|
2015-11-25 10:35:24 +01:00
|
|
|
return $skipped;
|
2015-11-24 20:45:38 +01:00
|
|
|
}
|
2015-11-18 18:16:23 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
private function saveData($source, $entityType, $row, $maps)
|
|
|
|
{
|
|
|
|
$transformer = $this->getTransformer($source, $entityType);
|
|
|
|
$resource = $transformer->transform($row, $maps);
|
|
|
|
|
|
|
|
if (!$resource) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->fractal->createData($resource)->toArray();
|
|
|
|
|
2015-11-25 10:35:24 +01:00
|
|
|
// if the invoice number is blank we'll assign it
|
2015-11-24 21:54:04 +01:00
|
|
|
if ($entityType == ENTITY_INVOICE && !$data['invoice_number']) {
|
2015-11-24 21:41:08 +01:00
|
|
|
$account = Auth::user()->account;
|
|
|
|
$invoice = Invoice::createNew();
|
|
|
|
$data['invoice_number'] = $account->getNextInvoiceNumber($invoice);
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
if ($this->validate($data, $entityType) !== true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entity = $this->{"{$entityType}Repo"}->save($data);
|
2015-11-25 10:35:24 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
// if the invoice is paid we'll also create a payment record
|
2015-11-24 22:10:09 +01:00
|
|
|
if ($entityType === ENTITY_INVOICE && isset($row->paid) && $row->paid) {
|
2015-11-24 20:45:38 +01:00
|
|
|
$this->createPayment($source, $row, $maps, $data['client_id'], $entity->public_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkData($entityType, $count)
|
|
|
|
{
|
|
|
|
if ($entityType === ENTITY_CLIENT) {
|
|
|
|
$this->checkClientCount($count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
public static function getTransformerClassName($source, $entityType)
|
|
|
|
{
|
|
|
|
return 'App\\Ninja\\Import\\'.$source.'\\'.ucwords($entityType).'Transformer';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTransformer($source, $entityType)
|
|
|
|
{
|
|
|
|
$className = self::getTransformerClassName($source, $entityType);
|
|
|
|
|
|
|
|
return new $className();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPayment($source, $data, $maps, $clientId, $invoiceId)
|
|
|
|
{
|
|
|
|
$paymentTransformer = $this->getTransformer($source, ENTITY_PAYMENT);
|
|
|
|
|
2015-11-24 22:10:09 +01:00
|
|
|
$data->client_id = $clientId;
|
|
|
|
$data->invoice_id = $invoiceId;
|
2015-11-24 20:45:38 +01:00
|
|
|
|
|
|
|
if ($resource = $paymentTransformer->transform($data, $maps)) {
|
|
|
|
$data = $this->fractal->createData($resource)->toArray();
|
|
|
|
$this->paymentRepo->save($data);
|
|
|
|
}
|
2015-11-18 15:40:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 18:16:23 +01:00
|
|
|
private function validate($data, $entityType)
|
|
|
|
{
|
|
|
|
if ($entityType === ENTITY_CLIENT) {
|
|
|
|
$rules = [
|
|
|
|
'contacts' => 'valid_contacts',
|
|
|
|
];
|
2015-11-25 10:35:24 +01:00
|
|
|
}
|
|
|
|
if ($entityType === ENTITY_INVOICE) {
|
2015-11-18 18:16:23 +01:00
|
|
|
$rules = [
|
|
|
|
'client.contacts' => 'valid_contacts',
|
|
|
|
'invoice_items' => 'valid_invoice_items',
|
|
|
|
'invoice_number' => 'required|unique:invoices,invoice_number,,id,account_id,'.Auth::user()->account_id,
|
|
|
|
'discount' => 'positive',
|
|
|
|
];
|
2015-11-24 22:10:09 +01:00
|
|
|
} else {
|
|
|
|
return true;
|
2015-11-18 18:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$validator = Validator::make($data, $rules);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
$messages = $validator->messages();
|
2015-11-24 20:45:38 +01:00
|
|
|
|
2015-11-18 18:16:23 +01:00
|
|
|
return $messages->first();
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 15:40:50 +01:00
|
|
|
private function createMaps()
|
|
|
|
{
|
|
|
|
$clientMap = [];
|
|
|
|
$clients = $this->clientRepo->all();
|
|
|
|
foreach ($clients as $client) {
|
|
|
|
$clientMap[$client->name] = $client->public_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoiceMap = [];
|
|
|
|
$invoices = $this->invoiceRepo->all();
|
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
$invoiceMap[$invoice->invoice_number] = $invoice->public_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$countryMap = [];
|
|
|
|
$countries = Cache::get('countries');
|
|
|
|
foreach ($countries as $country) {
|
|
|
|
$countryMap[$country->name] = $country->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
ENTITY_CLIENT => $clientMap,
|
|
|
|
ENTITY_INVOICE => $invoiceMap,
|
|
|
|
'countries' => $countryMap,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
public function mapCSV($files)
|
2015-11-18 15:40:50 +01:00
|
|
|
{
|
2015-11-24 20:45:38 +01:00
|
|
|
$data = [];
|
|
|
|
|
|
|
|
foreach ($files as $entityType => $filename) {
|
|
|
|
if ($entityType === ENTITY_CLIENT) {
|
|
|
|
$columns = Client::getImportColumns();
|
|
|
|
$map = Client::getImportMap();
|
|
|
|
} else {
|
|
|
|
$columns = Invoice::getImportColumns();
|
|
|
|
$map = Invoice::getImportMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup field translations
|
|
|
|
foreach ($columns as $key => $value) {
|
|
|
|
unset($columns[$key]);
|
|
|
|
$columns[$value] = trans("texts.{$value}");
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
public function mapFile($entityType, $filename, $columns, $map)
|
2015-11-18 18:16:23 +01:00
|
|
|
{
|
|
|
|
require_once app_path().'/Includes/parsecsv.lib.php';
|
|
|
|
$csv = new parseCSV();
|
|
|
|
$csv->heading = false;
|
|
|
|
$csv->auto($filename);
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
Session::put("{$entityType}-data", $csv->data);
|
2015-11-18 18:16:23 +01:00
|
|
|
|
|
|
|
$headers = false;
|
|
|
|
$hasHeaders = false;
|
|
|
|
$mapped = array();
|
|
|
|
|
|
|
|
if (count($csv->data) > 0) {
|
|
|
|
$headers = $csv->data[0];
|
|
|
|
foreach ($headers as $title) {
|
|
|
|
if (strpos(strtolower($title), 'name') > 0) {
|
|
|
|
$hasHeaders = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i<count($headers); $i++) {
|
|
|
|
$title = strtolower($headers[$i]);
|
|
|
|
$mapped[$i] = '';
|
|
|
|
|
|
|
|
if ($hasHeaders) {
|
|
|
|
foreach ($map as $search => $column) {
|
2015-11-25 10:35:24 +01:00
|
|
|
if ($this->checkForMatch($title, $search)) {
|
|
|
|
$mapped[$i] = $column;
|
|
|
|
break;
|
2015-11-18 18:16:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array(
|
2015-11-24 20:45:38 +01:00
|
|
|
'entityType' => $entityType,
|
2015-11-18 18:16:23 +01:00
|
|
|
'data' => $csv->data,
|
|
|
|
'headers' => $headers,
|
|
|
|
'hasHeaders' => $hasHeaders,
|
|
|
|
'columns' => $columns,
|
|
|
|
'mapped' => $mapped,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$excluded) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
public function importCSV($maps, $headers)
|
2015-11-18 18:16:23 +01:00
|
|
|
{
|
2015-11-25 10:35:24 +01:00
|
|
|
$skipped = [];
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
foreach ($maps as $entityType => $map) {
|
2015-11-25 10:35:24 +01:00
|
|
|
$result = $this->executeCSV($entityType, $map, $headers[$entityType]);
|
|
|
|
$skipped = array_merge($skipped, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $skipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function executeCSV($entityType, $map, $hasHeaders)
|
|
|
|
{
|
|
|
|
$skipped = [];
|
|
|
|
$source = IMPORT_CSV;
|
|
|
|
|
|
|
|
$data = Session::get("{$entityType}-data");
|
|
|
|
$this->checkData($entityType, count($data));
|
|
|
|
$maps = $this->createMaps();
|
|
|
|
|
|
|
|
foreach ($data as $row) {
|
|
|
|
if ($hasHeaders) {
|
|
|
|
$hasHeaders = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = $this->convertToObject($entityType, $row, $map);
|
|
|
|
$result = $this->saveData($source, $entityType, $row, $maps);
|
|
|
|
|
|
|
|
if ( ! $result) {
|
|
|
|
$skipped[] = $row;
|
|
|
|
}
|
2015-11-18 18:16:23 +01:00
|
|
|
}
|
2015-11-25 10:35:24 +01:00
|
|
|
|
|
|
|
Session::forget("{$entityType}-data");
|
|
|
|
|
|
|
|
return $skipped;
|
2015-11-24 20:45:38 +01:00
|
|
|
}
|
2015-11-18 18:16:23 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
private function convertToObject($entityType, $data, $map)
|
|
|
|
{
|
|
|
|
$obj = new stdClass();
|
2015-11-18 18:16:23 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
if ($entityType === ENTITY_CLIENT) {
|
|
|
|
$columns = Client::getImportColumns();
|
|
|
|
} else {
|
|
|
|
$columns = Invoice::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;
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
$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
|
|
|
}
|
2015-11-18 15:40:50 +01:00
|
|
|
}
|