From 73a01924461b9afb66028fd2d734f1c28f7a014f Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 18 Nov 2015 18:08:37 +0200 Subject: [PATCH] Working on import --- app/Http/Controllers/DashboardController.php | 4 ++-- app/Http/Controllers/ImportController.php | 10 ++++------ app/Http/Requests/CreateClientRequest.php | 2 +- app/Ninja/Repositories/PaymentRepository.php | 2 +- app/Services/ImportService.php | 16 +++++++++++----- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index d15c6d0da7..c97c193a3b 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -109,8 +109,8 @@ class DashboardController extends BaseController ->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id') ->leftJoin('invoices', 'invoices.id', '=', 'payments.invoice_id') ->where('payments.account_id', '=', Auth::user()->account_id) - ->where('payments.deleted_at', '=', null) - ->where('invoices.deleted_at', '=', null) + ->where('payments.is_deleted', '=', false) + ->where('invoices.is_deleted', '=', false) ->where('clients.deleted_at', '=', null) ->where('contacts.deleted_at', '=', null) ->where('contacts.is_primary', '=', true) diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 67ab454b1b..fd8eb46a28 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -1,5 +1,6 @@ importService->import(Input::get('source'), $files); - } catch (Exception $e) { - Session::flash('error', $e->getMessage()); - - return Redirect::to('/settings/'.ACCOUNT_IMPORT_EXPORT); + Session::flash('message', trans('texts.imported_file').' - '.$imported_files); + } catch (Exception $exception) { + Session::flash('error', $exception->getMessage()); } - Session::flash('message', trans('texts.imported_file').' - '.$imported_files); - return Redirect::to('/settings/'.ACCOUNT_IMPORT_EXPORT); } } diff --git a/app/Http/Requests/CreateClientRequest.php b/app/Http/Requests/CreateClientRequest.php index 2d1716cc1e..00837d01e5 100644 --- a/app/Http/Requests/CreateClientRequest.php +++ b/app/Http/Requests/CreateClientRequest.php @@ -29,7 +29,7 @@ class CreateClientRequest extends Request public function validator($factory) { - // support submiting the form with a single client record + // support submiting the form with a single contact record $input = $this->input(); if (isset($input['contact'])) { $input['contacts'] = [$input['contact']]; diff --git a/app/Ninja/Repositories/PaymentRepository.php b/app/Ninja/Repositories/PaymentRepository.php index 4640fe4818..158dd0637f 100644 --- a/app/Ninja/Repositories/PaymentRepository.php +++ b/app/Ninja/Repositories/PaymentRepository.php @@ -27,7 +27,7 @@ class PaymentRepository extends BaseRepository ->where('clients.deleted_at', '=', null) ->where('contacts.is_primary', '=', true) ->where('contacts.deleted_at', '=', null) - ->where('invoices.deleted_at', '=', null) + ->where('invoices.is_deleted', '=', false) ->select('payments.public_id', 'payments.transaction_reference', 'clients.name as client_name', diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index d2b00161e4..8ee0887bae 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -3,11 +3,13 @@ use Excel; use Cache; use Exception; +use Auth; use League\Fractal\Manager; use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Serializers\ArraySerializer; +use App\Models\Client; class ImportService { @@ -33,9 +35,6 @@ class ImportService //IMPORT_ZOHO, ]; - /** - * FreshBooksDataImporterService constructor. - */ public function __construct(Manager $manager, ClientRepository $clientRepo, InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo) { $this->fractal = $manager; @@ -61,7 +60,14 @@ class ImportService $transformer = new $transformerClassName; Excel::load($file, function($reader) use ($source, $entityType, $transformer) { - + + if ($entityType === ENTITY_CLIENT) { + $totalClients = count($reader->all()) + Client::scope()->withTrashed()->count(); + if ($totalClients > Auth::user()->getMaxNumClients()) { + throw new Exception(trans('texts.limit_clients', ['count' => Auth::user()->getMaxNumClients()])); + } + } + $maps = $this->createMaps(); $reader->each(function($row) use ($source, $entityType, $transformer, $maps) { @@ -71,7 +77,7 @@ class ImportService // if the invoice is paid we'll also create a payment record if ($entityType === ENTITY_INVOICE && isset($data['paid']) && $data['paid']) { - $class = self::getTransformerClassName($source, 'payment'); + $class = self::getTransformerClassName($source, ENTITY_PAYMENT); $paymentTransformer = new $class; $row->client_id = $data['client_id']; $row->invoice_id = $entity->public_id;