Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/profiles #321

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions app/Domains/Transaction/Services/ReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,34 @@ public static function getIncomeVsExpenses($teamId, $timeUnitDiff = 2, $startDat
$endDate = Carbon::now()->endOfMonth()->format('Y-m-d');
$startDate = Carbon::now()->subMonth($timeUnitDiff)->startOfMonth()->format('Y-m-d');

if ($timeUnit == 'year') {
$endDate = Carbon::now()->endOfYear()->format('Y-m-d');
$startDate = Carbon::now()->subYear($timeUnitDiff)->startOfYear()->format('Y-m-d');
}

$expenses = self::getExpensesByCategoriesInPeriod($teamId, $startDate, $endDate);
$expensesGroup = $expenses->groupBy('date');
$expensesGroup = $expenses->groupBy($timeUnit);



$income = self::getIncomeByPayeeInPeriod($teamId, $startDate, $endDate);
$incomeCategories = $income->groupBy('date');
$incomeCategories = $income->groupBy($timeUnit);

$dates = $expensesGroup->keys();


return $dates->map(function ($month) use ($incomeCategories, $expensesGroup) {
$incomeData = $incomeCategories->get($month);
$expenseData = $expensesGroup->get($month);
return $dates->map(function ($dateUnit) use ($incomeCategories, $expensesGroup) {
$incomeData = $incomeCategories->get($dateUnit);
$expenseData = $expensesGroup->get($dateUnit);
return [
'date' => $month,
'month_date' => $month,
'date' => $dateUnit,
'date_unit' => $dateUnit,
'income' => $incomeData?->values()->all() ?? [],
"expense" => $expenseData?->values()->all() ?? [],
'assets' => $incomeData?->sum('total_amount') ?? 0,
'debts' => $expenseData?->sum('total') ?? 0,
];
})->sortBy('date')->values()->toArray();
})->sortByDesc($timeUnit)->values()->toArray();
}

