diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 2c1a731228ad..99f6378f3ad5 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,7 +15,6 @@ use App\Jobs\Cron\AutoBillCron; use App\Jobs\Cron\RecurringExpensesCron; use App\Jobs\Cron\RecurringInvoicesCron; use App\Jobs\Cron\SubscriptionCron; -use App\Jobs\Cron\UpdateCalculatedFields; use App\Jobs\Invoice\InvoiceCheckLateWebhook; use App\Jobs\Ninja\AdjustEmailQuota; use App\Jobs\Ninja\BankTransactionSync; @@ -68,9 +67,6 @@ class Kernel extends ConsoleKernel /* Stale Invoice Cleanup*/ $schedule->job(new CleanStaleInvoiceOrder())->hourlyAt(30)->withoutOverlapping()->name('stale-invoice-job')->onOneServer(); - /* Stale Invoice Cleanup*/ - $schedule->job(new UpdateCalculatedFields())->hourlyAt(40)->withoutOverlapping()->name('update-calculated-fields-job')->onOneServer(); - /* Checks for large companies and marked them as is_large */ $schedule->job(new CompanySizeCheck())->dailyAt('23:20')->withoutOverlapping()->name('company-size-job')->onOneServer(); diff --git a/app/Jobs/Cron/UpdateCalculatedFields.php b/app/Jobs/Cron/UpdateCalculatedFields.php deleted file mode 100644 index 465108c96800..000000000000 --- a/app/Jobs/Cron/UpdateCalculatedFields.php +++ /dev/null @@ -1,105 +0,0 @@ -with('tasks')->whereHas('tasks', function ($query) { - $query->where('updated_at', '>', now()->subHours(2)); - }) - ->cursor() - ->each(function ($project) { - - $project->current_hours = $this->calculateDuration($project); - $project->save(); - }); - - - - } else { - //multiDB environment, need to - foreach (MultiDB::$dbs as $db) { - MultiDB::setDB($db); - - - Project::query()->with('tasks')->whereHas('tasks', function ($query) { - $query->where('updated_at', '>', now()->subHours(2)); - }) - ->cursor() - ->each(function ($project) { - $project->current_hours = $this->calculateDuration($project); - $project->save(); - }); - - //Clean password resets table - \DB::connection($db)->table('password_resets')->where('created_at', '<', now()->subHour())->delete(); - - } - } - } - - private function calculateDuration($project): int - { - $duration = 0; - - $project->tasks->each(function ($task) use (&$duration) { - - if(is_iterable(json_decode($task->time_log))) { - - foreach(json_decode($task->time_log) as $log) { - - if(!is_array($log)) - continue; - - $start_time = $log[0]; - $end_time = $log[1] == 0 ? time() : $log[1]; - - $duration += $end_time - $start_time; - - } - } - - }); - - return (int) round(($duration / 60 / 60), 0); - - } -} diff --git a/app/Jobs/Subscription/CleanStaleInvoiceOrder.php b/app/Jobs/Subscription/CleanStaleInvoiceOrder.php index ddf7ce404856..d8295ed9f54d 100644 --- a/app/Jobs/Subscription/CleanStaleInvoiceOrder.php +++ b/app/Jobs/Subscription/CleanStaleInvoiceOrder.php @@ -95,6 +95,8 @@ class CleanStaleInvoiceOrder implements ShouldQueue ->each(function ($invoice) { $invoice->service()->removeUnpaidGatewayFees(); }); + + \DB::connection($db)->table('password_resets')->where('created_at', '<', now()->subHours(12))->delete(); } } diff --git a/app/Models/Task.php b/app/Models/Task.php index 8479ec26f6f6..0df362e4d529 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -137,7 +137,7 @@ class Task extends BaseModel // 'project', ]; - protected $touches = []; + protected $touches = ['project']; public function getEntityType() { diff --git a/app/Observers/TaskObserver.php b/app/Observers/TaskObserver.php index 96dc353159e8..b9eaf49e5936 100644 --- a/app/Observers/TaskObserver.php +++ b/app/Observers/TaskObserver.php @@ -82,6 +82,7 @@ class TaskObserver if ($subscriptions) { WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(0); } + } /** diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 3b134ba355d3..4c7dd2691013 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -586,10 +586,6 @@ class BaseDriver extends AbstractPaymentDriver $invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->withTrashed()->get(); - // $invoices->each(function ($invoice) { - // $invoice->service()->deletePdf(); - // }); - $invoices->first()->invitations->each(function ($invitation) use ($nmo) { if ((bool) $invitation->contact->send_email !== false && $invitation->contact->email) { $nmo->to_user = $invitation->contact; diff --git a/app/PaymentDrivers/Rotessa/vendor/karneaud/omnipay-rotessa/src/Omnipay/Rotessa/Http/Client.php b/app/PaymentDrivers/Rotessa/vendor/karneaud/omnipay-rotessa/src/Omnipay/Rotessa/Http/Client.php index 4c8ea0d29075..1e0abbe8aba0 100644 --- a/app/PaymentDrivers/Rotessa/vendor/karneaud/omnipay-rotessa/src/Omnipay/Rotessa/Http/Client.php +++ b/app/PaymentDrivers/Rotessa/vendor/karneaud/omnipay-rotessa/src/Omnipay/Rotessa/Http/Client.php @@ -72,9 +72,9 @@ class Client extends HttpClient $response = $this->httpClient->sendRequest( $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion)); else $response = $this->httpClient->request($method, $uri, compact('body','headers')); } catch (\Http\Client\Exception\NetworkException $networkException) { - throw new NetworkException($networkException->getMessage(), $request, $networkException); + throw new \Exception($networkException->getMessage()); } catch (\Exception $exception) { - throw new RequestException($exception->getMessage(), $request, $exception); + throw new \Exception($exception->getMessage()); } return $response; diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index 0c1e3aeaae9e..ec96ed0670b0 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -46,16 +46,16 @@ class TaskRepository extends BaseRepository $this->new_task = false; } - if(isset($data['assigned_user_id']) && $data['assigned_user_id'] != $task->assigned_user_id){ - TaskAssigned::dispatch($task, $task->company->db)->delay(2); - } - if(!is_numeric($task->rate) && !isset($data['rate'])) $data['rate'] = 0; $task->fill($data); $task->saveQuietly(); + if(isset($data['assigned_user_id']) && $data['assigned_user_id'] != $task->assigned_user_id) { + TaskAssigned::dispatch($task, $task->company->db)->delay(2); + } + $this->init($task); if ($this->new_task && ! $task->status_id) { @@ -155,6 +155,8 @@ class TaskRepository extends BaseRepository $this->saveDocuments($data['documents'], $task); } + $this->calculateProjectDuration($task); + return $task; } @@ -261,6 +263,8 @@ class TaskRepository extends BaseRepository $task->saveQuietly(); } + $this->calculateProjectDuration($task); + return $task; } @@ -302,7 +306,10 @@ class TaskRepository extends BaseRepository $task->saveQuietly(); } + $this->calculateProjectDuration($task); + return $task; + } public function triggeredActions($request, $task) @@ -348,4 +355,67 @@ class TaskRepository extends BaseRepository return $task->number; } + + private function calculateProjectDuration(Task $task) + { + + if($task->project) { + + $duration = 0; + + $task->project->tasks->each(function ($task) use (&$duration) { + + if(is_iterable(json_decode($task->time_log))) { + + foreach(json_decode($task->time_log) as $log) { + + if(!is_array($log)) { + continue; + } + + $start_time = $log[0]; + $end_time = $log[1] == 0 ? time() : $log[1]; + + $duration += $end_time - $start_time; + + } + } + + }); + + $task->project->current_hours = (int) round(($duration / 60 / 60), 0); + $task->push(); + } + } + + /** + * @param $entity + */ + public function restore($task) + { + if (!$task->trashed()) { + return; + } + + parent::restore($task); + + $this->calculateProjectDuration($task); + + } + + /** + * @param $entity + */ + public function delete($task) + { + if ($task->is_deleted) { + return; + } + + parent::delete($task); + + $this->calculateProjectDuration($task); + + } + }