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;
nlog("importing ".count($data) ." ". $entity_type);
foreach ($data as $key => $record) {
try {
nlog("importing {$key}");
$entity = $this->transformer->transform($record);
/** @var \App\Http\Requests\Request $request */
@ -173,10 +171,9 @@ class BaseImport
$entity->saveQuietly();
$count++;
nlog("finished importing {$key}");
}
} catch (\Exception $ex) {
nlog("caught exception for {$key}");
if ($ex instanceof ImportException) {
$message = $ex->getMessage();
} else {

View File

@ -11,6 +11,7 @@
namespace App\Import\Transformer;
use App\Factory\ProjectFactory;
use App\Models\ClientContact;
use App\Models\Country;
use App\Models\PaymentType;
@ -418,7 +419,7 @@ class BaseTransformer
*
* @return int|null
*/
public function getProjectId($name)
public function getProjectId($name, $clientId = null)
{
$project = $this->company
->projects()
@ -427,7 +428,21 @@ class BaseTransformer
])
->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'])
? $this->getClientId($data['expense.client'])
: null,
'date' => isset($data['expense.date'])
? date('Y-m-d', strtotime($data['expense.date']))
: null,
'date' => $clientId,
'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'])
? $this->getProjectId($data['expense.project'], $clientId)
: null,
'payment_type_id' => isset($data['expense.payment_type'])
? $this->getPaymentTypeId($data['expense.payment_type'])

View File

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