public static function generateCurrentPreviousReport($teamId, $timeUnit = 'month', $timeUnitDiff = 2, $type = 'expenses')
Expand Down Expand Up @@ -137,7 +142,7 @@ public static function getIncomeByPayeeInPeriod($teamId, $startDate, $endDate)
->balance()
->inDateFrame($startDate, $endDate)
->incomePayees()
->selectRaw('date_format(transaction_lines.date, "%Y-%m-%01") as date, payees.name, payees.id')
->selectRaw('date_format(transaction_lines.date, "%Y-%m-%01") as date, date_format(transaction_lines.date, "%Y-%m-%01") as month, year(transaction_lines.date) as year, payees.name, payees.id')
->groupByRaw('date_format(transaction_lines.date, "%Y-%m"), payees.id')
->orderByDesc('date')
->join('transactions', 'transactions.id', 'transaction_lines.transaction_id')
Expand All @@ -150,7 +155,7 @@ public static function getExpensesByCategoriesInPeriod($teamId, $startDate, $end
->balance()
->inDateFrame($startDate, $endDate)
->expenseCategories($categories)
->selectRaw('date_format(transactions.date, "%Y-%m-01") as date, categories.name, categories.id')
->selectRaw('date_format(transactions.date, "%Y-%m-01") as date, date_format(transactions.date, "%Y-%m-01") as month, year(transactions.date) as year, categories.name, categories.id')
->groupByRaw('date_format(transactions.date, "%Y-%m"), categories.id')
->orderBy('date')
->get();
Expand All @@ -162,7 +167,7 @@ public static function getExpensesInPeriod($teamId, $startDate, $endDate)
->balance()
->inDateFrame($startDate, $endDate)
->expenseCategories()
->selectRaw('date_format(transaction_lines.date, "%Y-%m-%d") as date, date_format(transaction_lines.date, "%Y-%m") as month')
->selectRaw('date_format(transaction_lines.date, "%Y-%m-%d") as date, year(transaction_lines.date) as year, date_format(transaction_lines.date, "%Y-%m") as month')
->groupByRaw('date_format(transaction_lines.date, "%Y-%m"), transaction_lines.date')
->orderByDesc('date')
->join('transactions', 'transactions.id', 'transaction_lines.transaction_id')
Expand Down
14 changes: 7 additions & 7 deletions app/Domains/Transaction/Services/TransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ public function getNetWorthMonth($teamId, $endDate)
public static function getNetWorth($teamId, $startDate, $endDate)
{
return DB::select("
with data (month_date, total, type, balance_type, detail_type) AS (
SELECT LAST_DAY(tl.date) as month_date, tl.amount * tl.type, tl.type, accounts.balance_type, adt.name
with data (date_unit, total, type, balance_type, detail_type) AS (
SELECT LAST_DAY(tl.date) as date_unit, tl.amount * tl.type, tl.type, accounts.balance_type, adt.name
FROM transaction_lines tl
INNER JOIN transactions t on tl.transaction_id = t.id
INNER JOIN accounts on tl.account_id = accounts.id
Expand All @@ -234,12 +234,12 @@ public static function getNetWorth($teamId, $startDate, $endDate)
AND tl.team_id = :teamId
AND balance_type IS NOT null
)
SELECT month_date,
SUM(SUM(CASE WHEN balance_type = 'debit' THEN total ELSE 0 END)) over (ORDER BY month_date) as assets,
SUM(SUM(CASE WHEN balance_type = 'credit' THEN total ELSE 0 END)) over (ORDER BY month_date) as debts
SELECT date_unit,
SUM(SUM(CASE WHEN balance_type = 'debit' THEN total ELSE 0 END)) over (ORDER BY date_unit) as assets,
SUM(SUM(CASE WHEN balance_type = 'credit' THEN total ELSE 0 END)) over (ORDER BY date_unit) as debts
FROM DATA
GROUP BY month_date
ORDER BY month_date DESC
GROUP BY date_unit
ORDER BY date_unit DESC
LIMIT 12;
", [
'teamId' => $teamId,
Expand Down
7 changes: 6 additions & 1 deletion app/Http/Controllers/Finance/FinanceTrendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,13 @@ public function incomeExpensesGraph()
[$startDate, $endDate] = $this->getFilterDates($filters);
$teamId = request()->user()->current_team_id;

$span = [
"month" => 12,
"year" => 2
];

return [
'data' => ReportService::getIncomeVsExpenses($teamId, 12),
'data' => ReportService::getIncomeVsExpenses($teamId, 2, $startDate, 'year'),
'metaData' => [
'name' => 'incomeExpensesGraph',
'title' => 'Income vs Expenses',
Expand Down
73 changes: 42 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@vitejs/plugin-vue": "^4.5.0",
"@vue/server-renderer": "^3.3.8",
"@vueuse/core": "^10.6.1",
"atmosphere-ui": "^1.3.2",
"atmosphere-ui": "^1.3.3",
"autoprefixer": "^10.4.15",
"axios": "^1.4.0",
"date-fns": "^2.30.0",
Expand All @@ -56,13 +56,13 @@
"sass": "^1.69.5",
"slug": "^8.2.3",
"tailwindcss": "^3.3.5",
"typescript": "^5.2.2",
"unplugin-icons": "^0.16.6",
"typescript": "^5.3.2",
"unplugin-icons": "^0.17.4",
"unplugin-vue-components": "^0.25.2",
"uuid": "^9.0.1",
"vite": "^4.5.0",
"vite-plugin-pwa": "^0.16.7",
"vue": "^3.3.4",
"vite-plugin-pwa": "^0.17.0",
"vue": "^3.3.8",
"vue-multiselect": "^3.0.0-beta.3",
"vue3-apexcharts": "^1.4.4",
"vueuse-temporals": "^1.6.0"
Expand Down
8 changes: 4 additions & 4 deletions resources/js/Components/ChartNetworth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ const currentSeries = computed(() => {
return [{
name: 'Debts',
data: Object.values(props.data).map(item => Math.abs(item.debts)),
labels: Object.values(props.data).map(item => item.month_date)
labels: Object.values(props.data).map(item => item.date_unit)
},
{
name: 'Assets',
data: Object.values(props.data).map(item => item.assets),
labels: Object.values(props.data).map(item => item.month_date)
labels: Object.values(props.data).map(item => item.date_unit)
}];
})

const state = reactive({
headers: Object.entries(props.data).map(([dateString, item]) => ({
label: formatMonth(item.month_date),
label: item.date_unit,
value: [item.debts, item.assets],
id: item.month_date
id: item.date_unit
})),
options: {
colors: ["#7B77D1", "#F37EA1"],
Expand Down
24 changes: 12 additions & 12 deletions resources/js/Pages/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
</template>

<div class="px-5 mx-auto mt-5 mb-10 md:space-y-0 md:space-x-10 md:flex max-w-screen-2xl sm:px-6 lg:px-8">
<div class="mt-6 md:w-9/12">
<div class="mt-6 md:w-9/12 space-y-4">
<section class="flex flex-col md:flex-row md:space-x-4">
<BudgetTracker
class="md:w-8/12 w-full order-1 mt-2 md:mt-0"
Expand All @@ -127,22 +127,22 @@
:message="$t('dashboard.welcome')"
:username="user.name"
@section-click="selected=$event"
>
<ChartCurrentVsPrevious
v-if="selected=='expenses'"
class="w-full mt-4 md:mb-10 overflow-hidden bg-white rounded-lg"
:class="[cardShadow]"
:title="t('This month vs last month')"
ref="ComparisonRevenue"
:data="expenses"
/>
</BudgetTracker>
/>
<WeatherWidget class="md:w-4/12 md:order-1" />
</section>

<ChartCurrentVsPrevious
v-if="selected=='expenses'"
class="w-full md:mb-10 overflow-hidden bg-white rounded-lg"
:class="[cardShadow]"
:title="t('This month vs last month')"
ref="ComparisonRevenue"
:data="expenses"
/>

<section class="flex space-x-4">
<ChartComparison
class="w-full mt-4 md:mb-10 overflow-hidden bg-white rounded-lg"
class="w-full md:mb-10 overflow-hidden bg-white rounded-lg"
:class="[cardShadow]"
:title="$t('Spending summary')"
ref="ComparisonRevenue"
Expand Down
Loading
Loading