Skip to content

Commit

Permalink
Merge pull request #326 from jesusantguerrero/fix/balance-refreshes
Browse files Browse the repository at this point in the history
Fix/balance refreshes
  • Loading branch information
jesusantguerrero authored Dec 7, 2023
2 parents d66f337 + 66000eb commit 5b67854
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
14 changes: 7 additions & 7 deletions app/Console/Commands/MakeOccurrenceReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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));
}
}
}
47 changes: 36 additions & 11 deletions app/Domains/Housing/Models/Occurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Occurrence extends Model
protected $casts = [
'conditions' => 'array',
'log' => 'array',
'last_date' => 'date'
];

protected static function booted()
Expand All @@ -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,
Expand All @@ -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()
);
}
}
30 changes: 23 additions & 7 deletions app/Notifications/OccurrenceAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

/**
Expand Down Expand Up @@ -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',
];
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Finance/Account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions resources/js/domains/transactions/components/AccountItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', {
Expand All @@ -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))}` : '';
})
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 5b67854

Please sign in to comment.