From b98c1f82b7fe68f0b09f8c428dbb5e1f4f55de84 Mon Sep 17 00:00:00 2001 From: Jesus Guerrero Date: Wed, 6 Dec 2023 19:01:32 -0400 Subject: [PATCH 1/2] fix: balance refreshes --- resources/js/Pages/Finance/Account.vue | 1 + .../transactions/components/AccountItem.vue | 14 ++++++++------ .../transactions/components/TransactionModal.vue | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/resources/js/Pages/Finance/Account.vue b/resources/js/Pages/Finance/Account.vue index 742d11a3..380e15ed 100644 --- a/resources/js/Pages/Finance/Account.vue +++ b/resources/js/Pages/Finance/Account.vue @@ -121,6 +121,7 @@ const reconcileForm = useForm({ ...data, date: format(data.date, 'yyyy-MM-dd'), })).post(`/finance/reconciliation/accounts/${selectedAccount.value?.id}`, { + preserveScroll: true, only: ['transactions', 'accounts', 'stats'], onFinish() { reconcileForm.reset() diff --git a/resources/js/domains/transactions/components/AccountItem.vue b/resources/js/domains/transactions/components/AccountItem.vue index d3be617a..4e2481f2 100644 --- a/resources/js/domains/transactions/components/AccountItem.vue +++ b/resources/js/domains/transactions/components/AccountItem.vue @@ -6,9 +6,9 @@ import IconDrag from "@/Components/icons/IconDrag.vue"; import LogerButtonTab from "@/Components/atoms/LogerButtonTab.vue"; import AccountReconciliationAlert from "./AccountReconciliationAlert.vue"; -import { computed } from "vue"; +import { computed, toRefs } from "vue"; -const { account } = defineProps({ +const props = defineProps({ account: { type: Object, required: true, @@ -19,20 +19,22 @@ const { account } = defineProps({ }, }); +const { account } = toRefs(props) + const isDebt = (amount: number) => { return amount < 0; }; const hasPendingReconciliation = computed(() => { - return account.reconciliation_last?.status == 'pending'; + return account.value.reconciliation_last?.status == 'pending'; }) const isReconciled = computed(() => { - return account.reconciliation_last?.amount == account.balance; + return account.value.reconciliation_last?.amount == account.value.balance; }) const availableCredit = computed(() => { - return parseFloat(account.credit_limit) + parseFloat(account.balance); + return parseFloat(account.value.credit_limit) + parseFloat(account.value.balance); }) const creditLimitDate = computed(() => { const formatter = new Intl.PluralRules('en-US', { @@ -45,7 +47,7 @@ const creditLimitDate = computed(() => { ["few", "rd"], ["other", "th"], ]); - return account.credit_closing_day ? ` - ${account.credit_closing_day}${suffixes.get(formatter.select(account.credit_closing_day))}` : ''; + return account.value.credit_closing_day ? ` - ${account.value.credit_closing_day}${suffixes.get(formatter.select(account.credit_closing_day))}` : ''; }) diff --git a/resources/js/domains/transactions/components/TransactionModal.vue b/resources/js/domains/transactions/components/TransactionModal.vue index 308aaecb..899c621c 100644 --- a/resources/js/domains/transactions/components/TransactionModal.vue +++ b/resources/js/domains/transactions/components/TransactionModal.vue @@ -233,7 +233,7 @@ const onSubmit = (addAnother = false) => { return data; }) .submit(action.method, action.url(), { - preserveState: true, + preserveState: false, preserveScroll: true, onBefore(evt) { if (!evt.data.total) { @@ -247,7 +247,7 @@ const onSubmit = (addAnother = false) => { const items = splits.value; gridSplitsRef.value?.reset(items); }) - if (!isAddingAnother.value) { + if (!addAnother) { emit("close"); } transactionStore.emitTransaction(lastSaved as ITransaction, action.method, props.transactionData); From 66000eb6186678ed27c1a2888ae7b3014eac0e5d Mon Sep 17 00:00:00 2001 From: Jesus Guerrero Date: Thu, 7 Dec 2023 07:00:34 -0400 Subject: [PATCH 2/2] fix: --- .../Commands/MakeOccurrenceReminders.php | 14 +++--- app/Domains/Housing/Models/Occurrence.php | 47 ++++++++++++++----- app/Notifications/OccurrenceAlert.php | 30 +++++++++--- 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/app/Console/Commands/MakeOccurrenceReminders.php b/app/Console/Commands/MakeOccurrenceReminders.php index 286302db..791e5053 100644 --- a/app/Console/Commands/MakeOccurrenceReminders.php +++ b/app/Console/Commands/MakeOccurrenceReminders.php @@ -2,11 +2,11 @@ namespace App\Console\Commands; -use App\Domains\Housing\Contracts\OccurrenceNotifyTypes; -use App\Domains\Housing\Models\Occurrence; use App\Models\User; -use App\Notifications\OccurrenceAlert; use Illuminate\Console\Command; +use App\Notifications\OccurrenceAlert; +use App\Domains\Housing\Models\Occurrence; +use App\Domains\Housing\Contracts\OccurrenceNotifyTypes; class MakeOccurrenceReminders extends Command { @@ -34,14 +34,14 @@ public function handle() $occurrencesOnLast = Occurrence::getForNotificationType(OccurrenceNotifyTypes::LAST); $occurrencesOnAvg = Occurrence::getForNotificationType(OccurrenceNotifyTypes::AVG); - $this->sendNotifications($occurrencesOnLast); - $this->sendNotifications($occurrencesOnAvg); + $this->sendNotifications($occurrencesOnLast, OccurrenceNotifyTypes::LAST); + $this->sendNotifications($occurrencesOnAvg, OccurrenceNotifyTypes::AVG); } - public function sendNotifications($occurrences) + public function sendNotifications($occurrences, $type) { foreach ($occurrences as $occurrence) { - User::find($occurrence->user_id)->notify(new OccurrenceAlert($occurrence)); + User::find($occurrence->user_id)->notify(new OccurrenceAlert($occurrence, $type)); } } } diff --git a/app/Domains/Housing/Models/Occurrence.php b/app/Domains/Housing/Models/Occurrence.php index fd1f826e..20dd9fa4 100644 --- a/app/Domains/Housing/Models/Occurrence.php +++ b/app/Domains/Housing/Models/Occurrence.php @@ -47,6 +47,7 @@ class Occurrence extends Model protected $casts = [ 'conditions' => 'array', 'log' => 'array', + 'last_date' => 'date' ]; protected static function booted() @@ -67,17 +68,6 @@ public static function getLinkedModels() ]; } - public static function getForNotificationType(OccurrenceNotifyTypes $type) - { - $daysBefore = self::DAYS_BEFORE; - $activatedField = self::NOTIFY_FIELDS[$type->value]['activatedField']; - $countField = self::NOTIFY_FIELDS[$type->value]['countField']; - - return Occurrence::where($activatedField, true) - ->whereRaw("DATEDIFF( date_format(now(), '%Y-%m-%d'), last_date) > ($countField - $daysBefore)") - ->get(); - } - public static function scopeByTeam($query, int $teamId) { return $query->where([ 'team_id' => $teamId, @@ -89,4 +79,39 @@ public static function scopeByName($query, string $name) { 'name' => $name, ]); } + + public function currentCount() { + return $this->last_date->diffInDays(now()); + } + + public function diffWithAvg() { + return $this->currentCount() - $this->avg_days_passed; + } + + public function diffWithLastDuration() { + return $this->currentCount() - $this->previous_days_count; + } + + public function isCloseToAvg() { + + return $this->currentCount() >= $this->avg_days_passed - self::DAYS_BEFORE; + } + + public function isCloseToLastDuration() { + return $this->currentCount() >= $this->previous_days_count - self::DAYS_BEFORE; + } + + public static function getForNotificationType(OccurrenceNotifyTypes $type) + { + $activatedField = self::NOTIFY_FIELDS[$type->value]['activatedField']; + $countField = self::NOTIFY_FIELDS[$type->value]['countField']; + + return Occurrence::where($activatedField, true) + ->where($countField, '>', 1) + ->get() + ->filter(fn ($occurrence) => $type->value == OccurrenceNotifyTypes::AVG->value + ? $occurrence->isCloseToAvg() + :$occurrence->isCloseToLastDuration() + ); + } } diff --git a/app/Notifications/OccurrenceAlert.php b/app/Notifications/OccurrenceAlert.php index 7df27c7b..97f75a8c 100644 --- a/app/Notifications/OccurrenceAlert.php +++ b/app/Notifications/OccurrenceAlert.php @@ -2,25 +2,25 @@ namespace App\Notifications; -use App\Domains\Housing\Models\Occurrence; use Illuminate\Bus\Queueable; -use Illuminate\Notifications\Messages\MailMessage; +use App\Domains\Housing\Models\Occurrence; use Illuminate\Notifications\Notification; +use Illuminate\Notifications\Messages\MailMessage; +use App\Domains\Housing\Contracts\OccurrenceNotifyTypes; class OccurrenceAlert extends Notification { use Queueable; - private Occurrence $occurrence; /** * Create a new notification instance. * * @return void */ - public function __construct(Occurrence $occurrence) + public function __construct(private Occurrence $occurrence, private OccurrenceNotifyTypes $type) { - $this->occurrence = $occurrence; + } /** @@ -57,10 +57,26 @@ public function toMail($notifiable) public function toArray($notifiable) { $name = $this->occurrence->name; - $days = $this->occurrence->previous_days_count; + $types = [ + 'avg' => "its average of {$this->occurrence->avg_days_passed}", + 'last' => "its last duration of {$this->occurrence->previous_days_count}", + ]; + + $currentCount = $this->occurrence->currentCount(); + $referenceCount = $this->type->value == OccurrenceNotifyTypes::AVG->value ? $this->occurrence->avg_days_passed : $this->occurrence->previous_days_count; + $diff = $currentCount - $referenceCount; + $diffAbs = abs($diff); + + $messages = match (true) { + $diff < 0 => ["is close to", "days in $diffAbs days"], + $diff == 0 => ["is", ""], + default => ["has passed", "days by $diffAbs days"] + }; + + return [ - 'message' => "Hey the $name occurrence is close to $days days in 3 days", + 'message' => "The $name occurrence ({$currentCount}) {$messages[0]} {$types[$this->type->value]} {$messages[1]}", 'cta' => "Check $name", 'link' => '/housing/occurrence', ];