mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Expense import
This commit is contained in:
parent
4115a6837b
commit
6231f8bd20
@ -142,8 +142,11 @@ class BaseImport
|
|||||||
{
|
{
|
||||||
$count = 0;
|
$count = 0;
|
||||||
|
|
||||||
foreach ($data as $record) {
|
nlog("importing ".count($data) ." ". $entity_type);
|
||||||
|
|
||||||
|
foreach ($data as $key => $record) {
|
||||||
try {
|
try {
|
||||||
|
nlog("importing {$key}");
|
||||||
$entity = $this->transformer->transform($record);
|
$entity = $this->transformer->transform($record);
|
||||||
|
|
||||||
/** @var \App\Http\Requests\Request $request */
|
/** @var \App\Http\Requests\Request $request */
|
||||||
@ -169,8 +172,11 @@ class BaseImport
|
|||||||
|
|
||||||
$entity->saveQuietly();
|
$entity->saveQuietly();
|
||||||
$count++;
|
$count++;
|
||||||
|
|
||||||
|
nlog("finished importing {$key}");
|
||||||
}
|
}
|
||||||
} catch (\Exception $ex) {
|
} catch (\Exception $ex) {
|
||||||
|
nlog("caught exception for {$key}");
|
||||||
if ($ex instanceof ImportException) {
|
if ($ex instanceof ImportException) {
|
||||||
$message = $ex->getMessage();
|
$message = $ex->getMessage();
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,14 +8,17 @@
|
|||||||
*
|
*
|
||||||
* @license https://www.elastic.co/licensing/elastic-license
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace App\Import\Providers;
|
namespace App\Import\Providers;
|
||||||
|
|
||||||
use App\Factory\ClientFactory;
|
use App\Factory\ClientFactory;
|
||||||
|
use App\Factory\ExpenseFactory;
|
||||||
use App\Factory\InvoiceFactory;
|
use App\Factory\InvoiceFactory;
|
||||||
use App\Factory\PaymentFactory;
|
use App\Factory\PaymentFactory;
|
||||||
use App\Factory\ProductFactory;
|
use App\Factory\ProductFactory;
|
||||||
use App\Factory\VendorFactory;
|
use App\Factory\VendorFactory;
|
||||||
use App\Http\Requests\Client\StoreClientRequest;
|
use App\Http\Requests\Client\StoreClientRequest;
|
||||||
|
use App\Http\Requests\Expense\StoreExpenseRequest;
|
||||||
use App\Http\Requests\Invoice\StoreInvoiceRequest;
|
use App\Http\Requests\Invoice\StoreInvoiceRequest;
|
||||||
use App\Http\Requests\Payment\StorePaymentRequest;
|
use App\Http\Requests\Payment\StorePaymentRequest;
|
||||||
use App\Http\Requests\Product\StoreProductRequest;
|
use App\Http\Requests\Product\StoreProductRequest;
|
||||||
@ -24,11 +27,13 @@ use App\Import\ImportException;
|
|||||||
use App\Import\Providers\BaseImport;
|
use App\Import\Providers\BaseImport;
|
||||||
use App\Import\Providers\ImportInterface;
|
use App\Import\Providers\ImportInterface;
|
||||||
use App\Import\Transformer\Csv\ClientTransformer;
|
use App\Import\Transformer\Csv\ClientTransformer;
|
||||||
|
use App\Import\Transformer\Csv\ExpenseTransformer;
|
||||||
use App\Import\Transformer\Csv\InvoiceTransformer;
|
use App\Import\Transformer\Csv\InvoiceTransformer;
|
||||||
use App\Import\Transformer\Csv\PaymentTransformer;
|
use App\Import\Transformer\Csv\PaymentTransformer;
|
||||||
use App\Import\Transformer\Csv\ProductTransformer;
|
use App\Import\Transformer\Csv\ProductTransformer;
|
||||||
use App\Import\Transformer\Csv\VendorTransformer;
|
use App\Import\Transformer\Csv\VendorTransformer;
|
||||||
use App\Repositories\ClientRepository;
|
use App\Repositories\ClientRepository;
|
||||||
|
use App\Repositories\ExpenseRepository;
|
||||||
use App\Repositories\InvoiceRepository;
|
use App\Repositories\InvoiceRepository;
|
||||||
use App\Repositories\PaymentRepository;
|
use App\Repositories\PaymentRepository;
|
||||||
use App\Repositories\ProductRepository;
|
use App\Repositories\ProductRepository;
|
||||||
@ -195,6 +200,29 @@ class Csv extends BaseImport implements ImportInterface
|
|||||||
|
|
||||||
private function expense()
|
private function expense()
|
||||||
{
|
{
|
||||||
|
$entity_type = 'expense';
|
||||||
|
|
||||||
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
|
$data = $this->preTransform($data, $entity_type);
|
||||||
|
|
||||||
|
if (empty($data)) {
|
||||||
|
$this->entity_count['expenses'] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->request_name = StoreExpenseRequest::class;
|
||||||
|
$this->repository_name = ExpenseRepository::class;
|
||||||
|
$this->factory_name = ExpenseFactory::class;
|
||||||
|
|
||||||
|
$this->repository = app()->make($this->repository_name);
|
||||||
|
$this->repository->import_mode = true;
|
||||||
|
|
||||||
|
$this->transformer = new ExpenseTransformer($this->company);
|
||||||
|
|
||||||
|
$expense_count = $this->ingest($data, $entity_type);
|
||||||
|
|
||||||
|
$this->entity_count['expenses'] = $expense_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function quote()
|
private function quote()
|
||||||
|
@ -124,6 +124,21 @@ class BaseTransformer
|
|||||||
->exists();
|
->exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasProject($name)
|
||||||
|
{
|
||||||
|
return $this->company
|
||||||
|
->projects()
|
||||||
|
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||||
|
strtolower(str_replace(' ', '', $name)),
|
||||||
|
])
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
*
|
*
|
||||||
|
72
app/Import/Transformer/Csv/ExpenseTransformer.php
Normal file
72
app/Import/Transformer/Csv/ExpenseTransformer.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* client Ninja (https://clientninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/clientninja/clientninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. client Ninja LLC (https://clientninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
namespace App\Import\Transformer\Csv;
|
||||||
|
|
||||||
|
use App\Import\Transformer\BaseTransformer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class InvoiceTransformer.
|
||||||
|
*/
|
||||||
|
class ExpenseTransformer extends BaseTransformer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
*
|
||||||
|
* @return bool|array
|
||||||
|
*/
|
||||||
|
public function transform($data)
|
||||||
|
{
|
||||||
|
$clientId = isset($data['expense.client'])
|
||||||
|
? $this->getClientId($data['expense.client'])
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'amount' => $this->getFloat($data, 'expense.amount'),
|
||||||
|
'currency_id' => $this->getCurrencyByCode(
|
||||||
|
$data,
|
||||||
|
'expense.currency_id'
|
||||||
|
),
|
||||||
|
'vendor_id' => isset($data['expense.vendor'])
|
||||||
|
? $this->getVendorId($data['expense.vendor'])
|
||||||
|
: null,
|
||||||
|
'client_id' => isset($data['expense.client'])
|
||||||
|
? $this->getClientId($data['expense.client'])
|
||||||
|
: null,
|
||||||
|
'date' => isset($data['expense.date'])
|
||||||
|
? date('Y-m-d', strtotime($data['expense.date']))
|
||||||
|
: null,
|
||||||
|
'public_notes' => $this->getString($data, 'expense.public_notes'),
|
||||||
|
'private_notes' => $this->getString($data, 'expense.private_notes'),
|
||||||
|
'category_id' => isset($data['expense.category'])
|
||||||
|
? $this->getExpenseCategoryId($data['expense.category'])
|
||||||
|
: null,
|
||||||
|
'project_id' => isset($data['expense.project'])
|
||||||
|
? $this->getProjectId($data['expense.project'])
|
||||||
|
: null,
|
||||||
|
'payment_type_id' => isset($data['expense.payment_type'])
|
||||||
|
? $this->getPaymentTypeId($data['expense.payment_type'])
|
||||||
|
: null,
|
||||||
|
'payment_date' => isset($data['expense.payment_date'])
|
||||||
|
? date('Y-m-d', strtotime($data['expense.payment_date']))
|
||||||
|
: null,
|
||||||
|
'custom_value1' => $this->getString($data, 'expense.custom_value1'),
|
||||||
|
'custom_value2' => $this->getString($data, 'expense.custom_value2'),
|
||||||
|
'custom_value3' => $this->getString($data, 'expense.custom_value3'),
|
||||||
|
'custom_value4' => $this->getString($data, 'expense.custom_value4'),
|
||||||
|
'transaction_reference' => $this->getString(
|
||||||
|
$data,
|
||||||
|
'expense.transaction_reference'
|
||||||
|
),
|
||||||
|
'should_be_invoiced' => $clientId ? true : false,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -43,9 +43,7 @@ class CsvImportTest extends TestCase
|
|||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->withoutMiddleware(
|
$this->withoutMiddleware(ThrottleRequests::class);
|
||||||
ThrottleRequests::class
|
|
||||||
);
|
|
||||||
|
|
||||||
config(['database.default' => config('ninja.db.default')]);
|
config(['database.default' => config('ninja.db.default')]);
|
||||||
|
|
||||||
@ -54,9 +52,44 @@ class CsvImportTest extends TestCase
|
|||||||
$this->withoutExceptionHandling();
|
$this->withoutExceptionHandling();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testExpenseCsvImport()
|
||||||
|
{
|
||||||
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/expenses.csv'
|
||||||
|
);
|
||||||
|
$hash = Str::random(32);
|
||||||
|
$column_map = [
|
||||||
|
0 => 'expense.client',
|
||||||
|
1 => 'expense.project',
|
||||||
|
2 => 'expense.notes',
|
||||||
|
3 => 'expense.amount',
|
||||||
|
];
|
||||||
|
|
||||||
public function testVendorCsvImport() {
|
$data = [
|
||||||
$csv = file_get_contents( base_path() . '/tests/Feature/Import/vendors.csv' );
|
'hash' => $hash,
|
||||||
|
'column_map' => ['expense' => ['mapping' => $column_map]],
|
||||||
|
'skip_header' => true,
|
||||||
|
'import_type' => 'csv',
|
||||||
|
];
|
||||||
|
|
||||||
|
Cache::put($hash . '-expense', base64_encode($csv), 360);
|
||||||
|
|
||||||
|
$csv_importer = new Csv($data, $this->company);
|
||||||
|
|
||||||
|
$count = $csv_importer->import('expense');
|
||||||
|
|
||||||
|
$base_transformer = new BaseTransformer($this->company);
|
||||||
|
|
||||||
|
nlog($csv_importer->entity_count);
|
||||||
|
|
||||||
|
$this->assertTrue($base_transformer->hasProject('officiis'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testVendorCsvImport()
|
||||||
|
{
|
||||||
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/vendors.csv'
|
||||||
|
);
|
||||||
$hash = Str::random(32);
|
$hash = Str::random(32);
|
||||||
$column_map = [
|
$column_map = [
|
||||||
0 => 'vendor.name',
|
0 => 'vendor.name',
|
||||||
@ -84,16 +117,14 @@ class CsvImportTest extends TestCase
|
|||||||
|
|
||||||
$base_transformer = new BaseTransformer($this->company);
|
$base_transformer = new BaseTransformer($this->company);
|
||||||
|
|
||||||
$this->assertTrue($base_transformer->hasVendor("Ludwig Krajcik DVM"));
|
$this->assertTrue($base_transformer->hasVendor('Ludwig Krajcik DVM'));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function testProductImport()
|
public function testProductImport()
|
||||||
{
|
{
|
||||||
$csv = file_get_contents( base_path() . '/tests/Feature/Import/products.csv' );
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/products.csv'
|
||||||
|
);
|
||||||
$hash = Str::random(32);
|
$hash = Str::random(32);
|
||||||
Cache::put($hash . '-product', base64_encode($csv), 360);
|
Cache::put($hash . '-product', base64_encode($csv), 360);
|
||||||
|
|
||||||
@ -120,12 +151,13 @@ class CsvImportTest extends TestCase
|
|||||||
|
|
||||||
$this->assertTrue($base_transformer->hasProduct('officiis'));
|
$this->assertTrue($base_transformer->hasProduct('officiis'));
|
||||||
// $this->assertTrue($base_transformer->hasProduct('maxime'));
|
// $this->assertTrue($base_transformer->hasProduct('maxime'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testClientImport()
|
public function testClientImport()
|
||||||
{
|
{
|
||||||
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/clients.csv'
|
||||||
|
);
|
||||||
$hash = Str::random(32);
|
$hash = Str::random(32);
|
||||||
$column_map = [
|
$column_map = [
|
||||||
1 => 'client.balance',
|
1 => 'client.balance',
|
||||||
@ -156,24 +188,28 @@ class CsvImportTest extends TestCase
|
|||||||
|
|
||||||
$base_transformer = new BaseTransformer($this->company);
|
$base_transformer = new BaseTransformer($this->company);
|
||||||
|
|
||||||
$this->assertTrue($base_transformer->hasClient("Ludwig Krajcik DVM"));
|
$this->assertTrue($base_transformer->hasClient('Ludwig Krajcik DVM'));
|
||||||
|
|
||||||
$client_id = $base_transformer->getClient("Ludwig Krajcik DVM", null);
|
$client_id = $base_transformer->getClient('Ludwig Krajcik DVM', null);
|
||||||
|
|
||||||
$c = Client::find($client_id);
|
$c = Client::find($client_id);
|
||||||
|
|
||||||
$this->assertEquals($client_id, $c->id);
|
$this->assertEquals($client_id, $c->id);
|
||||||
|
|
||||||
$client_id = $base_transformer->getClient("a non existent clent", "brook59@example.org");
|
$client_id = $base_transformer->getClient(
|
||||||
|
'a non existent clent',
|
||||||
|
'brook59@example.org'
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals($client_id, $c->id);
|
$this->assertEquals($client_id, $c->id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvoiceImport()
|
public function testInvoiceImport()
|
||||||
{
|
{
|
||||||
/*Need to import clients first*/
|
/*Need to import clients first*/
|
||||||
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/clients.csv'
|
||||||
|
);
|
||||||
$hash = Str::random(32);
|
$hash = Str::random(32);
|
||||||
$column_map = [
|
$column_map = [
|
||||||
1 => 'client.balance',
|
1 => 'client.balance',
|
||||||
@ -203,12 +239,14 @@ class CsvImportTest extends TestCase
|
|||||||
|
|
||||||
$base_transformer = new BaseTransformer($this->company);
|
$base_transformer = new BaseTransformer($this->company);
|
||||||
|
|
||||||
$this->assertTrue($base_transformer->hasClient("Ludwig Krajcik DVM"));
|
$this->assertTrue($base_transformer->hasClient('Ludwig Krajcik DVM'));
|
||||||
|
|
||||||
/* client import verified*/
|
/* client import verified*/
|
||||||
|
|
||||||
/*Now import invoices*/
|
/*Now import invoices*/
|
||||||
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/invoice.csv'
|
||||||
|
);
|
||||||
$hash = Str::random(32);
|
$hash = Str::random(32);
|
||||||
|
|
||||||
$column_map = [
|
$column_map = [
|
||||||
@ -242,14 +280,13 @@ class CsvImportTest extends TestCase
|
|||||||
|
|
||||||
$csv_importer->import('invoice');
|
$csv_importer->import('invoice');
|
||||||
|
|
||||||
$this->assertTrue($base_transformer->hasInvoice("801"));
|
$this->assertTrue($base_transformer->hasInvoice('801'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Lets piggy back payments tests here to save rebuilding the test multiple times*/
|
/* Lets piggy back payments tests here to save rebuilding the test multiple times*/
|
||||||
|
|
||||||
|
$csv = file_get_contents(
|
||||||
$csv = file_get_contents( base_path() . '/tests/Feature/Import/payments.csv' );
|
base_path() . '/tests/Feature/Import/payments.csv'
|
||||||
|
);
|
||||||
$hash = Str::random(32);
|
$hash = Str::random(32);
|
||||||
|
|
||||||
$column_map = [
|
$column_map = [
|
||||||
@ -272,30 +309,18 @@ class CsvImportTest extends TestCase
|
|||||||
|
|
||||||
$csv_importer->import('payment');
|
$csv_importer->import('payment');
|
||||||
|
|
||||||
$this->assertTrue($base_transformer->hasInvoice("801"));
|
$this->assertTrue($base_transformer->hasInvoice('801'));
|
||||||
|
|
||||||
$invoice_id = $base_transformer->getInvoiceId("801");
|
$invoice_id = $base_transformer->getInvoiceId('801');
|
||||||
|
|
||||||
$invoice = Invoice::find($invoice_id);
|
$invoice = Invoice::find($invoice_id);
|
||||||
|
|
||||||
$this->assertTrue($invoice->payments()->exists());
|
$this->assertTrue($invoice->payments()->exists());
|
||||||
$this->assertEquals(1, $invoice->payments()->count());
|
$this->assertEquals(1, $invoice->payments()->count());
|
||||||
$this->assertEquals(400, $invoice->payments()->sum('payments.amount'));
|
$this->assertEquals(400, $invoice->payments()->sum('payments.amount'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// public function testClientCsvImport()
|
// public function testClientCsvImport()
|
||||||
// {
|
// {
|
||||||
// $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
// $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user