From dfe34f6368165be84a08897ea8477345787d6471 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Feb 2022 10:14:54 +1100 Subject: [PATCH] prettier --- app/Import/Providers/BaseImport.php | 385 ++++++++++-------- app/Import/Providers/Csv.php | 79 ++-- app/Import/Providers/Freshbooks.php | 3 +- app/Import/Transformer/BaseTransformer.php | 247 +++++++---- .../Transformer/Csv/ClientTransformer.php | 135 +++--- .../Transformer/Csv/InvoiceTransformer.php | 258 ++++++++---- .../Transformer/Csv/PaymentTransformer.php | 56 +-- .../Transformers/Csv/ClientTransformer.php | 133 +++--- 8 files changed, 793 insertions(+), 503 deletions(-) diff --git a/app/Import/Providers/BaseImport.php b/app/Import/Providers/BaseImport.php index 987d52a3b418..f5b325057f39 100644 --- a/app/Import/Providers/BaseImport.php +++ b/app/Import/Providers/BaseImport.php @@ -30,10 +30,10 @@ use League\Csv\Reader; use League\Csv\Statement; use Symfony\Component\HttpFoundation\ParameterBag; -class BaseImport { - +class BaseImport +{ use CleanLineItems; - + public Company $company; public array $request; @@ -42,54 +42,62 @@ class BaseImport { public $request_name; - public $repository_name; + public $repository_name; - public $factory_name; + public $factory_name; - public $repository; + public $repository; - public $transformer; - + public $transformer; - public function __construct( array $request, Company $company ) { - $this->company = $company; - $this->request = $request; - $this->hash = $request['hash']; - $this->import_type = $request['import_type']; - $this->skip_header = $request['skip_header'] ?? null; - $this->column_map = - ! empty( $request['column_map'] ) ? - array_combine( array_keys( $request['column_map'] ), array_column( $request['column_map'], 'mapping' ) ) : null; + public function __construct(array $request, Company $company) + { + $this->company = $company; + $this->request = $request; + $this->hash = $request['hash']; + $this->import_type = $request['import_type']; + $this->skip_header = $request['skip_header'] ?? null; + $this->column_map = !empty($request['column_map']) + ? array_combine( + array_keys($request['column_map']), + array_column($request['column_map'], 'mapping') + ) + : null; - auth()->login( $this->company->owner(), true ); - - auth()->user()->setCompany($this->company); - } + auth()->login($this->company->owner(), true); + auth() + ->user() + ->setCompany($this->company); + } - protected function getCsvData( $entity_type ) { - - $base64_encoded_csv = Cache::pull( $this->hash . '-' . $entity_type ); - if ( empty( $base64_encoded_csv ) ) { + protected function getCsvData($entity_type) + { + $base64_encoded_csv = Cache::pull($this->hash . '-' . $entity_type); + if (empty($base64_encoded_csv)) { return null; } - $csv = base64_decode( $base64_encoded_csv ); - $csv = Reader::createFromString( $csv ); + $csv = base64_decode($base64_encoded_csv); + $csv = Reader::createFromString($csv); $stmt = new Statement(); - $data = iterator_to_array( $stmt->process( $csv ) ); + $data = iterator_to_array($stmt->process($csv)); - if ( count( $data ) > 0 ) { + if (count($data) > 0) { $headers = $data[0]; // Remove Invoice Ninja headers - if ( count( $headers ) && count( $data ) > 4 && $this->import_type === 'csv' ) { + if ( + count($headers) && + count($data) > 4 && + $this->import_type === 'csv' + ) { $first_cell = $headers[0]; - if ( strstr( $first_cell, config( 'ninja.app_name' ) ) ) { - array_shift( $data ); // Invoice Ninja... - array_shift( $data ); // - array_shift( $data ); // Enitty Type Header + if (strstr($first_cell, config('ninja.app_name'))) { + array_shift($data); // Invoice Ninja... + array_shift($data); // + array_shift($data); // Enitty Type Header } } } @@ -97,23 +105,28 @@ class BaseImport { return $data; } - public function mapCSVHeaderToKeys( $csvData ) { - $keys = array_shift( $csvData ); + public function mapCSVHeaderToKeys($csvData) + { + $keys = array_shift($csvData); - return array_map( function ( $values ) use ( $keys ) { - return array_combine( $keys, $values ); - }, $csvData ); + return array_map(function ($values) use ($keys) { + return array_combine($keys, $values); + }, $csvData); } - private function groupInvoices( $csvData, $key ) { + private function groupInvoices($csvData, $key) + { // Group by invoice. $grouped = []; - foreach ( $csvData as $line_item ) { - if ( empty( $line_item[ $key ] ) ) { - $this->error_array['invoice'][] = [ 'invoice' => $line_item, 'error' => 'No invoice number' ]; + foreach ($csvData as $line_item) { + if (empty($line_item[$key])) { + $this->error_array['invoice'][] = [ + 'invoice' => $line_item, + 'error' => 'No invoice number', + ]; } else { - $grouped[ $line_item[ $key ] ][] = $line_item; + $grouped[$line_item[$key]][] = $line_item; } } @@ -129,208 +142,238 @@ class BaseImport { { $count = 0; - foreach ( $data as $record ) { - try { - $entity = $this->transformer->transform( $record ); + foreach ($data as $record) { + try { + $entity = $this->transformer->transform($record); - /** @var \App\Http\Requests\Request $request */ - $request = new $this->request_name(); + /** @var \App\Http\Requests\Request $request */ + $request = new $this->request_name(); - // Pass entity data to request so it can be validated - $request->query = $request->request = new ParameterBag( $entity ); - $validator = Validator::make( $entity, $request->rules() ); + // Pass entity data to request so it can be validated + $request->query = $request->request = new ParameterBag($entity); + $validator = Validator::make($entity, $request->rules()); - if ( $validator->fails() ) { - $this->error_array[ $entity_type ][] = - [ $entity_type => $record, 'error' => $validator->errors()->all() ]; - } else { - $entity = - $this->repository->save( - array_diff_key( $entity, [ 'user_id' => false ] ), - $this->factory_name::create( $this->company->id, $this->getUserIDForRecord( $entity ) ) ); + if ($validator->fails()) { + $this->error_array[$entity_type][] = [ + $entity_type => $record, + 'error' => $validator->errors()->all(), + ]; + } else { + $entity = $this->repository->save( + array_diff_key($entity, ['user_id' => false]), + $this->factory_name::create( + $this->company->id, + $this->getUserIDForRecord($entity) + ) + ); - $entity->saveQuietly(); - $count++; + $entity->saveQuietly(); + $count++; + } + } catch (\Exception $ex) { + if ($ex instanceof ImportException) { + $message = $ex->getMessage(); + } else { + report($ex); + $message = 'Unknown error'; + } - } - } catch ( \Exception $ex ) { - if ( $ex instanceof ImportException ) { - $message = $ex->getMessage(); - } else { - report( $ex ); - $message = 'Unknown error'; - } + $this->error_array[$entity_type][] = [ + $entity_type => $record, + 'error' => $message, + ]; + } - $this->error_array[ $entity_type ][] = [ $entity_type => $record, 'error' => $message ]; - } - - return $count; - } + return $count; + } } - public function ingestInvoices( $invoices , $invoice_number_key) { + public function ingestInvoices($invoices, $invoice_number_key) + { $invoice_transformer = $this->transformer; /** @var PaymentRepository $payment_repository */ - $payment_repository = app()->make( PaymentRepository::class ); + $payment_repository = app()->make(PaymentRepository::class); $payment_repository->import_mode = true; /** @var ClientRepository $client_repository */ - $client_repository = app()->make( ClientRepository::class ); + $client_repository = app()->make(ClientRepository::class); $client_repository->import_mode = true; - $invoice_repository = new InvoiceRepository(); + $invoice_repository = new InvoiceRepository(); $invoice_repository->import_mode = true; $invoices = $this->groupInvoices($invoices, $invoice_number_key); - foreach ( $invoices as $raw_invoice ) { + foreach ($invoices as $raw_invoice) { try { - - $invoice_data = $invoice_transformer->transform( $raw_invoice ); - $invoice_data['line_items'] = $this->cleanItems( $invoice_data['line_items'] ?? [] ); + $invoice_data = $invoice_transformer->transform($raw_invoice); + $invoice_data['line_items'] = $this->cleanItems( + $invoice_data['line_items'] ?? [] + ); // If we don't have a client ID, but we do have client data, go ahead and create the client. - if ( empty( $invoice_data['client_id'] ) && ! empty( $invoice_data['client'] ) ) { - $client_data = $invoice_data['client']; - $client_data['user_id'] = $this->getUserIDForRecord( $invoice_data ); + if ( + empty($invoice_data['client_id']) && + !empty($invoice_data['client']) + ) { + $client_data = $invoice_data['client']; + $client_data['user_id'] = $this->getUserIDForRecord( + $invoice_data + ); $client_repository->save( $client_data, - $client = ClientFactory::create( $this->company->id, $client_data['user_id'] ) + $client = ClientFactory::create( + $this->company->id, + $client_data['user_id'] + ) ); $invoice_data['client_id'] = $client->id; - unset( $invoice_data['client'] ); + unset($invoice_data['client']); } - $validator = Validator::make( $invoice_data, ( new StoreInvoiceRequest() )->rules() ); - if ( $validator->fails() ) { - $this->error_array['invoice'][] = - [ 'invoice' => $invoice_data, 'error' => $validator->errors()->all() ]; + $validator = Validator::make( + $invoice_data, + (new StoreInvoiceRequest())->rules() + ); + if ($validator->fails()) { + $this->error_array['invoice'][] = [ + 'invoice' => $invoice_data, + 'error' => $validator->errors()->all(), + ]; } else { - $invoice = InvoiceFactory::create( $this->company->id, $this->getUserIDForRecord( $invoice_data ) ); - if ( ! empty( $invoice_data['status_id'] ) ) { + $invoice = InvoiceFactory::create( + $this->company->id, + $this->getUserIDForRecord($invoice_data) + ); + if (!empty($invoice_data['status_id'])) { $invoice->status_id = $invoice_data['status_id']; } - $invoice_repository->save( $invoice_data, $invoice ); + $invoice_repository->save($invoice_data, $invoice); // If we're doing a generic CSV import, only import payment data if we're not importing a payment CSV. // If we're doing a platform-specific import, trust the platform to only return payment info if there's not a separate payment CSV. - if ( $this->import_type !== 'csv' || empty( $this->column_map['payment'] ) ) { + if ( + $this->import_type !== 'csv' || + empty($this->column_map['payment']) + ) { // Check for payment columns - if ( ! empty( $invoice_data['payments'] ) ) { - foreach ( $invoice_data['payments'] as $payment_data ) { - $payment_data['user_id'] = $invoice->user_id; - $payment_data['client_id'] = $invoice->client_id; - $payment_data['invoices'] = [ + if (!empty($invoice_data['payments'])) { + foreach ( + $invoice_data['payments'] + as $payment_data + ) { + $payment_data['user_id'] = $invoice->user_id; + $payment_data['client_id'] = + $invoice->client_id; + $payment_data['invoices'] = [ [ 'invoice_id' => $invoice->id, - 'amount' => $payment_data['amount'] ?? null, + 'amount' => + $payment_data['amount'] ?? null, ], ]; /* Make sure we don't apply any payments to invoices with a Zero Amount*/ - if($invoice->amount > 0) - { + if ($invoice->amount > 0) { $payment_repository->save( $payment_data, - PaymentFactory::create( $this->company->id, $invoice->user_id, $invoice->client_id ) + PaymentFactory::create( + $this->company->id, + $invoice->user_id, + $invoice->client_id + ) ); } } } } - $this->actionInvoiceStatus( $invoice, $invoice_data, $invoice_repository ); + $this->actionInvoiceStatus( + $invoice, + $invoice_data, + $invoice_repository + ); } - } catch ( \Exception $ex ) { - if ( $ex instanceof ImportException ) { + } catch (\Exception $ex) { + if ($ex instanceof ImportException) { $message = $ex->getMessage(); } else { - report( $ex ); + report($ex); $message = 'Unknown error'; } - $this->error_array['invoice'][] = [ 'invoice' => $raw_invoice, 'error' => $message ]; + $this->error_array['invoice'][] = [ + 'invoice' => $raw_invoice, + 'error' => $message, + ]; } } } - - - - - - - - - - - - - - - - - - - - - - - - - private function actionInvoiceStatus( $invoice, $invoice_data, $invoice_repository ) { - if ( ! empty( $invoice_data['archived'] ) ) { - $invoice_repository->archive( $invoice ); + private function actionInvoiceStatus( + $invoice, + $invoice_data, + $invoice_repository + ) { + if (!empty($invoice_data['archived'])) { + $invoice_repository->archive($invoice); $invoice->fresh(); } - if ( ! empty( $invoice_data['viewed'] ) ) { - $invoice = $invoice->service()->markViewed()->save(); + if (!empty($invoice_data['viewed'])) { + $invoice = $invoice + ->service() + ->markViewed() + ->save(); } - if( $invoice->status_id === Invoice::STATUS_DRAFT ){ - - } - elseif ( $invoice->status_id === Invoice::STATUS_SENT ) { - $invoice = $invoice->service()->markSent()->save(); - } - elseif ( $invoice->status_id <= Invoice::STATUS_SENT && $invoice->amount > 0 ) { - if ( $invoice->balance <= 0 ) { + if ($invoice->status_id === Invoice::STATUS_DRAFT) { + } elseif ($invoice->status_id === Invoice::STATUS_SENT) { + $invoice = $invoice + ->service() + ->markSent() + ->save(); + } elseif ( + $invoice->status_id <= Invoice::STATUS_SENT && + $invoice->amount > 0 + ) { + if ($invoice->balance <= 0) { $invoice->status_id = Invoice::STATUS_PAID; $invoice->save(); - } - elseif ( $invoice->balance != $invoice->amount ) { + } elseif ($invoice->balance != $invoice->amount) { $invoice->status_id = Invoice::STATUS_PARTIAL; $invoice->save(); - } + } } - return $invoice; } + protected function getUserIDForRecord($record) + { + if (!empty($record['user_id'])) { + return $this->findUser($record['user_id']); + } else { + return $this->company->owner()->id; + } + } + protected function findUser($user_hash) + { + $user = User::where('account_id', $this->company->account->id) + ->where( + \DB::raw('CONCAT_WS(" ", first_name, last_name)'), + 'like', + '%' . $user_hash . '%' + ) + ->first(); - protected function getUserIDForRecord( $record ) { - if ( ! empty( $record['user_id'] ) ) { - return $this->findUser( $record['user_id'] ); - } else { - return $this->company->owner()->id; - } - } - - protected function findUser( $user_hash ) { - $user = User::where( 'account_id', $this->company->account->id ) - ->where( \DB::raw( 'CONCAT_WS(" ", first_name, last_name)' ), 'like', '%' . $user_hash . '%' ) - ->first(); - - if ( $user ) { - return $user->id; - } else { - return $this->company->owner()->id; - } - } - + if ($user) { + return $user->id; + } else { + return $this->company->owner()->id; + } + } } diff --git a/app/Import/Providers/Csv.php b/app/Import/Providers/Csv.php index d7e0ffa73c24..ca68298276ba 100644 --- a/app/Import/Providers/Csv.php +++ b/app/Import/Providers/Csv.php @@ -30,29 +30,35 @@ use Symfony\Component\HttpFoundation\ParameterBag; class Csv extends BaseImport implements ImportInterface { - public array $entity_count = []; - public function import(string $entity) - { - - if(in_array($entity, [ 'client', 'product', 'invoice', 'payment', 'vendor', 'expense' ])) + public function import(string $entity) + { + if ( + in_array($entity, [ + 'client', + 'product', + 'invoice', + 'payment', + 'vendor', + 'expense', + ]) + ) { $this->{$entity}(); - + } + //collate any errors } - + private function client() { - $entity_type = 'client'; $data = $this->getCsvData($entity_type); $data = $this->preTransform($data, $entity_type); - if(empty($data)){ - + if (empty($data)) { $this->entity_count['clients'] = 0; return; } @@ -61,7 +67,7 @@ class Csv extends BaseImport implements ImportInterface $this->repository_name = ClientRepository::class; $this->factory_name = ClientFactory::class; - $this->repository = app()->make( $this->repository_name ); + $this->repository = app()->make($this->repository_name); $this->repository->import_mode = true; $this->transformer = new ClientTransformer($this->company); @@ -69,20 +75,17 @@ class Csv extends BaseImport implements ImportInterface $client_count = $this->ingest($data, $entity_type); $this->entity_count['clients'] = $client_count; - } private function product() { - $entity_type = 'product'; $data = $this->getCsvData($entity_type); $data = $this->preTransform($data, $entity_type); - if(empty($data)){ - + if (empty($data)) { $this->entity_count['products'] = 0; return; } @@ -91,7 +94,7 @@ class Csv extends BaseImport implements ImportInterface $this->repository_name = ProductRepository::class; $this->factory_name = ProductFactory::class; - $this->repository = app()->make( $this->repository_name ); + $this->repository = app()->make($this->repository_name); $this->repository->import_mode = true; $this->transformer = new ProductTransformer($this->company); @@ -99,20 +102,17 @@ class Csv extends BaseImport implements ImportInterface $product_count = $this->ingest($data, $entity_type); $this->entity_count['products'] = $product_count; - } private function invoice() { - $entity_type = 'invoice'; $data = $this->getCsvData($entity_type); $data = $this->preTransform($data, $entity_type); - if(empty($data)){ - + if (empty($data)) { $this->entity_count['invoices'] = 0; return; } @@ -121,47 +121,38 @@ class Csv extends BaseImport implements ImportInterface $this->repository_name = InvoiceRepository::class; $this->factory_name = InvoiceFactory::class; - $this->repository = app()->make( $this->repository_name ); + $this->repository = app()->make($this->repository_name); $this->repository->import_mode = true; $this->transformer = new InvoiceTransformer($this->company); - $invoice_count = $this->ingestInvoices($data, 'invoice.number'); + $invoice_count = $this->ingestInvoices($data); $this->entity_count['invoices'] = $invoice_count; - } - - public function preTransform(array $data, $entity_type) - { - - - if ( empty( $this->column_map[ $entity_type ] ) ) { + public function preTransform(array $data, $entity_type) + { + if (empty($this->column_map[$entity_type])) { return false; } - if ( $this->skip_header ) { - array_shift( $data ); + if ($this->skip_header) { + array_shift($data); } //sort the array by key - $keys = $this->column_map[ $entity_type ]; - ksort( $keys ); - - $data = array_map( function ( $row ) use ( $keys ) { - return array_combine( $keys, array_intersect_key( $row, $keys ) ); - }, $data ); + $keys = $this->column_map[$entity_type]; + ksort($keys); + $data = array_map(function ($row) use ($keys) { + return array_combine($keys, array_intersect_key($row, $keys)); + }, $data); return $data; - - } - public function transform(array $data) - { - + public function transform(array $data) + { } - -} \ No newline at end of file +} diff --git a/app/Import/Providers/Freshbooks.php b/app/Import/Providers/Freshbooks.php index 3f25795848f4..bb7759489141 100644 --- a/app/Import/Providers/Freshbooks.php +++ b/app/Import/Providers/Freshbooks.php @@ -12,5 +12,4 @@ namespace App\Import\Providers; class Freshbooks extends BaseImport { - -} \ No newline at end of file +} diff --git a/app/Import/Transformer/BaseTransformer.php b/app/Import/Transformer/BaseTransformer.php index 8476bb59b4b1..caa7bbdec8ee 100644 --- a/app/Import/Transformer/BaseTransformer.php +++ b/app/Import/Transformer/BaseTransformer.php @@ -25,7 +25,6 @@ use Illuminate\Support\Facades\Cache; */ class BaseTransformer { - protected $company; public function __construct($company) @@ -35,59 +34,64 @@ class BaseTransformer public function getString($data, $field) { - return (isset($data[$field]) && $data[$field]) ? $data[$field] : ''; + return isset($data[$field]) && $data[$field] ? $data[$field] : ''; } - public function getCurrencyByCode( $data, $key = 'client.currency_id' ) + public function getCurrencyByCode($data, $key = 'client.currency_id') { - - $code = array_key_exists( $key, $data ) ? $data[ $key ] : false; + $code = array_key_exists($key, $data) ? $data[$key] : false; $currencies = Cache::get('currencies'); - $currency = $currencies->filter(function ($item) use($code) { - return $item->code == $code; - })->first(); - - return $currency ? $currency->id : $this->company->settings->currency_id; + $currency = $currencies + ->filter(function ($item) use ($code) { + return $item->code == $code; + }) + ->first(); + return $currency + ? $currency->id + : $this->company->settings->currency_id; } - public function getClient($client_name, $client_email) + public function getClient($client_name, $client_email) { - // nlog("searching for {$client_name} with email {$client_email}"); - - $client_id_search = $this->company->clients()->where( 'id_number', $client_name ); - if ( $client_id_search->count() >= 1 ) { + $client_id_search = $this->company + ->clients() + ->where('id_number', $client_name); + + if ($client_id_search->count() >= 1) { // nlog("found via id number => {$client_id_search->first()->id}"); - return $client_id_search->first()->id; - } + return $client_id_search->first()->id; + } - $client_name_search = $this->company->clients()->where( 'name', $client_name ); + $client_name_search = $this->company + ->clients() + ->where('name', $client_name); - if ( $client_name_search->count() >= 1 ) { + if ($client_name_search->count() >= 1) { // nlog("found via name {$client_name_search->first()->id}"); return $client_name_search->first()->id; } - if ( ! empty( $client_email ) ) { - $contacts = ClientContact::where( 'company_id', $this->company->id ) - ->where( 'email', $client_email ); + if (!empty($client_email)) { + $contacts = ClientContact::where( + 'company_id', + $this->company->id + )->where('email', $client_email); - if ( $contacts->count() >= 1 ) { + if ($contacts->count() >= 1) { // nlog("found via contact {$contacts->first()->client_id}"); - return $contacts->first()->client_id; - } - } - + return $contacts->first()->client_id; + } + } + // nlog("did not find client"); - return null; - } - - + return null; + } /////////////////////////////////////////////////////////////////////////////////// /** @@ -97,7 +101,12 @@ class BaseTransformer */ public function hasClient($name) { - return $this->company->clients()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->exists(); + return $this->company + ->clients() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->exists(); } /** @@ -107,7 +116,12 @@ class BaseTransformer */ public function hasVendor($name) { - return $this->company->vendors()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->exists(); + return $this->company + ->vendors() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->exists(); } /** @@ -117,7 +131,12 @@ class BaseTransformer */ public function hasProduct($key) { - return $this->company->products()->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $key))])->exists(); + return $this->company + ->products() + ->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $key)), + ]) + ->exists(); } /** @@ -144,7 +163,12 @@ class BaseTransformer */ public function getClientId($name) { - $client = $this->company->clients()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + $client = $this->company + ->clients() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->first(); return $client ? $client->id : null; } @@ -156,14 +180,18 @@ class BaseTransformer */ public function getProduct($data, $key, $field, $default = false) { + $product = $this->company + ->products() + ->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $data->{$key})), + ]) + ->first(); - $product = $this->company->products()->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $data->{$key}))])->first(); - - if($product) + if ($product) { return $product->{$field} ?: $default; + } return $default; - } /** @@ -173,14 +201,18 @@ class BaseTransformer */ public function getContact($email) { - - $contact = $this->company->client_contacts()->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $email))])->first(); + $contact = $this->company + ->client_contacts() + ->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $email)), + ]) + ->first(); - if(!$contact) + if (!$contact) { return null; + } return $contact; - } /** @@ -190,10 +222,13 @@ class BaseTransformer */ public function getCountryId($name) { - if(strlen($name) == 2) + if (strlen($name) == 2) { return $this->getCountryIdBy2($name); + } - $country = Country::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + $country = Country::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ])->first(); return $country ? $country->id : null; } @@ -205,7 +240,9 @@ class BaseTransformer */ public function getCountryIdBy2($name) { - return Country::where('iso_3166_2', $name)->exists() ? Country::where('iso_3166_2', $name)->first()->id : null; + return Country::where('iso_3166_2', $name)->exists() + ? Country::where('iso_3166_2', $name)->first()->id + : null; } /** @@ -217,7 +254,12 @@ class BaseTransformer { $name = strtolower(trim($name)); - $tax_rate = $this->company->tax_rates()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + $tax_rate = $this->company + ->tax_rates() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->first(); return $tax_rate ? $tax_rate->rate : 0; } @@ -231,10 +273,14 @@ class BaseTransformer { $name = strtolower(trim($name)); - $tax_rate = $this->company->tax_rates()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + $tax_rate = $this->company + ->tax_rates() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->first(); return $tax_rate ? $tax_rate->name : ''; - } /** @@ -266,7 +312,7 @@ class BaseTransformer */ public function getInvoiceNumber($number) { - return $number ? ltrim( trim( $number ), '0' ) : null; + return $number ? ltrim(trim($number), '0') : null; } /** @@ -276,9 +322,14 @@ class BaseTransformer */ public function getInvoiceId($invoice_number) { - $invoice = $this->company->invoices()->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $invoice_number))])->first(); + $invoice = $this->company + ->invoices() + ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $invoice_number)), + ]) + ->first(); - return $invoice ? $invoice->id : null; + return $invoice ? $invoice->id : null; } /** @@ -288,9 +339,12 @@ class BaseTransformer */ public function hasInvoice($invoice_number) { - - return $this->company->invoices()->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $invoice_number))])->exists(); - + return $this->company + ->invoices() + ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $invoice_number)), + ]) + ->exists(); } /** @@ -300,7 +354,12 @@ class BaseTransformer */ public function getInvoiceClientId($invoice_number) { - $invoice = $this->company->invoices()->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $invoice_number))])->first(); + $invoice = $this->company + ->invoices() + ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $invoice_number)), + ]) + ->first(); return $invoice ? $invoice->client_id : null; } @@ -312,47 +371,61 @@ class BaseTransformer */ public function getVendorId($name) { - $vendor = $this->company->vendors()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + $vendor = $this->company + ->vendors() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->first(); return $vendor ? $vendor->id : null; } - /** - * @param $name - * - * @return int|null - */ - public function getExpenseCategoryId( $name ) { - - $ec = $this->company->expense_categories()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + /** + * @param $name + * + * @return int|null + */ + public function getExpenseCategoryId($name) + { + $ec = $this->company + ->expense_categories() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->first(); return $ec ? $ec->id : null; + } - } - - /** - * @param $name - * - * @return int|null - */ - public function getProjectId( $name ) { - - $project = $this->company->projects()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + /** + * @param $name + * + * @return int|null + */ + public function getProjectId($name) + { + $project = $this->company + ->projects() + ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ]) + ->first(); return $project ? $project->id : null; - } + } - /** - * @param $name - * - * @return int|null - */ - public function getPaymentTypeId( $name ) { - - $pt = PaymentType::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first(); + /** + * @param $name + * + * @return int|null + */ + public function getPaymentTypeId($name) + { + $pt = PaymentType::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ + strtolower(str_replace(' ', '', $name)), + ])->first(); return $pt ? $pt->id : null; - } - - + } } diff --git a/app/Import/Transformer/Csv/ClientTransformer.php b/app/Import/Transformer/Csv/ClientTransformer.php index 4df6591ff63d..3df03ad95e9f 100644 --- a/app/Import/Transformer/Csv/ClientTransformer.php +++ b/app/Import/Transformer/Csv/ClientTransformer.php @@ -19,64 +19,101 @@ use Illuminate\Support\Str; */ class ClientTransformer extends BaseTransformer { - /** + /** * @param $data * * @return array|bool */ - public function transform($data) - { - if (isset($data->name) && $this->hasClient($data->name)) { - throw new ImportException('Client already exists'); - } + public function transform($data) + { + if (isset($data->name) && $this->hasClient($data->name)) { + throw new ImportException('Client already exists'); + } - $settings = new \stdClass; - $settings->currency_id = (string)$this->getCurrencyByCode($data); + $settings = new \stdClass(); + $settings->currency_id = (string) $this->getCurrencyByCode($data); return [ - 'company_id' => $this->company->id, - 'name' => $this->getString( $data, 'client.name' ), - 'work_phone' => $this->getString( $data, 'client.phone' ), - 'address1' => $this->getString( $data, 'client.address1' ), - 'address2' => $this->getString( $data, 'client.address2' ), - 'postal_code' => $this->getString( $data, 'client.postal_code'), - 'city' => $this->getString( $data, 'client.city' ), - 'state' => $this->getString( $data, 'client.state' ), - 'shipping_address1' => $this->getString( $data, 'client.shipping_address1' ), - 'shipping_address2' => $this->getString( $data, 'client.shipping_address2' ), - 'shipping_city' => $this->getString( $data, 'client.shipping_city' ), - 'shipping_state' => $this->getString( $data, 'client.shipping_state' ), - 'shipping_postal_code' => $this->getString( $data, 'client.shipping_postal_code' ), - 'public_notes' => $this->getString( $data, 'client.public_notes' ), - 'private_notes' => $this->getString( $data, 'client.private_notes' ), - 'website' => $this->getString( $data, 'client.website' ), - 'vat_number' => $this->getString( $data, 'client.vat_number' ), - 'id_number' => $this->getString( $data, 'client.id_number' ), - 'custom_value1' => $this->getString( $data, 'client.custom_value1' ), - 'custom_value2' => $this->getString( $data, 'client.custom_value2' ), - 'custom_value3' => $this->getString( $data, 'client.custom_value3' ), - 'custom_value4' => $this->getString( $data, 'client.custom_value4' ), - 'balance' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.balance' ) ), - 'paid_to_date' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.paid_to_date' ) ), - 'credit_balance' => 0, - 'settings' => $settings, - 'client_hash' => Str::random( 40 ), - 'contacts' => [ + 'company_id' => $this->company->id, + 'name' => $this->getString($data, 'client.name'), + 'work_phone' => $this->getString($data, 'client.phone'), + 'address1' => $this->getString($data, 'client.address1'), + 'address2' => $this->getString($data, 'client.address2'), + 'postal_code' => $this->getString($data, 'client.postal_code'), + 'city' => $this->getString($data, 'client.city'), + 'state' => $this->getString($data, 'client.state'), + 'shipping_address1' => $this->getString( + $data, + 'client.shipping_address1' + ), + 'shipping_address2' => $this->getString( + $data, + 'client.shipping_address2' + ), + 'shipping_city' => $this->getString($data, 'client.shipping_city'), + 'shipping_state' => $this->getString( + $data, + 'client.shipping_state' + ), + 'shipping_postal_code' => $this->getString( + $data, + 'client.shipping_postal_code' + ), + 'public_notes' => $this->getString($data, 'client.public_notes'), + 'private_notes' => $this->getString($data, 'client.private_notes'), + 'website' => $this->getString($data, 'client.website'), + 'vat_number' => $this->getString($data, 'client.vat_number'), + 'id_number' => $this->getString($data, 'client.id_number'), + 'custom_value1' => $this->getString($data, 'client.custom_value1'), + 'custom_value2' => $this->getString($data, 'client.custom_value2'), + 'custom_value3' => $this->getString($data, 'client.custom_value3'), + 'custom_value4' => $this->getString($data, 'client.custom_value4'), + 'balance' => preg_replace( + '/[^0-9,.]+/', + '', + $this->getFloat($data, 'client.balance') + ), + 'paid_to_date' => preg_replace( + '/[^0-9,.]+/', + '', + $this->getFloat($data, 'client.paid_to_date') + ), + 'credit_balance' => 0, + 'settings' => $settings, + 'client_hash' => Str::random(40), + 'contacts' => [ [ - 'first_name' => $this->getString( $data, 'contact.first_name' ), - 'last_name' => $this->getString( $data, 'contact.last_name' ), - 'email' => $this->getString( $data, 'contact.email' ), - 'phone' => $this->getString( $data, 'contact.phone' ), - 'custom_value1' => $this->getString( $data, 'contact.custom_value1' ), - 'custom_value2' => $this->getString( $data, 'contact.custom_value2' ), - 'custom_value3' => $this->getString( $data, 'contact.custom_value3' ), - 'custom_value4' => $this->getString( $data, 'contact.custom_value4' ), + 'first_name' => $this->getString( + $data, + 'contact.first_name' + ), + 'last_name' => $this->getString($data, 'contact.last_name'), + 'email' => $this->getString($data, 'contact.email'), + 'phone' => $this->getString($data, 'contact.phone'), + 'custom_value1' => $this->getString( + $data, + 'contact.custom_value1' + ), + 'custom_value2' => $this->getString( + $data, + 'contact.custom_value2' + ), + 'custom_value3' => $this->getString( + $data, + 'contact.custom_value3' + ), + 'custom_value4' => $this->getString( + $data, + 'contact.custom_value4' + ), ], ], - 'country_id' => isset( $data['client.country'] ) ? $this->getCountryId( $data['client.country']) : null, - 'shipping_country_id' => isset($data['client.shipping_country'] ) ? $this->getCountryId( $data['client.shipping_country'] ) : null, + 'country_id' => isset($data['client.country']) + ? $this->getCountryId($data['client.country']) + : null, + 'shipping_country_id' => isset($data['client.shipping_country']) + ? $this->getCountryId($data['client.shipping_country']) + : null, ]; - } + } } - - diff --git a/app/Import/Transformer/Csv/InvoiceTransformer.php b/app/Import/Transformer/Csv/InvoiceTransformer.php index fb683ac32737..992ad63f2e5b 100644 --- a/app/Import/Transformer/Csv/InvoiceTransformer.php +++ b/app/Import/Transformer/Csv/InvoiceTransformer.php @@ -19,121 +19,221 @@ use App\Models\Invoice; /** * Class InvoiceTransformer. */ -class InvoiceTransformer extends BaseTransformer { - - +class InvoiceTransformer extends BaseTransformer +{ /** * @param $data * * @return bool|array */ - public function transform( $line_items_data ) { + public function transform($line_items_data) + { + $invoice_data = reset($line_items_data); - $invoice_data = reset( $line_items_data ); - - if ( $this->hasInvoice( $invoice_data['invoice.number'] ) ) { - throw new ImportException( 'Invoice number already exists' ); + if ($this->hasInvoice($invoice_data['invoice.number'])) { + throw new ImportException('Invoice number already exists'); } $invoiceStatusMap = [ - 'sent' => Invoice::STATUS_SENT, + 'sent' => Invoice::STATUS_SENT, 'draft' => Invoice::STATUS_DRAFT, ]; $transformed = [ - 'company_id' => $this->company->id, - 'number' => $this->getString( $invoice_data, 'invoice.number' ), - 'user_id' => $this->getString( $invoice_data, 'invoice.user_id' ), - 'amount' => $amount = $this->getFloat( $invoice_data, 'invoice.amount' ), - 'balance' => isset( $invoice_data['invoice.balance'] ) ? $this->getFloat( $invoice_data, 'invoice.balance' ) : $amount, - 'client_id' => $this->getClient( $this->getString( $invoice_data, 'client.name' ), $this->getString( $invoice_data, 'client.email' ) ), - 'discount' => $this->getFloat( $invoice_data, 'invoice.discount' ), - 'po_number' => $this->getString( $invoice_data, 'invoice.po_number' ), - 'date' => isset( $invoice_data['invoice.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['invoice.date'] ) ) : now()->format('Y-m-d'), - 'due_date' => isset( $invoice_data['invoice.due_date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['invoice.due_date'] ) ) : null, - 'terms' => $this->getString( $invoice_data, 'invoice.terms' ), - 'public_notes' => $this->getString( $invoice_data, 'invoice.public_notes' ), - 'private_notes' => $this->getString( $invoice_data, 'invoice.private_notes' ), - 'tax_name1' => $this->getString( $invoice_data, 'invoice.tax_name1' ), - 'tax_rate1' => $this->getFloat( $invoice_data, 'invoice.tax_rate1' ), - 'tax_name2' => $this->getString( $invoice_data, 'invoice.tax_name2' ), - 'tax_rate2' => $this->getFloat( $invoice_data, 'invoice.tax_rate2' ), - 'tax_name3' => $this->getString( $invoice_data, 'invoice.tax_name3' ), - 'tax_rate3' => $this->getFloat( $invoice_data, 'invoice.tax_rate3' ), - 'custom_value1' => $this->getString( $invoice_data, 'invoice.custom_value1' ), - 'custom_value2' => $this->getString( $invoice_data, 'invoice.custom_value2' ), - 'custom_value3' => $this->getString( $invoice_data, 'invoice.custom_value3' ), - 'custom_value4' => $this->getString( $invoice_data, 'invoice.custom_value4' ), - 'footer' => $this->getString( $invoice_data, 'invoice.footer' ), - 'partial' => $this->getFloat( $invoice_data, 'invoice.partial' ), - 'partial_due_date' => $this->getString( $invoice_data, 'invoice.partial_due_date' ), - 'custom_surcharge1' => $this->getString( $invoice_data, 'invoice.custom_surcharge1' ), - 'custom_surcharge2' => $this->getString( $invoice_data, 'invoice.custom_surcharge2' ), - 'custom_surcharge3' => $this->getString( $invoice_data, 'invoice.custom_surcharge3' ), - 'custom_surcharge4' => $this->getString( $invoice_data, 'invoice.custom_surcharge4' ), - 'exchange_rate' => $this->getString( $invoice_data, 'invoice.exchange_rate' ), - 'status_id' => $invoiceStatusMap[ $status = - strtolower( $this->getString( $invoice_data, 'invoice.status' ) ) ] ?? - Invoice::STATUS_SENT, - 'archived' => $status === 'archived', + 'company_id' => $this->company->id, + 'number' => $this->getString($invoice_data, 'invoice.number'), + 'user_id' => $this->getString($invoice_data, 'invoice.user_id'), + 'amount' => ($amount = $this->getFloat( + $invoice_data, + 'invoice.amount' + )), + 'balance' => isset($invoice_data['invoice.balance']) + ? $this->getFloat($invoice_data, 'invoice.balance') + : $amount, + 'client_id' => $this->getClient( + $this->getString($invoice_data, 'client.name'), + $this->getString($invoice_data, 'client.email') + ), + 'discount' => $this->getFloat($invoice_data, 'invoice.discount'), + 'po_number' => $this->getString($invoice_data, 'invoice.po_number'), + 'date' => isset($invoice_data['invoice.date']) + ? date('Y-m-d', strtotime($invoice_data['invoice.date'])) + : now()->format('Y-m-d'), + 'due_date' => isset($invoice_data['invoice.due_date']) + ? date('Y-m-d', strtotime($invoice_data['invoice.due_date'])) + : null, + 'terms' => $this->getString($invoice_data, 'invoice.terms'), + 'public_notes' => $this->getString( + $invoice_data, + 'invoice.public_notes' + ), + 'private_notes' => $this->getString( + $invoice_data, + 'invoice.private_notes' + ), + 'tax_name1' => $this->getString($invoice_data, 'invoice.tax_name1'), + 'tax_rate1' => $this->getFloat($invoice_data, 'invoice.tax_rate1'), + 'tax_name2' => $this->getString($invoice_data, 'invoice.tax_name2'), + 'tax_rate2' => $this->getFloat($invoice_data, 'invoice.tax_rate2'), + 'tax_name3' => $this->getString($invoice_data, 'invoice.tax_name3'), + 'tax_rate3' => $this->getFloat($invoice_data, 'invoice.tax_rate3'), + 'custom_value1' => $this->getString( + $invoice_data, + 'invoice.custom_value1' + ), + 'custom_value2' => $this->getString( + $invoice_data, + 'invoice.custom_value2' + ), + 'custom_value3' => $this->getString( + $invoice_data, + 'invoice.custom_value3' + ), + 'custom_value4' => $this->getString( + $invoice_data, + 'invoice.custom_value4' + ), + 'footer' => $this->getString($invoice_data, 'invoice.footer'), + 'partial' => $this->getFloat($invoice_data, 'invoice.partial'), + 'partial_due_date' => $this->getString( + $invoice_data, + 'invoice.partial_due_date' + ), + 'custom_surcharge1' => $this->getString( + $invoice_data, + 'invoice.custom_surcharge1' + ), + 'custom_surcharge2' => $this->getString( + $invoice_data, + 'invoice.custom_surcharge2' + ), + 'custom_surcharge3' => $this->getString( + $invoice_data, + 'invoice.custom_surcharge3' + ), + 'custom_surcharge4' => $this->getString( + $invoice_data, + 'invoice.custom_surcharge4' + ), + 'exchange_rate' => $this->getString( + $invoice_data, + 'invoice.exchange_rate' + ), + 'status_id' => + $invoiceStatusMap[ + ($status = strtolower( + $this->getString($invoice_data, 'invoice.status') + )) + ] ?? Invoice::STATUS_SENT, + 'archived' => $status === 'archived', ]; /* If we can't find the client, then lets try and create a client */ - if(!$transformed['client_id']){ - + if (!$transformed['client_id']) { $client_transformer = new ClientTransformer($this->company); - $transformed['client'] = $client_transformer->transform($invoice_data); - + $transformed['client'] = $client_transformer->transform( + $invoice_data + ); } - - if ( isset( $invoice_data['payment.amount'] ) ) { + if (isset($invoice_data['payment.amount'])) { $transformed['payments'] = [ [ - 'date' => isset( $invoice_data['payment.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['payment.date'] ) ) : date( 'y-m-d' ), - 'transaction_reference' => $this->getString( $invoice_data, 'payment.transaction_reference' ), - 'amount' => $this->getFloat( $invoice_data, 'payment.amount' ), + 'date' => isset($invoice_data['payment.date']) + ? date( + 'Y-m-d', + strtotime($invoice_data['payment.date']) + ) + : date('y-m-d'), + 'transaction_reference' => $this->getString( + $invoice_data, + 'payment.transaction_reference' + ), + 'amount' => $this->getFloat( + $invoice_data, + 'payment.amount' + ), ], ]; - } elseif ( $status === 'paid' ) { + } elseif ($status === 'paid') { $transformed['payments'] = [ [ - 'date' => isset( $invoice_data['payment.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['payment.date'] ) ) : date( 'y-m-d' ), - 'transaction_reference' => $this->getString( $invoice_data, 'payment.transaction_reference' ), - 'amount' => $this->getFloat( $invoice_data, 'invoice.amount' ), + 'date' => isset($invoice_data['payment.date']) + ? date( + 'Y-m-d', + strtotime($invoice_data['payment.date']) + ) + : date('y-m-d'), + 'transaction_reference' => $this->getString( + $invoice_data, + 'payment.transaction_reference' + ), + 'amount' => $this->getFloat( + $invoice_data, + 'invoice.amount' + ), ], ]; - } elseif ( isset( $transformed['amount'] ) && isset( $transformed['balance'] ) && ($transformed['amount'] != $transformed['balance'])) { + } elseif ( + isset($transformed['amount']) && + isset($transformed['balance']) && + $transformed['amount'] != $transformed['balance'] + ) { $transformed['payments'] = [ [ - 'date' => isset( $invoice_data['payment.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['payment.date'] ) ) : date( 'y-m-d' ), - 'transaction_reference' => $this->getString( $invoice_data, 'payment.transaction_reference' ), - 'amount' => $transformed['amount'] - $transformed['balance'], + 'date' => isset($invoice_data['payment.date']) + ? date( + 'Y-m-d', + strtotime($invoice_data['payment.date']) + ) + : date('y-m-d'), + 'transaction_reference' => $this->getString( + $invoice_data, + 'payment.transaction_reference' + ), + 'amount' => + $transformed['amount'] - $transformed['balance'], ], ]; } $line_items = []; - foreach ( $line_items_data as $record ) { + foreach ($line_items_data as $record) { $line_items[] = [ - 'quantity' => $this->getFloat( $record, 'item.quantity' ), - 'cost' => $this->getFloat( $record, 'item.cost' ), - 'product_key' => $this->getString( $record, 'item.product_key' ), - 'notes' => $this->getString( $record, 'item.notes' ), - 'discount' => $this->getFloat( $record, 'item.discount' ), - 'is_amount_discount' => filter_var( $this->getString( $record, 'item.is_amount_discount' ), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ), - 'tax_name1' => $this->getString( $record, 'item.tax_name1' ), - 'tax_rate1' => $this->getFloat( $record, 'item.tax_rate1' ), - 'tax_name2' => $this->getString( $record, 'item.tax_name2' ), - 'tax_rate2' => $this->getFloat( $record, 'item.tax_rate2' ), - 'tax_name3' => $this->getString( $record, 'item.tax_name3' ), - 'tax_rate3' => $this->getFloat( $record, 'item.tax_rate3' ), - 'custom_value1' => $this->getString( $record, 'item.custom_value1' ), - 'custom_value2' => $this->getString( $record, 'item.custom_value2' ), - 'custom_value3' => $this->getString( $record, 'item.custom_value3' ), - 'custom_value4' => $this->getString( $record, 'item.custom_value4' ), - 'type_id' => "1", //$this->getInvoiceTypeId( $record, 'item.type_id' ), + 'quantity' => $this->getFloat($record, 'item.quantity'), + 'cost' => $this->getFloat($record, 'item.cost'), + 'product_key' => $this->getString($record, 'item.product_key'), + 'notes' => $this->getString($record, 'item.notes'), + 'discount' => $this->getFloat($record, 'item.discount'), + 'is_amount_discount' => filter_var( + $this->getString($record, 'item.is_amount_discount'), + FILTER_VALIDATE_BOOLEAN, + FILTER_NULL_ON_FAILURE + ), + 'tax_name1' => $this->getString($record, 'item.tax_name1'), + 'tax_rate1' => $this->getFloat($record, 'item.tax_rate1'), + 'tax_name2' => $this->getString($record, 'item.tax_name2'), + 'tax_rate2' => $this->getFloat($record, 'item.tax_rate2'), + 'tax_name3' => $this->getString($record, 'item.tax_name3'), + 'tax_rate3' => $this->getFloat($record, 'item.tax_rate3'), + 'custom_value1' => $this->getString( + $record, + 'item.custom_value1' + ), + 'custom_value2' => $this->getString( + $record, + 'item.custom_value2' + ), + 'custom_value3' => $this->getString( + $record, + 'item.custom_value3' + ), + 'custom_value4' => $this->getString( + $record, + 'item.custom_value4' + ), + 'type_id' => '1', //$this->getInvoiceTypeId( $record, 'item.type_id' ), ]; } $transformed['line_items'] = $line_items; diff --git a/app/Import/Transformer/Csv/PaymentTransformer.php b/app/Import/Transformer/Csv/PaymentTransformer.php index b1264d938f2a..22ec6d349112 100644 --- a/app/Import/Transformer/Csv/PaymentTransformer.php +++ b/app/Import/Transformer/Csv/PaymentTransformer.php @@ -17,44 +17,52 @@ use App\Import\Transformer\BaseTransformer; /** * Class PaymentTransformer. */ -class PaymentTransformer extends BaseTransformer { +class PaymentTransformer extends BaseTransformer +{ /** * @param $data * * @return array */ - public function transform( $data ) { - - $client_id = $this->getClient( $this->getString( $data, 'payment.client_id' ), $this->getString( $data, 'payment.client_id' ) ); + public function transform($data) + { + $client_id = $this->getClient( + $this->getString($data, 'payment.client_id'), + $this->getString($data, 'payment.client_id') + ); - if ( empty( $client_id ) ) { - throw new ImportException( 'Could not find client.' ); + if (empty($client_id)) { + throw new ImportException('Could not find client.'); } $transformed = [ - 'company_id' => $this->maps['company']->id, - 'number' => $this->getString( $data, 'payment.number' ), - 'user_id' => $this->getString( $data, 'payment.user_id' ), - 'amount' => $this->getFloat( $data, 'payment.amount' ), - 'refunded' => $this->getFloat( $data, 'payment.refunded' ), - 'applied' => $this->getFloat( $data, 'payment.applied' ), - 'transaction_reference' => $this->getString( $data, 'payment.transaction_reference ' ), - 'date' => $this->getString( $data, 'payment.date' ), - 'private_notes' => $this->getString( $data, 'payment.private_notes' ), - 'custom_value1' => $this->getString( $data, 'payment.custom_value1' ), - 'custom_value2' => $this->getString( $data, 'payment.custom_value2' ), - 'custom_value3' => $this->getString( $data, 'payment.custom_value3' ), - 'custom_value4' => $this->getString( $data, 'payment.custom_value4' ), - 'client_id' => $client_id, + 'company_id' => $this->maps['company']->id, + 'number' => $this->getString($data, 'payment.number'), + 'user_id' => $this->getString($data, 'payment.user_id'), + 'amount' => $this->getFloat($data, 'payment.amount'), + 'refunded' => $this->getFloat($data, 'payment.refunded'), + 'applied' => $this->getFloat($data, 'payment.applied'), + 'transaction_reference' => $this->getString( + $data, + 'payment.transaction_reference ' + ), + 'date' => $this->getString($data, 'payment.date'), + 'private_notes' => $this->getString($data, 'payment.private_notes'), + 'custom_value1' => $this->getString($data, 'payment.custom_value1'), + 'custom_value2' => $this->getString($data, 'payment.custom_value2'), + 'custom_value3' => $this->getString($data, 'payment.custom_value3'), + 'custom_value4' => $this->getString($data, 'payment.custom_value4'), + 'client_id' => $client_id, ]; - - if ( isset( $data['payment.invoice_number'] ) && - $invoice_id = $this->getInvoiceId( $data['payment.invoice_number'] ) ) { + if ( + isset($data['payment.invoice_number']) && + ($invoice_id = $this->getInvoiceId($data['payment.invoice_number'])) + ) { $transformed['invoices'] = [ [ 'invoice_id' => $invoice_id, - 'amount' => $transformed['amount'] ?? null, + 'amount' => $transformed['amount'] ?? null, ], ]; } diff --git a/app/Import/Transformers/Csv/ClientTransformer.php b/app/Import/Transformers/Csv/ClientTransformer.php index 0ea9401a9e55..753ced94a14b 100644 --- a/app/Import/Transformers/Csv/ClientTransformer.php +++ b/app/Import/Transformers/Csv/ClientTransformer.php @@ -19,62 +19,101 @@ use Illuminate\Support\Str; */ class ClientTransformer extends BaseTransformer { - /** + /** * @param $data * * @return array|bool */ - public function transform($data) - { - if (isset($data->name) && $this->hasClient($data->name)) { - throw new ImportException('Client already exists'); - } + public function transform($data) + { + if (isset($data->name) && $this->hasClient($data->name)) { + throw new ImportException('Client already exists'); + } - $settings = new \stdClass; - $settings->currency_id = (string)$this->getCurrencyByCode($data); + $settings = new \stdClass(); + $settings->currency_id = (string) $this->getCurrencyByCode($data); return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString( $data, 'client.name' ), - 'work_phone' => $this->getString( $data, 'client.phone' ), - 'address1' => $this->getString( $data, 'client.address1' ), - 'address2' => $this->getString( $data, 'client.address2' ), - 'postal_code' => $this->getString( $data, 'client.postal_code'), - 'city' => $this->getString( $data, 'client.city' ), - 'state' => $this->getString( $data, 'client.state' ), - 'shipping_address1' => $this->getString( $data, 'client.shipping_address1' ), - 'shipping_address2' => $this->getString( $data, 'client.shipping_address2' ), - 'shipping_city' => $this->getString( $data, 'client.shipping_city' ), - 'shipping_state' => $this->getString( $data, 'client.shipping_state' ), - 'shipping_postal_code' => $this->getString( $data, 'client.shipping_postal_code' ), - 'public_notes' => $this->getString( $data, 'client.public_notes' ), - 'private_notes' => $this->getString( $data, 'client.private_notes' ), - 'website' => $this->getString( $data, 'client.website' ), - 'vat_number' => $this->getString( $data, 'client.vat_number' ), - 'id_number' => $this->getString( $data, 'client.id_number' ), - 'custom_value1' => $this->getString( $data, 'client.custom_value1' ), - 'custom_value2' => $this->getString( $data, 'client.custom_value2' ), - 'custom_value3' => $this->getString( $data, 'client.custom_value3' ), - 'custom_value4' => $this->getString( $data, 'client.custom_value4' ), - 'balance' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.balance' ) ), - 'paid_to_date' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.paid_to_date' ) ), - 'credit_balance' => 0, - 'settings' => $settings, - 'client_hash' => Str::random( 40 ), - 'contacts' => [ + 'company_id' => $this->maps['company']->id, + 'name' => $this->getString($data, 'client.name'), + 'work_phone' => $this->getString($data, 'client.phone'), + 'address1' => $this->getString($data, 'client.address1'), + 'address2' => $this->getString($data, 'client.address2'), + 'postal_code' => $this->getString($data, 'client.postal_code'), + 'city' => $this->getString($data, 'client.city'), + 'state' => $this->getString($data, 'client.state'), + 'shipping_address1' => $this->getString( + $data, + 'client.shipping_address1' + ), + 'shipping_address2' => $this->getString( + $data, + 'client.shipping_address2' + ), + 'shipping_city' => $this->getString($data, 'client.shipping_city'), + 'shipping_state' => $this->getString( + $data, + 'client.shipping_state' + ), + 'shipping_postal_code' => $this->getString( + $data, + 'client.shipping_postal_code' + ), + 'public_notes' => $this->getString($data, 'client.public_notes'), + 'private_notes' => $this->getString($data, 'client.private_notes'), + 'website' => $this->getString($data, 'client.website'), + 'vat_number' => $this->getString($data, 'client.vat_number'), + 'id_number' => $this->getString($data, 'client.id_number'), + 'custom_value1' => $this->getString($data, 'client.custom_value1'), + 'custom_value2' => $this->getString($data, 'client.custom_value2'), + 'custom_value3' => $this->getString($data, 'client.custom_value3'), + 'custom_value4' => $this->getString($data, 'client.custom_value4'), + 'balance' => preg_replace( + '/[^0-9,.]+/', + '', + $this->getFloat($data, 'client.balance') + ), + 'paid_to_date' => preg_replace( + '/[^0-9,.]+/', + '', + $this->getFloat($data, 'client.paid_to_date') + ), + 'credit_balance' => 0, + 'settings' => $settings, + 'client_hash' => Str::random(40), + 'contacts' => [ [ - 'first_name' => $this->getString( $data, 'contact.first_name' ), - 'last_name' => $this->getString( $data, 'contact.last_name' ), - 'email' => $this->getString( $data, 'contact.email' ), - 'phone' => $this->getString( $data, 'contact.phone' ), - 'custom_value1' => $this->getString( $data, 'contact.custom_value1' ), - 'custom_value2' => $this->getString( $data, 'contact.custom_value2' ), - 'custom_value3' => $this->getString( $data, 'contact.custom_value3' ), - 'custom_value4' => $this->getString( $data, 'contact.custom_value4' ), + 'first_name' => $this->getString( + $data, + 'contact.first_name' + ), + 'last_name' => $this->getString($data, 'contact.last_name'), + 'email' => $this->getString($data, 'contact.email'), + 'phone' => $this->getString($data, 'contact.phone'), + 'custom_value1' => $this->getString( + $data, + 'contact.custom_value1' + ), + 'custom_value2' => $this->getString( + $data, + 'contact.custom_value2' + ), + 'custom_value3' => $this->getString( + $data, + 'contact.custom_value3' + ), + 'custom_value4' => $this->getString( + $data, + 'contact.custom_value4' + ), ], ], - 'country_id' => isset( $data['client.country'] ) ? $this->getCountryId( $data['client.country']) : null, - 'shipping_country_id' => isset($data['client.shipping_country'] ) ? $this->getCountryId( $data['client.shipping_country'] ) : null, + 'country_id' => isset($data['client.country']) + ? $this->getCountryId($data['client.country']) + : null, + 'shipping_country_id' => isset($data['client.shipping_country']) + ? $this->getCountryId($data['client.shipping_country']) + : null, ]; - } + } }