From c1edc905dbb1a44d5c948ac48cea98d0aeca8379 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 20 Dec 2020 20:02:10 +1100 Subject: [PATCH] Importing invoices --- .../Requests/Invoice/StoreInvoiceRequest.php | 8 +------- app/Import/Transformers/BaseTransformer.php | 19 ++++++++++--------- app/Jobs/Import/CSVImport.php | 16 ++++------------ app/Utils/Number.php | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index 147d2bf0eb..9163f2fb9d 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -52,13 +52,7 @@ class StoreInvoiceRequest extends Request $rules['invitations.*.client_contact_id'] = 'distinct'; - // if ($this->input('number')) { - // $rules['number'] = 'unique:invoices,number,'.$this->id.',id,company_id,'.auth()->user()->company()->id; - // } - if ($this->input('number')) { - $rules['number'] = Rule::unique('invoices')->where('company_id', auth()->user()->company()->id); - } - + $rules['number'] = ['nullable',Rule::unique('invoices')->where('company_id', auth()->user()->company()->id)]; $rules['project_id'] = ['bail', 'sometimes', new ValidProjectForClient($this->all())]; diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index 19c9cbc6ec..404fad114a 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -12,6 +12,7 @@ namespace App\Import\Transformers; use App\Models\ClientContact; +use App\Utils\Number; use Carbon; use Exception; @@ -68,8 +69,7 @@ class BaseTransformer public function getClient($client_name, $client_email) { - info("search for {$client_name}"); - + $clients = $this->maps['company']->clients; $clients = $clients->where('name', $client_name); @@ -81,13 +81,8 @@ class BaseTransformer $contacts = ClientContact::where('company_id', $this->maps['company']->id) ->where('email', $client_email); - info("search for {$client_email}"); - if($contacts->count() >=1) return $contacts->first()->client_id; - - - info("no client found!!"); return NULL; @@ -152,8 +147,14 @@ class BaseTransformer * @return float */ public function getFloat($data, $field) - { - return (isset($data->$field) && $data->$field) ? Utils::parseFloat($data->$field) : 0; + { + + if(array_key_exists($field, $data)) + $number = preg_replace('/[^0-9-.]+/', '', $data[$field]); + else + $number = 0; + + return Number::parseStringFloat($number); } /** diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index 9a70d1f1d8..0381830b23 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -31,6 +31,7 @@ use App\Repositories\ClientContactRepository; use App\Repositories\ClientRepository; use App\Repositories\InvoiceRepository; use App\Repositories\ProductRepository; +use App\Utils\Traits\CleanLineItems; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -44,7 +45,7 @@ use League\Csv\Statement; class CSVImport implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, CleanLineItems; public $invoice; @@ -118,16 +119,10 @@ class CSVImport implements ShouldQueue $invoice_transformer = new InvoiceTransformer($this->maps); - info("import invoices"); - - info("column_map"); - $records = $this->getCsvData(); $invoice_number_key = array_search('Invoice Number', reset($records)); - info("number key = {$invoice_number_key}"); - if ($this->skip_header) array_shift($records); @@ -146,7 +141,6 @@ class CSVImport implements ShouldQueue foreach($unique_invoices as $unique) { - info("inside item loop {$unique}"); $invoices = array_filter($records, function($value) use($invoice_number_key, $unique){ return $value[$invoice_number_key] == $unique; @@ -181,7 +175,7 @@ class CSVImport implements ShouldQueue } - $invoice['line_items'] = $items; + $invoice['line_items'] = $this->cleanItems($items); $validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules()); @@ -235,8 +229,6 @@ class CSVImport implements ShouldQueue //clients $records = $this->getCsvData(); -info(print_r($this->column_map,1)); - $contact_repository = new ClientContactRepository(); $client_repository = new ClientRepository($contact_repository); $client_transformer = new ClientTransformer($this->maps); @@ -278,7 +270,7 @@ info(print_r($this->column_map,1)); private function importProduct() { - info("importing products"); + $product_repository = new ProductRepository(); $product_transformer = new ProductTransformer($this->maps); diff --git a/app/Utils/Number.php b/app/Utils/Number.php index 6d6c85c49f..46e2d9943d 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -70,6 +70,20 @@ class Number return (float) $s; } + public static function parseStringFloat($value) + { + $value = preg_replace('/[^0-9-.]+/', '', $value); + + // check for comma as decimal separator + if (preg_match('/,[\d]{1,2}$/', $value)) { + $value = str_replace(',', '.', $value); + } + + $value = preg_replace('/[^0-9\.\-]/', '', $value); + + return floatval($value); + } + /** * Formats a given value based on the clients currency AND country. *