1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Importing invoices

This commit is contained in:
David Bomba 2020-12-20 20:02:10 +11:00
parent ccde0eaced
commit c1edc905db
4 changed files with 29 additions and 28 deletions

View File

@ -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())];

View File

@ -12,6 +12,7 @@
namespace App\Import\Transformers;
use App\Models\ClientContact;
use App\Utils\Number;
use Carbon;
use Exception;
@ -68,7 +69,6 @@ class BaseTransformer
public function getClient($client_name, $client_email)
{
info("search for {$client_name}");
$clients = $this->maps['company']->clients;
@ -81,14 +81,9 @@ 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;
}
@ -153,7 +148,13 @@ class BaseTransformer
*/
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);
}
/**

View File

@ -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);

View File

@ -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.
*