Skip to content

Commit

Permalink
add command option to update invests on certificates (#631)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1a76c87)
  • Loading branch information
davidbeig authored Jan 9, 2025
1 parent be93577 commit 8d409e2
Showing 1 changed file with 119 additions and 41 deletions.
160 changes: 119 additions & 41 deletions src/Goteo/Console/Command/CertificateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,59 @@
use Symfony\Component\Console\Output\OutputInterface;


class CertificateCommand extends AbstractCommand {
class CertificateCommand extends AbstractCommand
{

protected function configure()
{
$this->setName("certificate")
->setDescription("Manages donors data")
->setDefinition(array(
new InputOption('update', 'u', InputOption::VALUE_NONE, 'Actually does the job. If not specified, nothing is done, readonly process.'),
new InputOption('update_donors', 'c', InputOption::VALUE_NONE, "If specified, checks all donor's data"),
new InputOption('donor', 'd', InputOption::VALUE_OPTIONAL, "If specified, checks donor's data"),
new InputOption('update_amounts', 'a', InputOption::VALUE_NONE, "If specified calculates new amounts for donors"),
new InputOption('update_status', 's', InputOption::VALUE_NONE, "If specified updates the status of the donors"),
new InputOption('user', 'usr', InputOption::VALUE_OPTIONAL, "If specified used to search donations from a user" ),
new InputOption('year', 'y', InputOption::VALUE_OPTIONAL, "If specified used to search for donors of the selected year, if not current year is used"),
new InputOption('unify', '', InputOption::VALUE_NONE, "If specified unifies donors certificates for the given year")
))
->setHelp(<<<EOT
->setDescription("Manages donors data")
->setDefinition(array(
new InputOption('update', 'u', InputOption::VALUE_NONE, 'Actually does the job. If not specified, nothing is done, readonly process.'),
new InputOption('update_donors', 'c', InputOption::VALUE_NONE, "If specified, checks all donor's data"),
new InputOption('donor', 'd', InputOption::VALUE_OPTIONAL, "If specified, checks donor's data"),
new InputOption('update_amounts', 'a', InputOption::VALUE_NONE, "If specified calculates new amounts for donors"),
new InputOption('update_invests', 'i', InputOption::VALUE_NONE, "If specified updates the invests for the certificates"),
new InputOption('update_status', 's', InputOption::VALUE_NONE, "If specified updates the status of the donors"),
new InputOption('user', 'usr', InputOption::VALUE_OPTIONAL, "If specified used to search donations from a user"),
new InputOption('year', 'y', InputOption::VALUE_OPTIONAL, "If specified used to search for donors of the selected year, if not current year is used"),
new InputOption('unify', '', InputOption::VALUE_NONE, "If specified unifies donors certificates for the given year")
))
->setHelp(
<<<EOT
This command checks the valid fields for donors.
Usage:
Update donors summary data
<info>./console donor --update_donors --update </info>
<info>./console certificate --update_donors --update </info>
Update the provided donor's summary data
<info>./console donor --update_donors --donor donor_id --update </info>
<info>./console certificate --update_donors --donor donor_id --update </info>
Update the donors amount
<info>./console donor --update_amounts </info>
<info>./console certificate --update_amounts </info>
Update the provided user's donor amount
<info>./console donor --update_amounts --user user_id --update </info>
<info>./console certificate --update_amounts --user user_id --update </info>
Update the donors amounts for a given year
<info>./console donor --update_amounts --year 2020 --update </info>
<info>./console certificate --update_amounts --year 2020 --update </info>
Update the provided user's donor status
<info>./console donor --update_status --user user_id --update </info>
<info>./console certificate --update_status --user user_id --update </info>
Update the provided user's donor status
<info>./console donor --update_status --user user_id --update </info>
<info>./console certificate --update_status --user user_id --update </info>
Update the donors status for a year
<info>./console donor --update_status --year 2020 --update </info>
<info>./console certificate --update_status --year 2020 --update </info>
Update the invests for the certificates
<info>./console certificate --update_invests --year 2024 --update </info>
EOT
);
);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -80,14 +86,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$update_donors = $input->getOption('update_donors');
$update_amounts = $input->getOption('update_amounts');
$update_status = $input->getOption('update_status');
$update_invests = $input->getOption('update_invests');
$unify = $input->getOption('unify');

if(!$update_donors && !$update_amounts && !$update_status && !$unify) {
if (!$update_donors && !$update_amounts && !$update_status && !$unify && !$update_invests) {
throw new InvalidArgumentException('No action defined. Please define any action with --update_donors, --update_amounts or --update_status');
}

$user = $input->getOption('user');
$year = $input->getOption('year')? $input->getOption('year') : date('Y');
$year = $input->getOption('year') ? $input->getOption('year') : date('Y');

$output->writeln("<info>Update donors thrown</info>");

Expand Down Expand Up @@ -115,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if(isset($errors['nif'])) {
if (isset($errors['nif'])) {
if ($valid_nif && $nif_type != Donor::VAT) {
$donor->legal_document_type = $nif_type;
$error_save = array();
Expand All @@ -131,7 +138,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}

} else {
$output->writeln("<info>The donor has no errors in its data</info>");
}
Expand Down Expand Up @@ -159,7 +165,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$invalid_nif = 0;
$cant_update = 0;

foreach($donors as $donor) {
foreach ($donors as $donor) {
if ($verbose) {
$progress_bar->clear();
$output->writeln("<info>Update {$donor->id} - {$donor->name} - {$donor->nif} donor</info>");
Expand All @@ -181,11 +187,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$invalid_donors++;
$valid_nif = Check::nif($donor->nif, $nif_type);

if(isset($errors['nif'])) {
if (isset($errors['nif'])) {

if ($valid_nif && $nif_type != Donor::VAT) {
$donor->legal_document_type = $nif_type;
if ($nif_type == Donor::CIF) {
if ($nif_type == Donor::CIF) {
$donor->legal_entity = Donor::LEGAL_PERSON;
} else {
$donor->legal_entity = Donor::NATURAL_PERSON;
Expand Down Expand Up @@ -261,7 +267,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$updated_donors++;
} else if ($can_be_updated && !$valid) {
$could_not_update++;
} else{
} else {
$cant_update++;
}

Expand All @@ -282,7 +288,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}

} else {
$valid_donors++;
}
Expand All @@ -300,7 +305,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Can NOT update {$could_not_update} invalid donors that have changes due to validation</info>");
$output->writeln("<info>Can NOT make changes to {$cant_update} invalid donors </info>");
$output->writeln("<info>{$updated} invalid donors HAVE been updated</info>");

}
} else if ($update_amounts) {

Expand Down Expand Up @@ -340,7 +344,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progress_bar->start();
$donors = Donor::getList($filter, 0, $total);

foreach($donors as $donor) {
foreach ($donors as $donor) {

$user_invests = Donor::getPendingInvestionsAmount($donor->user, $year);
$donor_amount = $user_invests['amount'] + $user_invests['donations_amount'];
Expand Down Expand Up @@ -379,7 +383,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
$output->writeln("<info>A total of {$can_be_updated} out of {$total_donors} can be updated using --update");
}

} else if ($update_status) {

$filter = [
Expand Down Expand Up @@ -418,7 +421,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

while ($donors = Donor::getList($filter, $offset, $limit)) {

foreach($donors as $donor) {
foreach ($donors as $donor) {
++$donors_treated;

if ($donor->status != Donor::PENDING)
Expand All @@ -434,7 +437,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Update {$donor->id} - {$donor->name} - {$donor->nif} can change it's status .</info>");
$progress_bar->display();
}

} else {
$donors_with_errors++;
if ($verbose_debug) {
Expand Down Expand Up @@ -462,7 +464,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
if ($verbose || $verbose_debug) {
$progress_bar->clear();
$output->writeln("<error> {$donor->id} - {$donor->name} - {$donor->nif}. ". implode(',', $errors) . "</error>");
$output->writeln("<error> {$donor->id} - {$donor->name} - {$donor->nif}. " . implode(',', $errors) . "</error>");
$progress_bar->display();
}
}
Expand All @@ -480,7 +482,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$progress_bar->advance();
}

$offset+=$limit;
$offset += $limit;
}
$progress_bar->finish();
$total_donors += $total;
Expand All @@ -491,7 +493,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>A total of {$updated_donors} out of {$total_donors} have been updated.</info>");
$output->writeln("<info>A total of {$donors_with_errors} out of {$total_donors} have errors.</info>");
$output->writeln("<info>A total of {$donor_without_amount} out of {$total_donors} have no amount.</info>");

} else {
$output->writeln("<info>A total of {$donors_treated} out of {$total_donors} have been treated");
$output->writeln("<info>A total of {$can_be_updated} out of {$total_donors} can be updated using --update");
Expand All @@ -503,11 +504,81 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else if ($unify) {
$this->info("Starting unification of certificates");
$this->unifyCertificates($input, $output);
} else if ($update_invests) {
return $this->update_invests($input, $output);
}
}

private function update_invests(InputInterface $input, OutputInterface $output): void
{

$year = $input->getOption('year');
$user = $input->getOption('user');
$donor = $input->getOption('donor');
$update = $input->getOption('update');

if (!$update)
throw new InvalidArgumentException('This command can only be executed with the update option');

$filters = [
'show_empty' => true
];

if ($year)
$filters['year'] = $year;

if ($user) {
$user = User::get($user);
$filters['user'] = $user;
}

if ($donor) {
$donor = Donor::get($donor);

$output->writeln("<info>Going to update certificate {$donor->id}</info>");

$valid = $donor->updateInvestions();
if (!$valid)
$output->writeln("<error>Could not update certificate {$donor->id}</error>");
else
$output->writeln("Certificate updated!");

return;
}

$certificatesToTreat = Donor::getList($filters, 0, 0, true);

$progress_bar = new ProgressBar($output, $certificatesToTreat);
$progress_bar->start();

$certificatesUpdated = 0;

$page = 0;
$limit = 500;


while ($donors = Donor::getList($filters, $page * $limit, $limit)) {
foreach ($donors as $donor) {
$this->writeToOutputWithProgressBar($output, $progress_bar, "<info>Going to update the investions for certificate {$donor->id}, {$donor->user->id}</info>");
$valid = $donor->updateInvestions();
if ($valid) {
$certificatesUpdated++;
} else {
$this->writeToOutputWithProgressBar($output, $progress_bar, "<error>Could not update certificate {$donor->id}</error>");
}

$progress_bar->advance();
}
++$page;
}
$progress_bar->finish();
$output->writeln('');

$output->writeln("<info>A total of $certificatesUpdated out of $certificatesToTreat donors that have been treated");
}

private function unifyCertificates(InputInterface $input, OutputInterface $output) {
private function unifyCertificates(InputInterface $input, OutputInterface $output)
{
$year = $input->getOption('year');
if (!$year)
throw new InvalidArgumentException('If you want to unify certificates you have to specify a year');
Expand All @@ -529,7 +600,7 @@ private function unifyCertificates(InputInterface $input, OutputInterface $outpu

$certificatesCreated = 0;

while($donors = Donor::getList(['year' => $year, 'donor_status' => Donor::PENDING], $page * $limit - $certificatesCreated, $limit)) {
while ($donors = Donor::getList(['year' => $year, 'donor_status' => Donor::PENDING], $page * $limit - $certificatesCreated, $limit)) {
foreach ($donors as $donor) {
if ($this->unifyUserCertificates($donor->user, $year, $input, $output))
$certificatesCreated++;
Expand Down Expand Up @@ -650,4 +721,11 @@ private function supersedeOldCertificates(array $certificates, Donor $newCertifi
}
}
}

private function writeToOutputWithProgressBar(OutputInterface $output, ProgressBar $progress_bar, string $text)
{
$progress_bar->clear();
$output->writeln($text);
$progress_bar->display();
}
}

0 comments on commit 8d409e2

Please sign in to comment.