Skip to content

Commit

Permalink
import donations
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Dec 3, 2023
1 parent a924244 commit 0f16489
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ npm-debug.log
phpunit.xml
yarn-error.log
.DS_Store
/.scannerwork/.sonar_lock
/.scannerwork/report-task.txt
6 changes: 3 additions & 3 deletions app/Console/Commands/Import/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function handle(): int
'--force' => $this->option('force'),
]);

$this->call(ImportDonationsCommand::class, [
'--force' => $this->option('force'),
]);
// $this->call(ImportDonationsCommand::class, [
// '--force' => $this->option('force'),
// ]);

$this->call(ImportArticlesCommand::class, [
'--skip-files' => $this->option('skip-files'),
Expand Down
47 changes: 45 additions & 2 deletions app/Console/Commands/Import/ImportDonationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace App\Console\Commands\Import;

use App\Enums\EuPlatescStatus;
use App\Models\Donation;
use App\Models\Project;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Throwable;

class ImportDonationsCommand extends Command
Expand All @@ -30,6 +35,8 @@ class ImportDonationsCommand extends Command
*/
public function handle(): int
{
Donation::truncate();

if (! $this->confirmToProceed()) {
return static::FAILURE;
}
Expand All @@ -39,13 +46,49 @@ public function handle(): int
->orderBy('dbo.Donations.Id');

$this->createProgressBar('Importing donations...', $query->count());
$projectsIds = Project::get()->pluck('id');

$query->chunk((int) $this->option('chunk'), function (Collection $items) {
$query->chunk((int) $this->option('chunk'), function (Collection $items) use ($projectsIds) {
$items
->reject(fn (object $row) => $this->getRejectedOrganizations()->contains($row->ONGId))
->reject(fn (object $row) => $projectsIds->doesntContain($row->ONGProjectId))
->each(function (object $row) {
try {
// TODO: import donations
$created_at = Carbon::parse($row->CreationDate);
Donation::forceCreate(
[
'id' => (int) $row->Id,
'project_id' => (int) $row->ONGProjectId,
'user_id' => $row->UserId,
'organization_id' => (int) $row->ONGId,
'amount' => (float) $row->Amount,
'charge_amount' => (float) $row->ChargedAmount,
'first_name' => $row->FirstName??'',
'last_name' => $row->LastName??'',
'email' => $row->Email??'',
'card_holder_status_message' => $row->CaldHolderStatusMessage,
'created_at' => $created_at,
'approval_date' => Carbon::parse(Str::replace(':AM','',$row->ApprovedDate)),
'charge_date' => Carbon::parse(Str::replace(':AM','',$row->ChargedDate)),
'updated_without_correct_e_pid' => (bool) $row->UpdatedWithoutCorrectEpId,
'status' => match ($row->DonationStatusTypeId) {
1 => EuPlatescStatus::INITIALIZE,
2 => EuPlatescStatus::AUTHORIZED,
3 => EuPlatescStatus::UNAUTHORIZED,
4 => EuPlatescStatus::ABORTED,
5 => EuPlatescStatus::CHARGED,
6 => EuPlatescStatus::POSSIBLE_FRAUD,
7 => EuPlatescStatus::PAYMENT_DECLINED,

default => throw new \Exception('Invalid status: ' . $row->Status),
},




]
);

} catch (Throwable $th) {
$this->logError('Error importing donation #' . $row->Id, [$th->getMessage()]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Organization extends Model implements HasMedia
protected $fillable = [
'name',
'cif',
// 'slug',
// 'slug',
'description',
'address',
'contact_person',
Expand Down

0 comments on commit 0f16489

Please sign in to comment.