From 8d409e2c7f476aabd5a8c38e981f1c1abc31d660 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 9 Jan 2025 08:59:11 +0100 Subject: [PATCH] add command option to update invests on certificates (#631) (cherry picked from commit 1a76c87a8d2f9840a4018e626a96d058631e5d61) --- .../Console/Command/CertificateCommand.php | 160 +++++++++++++----- 1 file changed, 119 insertions(+), 41 deletions(-) diff --git a/src/Goteo/Console/Command/CertificateCommand.php b/src/Goteo/Console/Command/CertificateCommand.php index d34a514057..b7104f0053 100644 --- a/src/Goteo/Console/Command/CertificateCommand.php +++ b/src/Goteo/Console/Command/CertificateCommand.php @@ -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(<<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( + <<./console donor --update_donors --update +./console certificate --update_donors --update Update the provided donor's summary data -./console donor --update_donors --donor donor_id --update +./console certificate --update_donors --donor donor_id --update Update the donors amount -./console donor --update_amounts +./console certificate --update_amounts Update the provided user's donor amount -./console donor --update_amounts --user user_id --update +./console certificate --update_amounts --user user_id --update Update the donors amounts for a given year -./console donor --update_amounts --year 2020 --update +./console certificate --update_amounts --year 2020 --update Update the provided user's donor status -./console donor --update_status --user user_id --update +./console certificate --update_status --user user_id --update Update the provided user's donor status -./console donor --update_status --user user_id --update +./console certificate --update_status --user user_id --update Update the donors status for a year -./console donor --update_status --year 2020 --update +./console certificate --update_status --year 2020 --update + +Update the invests for the certificates +./console certificate --update_invests --year 2024 --update EOT -); + ); } protected function execute(InputInterface $input, OutputInterface $output) @@ -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("Update donors thrown"); @@ -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(); @@ -131,7 +138,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } } } - } else { $output->writeln("The donor has no errors in its data"); } @@ -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("Update {$donor->id} - {$donor->name} - {$donor->nif} donor"); @@ -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; @@ -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++; } @@ -282,7 +288,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } } } - } else { $valid_donors++; } @@ -300,7 +305,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Can NOT update {$could_not_update} invalid donors that have changes due to validation"); $output->writeln("Can NOT make changes to {$cant_update} invalid donors "); $output->writeln("{$updated} invalid donors HAVE been updated"); - } } else if ($update_amounts) { @@ -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']; @@ -379,7 +383,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln("A total of {$can_be_updated} out of {$total_donors} can be updated using --update"); } - } else if ($update_status) { $filter = [ @@ -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) @@ -434,7 +437,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Update {$donor->id} - {$donor->name} - {$donor->nif} can change it's status ."); $progress_bar->display(); } - } else { $donors_with_errors++; if ($verbose_debug) { @@ -462,7 +464,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { if ($verbose || $verbose_debug) { $progress_bar->clear(); - $output->writeln(" {$donor->id} - {$donor->name} - {$donor->nif}. ". implode(',', $errors) . ""); + $output->writeln(" {$donor->id} - {$donor->name} - {$donor->nif}. " . implode(',', $errors) . ""); $progress_bar->display(); } } @@ -480,7 +482,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $progress_bar->advance(); } - $offset+=$limit; + $offset += $limit; } $progress_bar->finish(); $total_donors += $total; @@ -491,7 +493,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("A total of {$updated_donors} out of {$total_donors} have been updated."); $output->writeln("A total of {$donors_with_errors} out of {$total_donors} have errors."); $output->writeln("A total of {$donor_without_amount} out of {$total_donors} have no amount."); - } else { $output->writeln("A total of {$donors_treated} out of {$total_donors} have been treated"); $output->writeln("A total of {$can_be_updated} out of {$total_donors} can be updated using --update"); @@ -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("Going to update certificate {$donor->id}"); + + $valid = $donor->updateInvestions(); + if (!$valid) + $output->writeln("Could not update certificate {$donor->id}"); + 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, "Going to update the investions for certificate {$donor->id}, {$donor->user->id}"); + $valid = $donor->updateInvestions(); + if ($valid) { + $certificatesUpdated++; + } else { + $this->writeToOutputWithProgressBar($output, $progress_bar, "Could not update certificate {$donor->id}"); + } + + $progress_bar->advance(); + } + ++$page; + } + $progress_bar->finish(); + $output->writeln(''); + + $output->writeln("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'); @@ -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++; @@ -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(); + } }