Fixes for project imports

This commit is contained in:
David Bomba 2022-02-03 19:43:28 +11:00
parent 6231f8bd20
commit e0f5ac0751
4 changed files with 23 additions and 14 deletions

View File

@ -142,11 +142,9 @@ class BaseImport
{ {
$count = 0; $count = 0;
nlog("importing ".count($data) ." ". $entity_type);
foreach ($data as $key => $record) { 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 */
@ -173,10 +171,9 @@ 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 {

View File

@ -11,6 +11,7 @@
namespace App\Import\Transformer; namespace App\Import\Transformer;
use App\Factory\ProjectFactory;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Country; use App\Models\Country;
use App\Models\PaymentType; use App\Models\PaymentType;
@ -418,7 +419,7 @@ class BaseTransformer
* *
* @return int|null * @return int|null
*/ */
public function getProjectId($name) public function getProjectId($name, $clientId = null)
{ {
$project = $this->company $project = $this->company
->projects() ->projects()
@ -427,7 +428,21 @@ class BaseTransformer
]) ])
->first(); ->first();
return $project ? $project->id : null; return $project ? $project->id : $this->createProject($name, $clientId);
}
private function createProject($name, $clientId)
{
$project = ProjectFactory::create($this->company->id, $this->company->owner()->id);
$project->name = $name;
if($clientId)
$project->client_id = $clientId;
$project->saveQuietly();
return $project->id;
} }
/** /**

View File

@ -41,16 +41,14 @@ class ExpenseTransformer extends BaseTransformer
'client_id' => isset($data['expense.client']) 'client_id' => isset($data['expense.client'])
? $this->getClientId($data['expense.client']) ? $this->getClientId($data['expense.client'])
: null, : null,
'date' => isset($data['expense.date']) 'date' => $clientId,
? date('Y-m-d', strtotime($data['expense.date']))
: null,
'public_notes' => $this->getString($data, 'expense.public_notes'), 'public_notes' => $this->getString($data, 'expense.public_notes'),
'private_notes' => $this->getString($data, 'expense.private_notes'), 'private_notes' => $this->getString($data, 'expense.private_notes'),
'category_id' => isset($data['expense.category']) 'category_id' => isset($data['expense.category'])
? $this->getExpenseCategoryId($data['expense.category']) ? $this->getExpenseCategoryId($data['expense.category'])
: null, : null,
'project_id' => isset($data['expense.project']) 'project_id' => isset($data['expense.project'])
? $this->getProjectId($data['expense.project']) ? $this->getProjectId($data['expense.project'], $clientId)
: null, : null,
'payment_type_id' => isset($data['expense.payment_type']) 'payment_type_id' => isset($data['expense.payment_type'])
? $this->getPaymentTypeId($data['expense.payment_type']) ? $this->getPaymentTypeId($data['expense.payment_type'])

View File

@ -8,6 +8,7 @@
* *
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace Tests\Feature\Import\CSV; namespace Tests\Feature\Import\CSV;
use App\Import\Providers\Csv; use App\Import\Providers\Csv;
@ -61,7 +62,7 @@ class CsvImportTest extends TestCase
$column_map = [ $column_map = [
0 => 'expense.client', 0 => 'expense.client',
1 => 'expense.project', 1 => 'expense.project',
2 => 'expense.notes', 2 => 'expense.public_notes',
3 => 'expense.amount', 3 => 'expense.amount',
]; ];
@ -80,8 +81,6 @@ class CsvImportTest extends TestCase
$base_transformer = new BaseTransformer($this->company); $base_transformer = new BaseTransformer($this->company);
nlog($csv_importer->entity_count);
$this->assertTrue($base_transformer->hasProject('officiis')); $this->assertTrue($base_transformer->hasProject('officiis'));
} }