Skip to content

Commit

Permalink
Merge pull request #347 from jesusantguerrero/fix/occurrence-checks-fail
Browse files Browse the repository at this point in the history
fix: occurrence checks
  • Loading branch information
jesusantguerrero authored Dec 31, 2023
2 parents c55fce7 + 773a5de commit 20e4d41
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 19 deletions.
5 changes: 4 additions & 1 deletion app/Domains/Housing/Actions/RegisterOccurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class RegisterOccurrence
{
private function softAdd(Occurrence $occurrence, $date) {
$lastDuration = $occurrence->last_date ? $this->getDaysDifference($occurrence->last_date, $date) : 0;
$lastDuration = $occurrence->last_date ? $this->getDaysDifference($occurrence->last_date->format('Y-m-d'), $date) : 0;
$log = (array) $occurrence->log ?? [];
$log[] = $date;
$occurrenceCount = count($log);
Expand Down Expand Up @@ -93,8 +93,10 @@ public function sync(Occurrence $occurrence)
$occurrence->log = [];
$occurrence->saveQuietly();


$dates = $transactions->pluck('date')->toArray();


foreach ($dates as $date) {
try {
$this->softAdd(
Expand All @@ -103,6 +105,7 @@ public function sync(Occurrence $occurrence)
);
$occurrence->save();
} catch (\Exception $e) {
throw $e;
Log::error($e->getMessage());
continue;
}
Expand Down
45 changes: 45 additions & 0 deletions app/Domains/Transaction/Actions/FindLinkedDrafts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Domains\Transaction\Actions;

use Exception;
use Illuminate\Support\Facades\Log;
use App\Domains\Transaction\Models\Transaction;
use App\Domains\Transaction\Models\LinkedTransaction;
use App\Domains\Transaction\Services\TransactionService;

class FindLinkedDrafts
{
public const DATE_AFTER = 1;

public const DATE_BEFORE = 1;

public function __construct(public int $teamId) {}

public function handle()
{
$drafts = (new TransactionService())->getListByStatus(Transaction::STATUS_DRAFT)->get();
foreach ($drafts as $draftTransaction) {
$transactions = Transaction::matchedFor($this->teamId, [
'total' => $draftTransaction->total,
'date' => $draftTransaction->date,
]);


if (count($transactions)) {
foreach ($transactions as $transaction) {
try {
LinkedTransaction::create([
'team_id' => $this->teamId,
'user_id' => $draftTransaction->user_id,
'transaction_id' => $draftTransaction->id,
'linked_transaction_id' => $transaction->id,
]);
} catch (Exception $e) {
Log::expects($e->getMessage());
}
}
}
}
}
}
21 changes: 11 additions & 10 deletions app/Domains/Transaction/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Domains\Transaction\Models;

use App\Domains\AppCore\Models\Planner;
use Insane\Journal\Models\Core\Category;
use App\Domains\Transaction\Traits\TransactionTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Insane\Journal\Models\Core\Category;
use Insane\Journal\Models\Core\Transaction as CoreTransaction;

class Transaction extends CoreTransaction
Expand Down Expand Up @@ -47,18 +47,19 @@ public function copy($data = [])

public static function matchedFor($teamId, $searchParams)
{
return Transaction::select('id')->where([
return Transaction::select('id')
->where([
'team_id' => $teamId,
'total' => $searchParams['total'],
'status' => Transaction::STATUS_VERIFIED,
])
->whereRaw('date >= SUBDATE(?, interval ? DAY) and date <= ADDDATE(?, INTERVAL ? DAY)',
[
$searchParams['date'],
$searchParams['datesBefore'] ?? 1,
$searchParams['date'],
$searchParams['datesAfter'] ?? 1,
])->get();
->whereIn("status", [Transaction::STATUS_DRAFT, Transaction::STATUS_VERIFIED])
->whereRaw('date >= SUBDATE(?, interval ? DAY) and date <= ADDDATE(?, INTERVAL ? DAY)',
[
$searchParams['date'],
$searchParams['datesBefore'] ?? 1,
$searchParams['date'],
$searchParams['datesAfter'] ?? 1,
])->get();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Domains/Transaction/Services/TransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function getListByStatus(string $status)
ca.name counter_account_name,
accounts.name account_name,
linked.id linked_transaction_id,
linked.total linked_transaction_total
linked.total linked_transaction_total,
transactions.team_id,
transactions.user_id
')
->leftJoin('categories', 'categories.id', 'transactions.category_id')
->leftJoin('payees', 'payees.id', 'transactions.payee_id')
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Controllers/Finance/FinanceTransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Controllers\Traits\QuerifySlim;
use App\Domains\Transaction\Models\Transaction;
use Freesgen\Atmosphere\Http\InertiaController;
use App\Domains\Transaction\Actions\FindLinkedDrafts;
use App\Domains\Transaction\Exports\TransactionExport;
use App\Domains\Transaction\Services\TransactionService;
use App\Domains\Transaction\Resources\TransactionResource;
Expand Down Expand Up @@ -189,6 +190,12 @@ public function findLinked(Transaction $transaction)
return redirect()->back();
}

public function findLinkedDrafts()
{
(new FindLinkedDrafts(request()->user()->current_team_id))->handle();
return redirect()->back();
}

private function getOptionParams()
{
$queryParams = request()->query();
Expand Down
15 changes: 10 additions & 5 deletions resources/js/Pages/DashboardDrafts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import axios from 'axios';
import WidgetTitleCard from '@/Components/molecules/WidgetTitleCard.vue';
import TransactionsList from '@/domains/transactions/components/TransactionsList.vue';
import { removeTransaction, transactionDBToTransaction, useTransactionModal } from '@/domains/transactions';
import { removeTransaction, draftsDBToTransaction, useTransactionModal } from '@/domains/transactions';
import LogerButton from '@/Components/atoms/LogerButton.vue';
import { useTransactionStore } from '@/store/transactions';
import { router } from '@inertiajs/vue3';
const transactionsDraft = ref([]);
const isLoadingDrafts = ref(false);
const fetchTransactions = async () => {
const url = `/api/finance/transactions?filter[status]=draft&limit=10`;
const url = `/api/finance/transactions?filter[status]=draft&limit=10&relationships=linked`;
return axios.get(url).then<ITransaction[]>(({ data }) => {
transactionsDraft.value = data;
isLoadingDrafts.value = false
Expand All @@ -26,8 +27,12 @@
const isLoading = ref(false);
const updateTransactions = () => {
isLoading.value = true;
fetchTransactions().finally(() => {
isLoading.value = false;
router.post('/linked-drafts', {}, {
onSuccess() {
fetchTransactions().finally(() => {
isLoading.value = false;
})
}
})
}
Expand Down Expand Up @@ -62,7 +67,7 @@
class="w-full"
table-class="w-full p-2 overflow-auto text-sm rounded-t-lg shadow-md bg-base-lvl-3"
:transactions="transactionsDraft"
:parser="transactionDBToTransaction"
:parser="draftsDBToTransaction"
:allow-remove="true"
:allow-mark-as-approved="true"
:hide-accounts="true"
Expand Down
15 changes: 13 additions & 2 deletions resources/js/domains/transactions/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { v4 } from 'uuid';
export const transactionDBToTransaction = (transactions) => {
export const transactionDBToTransaction = (transactions: any[]) => {
return transactions.map(transaction => ({
id: transaction.id || v4(),
date: transaction.date,
title: transaction.description,
subtitle: transaction.account?.name ? `${transaction.account?.name} -> ${transaction.category?.name}` : '',
value: transaction.total,
status: 'VERIFIED',
status: transaction.status,
currencyCode: transaction.currency_code,
}))
}
export const draftsDBToTransaction = (transactions: any[]) => {
return transactions.map(transaction => ({
id: transaction.id || v4(),
date: transaction.date,
title: transaction.description,
subtitle: transaction.linked ? `${transaction.linked}` : '',
value: transaction.total,
status: transaction.status,
currencyCode: transaction.currency_code,
}))
}
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@

// Transactions
Route::controller(FinanceTransactionController::class)->group(function () {
Route::post('/linked-drafts', 'findLinkedDrafts')->name('finance.transactions.linked-drafts');
Route::patch('/transactions/{transaction}/linked', 'findLinked')->name('finance.transactions.linked');
Route::get('/api/finance/transactions', 'list')->name('finance.transactions.list');
Route::get('/finance/transactions', 'index')->name('finance.transactions');
Expand Down

0 comments on commit 20e4d41

Please sign in to comment.